Format specifier for cuDoubleComplex variables for accessing files

Please take mercy upon a CUDA newbie and I would very much appreciate some assistance.
In C , we can easily access file using fprintf and fscanf as shown below:
fp = fopen(“forces1.txt”, “w”);
fprintf(fp, “%f %f %f\n”, ForceX[h], ForceY[h], ForceZ[h]);

But I am using CUDA and my variables ForceX[h] etc are cuDoubleComplex. I want to ask two things:

  1. Whether I am allowed to use frintf and fscanf in CUDA, if not then how to access files.
  2. What will be the format specifier used in place of %f as my variable is not float.

Thank you so much in advance.

in cuComplex.h (which is in the CUDA include directory in your CUDA install) we can see the following typedef:

typedef double2 cuDoubleComplex;

and double2 is a struct definition (in vector_types.h, same directory) that looks like this:

struct double2 {

  double x,y;

So now your question is a C or C++ question. You can print the elements (.x, .y) of that struct easily enough using the %f format specifier.

Yes, you can use fprintf and fscanf in CUDA host code, just like you would in ordinary host code.

@Robert_Crovella Thanks for your valuable reply. I have takenForceX,ForceY,ForceZ values of type cuDoubleComplex. But when I am writing line fprintf(fp, “%f %f %f\n”, ForceX[h], ForceY[h], ForceZ[h]); I am getting output. But as my ForceX value was not of type float, how can I use %f. so, if I can’t use %f what specifier should I use in order to use fprintf statement with cuDoubleComplex type variable.

You’ll need to learn, using C or C++, what a structure is, and how to reference, and how to print the elements of a structure. This has nothing to do with CUDA. Your ForceX for example is (a pointer to) an array of structures. You select a specific structure by indexing into that array e.g. ForceX[h]. You cannot “print” that structure in any simple fashion using printf. The way to print elements of that structure would be to reference a specific element. In C or C++, the structure element reference operator is the . operator. So you would print ForceX[h].x to reference the first element.

@Robert_Crovella Yeah I have tried now using . operator also. I have written the following line:
printf("%lf + i%lf\n",ForceX[h].x,ForceX[h].y);

But still I am getting output as follows:

Great. It seems to be working correctly.

@Robert_Crovella Actually I was getting this output earlier also but all the values printed are zero which is not correct. I am getting wrong values at the output. Is it due to the fact that I am using %f specifier for a variable ForceX which is of type cuDoubleComplex?
What changes can be done in order to get correct output.
Please help.

I interpreted this question generally as “how do I use _printf format specifiers with cuDoubleComplex?”

I think we’ve covered that. If you are now using

Then I would say that is the correct way to do it. (To be very correct, the l is unnecessary and should not be used. %f by itself is a correct format specifier both for an underlying float type as well as an underlying double type.)

In that context, I would say it is “working correctly”.

If you are now seeing zeros as output, I would be fairly confident that the reason for it is that those variables actually do contain zero values which again is consistent with a statement that it is “working correctly”.

If you are now wondering why they have zero values (which I interpret as a different question than “how do I use _printf format specifiers with cuDoubleComplex?”), and we have more-or-less established that your print statements are constructed correctly, then I would suggest the reason for that lies in some other aspect of your code, which you have not shown (here, in this question). Yes, I realize you may have shown the code in another question. If I can help there, I will. However, as indicated there already, posting 1000 lines of improperly formatted code that are riddled with issues is not something I generally have the time to deal with. If you ask more focused questions like this one, preferably with short, complete test cases, that (in my opinion) is a best practice for getting a useful response on these forums.

And just to be clear, now that we have established that this is a correct methodology:

(but probably remove the l)

then we could apply that learning to the code you wrote at the beginning of this thread, and we would probably want to do something like:

fprintf(fp, “%f %f %f %f %f %f\n”, ForceX[h].x, ForceX[h].y, ForceY[h].x, ForceY[h].y, ForceZ[h].x, ForceZ[h].y);

@Robert_Crovella Thank you so much for taking out time in replying and helping me. Yeah I understand that the problem must be lying in some other part of code.

I completely agree that posting 1000 lines of code is not a good exercise at all.

I am trying to debug it from starting line. If still I get those zero values at the output then I’ll post some short , complete part of the code.
Thanks again.

Yeah now I have used .operator and also removed l from lf. still output values are zero. I am trying to see in other part of code if anything is missing there.

Thank you.