@@ -35,16 +35,46 @@ function threadedsensitivities(react; odesolver=nothing, senssolver=nothing,
35
35
forwardsensitivities= true , forwarddiff= react. forwarddiff, modelingtoolkit= react. modelingtoolkit,
36
36
tau= react. tau)
37
37
38
- salist = generatesensitivityodes (react, sol)
39
38
40
39
# Parallelize the SA calculations
41
40
solutiondictionary = Dict ()
42
41
42
+ nthreads = Threads. nthreads ()
43
+ if nthreads > 1 # each thread needs its own Reactor
44
+ reacts = [deepcopy (react) for i in 1 : nthreads]
45
+ else
46
+ reacts = [react]
47
+ end
48
+
43
49
@threads for i in 1 : length (react. p)
44
- s = solve (salist[i], senssolver; senskwargs... )
50
+ if nthreads > 1
51
+ id = Threads. threadid ()
52
+ r = reacts[id]
53
+ else
54
+ r = react
55
+ end
56
+ jacy! (J:: Q2 ,y:: T ,p:: V ,t:: Q ) where {Q2,T,Q<: Real ,V} = jacobiany! (J,y,p,t,r. domain,r. interfaces,nothing )
57
+ jacp! (J:: Q2 ,y:: T ,p:: V ,t:: Q ) where {Q2,T,Q<: Real ,V} = jacobianp! (J,y,p,t,r. domain,r. interfaces,nothing )
58
+
59
+ function dsdt! (ds, s, local_params, t)
60
+ jy = zeros (length (r. y0), length (r. y0))
61
+ jp = zeros (length (r. y0), length (r. p))
62
+ y = sol (t)
63
+ jacy! (jy, y, r. p, t)
64
+ jacp! (jp, y, r. p, t)
65
+ @views @inbounds c = jp[:, i]
66
+ @inbounds ds .= jy * s .+ c
67
+ end
68
+
69
+ # Create list of ODEProblems for each batch of parameters
70
+
71
+ odefcn = ODEFunction (dsdt!)
72
+ prob = ODEProblem (odefcn, zeros (length (r. y0)),r. tspan,0 )
73
+ s = solve (prob, senssolver; senskwargs... )
45
74
solutiondictionary[i] = s
46
75
end
47
- bigsol = generatesenssolution (sol, solutiondictionary, reactsens. ode)
76
+
77
+ bigsol = generatesenssolution (sol,solutiondictionary,reactsens. ode)
48
78
return bigsol
49
79
end
50
80
@@ -83,53 +113,48 @@ function threadedsensitivities(react, paramindices; odesolver=nothing, senssolve
83
113
forwardsensitivities= true , forwarddiff= react. forwarddiff, modelingtoolkit= react. modelingtoolkit,
84
114
tau= react. tau)
85
115
86
- salist = generatesensitivityodes (react, sol)
87
116
88
117
# Parallelize the SA calculations
89
118
solutiondictionary = Dict ()
90
-
91
- @threads for i in paramindices
92
- s = solve (salist[i], senssolver; senskwargs... )
93
- solutiondictionary[i] = s
119
+ nthreads = Threads. nthreads ()
120
+ if nthreads > 1 # each thread needs its own Reactor
121
+ reacts = [deepcopy (react) for i in 1 : nthreads]
122
+ else
123
+ reacts = [react]
94
124
end
95
-
96
- return solutiondictionary
97
- end
98
-
99
- export threadedsensitivities
100
-
101
- """
102
- generate individual sensitivity ODEs for each parameter
103
- """
104
- function generatesensitivityodes (react, sol)
105
- sa_list = []
106
- y0 = react. y0
107
- tspan = react. tspan
108
- p = react. p
109
- for i in 1 : length (p)
110
- r = deepcopy (react)
111
- jacy! (J:: Q2 , y:: T , p:: V , t:: Q ) where {Q2,T,Q<: Real ,V} = jacobiany! (J, y, p, t, r. domain, r. interfaces, nothing )
112
- jacp! (J:: Q2 , y:: T , p:: V , t:: Q ) where {Q2,T,Q<: Real ,V} = jacobianp! (J, y, p, t, r. domain, r. interfaces, nothing )
113
-
125
+ @threads for i in paramindices
126
+ if nthreads > 1
127
+ id = Threads. threadid ()
128
+ r = reacts[id]
129
+ else
130
+ r = react
131
+ end
132
+ jacy! (J:: Q2 ,y:: T ,p:: V ,t:: Q ) where {Q2,T,Q<: Real ,V} = jacobiany! (J,y,p,t,r. domain,r. interfaces,nothing )
133
+ jacp! (J:: Q2 ,y:: T ,p:: V ,t:: Q ) where {Q2,T,Q<: Real ,V} = jacobianp! (J,y,p,t,r. domain,r. interfaces,nothing )
134
+
114
135
function dsdt! (ds, s, local_params, t)
115
- jy = zeros (length (y0), length (y0))
116
- jp = zeros (length (y0), length (p))
136
+ jy = zeros (length (r . y0), length (r . y0))
137
+ jp = zeros (length (r . y0), length (r . p))
117
138
y = sol (t)
118
- jacy! (jy, y, p, t)
119
- jacp! (jp, y, p, t)
139
+ jacy! (jy, y, r . p, t)
140
+ jacp! (jp, y, r . p, t)
120
141
@views @inbounds c = jp[:, i]
121
142
@inbounds ds .= jy * s .+ c
122
143
end
123
144
124
145
# Create list of ODEProblems for each batch of parameters
125
146
126
147
odefcn = ODEFunction (dsdt!)
127
- prob = ODEProblem (odefcn, zeros (length (y0)), tspan, 0 )
128
- push! (sa_list, prob)
148
+ prob = ODEProblem (odefcn, zeros (length (r. y0)),r. tspan,0 )
149
+ s = solve (prob, senssolver; senskwargs... )
150
+ solutiondictionary[i] = s
129
151
end
130
- return sa_list
152
+
153
+ return solutiondictionary
131
154
end
132
155
156
+ export threadedsensitivities
157
+
133
158
"""
134
159
Combine ODE solutions into a sensitivity solution
135
160
"""
0 commit comments