-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.md
More file actions
51 lines (35 loc) · 1.78 KB
/
README.md
File metadata and controls
51 lines (35 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Occupancy Calculator
This CUDA kernel demonstrates how to calculate and optimize GPU occupancy using CUDA's occupancy API. Understanding occupancy is crucial for maximizing GPU performance and resource utilization.
## Overview
The program calculates the occupancy of a simple kernel by determining how many active blocks can run per multiprocessor (SM). This information helps developers optimize their kernel configurations for better performance.
## What is Occupancy?
Occupancy is the ratio of active warps to the maximum number of warps supported on a multiprocessor. Higher occupancy doesn't always guarantee better performance, but it provides more opportunities to hide latency through context switching.
## Code Explanation
The kernel performs simple element-wise multiplication:
```cuda
__global__ void MyKernel(int* d, int* a, int* b)
{
int idx = threadIdx.x + blockIdx.x * blockDim.x;
d[idx] = a[idx] * b[idx];
}
```
The host code uses `cudaOccupancyMaxActiveBlocksPerMultiprocessor()` to determine:
- Maximum number of active blocks per SM for a given block size
- Resulting occupancy as a percentage of maximum possible warps
## Key Concepts
- **Block Size**: Number of threads per block (64 in this example)
- **Active Blocks**: Maximum blocks that can execute simultaneously on one SM
- **Active Warps**: Number of warps that can be active (numBlocks × blockSize / warpSize)
- **Occupancy**: Percentage of maximum warps that are active
## Sample Output
```
Occupancy: 100%
```
This indicates the kernel configuration achieves maximum theoretical occupancy on the device.
## Usage
Compile and run to see the occupancy percentage for your GPU:
```bash
nvcc kernel.cu -o occupancy
./occupancy
```
The program will display the occupancy percentage based on your GPU's capabilities.