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
|
# File 'lib/rbbt/hpc.rb', line 15
def self.template(args, options = {})
development = options.delete :drbbt
contain = options.delete :contain
sync = options.delete :sync
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
if contain_and_sync
contain = "/scratch/tmp/rbbt-${USER}" if contain.nil?
sync = "~/.rbbt/var/jobs" if sync.nil?
end
contain = File.expand_path(contain) if contain
singularity = true if contain || ! development
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')
=<<-EOF
#!/bin/bash
#SBATCH --qos="#{queue}"
#SBATCH --job-name="#{name}"
#SBATCH --workdir="#{Dir.pwd}"
#SBATCH --output="#{fout}"
#SBATCH --error="#{ferr}"
#SBATCH --cpus-per-task="#{task_cpus}"
#SBATCH --time="#{time}"
#SBATCH --nodes="#{nodes}"
EOF
if highmem
+=<<-EOF
#SBATCH --constraint=highmem
EOF
end
if exclusive
+=<<-EOF
#SBATCH --exclusive
EOF
end
+=<<-EOF
#CMD: #{rbbt_cmd}
EOF
env = ""
env +=<<-EOF
# Prepare env
[[ -f ~/config/load.sh ]] && source ~/config/load.sh
module load java
# Calculate max available memory
let "MAX_MEMORY=$SLURM_MEM_PER_CPU * $SLURM_CPUS_ON_NODE"
EOF
if singularity
env +=<<-EOF
module load intel/2018.1
module load singularity
PROJECTS_ROOT="/gpfs/projects/bsc26/"
SINGULARITY_IMG="$PROJECTS_ROOT/rbbt.singularity.img"
SINGULARITY_OPT_DIR="$PROJECTS_ROOT/singularity_opt/"
SINGULARITY_RUBY_INLINE="$HOME/.singularity_ruby_inline"
mkdir -p "$SINGULARITY_RUBY_INLINE"
EOF
end
if contain
user = ENV['USER'] || `whoami`.strip
group = File.basename(File.dirname(ENV['HOME']))
scratch_group_dir = File.join('/gpfs/scratch/', group)
projects_group_dir = File.join('/gpfs/projects/', group)
env +=<<-EOF
# Prepare container dir
CONTAINER_DIR="#{contain}"
mkdir -p $CONTAINER_DIR/.rbbt/etc/
for dir in .ruby_inline git home; do
mkdir -p $CONTAINER_DIR/$dir
done
for tmpd in persist_locks produce_locks R_sockets sensiblewrite sensiblewrite_locks step_info_locks tsv_open_locks; do
mkdir -p $CONTAINER_DIR/.rbbt/tmp/$tmpd
done
# Copy environment
cp ~/.rbbt/etc/environment $CONTAINER_DIR/.rbbt/etc/
# Set search_paths
echo "rbbt_user: /home/rbbt/.rbbt/{TOPLEVEL}/{SUBPATH}" > $CONTAINER_DIR/.rbbt/etc/search_paths
echo "home: $CONTAINER_DIR/home/{TOPLEVEL}/{SUBPATH}" >> $CONTAINER_DIR/.rbbt/etc/search_paths
echo "group_projects: $CONTAINER_DIR/projects/{PKGDIR}/{TOPLEVEL}/{SUBPATH}" >> $CONTAINER_DIR/.rbbt/etc/search_paths
echo "group_scratch: $CONTAINER_DIR/scratch/{PKGDIR}/{TOPLEVEL}/{SUBPATH}" >> $CONTAINER_DIR/.rbbt/etc/search_paths
echo "user_projects: $CONTAINER_DIR/projects/#{user}/{PKGDIR}/{TOPLEVEL}/{SUBPATH}" >> $CONTAINER_DIR/.rbbt/etc/search_paths
echo "user_scratch: $CONTAINER_DIR/scratch/#{user}/{PKGDIR}/{TOPLEVEL}/{SUBPATH}" >> $CONTAINER_DIR/.rbbt/etc/search_paths
echo "$CONTAINER_DIR/projects/rbbt/workflows/" > $CONTAINER_DIR/.rbbt/etc/workflow_dir
[[ -a "$CONTAINER_DIR/projects" ]] || ln -s '#{projects_group_dir}' "$CONTAINER_DIR/projects"
[[ -a "$CONTAINER_DIR/scratch" ]] || ln -s '#{scratch_group_dir}' "$CONTAINER_DIR/scratch"
EOF
if inputs_dir
env +=<<-EOF
# Copy inputs
[[ -d '#{inputs_dir}' ]] && cp -R '#{inputs_dir}' $CONTAINER_DIR/inputs
EOF
rbbt_cmd = rbbt_cmd.sub(inputs_dir, "#{contain}/inputs")
end
if copy_image
env +=<<EOF
# Copy image
rsync -avz "$SINGULARITY_IMG" "$CONTAINER_DIR/rbbt.singularity.img" 1>&2
SINGULARITY_IMG="$CONTAINER_DIR/rbbt.singularity.img"
EOF
end
if wipe_container == "pre" || wipe_container == "both"
env +=<<-EOF
# Clean container pre
singularity exec -e -C -H "$CONTAINER_DIR" "$SINGULARITY_IMG" rm -Rfv .rbbt/var/jobs &>> #{fsync}
singularity exec -e -C -H "$CONTAINER_DIR" "$SINGULARITY_IMG" rbbt system clean -f &>> #{fsync}
singularity exec -e -C -H "$CONTAINER_DIR" "$SINGULARITY_IMG" rm -Rfv tmp/ &>> #{fsync}
EOF
end
end
run = ""
exec_cmd = %(env _JAVA_OPTIONS="-Xms1g -Xmx${MAX_MEMORY}m")
if singularity
singularity_exec = %(singularity exec -e -B $SINGULARITY_OPT_DIR:/singularity_opt/ -B /apps/)
if contain
singularity_exec << %( -C -H "$CONTAINER_DIR" \
-B /scratch/tmp \
-B "$SINGULARITY_RUBY_INLINE":"$CONTAINER_DIR/.ruby_inline":rw \
-B ~/git:"$CONTAINER_DIR/git":ro \
-B ~/.rbbt/software/opt/:"/opt/":ro \
-B ~/.rbbt:"$CONTAINER_DIR/home/":ro \
-B #{scratch_group_dir} \
-B #{projects_group_dir} \
"$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=git'
else
exec_cmd += ' rbbt'
end
exec_cmd = singularity_exec + " " + exec_cmd
else
exec_cmd << %(~/git/rbbt-util/bin/rbbt --dev=~/git/)
end
cmd =<<-EOF
#{exec_cmd} \\
#{rbbt_cmd}
EOF
run +=<<-EOF
# Run command
#{cmd}
# Save exit status
exit_status=$?
# Clean job.id, since we are done
rm #{fjob}
EOF
coda = ""
if sync
if singularity
coda +=<<-EOF
singularity exec -e -C -H "$CONTAINER_DIR" "$SINGULARITY_IMG" rbbt system clean all -q &>> #{fsync}
EOF
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 +=<<-EOF
rsync -avt "#{source}/" "#{target}/" &>> #{fsync}
sync_es="$?"
find '#{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
EOF
if contain && (wipe_container == "post" || wipe_container == "both")
coda +=<<-EOF
singularity exec -e -C -H "$CONTAINER_DIR" "$SINGULARITY_IMG" rbbt system clean -f &>> #{fsync}
singularity 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}
if [ $sync_es == '0' ]; then
singularity exec -e -C -H "$CONTAINER_DIR" "$SINGULARITY_IMG" rm -Rfv .rbbt/var/jobs &>> #{fsync}
singularity exec -e -C -H "$CONTAINER_DIR" "$SINGULARITY_IMG" rm -Rfv tmp/ &>> #{fsync}
else
echo "WARNING: Results could not sync correctly. Job directory not purged"
fi
unset sync_es
EOF
end
end
coda +=<<-EOF
# Write exit status to file
echo $exit_status > #{fexit}
unset exit_status
EOF
template = [, env, run, coda] * "\n"
template
end
|