我注意到在cuda中有一个float1
结构体类型。例如,在使用float array
和float1 array
时,相对于简单的float
,是否存在性能优势?
struct __device_builtin__ float1
{
float x;
};
在
float4
中,根据场合不同,存在性能优势,因为对齐方式为 4x4 字节 = 16 字节。
这只是在带有 float1
参数的 __device__
函数中特殊使用吗?提前致谢。
根据@talonmies在CUDA Thrust reduction with double2 arrays的评论,我比较了使用CUDA Thrust和在float
和float1
之间切换计算向量范数的情况。我考虑了一个GT210卡(cc 1.2)上有N=1000000
个元素的数组。看起来,对于这两种情况,即计算范数需要大约3.4s
的时间,因此没有性能提升。从下面的代码中可以看出,也许float
比float1
更加舒适易用。
最后,请注意float4
的优点源于对齐__builtin__align__
而不是__device_builtin__
。
#include <thrust\device_vector.h>
#include <thrust\transform_reduce.h>
struct square
{
__host__ __device__ float operator()(float x)
{
return x * x;
}
};
struct square1
{
__host__ __device__ float operator()(float1 x)
{
return x.x * x.x;
}
};
void main() {
const int N = 1000000;
float time;
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
thrust::device_vector<float> d_vec(N,3.f);
cudaEventRecord(start, 0);
float reduction = sqrt(thrust::transform_reduce(d_vec.begin(), d_vec.end(), square(), 0.0f, thrust::plus<float>()));
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&time, start, stop);
printf("Elapsed time reduction: %3.1f ms \n", time);
printf("Result of reduction = %f\n",reduction);
thrust::host_vector<float1> h_vec1(N);
for (int i=0; i<N; i++) h_vec1[i].x = 3.f;
thrust::device_vector<float1> d_vec1=h_vec1;
cudaEventRecord(start, 0);
float reduction1 = sqrt(thrust::transform_reduce(d_vec1.begin(), d_vec1.end(), square1(), 0.0f, thrust::plus<float>()));
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&time, start, stop);
printf("Elapsed time reduction1: %3.1f ms \n", time);
printf("Result of reduction1 = %f\n",reduction1);
getchar();
}
__device_builtin__
没有性能影响,但我再也找不到那篇帖子了。 - Vitality