In: Computer Science
Research a computer operation that could be improved through parallelization on a multi-core CPU. Include the following:
Here we will see how computer multi core processor can be used for parallelisation to increase speed of Python code
In multiprocessing, parallelisation refers to the multiple cores in CPU. Today Computer processors have 2 to more cores, this can improve processing time. These cores makes possible that all calculations of section code can done parallel. Early Computers had only one CPU core or processor, which will take more execution time for all calculations of section code.
Python code allows the all section of code to run faster because it can use the advantage of multiprocessing. Python having built-in multiprocessing module that allows us to send the particular section of code to multiple cores or processors for parallel execution.
consider following subsection of python code that uses single core processor for execution. it uses for loop statement to execute the function func1.the function executed on each iteration individually. therefore it takes more CPU time to execute.
import time
def func1(x):
if x%2 == 0:
return 'even'
elif x == 0:
return 'zero'
else:
return 'odd'
starttime = time.time()
for i in range(0,15):
y = i*i
print('{} squared results in a/an {} number'.format(i, func1(y)))
print('That taken {} seconds'.format(time.time() - starttime))
If we use multiprocessing on multi core processor, the function func1 would be executed on multiple list items at time. therefore it takes less CPU time to execute.
The multiprocessing Python module contains Process class & Pool class. Process class sends each process to a different processor, and the Pool class sends sets of processes to different processors.
Here we will see how the above section of python code can be executed parallel on multi core processor using Process class & Pool class in multiprocessing Python module.The Process class is more efficient for small amounts of processes.Pool is useful for large amounts of processes.
Following code Uses Process class:
import time
import multiprocessing
def func1(x):
if x%2 == 0:
return 'even'
elif x == 0:
return 'zero'
else:
return 'odd'
def multiprocessfunc(x):
y = x*x
print('{} squared results in a/an {} number'.format(x, func1(y)))
if __name__ == '__main__':
starttime = time.time()
processes = []
for i in range(0,15):
p = multiprocessing.Process(target=multiprocessfunc, args=(i,))
processes.append(p)
p.start()
for process in processes:
process.join()
print('That taken {} seconds'.format(time.time() - starttime))
the above python code uses the Process class, in this code the functions func1 & multiprocessfunc are executed on each list item. These functions will take a list item as one of its arguments. Next, it uses the python multiprocessing module, that creates a new process for each list item, and calls each process in one call. We making a list of these processes. once creating all the processes, get the separate output of each processor(i.e processes) and join them into a single list.
Following code Uses Pool class:
import time
import multiprocessing
def func1(x):
if x%2 == 0:
return 'even'
elif x == 0:
return 'zero'
else:
return 'odd'
def multiprocessfunc(x):
y = x*x
print('{} squared results in a/an {} number'.format(x, func1(y)))
if __name__ == '__main__':
starttime = time.time()
pool = multiprocessing.Pool()
pool.map(multiprocessfunc, range(0,15))
pool.close()
print('That taken {} seconds'.format(time.time() - starttime))
the above code uses the Pool class, in this code the functions func1 & multiprocessfunc are executed on each list item. These functions will take a list item as one of its arguments.Next, it uses the python multiprocessing module, that creates a Pool object called pool(i.e pool = multiprocessing.Pool()).the object pool has function map.The map function takes function(i,e multiprocessfunc) that we want to process multiple times & list as arguments.