0

I'm a bit new to CUDA so please forgive me if this is a dumb question. I've been reading/watching quite a few tutorials but they are all very confusing but I think I have the basic idea down. Anyway I'm trying to do the following: I want to initialize several constant variables on the device (hbar,kb,q,T,me). I then want to also have a variable muB that's a function of these other initialized constants. However, I believe to do that, I need to first copy the constant values to the host memory, compute muB and pass it back to the device memory. I attempt to do this in the main function.

#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <ctime>

#include <cuda.h>
#include <cuda_runtime.h>

using namespace std;

//Physical Constants
__constant__ float hbar = 1.054e-34;                   
__constant__ float kb = 1.38e-23;                       
__constant__ float q = 1.6e-19;                         
__constant__ float T = 300.0;                           
__constant__ float me = 9.1e-31;                        
__constant__ float muB = 0.0;                           

int main() {
    float tmp = 0.0f;

    float h_q, h_hbar, h_me;
    cudaMemcpyFromSymbol(&h_q, &q, sizeof(float));
    cudaMemcpyFromSymbol(&h_hbar, &hbar, sizeof(float));
    cudaMemcpyFromSymbol(&h_me, &me, sizeof(float));
    tmp = h_q*h_hbar / 2 / h_me;
    cudaMemcpyToSymbol(&muB, &tmp, sizeof(float));

    return 0;
}

The problem is, when I run this program, h_q, h_hbar, and h_me are all equal to -107374176 which is not what I initialized those constant variables to. tmp is also equal to -53687088.0. I know I must be doing something wrong but can't quite figure out what. Anyone have any insights or suggestions? Thanks

John
  • 1
  • 4

1 Answers1

1

When using cudaMemcpyTo/FromSymbol, we use the symbol name directly, not with the ampersand. So instead of this:

cudaMemcpyFromSymbol(&h_q, &q, sizeof(float));
cudaMemcpyFromSymbol(&h_hbar, &hbar, sizeof(float));
cudaMemcpyFromSymbol(&h_me, &me, sizeof(float));
tmp = h_q*h_hbar / 2 / h_me;
cudaMemcpyToSymbol(&muB, &tmp, sizeof(float));

do this:

cudaMemcpyFromSymbol(&h_q, q, sizeof(float));
cudaMemcpyFromSymbol(&h_hbar, hbar, sizeof(float));
cudaMemcpyFromSymbol(&h_me, me, sizeof(float));
tmp = h_q*h_hbar / 2 / h_me;
cudaMemcpyToSymbol(muB, &tmp, sizeof(float));

Also, I'm pretty sure that if you did proper cuda error checking, each of those lines you wrote would throw an error.

Community
  • 1
  • 1
Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
  • Oh I see what happened now. I originally had the format in the correct. However, I'm using Visual Studio 2013 and for some reason the editor marked the q, hbar, me, muB variable names inside the Memcpy functions with an error warning: "Error: argument of type "float" is incompatible with parameter of type "const void" ". When I changed it to my solution above, the editor didn't complain about the errors and I thought that function was supposed to be like that. However now that I run the code you provided, I see it compiles correctly (though VSeditor is still complaining about the syntax). – John Jan 31 '16 at 05:57
  • 1
    Yes, intellisense can be misleading. – Robert Crovella Jan 31 '16 at 05:58
  • You must be very experienced with these things so if I may ask a question, do you use an IDE when coding in CUDA and if so, do you have problems with the editors marking syntax as incorrect. For instance, in VS2013, the third less than symbol of the kernal call "<<<1,1>>>" is marked as incorrect but it compiles fine. Any thoughts on that? Anyway thank you so much for your help, I really appreciate it. – John Jan 31 '16 at 06:00
  • 2
    The syntax `<<<>>>` for a launch configuration is a CUDA extension to C++ and therefore looks like a syntax error to any IDE that just "knows" C/C++ syntax. I personally don't use any IDEs, they have a tendency to get in my way. – njuffa Jan 31 '16 at 08:10
  • I don't generally use an IDE. As @njuffa has already indicated, the syntax you indicate is CUDA-specific, so is likely to throw off any "ordinary" parser. If you search in the upper right hand corner of this page on "cuda red underline" you'll find many discussions of this issue, perhaps with some methods to mitigate it. YMMV. Personally I would just ignore it, as I don't depend on the IDE to tell me if my CUDA syntax is correct, and intellisense/the red underline in no way affects your ability to write and compile correct code. – Robert Crovella Feb 01 '16 at 11:46