Cuda 带常数源的推力拷贝

Cuda 带常数源的推力拷贝,cuda,constants,gpu,thrust,const-iterator,Cuda,Constants,Gpu,Thrust,Const Iterator,我的问题出现在以下代码中: 当源不是常量时(迭代器会相应地进行调整),filter函数会编译并运行。但是,当我将source更改为const时,编译器会对copy_if语句的前两个变量给出以下错误: “对象具有与成员函数不兼容的类型限定符” 我相信在某个地方存在常量到非常量的转换错误,但坦率地说,我不知道在哪里。任何帮助都将不胜感激 #include "thrust\device_vector.h" #include "thrust\copy.h" typedef thrust::device

我的问题出现在以下代码中:

当源不是常量时(迭代器会相应地进行调整),filter函数会编译并运行。但是,当我将source更改为const时,编译器会对copy_if语句的前两个变量给出以下错误: “对象具有与成员函数不兼容的类型限定符”

我相信在某个地方存在常量到非常量的转换错误,但坦率地说,我不知道在哪里。任何帮助都将不胜感激

#include "thrust\device_vector.h"
#include "thrust\copy.h"

typedef thrust::device_vector<float>::const_iterator    Dc_FloatIterator;
typedef thrust::device_vector<float>::iterator          D_FloatIterator;

typedef thrust::device_vector<int>::const_iterator  Dc_IntIterator;
typedef thrust::device_vector<int>::iterator        D_IntIterator;

typedef thrust::tuple< Dc_IntIterator, Dc_IntIterator, Dc_FloatIterator> Dc_ListIteratorTuple;
typedef thrust::zip_iterator<Dc_ListIteratorTuple>   Dc_ListIterator;//type of the class const iterator

typedef thrust::tuple< D_IntIterator, D_IntIterator, D_FloatIterator > D_ListIteratorTuple;
typedef thrust::zip_iterator<D_ListIteratorTuple>    D_ListIterator;//type of the class iterator

struct selector{//selector functor for the copy if call
const int val;

selector(int _val) : val(_val) {}

__host__ __device__
bool operator()(const int& x ) {
return ( x == val );
   }
};

class Foo{    
public:
    thrust::device_vector<int>      ivec1;
    thrust::device_vector<int>      ivec2;
    thrust::device_vector<float>    fvec1;

    Foo(){;}
    ~Foo(){;}

    D_ListIterator begin(){//cast of begin iterator
        return D_ListIterator(D_ListIteratorTuple( ivec1.begin(), ivec2.begin(), fvec1.begin() ));
    }
    D_ListIterator end(){//cast of end iterator
        return D_ListIterator(D_ListIteratorTuple( ivec1.end(), ivec2.end(), fvec1.end() ));
    }

    Dc_ListIterator cbegin(){//cast of const begin iterator
        return Dc_ListIterator(Dc_ListIteratorTuple( ivec1.cbegin(), ivec2.cbegin(), fvec1.cbegin() ));
    }
    Dc_ListIterator cend(){//cast of const end iterator
        return Dc_ListIterator(Dc_ListIteratorTuple( ivec1.cend(), ivec2.cend(), fvec1.cend() ));
    }

    void const_filter( const Foo& TheOther, const int& target ){//doesnt work
        //This function should copy those member of the vectors where
        //the ivec2[i] == target is true
        thrust::copy_if(
            TheOther.cbegin(),
            TheOther.cend(),
            TheOther.ivec2.cbegin(),
            this->begin(),
            selector(target) );
    }
    void filter( Foo& TheOther, const int& target ){//works
        //This function should copy those member of the vectors where
        //the ivec2[i] == target is true
        thrust::copy_if(
            TheOther.begin(),
            TheOther.end(),
            TheOther.ivec2.cbegin(),
            this->begin(),
            selector(target) );
    }
    void insert(const int& one, const int& two,const float& three ){
        ivec1.push_back(one);
        ivec2.push_back(two);
        fvec1.push_back(three);
    }

    int size(){
        return ivec1.size();
    }
};

bool CheckIfSublistIsConnected(const Foo& list,const int& sublist_num){
Foo tmp;

tmp.const_filter( list, sublist_num );

return (bool)tmp.size();//for symplicity, othervise here is a function that check if
                        //the edge list represents a connected graph
}
int main(void){
Foo list;
bool connected;
list.insert(10,2,1.0);
list.insert(11,2,1.0);
list.insert(12,2,1.0);
list.insert(10,3,1.0);
list.insert(10,3,1.0);

connected=CheckIfSublistIsConnected(list,2); 

if( connected ) return 0;
else return -1;
}

当它出来的时候,我已经开始在cend/cbegin的定义中添加常量魔法词

Dc_ListIterator cbegin() const {
        return Dc_ListIterator(Dc_ListIteratorTuple( ivec1.cbegin(), ivec2.cbegin(), fvec1.cbegin() ));
    }
Dc_ListIterator cend() const {
        return Dc_ListIterator(Dc_ListIteratorTuple( ivec1.cend(), ivec2.cend(), fvec1.cend() ));
    }

您能否提供一个完整的示例,显示在传递/编译情况下对
Foo::filter
的调用,以及对
Foo::filter
的调用以及导致编译失败的参数?它并不是真正完成的。我不知道
cbegin()
cend()
是什么样子。您能否创建一个简单、可编译的示例来演示问题,但其中没有任何不必要的代码来演示问题?我想要一些我可以复制、粘贴和编译的东西,而无需编辑或添加任何内容。我编辑了一段代码,显示了问题,您可以尝试编译它。在构造这个示例时,我注意到如果filter(…)调用只接受一个Foo&(而不是const),并且我调用了cend和cbegin,那么它就可以工作了。然而在我的实际程序中,这个调用有5次调用深度,第一个函数也将Foo作为const引用,因此我必须将它作为const引用传递。我还将
const
添加到
cend
cbegin
定义中,然后(编译)问题就消失了。这是否意味着你已经解决了你的问题?如果是这样的话,请你把它作为一个答案贴出来,我会投赞成票。(这似乎不是一个重点问题?)。在这个项目中,我90%的bug都是与推力相关的,所以我认为这个也是。
Dc_ListIterator cbegin() const {
        return Dc_ListIterator(Dc_ListIteratorTuple( ivec1.cbegin(), ivec2.cbegin(), fvec1.cbegin() ));
    }
Dc_ListIterator cend() const {
        return Dc_ListIterator(Dc_ListIteratorTuple( ivec1.cend(), ivec2.cend(), fvec1.cend() ));
    }