-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiprocessing_Library.py
More file actions
82 lines (64 loc) · 2 KB
/
Multiprocessing_Library.py
File metadata and controls
82 lines (64 loc) · 2 KB
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
73
74
75
76
77
78
79
80
81
82
'''
Created on Aug 25, 2022
@author: Zeros
'''
import multiprocessing
from multiprocessing import RawArray
import numpy as np
def add(lower, size, a, b, c):
upper = int(lower + size)
#for i in range(lower, upper):
# c[i] = a[i] + b[i]
c[lower:upper] = a[lower:upper] + b[lower:upper]
return c
def sub(lower, size, a, b, c):
upper = int(lower + size)
for i in range(lower, upper):
c[i] = a[i] - b[i]
return c
def mul(lower, size, a, b, c):
upper = int(lower + size)
for i in range(lower, upper):
c[i] = a[i] * b[i]
return c
def div(lower, size, a, b, c):
upper = int(lower + size)
for i in range(lower, upper):
c[i] = a[i] / b[i]
return c
def array_op(array1, array2, mode="add"):
array3 = np.zeros_like(array1)
manager = multiprocessing.Manager()
array1_m = manager.Array("f", np.ravel(array1))
array2_m = manager.Array("f", np.ravel(array2))
array3_m = manager.Array("f", np.ravel(array3))
number_of_blocks = multiprocessing.cpu_count()
block_size = divmod(len(array1_m), number_of_blocks)
if block_size[0] == 0:
block_size = 1
#print(block_size)
vals = []
for i in range(0, len(array1_m), block_size[0]):
vals.append([i, block_size[0], array1_m, array2_m, array3_m])
f = []
if mode == "add":
f = add
elif mode == "sub":
f = sub
elif mode == "mul":
f = mul
elif mode == "div":
f = div
else:
pass
with multiprocessing.Pool() as pool:
pool.starmap(f, vals)
return np.reshape(np.asarray(array3_m), array1.shape)
if __name__ == "__main__":
shape = (3, 100, 100, 100)
array1 = np.ones(shape)*100
array2 = np.asarray(range(0, 3000000))#np.ones(shape)
array2 = np.reshape(array2, shape)
array3 = array_op(array1, array2, mode="add")
print(array3)
print(np.shape(array3))