Reputation: 1
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
Upvotes: 0
Views: 1660
Reputation: 151849
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.
Upvotes: 1