I had a problem on using constant memory

I have problems,when I tried to use constant memory and I wrote following code

    __constant__ int M;

    void main(void)
    int M_Host = 5;
    cudaMemcpyToSymbol(&M,&M_Host , sizeof(M));


In my kernel, I found M didn’ get any value from “cudaMemcpyToSymbol” and my compilation is successful. Does anybody know what happened??

And I did another tests, if I wrote follows, “M” can work in kernel… I’m confused.

constant int M = 5;

A. you need to check the return code of pretty much every cuda… function call. I don’t know what you can do when you get a failure, but this is a very important development step. I have had lots of failures with cudaMemcpyToSymbol and it took a while to figure it out.

B. Be aware that in a .CPP module you are referencing a template and not the base function. Look at the template and make sure you understand what it is doing (for?) to you.

I’m betting the cudaMemcpyToSymbol is failing.

Yes, you are right, I got error in cudaMemcpyToSymbol and I solve it according to the error message, thank you : ).

You shouldn’t open multiple topics for the same problem

Shouldn’t be like this

// after the headers
___constant__ int M; 

// inside the code

When a variable is declared in this manner it is global and it can be access directly from all kernels without needing to use it as an argument.

I see. Thanks.