想在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
我的“kernel.cu”文件是这样的
#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
}
我参加了visual studio 2008和CUDA 5.0。当我构建我的项目时,我遇到了这些错误:
error: calling a _host_ function("malloc") from a _global_ function("Init") is not allowed.
我想知道我该怎么做? (我知道'malloc'和其他cpu内存分配不允许用于设备内存。
答案 0 :(得分:0)
您需要编译器参数-arch=sm_20
和支持它的GPU。
答案 1 :(得分:0)
malloc
allowed in device code但你必须为cc2.0或更高目标GPU进行编译。
调整您的VS项目设置以删除compute_10,sm_10
等任何GPU设备设置,并将其替换为compute_20,sm_20
或更高版本以匹配您的GPU。 (并且,要运行该代码,您的GPU需要为cc2.0或更高版本。)