std::complex<double> to cuComplexDouble

Open discussion for MAGMA

std::complex<double> to cuComplexDouble

Postby hsahasra » Mon Jun 24, 2013 3:49 pm

Hi!

I'm new to MAGMA.

My code uses that LAPACK function zgeev for solving a Eigenvalue problem. I'm trying to use the MAGMA implementation of zgeev in my code. In my code, I use std::complex<double> to store complex numbers and MAGMA uses cuDoubleComplex. So is there any way to convert from std::complex<double> to cuDoubleComplex without having to allocate more memory?

Thanks!
Harshad
hsahasra
 
Posts: 27
Joined: Mon Jun 24, 2013 3:40 pm

Re: std::complex<double> to cuComplexDouble

Postby mgates3 » Mon Jun 24, 2013 5:03 pm

They are binary compatible. Just use a cast. For instance:

Code: Select all
// This does some simple tests to verify that C++ complex<float>
// and CUDA's cuFloatComplex are compatible, as one would expect.

#include <stdio.h>
#include <complex>
#include <cublas_v2.h>

int main( int argc, char** argv )
{
    std::complex<float> *hx, *dx, *dy;
    std::complex<float> alpha = 2.0;
    float sum;
    int n = 100;
    cublasHandle_t handle;
    cublasCreate( &handle );
   
    // create vectors
    hx = new std::complex<float>[ n ];
    cudaMalloc( &dx, n*sizeof(std::complex<float>) );
    cudaMalloc( &dy, n*sizeof(std::complex<float>) );
    for( int i=0; i < n; ++i ) {
        hx[i] = i;
    }
    cublasSetVector( n, sizeof(std::complex<float>), hx, 1, dx, 1 );
    cublasSetVector( n, sizeof(std::complex<float>), hx, 1, dy, 1 );
   
    // y += alpha*x
    // sum( y )
    // ----- with cublas v1:
    // cublasCaxpy( n, *((cuFloatComplex*) &alpha), (cuFloatComplex*) dx, 1, (cuFloatComplex*) dy, 1 );
    // sum = cublasScasum( n, (cuFloatComplex*) dy, 1 );
   
    // ----- with cublas v2:
    cublasCaxpy(  handle, n, (cuFloatComplex*) &alpha, (cuFloatComplex*) dx, 1, (cuFloatComplex*) dy, 1 );
    cublasScasum( handle, n, (cuFloatComplex*) dy, 1, &sum );
    printf( "sum %.2f\n", sum );
   
    // cleanup
    cudaFree( dx );
    cudaFree( dy );
    delete[] hx;
   
    cublasDestroy( handle );
    return 0;
}
mgates3
 
Posts: 427
Joined: Fri Jan 06, 2012 2:13 pm

Re: std::complex<double> to cuComplexDouble

Postby hsahasra » Tue Aug 13, 2013 1:33 pm

Thanks, this works!
hsahasra
 
Posts: 27
Joined: Mon Jun 24, 2013 3:40 pm


Return to User discussion

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron