目前我有一个像素缓冲区,我使用单个内核调用处理其中的数据:
dim3 threadsPerBlock(32, 32)
dim3 blocks(screenWidth / threadsPerBlock.x, screenHeight / threadsPerBlock.y);
kernel<<<blocks, threadsPerBlock>>>();
像素缓冲区包含窗口中尺寸为screenWidth x screenHeight的所有像素。
我的想法是将窗口分成2或4个部分并同时处理像素数据。
可以这样做,如果可以 - 怎么做?
我几乎没有读过关于流的内容,但据我所知,两个数据流不能用于单个数据(例如我的pixelBuffer),或者我错了?
编辑:我的显卡具有计算能力3.0
编辑2:我使用SDL进行绘图,我有一个GPU,我使用用户定义的数据阵列:
main.cu
Color vfb_linear[VFB_MAX_SIZE * VFB_MAX_SIZE]; // array on the Host
Color vfb[VFB_MAX_SIZE][VFB_MAX_SIZE] // 2D array used for SDL
extern "C" void callKernels(Color* dev_vfb);
int main()
{
Color* dev_vfb; // pixel array used on the GPU
// allocate memory for dev_vfb on the GPU
cudaMalloc((void**)&dev_vfb, sizeof(Color) * RES_X * RES_Y);
// memcpy HostToDevice
cudaMemcpy(dev_vfb, vfb_linear, sizeof(Color) * RES_X * RES_Y, cudaMemcpyHostToDevice);
callKernels(dev_vfb); // wrapper function that calls the kernels
// memcpy DeviceToHost
cudaMemcpy(vfb_linear, dev_vfb, sizeof(Color) * RES_X * RES_Y, cudaMemcpyDeviceToHost);
// convert vfb_linear into 2D array so it can be handled by SDL
convertDeviceToHostBuffer();
display(vfb); // render pixels on screen with SDL
}
cudaRenderer.cu
__global__ void kernel(Color* dev_vfb)
{
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x;
if (offset < RES_X * RES_Y)
{
dev_vfb[offset] = getColorForPixel();
}
}
extern "C" callKernels(Color* dev_vfb)
{
dim3 threadsPerBlock(32, 32)
dim3 blocks(screenWidth / threadsPerBlock.x, screenHeight / threadsPerBlock.y);
kernel<<<blocks, threadsPerBlock>>>(dev_vfb);
}
显示内容(vfb):
void display(Color vfb[VFB_MAX_SIZE][VFB_MAX_SIZE])
{
// screen is pointer to SDL_Surface
int rs = screen->format->Rshift;
int gs = screen->format->Gshift;
int bs = screen->format->Bshift;
for (int y = 0; y < screen->h; ++y)
{
Uint32* row = (Uint32*) ((Uint8*) screen->pixels + y * screen->pitch);
for (int x = 0; x < screen->w; ++x)
row[x] = vfb[y][x].toRGB32(rs, gs, bs);
}
SDL_Flip(screen);
}
这是我在项目中所做的一个简单示例。它是一个光线跟踪器,也许SDL是与CUDA互操作的最差选择,但我不知道我是否有时间改变它。
答案 0 :(得分:1)
没有什么可以阻止两个流处理一个设备的全局内存中的同一条数据。
正如我在评论中所说,我不认为这是让事情变得更快的合理方法。但是,对代码的修改将是这样的(在浏览器中编码,未经过测试):
__global__ void kernel(Color* dev_vfb, int slices)
{
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x;
if (offset < (RES_X * RES_Y/slices)
{
dev_vfb[offset] = getColorForPixel();
}
}
extern "C" callKernels(Color* dev_vfb)
{
int num_slices=2;
cudaStream_t streams[num_slices];
for (int i = 0; i < num_slices; i++)
cudaStreamCreate(&(streams[i]));
dim3 threadsPerBlock(32, 32)
dim3 blocks(screenWidth / threadsPerBlock.x, screenHeight / (num_slices*threadsPerBlock.y));
for (int i = 0; i < num_slices; i++){
int off = i * (screenWidth*screenHeight/num_slices);
kernel<<<blocks, threadsPerBlock, 0, streams[i]>>>(dev_vfb+off, num_slices); }
}