liveBook · Manning

cudaMalloc

As explained in Grokking GPU Programming

cudaMalloc is a CUDA runtime API function that allocates memory on a GPU device.

cudaMalloc reserves memory on the GPU, much like malloc reserves memory on the CPU. It takes the address of a pointer and the requested size, writes the device address through that pointer, and returns an error code. The call blocks until allocation finishes. Reserved memory is uninitialized; allocating it does not copy host data to the GPU. section 2.15

Reserve space for an array

Use cudaMalloc(&d_data, n * sizeof(int)) to reserve GPU memory for n integers. Passing &d_data lets cudaMalloc write the device address into d_data; assigning a return value to d_data would be incorrect. Allocation can fail if device memory runs out. section 2.15

Figure 2.7 Step 1 — Allocate: reserving space on the GPU. cudaMalloc carves out a block of device memory the same size as the host array, but doesn't copy anything yet. The GPU slots contain uninitialized data (the ? marks) until the copy step. Each region is labeled with the pointer that refers to it: h_data for the host array and d_data for the device array, a reminder that they point into two separate memories.

Shows host and device arrays in separate memories, and GPU slots still uninitialized after allocation. section 2.15

How Grokking GPU Programming covers it

How deep it goes
Explains the allocation call, pointer argument, blocking behavior, uninitialized memory, and allocation failure.
Examples
Uses an integer-array allocation and a grading-room analogy for reserving GPU space.
What you can do after
Operate cudaMalloc to reserve device storage and capture its address through a pointer.
How it connects
Compares cudaMalloc with malloc and distinguishes host memory from device memory.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking GPU Programming. The section links point to the parts of the book it draws on.