4

float2CUDA Thrust を使用して配列のリダクションを実行する次の (コンパイル可能で実行可能な) コードがあります。正しく動作します

using namespace std;

// includes, system 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <conio.h>

#include <typeinfo>  
#include <iostream>

// includes CUDA
#include <cuda.h>
#include <cuda_runtime.h>

// includes Thrust
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/reduce.h>

// float2 + struct
struct add_float2 {
    __device__ float2 operator()(const float2& a, const float2& b) const {
        float2 r;
        r.x = a.x + b.x;
        r.y = a.y + b.y;
        return r;
    }
 };

// double2 + struct
struct add_double2 {
    __device__ double2 operator()(const double2& a, const double2& b) const {
        double2 r;
        r.x = a.x + b.x;
        r.y = a.y + b.y;
        return r;
    }
 };

void main( int argc, char** argv) 
{
    int N = 20;

    // --- Host
    float2* ha; ha = (float2*) malloc(N*sizeof(float2));
    for (unsigned i=0; i<N; ++i) {
        ha[i].x = 1;
        ha[i].y = 2;
    }

    // --- Device
    float2* da; cudaMalloc((void**)&da,N*sizeof(float2));
    cudaMemcpy(da,ha,N*sizeof(float2),cudaMemcpyHostToDevice);

    thrust::device_ptr<float2> dev_ptr_1(da);
    thrust::device_ptr<float2> dev_ptr_2(da+N);

    float2 init; init.x = init.y = 0.0f;

    float2 sum = thrust::reduce(dev_ptr_1,dev_ptr_2,init,add_float2());

    cout << " Real part = " << sum.x << "; Imaginary part = " << sum.y << endl;

    getch();

 }

ただし、プログラムでに変更すると、float2つまりdouble2main

void main( int argc, char** argv) 
{
    int N = 20;

    // --- Host
    double2* ha; ha = (double2*) malloc(N*sizeof(double2));
    for (unsigned i=0; i<N; ++i) {
        ha[i].x = 1;
        ha[i].y = 2;
    }

    // --- Device
    double2* da; cudaMalloc((void**)&da,N*sizeof(double2));
    cudaMemcpy(da,ha,N*sizeof(double2),cudaMemcpyHostToDevice);

    thrust::device_ptr<double2> dev_ptr_1(da);
    thrust::device_ptr<double2> dev_ptr_2(da+N);

    double2 init; init.x = init.y = 0.0;

    double2 sum = thrust::reduce(dev_ptr_1,dev_ptr_2,init,add_double2());

    cout << " Real part = " << sum.x << "; Imaginary part = " << sum.y << endl;

    getch();

}

exceptionラインで受け取りますreducedouble2配列で CUDA Thrust リダクションを使用するにはどうすればよいですか? 私は何か間違ったことをしていますか?前もって感謝します。

TALONMIES の回答に続く実用的なソリューション

名前空間 std を使用します。

// includes, system
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <conio.h>

#include <typeinfo>
#include <iostream>

// includes CUDA
#include <cuda.h>
#include <cuda_runtime.h>

// includes Thrust
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/reduce.h>

struct my_double2 {
    double x, y;
};

// double2 + struct
struct add_my_double2 {
    __device__ my_double2 operator()(const my_double2& a, const my_double2& b) const {
        my_double2 r;
        r.x = a.x + b.x;
        r.y = a.y + b.y;
        return r;
    }
};

void main( int argc, char** argv) 
{
    int N = 20;

    // --- Host
    my_double2* ha; ha = (my_double2*) malloc(N*sizeof(my_double2));
    for (unsigned i=0; i<N; ++i) {
        ha[i].x = 1;
        ha[i].y = 2;
    }

    // --- Device
    my_double2* da; cudaMalloc((void**)&da,N*sizeof(my_double2));
    cudaMemcpy(da,ha,N*sizeof(my_double2),cudaMemcpyHostToDevice);

    thrust::device_ptr<my_double2> dev_ptr_1(da);
    thrust::device_ptr<my_double2> dev_ptr_2(da+N);

    my_double2 init; init.x = init.y = 0.0;

    cout << "here3\n";
    my_double2 sum = thrust::reduce(dev_ptr_1,dev_ptr_2,init,add_my_double2());

    cout << " Real part = " << sum.x << "; Imaginary part = " << sum.y << endl;

    getch();

}
4

1 に答える 1

4

これは、MSVC および nvcc との既知の非互換性です。たとえば、ここを参照してください。解決策は、独自のバージョンの を定義double2 し、代わりにそれを使用することです。

参考までに、CUDA 5.5 を搭載した Linux 64 ビット ボックスでコードを正しくコンパイルして実行できます。

于 2013-08-08T11:01:17.097 に答える