Commit 37b391fe authored by RODIONOV Sergey's avatar RODIONOV Sergey

add run.sh's

parent 933c800b
qsub -t 1-20 batch_example2.sh
qsub batch_example3.sh
......@@ -9,11 +9,16 @@ args = parser.parse_args()
Nm = args.Nm
rez = 0
for i in range(Nm):
np.random.seed(i)
def fun(i):
np.random.seed(i)
A = np.random.rand (100, 100)
B = np.random.rand (100, 100)
rez += np.sum(np.dot(A, B))
return np.sum(np.dot(A, B))
rez = 0
for i in range(Nm):
rez += fun(i)
# it is equivalent to rez = np.sum( map(fun, range(Nm)) )
print (rez)
......@@ -25,3 +25,6 @@ p = Pool(args.ncores);
rez = np.sum( p.map(fun, range(Nm)) )
print (rez)
p.close()
p.join()
......@@ -32,3 +32,6 @@ for i in range(Nm):
rez += async_rez[i].get()
print (rez)
p.close()
p.join()
......@@ -7,7 +7,9 @@ import mkl
# To be sure that we use 1 thread in MKL
mkl.set_num_threads(1)
def fun(my_id, orderq, rezq):
# function wait order from orderq
# stop if order < 0, otherwise make computation and put result in the rezq
def fun(orderq, rezq):
while True:
order = orderq.get();
if (order < 0):
......@@ -15,7 +17,8 @@ def fun(my_id, orderq, rezq):
np.random.seed(order)
A = np.random.rand (100, 100)
B = np.random.rand (100, 100)
rezq.put([my_id, np.sum(np.dot(A, B))])
rezq.put(np.sum(np.dot(A, B)))
# Parse command line arguments
parser = argparse.ArgumentParser()
......@@ -28,47 +31,40 @@ ncores = args.ncores
if (ncores == None):
ncores = cpu_count()
#Adjust number of cores if necessary
if (ncores > Nm):
ncores = Nm
# Result Queue will be shared between all workers
rezq = Queue()
# Make order and result queues
orderq = Queue()
rezq = Queue()
# List of all workers
procs = []
# There is one order queue/pipe for each of worker
orderqs = []
for pid in range(ncores):
# We could use Pipe for orderq (it would be faster)
orderq = Queue()
p = Process(target=fun, args=(pid, orderq, rezq))
#Initialize all workers
for i in range(ncores):
p = Process(target=fun, args=(orderq, rezq))
p.start()
procs.append(p)
orderqs.append(orderq)
#Make first ncores orders
for i in range(ncores):
orderqs[i].put(i)
rez = 0
#Wait and make all other orders
for i in range(ncores, Nm):
[w_id, s] = rezq.get()
rez += s
orderqs[w_id].put(i)
# Put all jobs in orderq
for i in range(Nm):
orderq.put(i);
#Collect last ncores results
for i in range(ncores):
[w_id, s] = rezq.get()
rez += s
rez = 0
# Recive all results
for i in range(Nm):
rez += rezq.get()
#stop all workers
for i in range(ncores):
orderqs[i].put(-1)
procs[i].join()
print (rez)
orderq.put(-1)
#Join all workers
for p in procs:
p.join()
print (rez)
qsub batch_example1.sh
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment