链接CUDA中的错误
Linking error in Cuda
我遇到问题,试图构建基本的cuda/推力代码,以使GPU编程更熟悉。我可能没有正确编译它,所以我想知道我在做什么错?
我正在使用以下说明构建
nvcc -c gpu_functions.cu
nvcc gpu_functions.o gpu_test.cu -o gpu_test
但是,我会收到一个链接错误:
jim@pezbox:~/dev/analytics/src$ nvcc gpu_functions.o gpu_test.cu -o gpu_test
/tmp/tmpxft_00002383_00000000-14_gpu_test.o: In function `main':
tmpxft_00002383_00000000-3_gpu_test.cudafe1.cpp:(.text+0x6e): undefined reference to `void add<thrust::device_vector<int, thrust::device_malloc_allocator<int> > >(thrust::device_vector<int, thrust::device_malloc_allocator<int> > const&, thrust::device_vector<int, thrust::device_malloc_allocator<int> > const&, thrust::device_vector<int, thrust::device_malloc_allocator<int> >&)'
collect2: ld returned 1 exit status
我有三个文件:
- gpu_functions.h(gpu函数的标头函数)
- gpu_functions.cu(gpu函数的实现)
- gpu_test.cu(调用我定义的gpu函数的主要循环)
gpu_functions.h
template<typename Vector>
void add(const Vector& in1, const Vector& in2, Vector& out);
gpu_functions.cu
#include "gpu_functions.h"
#include <thrust/sequence.h>
#include <thrust/transform.h>
#include <thrust/sequence.h>
#include <thrust/copy.h>
#include <thrust/fill.h>
#include <thrust/replace.h>
#include <thrust/functional.h>
using namespace thrust;
template<typename Vector>
void add(const Vector& in1, const Vector& in2, Vector& out) {
transform(in1.begin(), in1.end(), in2.begin(), out.begin(),
plus<typename Vector::value_type>());
}
gpu_test.cu
#include "piston_functions.h"
#include <thrust/device_vector.h>
#include <iostream>
#include <stdio.h>
using namespace thrust;
int main(void) {
const int n = 100000000;
// allocate three device_vectors with 10 elements
device_vector<int> in1(n, 1);
device_vector<int> in2(n, 2);
device_vector<int> out(n, 0);
add(in1, in2, out);
thrust::copy(out.begin(), out.begin()+10, std::ostream_iterator<int>(std::cout,"n"));
return 0;
}
我可能在做一些愚蠢的事情,或者我错过了很明显的事情。
一旦声明,模板函数需要明确或隐式的实例化,即,为模板参数的特定组合生成具体函数(实例)。
在gpu_functions.cu
编译单元中,您两者都缺少。换句话说,编译器没有生成函数add
的实例,因此链接器找不到任何链接的内容。
您应该通过在隐式实例化的位置(即包含main
函数的汇编单元)中包含模板函数声明来解决此问题。
换句话说,下面的代码将正确编译
#include <thrust/device_vector.h>
#include <iostream>
#include <stdio.h>
using namespace thrust;
template<typename Vector>
void add(const Vector& in1, const Vector& in2, Vector& out) {
transform(in1.begin(), in1.end(), in2.begin(), out.begin(),
plus<typename Vector::value_type>());
}
int main(void) {
const int n = 100000000;
device_vector<int> in1(n, 1);
device_vector<int> in2(n, 2);
device_vector<int> out(n, 0);
add(in1, in2, out);
thrust::copy(out.begin(), out.begin()+10, std::ostream_iterator<int>(std::cout,"n"));
return 0;
}
当然,您可以在单独的.cuh
文件中移动模板函数声明,并通过#include
指令将其包括在内。
最后,请记住要添加CUDA错误检查。
相关文章:
- cuda-gdb 给出错误"warning: Cuda API error detected: cudaLaunchKernel returned (0x7)"
- CUDA返回值错误35的含义是什么
- 链接器错误-在ubuntu中找不到CMake和Cuda 10.1的lcudart
- 在尝试使用CUDA分配内存时,我遇到了访问冲突写入位置错误
- Cuda 编译错误:已定义类模板
- 致命错误:cuda.h:没有用于 c++ 的此类文件或目录
- 带有大结构变量的 CUDA 内核函数给出了错误的结果
- NPP 和 CUDA 错误检查
- cmake cuda在Windows上单独的编译静态LIB链接错误,但在Ubuntu上没有
- GCC(通过 CUDA)内部函数的编译器错误,但我没有使用任何
- 推力(cuda)错误:无法使用给定参数列表调用函数
- cuda 内核调用/传递参数中的编译错误
- 使用 QtCreator 构建 CUDA/C++ 测试程序时出现错误 2
- CUDA 错误:"dynamic initialization is not supported for __device__, __constant__ and __shared__ variabl
- CUDA 编译错误:未定义__hmul和__hneg
- CUDA 模板错误:没有与参数列表匹配的函数模板实例
- Microsoft Visual Studio发现我所有的CUDA设备端函数都有错误
- 如何调试错误代码 77:cudaErrorIllegal地址当 cuda-memcheck 没有发现问题时
- 为什么 CUDA .cu 文件中会出现多个定义错误
- 咖啡错误 == cuda成功(77 与 0)遇到非法内存访问