Commit 933c800b authored by RODIONOV Sergey's avatar RODIONOV Sergey

add example of pool without pool

parent 674b8847
#!/usr/bin/env python
import numpy as np
import argparse
from multiprocessing import Process, Queue, Pipe, cpu_count
import mkl
# To be sure that we use 1 thread in MKL
mkl.set_num_threads(1)
def fun(my_id, orderq, rezq):
while True:
order = orderq.get();
if (order < 0):
return
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))])
# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("Nm", help="Number of matrices", type=int);
parser.add_argument("-ncores", help="Number of cores to use", type=int);
args = parser.parse_args()
Nm = args.Nm
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()
# 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))
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)
#Collect last ncores results
for i in range(ncores):
[w_id, s] = rezq.get()
rez += s
#stop all workers
for i in range(ncores):
orderqs[i].put(-1)
procs[i].join()
print (rez)
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