Cuda 推力 - 最大 vec3
Cuda Thrust - max vec3
当我想对 float 数组进行归约时,我通常会执行以下操作:
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);
这可能吗?
是的,有可能。如果您还不熟悉 thrust quickstart guide,您可能需要阅读它。
如果您查看 thrust extrema documentation,您会注意到 thrust::max_element
有几种不同的变体(大多数推力算法也是如此)。其中之一接受二进制比较函子。我们可以定义一个比较仿函数,它会做你想做的事。
这是一个简单的工作示例:
$ 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
$
当我想对 float 数组进行归约时,我通常会执行以下操作:
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);
这可能吗?
是的,有可能。如果您还不熟悉 thrust quickstart guide,您可能需要阅读它。
如果您查看 thrust extrema documentation,您会注意到 thrust::max_element
有几种不同的变体(大多数推力算法也是如此)。其中之一接受二进制比较函子。我们可以定义一个比较仿函数,它会做你想做的事。
这是一个简单的工作示例:
$ 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
$