【发布时间】:2021-07-25 01:08:10
【问题描述】:
我在 2D 相机上收集了一些探测器数据,然后将其转换为实验室框架,因此我最终得到图像中每个像素的 (x^2+y^2) 和 z 坐标。但是随后对象围绕它正常旋转,并且每次旋转都有一个 img 。我将旋转矩阵应用于 (x^2+y^2) 以获得每个 img 的 x 和 y 矩阵,所以我最终为每个图像/角度得到类似的结果。所以每个像素都有一个 3D 位置和强度。
z x y img
444444444 123456789 123456789 123456789
333333333 123456789 123456789 423466789
222222222 123456789 123456789 223256789
111111111 123456789 123456789 523456689
然后我想做的是提取一个平面,即为给定的 z 范围绘制 x、y 的地图。
以下问题稍微复杂一些:
labframe 实际上是弯曲的,所以我不能依赖 x 和 y 的每一行都相同。 图片大小约为 2048x2048x32bits (Tiff) - 可以有 1000 张图片。
我当前的解决方案是使用 CUDA/Numba,我有一个函数可以计算给定角度的z,x,y,img,所以我对所有角度都这样做。每次我然后切片一些行,并用x、y、img 值扩展一个列表。然后使用scipy.interpolate.griddata 给出一个二维地图。 griddata 也很慢,GPU 上的任何东西都可能会更好。
整个过程很慢,所以我正在寻找更好的解决方案,或者图书馆可能已经这样做了? CUDA 代码看起来像这样,它本身并没有那么慢:
#constants are q0, angi, rot_direction, SDD, k0, Binv
@cuda.jit
def detector_to_hkl_kernel(h_glob,k_glob,l_glob,omega_rad):
#get the current thread position
j,i = cuda.grid(2)
if j < h_glob.shape[0] and i < h_glob.shape[1]:
delta_z= (q0[1]-j)*pixel_y #real-space dinstance from centre pixel y
delta_x = (i-q0[0])*pixel_x #real-space dinstance from centre pixel x
delR = math.sqrt(delta_x**2 + delta_z**2)
dist = math.sqrt(delta_x**2+SDD**2 + delta_z**2) #distance to pixel
#lab coorindates of pixel in azimuthal angles
del_pix = math.atan(delta_x/ SDD)
gam_pix = math.atan(delta_z/math.sqrt(delta_x**2 + SDD**2))-angi*math.cos(del_pix)
#lab coordinates in momenturm transfer
qx = k0*(math.cos(gam_pix)*math.cos(del_pix)-math.cos(angi))
qy = k0*(math.cos(gam_pix)*math.sin(del_pix))
qz = k0*(math.sin(gam_pix)+math.sin(angi))
so = math.sin(rotDirection*omega_rad)
co = math.cos(rotDirection*omega_rad)
# we deal with the angle of incidence in the momentum transfer calc
# so that part of the rotation matrix can be fixed
ci = 1 #math.cos(angi)
si = 0 #math.sin(angi)
#rotation matrix
hphi_1 = so*(ci*qy+si*qz)+co*qx
hphi_2 = co*(ci*qy+si*qz)-so*qx
hphi_3 = ci*qz-si*qy
#H= Binv dot Hphi
# compute the dot product manually
h_glob[j,i] = Binv[0][0]*hphi_1+Binv[0][1]*hphi_2+Binv[0][2]*hphi_3
k_glob[j,i] = Binv[1][0]*hphi_1+Binv[1][1]*hphi_2+Binv[1][2]*hphi_3
l_glob[j,i] = Binv[2][0]*hphi_1+Binv[2][1]*hphi_2+Binv[2][2]*hphi_3
h_global_mem = cuda.to_device(np.zeros((pixel_count_y,pixel_count_x)))
k_global_mem = cuda.to_device(np.zeros((pixel_count_y,pixel_count_x)))
l_global_mem = cuda.to_device(np.zeros((pixel_count_y,pixel_count_x)))
# Configure the blocks
threadsperblock = (16, 16)
blockspergrid_x = int(math.ceil(pixel_count_y / threadsperblock[0]))
blockspergrid_y = int(math.ceil(pixel_count_x / threadsperblock[1]))
blockspergrid = (blockspergrid_x, blockspergrid_y)
detector_to_hkl_kernel[blockspergrid, threadsperblock](h_global_mem,k_global_mem,l_global_mem, omega_rad)
return [h_global_mem.copy_to_host(),k_global_mem.copy_to_host(),l_global_mem.copy_to_host()]
【问题讨论】:
-
代码对我来说似乎并不算太糟糕(除了可能不可避免的三角函数调用)。您使用什么 GPU?
-
我看不到内核中在哪里使用了
delR或dist。我们希望内核将它们优化掉,但是用冗余代码来诱惑命运有什么意义呢? -
啊,是的,我错过了当我为发布进行简化时,还有另一个强度校正数组,它是针对它们用于的每个像素计算的。是的,主要问题实际上是对大量图像执行此操作,然后从最终结果中取出飞机。
-
使用 GTX 1660 super,但只是因为我的 3080 上周爆炸了(我认为这无关哈哈)。我的 quadro 现在都 4 或 5 岁了。
标签: python performance parallel-processing cuda scientific-computing