免責事項:
あなたは明らかに実際のコードを投稿しておらず、あなたの例はいくつかの無関係なコード行のように見えるので、私の答えはあなたが探しているものではないかもしれません-SSCCEは良かったでしょう。
私が正しく理解していれば、MyStruct
sのベクトルをすべての構造体メンバーの値の合計に変換する必要があります。thrust::add
これを行うには、バイナリ加算( )と単項演算が必要です。aを取り、MyStruct
そのメンバー値の加算を返します。
struct MyStruct {
float value1;
float value2;
};
std::vector<MyStruct> myvec;
/* fill myvec */
//C++11 with lambdas:
auto result = thrust::transform_reduce(begin(myvec), end(myvec),
[](MyStruct const& ms) { //unary op for transform
return ms.value1 + ms.value2;
},
0, thrust::add);
//C++03 with a functor:
struct MyStructReducer {
float operator()(MyStruct const& ms) {
return ms.value1 + ms.value2;
}
};
float result = thrust::transform_reduce(myvec.begin, myvec.end(),
MyStructReducer(), 0, thrust::add);
Reducerクラスの代わりにfree関数を使用することもできます。
//C++03 with a function:
float reduceMyStruct(MyStruct const& ms) {
return ms.value1 + ms.value2;
}
/* ... */
float result = thrust::transform_reduce(myvec.begin, myvec.end(),
reduceMyStruct, 0, thrust::add);
HTH