【发布时间】:2021-06-08 19:12:48
【问题描述】:
我正在使用 opengl(实现 sph 算法)开发流体模拟器。我尝试了很多方法来运行我的模拟器,首先我使用八叉树,然后是哈希图,现在我尝试使用 Z 顺序,为此我需要根据它们的索引对我的粒子进行排序。
我难以理解的是,如果我有一个推力::sort 需要 15 毫秒,如果我有两个推力::sort 需要 17 毫秒。
为了更清楚,我在 opengl 中做我的模拟器(我所有的缓冲区都是使用 opengl 创建的),并且我使用 cuda 互操作来对我的缓冲区进行排序,它使用 cuda。
这是我获取缓冲区并将它们“链接”到 cuda 的部分
//I use this if to do the registerBuffer only one time
if (first == 0) {
//index
IBuffer* bIndex = RESOURCEMANAGER->getBuffer("particleLib::Index");
int buffIdIndex = bIndex->getPropi(IBuffer::ID);
//Position
IBuffer* bPosition = RESOURCEMANAGER->getBuffer("particleLib::Position");
int buffIdPosition = bPosition->getPropi(IBuffer::ID);
//TempIndex
IBuffer* bTempIndex = RESOURCEMANAGER->getBuffer("particleLib::TempIndex");
int buffIdTempIndex = bTempIndex->getPropi(IBuffer::ID);
//Velocity
IBuffer* bVelocity = RESOURCEMANAGER->getBuffer("particleLib::Velocity");
int buffIdVelocity = bVelocity->getPropi(IBuffer::ID);
// register this buffer object with CUDA
//So devia chamar isto uma vez
cudaGraphicsGLRegisterBuffer(&cuda_ssbo_Index, buffIdIndex, cudaGraphicsMapFlagsNone);
cudaGraphicsGLRegisterBuffer(&cuda_ssbo_TempIndex, buffIdTempIndex, cudaGraphicsMapFlagsNone);
cudaGraphicsGLRegisterBuffer(&cuda_ssbo_Position, buffIdPosition, cudaGraphicsMapFlagsNone);
cudaGraphicsGLRegisterBuffer(&cuda_ssbo_Velocity, buffIdVelocity, cudaGraphicsMapFlagsNone);
first = 1;
}
// map OpenGL buffer object for writing from CUDA
int* dptrssboIndex;
int* dptrssboTempIndex;
float4 * dptrssboPosition;
float4 * dptrssboVelocity;
cudaGraphicsMapResources(1, &cuda_ssbo_Index, 0);
cudaGraphicsMapResources(1, &cuda_ssbo_TempIndex, 0);
cudaGraphicsMapResources(1, &cuda_ssbo_Position, 0);
cudaGraphicsMapResources(1, &cuda_ssbo_Velocity, 0);
size_t num_bytesssbo_Index;
size_t num_bytesssbo_TempIndex;
size_t num_bytesssbo_Position;
size_t num_bytesssbo_Velocity;
cudaGraphicsResourceGetMappedPointer((void**)&dptrssboIndex, &num_bytesssbo_Index, cuda_ssbo_Index);
cudaGraphicsResourceGetMappedPointer((void**)&dptrssboTempIndex, &num_bytesssbo_TempIndex, cuda_ssbo_TempIndex);
cudaGraphicsResourceGetMappedPointer((void**)&dptrssboPosition, &num_bytesssbo_Position, cuda_ssbo_Position);
cudaGraphicsResourceGetMappedPointer((void**)&dptrssboVelocity, &num_bytesssbo_Velocity, cuda_ssbo_Velocity);
mysort(&dptrssboIndex,&dptrssboPosition, &dptrssboTempIndex, &dptrssboVelocity,216000);
cudaGraphicsUnmapResources(1, &cuda_ssbo_Index, 0);
cudaGraphicsUnmapResources(1, &cuda_ssbo_TempIndex, 0);
cudaGraphicsUnmapResources(1, &cuda_ssbo_Position, 0);
cudaGraphicsUnmapResources(1, &cuda_ssbo_Velocity, 0);
这是来自 mysort 的代码
void mysort(int ** index1, float4 ** values1, int** index2, float4 ** values2,int particles){
thrust::device_ptr<int> i1buff = thrust::device_pointer_cast(*(index1));
thrust::device_ptr<float4> v1buff = thrust::device_pointer_cast(*(values1));
thrust::device_ptr<int> i2buff = thrust::device_pointer_cast(*(index2));
thrust::device_ptr<float4> v2buff = thrust::device_pointer_cast(*(values2));
//sorts
thrust::stable_sort_by_key(i1buff, i1buff + particles,v1buff); // 15 ms
//cudaThreadSynchronize();
thrust::stable_sort_by_key(i2buff, i2buff + particles, v2buff); // 17 ms
//repetido so para ver o tempo
thrust::stable_sort_by_key(i1buff, i1buff + particles, v1buff);
//cudaThreadSynchronize();
thrust::stable_sort_by_key(i2buff, i2buff + particles, v2buff); //4 sorts -> 19 ms
//cudaThreadSynchronize();
}
有人可以解释发生了什么吗?
编辑1: 我使用 cudaDeviceSynchronize() 来测量每种排序所需的时间(如@Jérôme-Richard 所示),即使我更改订单,第一次排序总是需要更长的时间。 另一个事实是,如果我的相机离场景更近,第一种需要更长的时间,这表明 Cuda 可能正在等待 opengl 完成他的工作,从而使第一种“需要更长时间”。 我还尝试在我的 mysort() 函数上没有排序,我里面唯一的东西是 cudaDeviceSynchronize() 并且它花了 15 毫秒,这再次表明 cuda 可能正在等待 opengl 从最后一帧完成工作.
编辑2: 我做了更多的调试,我认为似乎是真的。真正的减速来自 cudaGraphicsMapResources 调用。据此(cudaGraphicsMapResources slow speed when mapping DirectX texture):
此函数提供同步保证,即在 cudaGraphicsMapResources() 之前发出的任何图形调用将在流中发出的任何后续 CUDA 工作开始之前完成。
是的,它正在等待 opengl 绘制一些东西,因为 camara 距离会影响 cudaGraphicsMapResources 花费的时间。
【问题讨论】:
-
我自己也不确定,但我估计第一次排序调用的时间包括其他操作,比如内存分配和传输?您的通话时间究竟是怎样的?
-
可能会发生一些事情,我假设进入 mysort 的所有值都已经在主内存中,因此随后对该函数的调用或多或少相同的值将导致它们被留下在 cpu 缓存中(比内存快),因此给你一些额外的性能。如果您真的想根据调用次数来衡量性能,请使用迭代次数,例如 1000,2000 ......然后标准化。但是,如果您在进行分析,则与后续调用相关的函数性能不如与整个程序相关的性能重要。
-
嗨@Beko,排序不应该分配内存,因为缓冲区已经在gpu上。我知道时间,因为我正在使用一个名为 Nau3d 的成本引擎,并且分析器告诉我每个“步骤”(通过)所花费的时间。时间应该是正确的,因为我可以看到排序对 fps 的影响。当我进行 1 次排序时,我的 fps 计数会受到影响,而当我进行 2 次排序时,fps 几乎保持不变。
-
好的。只是我在使用
nvprof之前也有过类似的经历,在内核调用之后的第一个cudaMemcpy所用的时间比之后的要长得多。那是因为cudaMemcpy进行了隐式同步,所以cudaMemcpy的计时器实际上包括内核完成所花费的时间等。也许您仍然可以尝试以更“传统”的方式测量时间, IE。通过cudaEvents或类似的东西(不知道这是否适用于Thrust),看看结果是否不同。但不幸的是,除此之外,我一无所知。 -
@Beko 我认为你是对的。我认为 cuda 正在等待 opengl 完成他的工作,这就是为什么第一种需要更长的时间。第一种需要的时间越长,我的相机离场景越近(我越接近 opengl 要做的工作就越多)。这表明 cuda 可能正在等待 opengl 完成。