[英]CUDA global memory
this is my code 这是我的代码
#include "stdafx.h"
#include <iostream>
using namespace std;
#define n 10
__device__ int glMem[n];
__global__ void initVals()
{
for(int i=0;i<n;i++)
glMem[i] = 0;
}
__global__ void test(int *out)
{
for(int i=0;i<n;i++)
out[i] = 10;
}
int main()
{
const size_t sz = size_t(n)*sizeof(int);
initVals<<<1,1>>>();
int *devMem;
cudaMalloc((void **)&devMem, sz);
test<<<1, 1>>>(devMem);
int *hoMem=new int[n];
cudaMemcpy(hoMem, devMem,sz, cudaMemcpyDeviceToHost);
//print
for(int i=0;i<n;i++)
cout<<hoMem[i]<<endl;
return 0;
}
IN this code I define 我在这段代码中定义
glMem
to size n. 到大小 If I dont know the size earlier hw can I define?? 如果我不知道以前的尺寸,我可以定义吗? for example I need to define like this. 例如,我需要这样定义。
__device__ int *glMem;
It doesnt work. 它不起作用。 Please give some code sample.. 请提供一些代码示例。
In that case you need to allocate the memory into the device. 在这种情况下,您需要将内存分配到设备中。
// size of data
unsigned int size_of_glMem = n * sizeof(int);
// allocate device memory for result
int* glMem = NULL;
cudaMalloc( (void**) &glMem, size_of_glMem );
Hope this help. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.