這段CUDA代碼在Nsight分析時會報告很多銀行衝突。第一個片段包含常量的定義和內核調用:如何減少此代碼中的銀行衝突?
// Front update related constants
#define NDEQUES 6
#define FRONT_UPDATE_THREADS 480
#define BVTT_DEQUE_SIZE 500000
#define FRONT_DEQUE_SIZE 5000000
#define FRONT_UPDATE_SHARED_SIZE FRONT_UPDATE_THREADS*2
updateFront<OBBNode , OBB , BVTT_DEQUE_SIZE , FRONT_DEQUE_SIZE , FRONT_UPDATE_THREADS>
<<<NDEQUES, FRONT_UPDATE_THREADS>>>
(d_aFront , d_aOutputFront , d_aiFrontCounts , d_aWorkQueues , d_aiWorkQueueCounts , d_collisionPairs ,
d_collisionPairIndex , obbTree1 , d_triIndices1);
第二個片段中的內核代碼:
template<typename TreeNode , typename BV , unsigned int uiGlobalWorkQueueCapacity , unsigned int uiGlobalFrontCapacity ,
unsigned int uiNThreads>
void __global__ updateFront(Int2Array *aFront , Int2Array *aOutputFront , int *aiFrontIdx , Int2Array *aWork_queues ,
int* aiWork_queue_counts , int2 *auiCollisionPairs , unsigned int *uiCollisionPairsIdx , const TreeNode* tree ,
uint3 *aTriIndices)
{
__shared__ unsigned int uiInputFrontIdx;
__shared__ unsigned int uiOutputFrontIdx;
__shared__ unsigned int uiWorkQueueIdx;
__shared__ int iLeafLeafOffset;
__shared__ int iNode0GreaterOffset;
__shared__ int iNode1GreaterOffset;
__shared__ int aiLeafLeafFrontX[FRONT_UPDATE_SHARED_SIZE];
__shared__ int aiLeafLeafFrontY[FRONT_UPDATE_SHARED_SIZE];
__shared__ int aiNode0GreaterFrontX[FRONT_UPDATE_SHARED_SIZE];
__shared__ int aiNode0GreaterFrontY[FRONT_UPDATE_SHARED_SIZE];
__shared__ int aiNode1GreaterFrontX[FRONT_UPDATE_SHARED_SIZE];
__shared__ int aiNode1GreaterFrontY[FRONT_UPDATE_SHARED_SIZE];
if(threadIdx.x == 0)
{
uiInputFrontIdx = aiFrontIdx[blockIdx.x];
uiOutputFrontIdx = 0;
uiWorkQueueIdx = aiWork_queue_counts[blockIdx.x];
iLeafLeafOffset = 0;
iNode0GreaterOffset = 0;
iNode1GreaterOffset = 0;
}
__syncthreads();
unsigned int uiThreadOffset = threadIdx.x;
while(uiThreadOffset < uiInputFrontIdx + FRONT_UPDATE_THREADS - (uiInputFrontIdx % FRONT_UPDATE_THREADS))
{
if(uiThreadOffset < uiInputFrontIdx)
{
int2 bvttNode;
aFront->getElement(bvttNode , blockIdx.x*FRONT_DEQUE_SIZE + uiThreadOffset);
TreeNode node0 = tree[bvttNode.x];
TreeNode node1 = tree[bvttNode.y];
if(node0.isLeaf() && node1.isLeaf())
{
int iOffset = atomicAdd(&iLeafLeafOffset , 1);
//Bank conflict source
aiLeafLeafFrontX[iOffset] = bvttNode.x;
aiLeafLeafFrontY[iOffset] = bvttNode.y;
//End of bank conflict source
}
else if(node1.isLeaf() || (!node0.isLeaf() && (node0.bbox.getSize() > node1.bbox.getSize())))
{ // node0 is bigger. Subdivide it.
int iOffset = atomicAdd(&iNode0GreaterOffset , 1);
//Bank conflict source
aiNode0GreaterFrontX[iOffset] = bvttNode.x;
aiNode0GreaterFrontY[iOffset] = bvttNode.y;
//End of bank conflict source
}
else
{ // node1 is bigger. Subdivide it.
int iOffset = atomicAdd(&iNode1GreaterOffset , 1);
//Bank conflict source
aiNode1GreaterFrontX[iOffset] = bvttNode.x;
aiNode1GreaterFrontY[iOffset] = bvttNode.y;
//End of bank conflict source
}
}
__syncthreads();
/* ... */
uiThreadOffset += uiNThreads;
__syncthreads();
}
我想知道爲什麼銀行衝突ocurring。我認爲衝突可能發生的唯一方法是如果映射到同一個銀行的不同陣列中的訪問被序列化。
這是相當困難的,甚至不知道該共享存儲器陣列的尺寸銀行衝突發表評論.... – talonmies
我與內核調用代碼和常量定義編輯。 –