7
7
8
8
# -------------------------------------------------------------------------------------------
9
9
"""
10
- shareLoad(nModels, nMatlab, verbose )
10
+ shareLoad(nModels, nMatlab, printLevel )
11
11
12
12
Function shares the number of `nModels` across `nMatlab` sessions (Euclidian division)
13
13
@@ -17,8 +17,9 @@ Function shares the number of `nModels` across `nMatlab` sessions (Euclidian div
17
17
18
18
# OPTIONAL INPUTS
19
19
20
- - `nMatlab`: Number of desired MATLAB sessions (default: 2)
21
- - `verbose`: Verbose mode, set `false` for quiet load sharing (default: true)
20
+ - `nMatlab`: Number of desired MATLAB sessions (default: 2)
21
+ - `printLevel`: Verbose level (default: 1). Mute all output with `printLevel = 0`.
22
+ - `dryRun`: Test load sharing without changing the parpool (default: false)
22
23
23
24
# OUTPUTS
24
25
@@ -35,71 +36,79 @@ julia> shareLoad(nModels)
35
36
36
37
- Determination of the load of 4 models in 2 MATLAB sessions
37
38
```julia
38
- julia> shareLoad(4, 2, false )
39
+ julia> shareLoad(4, 2, 1 )
39
40
```
40
41
41
42
See also: `createPool()` and `PALM`
42
43
43
44
"""
44
45
45
- function shareLoad (nModels:: Int , nMatlab:: Int = 2 , verbose:: Bool = true )
46
+ function shareLoad (nModels:: Int , nMatlab:: Int = 2 , printLevel:: Int = 1 , dryRun:: Bool = false )
47
+
48
+ if printLevel > 0 && dryRun
49
+ info (" Load sharing is determined without actively changing the number of connected workers (dryRun = true)." )
50
+ end
46
51
47
52
# Make sure that not more processes are launched than there are models (load ratio >= 1)
48
53
if nMatlab > nModels
49
- if verbose
54
+ if printLevel > 0
50
55
warn (" Number of workers ($nMatlab ) exceeds the number of models ($nModels )." )
51
56
end
52
57
53
58
nMatlab = nModels
54
59
55
60
# remove the last workers in the pool
56
- for k = length (workers ()): - 1 : nModels
57
- rmprocs (k)
61
+ if ! dryRun
62
+ for k = length (workers ()): - 1 : nModels
63
+ rmprocs (k)
64
+ end
58
65
end
59
66
60
- if verbose
67
+ if printLevel > 0
61
68
warn (" Number of workers reduced to number of models for ideal load distribution.\n " )
62
69
end
63
70
end
64
71
65
72
# Definition of workers and load distribution
66
- wrks = workers ()
73
+ if dryRun
74
+ wrks = [1 : nMatlab;]
75
+ else
76
+ wrks = workers ()
77
+ end
67
78
nWorkers = length (wrks)
68
79
quotientModels = Int (ceil (nModels / nWorkers))
69
80
70
81
remainderModels = 0
71
82
72
83
if nModels% nWorkers > 0
73
- if verbose
74
- println (" >> Every worker (#" , wrks[1 ], " - #" , wrks[end - 1 ], " ) will solve " , quotientModels, " model(s)." )
84
+ if printLevel > 0
85
+ println (" >> Every worker (#" , wrks[1 ], " - #" , wrks[end - 1 ], " ) will run (at least) " , quotientModels, " model(s)." )
75
86
end
76
87
77
- # remainderModels = Int(nModels - (nWorkers) * quotientModels)
78
88
remainderModels = Int (nModels - (nWorkers - 1 ) * quotientModels)
79
89
if remainderModels > 0
80
- if verbose
81
- # println(" >> Worker #", wrks[end-1], " will solve ", quotientModels + remainderModels, " model(s).")
90
+ if printLevel > 0
82
91
println (" >> Worker #" , wrks[end - 1 ], " will solve " , quotientModels + remainderModels, " model(s)." )
83
92
end
84
93
end
85
94
86
95
if quotientModels < remainderModels - 1 || remainderModels < 1
87
- if verbose
96
+ if printLevel > 0
88
97
print_with_color (:red , " \n >> Load sharing is not fair. Consider adjusting the maximum poolsize.\n " )
89
98
end
90
99
else
91
- if verbose
100
+ if printLevel > 0
92
101
print_with_color (:yellow , " \n >> Load sharing is almost ideal.\n " )
93
102
end
94
103
end
95
104
else
96
- if verbose
105
+ if printLevel > 0
97
106
println (" >> Every worker will run " , quotientModels, " model(s)." )
98
107
print_with_color (:green , " >> Load sharing is ideal.\n " )
99
108
end
100
109
end
101
110
102
- if verbose
111
+ if printLevel > 0
103
112
println (" \n -- Load distribution --\n " )
104
113
println (" - Number of models: $nModels " )
105
114
println (" - Number of workers: $nWorkers " )
113
122
114
123
# -------------------------------------------------------------------------------------------
115
124
"""
116
- loopModels(dir, p, scriptName, dirContent, startIndex, endIndex, varsCharact)
125
+ loopModels(dir, p, scriptName, dirContent, startIndex, endIndex, varsCharact, printLevel )
117
126
118
127
Function `loopModels` is generally called in a loop from `PALM()` on worker `p`.
119
128
Runs `scriptName` for all models with an index in `dirContent` between `startIndex` and `endIndex`.
@@ -130,6 +139,10 @@ is computed as `nModels = endIndex - startIndex + 1`.
130
139
- `endIndex`: Index of the last model in `dirContent` to be used on worker `p`
131
140
- `varsCharact`: Array with the names of variables to be retrieved from the MATLAB session on worker `p`
132
141
142
+ # OPTIONAL INPUTS
143
+
144
+ - `printLevel`: Verbose level (default: 1). Mute all output with `printLevel = 0`.
145
+
133
146
# OUTPUTS
134
147
135
148
- `data`: Mixed array of variables retrieved from worker p (rows: models, columns: variables).
@@ -146,37 +159,39 @@ See also: `PALM()`
146
159
147
160
"""
148
161
149
- function loopModels (dir, p, scriptName, dirContent, startIndex, endIndex, varsCharact, localnModels)
162
+ function loopModels (dir, p, scriptName, dirContent, startIndex, endIndex, varsCharact, localnModels, printLevel :: Int = 1 )
150
163
151
164
# determine the lengt of the number of variables
152
165
nCharacteristics = length (varsCharact)
153
166
154
167
# local nModels
155
168
if endIndex >= startIndex
156
- # nModels = endIndex - startIndex + 1
157
-
158
169
# declaration of local data array
159
170
data = Array {Union{Int,Float64,AbstractString}} (localnModels, nCharacteristics + 1 )
160
171
161
172
for k = 1 : localnModels
162
173
PALM_iModel = k # + (p - 1) * nModels
163
174
PALM_modelFile = dirContent[startIndex+ k- 1 ]
164
175
PALM_dir = dir
176
+ PALM_printLevel = printLevel
165
177
166
178
# save the modelName
167
179
data[k, 1 ] = PALM_modelFile
168
180
169
181
MATLAB. @mput PALM_iModel
170
182
MATLAB. @mput PALM_modelFile
171
183
MATLAB. @mput PALM_dir
184
+ MATLAB. @mput PALM_printLevel
172
185
eval (parse (" MATLAB.mat\" run('$scriptName ')\" " ))
173
186
174
187
for i = 1 : nCharacteristics
175
188
data[k, i + 1 ] = MATLAB. get_variable (Symbol (varsCharact[i]))
176
189
end
177
190
end
178
191
179
- @show data
192
+ if printLevel > 0
193
+ @show data
194
+ end
180
195
181
196
return data
182
197
else
186
201
187
202
# -------------------------------------------------------------------------------------------
188
203
"""
189
- PALM(dir, scriptName, nMatlab, outputFile, cobraToolboxDir)
204
+ PALM(dir, scriptName, nMatlab, outputFile, cobraToolboxDir, printLevel )
190
205
191
206
Function reads the directory `dir`, and launches `nMatlab` sessions to run `scriptName`.
192
207
Results are saved in the `outputFile`.
@@ -201,6 +216,7 @@ Results are saved in the `outputFile`.
201
216
- `nMatlab`: Number of desired MATLAB sessions (default: 2)
202
217
- `outputFile`: Name of `.mat` file to save the result table named "summaryData" (default name: "PALM_data.mat")
203
218
- `cobraToolboxDir`: Directory of the COBRA Toolbox (default: "~/cobratoolbox")
219
+ - `printLevel`: Verbose level (default: 1). Mute all output with `printLevel = 0`.
204
220
205
221
# OUTPUTS
206
222
@@ -222,12 +238,14 @@ See also: `loopModels()` and `shareLoad()`
222
238
223
239
"""
224
240
225
- function PALM (dir, scriptName, nMatlab:: Int = 2 , outputFile:: AbstractString = " PALM_data.mat" , varsCharact= [], cobraToolboxDir= ENV [" HOME" ]* Base. Filesystem. path_separator* " cobratoolbox" )
241
+ function PALM (dir, scriptName, nMatlab:: Int = 2 , outputFile:: AbstractString = " PALM_data.mat" , varsCharact= [], cobraToolboxDir= ENV [" HOME" ]* Base. Filesystem. path_separator* " cobratoolbox" , printLevel :: Int = 1 )
226
242
227
243
# read the content of the directory
228
244
dirContent = readdir (dir)
229
245
230
- info (" Directory with $(length (dirContent)) models read successfully." )
246
+ if printLevel > 0
247
+ info (" Directory with $(length (dirContent)) models read successfully." )
248
+ end
231
249
232
250
nWorkers, quotientModels, remainderModels = shareLoad (length (dirContent), nMatlab)
233
251
@@ -263,22 +281,27 @@ function PALM(dir, scriptName, nMatlab::Int=2, outputFile::AbstractString="PALM_
263
281
# launch the function loopModels on every worker
264
282
@sync for (p, pid) in enumerate (workers ())
265
283
266
- info (" Launching MATLAB session on worker $(p+ 1 ) ." )
284
+ if printLevel > 0
285
+ info (" Launching MATLAB session on worker $(p+ 1 ) ." )
286
+ end
267
287
288
+ # adding the model directory and eventual subdirectories to the MATLAB path
289
+ # Note: the fileseparator `/` also works on Windows systems if git Bash has been installed
268
290
@async R[p] = @spawnat (p+ 1 ) begin
269
- # adding the model directory and eventual subdirectories to the MATLAB path
270
291
eval (parse (" mat\" addpath(genpath('/tmp/test-ct-$p '))\" " ))
271
- eval (parse (" mat\" run('/tmp/test-ct-$p /initCobraToolbox.m');\" " )) # *Base.Filesystem.path_separator*
272
- # add the path with the models
273
- # eval(parse("mat\"addpath('$dir');\""))
292
+ eval (parse (" mat\" run('/tmp/test-ct-$p /initCobraToolbox.m');\" " ))
274
293
end
275
294
276
295
end
277
296
278
- info (" > MATLAB sessions initializing" )
297
+ # print an informative message
298
+ if printLevel > 0
299
+ info (" > MATLAB sessions initializing" )
300
+ end
279
301
280
302
@sync for (p, pid) in enumerate (workers ())
281
303
304
+ # determine the starting index
282
305
startIndex = Int ((p - 1 ) * quotientModels + 1 )
283
306
284
307
# save the startIndex for each worker
@@ -292,8 +315,9 @@ function PALM(dir, scriptName, nMatlab::Int=2, outputFile::AbstractString="PALM_
292
315
293
316
localnModels = quotientModels
294
317
295
- info (" (case1): Worker $(p+ 1 ) runs $localnModels models: from $startIndex to $endIndex " )
296
-
318
+ if printLevel > 0
319
+ info (" (case1): Worker $(p+ 1 ) runs $localnModels models: from $startIndex to $endIndex " )
320
+ end
297
321
else
298
322
endIndex = Int ((p+ 1 ) * quotientModels + remainderModels)
299
323
@@ -306,8 +330,9 @@ function PALM(dir, scriptName, nMatlab::Int=2, outputFile::AbstractString="PALM_
306
330
307
331
localnModels = endIndex - startIndex + 1
308
332
309
- info (" (case 2): Worker $(p+ 1 ) runs $localnModels models: from $startIndex to $endIndex " )
310
-
333
+ if printLevel > 0
334
+ info (" (case 2): Worker $(p+ 1 ) runs $localnModels models: from $startIndex to $endIndex " )
335
+ end
311
336
end
312
337
313
338
@async R[p] = @spawnat (p+ 1 ) begin
0 commit comments