如何编译推力和c ++项目? [英] How to compile thrust and c++ project?
问题描述
这里是项目结构(只是一个测试项目):
sortbase.h
#include< iostream> ;
#include< thrust / device_vector.h>
using namespace std;
template< class T>
class SortBase
{
public:
void Init()
{
}
void resize(const int& x)
{
CV.resize(x);
cout<<resize succeed!<< endl;
}
private:
thrust :: device_vector< T>简历;
};
sorter.h
#ifndef __SORTER_H__
#define __SORTER_H__
#include< iostream>
#include< thrust / device_vector.h>
#includesortbase.h
using namespace std;
template< class T>
void ttest()
{cout<<tttttttttttttt; cout<< endl;}
template< class KEY>
class Sorter
{
public:
Sorter():CV(NULL)
{
CV = new SortBase< KEY>();
}
〜Sorter()
{
if(CV!= NULL)
{
delete CV;
CV = NULL;
}
}
__host__ __device__
virtual bool operator()(const KEY& x,const KEY& y)const
{
return(x& y)。
}
void test()
{
// ttest< KEY>();
resize(5);
}
private:
void resize(const int& x)
{
CV-> resize(x);
cout<<resize:<<< x<< endl;
}
private:
SortBase< KEY> *简历;
};
#endif
main.cpp / p>
#include< iostream>
#includeinclude / sorter.cu
using namespace std;
int main(int argc,char ** argv)
{
Sorter< int> * sorter = new Sorter< int>();
sorter-> test();
return 0;
}
我这样做:
nvcc -c sorter.cu
g ++ -c main.cpp -I / usr / local / cuda / include
g ++ sorter.o main。 o -o sorter
但第二步给出错误消息,大量的警告和错误消息。
谁可以帮助我?
如下所示的一些警告和错误消息:
/ usr / local / cuda / include / /cuda/for_each.inl:In function \\\‘RandomAccessIterator thrust :: detail :: device :: cuda :: for_each_n(RandomAccessIterator,Size,UnaryFunction)[With RandomAccessIterator = thrust :: detail :: normal_iterator< thrust :: device_ptr< int> ; >,Size = long unsigned int,UnaryFunction = thrust :: detail :: generate_functor< thrust :: detail :: fill_functor< int> >] \\\’:
/usr/local/cuda/include/thrust/detail/device/dispatch/for_each.h:56:从\\\‘RandomAccessIterator thrust :: detail :: device :: dispatch实例化: :for_each_n(RandomAccessIterator,Size,UnaryFunction,thrust :: detail :: cuda_device_space_tag)[with RandomAccessIterator = thrust :: detail :: normal_iterator< thrust :: device_ptr< int> >,Size = long unsigned int,UnaryFunction = thrust :: detail :: generate_functor< thrust :: detail :: fill_functor< int> >] \\\’
/usr/local/cuda/include/thrust/detail/device/for_each.inl:43:实例化于\\\‘OutputIterator thrust :: detail :: device :: for_each_n(OutputIterator,Size ,UnaryFunction)[with OutputIterator = thrust :: detail :: normal_iterator< thrust :: device_ptr< int> >,Size = long unsigned int,UnaryFunction = thrust :: detail :: generate_functor< thrust :: detail :: fill_functor< int> >] \\\’
/usr/local/cuda/include/thrust/detail/dispatch/for_each.h:84:从\\\‘OutputIterator启动launch :: detail :: dispatch :: for_each_n(OutputIterator,Size ,UnaryFunction,thrust :: device_space_tag)[with OutputIterator = thrust :: detail :: normal_iterator< thrust :: device_ptr< int> >,Size = long unsigned int,UnaryFunction = thrust :: detail :: generate_functor< thrust :: detail :: fill_functor< int> >] \\\’
/usr/local/cuda/include/thrust/detail/for_each.inl:41:实例化于\\\‘OutputIterator thrust :: detail :: for_each_n(OutputIterator,Size,UnaryFunction)[with OutputIterator = thrust :: detail :: normal_iterator< thrust :: device_ptr< int> >,Size = long unsigned int,UnaryFunction = thrust :: detail :: generate_functor< thrust :: detail :: fill_functor< int> >] \\\’
/usr/local/cuda/include/thrust/detail/generate.inl:47:从\\\‘OutputIterator启动launch :: generate_n(OutputIterator,Size,Generator)[with OutputIterator = thrust :: detail :: normal_iterator< thrust :: device_ptr< int> >,Size = long unsigned int,Generator = thrust :: detail :: fill_functor< int>] \\\’
/usr/local/cuda/include/thrust/detail/device/cuda/fill.inl: 87:实例化从\\\‘OutputIterator thrust :: detail :: device :: cuda :: detail :: fill_n(OutputIterator,Size,const T& thrust :: detail :: false_type)[with OutputIterator = thrust :: detail :: normal_iterator< thrust :: device_ptr< int> >,Size = long unsigned int,T = int] \\\’
/usr/local/cuda/include/thrust/detail/device/cuda/fill.inl:117:从\\\‘OutputIterator启动实例化推力: :detail :: device :: cuda :: detail :: fill_n(OutputIterator,Size,const T& thrust :: detail :: true_type)[with OutputIterator = thrust :: detail :: normal_iterator< thrust :: device_ptr< int> >,Size = long unsigned int,T = int] \\\’
/usr/local/cuda/include/thrust/detail/device/cuda/fill.inl:138:从\\\‘OutputIterator实例化推力: :detail :: device :: cuda :: fill_n(OutputIterator,Size,const T&)[with OutputIterator = thrust :: detail :: normal_iterator< thrust :: device_ptr< int> >,Size = long unsigned int,T = int] \\\’
/usr/local/cuda/include/thrust/detail/device/dispatch/fill.h:57:从\\\‘OutputIterator实例化推力: :detail :: device :: dispatch :: fill_n(OutputIterator,Size,const T& thrust :: detail :: cuda_device_space_tag)[with OutputIterator = thrust :: detail :: normal_iterator< thrust :: device_ptr< int> >,Size = long unsigned int,T = int] \\\’
/usr/local/cuda/include/thrust/detail/device/fill.inl:47:实例化来自\\\‘OutputIterator thrust :: detail :: device :: fill_n(OutputIterator,Size,const T&)[with OutputIterator = thrust :: detail :: normal_iterator< thrust :: device_ptr< int> >,Size = long unsigned int,T = int] \\\’
/usr/local/cuda/include/thrust/detail/dispatch/fill.h:76:从\\\‘OutputIterator实例化thrust :: detail :: dispatch :: fill_n(OutputIterator,Size,const T& thrust; device_space_tag)[with OutputIterator = thrust :: detail :: normal_iterator< thrust :: device_ptr< int> >,Size = long unsigned int,T = int] \\\’
/usr/local/cuda/include/thrust/detail/fill.inl:46:实例化来自\\\‘OutputIterator thrust :: fill_n(OutputIterator ,Size,const T&)[with OutputIterator = thrust :: detail :: normal_iterator< thrust :: device_ptr< int> >,Size = long unsigned int,T = int] \\\’
/usr/local/cuda/include/thrust/detail/vector_base.inl:757:实例化从\\\‘void thrust :: detail :: vector_base< T,Alloc> :: fill_insert(typename thrust :: detail :: contiguous_storage< T,Alloc> :: iterator,typename thrust :: detail :: contiguous_storage< T,Alloc> :: size_type,const T& T = int,Alloc = thrust :: device_malloc_allocator< int>] \\\’
/usr/local/cuda/include/thrust/detail/vector_base.inl:561:实例化从\\\‘void thrust :: details: :vector_base< T,Alloc> :: insert(typename thrust :: detail :: contiguous_storage< T,Alloc> :: iterator,typename thrust :: detail :: contiguous_storage< T,Alloc> :: size_type,const T& with T = int,Alloc = thrust :: device_malloc_allocator< int>] \\\’
/usr/local/cuda/include/thrust/detail/vector_base.inl:223:实例化从\\\‘void thrust :: detail :: vector_base< T,Alloc> :: resize(typename thrust :: detail :: contiguous_storage< T,Alloc> :: size_type,typename thrust :: detail :: contiguous_storage< T,Alloc> int,Alloc = thrust :: device_malloc_allocator< int>] \\\’
include / sortbase.h:14:从void SortBase T实例化:resize int]
include / sorter.cu:40:从void Sorter实例化< KEY> :: resize(const int&)[with KEY = int]
include / sorter.cu:35: Sorter< KEY> :: test()[with KEY = int]
main.cpp:9:从这里实例化
/ usr / local / cuda / include / thrust / detail / device / cuda / for_each .inl:93:错误:将sizeof应用于不完全类型推力:: detail :: STATIC_ASSERTION_FAILURE< false>
/usr/local/cuda/include/thrust/detail/device/cuda/arch.h:在全局范围:
/ usr / local / cuda / include / thrust / detail / device / cuda / arch.h:59:warning:inline function size_t thrust :: detail :: device :: cuda :: arch :: compute_capability()但从未定义
我认为问题是你正在从.cpp文件中包括.cu文件。推力代码只能从.cu文件使用,这些文件必须由NVCC编译,而不能由主机编译器g ++编译。因此,您需要将所有的Thrust和CUDA设备代码移动到可以从.cpp文件调用的函数中的.cu文件中。
I got big problems about the compile when I want to use thrust and c++ meanwhile.
Here is the project structure(just a test project):
sortbase.h
#include<iostream>
#include <thrust/device_vector.h>
using namespace std;
template<class T>
class SortBase
{
public:
void Init()
{
}
void resize(const int &x)
{
CV.resize(x);
cout<<"resize succeed!"<<endl;
}
private:
thrust::device_vector<T> CV;
};
sorter.h
#ifndef __SORTER_H__
#define __SORTER_H__
#include <iostream>
#include <thrust/device_vector.h>
#include "sortbase.h"
using namespace std;
template<class T>
void ttest()
{cout<<"tttttttttttttttt";cout<<endl;}
template<class KEY>
class Sorter
{
public:
Sorter():CV(NULL)
{
CV=new SortBase<KEY>();
}
~Sorter()
{
if (CV!=NULL)
{
delete CV;
CV=NULL;
}
}
__host__ __device__
virtual bool operator()(const KEY& x,const KEY& y)const
{
return (x<y);
}
void test()
{
//ttest<KEY>();
resize(5);
}
private:
void resize(const int &x)
{
CV->resize(x);
cout<<"resize: "<<x<<endl;
}
private:
SortBase<KEY> *CV;
};
#endif
main.cpp
#include <iostream>
#include "include/sorter.cu"
using namespace std;
int main(int argc,char **argv)
{
Sorter<int> *sorter=new Sorter<int>();
sorter->test();
return 0;
}
What I do like this:
nvcc -c sorter.cu
g++ -c main.cpp -I/usr/local/cuda/include
g++ sorter.o main.o -o sorter
but the second step gives the error message, a lot of warning and error message. who can help me? some warning and error message like these:
/usr/local/cuda/include/thrust/detail/device/cuda/for_each.inl: In function \u2018RandomAccessIterator thrust::detail::device::cuda::for_each_n(RandomAccessIterator, Size, UnaryFunction) [with RandomAccessIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, UnaryFunction = thrust::detail::generate_functor<thrust::detail::fill_functor<int> >]\u2019:
/usr/local/cuda/include/thrust/detail/device/dispatch/for_each.h:56: instantiated from \u2018RandomAccessIterator thrust::detail::device::dispatch::for_each_n(RandomAccessIterator, Size, UnaryFunction, thrust::detail::cuda_device_space_tag) [with RandomAccessIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, UnaryFunction = thrust::detail::generate_functor<thrust::detail::fill_functor<int> >]\u2019
/usr/local/cuda/include/thrust/detail/device/for_each.inl:43: instantiated from \u2018OutputIterator thrust::detail::device::for_each_n(OutputIterator, Size, UnaryFunction) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, UnaryFunction = thrust::detail::generate_functor<thrust::detail::fill_functor<int> >]\u2019
/usr/local/cuda/include/thrust/detail/dispatch/for_each.h:84: instantiated from \u2018OutputIterator thrust::detail::dispatch::for_each_n(OutputIterator, Size, UnaryFunction, thrust::device_space_tag) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, UnaryFunction = thrust::detail::generate_functor<thrust::detail::fill_functor<int> >]\u2019
/usr/local/cuda/include/thrust/detail/for_each.inl:41: instantiated from \u2018OutputIterator thrust::detail::for_each_n(OutputIterator, Size, UnaryFunction) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, UnaryFunction = thrust::detail::generate_functor<thrust::detail::fill_functor<int> >]\u2019
/usr/local/cuda/include/thrust/detail/generate.inl:47: instantiated from \u2018OutputIterator thrust::generate_n(OutputIterator, Size, Generator) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, Generator = thrust::detail::fill_functor<int>]\u2019
/usr/local/cuda/include/thrust/detail/device/cuda/fill.inl:87: instantiated from \u2018OutputIterator thrust::detail::device::cuda::detail::fill_n(OutputIterator, Size, const T&, thrust::detail::false_type) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, T = int]\u2019
/usr/local/cuda/include/thrust/detail/device/cuda/fill.inl:117: instantiated from \u2018OutputIterator thrust::detail::device::cuda::detail::fill_n(OutputIterator, Size, const T&, thrust::detail::true_type) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, T = int]\u2019
/usr/local/cuda/include/thrust/detail/device/cuda/fill.inl:138: instantiated from \u2018OutputIterator thrust::detail::device::cuda::fill_n(OutputIterator, Size, const T&) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, T = int]\u2019
/usr/local/cuda/include/thrust/detail/device/dispatch/fill.h:57: instantiated from \u2018OutputIterator thrust::detail::device::dispatch::fill_n(OutputIterator, Size, const T&, thrust::detail::cuda_device_space_tag) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, T = int]\u2019
/usr/local/cuda/include/thrust/detail/device/fill.inl:47: instantiated from \u2018OutputIterator thrust::detail::device::fill_n(OutputIterator, Size, const T&) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, T = int]\u2019
/usr/local/cuda/include/thrust/detail/dispatch/fill.h:76: instantiated from \u2018OutputIterator thrust::detail::dispatch::fill_n(OutputIterator, Size, const T&, thrust::device_space_tag) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, T = int]\u2019
/usr/local/cuda/include/thrust/detail/fill.inl:46: instantiated from \u2018OutputIterator thrust::fill_n(OutputIterator, Size, const T&) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, T = int]\u2019
/usr/local/cuda/include/thrust/detail/vector_base.inl:757: instantiated from \u2018void thrust::detail::vector_base<T, Alloc>::fill_insert(typename thrust::detail::contiguous_storage<T, Alloc>::iterator, typename thrust::detail::contiguous_storage<T, Alloc>::size_type, const T&) [with T = int, Alloc = thrust::device_malloc_allocator<int>]\u2019
/usr/local/cuda/include/thrust/detail/vector_base.inl:561: instantiated from \u2018void thrust::detail::vector_base<T, Alloc>::insert(typename thrust::detail::contiguous_storage<T, Alloc>::iterator, typename thrust::detail::contiguous_storage<T, Alloc>::size_type, const T&) [with T = int, Alloc = thrust::device_malloc_allocator<int>]\u2019
/usr/local/cuda/include/thrust/detail/vector_base.inl:223: instantiated from \u2018void thrust::detail::vector_base<T, Alloc>::resize(typename thrust::detail::contiguous_storage<T, Alloc>::size_type, typename thrust::detail::contiguous_storage<T, Alloc>::value_type) [with T = int, Alloc = thrust::device_malloc_allocator<int>]\u2019
include/sortbase.h:14: instantiated from void SortBase<T>::resize(const int&) [with T = int]
include/sorter.cu:40: instantiated from void Sorter<KEY>::resize(const int&) [with KEY = int]
include/sorter.cu:35: instantiated from void Sorter<KEY>::test() [with KEY = int]
main.cpp:9: instantiated from here
/usr/local/cuda/include/thrust/detail/device/cuda/for_each.inl:93: error: invalid application of sizeof to incomplete type thrust::detail::STATIC_ASSERTION_FAILURE<false>
/usr/local/cuda/include/thrust/detail/device/cuda/arch.h: At global scope:
/usr/local/cuda/include/thrust/detail/device/cuda/arch.h:59: warning: inline function size_t thrust::detail::device::cuda::arch::compute_capability() used but never defined
I think that the problem is you are including your .cu file from a .cpp file. Thrust code can only be used from .cu files, which must be compiled by NVCC, not by the host compiler g++. So you need to move all of your Thrust and CUDA device code inside .cu files in functions that can be called from your .cpp files.
这篇关于如何编译推力和c ++项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!