15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
# File 'lib/rbbt/hpc.rb', line 15
def self.template(args, options = {})
development = options.delete :drbbt
singularity = options.delete :singularity
contain = options.delete :contain
sync = options.delete :sync
user_group = options.delete :user_group
contain_and_sync = options.delete :contain_and_sync
wipe_container = options.delete :wipe_container
copy_image = options.delete :copy_image
exclusive = options.delete :exclusive
highmem = options.delete :highmem
queue = options.delete(:queue) || 'bsc_ls'
task_cpus = options.delete(:task_cpus) || 1
nodes = options.delete(:nodes) || 1
time = options.delete(:time) || "0:00:10"
inputs_dir = options.delete :inputs_dir
config_keys = options.delete :config_keys
user = ENV['USER'] || `whoami`.strip
group = File.basename(File.dirname(ENV['HOME']))
if contain_and_sync
contain = "/scratch/tmp/rbbt-#{user}" if contain.nil?
sync = "~/.rbbt/var/jobs" if sync.nil?
wipe_container = "post" if wipe_container.nil?
end
contain = nil if contain == "" || contain == "none"
sync = nil if sync == "" || sync == "none"
contain = File.expand_path(contain) if contain
name = options[:name] ||= Misc.obj2digest({:options => options.collect{|k,v| [k,v]}.sort_by{|k,v| k.to_s }, :args => args})
options.delete(:name)
slurm_basedir = options[:slurm_basedir] ||= File.expand_path(File.join('~/rbbt-slurm', name)) if slurm_basedir.nil?
options.delete(:slurm_basedir)
rbbt_cmd = args.reject{|e| e == '--' }.collect{|e| e.include?(" ")? '"' + e + '"' : e } * " "
rbbt_cmd += " " << options.collect do |o,v|
o = o.to_s
case v
when TrueClass
'--' << o
when FalseClass
'--' << o << "=false"
else
['--' << o, "'#{v}'"] * " "
end
end * " "
rbbt_cmd << " --config_keys='#{config_keys}'" if config_keys and not config_keys.empty?
time = Misc.format_seconds Misc.timespan(time) unless time.include? ":"
Open.mkdir slurm_basedir
fout = File.join(slurm_basedir, 'std.out')
ferr = File.join(slurm_basedir, 'std.err')
fjob = File.join(slurm_basedir, 'job.id')
fexit = File.join(slurm_basedir, 'exit.status')
fsync = File.join(slurm_basedir, 'sync.log')
fcmd = File.join(slurm_basedir, 'command.slurm')
="#!/bin/bash\n#SBATCH --qos=\"\#{queue}\"\n#SBATCH --job-name=\"\#{name}\"\n#SBATCH --workdir=\"\#{Dir.pwd}\"\n#SBATCH --output=\"\#{fout}\"\n#SBATCH --error=\"\#{ferr}\"\n#SBATCH --cpus-per-task=\"\#{task_cpus}\"\n#SBATCH --time=\"\#{time}\"\n#SBATCH --nodes=\"\#{nodes}\"\n EOF\n\n prep = \"\"\n\n if highmem\n header +=<<-EOF\n#SBATCH --constraint=highmem \n EOF\n end\n\n if exclusive\n header +=<<-EOF\n#SBATCH --exclusive \n EOF\n end\n\n header +=<<-EOF\n#CMD: \#{rbbt_cmd}\n EOF\n\n # ENV\n env = \"\"\n env +=<<-EOF\n# Prepare env\n[[ -f ~/config/load.sh ]] && source ~/config/load.sh\nmodule load java\n\n# Calculate max available memory\nlet \"MAX_MEMORY=$SLURM_MEM_PER_CPU * $SLURM_CPUS_PER_TASK\"\n EOF\n\n\n # RUN\n run = \"\"\n exec_cmd = %(env _JAVA_OPTIONS=\"-Xms1g -Xmx${MAX_MEMORY}m\")\n\n\n if singularity\n \#{{{ SINGULARITY\n \n singularity_exec = %(singularity exec -e -B $SINGULARITY_OPT_DIR:/singularity_opt/ -B /apps/)\n \n env +=<<-EOF\nmodule load intel/2018.1\nmodule load singularity\nPROJECTS_ROOT=\"/gpfs/projects/bsc26/\"\nSINGULARITY_IMG=\"$PROJECTS_ROOT/rbbt.singularity.img\"\nSINGULARITY_OPT_DIR=\"$PROJECTS_ROOT/singularity_opt/\"\nSINGULARITY_RUBY_INLINE=\"$HOME/.singularity_ruby_inline\"\nmkdir -p \"$SINGULARITY_RUBY_INLINE\"\n EOF\n\n if contain\n scratch_group_dir = File.join('/gpfs/scratch/', group)\n projects_group_dir = File.join('/gpfs/projects/', group)\n\n prep +=<<-EOF\n\n# Prepare container dir\nCONTAINER_DIR=\"\#{contain}\"\nmkdir -p $CONTAINER_DIR/.rbbt/etc/\n\nfor dir in .ruby_inline git home; do\nmkdir -p $CONTAINER_DIR/$dir\ndone\n\nfor tmpd in persist_locks produce_locks R_sockets sensiblewrite sensiblewrite_locks step_info_locks tsv_open_locks; do\nmkdir -p $CONTAINER_DIR/.rbbt/tmp/$tmpd\ndone\n\n# Copy environment \ncp ~/.rbbt/etc/environment $CONTAINER_DIR/.rbbt/etc/\n\n# Set search_paths\necho \"singularity: /singularity_opt/{PKGDIR}/{TOPLEVEL}/{SUBPATH}\" > $CONTAINER_DIR/.rbbt/etc/search_paths\necho \"rbbt_user: /home/rbbt/.rbbt/{TOPLEVEL}/{SUBPATH}\" >> $CONTAINER_DIR/.rbbt/etc/search_paths\necho \"outside_home: $CONTAINER_DIR/home/{TOPLEVEL}/{SUBPATH}\" >> $CONTAINER_DIR/.rbbt/etc/search_paths\necho \"group_projects: \#{projects_group_dir}/{PKGDIR}/{TOPLEVEL}/{SUBPATH}\" >> $CONTAINER_DIR/.rbbt/etc/search_paths\necho \"group_scratch: \#{scratch_group_dir}/{PKGDIR}/{TOPLEVEL}/{SUBPATH}\" >> $CONTAINER_DIR/.rbbt/etc/search_paths\necho \"user_projects: \#{projects_group_dir}/\#{user}/{PKGDIR}/{TOPLEVEL}/{SUBPATH}\" >> $CONTAINER_DIR/.rbbt/etc/search_paths\necho \"user_scratch: \#{scratch_group_dir}/\#{user}/{PKGDIR}/{TOPLEVEL}/{SUBPATH}\" >> $CONTAINER_DIR/.rbbt/etc/search_paths\n EOF\n\n if user_group && group != user_group\n prep +=<<-EOF\n\n# Add user_group search_path\necho \"\#{user_group}: /gpfs/projects/\#{user_group}/{PKGDIR}/{TOPLEVEL}/{SUBPATH}\" >> $CONTAINER_DIR/.rbbt/etc/search_paths\n EOF\n end\n\n if inputs_dir\n prep +=<<-EOF\n\n# Copy inputs\n[[ -d '\#{inputs_dir}' ]] && cp -R '\#{inputs_dir}' $CONTAINER_DIR/inputs\n EOF\n rbbt_cmd = rbbt_cmd.sub(inputs_dir, \"\#{contain}/inputs\")\n end\n\n if copy_image\n prep +=<<EOF\n\n# Copy image\nrsync -avz \"$SINGULARITY_IMG\" \"$CONTAINER_DIR/rbbt.singularity.img\" 1>&2\nSINGULARITY_IMG=\"$CONTAINER_DIR/rbbt.singularity.img\"\n"
end
if wipe_container == "pre" || wipe_container == "both"
if singularity
prep +="\n# Clean container pre\nsingularity exec -e -C -H \"$CONTAINER_DIR\" \"$SINGULARITY_IMG\" rm -Rfv .rbbt/var/jobs &>> \#{fsync}\nsingularity exec -e -C -H \"$CONTAINER_DIR\" \"$SINGULARITY_IMG\" rbbt system clean -f &>> \#{fsync}\nsingularity exec -e -C -H \"$CONTAINER_DIR\" \"$SINGULARITY_IMG\" rm -Rfv tmp/ &>> \#{fsync}\n"
else
prep = ""
end
end
end
if contain
singularity_exec << %( -C -H "$CONTAINER_DIR" \
-B /scratch/tmp \
#{ group != user_group ? "-B /gpfs/projects/#{user_group}" : "" } \
-B #{scratch_group_dir} \
-B #{projects_group_dir} \
-B "$SINGULARITY_RUBY_INLINE":"$CONTAINER_DIR/.ruby_inline":rw \
-B ~/git:"$CONTAINER_DIR/git":ro \
#{Open.exists?('~/.rbbt/software/opt/')? '-B ~/.rbbt/software/opt/:"/opt/":ro' : '' } \
-B ~/.rbbt:"$CONTAINER_DIR/home/":ro \
"$SINGULARITY_IMG")
exec_cmd << ' TMPDIR="$CONTAINER_DIR/.rbbt/tmp" '
else
singularity_exec += %( -B "$SINGULARITY_RUBY_INLINE":"$HOME/.ruby_inline":rw "$SINGULARITY_IMG" )
end
if development
exec_cmd += " rbbt --dev='#{development}'"
else
exec_cmd += ' rbbt'
end
exec_cmd = singularity_exec + " " + exec_cmd
else
if development
exec_cmd << " " << %(~/git/rbbt-util/bin/rbbt --dev=#{development})
else
exec_cmd << " " << 'rbbt'
end
if contain
rbbt_cmd << " " << %(--workdir_all='#{contain}')
end
end
cmd ="\#{exec_cmd} \\\\\n\#{rbbt_cmd}\n"
run +="\n# Run command\n\#{cmd}\n\n# Save exit status\nexit_status=$?\n\n"
coda = ""
if sync
if singularity
coda +="singularity exec -e -C -H \"$CONTAINER_DIR\" \"$SINGULARITY_IMG\" rbbt system clean all -q &>> \#{fsync}\n"
else
coda +="rbbt system clean all -q &>> \#{fsync}\n"
end
if sync.include?("=>")
source, _sep, sync = sync.partition("=>")
source = source.strip
sync = sync.strip
source = File.join(File.expand_path(contain), source)
else
source = File.join(File.expand_path(contain), '.rbbt/var/jobs')
end
target = File.expand_path(sync)
coda +="\n# Sync data to target location\nmkdir -p \"$(dirname '\#{target}')\"\nrsync -avztAXHP --copy-unsafe-links \"\#{source}/\" \"\#{target}/\" &>> \#{fsync} \nsync_es=\"$?\" \nfind '\#{target}' -type l -ls | awk '$13 ~ /^\#{target.gsub('/','\\/')}/ { sub(\"\#{source}\", \"\#{target}\", $13); print $11, $13 }' | while read A B; do rm $A; ln -s $B $A; done\n"
if contain && (wipe_container == "post" || wipe_container == "both")
prep ="if ls -A '\#{contain}' &> /dev/null ; then\necho \"ERROR: Container directory not empty, refusing to wipe. \#{contain}\" &>> \#{fsync}\nfi\n EOF\n if singularity\n coda +=<<-EOF\nsingularity exec -e -C -H \"$CONTAINER_DIR\" \"$SINGULARITY_IMG\" rm -v /dev/shm/sem.*.{in,out,process} /dev/shm/sem.Session-PID.*.sem 2> /dev/null >> \#{fsync}\n\n\n# Clean container directory\n#if [ $exit_status == '0' -a $sync_es == '0' ]; then \nsingularity exec -e -C -H \"$CONTAINER_DIR\" \"$SINGULARITY_IMG\" rbbt system clean -f &>> \#{fsync}\nsingularity exec -e -C -H \"$CONTAINER_DIR\" \"$SINGULARITY_IMG\" rm -Rfv .rbbt/var/jobs &>> \#{fsync}\nsingularity exec -e -C -H \"$CONTAINER_DIR\" \"$SINGULARITY_IMG\" rm -Rfv tmp/ &>> \#{fsync}\n#else\n# echo \"ERROR: Process failed or results could not sync correctly. Contain directory not purged\" &>> \#{fsync}\n#fi\n" + prep
else
coda +="\#{exec_cmd} system clean\nif [ $exit_status == '0' -a $sync_es == '0' ]; then \nrm -Rfv \#{contain} &>> \#{fsync}\nelse\necho \"ERROR: Process failed or results could not sync correctly. Contain directory not purged\" &>> \#{fsync}\nfi\nunset sync_es\n"
end
end
end
coda +="\n# Write exit status to file\necho $exit_status > \#{fexit}\n"
if sync
coda +="if [ \"$sync_es\" == '0' ]; then \n unset sync_es\n exit $exit_status\nelse\n exit $sync_es\nfi\n"
else
coda +="exit $exit_status\n"
end
template = [, env, prep, run, coda] * "\n"
template
end
|