我正在嘗試運行一個異步函式,向它傳遞一個要執行的函式 f 和一個模板函式 f0 作為屬性。
這是我通過模板創建的功能
DivideVerticesInThreads<0> f0(g, {}, {});
模板是
template <int NSegments, int Segment> struct SegmentVertices {
std::hash<Graph::vertex_descriptor> _h;
bool operator()(Graph::vertex_descriptor vd) const { return (_h(vd) % NSegments) == Segment; }
};
template <int N>
using DivideVerticesInThreads = boost::filtered_graph<Graph, boost::keep_all, SegmentVertices<4, N>>;
然后我這樣呼叫異步函式
auto handle = async(std::launch::async,f, f0);
我將函式 f 傳遞給它:
auto f = [&](G const& f0) {
for(vp = vertices(f0); vp.first != vp.second; vp.first){
//...
}
};
完整的一段代碼是:
template<typename G>
void parallel_count_adj_luby(G g){
property_map<Graph,vertex_degree_t>::type deg = get(vertex_degree, g);
auto f = [&](G const& g1) {
for(vp = vertices(g1); vp.first != vp.second; vp.first){
// ...
}
};
DivideVerticesInThreads<0> f0(g, {}, {});
auto handle = async(std::launch::async,f, f0);
}
問題是異步函式給了我這個錯誤
error: no matching function for call to 'async(std::launch, parallel_count_adj_luby(G) [with G = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::property<boost::vertex_color_t, int, boost::property<boost::vertex_degree_t, int> > >]::<lambda(const boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::property<boost::vertex_color_t, int, boost::property<boost::vertex_degree_t, int> > >&)>&, DivideVerticesInThreads<0>&)'
auto handle = async(std::launch::async,f, f0);
我不太了解 c 和未來,所以錯誤可能是假的。我做錯了什么?
uj5u.com熱心網友回復:
我有點同意評論者的觀點,即問題的闡述是不必要的不??清楚。1
但是,我想我知道發生了什么是由于我在較早的答案中向您展示此示例代碼時所做的選擇。
看來我專注于效率,它讓你遇到了你不知道如何處理的 C 挑戰。
我將忽略混淆并洗掉帶有運行時引數的靜態型別引數。這樣你的過濾圖段都可以有相同的靜態型別
實時編譯器資源管理器
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/filtered_graph.hpp>
#include <boost/graph/random.hpp>
#include <iostream>
#include <random>
static std::mt19937 s_prng(std::random_device{}());
using G = boost::adjacency_list<>;
using V = G::vertex_descriptor;
struct SegmentVertices {
unsigned nsegments, segment;
bool operator()(V vd) const {
return (std::hash<V>{}(vd) % nsegments) == segment;
}
};
using F = boost::filtered_graph<G, boost::keep_all, SegmentVertices>;
G make_graph() {
G g;
generate_random_graph(g, 32 * 1024 - (s_prng() % 37), 64 * 1024, s_prng);
return g;
}
void the_function(G const& g)
{
std::cout << "Full graph " << size(boost::make_iterator_range(vertices(g)))
<< " vertices\n";
}
void the_function(F const& f)
{
auto& pred = f.m_vertex_pred;
std::cout << pred.segment << "/" << pred.nsegments << " "
<< size(boost::make_iterator_range(vertices(f))) << " vertices\n";
}
int main()
{
G g = make_graph();
the_function(g);
unsigned NSegments = s_prng() % 10 2;
for (unsigned seg = 0; seg < NSegments; seg) {
the_function(F(g, {}, {seg, NSegments}));
}
}
列印例如
Full graph 32736 vertices
0/3 10912 vertices
1/3 10912 vertices
2/3 10912 vertices
或者
Full graph 32741 vertices
0/7 4678 vertices
1/7 4678 vertices
2/7 4677 vertices
3/7 4677 vertices
4/7 4677 vertices
5/7 4677 vertices
6/7 4677 vertices
如您所見,使用運行時過濾器引數以性能換取運行時靈活性和型別便利性。
1 E.g. even the first claim "This is the function that I create through a template" makes no sense, because what follows is not a function and no template is being used to create it either.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/360662.html
