-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel.py
217 lines (179 loc) · 6.57 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# ----------------------------------------------------------------------------------------------------------------------------
# LOGGING
# ----------------------------------------------------------------------------------------------------------------------------
import logging
logger = logging.getLogger(__name__)
# ----------------------------------------------------------------------------------------------------------------------------
# Packages
# ----------------------------------------------------------------------------------------------------------------------------
from .pythonlib import ensure_installed
# ensure_installed("psutil pandas")
# ----------------------------------------------------------------------------------------------------------------------------
# Normal Imports
# ----------------------------------------------------------------------------------------------------------------------------
from typing import *
import numpy as np
import heapq
import psutil
import pandas as pd
from multiprocessing.pool import ThreadPool
from multiprocessing import Pool, set_start_method
from joblib import Parallel, delayed, parallel_backend
import contextlib
import os
import tempfile
from joblib import load, dump
from .system import tqdmu
def split_list_into_chunks(the_list: list, chunk_size: int) -> list:
"""
>>>list(split_list_into_chunks(list(range(10)),3))
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
"""
if chunk_size == 0:
chunk_size = 1
t = len(the_list)
n = int(t / chunk_size)
for i in range(n + 1):
l = i * chunk_size
r = l + chunk_size
if r > t:
r = t
if r > l:
yield the_list[l:r]
def split_list_into_chunks_indices(the_list: list, chunk_size: int) -> list:
"""
>>>list(split_list_into_chunks(list(range(10)),3))
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
"""
t = len(the_list)
n = int(t / chunk_size)
for i in range(n + 1):
l = i * chunk_size
r = l + chunk_size
if r > t:
r = t
yield l, r
def split_list_into_nchunks_indices(the_list: list, nchunks: int) -> list:
"""
>>>list(split_list_into_nchunks_indices(list(range(10)),3))
[(0, 3), (3, 6), (6, 10)]
"""
total_length = len(the_list)
step = total_length // nchunks
for chunk in range(nchunks):
if chunk == nchunks - 1:
l = step * chunk
r = total_length
yield l, r
else:
yield step * chunk, (chunk + 1) * step
def split_array(arr: object, step: int) -> list:
"""
Returns list of (a,b) tuples of array split into chunks using certain step size.
>>>split_array(np.random.uniform(0,1,5477),step=1000)
[(0, 1000),
(1000, 2000),
(2000, 3000),
(3000, 4000),
(4000, 5000),
(5000, 5477)]
"""
l = len(arr)
a = 0
b = a
res = []
assert step > 0
while True:
b = a + step
if b >= l:
b = l
if b > 0:
res.append((a, b))
if b == l:
break
a = b
return res
def distribute_work(workload: Sequence, nworkers: int) -> tuple:
"""Distribute array workload into nworkers chunks of approximately same total size."""
if nworkers <= 0:
nworkers = psutil.cpu_count(logical=False)
planned_work_per_worker = [[] for _ in range(nworkers)]
workload_indices_per_worker = [[] for _ in range(nworkers)]
totals = [(0, i) for i in range(nworkers)]
heapq.heapify(totals)
for i, value in enumerate(workload):
total, index = heapq.heappop(totals)
planned_work_per_worker[index].append(value)
workload_indices_per_worker[index].append(i)
heapq.heappush(totals, (total + value, index))
return planned_work_per_worker, workload_indices_per_worker
def parallel_run(
jobslist: Sequence, n_jobs: int = -1, backend: str = None, max_nbytes: int = 50_000, prefer_real_cores: bool = True, verbose: int = 0, **parallel_kwargs
):
"""Runs function in parallel using the joblib package with flexible backend (including Dask)."""
if n_jobs <= 0 and prefer_real_cores:
n_jobs = psutil.cpu_count(logical=False)
ctx_mgr = parallel_backend(backend) if (backend and "dask" in backend) else contextlib.nullcontext()
with ctx_mgr:
return Parallel(n_jobs=n_jobs, backend=None if (backend and "dask" in backend) else backend, max_nbytes=max_nbytes, verbose=verbose, **parallel_kwargs)(
jobslist
)
def applyfunc_parallel(
iterable: list,
func: object,
n_cores: int = None,
return_dataframe: bool = True,
logical: bool = False,
initializer=None,
initargs=(),
use_threads: bool = False,
) -> list:
"""Runs function in parallel using the multiprocessing Pool."""
if n_cores is None:
n_cores = min(psutil.cpu_count(logical=logical), len(iterable))
try:
fname = func.__name__
except:
fname = "function"
logger.info(f"Applying of {fname} started, ncores={n_cores}, nchunks={len(iterable):,}...")
if use_threads:
with ThreadPool(
processes=n_cores,
initializer=initializer,
initargs=initargs,
) as pool:
res = tqdmu(pool.starmap(func, iterable))
else:
with Pool(
processes=n_cores,
initializer=initializer,
initargs=initargs,
) as pool:
res = tqdmu(pool.starmap(func, iterable))
logger.info(f"Function applied.")
if return_dataframe:
res = pd.concat(res, ignore_index=True)
logger.info(f"Results Concatenated.")
return res
def set_tf_gpu(gpu: int):
ensure_installed("tensorflow")
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices("GPU")
if gpus:
# Restrict TensorFlow to only use the first GPU
try:
tf.config.experimental.set_visible_devices(gpus[3], "GPU")
logical_gpus = tf.config.experimental.list_logical_devices("GPU")
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPU")
except RuntimeError as e:
# Visible devices must be set before GPUs have been initialized
print(e)
def mem_map_array(obj: np.ndarray, file_name: str, mmap_mode: str = "r") -> object:
"""Maps a numpy array to memory"""
temp_folder = tempfile.mkdtemp()
filename = os.path.join(temp_folder, f"{file_name}.mmap")
if os.path.exists(filename):
os.unlink(filename)
_ = dump(obj, filename)
memmaped = load(filename, mmap_mode=mmap_mode)
return memmaped