1"""A very simple parallel code example to execute parallel functions in python"""
2import multiprocessing
3import numpy as np
4def multiprocessing_func(x):
5 """Individually prints the squares y_i of the elements x_i of a vector x"""
6 for x_i in x:
7 y=x_i**2
8 print('The square of ',x_i,' is ',y)
9def chunks(input, n):
10 """Yields successive n-sized chunks of input"""
11 for i in range(0, len(input), n):
12 yield input[i:i + n]
13if __name__=='__main__':
14 n_proc=4 #Numer of available processors
15 x=np.arange(100) #Input
16 chunked_x=list(chunks(x, int(x.shape[0]/n_proc)+1)) #Splits input among n_proc chunks
17 processes=[] #Initialize the parallel processes list
18 for i in np.arange(0,n_proc):
19 """Execute the target function on the n_proc target processors using the splitted input"""
20 p = multiprocessing.Process(target=multiprocessing_func,args=(chunked_x[i],))
21 processes.append(p)
22 p.start()
23 for process in processes:
24 process.join()
1import time
2from multiprocessing import Process
3
4# My functions (threads)
5def my_func_1():...
6def my_func_2():...
7
8# Single calculation
9start = time.time()
10my_func_1()
11my_func_2()
12print(f'Single thread total time: {time.time() - start}')
13
14# Processes
15process = Process(target=my_func_1)
16process2 = Process(target=my_func_2)
17process.start()
18process2.start()
19
20start = time.time() # Start the two processes
21
22process.join() # Wait till processes finish
23process2.join()
24
25print(f'Two thread total time: {time.time() - start}')
1from multiprocessing import Pool
2
3def f(x):
4 return x*x
5
6if __name__ == '__main__':
7 with Pool(5) as p:
8 print(p.map(f, [1, 2, 3]))
1from multiprocessing import Pool
2
3def test( v ):
4 print(v)
5 return v
6
7if __name__ == '__main__':
8 with Pool(2) as p:
9 print(p.map(test, range(5)))