我需要在CUDA中执行三线性插值。以下是问题定义:
给定三个点向量:x[nx]
,y[ny]
,z[nz]
和函数值矩阵func[nx][ny][nz]
,我想在某些随机点之间找到函数值范围x
,y
和z
。
我可以在CUDA中编写自己的插值内核,但我想知道是否有一个已经存在来完成这项工作。
谢谢!
答案 0 :(得分:3)
如@Farzad所述,您可以使用纹理过滤在CUDA中执行三线性插值。 simpleTexture3D示例提供了有关如何使用它的完整示例。但是,就目前而言,它可能不会立即使用,因为它涉及使用像OpenGL和过剩的库以及其他外部依赖项,如cutil.h
。
因此,我发现将上述代码减少到最小尺寸"示例显示了这个概念。正如您将看到的,代码加载位于名为Bucky.raw
的文件中的外部数据,我已经借用了#34;来自链接到上面的github页面。
下面的代码将一个数据立方体插入位于其中心切片中的常规笛卡尔网格。如果一切顺利,您将重建的图像是下面报告的图像。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <cuda_runtime.h>
#include <cuda.h>
typedef unsigned char uchar;
#define BLOCKSIZE 16
float w = 0.5; // texture coordinate in z
/********************/
/* CUDA ERROR CHECK */
/********************/
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) { getchar(); exit(code); }
}
}
typedef unsigned int uint;
typedef unsigned char uchar;
texture<uchar, 3, cudaReadModeNormalizedFloat> tex; // 3D texture
cudaArray *d_volumeArray = 0;
uint *d_output = NULL;
uint *h_output = NULL;
/************************************************/
/* TEXTURE-BASED TRILINEAR INTERPOLATION KERNEL */
/************************************************/
__global__ void
d_render(uint *d_output, uint imageW, uint imageH, float w)
{
uint x = __umul24(blockIdx.x, blockDim.x) + threadIdx.x;
uint y = __umul24(blockIdx.y, blockDim.y) + threadIdx.y;
float u = x / (float) imageW;
float v = y / (float) imageH;
// read from 3D texture
float voxel = tex3D(tex, u, v, w);
if ((x < imageW) && (y < imageH)) {
// write output color
uint i = __umul24(y, imageW) + x;
d_output[i] = voxel*255;
}
}
void main() {
int N = 32;
int imageH = 512;
int imageW = 512;
const char* filename = "Bucky.raw";
// --- Loading data from file
FILE *fp = fopen(filename, "rb");
if (!fp) { fprintf(stderr, "Error opening file '%s'\n", filename); getchar(); return; }
uchar *data = (uchar*)malloc(N*N*N*sizeof(uchar));
size_t read = fread(data, 1, N*N*N, fp);
fclose(fp);
printf("Read '%s', %lu bytes\n", filename, read);
gpuErrchk(cudaMalloc((void**)&d_output, imageH*imageW*sizeof(uint)));
// --- Create 3D array
const cudaExtent volumeSize = make_cudaExtent(N, N, N);
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<uchar>();
gpuErrchk(cudaMalloc3DArray(&d_volumeArray, &channelDesc, volumeSize));
// --- Copy data to 3D array (host to device)
cudaMemcpy3DParms copyParams = {0};
copyParams.srcPtr = make_cudaPitchedPtr((void*)data, volumeSize.width*sizeof(uchar), volumeSize.width, volumeSize.height);
copyParams.dstArray = d_volumeArray;
copyParams.extent = volumeSize;
copyParams.kind = cudaMemcpyHostToDevice;
gpuErrchk(cudaMemcpy3D(©Params));
// --- Set texture parameters
tex.normalized = true; // access with normalized texture coordinates
tex.filterMode = cudaFilterModeLinear; // linear interpolation
tex.addressMode[0] = cudaAddressModeWrap; // wrap texture coordinates
tex.addressMode[1] = cudaAddressModeWrap;
tex.addressMode[2] = cudaAddressModeWrap;
// --- Bind array to 3D texture
gpuErrchk(cudaBindTextureToArray(tex, d_volumeArray, channelDesc));
// --- Launch the interpolation kernel
const dim3 blockSize(BLOCKSIZE, BLOCKSIZE, 1);
const dim3 gridSize(imageW / blockSize.x, imageH / blockSize.y);
d_render<<<gridSize, blockSize>>>(d_output, imageW, imageH, w);
gpuErrchk(cudaPeekAtLastError());
gpuErrchk(cudaDeviceSynchronize());
// --- Copy the interpolated data to host
h_output = (uint*)malloc(imageW*imageH*sizeof(uint));
gpuErrchk(cudaMemcpy(h_output,d_output,imageW*imageH*sizeof(uint),cudaMemcpyDeviceToHost));
std::ofstream outfile;
outfile.open("out_texture.dat", std::ios::out | std::ios::binary);
outfile.write((char*)h_output, imageW*imageH*sizeof(uint));
outfile.close();
getchar();
}
代码以二进制格式保存结果out_texture.dat
。您可以根据
fd = fopen('out_texture.dat','r');
U = fread(fd,imageH*imageW,'unsigned int');
fclose(fd);