【发布时间】:2022-04-28 05:46:34
【问题描述】:
将 CUDA 5 与 VS 2012 和功能 3.5(Titan 和 K20)一起使用。
在内核执行的特定阶段,我想将生成的数据块发送到主机内存并通知主机数据已准备好,以便主机对其进行操作。
我不能等到内核执行结束才从设备读回数据,因为:
- 数据一经计算就不再与设备相关,因此将其保留到最后没有意义。
- 数据量太大,设备内存无法容纳,请等到最后。
- 主机不必等到内核执行结束才开始处理数据。
您能否指出我必须采取的路径以及我必须使用哪些 cuda 概念和功能来实现我的要求?简而言之,如何写入主机并通知主机一个块数据已准备好供主机处理?
注意每个线程不与任何其他线程共享任何生成的数据,它们独立运行。所以,据我所知(如果我错了,请纠正我),块、线程和扭曲的概念不会影响问题。或者换句话说,如果它们有助于答案,我可以随意更改它们的组合。
以下是显示我正在尝试做的示例代码:
#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");
}
【问题讨论】:
-
数据块是如何以及何时生成的?几个块会生成一个块吗?或者每个块是否由来自所有块的数据组成,在块执行期间的不同时间写入?在后一种情况下,您需要注意数据的生成分布在内核的整个运行时。
-
我更新了我的问题以反映您问题的答案。
标签: cuda