-
Notifications
You must be signed in to change notification settings - Fork 17
/
temp.py
162 lines (127 loc) · 5.14 KB
/
temp.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
#%%
import numpy as np
import os; os.environ["NUMBA_ENABLE_CUDASIM"] = "0"; os.environ["NUMBA_CUDA_DEBUGINFO"] = "0";
import numba
from numba import cuda
import conehead.dda_3d
import time
import math
# grid = np.zeros((64, 64, 64))
# d_eff_cpu = np.zeros_like(grid)
# direction = [1, 0, 0]
# voxel_size = [1, 1, 1]
# start = time.time()
# for i in range(grid.shape[0]):
# for j in range(grid.shape[1]):
# for k in range(grid.shape[2]):
# current_voxel = [i, j, k]
# voxels_traversed, intersection_t_values = conehead.dda_3d.dda_3d(direction, grid, current_voxel, voxel_size) # type: ignore
# d_eff_cpu[i, j, k] = np.sum(intersection_t_values)
# end = time.time()
# print(f"{end - start:.2f} s")
# print(d_eff)
# %%
@cuda.jit
def dda_3d_cuda(direction, grid, voxel_size, d_eff):
x, y, z = cuda.grid(3)
current_voxel = cuda.local.array(3, numba.int32)
current_voxel[0] = x
current_voxel[1] = y
current_voxel[2] = z
if x < grid.shape[0] and y < grid.shape[1] and z < grid.shape[2]:
# print(x, y, z)
step = cuda.local.array(3, numba.int32)
step[0] = -1 if direction[0] < 0 else 1
step[1] = -1 if direction[1] < 0 else 1
step[2] = -1 if direction[2] < 0 else 1
# print(step[0], step[1], step[2])
t = cuda.local.array(3, numba.float32)
t[0] = 0.0
t[1] = 0.0
t[2] = 0.0
delta_t = cuda.local.array(3, numba.float32)
delta_t[0] = 0.0
delta_t[1] = 0.0
delta_t[2] = 0.0
big_number = 1000000000.0
if direction[0] == 0.0:
t[0] = big_number
delta_t[0] = big_number
else:
t[0] = (voxel_size[0] / 2) / direction[0]
delta_t[0] = voxel_size[0] / direction[0]
if direction[1] == 0.0:
t[1] = big_number
delta_t[1] = big_number
else:
t[1] = (voxel_size[1] / 2) / direction[1]
delta_t[1] = voxel_size[1] / direction[1]
if direction[2] == 0.0:
t[2] = big_number
delta_t[2] = big_number
else:
t[2] = (voxel_size[2] / 2) / direction[2]
delta_t[2] = voxel_size[2] / direction[2]
# print(x, y, z, t[0], t[1], t[2], delta_t[0], delta_t[1], delta_t[2])
xmax = grid.shape[0]
ymax = grid.shape[1]
zmax = grid.shape[2]
# voxels_traversed = cuda.local.array(100,)
max_array_length = 444 # Longest diagonal of 256 x 256 x 256 grid
intersection_t_values = cuda.local.array(max_array_length, numba.float32)
intersection_t_values_count = 0
while (current_voxel[0] >= 0 and current_voxel[0] < xmax and
current_voxel[1] >= 0 and current_voxel[1] < ymax and
current_voxel[2] >= 0 and current_voxel[2] < zmax):
# voxels_traversed.append(np.copy(current_voxel))
if t[0] < t[1]:
if t[0] < t[2]:
intersection_t_values[intersection_t_values_count] = t[0]
intersection_t_values_count += 1
t[0] += delta_t[0]
current_voxel[0] += step[0]
else:
intersection_t_values[intersection_t_values_count] = t[2]
intersection_t_values_count += 1
t[2] += delta_t[2]
current_voxel[2] += step[2]
else:
if t[1] < t[2]:
intersection_t_values[intersection_t_values_count] = t[1]
intersection_t_values_count += 1
t[1] += delta_t[1]
current_voxel[1] += step[1]
else:
intersection_t_values[intersection_t_values_count] = t[2]
intersection_t_values_count += 1
t[2] += delta_t[2]
current_voxel[2] += step[2]
# print(x, y, z, t[0], t[1], t[2], delta_t[0], delta_t[1], delta_t[2], intersection_t_values.size)
for i in range(len(intersection_t_values)):
d_eff[x, y, z] += intersection_t_values[i]
grid = np.zeros((256, 256, 256), dtype=np.float32)
d_eff = np.zeros_like(grid, dtype=np.float32)
direction = [np.float32(1), np.float32(0), np.float32(0)]
voxel_size = [np.float32(1), np.float32(1), np.float32(1)]
grid_device = cuda.to_device(grid)
d_eff_device = cuda.to_device(d_eff)
direction_device = cuda.to_device(direction)
voxel_size_device = cuda.to_device(voxel_size)
threadsperblock = (8, 8, 8)
blockspergrid_x = math.ceil(grid.shape[0] / threadsperblock[0])
blockspergrid_y = math.ceil(grid.shape[1] / threadsperblock[1])
blockspergrid_z = math.ceil(grid.shape[2] / threadsperblock[2])
blockspergrid = (blockspergrid_x, blockspergrid_y, blockspergrid_z)
print(threadsperblock)
print(blockspergrid)
print(grid.shape)
start = time.time()
dda_3d_cuda[blockspergrid, threadsperblock](direction_device, grid_device, voxel_size_device, d_eff_device)
d_eff_cuda = d_eff_device.copy_to_host()
end = time.time()
print(f"{end - start:.2f} s")
# print(d_eff_cpu)
# print(d_eff_cuda)
# assert (np.array_equal(d_eff_cpu, d_eff_cuda))
# print(d_eff)
# %%