Hi! Previously one expert in this forum told me can not use “device function” to move data from global memory to constant memory. And now I am wondering, can we use “host function” to somehow achieve that??
Thank you!!!
I tired this guy’s idea, and failed…
I got a hint!
Definition after includes…
__contant__ float constant_device_variable
Host code…
float *device_pointer
cudaMalloc( (void**)&device_pointer, device_pointer_size );
[...]
cudaMemcpyToSymbol( "constant_device_variable", device_pointer, sizeof( float ), 0, cudaMemcpyDeviceToDevice );
This version works.
Important is to set a 0-offset. Perhaps cudaMemcpyDeviceToDevice is misinterpreted by nvcc as an offset, when there are three arguments in the function call.
edit: perhaps cud…
It seems to work for me:
$ cat t2081.cu
#include <cstdio>
__constant__ float f = 0.0f;
__global__ void k(){
printf("%f\n", f);
}
int main(){
float *d_f, *h_f = new float(1.234f);
cudaMalloc(&d_f, sizeof(float));
cudaMemcpy(d_f, h_f, sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpyToSymbol(f, d_f, sizeof(float), 0, cudaMemcpyDeviceToDevice);
k<<<1,1>>>();
cudaDeviceSynchronize();
}
$ nvcc -o t2081 t2081.cu
$ compute-sanitizer ./t2081
========= COMPUTE-SANITIZER
1.234000
========= ERROR SUMMARY: 0 errors
$
CUDA 11.4, V100, CentOS7
2 Likes
system
Closed
August 17, 2022, 2:50am
4
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.