forked from cjnolet/text-renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitan_utils.py
55 lines (49 loc) · 1.64 KB
/
titan_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
import math
import os
TASK_ID = os.environ.get('SGE_TASK_ID')
LAST_TASK_ID = os.environ.get('SGE_TASK_LAST')
TASK_STEPSIZE = os.environ.get('SGE_TASK_STEPSIZE')
ISTITAN = os.environ.get('ISTITAN')
def is_cluster():
return not (ISTITAN is None)
def get_num_tasks():
if not TASK_ID:
return 1
return int(math.ceil(float(LAST_TASK_ID)/float(TASK_STEPSIZE)))
def get_task_id():
try:
return int(TASK_ID)
except TypeError:
return 1
def crange(in_range):
"""
split the range up equally amongst the tasks (tasks are alwayssequential e.g. 1-80:1)
"""
n_tasks = get_num_tasks()
if n_tasks == 1:
return in_range
if n_tasks < len(in_range):
split = math.floor(float(len(in_range))/float(n_tasks))
dist = [split for i in range(n_tasks)]
remainder = len(in_range) - (split*n_tasks)
# distribute this remainder across the first tasks
i = 0
while remainder > 0:
dist[i] += 1
remainder -= 1
i += 1
start_i = int(sum(dist[0:int(TASK_ID)-1]))
end_i = int(start_i + dist[int(TASK_ID)-1])
if int(TASK_ID) == n_tasks:
out_range = in_range[start_i:]
else:
out_range = in_range[start_i:end_i]
print 'Task %d of %d, split is %d of %d' % (int(TASK_ID), n_tasks, len(out_range), len(in_range))
return out_range
else:
# if less range than tasks then just process one on each task and quit the rest
if int(TASK_ID) <= len(in_range):
return [in_range[int(TASK_ID)-1]]
else:
print 'Nothing to process'
return []