Solved

Casting between two structs in C (CUDA)

Posted on 2010-08-12
13
1,576 Views
Last Modified: 2012-05-10
The CUDA library effectively defines its own complex number data type, cuComplex, as follows:

  struct __builtin_align__(8) float2
  {
    float x, y;
  };
  typedef float2 cuFloatComplex;
  typedef cuFloatComplex cuComplex;

I am already using my own complex data type:

  typedef struct  { float re; float im; } complex;

and I wish to be able to cast between the two. Here's an example of what I'm currently trying:

  cuComplex cu_data;
  ...
  complex c_data;
  c_data = (complex)cu_data;

but I'm getting the (nvcc) error:

  'no suitable user-defined conversion from "cuComplex" to "complex" exists'.

So how do I do this? Equivalently: is there a way to do a typedef, but to simply rename the members of that struct? (ie rename 'x' to 're', 'y' to 'im')?

Thanks!
0
Comment
Question by:InteractiveMind
  • 4
  • 3
  • 2
  • +4
13 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 33421947
You could provide your own operator, e.g.
  struct /*__builtin_align__(8)*/ float2

  {

    float x, y;

  };

  typedef float2 cuFloatComplex;

  typedef cuFloatComplex cuComplex;



 typedef struct  { float re; float im; operator cuComplex();} complex;



 complex::operator cuComplex() {



   cuComplex out;



   out.x = re;

   out.y = im;



   return out;

 }



  int main () {



  cuComplex out;

  complex in;



  out = (cuComplex) in;



  return 0;

  }

Open in new window

0
 
LVL 86

Accepted Solution

by:
jkr earned 167 total points
ID: 33421993
Ooops, seems that I missed the 'C' part - well, in that case, make that a function:
  struct /*__builtin_align__(8)*/ float2

  {

    float x, y;

  };

  typedef float2 cuFloatComplex;

  typedef cuFloatComplex cuComplex;



 typedef struct  { float re; float im; operator cuComplex();} complex;



 cuComplex to_cuComplex(complex* in) {



   cuComplex out;



   out.x = in->re;

   out.y = in->im;



   return out;

 }



  int main () {



  cuComplex out;

  complex in;



  out = to_cuComplex(&in);



  return 0;

  }

Open in new window

0
 
LVL 1

Expert Comment

by:sridhard
ID: 33422053

Hi,

Try using pointers . This will assign/point the correponding values. Note sure whether this helps

  cuComplex cu_data = (cuComplex  *) malloc(sizeof(cuComplex ));
  ...
  complex *c_data;
  c_data = (complex*)cu_data;

You can delete any one pointer later .
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 33422155
Thank you, both. I think I should mention that I am in fact casting pointers/arrays of such types in my code (but I get the same problem).

@jkr, would your method work for casting pointers? (I can't test it at the moment). If so, my code needs to be as fast as possible, so given that I'm going to need to do [probably] millions of such casts per second, would it be quicker to just do a typedef:

   typedef cuComplex complex;

and then a global search and replace of '.re' with '.x' etc? (I'm reluctant to do so)

Also, can you think of any reason why what I'm already doing wouldn't work? Is it the __builtin_align__(8) that's causing problems?
0
 
LVL 86

Expert Comment

by:jkr
ID: 33422183
>>@jkr, would your method work for casting pointers?

If you use the indirecton appropriately - yes.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 167 total points
ID: 33427090
>> given that I'm going to need to do [probably] millions of such casts per second, would it be quicker to just do a typedef:

Yes. Because no conversion would be needed then. Performing no conversion is faster than performing a conversion ;)


>> Also, can you think of any reason why what I'm already doing wouldn't work?

The error says it :

>>   'no suitable user-defined conversion from "cuComplex" to "complex" exists'.

the compiler doesn't know how to perform the conversion from cuComplex to complex, because you didn't tell it how to do that (see jkr's post for that).


>> I should mention that I am in fact casting pointers/arrays of such types in my code (but I get the same problem).

Casting pointers wouldn't give you the same problem. It would give you a different problem in C++ : the type safety system in C++ wouldn't allow a cast from cuComplex* to complex*, because the two types are not related - ie. the cast is not safe.
If you shut the compiler up, and force the cast, then the __builtin_align__(8) can indeed cause a problem. If the packing of the complex type is not the same as that of the cuComplex type, then something will go wrong badly.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 20

Assisted Solution

by:ikework
ikework earned 166 total points
ID: 33427141
If you use the same alignment for your struct (8 bytes) then the cast shouldn't be a problem.
0
 
LVL 20

Expert Comment

by:ikework
ID: 33427161
The safest strategy is to use only cuda's struct in the first place, also in your code, so there is no cast needed anymore.
0
 
LVL 22

Expert Comment

by:ambience
ID: 33429106
>> so given that I'm going to need to do [probably] millions of such casts per second, would it be quicker to just do a typedef:

My vote -> YES
0
 
LVL 20

Expert Comment

by:ikework
ID: 33429220
@ambience, did you consider the different alignments as previous posts said?
0
 
LVL 20

Expert Comment

by:ikework
ID: 33429254
@ambience, sorry I misunderstood your post, I thought you were suggesting just cast anyway, nevermind :)
0
 
LVL 1

Expert Comment

by:soscpd
ID: 33436784
Hi

Your answer is a function ponter. There (in the function) you read/write cuComplex/complex structs anyway you like.

Rafael
0
 
LVL 25

Author Closing Comment

by:InteractiveMind
ID: 33558606
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
List out all word 7 259
Should CArray be used for a list of pointers in C++? 19 97
Path to  STL Map header file 1 41
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now