n00b error with cudaMemcpy

I did search for an answer to this on the forums but didn’t find one, here goes:

When I try to use cudaMemcpy everything compiles fine, but in debug mode I notice that it isnt actually copying from the host to device, here is the relevant section of code:

const int max_particles = 1000;

int main(){

		

	int a = 256;

	struct element{

		float x[max_particles];

		float p_x[max_particles];

	} particle; //The struct object for the elements of particles is called particle

/*

Here be a section of code which reads values into particle.x and particle.p_x

*/

	

	size_t size = a * sizeof(float); //Determine amount of memory  required for each variable

	  

	//Create memory on the Host and assign pointers for each variable

	float* host_particle_x = (float*)malloc(size); 

	float* host_particle_p_x = (float*)malloc(size);

	cout << "Allocating host memory and initialising...\n" << endl;

	//Initialise Host pointers with the determined variables

	host_particle_x = particle.x;

	host_particle_p_x = particle.p_x;

	//Create variables for the device

	float* device_particle_x;

	float* device_particle_p_x;

	cout << "Allocating Device memory..." << endl;

	//Allocate memory for the device variables

	cutilSafeCall(cudaMalloc((void**)&device_particle_x, size));

	cutilSafeCall(cudaMalloc((void**)&device_particle_p_x, size));

	cout << "Copying Host memory to Device memory..." << endl;

	//Copy the variables from the host memory to the device memory

	cutilSafeCall(cudaMemcpy(device_particle_x, host_particle_x, size, cudaMemcpyHostToDevice));

	cutilSafeCall(cudaMemcpy(device_particle_p_x, host_particle_p_x, size, cudaMemcpyHostToDevice));

Can anyone see what it is that I’m doing wrong?

Thank you in advance x

The just allocated host_particle_x, host_particle_p_x are overidden by particle.x, particle.p_x, which seems wrong(memory leak).

Maybe you want to do a “mempy(host_particle_x, particle.x, size)”?

The just allocated host_particle_x, host_particle_p_x are overidden by particle.x, particle.p_x, which seems wrong(memory leak).

Maybe you want to do a “mempy(host_particle_x, particle.x, size)”?

Thankyou for the fast reply

I have semi-solved it, for some reason it works in release mode but not in debug mode?

I think your right Nighthawk, my host variables are abit redundant when they can be replaced with particle.x and particle.p_x, so I’ll change that

If you have any idea on how I can fix it for debug mode that would be great, but I can deal with it only working in release mode for now.

Many thanks x

Thankyou for the fast reply

I have semi-solved it, for some reason it works in release mode but not in debug mode?

I think your right Nighthawk, my host variables are abit redundant when they can be replaced with particle.x and particle.p_x, so I’ll change that

If you have any idea on how I can fix it for debug mode that would be great, but I can deal with it only working in release mode for now.

Many thanks x