-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel.py
45 lines (36 loc) · 1.11 KB
/
parallel.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
import multiprocessing as mp
import numpy as np
from functools import reduce
from operator import mul
import math
class SharedArray:
def __init__(self, dtype, shape):
self.shape = shape
self.nptype = np.dtype(dtype)
ctype = np.ctypeslib.as_ctypes_type(self.nptype)
self.shm = mp.RawArray(ctype, reduce(mul, shape))
def to_numpy(self):
return np.frombuffer(self.shm, dtype=self.nptype).reshape(self.shape)
def to_shm(self):
return self.shm
def dims(self):
return self.shape
def type(self):
return self.nptype
## someday add subscription support lol
def roundDown2sqrt(x: int) -> int:
"""
Round down to the nearest square root.
"""
return int(math.floor(math.sqrt(x)))
def splitRanges(x: int, n: int) -> list[tuple]:
"""
Split `range(x)` into n tuple containing new ranges with balanced length.
"""
if x <= n:
for i in range(x):
yield i, i + 1
else:
k, m = divmod(x, n)
for i in range(n):
yield i * k + min(i, m), (i + 1) * k + min(i + 1, m)