为cuda中的_global uu函数分配特定设备内存

为cuda中的_global uu函数分配特定设备内存,cuda,pycuda,Cuda,Pycuda,想在cuda上做这个节目 1.in main.cpp struct Center{ double * Data; int dimension; }; typedef struct Center Center; //I allow a pointer on N Center elements by the CUDAMALLOC like follow .... #include "kernel.cu" .... center *V_dev; int M =100, n=4; cudaSta

想在cuda上做这个节目

1.in main.cpp

struct Center{
double * Data;
int dimension;
};
typedef struct Center Center;

//I allow a pointer on N Center elements by the CUDAMALLOC like follow

....
#include "kernel.cu"
....
center *V_dev;
int M =100, n=4; 

cudaStatus = cudaMalloc((void**)&V_dev,M*sizeof(Center));
Init<<<1,M>>>(V_dev, M, N); //I always know the dimension of N before calling

我想知道我该怎么做?我知道“malloc”和其他cpu内存分配不允许用于设备内存。

您需要编译器参数-arch=sm_20和支持它的GPU。

malloc是,但您必须为cc2.0或更高的目标GPU进行编译


调整VS project设置以删除任何GPU设备设置,如compute_10、sm_10,并将其替换为compute_20、sm_20或更高版本,以匹配您的GPU。而且,要运行该代码,您的GPU必须是cc2.0或更高版本。

我的GPU设备是Geforce 210,cuda功能主要/次要版本号为1.2。这是否意味着它允许我的设备更改项目设置compute_10、sm_10以执行我的操作?当我列出我的项目的CUDA运行时API/GPU时,我有以下内容:GPU架构1:sm_10 GPU架构2:sm_20。这意味着什么?那么,不,你不能从设备代码中执行malloc。相反,计算出需要多少内存,在内核调用之前使用cudamaloc分配内存,并将指向它的指针传递给内核。您可以通过这种方式轻松创建一个double数组,然后在内核代码中为每个V[threadIdx.x]分配指向该数组中不同位置的数据。这是我尝试过的方法,但如何为每个中心留出足够的空间。数据?我可以这样做吗?代码cudaMallocvoid**&V_dev,M*sizeofCenter*n@dadson sizeofCenter等于sizeofdouble*+SizeofFint,我认为这不是你想要的。如果您知道维度,即N,即使在编译时,也可以替换double*数据;使用静态大小的数组双数据[N];。那么你发布的代码应该可以正常工作。
#include "cuda_runtime.h"
#include"device_launch_parameters.h"
... //other include headers to allow my .cu file to know the Center type definition

__global__ void Init(Center *V, int N, int dimension){
V[threadIdx.x].dimension = dimension;
V[threadIdx.x].Data = (double*)malloc(dimension*sizeof(double));
for(int i=0; i<dimension; i++)
    V[threadIdx.x].Data[i] = 0; //For the value, it can be any kind of operation returning a float that i want to be able put here

} 
error: calling a _host_ function("malloc") from a _global_ function("Init") is not allowed.