-
Notifications
You must be signed in to change notification settings - Fork 4
/
pool.py
72 lines (52 loc) · 1.43 KB
/
pool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import sys
import time
import numpy
import multiprocessing
from functools import partial
from multiprocessing import Pool
size = 2000000
number_cores = 4
a = numpy.full(size, 1.0, dtype = numpy.float64)
b = numpy.full(size, 1.0, dtype = numpy.float64)
def vector_square(x):
return x * x
def vector_add(elem_list):
return sum(elem_list)
def elem_elem_add(a, b, i):
return a[i] + b[i]
def THICK_elem_elem_add(a, b, number_cores, i):
slice_size = a.size / number_cores
lb = slice_size * i;
ub = lb + slice_size;
return a[lb:ub] + b[lb:ub]
time1 = time.time()
pool = Pool(number_cores)
c = pool.map(vector_square, a)
pool.close()
pool.join()
time2 = time.time() - time1
print("Time for vector_square \t\t = {0}".format(time2))
time1 = time.time()
pool = Pool(number_cores)
c = pool.map(vector_add, zip(a, b))
pool.close()
pool.join()
time2 = time.time() - time1
print("Time for vector_add \t\t = {0}".format(time2))
time1 = time.time()
pool = Pool(number_cores)
map_func = partial(elem_elem_add, a, b)
c = pool.map(map_func, iter(range(size)))
pool.close()
pool.join()
time2 = time.time() - time1
print("Time for elem_elem_add \t\t = {0}".format(time2))
time1 = time.time()
pool = Pool(number_cores)
map_func = partial(THICK_elem_elem_add, a, b, number_cores)
c = pool.map(map_func, iter(range(number_cores)))
pool.close()
pool.join()
time2 = time.time() - time1
print("Time for THICK_elem_elem_add \t = {0}".format(time2))