Can we move data from global to constant memory in host function?

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…

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

Thank you!! It works!!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.