2013-06-03 3 views
2

저는 CUDA와 Thrust와 함께 일하고 있습니다. thrust::transform [plus/minus/divide]을 입력하는 것이 지루할 정도로 단순한 연산자를 너무 많이 사용하고 싶습니다.추력을위한 오버로드 "+"아이디어가 있습니까?

내가 할 수 있다면 좋지 않을까 :

template <typename T> 
__host__ __device__ T& operator+(T &lhs, const T &rhs) { 
    thrust::transform(rhs.begin(), rhs.end(), 
         lhs.begin(), lhs.end(), thrust::plus<?>()); 
    return lhs; 
} 

그러나, thrust::plus<?>가 제대로 오버로드되지 않았거나 나는 일을 아니에요 : 여기

thrust::[host/device]_vector<float> host; 
thrust::[host/device]_vector<float> otherHost; 
thrust::[host/device]_vector<float> result = host + otherHost; 

+에 대한 예제 코드 조각입니다 그것은 정확하게 ... 하나 또는 다른 것. (간단한 연산자를 오버로딩하는 것이 좋지 않은 경우 이유를 설명하십시오.) 처음에는 자리 표시 자에 typename T::iterator과 같은 내용으로 오버로드 할 수 있다고 생각했지만 작동하지 않았습니다.

+ 연산자에 벡터 유형의 벡터 반복기를 오버로드하는 방법을 모르겠습니다. 이게 말이 돼?

도움 주셔서 감사합니다.

이 다른 사람들이 더 나은 아이디어가있을 수 있습니다 작동하는 것 같다
+0

''의 의미는 무엇입니까? – Elazar

+0

@Elazar 그것은 내가 무엇을 넣어야할지 모르겠다는 것을 의미합니다. 어쩌면 어떤 종류의'T :: iterator' 유형 또는 뭔가. –

+0

방금 ​​편집했습니다 –

답변

2

: 어떤 경우에, 나는 자동으로 생성 추력 차이를 볼 수 있기 때문에, 결과에 대한 호스트 또는 디바이스 벡터를 지정할 수 있다는

#include <ostream> 
#include <thrust/host_vector.h> 
#include <thrust/device_vector.h> 
#include <thrust/transform.h> 
#include <thrust/functional.h> 
#include <thrust/copy.h> 
#include <thrust/fill.h> 

#define DSIZE 10 


template <typename T> 
thrust::device_vector<T> operator+(thrust::device_vector<T> &lhs, const thrust::device_vector<T> &rhs) { 
    thrust::transform(rhs.begin(), rhs.end(), 
         lhs.begin(), lhs.begin(), thrust::plus<T>()); 
    return lhs; 
} 

template <typename T> 
thrust::host_vector<T> operator+(thrust::host_vector<T> &lhs, const thrust::host_vector<T> &rhs) { 
    thrust::transform(rhs.begin(), rhs.end(), 
         lhs.begin(), lhs.begin(), thrust::plus<T>()); 
    return lhs; 
} 
int main() { 


    thrust::device_vector<float> dvec(DSIZE); 
    thrust::device_vector<float> otherdvec(DSIZE); 
    thrust::fill(dvec.begin(), dvec.end(), 1.0f); 
    thrust::fill(otherdvec.begin(), otherdvec.end(), 2.0f); 
    thrust::host_vector<float> hresult1 = dvec + otherdvec; 

    std::cout << "result 1: "; 
    thrust::copy(hresult1.begin(), hresult1.end(), std::ostream_iterator<float>(std::cout, " ")); std::cout << std::endl; 

    thrust::host_vector<float> hvec(DSIZE); 
    thrust::fill(hvec.begin(), hvec.end(), 5.0f); 
    thrust::host_vector<float> hresult2 = hvec + hresult1; 


    std::cout << "result 2: "; 
    thrust::copy(hresult2.begin(), hresult2.end(), std::ostream_iterator<float>(std::cout, " ")); std::cout << std::endl; 

    // this line would produce a compile error: 
    // thrust::host_vector<float> hresult3 = dvec + hvec; 

    return 0; 
} 

주를 필요한 복사 작업. 따라서 내 템플릿의 결과 벡터 유형 (호스트, 장치)은 중요하지 않습니다.

또한 템플릿 정의에 사용 된 thrust::transform 함수 매개 변수가 올바르지 않습니다.

+0

아, 그게 효과가 있다고 생각합니다. 나는 아침에 일하러 돌아올 때 그것을 확인합니다. –

+1

내가 지적 했어야 할 또 하나의 이유는 (내가 생각하기에) 당신은 내부 변환을 제안했기 때문입니다. (이것이 왜 당신이'lhs '를 반환 할 것을 제안했는지, 당신의 관습을 따랐습니다), 그 효과는 하나입니다 두 피연산자 (lhs)의 값이 겹쳐 쓰여지고 있습니다. 그래서 이것은 결과가 vec1 + vec2;가'* result'와'vec1' 둘 다에 답을 넣는 약간 비 직관적 인 행동을 만들지도 모릅니다. –

관련 문제