multi_dim.md 616 字节
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 33 34 35 36
# 多维网格
以下代码调用kernel函数, 对于多维Grid中每个线程在***全局***中的索引(x, y)正确的是?
```C++
dim3 gridDim = (blocksPerGrid, blocksPerGrid);
dim3 blockDim = (threadsPerBlock, threadsPerBlock);
....
kernel<<<gridDim, blockDim>>>(a,b,c)
....

```

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

## 选项

### A
```C++
x = threadIdx.x + blockIdx.x ;
y = threadIdx.y + blockIdx.y ;
```

### B
```C++
x = threadIdx.x * blockDim.x ;
y = threadIdx.y * blockDim.y ;
```

### C
```C++
x = threadIdx.x ;
y = threadIdx.y ;
```