编译 Cuda 时出错 - 预期的主要表达式
这个程序看起来不错,但我仍然收到错误,有什么建议吗?
程序:
#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;
}
错误:
DotProductCuda.cpp:27: 错误:
'<'
标记之前应有主表达式
DotProductCuda.cpp:27: 错误:'>'
标记之前需要主表达式
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
<代码><<<用于调用内核的 >>> 语法不是标准 C 或 C++。这些调用必须位于 NVCC 编译器编译的文件中。这些文件通常以 .cu 扩展名命名。对 CUDA 的其他 API 调用(例如
cudaMalloc
)可以位于常规 .c 或 .cpp 文件中。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 ascudaMalloc
can be in regular .c or .cpp files.nvcc
使用文件扩展名来确定如何处理文件的内容。如果文件中有 CUDA 语法,则它必须具有 .cu 扩展名,否则 nvcc 只会将文件原封不动地传递给主机编译器,从而导致您观察到的语法错误。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.编译器似乎无法识别 <<<,>>>句法。我没有使用 CUDA 的经验,但我猜你需要使用特殊的编译器而不是普通的 C 编译器来编译这个文件。
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.
也许您在内核中使用主机函数(例如 printf)?
Maybe you use a host function (printf for example) inside kernel?