I am writing a program where a C++ file calls an extern “C” function in order to launch several CUDA kernel functions. While debugging the code, I discovered that the target address for one of my pointers changes when I enter the extern function.
Here is the offending code (within my .cpp file):
cout << "knnIndices before launch: " << knnIndices_d << endl;
launch_kernel(numParticles, dptr /*positions_d*/, velocities_d, embedded_d,
forces_d,
#ifndef USE_ATOMIC_FLOAT
externalForces_d,
#endif
masses_d, knnIndices_d, dt);
cout << "knnIndices after launch complete: " << knnIndices_d << endl;
and within the .cu file:
extern "C" void launch_kernel(int numParticles, float4* positions, float4* velocities,
float4* embedded, float4* forces,
#ifndef USE_ATOMIC_FLOAT
int4* externalForces,
#endif
float* masses, int* knnIndices, float dt)
{
std::cout << "knnIndices at launch start: " << knnIndices << std::endl;
The output from this is:
knnIndices before launch: 0x200420000
knnIndices at launch start: 0x200321400
knnIndices after launch complete: 0x200420000
I’ve run out of ideas trying to explain this behavior, and I would appreciate some help. Thanks!
Seems to me like you defined
USE_ATOMIC_FLOATonly in one of the sources. So the real index of the parameter is wrong, try printing the parameters that are before and after (or just check the code) to confirm.This is one side of C functions, usually if they are in different object files, the symbol contains only the name of the function, and not the parameters…