Optimization 使用CUDA优化矢量元素交换

Optimization 使用CUDA优化矢量元素交换,optimization,vector,cuda,swap,Optimization,Vector,Cuda,Swap,因为我是cuda的新手。。我需要你的帮助 我有一个长向量,对于每组24个元素,我需要执行以下操作: 对于前12个元素,偶数编号的元素乘以-1, 对于第二个12个元素,奇数元素乘以-1,然后进行以下交换: 图表:因为我还没有足够的分数,我无法发布图片,所以它在这里: 我已经写了这段代码,不知您能否帮助我进一步优化它,以解决分歧或银行冲突 //subvector is a multiple of 24, Mds and Nds are shared memory ____shared____ d

因为我是cuda的新手。。我需要你的帮助 我有一个长向量,对于每组24个元素,我需要执行以下操作: 对于前12个元素,偶数编号的元素乘以-1, 对于第二个12个元素,奇数元素乘以-1,然后进行以下交换:

图表:因为我还没有足够的分数,我无法发布图片,所以它在这里:

我已经写了这段代码,不知您能否帮助我进一步优化它,以解决分歧或银行冲突

//subvector is a multiple of 24, Mds and Nds are shared memory

____shared____ double Mds[subVector];

____shared____ double Nds[subVector];

int tx = threadIdx.x;
int tx_mod = tx ^ 0x0001;
int  basex = __umul24(blockDim.x, blockIdx.x);

 Mds[tx] = M.elements[basex + tx];
__syncthreads();

// flip the signs 
 if (tx < (tx/24)*24 + 12)
 {  
    //if < 12 and even
    if ((tx & 0x0001)==0)
    Mds[tx] = -Mds[tx];
 }
 else
 if (tx < (tx/24)*24 + 24)
 {
    //if >12 and < 24 and odd
    if ((tx & 0x0001)==1)
    Mds[tx] = -Mds[tx];
 }

 __syncthreads();

 if (tx < (tx/24)*24 + 6)
 {  
//for the first 6 elements .. swap with last six in the 24elements group (see graph)
    Nds[tx] = Mds[tx_mod + 18];
    Mds [tx_mod + 18] = Mds [tx];
    Mds[tx] = Nds[tx];
 }
 else
 if (tx < (tx/24)*24 + 12)
 {
    // for the second 6 elements .. swp with next adjacent group (see graph)
    Nds[tx] = Mds[tx_mod + 6];
    Mds [tx_mod + 6] = Mds [tx];
    Mds[tx] = Nds[tx];
 }   
__syncthreads();
//子向量是24的倍数,Mds和Nds是共享内存
____共享双Mds[子向量];
____共享双Nds[子向量];
int tx=线程idx.x;
int tx_mod=tx^0x0001;
int basex=uu umul24(blockDim.x,blockIdx.x);
Mds[tx]=M.elements[basex+tx];
__同步线程();
//翻转标志
如果(tx<(tx/24)*24+12)
{  
//如果<12且为偶数
如果((tx&0x0001)==0)
Mds[tx]=-Mds[tx];
}
其他的
如果(tx<(tx/24)*24+24)
{
//如果大于12且小于24且奇数
如果((tx&0x0001)==1)
Mds[tx]=-Mds[tx];
}
__同步线程();
如果(tx<(tx/24)*24+6)
{  
//对于前6个元素..与24elements组中的最后6个元素交换(见图)
Nds[tx]=Mds[tx_mod+18];
Mds[tx_mod+18]=Mds[tx];
Mds[tx]=Nds[tx];
}
其他的
如果(tx<(tx/24)*24+12)
{
//对于第二个6个元素..swp和下一个相邻组(见图)
Nds[tx]=Mds[tx_mod+6];
Mds[tx_mod+6]=Mds[tx];
Mds[tx]=Nds[tx];
}   
__同步线程();

提前感谢。

保罗为你之前的问题提供了很好的起点

两件事要注意:你正在做非基本2师,这是昂贵的。 相反,尝试利用线程块的多维特性。例如,制作尺寸为24的x维度,这将消除分割的需要

通常,请尝试调整螺纹块尺寸以反映数据尺寸

简化符号翻转:例如,如果您不想翻转符号,仍然可以乘以identity
1
。了解如何使用算术将偶数/奇数映射到1和-1:例如
sign=(偶数*2+1)-2
,其中偶数为1或0