如何从单独的函数调用cudamaloc?

如何从单独的函数调用cudamaloc?,cuda,Cuda,我正在学习cuda,并尝试编写一个函数,以类似于主机上的方式在设备上分配内存。例如: //host float* allocate1D_float(int size) { float* array = (float*)malloc(size* sizeof(float)); if (array==NULL) { printf("\n Error allocating memory 1\n"); free(array);

我正在学习cuda,并尝试编写一个函数,以类似于主机上的方式在设备上分配内存。例如:

//host
float* allocate1D_float(int size)
{
    float* array = (float*)malloc(size* sizeof(float));
    if (array==NULL)
    {
        printf("\n Error allocating memory 1\n");
            free(array);
        exit(EXIT_FAILURE);
    }
    return array;
}
float *h_A = allocate1D_float(numElements);

//device
float* alloc_cuda1D_float(int numElements)
{
    float *d_array = NULL;
    size_t size = numElements * sizeof(float);
    cudaError_t err = cudaSuccess;
    err = cudaMalloc((void **)&d_array, size);

    if (err != cudaSuccess)
    {
        fprintf(stderr, "Failed to allocate device vector (error code %s)!\n", cudaGetErrorString(err));
        exit(EXIT_FAILURE);
    }
    return d_array;
}
float *d_A = alloc_cuda1D_float(int numElements);
然而,nvcc一直在说 错误:不允许使用类型名称 错误:应为“)” 主机功能正常时,用于设备功能。希望你能帮我解决这个问题

谢谢。

关于“不允许输入名称”:

您这样做是正确的:

float *h_A = allocate1D_float(numElements);
但这是错误的:

float *d_A = alloc_cuda1D_float(int numElements);
                                ^^^
                                This int shouldn't be here
因此,移除
numElements


这当然与CUDA无关。如果您试图将
int
放在不属于该调用的位置,则主机函数调用可能会出现类似错误。

如果您有特定的编译错误需要帮助,则必须向我们显示产生错误的确切代码,以及用于编译代码的确切命令,以及编译失败时编译器发出的确切错误消息。正如上面所写的,你的问题是非常不明确的。这真是一个令人尴尬的错误!我想我需要咖啡。