An odd problem with returned value, please help!

Hello,

I encountered this very odd problem while working on a project.

I have a host function inside a .cu file like this.

double func(....)
{
//...body

printf("%f\n",val);
return val;
}

And an assignment statement inside of a .c file like this.

...
newVal = func(....);
printf("%f\n",newVal);
...

The printf in func(…) right before the return statement prints out the value I want to see. Let’s say 0.1. However, in the .c file, when the returned value of func gets assigned to newVal, newVal gets some very odd value like 16.0 or 24.0.

You can see that I don’t do anything in between to alter the value of newVal in the .c file. I’ve never seen this before. Please help! I’d appreciate that.

Are you including a header file with the prototype for func() in both the .cu and the .c file? If there is no prototype in scope, a return type of ‘int’ is assumed, as far as I recall the C/C++ standard. Since the function returns double, that would cause data to be mangled.

By the way, you will have to declare the function as

extern “C”

in order for the code to link correctly. The CUDA tool chain is C++ based, and thus function names are decorated internally meaning they won’t match with the same function referenced in C code.

Thanks for the response. I did declare the function as extern “C”. It caused me the problem a while ago when I couldn’t call the functions in .cu from .c.

I just tried including the header file but the problem is the same.