2016-04-05 11 views
5

tôi muốn để tính \left | \vec{a} - \vec{b} \right | cho vectơ \vec{a}\vec{b}, nơi \left | \vec{x} \right | biểu thị độ lớn của vector \vec{x}. Vì điều này liên quan đến việc lấy căn bậc hai của tổng các bình phương của sự khác biệt giữa mỗi thành phần tương ứng của hai vectơ, nên nó là một nhiệm vụ có khả năng song song cao. Tôi đang sử dụng Cuda và Thrust, thông qua Cygwin, trên Windows 10. Cả Cuda và Thrust đều đang hoạt động chung.CUDA đẩy tuple zip_iterator transform_reduce

Mã dưới đây biên dịch và chạy (với nvcc), nhưng chỉ vì tôi đã nhận xét ba dòng về phía dưới cùng của main, mỗi trong số đó tôi nghĩ rằng nên làm việc nhưng không. func::operator()(tup t) nghĩ rằng các đối số mà tôi truyền đi không thực sự là loại tup.

Tôi cũng đã nhận xét về cơ thể thực sự của nhà điều hành, vì lợi ích làm cho nó có nhiều khả năng biên dịch ít nhất. Toán tử được cho là tìm ra sự khác biệt bình phương giữa các phần tử của tup đầu vào. Việc giảm unary_op từ transform_reduce (trong trường hợp này là func()) sau đó sẽ thêm những điều này, cho tôi chỉ tiêu bình phương về sự khác biệt của vectơ.

#include <iostream> 
#include <stdlib.h> 
#include <thrust/device_vector.h> 
#include <thrust/transform.h> 
#include <thrust/tuple.h> 
#include <thrust/transform_reduce.h> 
#include <thrust/iterator/zip_iterator.h> 

typedef thrust::device_vector<float> dvec; 
typedef dvec::iterator iter; 
typedef thrust::tuple<iter, iter> tup; 

struct func: public thrust::unary_function<tup, float> 
{ 
    __device__ float operator()(tup t) //difsq 
    { 
    // I've commented out these two lines for testing purposes: 
    // float f = thrust::get<0>(t) - thrust::get<1>(t); 
    // return f*f; 
    return 3.14; 
    } 
}; 

int main() 
{ 
    dvec a(40, 4.f); 
    dvec b(40, 3.f); 
    auto begin = thrust::make_zip_iterator(thrust::make_tuple(a.begin(), b.begin())); 
    auto end = thrust::make_zip_iterator(thrust::make_tuple(a.end(), b.end())); 

    //these two lines work 
    thrust::get<0>(begin[0]); 
    std::cout << thrust::get<0>(begin[0]) - thrust::get<1>(begin[0]); 


    //these three lines do not 
    //thrust::transform_reduce(begin, end, func(), 0.0f, thrust::plus<float>()); 
    //func()(begin[0]); 
    //thrust::transform(begin, end, begin, func()); 


    std::cout << "done" << std::endl; 
    return 0; 
} 

tôi nhận được lỗi này: (chương trình của tôi được gọi là sandbox.cu)

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\bin/../include\thrust/detail/tuple.inl(310): error: no instance of constructor "thrust::detail::normal_iterator<Pointer>::normal_iterator [with Pointer=thrust::device_ptr<float>]" matches the argument list 
     argument types are: (const thrust::device_reference<float>) 
     detected during: 
     instantiation of "thrust::detail::cons<HT, TT>::cons(const thrust::detail::cons<HT2, TT2> &) [with HT=iter, TT=thrust::detail::cons<iter, thrust::null_type>, HT2=thrust::device_reference<float>, TT2=thrust::detail::cons<thrust::device_reference<float>, thrust::null_type>]" 
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\bin/../include\thrust/tuple.h(361): here 
     instantiation of "thrust::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::tuple(const thrust::detail::cons<U1, U2> &) [with T0=iter, T1=iter, T2=thrust::null_type, T3=thrust::null_type, T4=thrust::null_type, T5=thrust::null_type, T6=thrust::null_type, T7=thrust::null_type, T8=thrust::null_type, T9=thrust::null_type, U1=thrust::device_reference<float>, U2=thrust::detail::cons<thrust::device_reference<float>, thrust::null_type>]" 
sandbox.cu(37): here 

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\bin/../include\thrust/detail/tuple.inl(411): error: no instance of constructor "thrust::detail::normal_iterator<Pointer>::normal_iterator [with Pointer=thrust::device_ptr<float>]" matches the argument list 
      argument types are: (const thrust::device_reference<float>) 
      detected during: 
      instantiation of "thrust::detail::cons<HT, thrust::null_type>::cons(const thrust::detail::cons<HT2, thrust::null_type> &) [with HT=iter, HT2=thrust::device_reference<float>]" 
(310): here 
      instantiation of "thrust::detail::cons<HT, TT>::cons(const thrust::detail::cons<HT2, TT2> &) [with HT=iter, TT=thrust::detail::cons<iter, thrust::null_type>, HT2=thrust::device_reference<float>, TT2=thrust::detail::cons<thrust::device_reference<float>, thrust::null_type>]" 
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\bin/../include\thrust/tuple.h(361): here 
      instantiation of "thrust::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::tuple(const thrust::detail::cons<U1, U2> &) [with T0=iter, T1=iter, T2=thrust::null_type, T3=thrust::null_type, T4=thrust::null_type, T5=thrust::null_type, T6=thrust::null_type, T7=thrust::null_type, T8=thrust::null_type, T9=thrust::null_type, U1=thrust::device_reference<float>, U2=thrust::detail::cons<thrust::device_reference<float>, thrust::null_type>]" 
sandbox.cu(37): here 

2 errors detected in the compilation of "C:/cygwin64/tmp/tmpxft_00001a90_00000000-10_sandbox.cpp1.ii". 

Trả lời

3

Giải Quyết! tup phải là thrust::tuple<float, float>, không phải là thrust::tuple<iter, iter>. Giải pháp đầy đủ:

#include <iostream> 
#include <stdlib.h> 
#include <thrust/device_vector.h> 
#include <thrust/transform.h> 
#include <thrust/tuple.h> 
#include <thrust/transform_reduce.h> 
#include <thrust/iterator/zip_iterator.h> 

typedef thrust::device_vector<float> dvec; 
typedef thrust::tuple<float, float> tup; 

struct func 
{ 
    __device__ float operator()(tup t) //difsq 
    { 
    float f = thrust::get<0>(t) - thrust::get<1>(t); 
    return f*f; 
    } 
}; 

int main() 
{ 
    dvec a(4, 3.f); 
    dvec b(4, 2.f); 
    auto begin = thrust::make_zip_iterator(thrust::make_tuple(a.begin(), b.begin())); 
    auto end = thrust::make_zip_iterator(thrust::make_tuple(a.end(), b.end())); 
    std::cout << thrust::transform_reduce(begin, end, func(), 0.0f, thrust::plus<float>()) << std::endl; 
    std::cout << "done" << std::endl; 
    return 0; 
}