7

同じサイズの配列があります(要素3よりも多い)。300.000浮動小数点数の 1 つの配列とインデックスの 2 つの配列。したがって、番号ごとに2IDがあります。

すべての3配列は既に GPU グローバル メモリにあります。それに応じて、すべての番号を ID で並べ替えたいと思います。

Thrust ライブラリを使用してこのタスクを実行する方法はありますか? Thrustライブラリよりも良い方法はありますか?

もちろん、私はそれらをホスト メモリとの間で何度もコピーしたくありません。ちなみに、これらはベクトルではなく配列です。

事前にご協力いただきありがとうございます。


暫定的な解決策ですが、これは非常に遅いです。ほぼ4数秒かかり、配列サイズは次の順序です300000

thrust::device_ptr<float> keys(afterSum);
thrust::device_ptr<int> vals0(d_index);
thrust::device_ptr<int> vals1(blockId); 

thrust::device_vector<int> sortedIndex(numElements);
thrust::device_vector<int> sortedBlockId(numElements);

thrust::counting_iterator<int> iter(0);
thrust::device_vector<int> indices(numElements);
thrust::copy(iter, iter + indices.size(), indices.begin()); 

thrust::sort_by_key(keys, keys + numElements , indices.begin());    

thrust::gather(indices.begin(), indices.end(), vals0, sortedIndex.begin());
thrust::gather(indices.begin(), indices.end(), vals1, sortedBlockId.begin());

thrust::host_vector<int> h_sortedIndex=sortedIndex;
thrust::host_vector<int> h_sortedBlockId=sortedBlockId;
4

3 に答える 3

11

もちろん、スラストを使用することもできます。まず、生のCUDAデバイスポインターをでラップする必要がありますthrust::device_ptr。float値が配列pkeysにあり、IDが配列pvals0pvals1にあり、numElementsが配列の長さであるとすると、次のように機能するはずです。

#include <thrust/device_ptr.h>
#include <thrust/sort.h>
#include <thrust/gather.h>
#include <thrust/iterator/counting_iterator.h>

cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);

cudaEventRecord(start);

thrust::device_ptr<float> keys(pkeys);
thrust::device_ptr<int> vals0(pvals0);
thrust::device_ptr<int> vals1(pvals1);

// allocate space for the output
thrust::device_vector<int> sortedVals0(numElements);
thrust::device_vector<int> sortedVals1(numElements);

// initialize indices vector to [0,1,2,..]
thrust::counting_iterator<int> iter(0);
thrust::device_vector<int> indices(numElements);
thrust::copy(iter, iter + indices.size(), indices.begin());

// first sort the keys and indices by the keys
thrust::sort_by_key(keys.begin(), keys.end(), indices.begin());

// Now reorder the ID arrays using the sorted indices
thrust::gather(indices.begin(), indices.end(), vals0.begin(), sortedVals0.begin());
thrust::gather(indices.begin(), indices.end(), vals1.begin(), sortedVals1.begin());

cudaEventRecord(stop);
cudaEventSynchronize(stop);
float milliseconds = 0;
cudaEventElapsedTime(&milliseconds, start, stop);
printf("Took %f milliseconds for %d elements\n", milliseconds, numElements);
于 2011-07-08T00:43:24.700 に答える