Recently, I found that if I define a constant memory variable with “constant” in other *.cu file and then I use cudaMemcpyToSymbol to assign value for constant memory variable, the value of constant memeory varibale are always zero!!! I don’t know why. my os is ubuntu16.04. cuda is 9.0. Could you someone tell me the reason?
Best regards for you!
here is my detailed information:
foo.h is as follow:
struct align(16) Test {
Test(){};
int a ;
int b ;
float c;
};
foo.cu is as follow:
#include <device_functions.h>
#include <stdio.h>
#include <cuda_runtime.h>
#include “foo.h”
constant Test Test_3;
main.cu is as follow:
#include <device_functions.h>
#include <stdio.h>
#include <cuda_runtime.h>
#include “foo.h”
#define CHECK(res) { if(res != cudaSuccess){printf("Error :%s:%d , ", FILE,LINE);
printf(“code : %d , reason : %s \n”, res,cudaGetErrorString(res));exit(-1);}}
constant Test Test_1; // definition in main.cu
extern constant Test Test_3; //definition in foo.cu
global void foo()
{
printf("a1 =%d\nb1 =%d\nc1 = %f\n",Test_1.a,Test_1.b,Test_1.c);
printf("a3 =%d\nb3 =%d\nc3 = %f\n",Test_3.a,Test_3.b,Test_3.c);
}
void useCUDA()
{
Test Test2;
Test2.a =70;
Test2.b =90;
Test2.c =900.9;
cudaMemcpyToSymbol(Test_1,&Test2, sizeof(Test2));
cudaMemcpyToSymbol(Test_3,&Test2, sizeof(Test2));
foo<<<1,5>>>();
CHECK(cudaDeviceSynchronize());
}
int main(){
useCUDA();
}
CMakeLists.txt is as follows:
project(gpu)
cmake_minimum_required(VERSION 2.8)
find_package(CUDA)
set(CUDA_NVCC_FLAGS -O3;-G;-g;-std=c++11)
file(GLOB_RECURSE CURRENT_HEADERS *.h *.hpp .cuh containers/.hpp)
file(GLOB CURRENT_SOURCES *.cpp .cu containers/.cpp )
source_group(“Include” FILES ${CURRENT_HEADERS})
source_group(“Source” FILES ${CURRENT_SOURCES})
cuda_add_executable(main main.cu)