1

私は使っている

thrust::sequence(myvector.begin(), myvector.end(), 0, 1)

次のような適切な順序付きリストを実現します。

0, 1, 2, 3, 4

私の質問は、以下のようなリストをどのように達成できるかです (最善の方法は?)

0, 0, 0, 1, 1, 1, 2, 2 ,2, 3, 3, 3

私はファンクターでそれを作る方法を知っているので、ファンクターで答えようとしないでください。Thrustに最適化された方法があるかどうかを知りたい、または簡単な方法が欠けている..

4

1 に答える 1

4

このようなもの:

thrust::device_vector<int> myvector(N);

thrust::transform( thrust::make_counting_iterator(0),
                   thrust::make_counting_iterator(N),
                   thrust::make_constant_iterator(3),
                   myvector.begin(),
                   thrust::divides<int>() );

(免責事項、ブラウザで書かれ、コンパイルもテストもされていない、自己責任で使用してください)

[0..N]//3で結果を計算して出力することにより、探しているシーケンスが得られるはずですmyvector


バージョンのコンパイルに問題があるため、コンパイルして実行する完全な例を次に示します。

#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/constant_iterator.h>
#include <cstdio>

int main(void)
{
    const int N = 18, M = 3;
    thrust::device_vector<int> myvector(N);

    thrust::transform(  thrust::make_counting_iterator(0),
                        thrust::make_counting_iterator(N),
                        thrust::make_constant_iterator(M),
                        myvector.begin(),
                        thrust::divides<int>() );

    for(int i=0; i<N; i++) {
        int val = myvector[i];
        printf("%d %d\n", i, val);
    }
    return 0;
}
于 2012-06-13T14:53:30.290 に答える