-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalc.py
More file actions
215 lines (164 loc) · 6.73 KB
/
Calc.py
File metadata and controls
215 lines (164 loc) · 6.73 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
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
from __future__ import annotations
import time
from typing import Tuple, List
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from queue import Queue
from Charge import ChargeDist
import threading
import numpy as np
from numba import cuda
import Charge
class Calc:
def __init__(self,
charges: Tuple[ChargeDist],
phy_rect: Tuple[float, float, float, float],
data: np.ndarray,
ref_point: Tuple[float, float] | None = None,
device: str = 'cpu'):
self.charges: Tuple[ChargeDist] = charges
self.phy_rect = np.array(phy_rect, dtype=np.float32)
self.data = data
self.ref_point = ref_point
self.device = device
def do(self, progress_q: Queue, verbose=True) -> None:
self.data.fill(0.0)
ref_potential = 0.0
if self.ref_point is not None:
ref_potential = self.__get_potential(self.ref_point[0], self.ref_point[1])
self.data -= ref_potential
if self.device == 'cpu':
self.do_on_cpu(progress_q, verbose=verbose)
elif self.device == 'gpu':
self.do_on_gpu(progress_q, verbose=verbose)
def do_on_cpu(self, progress_q: Queue, verbose: bool = True) -> None:
st_tm = time.time()
lock = threading.Lock()
n_row, n_col = self.data.shape
n_worker = 32
n_done_row = [0]
th_list = []
for worker_idx in range(n_worker):
st_row = (n_row // n_worker) * worker_idx
en_row = (n_row // n_worker) * (worker_idx + 1) - 1 if worker_idx != n_worker - 1 else n_row - 1
worker = threading.Thread(target=self.cpu_worker,
args=(worker_idx, self, lock, self.data, st_row, en_row, n_done_row))
th_list.append(worker)
worker.start()
while n_done_row[0] != n_row:
time.sleep(0.3)
progress = n_done_row[0] / n_row * 100
el_tm = time.time() - st_tm
est_tm = 100 / progress * el_tm if progress != 0 else np.NaN
progress_q.put({
'task': 'calc',
'progress': progress,
'el_tm': el_tm,
'est_tm': est_tm
})
if verbose is True:
print('\rcalc : {:.3f}% | {:.2f}s/{:.2f}s'.format(progress, el_tm, est_tm), end='')
el_tm = time.time() - st_tm
print('\rcalc done {:.2f}s'.format(el_tm))
for worker in th_list:
worker.join()
def do_on_gpu(self, progress_q: Queue, verbose: bool = True) -> None:
st_tm = time.time()
# Index
# 0 ~ 3 : x1, y1, x2, y2
# 4 ~ 7 : unit vec
# 8 ~ 9 : density
# 10 : depth
# 11 : form
charge_info_arr = np.zeros((len(self.charges), 12), dtype=np.float32)
for idx, charge in enumerate(self.charges):
buf = np.zeros(12, dtype=np.float32)
buf[0:2] = charge.p1
buf[2:4] = charge.p2
buf[4:6] = charge.u_vec[0]
buf[6:8] = charge.u_vec[1]
buf[8] = charge.density
buf[10] = charge.depth
buf[11] = Charge.FORM_CONSTANT if charge.form == 'constant' else 1.0
charge_info_arr[idx] = buf
n_data = self.data.shape[0] * self.data.shape[1]
d_phy_rect = cuda.to_device(self.phy_rect)
d_data = cuda.to_device(self.data)
d_charge = cuda.to_device(charge_info_arr)
d_cnt = cuda.to_device(np.array([0], dtype=np.int32))
n_thread_in_block = (16, 16)
n_block_in_grid = (
self.data.shape[1] // n_thread_in_block[0] + 1,
self.data.shape[0] // n_thread_in_block[1] + 1
)
kernel_s = cuda.stream()
progress_s = cuda.stream()
gpu_kernel[n_block_in_grid, n_thread_in_block, kernel_s](d_phy_rect, d_data, d_charge, d_cnt)
h_cnt = [0]
while h_cnt[0] != n_data:
time.sleep(0.3)
h_cnt[0] = d_cnt.copy_to_host(stream=progress_s)[0]
progress = h_cnt[0] / n_data * 100
el_tm = time.time() - st_tm
est_tm = 100 / progress * el_tm if progress != 0 else np.NaN
progress_q.put({
'task': 'calc',
'progress': progress,
'el_tm': el_tm,
'est_tm': est_tm
})
if verbose is True:
print('\rcalc : {:.3f}% | {:.2f}s/{:.2f}s'.format(progress, el_tm, est_tm), end='')
el_tm = time.time() - st_tm
print('\rcalc done {:.2f}s'.format(el_tm))
# kernel_s.synchronize()
self.data[:] = d_data.copy_to_host()
del progress_s
del kernel_s
del d_cnt
del d_charge
del d_data
del d_phy_rect
def __get_potential(self, x: float, y: float) -> float:
res = 0.0
for charge in self.charges:
res += charge.get_potential(x, y)
return res
def __get_sample_phy_pos(self, col_idx: int, row_idx: int):
phy_rect = self.phy_rect
n_row, n_col = self.data.shape
sample_x = phy_rect[0] + (phy_rect[2] - phy_rect[0]) * col_idx / (n_col - 1)
sample_y = phy_rect[1] - (phy_rect[1] - phy_rect[3]) * row_idx / (n_row - 1)
return sample_x, sample_y
@staticmethod
def cpu_worker(th_idx: int, calc: Calc, lock: threading.Lock,
data: np.ndarray, st_row: int, en_row: int,
n_done_row: List[int]):
buf = np.zeros((en_row - st_row + 1, data.shape[1]), dtype=np.float32)
for row_idx in range(st_row, en_row + 1):
for col_idx in range(data.shape[1]):
sample_x, sample_y = Calc.__get_sample_phy_pos(calc, col_idx, row_idx)
potential = Calc.__get_potential(calc, sample_x, sample_y)
buf[row_idx - st_row][col_idx] += potential
lock.acquire()
n_done_row[0] += 1
lock.release()
lock.acquire()
data[st_row:en_row + 1] += buf
lock.release()
@cuda.jit('void(float32[:], float32[:,:], float32[:,:], int32[:])')
def gpu_kernel(phy_rect, data, charges, cnt):
x, y = cuda.grid(2)
if not (x < data.shape[1] and y < data.shape[0]):
return
n_row, n_col = data.shape
sample_x = phy_rect[0] + (phy_rect[2] - phy_rect[0]) * x / (n_col - 1)
sample_y = phy_rect[1] - (phy_rect[1] - phy_rect[3]) * y / (n_row - 1)
res = 0.0
for charge in charges:
res += Charge.gpu_get_potential(sample_x, sample_y, charge)
data[y][x] += res
cuda.atomic.add(cnt, 0, 1)
# n_data = data.shape[0] * data.shape[1]
# if cnt[0] % 10000000 == 0 or cnt[0] == n_data:
# print('(', round(cnt[0] / n_data * 100, 3), '%)')