PGROUTING  3.2
max_flow.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: max_flow_many_to_many.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2016 Andrea Nardelli
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28  ********************************************************************PGR-GNU*/
29 
30 #include <stdbool.h>
31 
33 #include "utils/array.h"
34 
35 
36 #include "c_common/debug_macro.h"
37 #include "c_common/e_report.h"
38 #include "c_common/time_msg.h"
39 #include "c_common/edges_input.h"
40 #include "c_common/arrays_input.h"
43 
44 PGDLLEXPORT Datum
45 _pgr_maxflow(PG_FUNCTION_ARGS);
46 
47 static
48 void
50  char *edges_sql,
51  char *combinations_sql,
52  ArrayType *starts,
53  ArrayType *ends,
54  int algorithm,
55  bool only_flow,
56  pgr_flow_t **result_tuples,
57  size_t *result_count) {
58  if (algorithm < 1 || algorithm > 3) {
59  elog(ERROR, "Unknown algorithm");
60  }
61 
63 
64  int64_t *source_vertices = NULL;
65  size_t size_source_verticesArr = 0;
66 
67  int64_t *sink_vertices = NULL;
68  size_t size_sink_verticesArr = 0;
69 
70  pgr_edge_t *edges = NULL;
71  size_t total_edges = 0;
72 
73  pgr_combination_t *combinations = NULL;
74  size_t total_combinations = 0;
75 
76  if (starts && ends) {
77  source_vertices = (int64_t*)
78  pgr_get_bigIntArray(&size_source_verticesArr, starts);
79  sink_vertices = (int64_t*)
80  pgr_get_bigIntArray(&size_sink_verticesArr, ends);
81  } else if (combinations_sql) {
82  pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
83  if (total_combinations == 0) {
84  if (combinations)
85  pfree(combinations);
87  return;
88  }
89  }
90 
91  /* NOTE:
92  * For flow, cost and reverse_cost are really capacity and reverse_capacity
93  */
94  pgr_get_flow_edges(edges_sql, &edges, &total_edges);
95 
96  if (total_edges == 0) {
97  if (source_vertices) pfree(source_vertices);
98  if (sink_vertices) pfree(sink_vertices);
100  return;
101  }
102 
103 
104  PGR_DBG("Starting timer");
105  clock_t start_t = clock();
106  char* log_msg = NULL;
107  char* notice_msg = NULL;
108  char *err_msg = NULL;
109 
111  edges, total_edges,
112  combinations, total_combinations,
113  source_vertices, size_source_verticesArr,
114  sink_vertices, size_sink_verticesArr,
115  algorithm,
116  only_flow,
117 
118  result_tuples, result_count,
119 
120  &log_msg,
121  &notice_msg,
122  &err_msg);
123 
124  if (only_flow) {
125  time_msg("pgr_maxFlow(many to many)",
126  start_t, clock());
127  } else if (algorithm == 1) {
128  time_msg("pgr_maxFlowPushRelabel(many to many)",
129  start_t, clock());
130  } else if (algorithm == 3) {
131  time_msg("pgr_maxFlowEdmondsKarp(many to many)",
132  start_t, clock());
133  } else {
134  time_msg("pgr_maxFlowBoykovKolmogorov(many to many)",
135  start_t, clock());
136  }
137 
138 
139  if (edges) pfree(edges);
140  if (source_vertices) pfree(source_vertices);
141  if (sink_vertices) pfree(sink_vertices);
142 
143  if (err_msg && (*result_tuples)) {
144  pfree(*result_tuples);
145  (*result_tuples) = NULL;
146  (*result_count) = 0;
147  }
148 
149  pgr_global_report(log_msg, notice_msg, err_msg);
150 
151  if (log_msg) pfree(log_msg);
152  if (notice_msg) pfree(notice_msg);
153  if (err_msg) pfree(err_msg);
154 
155  pgr_SPI_finish();
156 }
157 
158 
160 PGDLLEXPORT Datum
161 _pgr_maxflow(PG_FUNCTION_ARGS) {
162  FuncCallContext *funcctx;
163  TupleDesc tuple_desc;
164 
165  /**************************************************************************/
166  pgr_flow_t *result_tuples = 0;
167  size_t result_count = 0;
168  /**************************************************************************/
169 
170  if (SRF_IS_FIRSTCALL()) {
171  MemoryContext oldcontext;
172  funcctx = SRF_FIRSTCALL_INIT();
173  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
174 
175 
176  /**********************************************************************/
177 
178  if (PG_NARGS() == 5) {
179  /*
180  * many to many
181  */
182  process(
183  text_to_cstring(PG_GETARG_TEXT_P(0)),
184  NULL,
185  PG_GETARG_ARRAYTYPE_P(1),
186  PG_GETARG_ARRAYTYPE_P(2),
187  PG_GETARG_INT32(3),
188  PG_GETARG_BOOL(4),
189  &result_tuples,
190  &result_count);
191 
192  } else if (PG_NARGS() == 4) {
193  /*
194  * combinations
195  */
196  process(
197  text_to_cstring(PG_GETARG_TEXT_P(0)),
198  text_to_cstring(PG_GETARG_TEXT_P(1)),
199  NULL,
200  NULL,
201  PG_GETARG_INT32(2),
202  PG_GETARG_BOOL(3),
203  &result_tuples,
204  &result_count);
205  }
206 
207  /* */
208  /**********************************************************************/
209 
210 #if PGSQL_VERSION > 95
211  funcctx->max_calls = result_count;
212 #else
213  funcctx->max_calls = (uint32_t)result_count;
214 #endif
215  funcctx->user_fctx = result_tuples;
216  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
217  != TYPEFUNC_COMPOSITE) {
218  ereport(ERROR,
219  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
220  errmsg("function returning record called in context "
221  "that cannot accept type record")));
222  }
223 
224  funcctx->tuple_desc = tuple_desc;
225  MemoryContextSwitchTo(oldcontext);
226  }
227 
228  funcctx = SRF_PERCALL_SETUP();
229  tuple_desc = funcctx->tuple_desc;
230  result_tuples = (pgr_flow_t *) funcctx->user_fctx;
231 
232  if (funcctx->call_cntr < funcctx->max_calls) {
233  HeapTuple tuple;
234  Datum result;
235  Datum *values;
236  bool *nulls;
237  size_t call_cntr = funcctx->call_cntr;
238 
239  /**********************************************************************/
240  /* MODIFY AS NEEDED */
241  values = palloc(6 * sizeof(Datum));
242  nulls = palloc(6 * sizeof(bool));
243 
244  size_t i;
245  for (i = 0; i < 6; ++i) {
246  nulls[i] = false;
247  }
248 
249  values[0] = Int32GetDatum(call_cntr + 1);
250  values[1] = Int64GetDatum(result_tuples[call_cntr].edge);
251  values[2] = Int64GetDatum(result_tuples[call_cntr].source);
252  values[3] = Int64GetDatum(result_tuples[call_cntr].target);
253  values[4] = Int64GetDatum(result_tuples[call_cntr].flow);
254  values[5] = Int64GetDatum(result_tuples[call_cntr].residual_capacity);
255  /**********************************************************************/
256 
257  tuple = heap_form_tuple(tuple_desc, values, nulls);
258  result = HeapTupleGetDatum(tuple);
259  SRF_RETURN_NEXT(funcctx, result);
260  } else {
261  SRF_RETURN_DONE(funcctx);
262  }
263 }
264 
max_flow_driver.h
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_maxflow)
combinations_input.h
time_msg.h
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
arrays_input.h
pgr_flow_t
Definition: pgr_flow_t.h:37
pgr_get_flow_edges
void pgr_get_flow_edges(char *sql, pgr_edge_t **edges, size_t *total_edges)
read edges for flow
Definition: edges_input.c:691
edge
Definition: trsp.h:41
debug_macro.h
process
static void process(char *edges_sql, char *combinations_sql, ArrayType *starts, ArrayType *ends, int algorithm, bool only_flow, pgr_flow_t **result_tuples, size_t *result_count)
Definition: max_flow.c:49
pgr_get_bigIntArray
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Enforces the input array to be NOT empty.
Definition: arrays_input.c:146
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
pgr_combination_t
Definition: pgr_combination_t.h:43
pgr_get_combinations
void pgr_get_combinations(char *combinations_sql, pgr_combination_t **combinations, size_t *total_combinations)
combinations_sql
Definition: combinations_input.c:147
_pgr_maxflow
PGDLLEXPORT Datum _pgr_maxflow(PG_FUNCTION_ARGS)
Definition: max_flow.c:161
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
edges_input.h
do_pgr_max_flow
void do_pgr_max_flow(pgr_edge_t *data_edges, size_t total_edges, pgr_combination_t *combinations, size_t total_combinations, int64_t *source_vertices, size_t size_source_verticesArr, int64_t *sink_vertices, size_t size_sink_verticesArr, int algorithm, bool only_flow, pgr_flow_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: max_flow_driver.cpp:43
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93