[英]Boost graph -- storing vertices / edges as std::list and subsequent random access of associated vertex / edge property map
[英]Random access of Vertices using Boost::graph
我試圖使用OpenMP並行迭代我的boost圖的頂點。 這似乎需要一個支持隨機訪問元素的迭代器(例如, itr[i]
得到第i
個元素)。 但是, vertices(g)
返回的迭代器(一個vertex_iterator
)似乎不支持這個。 有沒有一種有效,干凈的方法來實現這一目標? 理想情況下,我只想要一個標准的循環,如下所示:
for (int i = 0; i < num_vertices; i++) {
vertex v = itr[i];
// Compute on vertex
}
這將與OpenMP合作。 謝謝!
使用adjacency_list<..., vecS, ...>
或adjacency_matrix
將通過具有整數類型的頂點描述符來實現此目的。
開箱即用的想法,看一下Parallel Boost Graph Library (Parallel BGL)。 它很可能會做你想要的(和更多),但更好?
示例輸出(在我的系統上):
Generated 50000000 vertices in 1879ms
Using 8 threads.
Sum of volumes for 50000000 vertices in 94ms: 2.5603e+10
完整列表:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/random.hpp>
#include <chrono>
#include <iostream>
#include <omp.h>
#include <random>
static std::mt19937 prng { std::random_device{}() };
struct MyVertex {
uintmax_t volume = [] { static std::uniform_int_distribution<int> pick(0, 1024); return pick(prng); }();
};
using namespace boost;
using G = adjacency_list<vecS, vecS, directedS, MyVertex>;
G generate() {
using namespace std::chrono;
auto start = high_resolution_clock::now();
G g;
generate_random_graph(g, 50000000, 0, prng);
auto end = high_resolution_clock::now();
std::cerr << "Generated " << num_vertices(g) << " vertices " << "in " << duration_cast<milliseconds>(end-start).count() << "ms\n";
return g;
}
int main() {
auto const g = generate();
using namespace std::chrono;
auto start = high_resolution_clock::now();
double sum = 0;
#pragma omp parallel
{
#pragma omp single
std::cerr << "Using " << omp_get_num_threads() << " threads.\n";
#pragma omp for reduction(+:sum)
for (G::vertex_descriptor u = 0; u < num_vertices(g); ++u) {
sum += g[vertex(u, g)].volume;
}
}
auto end = high_resolution_clock::now();
std::cerr << "Sum of volumes for " << num_vertices(g) << " vertices "
<< "in " << duration_cast<milliseconds>(end-start).count() << "ms: " << sum << "\n";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.