现在我试图加速我的Force-Directed图形实现。到目前为止,我已经实现了使用八叉树来减少计算次数的Barnes-Hut算法。我已经多次测试过,并且与力相关的计算次数确实大幅减少。下面是没有Barns-Hut(蓝线)和(红线)的节点数的计算图: 即使现在它应该快得多,事实是,在速度(时间)的问题上,升级只有百分之几。八叉树实现的速度问题
我想这可能是造成这种情况的一个部分,就是树的创建和树中的元素放置。由于元素不断移动,我需要在每个循环中重新创建树,直到达到一些停止条件。但是如果我会花很多时间来创建树,那么我会在那里花费大量时间来增加强制计算。至少这是我的想法。这是我如何在我的主文件循环添加元素:
void AddTreeElements(Octree* tree, glm::vec3* boundries, Graph& graph)
{
for(auto& node:graph.NodeVector())
{
node.parent_group = nullptr;
if(node.pos[0] < boundries[1][0] && node.pos[0] > boundries[0][0] &&
node.pos[1] > boundries[4][1] && node.pos[1] < boundries[1][1] &&
node.pos[2] < boundries[0][2] && node.pos[2] > boundries[3][2])
{
tree->AddObject(&node.second);
continue;
}
if(node.pos[0] < boundries[0][0])
{
boundries[0][0] = node.pos[0]-1.0f;
boundries[3][0] = node.pos[0]-1.0f;
boundries[4][0] = node.pos[0]-1.0f;
boundries[7][0] = node.pos[0]-1.0f;
}
else if(node.pos[0] > boundries[1][0])
{
boundries[1][0] = node.pos[0]+1.0f;
boundries[2][0] = node.pos[0]+1.0f;
boundries[5][0] = node.pos[0]+1.0f;
boundries[6][0] = node.pos[0]+1.0f;
}
if(node.pos[1] < boundries[4][1])
{
boundries[4][1] = node.pos[1]-1.0f;
boundries[5][1] = node.pos[1]-1.0f;
boundries[6][1] = node.pos[1]-1.0f;
boundries[7][1] = node.pos[1]-1.0f;
}
else if(node.pos[1] > boundries[0][1])
{
boundries[0][1] = node.pos[1]+1.0f;
boundries[1][1] = node.pos[1]+1.0f;
boundries[2][1] = node.pos[1]+1.0f;
boundries[3][1] = node.pos[1]+1.0f;
}
if(node.pos[2] < boundries[3][2])
{
boundries[2][2] = node.pos[2]-1.0f;
boundries[3][2] = node.pos[2]-1.0f;
boundries[6][2] = node.pos[2]-1.0f;
boundries[7][2] = node.pos[2]-1.0f;
}
else if(node.pos[2] > boundries[0][2])
{
boundries[0][2] = node.pos[2]+1.0f;
boundries[1][2] = node.pos[2]+1.0f;
boundries[4][2] = node.pos[2]+1.0f;
boundries[5][2] = node.pos[2]+1.0f;
}
}
}
我在做什么这里是经过我在图中的所有元素,并将它们添加到树的根部。另外,我正在扩展代表下一个循环的八叉树边界的方框,因此所有节点都将适合内部。如下所示
字段以八叉树更新重要:
Octree* trees[2][2][2];
glm::vec3 vBoundriesBox[8];
bool leaf;
float combined_weight = 0;
std::vector<Element*> objects;
,并负责更新代码的一部分:
#define MAX_LEVELS 5
void Octree::AddObject(Element* object)
{
this->objects.push_back(object);
}
void Octree::Update()
{
if(this->objects.size()<=1 || level > MAX_LEVELS)
{
for(Element* Element:this->objects)
{
Element->parent_group = this;
}
return;
}
if(leaf)
{
GenerateChildren();
leaf = false;
}
while (!this->objects.empty())
{
Element* obj = this->objects.back();
this->objects.pop_back();
if(contains(trees[0][0][0],obj))
{
trees[0][0][0]->AddObject(obj);
trees[0][0][0]->combined_weight += obj->weight;
} else if(contains(trees[0][0][1],obj))
{
trees[0][0][1]->AddObject(obj);
trees[0][0][1]->combined_weight += obj->weight;
} else if(contains(trees[0][1][0],obj))
{
trees[0][1][0]->AddObject(obj);
trees[0][1][0]->combined_weight += obj->weight;
} else if(contains(trees[0][1][1],obj))
{
trees[0][1][1]->AddObject(obj);
trees[0][1][1]->combined_weight += obj->weight;
} else if(contains(trees[1][0][0],obj))
{
trees[1][0][0]->AddObject(obj);
trees[1][0][0]->combined_weight += obj->weight;
} else if(contains(trees[1][0][1],obj))
{
trees[1][0][1]->AddObject(obj);
trees[1][0][1]->combined_weight += obj->weight;
} else if(contains(trees[1][1][0],obj))
{
trees[1][1][0]->AddObject(obj);
trees[1][1][0]->combined_weight += obj->weight;
} else if(contains(trees[1][1][1],obj))
{
trees[1][1][1]->AddObject(obj);
trees[1][1][1]->combined_weight += obj->weight;
}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<2;k++)
{
trees[i][j][k]->Update();
}
}
}
}
bool Octree::contains(Octree* child, Element* object)
{
if(object->pos[0] >= child->vBoundriesBox[0][0] && object->pos[0] <= child->vBoundriesBox[1][0] &&
object->pos[1] >= child->vBoundriesBox[4][1] && object->pos[1] <= child->vBoundriesBox[0][1] &&
object->pos[2] >= child->vBoundriesBox[3][2] && object->pos[2] <= child->vBoundriesBox[0][2])
return true;
return false;
}
因为我使用的指针围绕树移动元素我不认为对象创建/销毁是一个问题。一个地方,我想可能对速度的影响是这一个:
Element* obj = this->objects.back();
this->objects.pop_back();
if(contains(trees[0][0][0],obj))
虽然我不知道我该怎么ommit /加快速度。有人有什么建议可以在这里做什么?
编辑:
我已经做了一些餐巾数学,我想还有一个地方可能会造成重大的速度下降。 Boundries在Update
方法如下检查就像是做了很多,我的计算是,由于这种增加的复杂性是在最坏的情况下:
number_of_elements * number_of_childern * number_of_faces * MAX_LEVELS
这在我的情况下,等于到number_of_elements * 240。
有人可以确认我的想法是否合理吗?
http://codereview.stackexchange.com/ – Mihai
@Mihai我在你的建议后发布它:http://codereview.stackexchange.com/questions/127693/speed-concerns-of-octree-plementation – sebap123
DrunkCoder说的可能会帮助,但请记住性能优化的前三条规则:度量,度量,度量!为您的平台使用采样CPU分析器(例如,Linux上的perf +热点,Windows上的Visual Studio分析器或macOS上的Instruments),然后使用该数据查找性能原因。 – milianw