-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunMappingParallel_Z.py
executable file
·150 lines (128 loc) · 3.45 KB
/
runMappingParallel_Z.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
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
#!/usr/bin/env python
'''
Created on July 14th, 2015
@author: Nicola Pastorello
'''
import glob, os, numpy, time, sys
from sys import stdout
import multiprocessing as mp
from multiprocessing import Process
import platform
######################
# Parallel functions #
######################
def worker(input_queue, output_queue):
"""Start the code processes one after the other"""
while input_queue.empty() != True:
listPar = input_queue.get()
statusReal = job_chunk(listPar)
if statusReal:
output_queue.put(True)
else:
output_queue.put(False)
return
def job_chunk(listPar):
status = runOnGalaxy(listPar)
return status
def status(proc):
"""Check for processes status"""
if proc.is_alive==True:
return 'alive'
elif proc.is_alive==False:
return 'dead'
else:
return proc.is_alive()
def runOnGalaxy(listPar):
try:
import time
time0 = time.time()
#Reading old file
#print ii
# Create flag file (existing during work in progress)
open(listPar[1][:-25]+'/inProgress', 'a').close()
#
os.system('python '+listPar[1][:59]+'KrigingMapping_v4.py '+listPar[0]+' Z -f')
#
#
print "DONE with "+ii+" in "+str(round((time.time() - time0)/60.,2))+" minutes."
# os.remove(listPar[1][:-25]+'/inProgress')
# Create flag file (work done)
# open(listPar[1][:-25]+'/done', 'a').close()
#
return True
#IF ERRORS OCCURED
except:
#os.remove(listPar[1][:-25]+'/inProgress')
#
return False
########
# MAIN #
########
def mainRun():
listDirectories = glob.glob('NGC*'); galnames = []
for ii in listDirectories:
if not('old' in ii):
galnames.append(ii)
dicPathInput = {}
pathNick = os.getcwd()
#
if len(galnames) == 0:
print "ERROR, NO INPUT DIRECTORIES FOUND"
else:
for ii in galnames:
tmpPath = glob.glob(pathNick+'/'+ii+'/OutputMet_corr_CLEAN.txt')
dicPathInput[ii] = tmpPath[0]
#Removing galaxies already mapped ('done' flag file in directory)
for ii in dicPathInput.keys():
if ((len(glob.glob(ii+'/done')) > 0) or (ii in ['NGC4449', 'NGC5907'])):
dicPathInput.pop(ii, None)
if (len(glob.glob(pathNick+ii+'/inProgress')) > 0):
os.remove(pathNick+ii+'/inProgress')
#Let's parallel!
nproc = mp.cpu_count()
#
input_queue = mp.Queue()
output_queue = mp.Queue()
#
namegals = []
for ii in dicPathInput.keys():
# for ii in ['NGC3377']:
input_queue.put([ii, dicPathInput[ii]])
namegals.append(ii)
##
if platform.system() == 'Linux':
nElements = input_queue.qsize()
else:
nElements = len(dicPathInput.keys())
procs = [] # processes container
# Start the worker processes
#
for ii in range(nproc-1):
print "Process loop ", ii
procs.append(mp.Process(target=worker, args=(input_queue,output_queue,)))
#
for ii in procs:
ii.start()
#
for ii in procs:
print "Process ", ii," @ " , ii.pid, " is ", status(ii)
# Wait processes to finish
# while input_queue.empty() != True:
# time.sleep(10) # loose 10 seconds
counter = 0
while output_queue.qsize() != nElements:
stdout.write("\rDONE %i/%i %i" % (output_queue.qsize(),
nElements, counter))
stdout.flush()
counter += 1
time.sleep(30)
#
print "FAILED WITH THE GALAXIES:"
lenOutputQueue = output_queue.qsize()
if len(namegals) == lenOutputQueue:
for ii in numpy.arange(lenOutputQueue):
if not(output_queue.get()):
print namegals[ii]
return 'Completed!'
if __name__ == '__main__':
aa = mainRun()