使用纹理 1D 的 PyCuda 1D 插值
PyCuda 1D interpolation using texture 1D
我是 CUDA 新手,我的目标是使用 PyCUDA 和 CUDA 一维纹理实现简单的一维插值。出于测试目的,我只想要一个 returns 数组中的原始图像值(从纹理中提取)的内核。问题是 tex1D(tex, pos);
returns 总是 0.
这是我的 CUDA 内核代码:
interp1 = """
#include <stdint.h>
texture<uint8_t, 1> tex;
__global__
void interp1(uint8_t *out) {
unsigned int pos = blockIdx.x * blockDim.x + threadIdx.x;
out[pos] = tex1D(tex, pos);
}
"""
这是我的 python 代码片段,我在其中读取测试图像,在 GPU 上分配内存,将图像复制到 GPU,通过 set_address
创建我的纹理参考并调用我的内核:
...
img = cv2.imread("lena.jpg", 0)
img_in = pycuda.driver.to_device(img.flatten())
texref.set_address(img_in, img.nbytes)
texref.set_format(pycuda.driver.array_format.UNSIGNED_INT8, 1)
img_out = pycuda.driver.mem_alloc(img.nbytes)
interp1_func(img_out, block=(512, 1, 1), grid=(7200, 1, 1)) # image is 1920 x 1920
context.synchronize()
imgnew = np.zeros_like(img.flatten())
pycuda.driver.memcpy_dtoh(imgnew, img_out)
imgnew = imgnew.reshape(img.shape)
...
希望有人能帮我解决这个问题。
因为您已经将线性内存绑定到纹理引用,所以您必须在内核中使用 tex1Dfetch
, rather than tex1D
来访问纹理。
请注意,在这种情况下,也无法执行插值,只能执行查找。
我是 CUDA 新手,我的目标是使用 PyCUDA 和 CUDA 一维纹理实现简单的一维插值。出于测试目的,我只想要一个 returns 数组中的原始图像值(从纹理中提取)的内核。问题是 tex1D(tex, pos);
returns 总是 0.
这是我的 CUDA 内核代码:
interp1 = """
#include <stdint.h>
texture<uint8_t, 1> tex;
__global__
void interp1(uint8_t *out) {
unsigned int pos = blockIdx.x * blockDim.x + threadIdx.x;
out[pos] = tex1D(tex, pos);
}
"""
这是我的 python 代码片段,我在其中读取测试图像,在 GPU 上分配内存,将图像复制到 GPU,通过 set_address
创建我的纹理参考并调用我的内核:
...
img = cv2.imread("lena.jpg", 0)
img_in = pycuda.driver.to_device(img.flatten())
texref.set_address(img_in, img.nbytes)
texref.set_format(pycuda.driver.array_format.UNSIGNED_INT8, 1)
img_out = pycuda.driver.mem_alloc(img.nbytes)
interp1_func(img_out, block=(512, 1, 1), grid=(7200, 1, 1)) # image is 1920 x 1920
context.synchronize()
imgnew = np.zeros_like(img.flatten())
pycuda.driver.memcpy_dtoh(imgnew, img_out)
imgnew = imgnew.reshape(img.shape)
...
希望有人能帮我解决这个问题。
因为您已经将线性内存绑定到纹理引用,所以您必须在内核中使用 tex1Dfetch
, rather than tex1D
来访问纹理。
请注意,在这种情况下,也无法执行插值,只能执行查找。