-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
66 lines (56 loc) · 1.81 KB
/
utils.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
import sys
import os
import numpy as np
from types import ModuleType, FunctionType
from gc import get_referents
import psutil
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
nproc = comm.Get_size()
total_memory = psutil.virtual_memory().total
# Custom objects know their class.
# Function objects seem to know way too much, including modules.
# Exclude modules as well.
BLACKLIST = type, ModuleType, FunctionType
def getsize(obj):
"""sum size of object & members."""
if isinstance(obj, BLACKLIST):
raise TypeError('getsize() does not take argument of type: ' + str(type(obj)))
seen_ids = set()
size = 0
objects = [obj]
while objects:
need_referents = []
for obj in objects:
if not isinstance(obj, BLACKLIST) and id(obj) not in seen_ids:
seen_ids.add(id(obj))
size += sys.getsizeof(obj)
need_referents.append(obj)
objects = get_referents(*need_referents)
return size
def dump_memory_usage() -> None:
process = psutil.Process(os.getpid())
memory = process.memory_info().rss # in bytes
print((
f"[Resources] Memory usage: {memory / 1024 / 1024:.2f} MB "
f"({memory / total_memory * 100:.2f}%)"
))
def commune(data, obj: bool = False):
tmp = np.zeros(nproc, dtype=np.int)
tmp[rank] = len(data)
cnts = np.zeros(nproc, dtype=np.int)
comm.Allreduce([tmp, MPI.INT], [cnts, MPI.INT], op=MPI.SUM)
del tmp
dspl = np.zeros(nproc, dtype=np.int)
i = 0
for j in range(nproc):
dspl[j] = i
i += cnts[j]
rslt = np.empty(i, dtype=data.dtype)
if obj:
rslt = comm.allgather(data)
else:
comm.Allgatherv([data, cnts[rank]], [rslt, cnts, dspl, MPI._typedict[data.dtype.char]])
del data, cnts, dspl
return rslt