thread_index.md 462 字节
Newer Older
K
Ken He  
Ken He 已提交
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
# CUDA线程
在以下形式代码中调用kernel函数的前提下, 那每个线程的全局索引正确的是?
```C++
dim3 gridDim = blocksPerGrid;
dim3 blockDim = threadsPerBlock;
....
kernel<<<gridDim, blockDim>>>(a,b,c)
....

```

## 答案
```C++
index = threadIdx.x + blockIdx.x * blockDim;
```

## 选项

### A
```C++
index = threadIdx.x + blockDim;
```

### B
```C++
index = threadIdx.x * threadDim;
```

### C
```C++
index = blockIdx.x * blockDim;
```