CUDA on Jetson Xavier AGX

I just started learning about programming in CUDA. According to what I have learnt so far, in order to access variables from the host(CPU), the device(GPU) pointers need to be copied to the host memory using the function -
cudaMemcpy(&h_c, d_c, sizeof(int), cudaMemcpyDeviceToHost);
where h_c is host variable, and *d_c is a device pointer.

However, since in Jetson Xavier AGX the GPU and the CPU use the same memory is there a way that we can avoid the copying and access the variables directly?
If yes, an example would be extremely helpful.

I tried executing the following -

#include <iostream>
#include <cuda.h>
#include <cuda_runtime.h>

__global__ void gpuAdd(int d_a, int d_b, int *d_c)
	//This code will be executed on the device instead of the host.
	*d_c = d_a + d_b;

int	main(void)	
	//Defining	device	pointer
	int	*d_c;
	//Allocating	memory	for	device	pointer
	cudaMalloc((void**)&d_c,	sizeof(int));

	//Kernel	call	by	passing	1	and	4	as	inputs	and	storing	answer	in	d_c
	//<<	<1,1>	>>	means	1	block	is	executed	with	1	thread	per	block
	gpuAdd	<<	<1,	1	>>	>	(1,	4,	d_c);

	printf("1 + 4 = %d\n",	*d_c);

	//Free	up	memory
	return	0;

On executing it gives the following error -
Which means that the memory is out of bounds for the host.


printf is a CPU function so it cannot access the GPU buffer.
Please check the below document for the details of Jetson memory:


Thank you for your prompt reply. I went through the link you provided. On the same website, the following is mentioned -

I just wanted to know the way to avoid data transfers between the GPU and the CPU memory, since they share the same physical SoC DRAM as mentioned above.


You can follow the example in the same document.
For example, pinned memory:


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.