我正在阅读CUB文档和示例:
#include <cub/cub.cuh> // or equivalently <cub/block/block_radix_sort.cuh>
__global__ void ExampleKernel(...)
{
// Specialize BlockRadixSort for 128 threads owning 4 integer items each
typedef cub::BlockRadixSort<int, 128, 4> BlockRadixSort;
// Allocate shared memory for BlockRadixSort
__shared__ typename BlockRadixSort::TempStorage temp_storage;
// Obtain a segment of consecutive items that are blocked across threads
int thread_keys[4];
...
// Collectively sort the keys
BlockRadixSort(temp_storage).Sort(thread_keys);
...
}在这个例子中,每个线程有4个键。看起来'thread_keys‘将被分配到全局本地内存中。如果我每个线程只有一个键,我可以声明“int thread_key;”并且只在寄存器中设置这个变量吗?
BlockRadixSort(temp_storage).Sort()将指向键的指针作为参数。这是否意味着密钥必须在全局内存中?
我想使用这段代码,但我希望每个线程在寄存器中保留一个键,并在它们排序后将其保留在寄存器/共享内存中的芯片上。提前感谢!
发布于 2014-02-28 00:17:51
您可以使用共享内存(这将使其保持在芯片上)来做到这一点。我不确定我是否知道如何使用严格的寄存器而不解构BlockRadixSort对象。
下面是使用共享内存保存要排序的初始数据和最终排序结果的示例代码。这个示例主要是为每个线程设置一个数据元素,因为这似乎就是您所要求的。将其扩展到每个线程的多个元素并不困难,我已经完成了大部分工作,但数据合成和调试打印输出除外:
#include <cub/cub.cuh>
#include <stdio.h>
#define nTPB 32
#define ELEMS_PER_THREAD 1
// Block-sorting CUDA kernel (nTPB threads each owning ELEMS_PER THREAD integers)
__global__ void BlockSortKernel()
{
__shared__ int my_val[nTPB*ELEMS_PER_THREAD];
using namespace cub;
// Specialize BlockRadixSort collective types
typedef BlockRadixSort<int, nTPB, ELEMS_PER_THREAD> my_block_sort;
// Allocate shared memory for collectives
__shared__ typename my_block_sort::TempStorage sort_temp_stg;
// need to extend synthetic data for ELEMS_PER_THREAD > 1
my_val[threadIdx.x*ELEMS_PER_THREAD] = (threadIdx.x + 5)%nTPB; // synth data
__syncthreads();
printf("thread %d data = %d\n", threadIdx.x, my_val[threadIdx.x*ELEMS_PER_THREAD]);
// Collectively sort the keys
my_block_sort(sort_temp_stg).Sort(*static_cast<int(*)[ELEMS_PER_THREAD]>(static_cast<void*>(my_val+(threadIdx.x*ELEMS_PER_THREAD))));
__syncthreads();
printf("thread %d sorted data = %d\n", threadIdx.x, my_val[threadIdx.x*ELEMS_PER_THREAD]);
}
int main(){
BlockSortKernel<<<1,nTPB>>>();
cudaDeviceSynchronize();
}这对我来说似乎是正确的,在这种情况下,我碰巧使用了RHEL 5.5/gcc 4.1.2,CUDA 6.0RC和CUB v1.2.0 (相当新)。
据我所知,需要使用奇怪/丑陋的static casting,因为CUB Sort是长度等于定制参数ITEMS_PER_THREAD的expecting a reference to an array (即ELEMS_PER_THREAD):
__device__ __forceinline__ void Sort(
Key (&keys)[ITEMS_PER_THREAD],
int begin_bit = 0,
int end_bit = sizeof(Key) * 8)
{ ...https://stackoverflow.com/questions/21807872
复制相似问题