PGROUTING  2.6-dev
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
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2016 Andrea Nardelli
10 Mail: nrd.nardelli@gmail.com
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 
31 #include "utils/array.h"
32 
33 
34 #include "c_common/debug_macro.h"
35 #include "c_common/e_report.h"
36 #include "c_common/time_msg.h"
37 #include "c_common/edges_input.h"
38 #include "c_common/arrays_input.h"
40 
41 PGDLLEXPORT Datum
42 max_flow_many_to_many(PG_FUNCTION_ARGS);
43 
44 static
45 void
47  char *edges_sql,
48  ArrayType *starts,
49  ArrayType *ends,
50  int algorithm,
51  bool only_flow,
52  pgr_flow_t **result_tuples,
53  size_t *result_count) {
54  if (algorithm < 1 || algorithm > 3) {
55  elog(ERROR, "Unknown algorithm");
56  }
57 
59 
60  size_t size_source_verticesArr = 0;
61  int64_t* source_vertices =
62  pgr_get_bigIntArray(&size_source_verticesArr, starts);
63 
64  size_t size_sink_verticesArr = 0;
65  int64_t* sink_vertices =
66  pgr_get_bigIntArray(&size_sink_verticesArr, ends);
67 
68  pgr_edge_t *edges = NULL;
69 
70  size_t total_edges = 0;
71 
72  /* NOTE:
73  * For flow, cost and reverse_cost are really capacity and reverse_capacity
74  */
75  pgr_get_flow_edges(edges_sql, &edges, &total_edges);
76 
77  if (total_edges == 0) {
78  if (source_vertices) pfree(source_vertices);
79  if (sink_vertices) pfree(sink_vertices);
81  return;
82  }
83 
84 
85  PGR_DBG("Starting timer");
86  clock_t start_t = clock();
87  char* log_msg = NULL;
88  char* notice_msg = NULL;
89  char *err_msg = NULL;
90 
92  edges, total_edges,
93  source_vertices, size_source_verticesArr,
94  sink_vertices, size_sink_verticesArr,
95  algorithm,
96  only_flow,
97 
98  result_tuples, result_count,
99 
100  &log_msg,
101  &notice_msg,
102  &err_msg);
103 
104  if (only_flow) {
105  time_msg("pgr_maxFlow(many to many)",
106  start_t, clock());
107  } else if (algorithm == 1) {
108  time_msg("pgr_maxFlowPushRelabel(many to many)",
109  start_t, clock());
110  } else if (algorithm == 3) {
111  time_msg("pgr_maxFlowEdmondsKarp(many to many)",
112  start_t, clock());
113  } else {
114  time_msg("pgr_maxFlowBoykovKolmogorov(many to many)",
115  start_t, clock());
116  }
117 
118 
119  if (edges) pfree(edges);
120  if (source_vertices) pfree(source_vertices);
121  if (sink_vertices) pfree(sink_vertices);
122 
123  if (err_msg && (*result_tuples)) {
124  pfree(*result_tuples);
125  (*result_tuples) = NULL;
126  (*result_count) = 0;
127  }
128 
129  pgr_global_report(log_msg, notice_msg, err_msg);
130 
131  if (log_msg) pfree(log_msg);
132  if (notice_msg) pfree(notice_msg);
133  if (err_msg) pfree(err_msg);
134 
135  pgr_SPI_finish();
136 }
137 
138 
140 PGDLLEXPORT Datum
141 max_flow_many_to_many(PG_FUNCTION_ARGS) {
142  FuncCallContext *funcctx;
143  TupleDesc tuple_desc;
144 
145  /**************************************************************************/
146  pgr_flow_t *result_tuples = 0;
147  size_t result_count = 0;
148  /**************************************************************************/
149 
150  if (SRF_IS_FIRSTCALL()) {
151  MemoryContext oldcontext;
152  funcctx = SRF_FIRSTCALL_INIT();
153  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
154 
155 
156  /**********************************************************************/
157 
158  process(
159  text_to_cstring(PG_GETARG_TEXT_P(0)),
160  PG_GETARG_ARRAYTYPE_P(1),
161  PG_GETARG_ARRAYTYPE_P(2),
162  PG_GETARG_INT32(3),
163  PG_GETARG_BOOL(4),
164  &result_tuples,
165  &result_count);
166 
167  /* */
168  /**********************************************************************/
169 
170 #if PGSQL_VERSION > 95
171  funcctx->max_calls = result_count;
172 #else
173  funcctx->max_calls = (uint32_t)result_count;
174 #endif
175  funcctx->user_fctx = result_tuples;
176  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
177  != TYPEFUNC_COMPOSITE) {
178  ereport(ERROR,
179  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
180  errmsg("function returning record called in context "
181  "that cannot accept type record")));
182  }
183 
184  funcctx->tuple_desc = tuple_desc;
185  MemoryContextSwitchTo(oldcontext);
186  }
187 
188  funcctx = SRF_PERCALL_SETUP();
189  tuple_desc = funcctx->tuple_desc;
190  result_tuples = (pgr_flow_t *) funcctx->user_fctx;
191 
192  if (funcctx->call_cntr < funcctx->max_calls) {
193  HeapTuple tuple;
194  Datum result;
195  Datum *values;
196  bool *nulls;
197  size_t call_cntr = funcctx->call_cntr;
198 
199  /**********************************************************************/
200  /* MODIFY AS NEEDED */
201  values = palloc(6 * sizeof(Datum));
202  nulls = palloc(6 * sizeof(bool));
203 
204  size_t i;
205  for (i = 0; i < 6; ++i) {
206  nulls[i] = false;
207  }
208 
209  values[0] = Int32GetDatum(call_cntr + 1);
210  values[1] = Int64GetDatum(result_tuples[call_cntr].edge);
211  values[2] = Int64GetDatum(result_tuples[call_cntr].source);
212  values[3] = Int64GetDatum(result_tuples[call_cntr].target);
213  values[4] = Int64GetDatum(result_tuples[call_cntr].flow);
214  values[5] = Int64GetDatum(result_tuples[call_cntr].residual_capacity);
215  /**********************************************************************/
216 
217  tuple = heap_form_tuple(tuple_desc, values, nulls);
218  result = HeapTupleGetDatum(tuple);
219  SRF_RETURN_NEXT(funcctx, result);
220  } else {
221  SRF_RETURN_DONE(funcctx);
222  }
223 }
224 
Definition: trsp.h:31
#define PGR_DBG(...)
Definition: debug_macro.h:34
PG_FUNCTION_INFO_V1(max_flow_many_to_many)
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
enforces the input array to be NOT empty
Definition: arrays_input.c:124
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
void do_pgr_max_flow(pgr_edge_t *data_edges, size_t total_edges, 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)
void pgr_get_flow_edges(char *sql, pgr_edge_t **edges, size_t *total_edges)
read edges for flow
Definition: edges_input.c:535
if(DOXYGEN_FOUND) configure_file($
Definition: CMakeLists.txt:13
static void process(char *edges_sql, ArrayType *starts, ArrayType *ends, int algorithm, bool only_flow, pgr_flow_t **result_tuples, size_t *result_count)
Definition: max_flow.c:46
void pgr_SPI_connect(void)
PGDLLEXPORT Datum max_flow_many_to_many(PG_FUNCTION_ARGS)
Definition: max_flow.c:141