Last active
September 26, 2018 14:24
-
-
Save sjev/4ead13d109150c33d542cb7995edf55a to your computer and use it in GitHub Desktop.
multicore processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import time | |
import os | |
import multiprocessing | |
import numpy as np | |
def threshold(plane, t=0.5): | |
""" threshold all array values """ | |
bw = np.zeros(plane.shape, dtype = np.uint8) | |
for idx,x in np.ndenumerate(plane): | |
bw[idx] = 1*(x>t) | |
return bw | |
def doWork(plane,idx,q): | |
""" function for multithring """ | |
bw = threshold(plane) | |
q.put((bw,idx)) | |
if __name__ == "__main__": | |
N = 1000 | |
nrPlanes = 4 | |
fullMat = np.random.rand(N,N,nrPlanes) | |
print('Starting single thread') | |
# sequential -------------------- | |
tStart = time.time() | |
bw = np.zeros(fullMat.shape) | |
for i in range(nrPlanes): | |
plane = fullMat[:,:,i] | |
bw[:,:,i] = threshold(plane) | |
tSingle = time.time() - tStart | |
print('Single thread done in %.3f seconds' % tSingle) | |
# parallel ----------------------------------- | |
tStart = time.time() | |
bw2 = np.zeros(fullMat.shape) | |
q = multiprocessing.Queue() | |
processes = [] | |
# create subprocesses | |
for iPlane in range(nrPlanes): | |
print('Starting processing plane ', iPlane) | |
p = multiprocessing.Process(target=doWork, args=(fullMat[:,:,iPlane],iPlane,q)) | |
p.start() | |
processes.append(p) | |
print('Getting results..') | |
for idx in range(nrPlanes): | |
res = q.get(True) | |
iPlane = res[1] | |
print('Processed plane ',iPlane) | |
bw2[:,:,iPlane] = res[0] | |
for p in processes: | |
p.join() | |
tMulti = time.time() - tStart | |
print('Multiple thread done in %.3f seconds' % tMulti) | |
print('Speedup: %.2f x' % (tSingle/tMulti)) | |
assert (bw==bw2).all(), 'Results not equal!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment