Skip to content

Instantly share code, notes, and snippets.

@iwiwi
Last active April 27, 2016 06:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwiwi/5337010 to your computer and use it in GitHub Desktop.
Save iwiwi/5337010 to your computer and use it in GitHub Desktop.
Torque job machine-gun
#!/usr/bin/env ruby
#
# qsub.rb --- Torque job machine-gun
#
# Usage:
# 1. Rewrite the shell script template & parameters
# 2. Execute
# $ qsub.rb (cat) --> Output generated shell scripts
# $ qsub.rb sh --> Execute locally
# $ qsub.rb qsub --> Enqueue onto Torque
#
# Author:
# Takuya Akiba (@iwiwi)
# http://www-imai.is.s.u-tokyo.ac.jp/~takiba/index_e.html
#
def main
qsub({
:file => ["a.txt", "b.txt", "c.txt"],
:parameter => [1, 2],
}) do <<-EOS
#!/bin/sh
#PBS -j oe
#PBS -l nodes=1:ppn=12
#PBS -M csc_torque@twittermail.com
#PBS -mb -me
#PBS -N #{$parameter},#{File.basename($file)}
ulimit -t 86400
ulimit -v 41943040
cd #{File.dirname(File.expand_path(__FILE__))}
echo #{$file} #{$parameter}
EOS
end
end
def qsub(args = {})
system("mkdir -p tmp")
def rec(i, args, env)
if i < args.length
name = args[i][0].to_s
values = args[i][1].is_a?(Array) ? args[i][1] : [args[i][1]]
values.each do |v|
env[i] = [name, v]
rec(i + 1, args, env) { yield(env) }
end
else
yield(env)
end
end
rec(0, args.to_a, []) do |env|
open("tmp/tmp.sh", "w") do |f|
eval(env.map{|k, v| "$#{k} = '#{v.to_s}'\n"}.join(""))
f.puts(yield)
end
cmd = ARGV.length == 0 ? "cat" : ARGV[0]
sh = ""
if cmd == "qsub"
jobname = env.map do |a|
if a[1].is_a?(String)
s = File.basename(a[1].to_s)
s[0, [s.length, 4].min]
else
a[1].to_s
end
end.join(",")
sh = "cd tmp; qsub tmp.sh; cd ../"
else
sh = "#{cmd} tmp/tmp.sh"
end
system("#{sh}")
end
end
if __FILE__ == $0
main
end
@iwiwi
Copy link
Author

iwiwi commented Jun 13, 2013

History

  • 2013/02/11: Ver.0.01
  • 2013/02/18: Ver.0.02 --- job title
  • 2013/04/08: Ver.0.03 --- log directory
  • 2013/06/13: Ver.0.04 --- cleanup
  • 2013/07/05: Ver.0.05 --- ulimit, job title

@iwiwi
Copy link
Author

iwiwi commented Jun 13, 2013

FAQ

  • Q: アレイジョブ知らないんですか?
  • A: アレイジョブでは満足できないから作りました.複数のパラメータの組合せを網羅的に実行することが簡単にできますし,パラメータを増やしたり減らしたりも手軽にできます.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment