4

I have the following (compilable and executable) code using CUDA Thrust to perform reductions of float2 arrays. It works correctly

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();

 }

However, when I change float2 to double2 in the main program, namely

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();

}

I receive an exception at the reduce line. How can I use CUDA Thrust reduction with double2 arrays? Am i doing anything wrong? Thanks in advance.

WORKING SOLUTION FOLLOWING TALONMIES' ANSWER

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>

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();

}

1 Answer 1

4

This is a known incompatibility with MSVC and nvcc. See here for example. The solution is to define your own version of double2 and use that instead.

Just for reference, I can compile and run your code correctly on a Linux 64 bit box with CUDA 5.5.

4
  • Thank you very much for your answer. What is the reason for such an incompatibility? Does it involve also the latest versions of MSVC? In the quoted web page, it is suggested to define a customized version of double2 as a struct. What about performance? In the past I have played a bit with my own version of double2 as couple of double numbers, but I realized that a solution with a wrapper class internally using double2, instead of couple of doubles, was faster.
    – Vitality
    Aug 8, 2013 at 13:27
  • 1
    I am not that familiar with Microsoft's compilers and libraries, so I can't really tell you more than what I offered in the answer. As for double2 versus a struct - I take it you are not aware that double2 is a simple struct (look in vector_types.h). There should be (and in my experience isn't) any difference in emitted code or performance in using a CUDA vector type and using your own functionally equivalent structure.
    – talonmies
    Aug 8, 2013 at 14:26
  • Thanks, I accepted your answer. Yes, I'm aware that double2 is a struct, but there are special keywords in front of the definition: __device_builtin__ __builtin_align__(16). As long as I know, they should allow for coalesced memory accesses. Would this provide some improvements over a customized definition not using alignment?
    – Vitality
    Aug 8, 2013 at 18:37
  • 2
    __device__builtin__ is just a tag, it doesn't have any effect, and __builtin__align__ resolves to the basic __align__ macro. The CUDA "builin" vector types are just simple structures and they rely on compiler analysis to achieve coalescing, just like any user defined type. The only place they have any real meaning is in relation to texture hardware, and even then only so that the type size and alignment matches what the texture APIs expect.
    – talonmies
    Aug 8, 2013 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.