2016-08-19 2 views
0

반복기의 색인/위치를 찾으려고 할 때 thrust :: distance()를 사용합니다. 그러나 이상한 가치를 반환합니다. 벡터 크기는 10입니다.이 메서드를 사용하면 "131"값이 반환됩니다. 여기에 완전히 작동하는 예제입니다.추력 장치 벡터 반복기 위치

#include <thrust/device_vector.h> 
#include <thrust/host_vector.h> 
#include <thrust/reduce.h> 
#include <thrust/extrema.h> 
#include <iostream> 
#include <iomanip> 
#include <thrust/sort.h> 
#include <thrust/copy.h> 
#include <thrust/random.h> 
#include <thrust/unique.h> 
#include <thrust/reduce.h> 
#include <thrust/iterator/constant_iterator.h> 

using namespace std; 
template <typename Vector> 
void print_vector(const std::string& name, const Vector& v) 
{ 
    typedef typename Vector::value_type T; 
    std::cout << " " << std::setw(20) << name << " "; 
    thrust::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, "")); 
    std::cout << std::endl; 
} 

int main() 
{ 
thrust::device_vector<int> x; 
x.push_back(1); 
x.push_back(10); 
x.push_back(1); 
x.push_back(11); 
x.push_back(1); 
x.push_back(11); 
thrust::device_vector<int> y(10); 

print_vector("Original",x); 

thrust::sort(x.begin(),x.end()); 
print_vector("sort",x); 

thrust::device_vector<int>::iterator it=thrust::unique(x.begin(),x.end()); 
std::cout<<*it<<std::endl; 

//int newsize=it-y.begin(); 
int newsize=thrust::distance(y.begin(),it); 
cout<<"nsz:"<<newsize<<endl; 

return 0; 
} 

답변

0

반복자 it는 벡터 x에 대한 설립 :

thrust::device_vector<int>::iterator it=thrust::unique(x.begin(),x.end()); 
                ^  ^

하지만 당신은 벡터 y의 시작이 반복자로부터의 거리를 요구하고 :

int newsize=thrust::distance(y.begin(),it); 
          ^

그건 말이되지 않습니다. it과 벡터 y 사이에는 정의 된 관계가 없습니다.

당신이 벡터의 시작 부분까지의 거리를 요청할 경우 x 대신, 좀 더 합리적인 결과를 얻을 것이다 :

$ cat t1244.cu 
#include <thrust/device_vector.h> 
#include <thrust/host_vector.h> 
#include <thrust/reduce.h> 
#include <thrust/extrema.h> 
#include <iostream> 
#include <iomanip> 
#include <thrust/sort.h> 
#include <thrust/copy.h> 
#include <thrust/random.h> 
#include <thrust/unique.h> 
#include <thrust/reduce.h> 
#include <thrust/iterator/constant_iterator.h> 

using namespace std; 
template <typename Vector> 
void print_vector(const std::string& name, const Vector& v) 
{ 
    typedef typename Vector::value_type T; 
    std::cout << " " << std::setw(20) << name << " "; 
    thrust::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " ")); 
    std::cout << std::endl; 
} 

int main() 
{ 
thrust::device_vector<int> x; 
x.push_back(1); 
x.push_back(10); 
x.push_back(1); 
x.push_back(11); 
x.push_back(1); 
x.push_back(11); 
thrust::device_vector<int> y(10); 

print_vector("Original",x); 

thrust::sort(x.begin(),x.end()); 
print_vector("sort",x); 

thrust::device_vector<int>::iterator it=thrust::unique(x.begin(),x.end()); 
std::cout<<*it<<std::endl; 

//int newsize=it-y.begin(); 
int newsize=thrust::distance(x.begin(),it); 
cout<<"nsz:"<<newsize<<endl; 

return 0; 
} 
$ nvcc -o t1244 t1244.cu 
$ ./t1244 
       Original 1 10 1 11 1 11 
        sort 1 1 1 10 11 11 
10 
nsz:3 
$ 
+0

덕분에 많이. 당신의 대답은 나를 잘못 보여줍니다 :) 그것은 나에게 큰 실수입니다. –