我需要找到数组中的最小索引。使用一个线程很容易做到这一点,但我想使用并行线程来减少它。
我使用一个线程(如果 threadIDx.x == 1)....... 并且它工作正常。但并行执行此操作将提高我正在寻找的效率。
我写了这段代码,对我来说它看起来很合乎逻辑。但是当我调试它时根本没有选择最小值!
代码:
#define MIN(x,y) ((x < y) ? x : y)
#define MIN_IDX(x,y, idx_x, idx_y) ((x < y) ? idx_x : idx_y)
....
....
__shared__ int costs[nt];
__shared__ int bstids[nt];
int myM = 9999999;
int mtMId;
for (int s = nt/2 ; s >= 1 ; s/=2) {
if (threadIdx.x < s) {
myM = MIN(costs[threadIdx.x], costs[threadIdx.x+s]);
costs[threadIdx.x] = myM;
mtMId = MIN_IDX(costs[threadIdx.x], costs[threadIdx.x+s],bstids[threadIdx.x], bstids[threadIdx.x+s]);
bstids[threadIdx.x] = mtMId;
__syncthreads();
}
}
nt 是线程数,它是 2 的幂