使用CUDA 5与VS 2012和功能3.5(Titan和K20)。从设备写入主机并通知主机
在内核执行的特定阶段,我想将生成的数据块发送到主机内存,并通知主机数据已准备就绪,因此主机将对其进行操作。
我不能等到内核执行结束读取数据从设备背面,因为:
- 一旦计算出的数据不再与设备相关的,所以没有点保持最后。
- 数据大小太大而无法放在设备内存上,并等到结束。
- 主机不应该等到内核执行结束才开始处理数据。
你能指出我我必须采取的路径和可能的CUDA概念和功能,我必须使用达到我的要求?简而言之,如何写入主机并通知主机块数据已准备好进行主机处理?
N.B.每个线程不会与任何其他线程共享任何生成的数据,它们将独立运行。所以,据我所知(如果我错了,请纠正我),块,线和经纱的概念不会影响问题。换句话说,如果他们帮助答案,我可以自由改变他们的组合。
下面是一个示例代码,显示我试图做的事:
#pragma once
#include <conio.h>
#include <cstdio>
#include <cuda_runtime_api.h>
__global__ void Kernel(size_t length, float* hResult)
{
int tid = threadIdx.x + blockIdx.x * blockDim.x;
// Processing multiple data chunks
for(int i = 0;i < length;i++)
{
// Once this is assigned, I don't need it on the device anymore.
hResult[i + (tid * length)] = i * 100;
}
}
void main()
{
size_t length = 10;
size_t threads = 2;
float* hResult;
// An array that will hold all data from all threads
cudaMallocHost((void**)&hResult, threads * length * sizeof(float));
Kernel<<<threads,1>>>(length, hResult);
// I DO NOT want to wait to the end and block to get the data
cudaError_t error = cudaDeviceSynchronize();
if (error != cudaSuccess) { throw error; }
for(int i = 0;i < threads * length;i++)
{
printf("%f\n", hResult[i]);;
}
cudaFreeHost(hResult);
system("pause");
}
如何以及何时生成数据块?两个块是否会生成一个块?还是每个块都由来自所有块的数据组成,在块执行期间的不同时间写入?在后一种情况下,您需要知道数据的生成遍布整个内核运行时。 – tera 2013-05-06 10:00:36
我更新了我的问题以反映您的问题的答案。 – Adam 2013-05-06 13:24:50