2017-05-26 8 views
0

クーダスラスト - 私は通常、次の操作を行いフロートのアレイ上の削減を実行したい最大vec3

float res = *thrust::max_element(thrust::device, 
      thrust::device_ptr<float>(dDensities), 
      thrust::device_ptr<float>(dDensities+numParticles) 
      ); 

は、しかし、私が今やりたいものをvec3にほとんど同じことです(GLMライブラリタイプ)配列:

float res = *thrust::max_element(thrust::device, 
      thrust::device_ptr<glm::vec3>(dDensities), 
      thrust::device_ptr<glm::vec3>(dDensities+numParticles) 
      ); 

あなたが見ることができるように「<」演算子は、上で定義されていないため、これは何の意味もありません。しかし、私は彼の長さに基づいて最大vec3を取得したいと思います:

len = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z); 

それは可能ですか?

答えて

1

はい、可能です。 thrust quickstart guideをよく読んでいない場合は、こちらをご覧ください。

thrust extrema documentationを確認すると、多くの推力アルゴリズムと同様に、thrust::max_elementにはいくつかの種類があります。これらのうちの1つはバイナリ比較ファンクタを受け入れます。私たちは、あなたが望むことをする比較関数を定義することができます。ここで

は些細な例を挙げて取り組んだ:

$ cat t134.cu 
#include <thrust/extrema.h> 
#include <thrust/device_ptr.h> 
#include <glm/glm.hpp> 
#include <iostream> 

struct comp 
{ 
template <typename T> 
__host__ __device__ 
bool operator()(T &t1, T &t2){ 
    return ((t1.x*t1.x+t1.y*t1.y+t1.z*t1.z) < (t2.x*t2.x+t2.y*t2.y+t2.z*t2.z)); 
    } 
}; 

int main(){ 

    int numParticles = 3; 
    glm::vec3 d[numParticles]; 
    d[0].x = 0; d[0].y = 0; d[0].z = 0; 
    d[1].x = 2; d[1].y = 2; d[1].z = 2; 
    d[2].x = 1; d[2].y = 1; d[2].z = 1; 

    glm::vec3 *dDensities; 
    cudaMalloc(&dDensities, numParticles*sizeof(glm::vec3)); 
    cudaMemcpy(dDensities, d, numParticles*sizeof(glm::vec3), cudaMemcpyHostToDevice); 
    glm::vec3 res = *thrust::max_element(thrust::device, 
      thrust::device_ptr<glm::vec3>(dDensities), 
      thrust::device_ptr<glm::vec3>(dDensities+numParticles), 
      comp() 
      ); 
    std::cout << "max element x: " << res.x << " y: " << res.y << " z: " << res.z << std::endl; 
} 
$ nvcc -arch=sm_61 -o t134 t134.cu 
$ ./t134 
max element x: 2 y: 2 z: 2 
$ 
関連する問題