Yes, it’s one Source module, and image is declared as an array converted to texref and a few flags are set to it
Here is the full function, the items passed into it are numpy arrays
def cuda_forward_project(rays, image, size, steps):
mod = SourceModule("""
texture<float, 2> image;
__global__ void forward_project(const float *rays, float *sinogram, int sz, const float minx, const float maxx, const float miny, const float maxy, const int steps)
{
const int cu_x = threadIdx.x + blockIdx.x * blockDim.x;
const int cu_y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = cu_x + cu_y * blockDim.x * gridDim.x;
if (offset >= sz)
{
return;
}
float ray[4];
ray[0] = rays[offset*4 + 0];
ray[1] = rays[offset*4 + 1];
ray[2] = rays[offset*4 + 2];
ray[3] = rays[offset*4 + 3];
float line_distance = hypotf(ray[2] - ray[0], ray[3] - ray[1]);
float integral = 0;
if (line_distance > 0.1)
{
for (unsigned int j = 0; j < steps; j++)
{
float x = ray[0] + j * (ray[2] - ray[0]) / steps;
float xnormed = (x - minx) / (maxx - minx);
float y = ray[1] + j * (ray[3] - ray[1]) / steps;
float ynormed = (y - miny) / (maxy - miny);
integral += tex2D(image, xnormed, ynormed);
}
}
sinogram[offset] = integral * line_distance / steps;
}
""")
# CUDA stuff
func = mod.get_function("forward_project")
tex_image = mod.get_texref("image")
image = image.astype(np.float32)
cuda.matrix_to_texref(image, tex_image, order="C")
tex_image.set_flags(cuda.TRSF_NORMALIZED_COORDINATES)
tex_image.set_filter_mode(cuda.filter_mode.LINEAR)
threads_per_blockdim = 16
nblocks_x = np.size(rays, 0) // threads_per_blockdim**2 + 1
nblocks_y = 1
block=(threads_per_blockdim, threads_per_blockdim, 1)
grid=(nblocks_x, nblocks_y, 1)
# setup vars
rays_shape = rays.shape
rays_ = rays.reshape(-1, 4).astype(np.float32)
sinogram_ = np.zeros(np.prod(rays_shape[:-1]), dtype=np.float32)
sz_ = np.int32(np.prod(rays_shape[:-1]))
minx_ = np.float32(size[0])
maxx_ = np.float32(size[1])
miny_ = np.float32(size[2])
maxy_ = np.float32(size[3])
steps_ = np.int32(steps)
print(block, grid, rays_shape, sz_)
func(cuda.In(rays_), cuda.Out(sinogram_), sz_, minx_, maxx_, miny_, maxy_, steps_, block=block, grid=grid, texrefs=[tex_image])
return sinogram_