NCA CUDA Programming & Parallel Computing 3 — Questions and Answers
Question 1: What is the purpose of cudaMemcpy with the flag cudaMemcpyDeviceToHost?
- Copies data from host RAM to GPU global memory
- Copies data from GPU global memory to host RAM (Correct answer)
- Copies data between two GPU devices
- Copies data within GPU shared memory
Correct answer: Copies data from GPU global memory to host RAM
cudaMemcpyDeviceToHost transfers data from device (GPU) memory back to host (CPU) memory.
Question 2: In CUDA, what is 'occupancy'?
- The percentage of global memory in use
- The ratio of active warps to maximum warps on an SM (Correct answer)
- The number of thread blocks per grid
- The fraction of time the GPU is not idle
Correct answer: The ratio of active warps to maximum warps on an SM
Occupancy measures how many warps are active on an SM relative to the hardware maximum, indicating how well latency can be hidden.
Question 3: Which qualifier declares a function that runs on the GPU and is called from the host?
- __device__
- __host__
- __global__ (Correct answer)
- __shared__
Correct answer: __global__
__global__ marks a kernel function that executes on the GPU but is invoked from CPU code using the <<<>>> launch syntax.
Question 4: What causes shared memory bank conflicts?
- Multiple threads accessing the same shared memory bank in a single transaction (Correct answer)
- Threads accessing global memory simultaneously
- Using more shared memory than available on the SM
- Calling __syncthreads() without a barrier
Correct answer: Multiple threads accessing the same shared memory bank in a single transaction
Bank conflicts occur when multiple threads in a warp access different addresses that map to the same memory bank, forcing serialized access.
Question 5: What does the CUDA programming model term 'grid' refer to?
- A single thread executing on the GPU
- The collection of all thread blocks launched by a kernel (Correct answer)
- A group of 32 threads executing in lockstep
- The shared memory region of an SM
Correct answer: The collection of all thread blocks launched by a kernel
A grid is the complete set of thread blocks that are launched together when a kernel is invoked.
Question 6: Which memory space in CUDA is read-only, cached, and optimized for broadcast to all threads?
- Shared memory
- Local memory
- Constant memory (Correct answer)
- Texture memory
Correct answer: Constant memory
Constant memory is a read-only, cached region optimal when all threads read the same address simultaneously.
Question 7: How do you specify the number of thread blocks and threads per block when launching a CUDA kernel?
- Using cudaLaunchKernel() API only
- Inside the kernel using blockDim and gridDim
- With the triple angle bracket <<<grid, block>>> syntax (Correct answer)
- Via a configuration struct passed as the first argument
Correct answer: With the triple angle bracket <<<grid, block>>> syntax
CUDA kernels are launched with kernel<<<gridDim, blockDim>>>(...) where the triple angle brackets specify the execution configuration.
What is the purpose of cudaMemcpy with the flag cudaMemcpyDeviceToHost?