[英]std::vector::push_back throws segmentation fault in GraphLab apply()
我正在尝试使用GraphLab实现BFS算法。 在我的vertex_data结构中,我正在使用向量来保存到目前为止已遍历的路径。
struct vertex_data {
std::vector<int> current_path;
bool visited;
int destination;
vertex_data() {
destination = -1;
visited = false;
}
}
然后在apply()函数中,我尝试将当前顶点ID推送到该特定顶点的current_path:
class bfs: public graphlab::ivertex_program<graph_type, graphlab::empty,
vertex_data> {
private:
vertex_data msg;
public:
void apply(icontext_type& context, vertex_type& vertex, const gather_type& gather_result) {
int vid = vertex.id();
vertex.data().destination = msg.destination;
vertex.data().visited = true;
msg.current_path.push_back(vid);
vertex.data().current_path.push_back(vid);
}
}
此外,文档中data()的定义是:
vertex_data_type & data ()
Returns a reference to the data on the vertex.
GraphLab负责将vertex_data对象包装为vertex_data_type。 当我运行它时,在某些顶点的push_back()函数中出现段错误。 我检查了vid,它始终是一个带有实际值的整数。 运行vertex.data()。current_path.size(),我得到0。
我尝试了几种方法,例如创建本地矢量,并在vertex.data()处替换本地矢量,然后在推送ID之前调整其大小。 在第一种情况下,尝试更换时出现段错误,在第二种情况下,重新调整大小没有问题,但是推压一直在给我段错误。
您对可能的问题有任何想法吗?
谢谢
Graphlab中的类需要可序列化。 因此,如果变量不是普通旧数据(POD),则需要编写自己的加载和保存函数。
在您的情况下,应该是这样的(请注意,加载和保存列表的顺序应相同):
struct vertex_data {
std::vector<int> current_path;
bool visited;
int destination;
vertex_data() {
destination = -1;
visited = false;
}
void save(graphlab::oarchive& oarc) const {
oarc << current_path << visited << destination;
}
void load(graphlab::iarchive& iarc) {
iarc >> current_path >> visited >> destination;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.