Can't copy struct data from host to device

Here is a simple code:

struct Point 
{ 
	float3 x;
	int3 y; 
	float z;
	int w;
};

int main() 
{ 
	Point *h_a = (Point*)malloc(sizeof(Point)); 
	h_a->x=make_float3(1.0,2.0,3.0); 
	h_a->y=make_int3(1,2,3); 
	h_a->z=10.0;
	h_a->w = 3;

	Point *d_a; 
	cudaMalloc((void**)&d_a,sizeof(Point));

	cudaMemcpy(d_a,h_a,sizeof(Point),cudaMemcpyHostToDevice); 

	return 0;
}

It’s just a simple code, but I can’t access device value pointed by d_a, can someone help me?

I would suggest adding code to check the return status of every CUDA API call. It is entirely possible that CUDA failed to initialize, for example due to a driver version mismatch.

You state that you cannot access the device memory, what exactly does that mean? For example, there is no device code that operates on the data pointed to by d_a. The code snippet above does not seem to have any obvious problems.

See the revised code below:

#include<cutil.h>
#include <stdio.h>

struct Point 
{ 
	float3 x;
	int3 y; 
	float z;
	int w;
};

int main() 
{ 
	Point *h_a = (Point*)malloc(sizeof(Point)); 
	h_a->x=make_float3(1.0,2.0,3.0); 
	h_a->y=make_int3(1,2,3); 
	h_a->z=10.0;
	h_a->w = 3;

	Point *d_a; 
	CUDA_SAFE_CALL(cudaMalloc((void**)&d_a,sizeof(Point)));

	CUDA_SAFE_CALL(cudaMemcpy(d_a,h_a,sizeof(Point),cudaMemcpyHostToDevice)); 

	if (d_a)
	{
		printf("d_a->z = %f", d_a->z);
	}

	return 0;
}

It will crash in the code line: printf(“d_a->z = %f”, d_a->z); I can’t dereference the pointer d_a which is not NULL.And I didn’t get any error prompts after using cudaMalloc & cudaMemcpy.

Device and host (system) memory are different address spaces. You cannot dereference a pointer to device memory in host code. Likewise you cannot dereference a pointer to host memory in device code. There are ways of mapping host memory into the memory space of the device, but that is an advanced CUDA programming topic.

I would suggest reading the relevant sections of the CUDA C Programming Guide, or an introductory text, such as “CUDA by Example”.

Thanks.

This helps me a lot. I will follow your advice.:)