Accelerate for loop Using Parallel Computing Tricks in MATLAB and Python
MATLAB - parfor loop[1]
Parallel for loop
Syntax
parfor loopvar = initval:endval; statements; end
parfor (loopvar = initval:endval, M); statements; end
Description
parfor loopvar = initval:endval; statements; end executes a series of MATLAB® statements for values of loopvar between initval and endval, inclusive, which specify a vector of increasing integer values. The loop occurs in parallel when you open a pool of workers with Parallel Computing Toolbox™ or when you create a MEX function with MATLAB Coder™. Unlike a traditional for-loop, iterations are not executed in a guaranteed order.
parfor (loopvar = initval:endval, M); statements; end executes statements in a loop using a maximum of M workers or threads, where M is a nonnegative integer.
Examples
Perform three large eigenvalue computations using three workers or cores with Parallel Computing Toolbox software:
parpool(3)
parfor i=1:3, c(:,i) = eig(rand(1000)); end
--
Python - multiprocessing[2]
Process-based “threading” interface
Introduction
multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.
Using a pool of workers
The Pool class represents a pool of worker processes. It has methods which allows tasks to be offloaded to the worker processes in a few different ways.
For example:
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
pool = Pool(processes=4) # start 4 worker processes
result = pool.apply_async(f, [10]) # evaluate "f(10)" asynchronously
print result.get(timeout=1) # prints "100" unless your computer is *very* slow
print pool.map(f, range(10)) # prints "[0, 1, 4,..., 81]"
Note that the methods of a pool should only ever be used by the process which created it.
--
References
Parallel for loop - MATLAB parfor: http://www.mathworks.cn/cn/help/matlab/ref/parfor.html