如何使用一个线程在多块中反转数组? [英] How to reverse an array in multiple-blocks with just one thread?

查看:114
本文介绍了如何使用一个线程在多块中反转数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个数组,它接受两个参数:数组及​​其大小。

I need to create an array which takes two arguments: array and its size.

我有一个这样的函数:

__global__ void reverseArray(int *data, int size){

    int tid = blockIdx.x// Total blocks

}

如何使用此函数反转数组?

How can I reverse array with this function?

推荐答案

这取决于您的启动参数,但您可以尝试

It depends on your launch parameters, but you can try doing

__global__ void reverseArray(int *data,int count){
    const int tid = threadIdx.x + blockIdx.x*blockDim.x;
    if(tid < count/2)
    {
        const int new_tid = count - tid - 1;
        int prev_valA = data[tid];
        int prev_valB = data[new_tid];

        data[new_tid] = prev_valA;
        data[tid] = prev_valB;
    }
}



我假设这是您的< a href =http://stackoverflow.com/questions/16576971/why-does-reverse-this-function-not-work/16577569#16577569>上一个问题?

此外,请注意,假设您只使用x维度作为内核启动参数

Also, note that this assumes you're only using the x-dimension for your kernel launch parameters

这篇关于如何使用一个线程在多块中反转数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆