PGROUTING  2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
max_flow.c File Reference
#include "c_common/postgres_connection.h"
#include "utils/array.h"
#include "c_common/debug_macro.h"
#include "c_common/e_report.h"
#include "c_common/time_msg.h"
#include "c_common/edges_input.h"
#include "c_common/arrays_input.h"
#include "./max_flow_driver.h"
Include dependency graph for max_flow.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum max_flow_many_to_many (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (max_flow_many_to_many)
 
static void process (char *edges_sql, ArrayType *starts, ArrayType *ends, int algorithm, bool only_flow, pgr_flow_t **result_tuples, size_t *result_count)
 

Function Documentation

PGDLLEXPORT Datum max_flow_many_to_many ( PG_FUNCTION_ARGS  )

Definition at line 141 of file max_flow.c.

References process().

141  {
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 }
Definition: trsp.h:31
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

Here is the call graph for this function:

PG_FUNCTION_INFO_V1 ( max_flow_many_to_many  )
static void process ( char *  edges_sql,
ArrayType *  starts,
ArrayType *  ends,
int  algorithm,
bool  only_flow,
pgr_flow_t **  result_tuples,
size_t *  result_count 
)
static

Definition at line 46 of file max_flow.c.

References do_pgr_max_flow(), edges, PGR_DBG, pgr_get_bigIntArray(), pgr_get_flow_edges(), pgr_global_report(), pgr_SPI_connect(), pgr_SPI_finish(), and time_msg().

Referenced by max_flow_many_to_many().

53  {
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 }
static edge_t edges[22573]
#define PGR_DBG(...)
Definition: debug_macro.h:34
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:530
void pgr_SPI_connect(void)

Here is the call graph for this function:

Here is the caller graph for this function: