> For the complete documentation index, see [llms.txt](https://notes.dizy.cc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.dizy.cc/graphics-processing-units/cuda.md).

# CUDA

## Allocating Device Memory

`cudaMalloc(LOCATION, SIZE)`

* LOCATION: Memory location on Device to allocate memory, an address in the GPU's memory
* SIZE: number of bytes to allocate

De-Allocate: `cudaFree()`

## Copy Data between Host and Device

`cudaMemory(DST, SRC, NUM_BYTES, DIRECTION)`

* DST: An address of the memory to copy into
* SRC: An address of the memory to copy from
* NUM\_BYTES: N \* sizeof(type)
* DIRECTION:
  * cudaMemcpyHostToDevice
  * cudaMemcpyDeviceToHost

## Define the Kernel

```cpp
__global__ void kernel(int *d_out, int *d_in) {
    d_out[0] = d_in[0];
}
```

### Thread Index

In kernel definition, built-in variable `threadIdx`  is accessible to get thread index within the thread block for each thread.

It has 3 dimensions: `threadIdx.x`, `threadIdx.y` and `threadIdx.z`.

### Block Index

Index of a block: `blockIdx.x`, `blockIdx.y` and `blockIdx.z`.&#x20;

### Indexing within Grid

```cpp
i = threadIdx.x + blockIdx.x * blockDim.x;
```

### \_\_syncthreads

To explicitly synchronize all threads (adding barriers), use `__syncthreads.`

```cpp
int temp = a[i + 1];
__syncthreads;
a[i] = temp;
__syncthreads;
```

## Launch the Kernel

```cpp
int *h_c, *d_c;

// Allocate memeory on the device
cudaMalloc((void**)&d_c, sizeof(int));
// Copy content from host to device
cudaMemcpy(d_c, h_c, sizeof(int), cudaMemcpyHostToDevice);

// Launch the kernel
dim3 grid_size(1);
dim3 block_size(1);
kernel<<<grid_size, block_size>>>(...);

// Force host to wait on the completion of the kernel
cudaDeviceSynchronize();

// Copy data back to host
cudaMemcpy(h_c, d_c, sizeof(int), cudaMemcpyDeviceToHost);

// Clean up memory
cudaFree(d_c);
free(h_c);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://notes.dizy.cc/graphics-processing-units/cuda.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
