Tulip  6.0.0
Large graphs analysis and drawing
GraphParallelTools.h
1 /*
2  *
3  * This file is part of Tulip (https://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef GRAPH_PARALLEL_TOOLS_H
21 #define GRAPH_PARALLEL_TOOLS_H
22 
23 #include <tulip/Graph.h>
24 #include <tulip/ParallelTools.h>
25 
26 namespace tlp {
27 
28 // ===================================================================================
29 
30 template <typename NodeIndexFunction>
31 void inline TLP_MAP_NODES_AND_INDICES(const tlp::Graph *graph,
32  const NodeIndexFunction &nodeIndexFunction) {
33  unsigned int i = 0;
34  for (auto n : graph->nodes()) {
35  nodeIndexFunction(n, i++);
36  }
37 }
38 
39 // ===================================================================================
40 
41 /**
42  * Template function to ease the creation of OpenMP parallel jobs taking
43  * a graph node as parameter.
44  *
45  * @since Tulip 5.2
46  *
47  * @param graph the graph on which to run job on the nodes
48  * @param nodeFunction callable object (e.g. lambda function) taking a tlp::node as parameter
49  *
50  * Example of use:
51  *
52  * @code
53  * auto computationIntensiveTask = [&](const tlp::node &n) {
54  * double result = 0;
55  * ...
56  * return result;
57  * };
58  * tlp::NodeStaticProperty<double> result(graph);
59  * TLP_PARALLEL_MAP_NODES(graph, [&](const tlp::node &n) {
60  * // run task in a thread
61  * result[n] = computationIntensiveTask(n);
62  * });
63  * @endcode
64  */
65 template <typename NodeFunction>
66 void inline TLP_PARALLEL_MAP_NODES(const tlp::Graph *graph, const NodeFunction &nodeFunction) {
67  TLP_PARALLEL_MAP_VECTOR<tlp::node, NodeFunction>(graph->nodes(), nodeFunction);
68 }
69 
70 // ===================================================================================
71 
72 /**
73  * Template function to ease the creation of OpenMP parallel jobs taking
74  * a graph node and its iteration index as parameter.
75  *
76  * @since Tulip 5.2
77  *
78  * @param graph the graph on which to run job on the nodes
79  * @param nodeIndexFunction callable object (e.g. lambda function) taking a tlp::node and
80  * and unsigned integer as parameter
81  *
82  * Example of use:
83  *
84  * @code
85  * auto computationIntensiveTask = [&](const tlp::node &n, unsigned int i) {
86  * double result = 0;
87  * ...
88  * return result;
89  * };
90  * tlp::NodeStaticProperty<double> result(graph);
91  * TLP_PARALLEL_MAP_EDGES(graph, [&](const tlp::node &n, unsigned int i) {
92  * // run task in a thread
93  * result[n] = computationIntensiveTask(n, i);
94  * });
95  * @endcode
96  */
97 template <typename NodeFunction>
99  const NodeFunction &nodeFunction) {
100  TLP_PARALLEL_MAP_VECTOR_AND_INDICES<tlp::node, NodeFunction>(graph->nodes(), nodeFunction);
101 }
102 
103 // ===================================================================================
104 
105 template <typename EdgeIndexFunction>
106 void inline TLP_MAP_EDGES_AND_INDICES(const tlp::Graph *graph,
107  const EdgeIndexFunction &edgeIndexFunction) {
108  unsigned int i = 0;
109  for (auto e : graph->edges()) {
110  edgeIndexFunction(e, i++);
111  }
112 }
113 
114 // ===================================================================================
115 
116 /**
117  * Template function to ease the creation of OpenMP parallel jobs taking
118  * a graph edge as parameter.
119  *
120  * @since Tulip 5.2
121  *
122  * @param graph the graph on which to run job on the edges
123  * @param edgeFunction callable object (e.g. lambda function) taking a tlp::edge as parameter
124  *
125  * Example of use:
126  *
127  * @code
128  * auto computationIntensiveTask = [&](const tlp::edge &e) {
129  * double result = 0;
130  * ...
131  * return result;
132  * };
133  * tlp::EdgeStaticProperty<double> result(graph);
134  * TLP_PARALLEL_MAP_EDGES(graph, [&](const tlp::edge &e) {
135  * // run task in a thread
136  * result[e] = computationIntensiveTask(e);
137  * });
138  * @endcode
139  */
140 template <typename EdgeFunction>
141 void inline TLP_PARALLEL_MAP_EDGES(const tlp::Graph *graph, const EdgeFunction &edgeFunction) {
142  TLP_PARALLEL_MAP_VECTOR<tlp::edge, EdgeFunction>(graph->edges(), edgeFunction);
143 }
144 
145 // ===================================================================================
146 
147 /**
148  * Template function to ease the creation of OpenMP parallel jobs taking
149  * a graph edge and its iteration index as parameter.
150  *
151  * @since Tulip 5.2
152  *
153  * @param graph the graph on which to run job on the edges
154  * @param edgeIndexFunction callable object (e.g. lambda function) taking a tlp::edge and
155  * and unsigned integer as parameter
156  *
157  * Example of use:
158  *
159  * @code
160  * auto computationIntensiveTask = [&](const tlp::edge &e, unsigned int i) {
161  * double result = 0;
162  * ...
163  * return result;
164  * };
165  * tlp::EdgeStaticProperty<double> result(graph);
166  * TLP_PARALLEL_MAP_EDGES(graph, [&](const tlp::edge &e, unsigned int i) {
167  * // run task in a thread
168  * result[e] = computationIntensiveTask(e, i);
169  * });
170  * @endcode
171  */
172 template <typename EdgeFunction>
174  const EdgeFunction &edgeFunction) {
175  TLP_PARALLEL_MAP_VECTOR_AND_INDICES<tlp::edge, EdgeFunction>(graph->edges(), edgeFunction);
176 }
177 } // namespace tlp
178 
179 #endif
virtual const std::vector< node > & nodes() const =0
Return a const reference on the vector of nodes of the graph It is the fastest way to access to nodes...
virtual const std::vector< edge > & edges() const =0
Return a const reference on the vector of edges of the graph It is the fastest way to access to edges...
void TLP_PARALLEL_MAP_EDGES(const tlp::Graph *graph, const EdgeFunction &edgeFunction)
void TLP_PARALLEL_MAP_EDGES_AND_INDICES(const tlp::Graph *graph, const EdgeFunction &edgeFunction)
void TLP_PARALLEL_MAP_NODES_AND_INDICES(const tlp::Graph *graph, const NodeFunction &nodeFunction)
void TLP_PARALLEL_MAP_NODES(const tlp::Graph *graph, const NodeFunction &nodeFunction)