Hello,
I have a simple problem that assigns value to some global constant variables on device, such as:
#include <iostream>
#include <cuda.h>
__constant__ float FCONST[2];
int main(){
float a[2];
float b[2];
a[0] = 1.0f;
a[1] = 2.0f;
cudaMemcpyToSymbol( FCONST, a, 2*sizeof(float), 0, cudaMemcpyHostToDevice);
cudaMemcpy(b, FCONST, 2*sizeof(float), cudaMemcpyDeviceToHost);
std::cout<< "a[0]=" << a[0] << " , a[1]=" << a[1] << std::endl;
std::cout<< "b[0]=" << b[0] << " , b[1]=" << b[1] << std::endl;
return 0;
}
However, the above code does not run correctly, and I could not figure out the reason.
It gave me:
a[0]=1 a[1]=2
b[0]=0 b[1]=0
I tried google the problem, but all threads/discussions are almost the same implying the above code is correct.
I’ve tested using “cudaMemcpyDefault”, or &FCONST[0] instead of FCONST, but none of the methods works.
Maybe I missed some thing here. Please help me to clarify it.
Great thanks!
Best
#edit:
I also tested the code with “cudaMemcpyFromSymbol”.
This time the “b” gets correct results, but when I checked with NSIGHTS, the “FCONST” remains 0 when I paused to check the value.
So I’m a bit confused if variables on device can not be checked at run-time with NSIGHT, or are shown incorrectly?
Thanks.