Error compiling Cuda - expected primary-expression

你说的曾经没有我的故事 提交于 2019-12-05 15:09:21

问题


this program seems be fine but I still getting an erro, some suggestion?

Program:

#include "dot.h"
#include <cuda.h>
#include <cuda_runtime.h>
#include <stdio.h>

int main(int argc, char** argv)
{
    int *a, *b, *c;
    int *dev_a, *dev_b, *dev_c;
    int size = N * sizeof(int);

    cudaMalloc((void**)&dev_a, size);
    cudaMalloc((void**)&dev_b, size);
    cudaMalloc((void**)&dev_c, sizeof(int));

    a = (int *)malloc (size);
    b = (int *)malloc (size);
    c = (int *)malloc (sizeof(int));

    random_ints(a, N);
    random_ints(b, N);

    cudaMemcpy(dev_a, a, size, cudaMemcpyHostToDevice);
    cudaMemcpy(dev_b, b, size, cudaMemcpyHostToDevice);

    int res = N/THREADS_PER_BLOCK;
    dot<<< res, THREADS_PER_BLOCK >>> (dev_a, dev_b, dev_c);
    //helloWorld<<< dimGrid, dimBlock >>>(d_str);

    cudaMemcpy (c, dev_c, sizeof(int), cudaMemcpyDeviceToHost);

    free(a); free(b); free(c);
    cudaFree(dev_a);
    cudaFree(dev_b);
    cudaFree(dev_c);
    return 0;
}

the error:

DotProductCuda.cpp:27: error: expected primary-expression before '<' token
DotProductCuda.cpp:27: error: expected primary-expression before '>' token


回答1:


The <<< >>> syntax for calling a kernel is not standard C or C++. Those calls must be in a file compiled by the NVCC compiler. Those files are normally named with a .cu extension. Other API calls to CUDA such as cudaMalloc can be in regular .c or .cpp files.




回答2:


It seems the compiler cannot recognize the <<<,>>> syntax. I have no experience with CUDA, but I guess you need to compile this file with a special compiler and not an ordinary C compiler.




回答3:


nvcc uses the file extension to determine how to process the contents of the file. If you have CUDA syntax inside the file, it must have a .cu extension, otherwise nvcc will simply pass the file untouched to the host compiler, resulting in the syntax error you are observing.




回答4:


Maybe you use a host function (printf for example) inside kernel?



来源:https://stackoverflow.com/questions/5986070/error-compiling-cuda-expected-primary-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!