Skip to content

Commit 0c8fbbe

Browse files
committed
(maxFlow) using new process and driver
1 parent 4f1589d commit 0c8fbbe

4 files changed

Lines changed: 41 additions & 100 deletions

File tree

include/max_flow/maxflow.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
4444

4545
#include "c_types/flow_t.h"
4646
#include "cpp_common/edge_t.hpp"
47+
#include "c_common/enums.h"
4748
#include "c_types/path_rt.h"
4849
#include "cpp_common/interruption.hpp"
4950

@@ -107,10 +108,10 @@ class PgrFlowGraph {
107108
}
108109

109110
PgrFlowGraph(
110-
const std::vector<Edge_t> &edges,
111-
const std::set<int64_t> &source_vertices,
112-
const std::set<int64_t> &sink_vertices,
113-
int algorithm);
111+
const std::vector<Edge_t>&,
112+
const std::set<int64_t>&,
113+
const std::set<int64_t>&,
114+
Which);
114115

115116
PgrFlowGraph(
116117
const std::vector<Edge_t> &edges,

src/max_flow/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ ADD_LIBRARY(max_flow OBJECT
77
maximum_cardinality_matching.c
88
edge_disjoint_paths.c
99

10-
max_flow_driver.cpp
10+
maxFlow_driver.cpp
11+
maxFlow_process.cpp
12+
1113
maximum_cardinality_matching_driver.cpp
1214
edge_disjoint_paths_driver.cpp
1315
minCostMaxFlow_driver.cpp

src/max_flow/max_flow.c

Lines changed: 20 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -31,124 +31,56 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3131

3232
#include "c_common/postgres_connection.h"
3333

34-
#include "c_common/debug_macro.h"
35-
#include "c_common/e_report.h"
36-
#include "c_common/time_msg.h"
37-
#include "drivers/max_flow/max_flow_driver.h"
38-
3934
#include "c_types/flow_t.h"
4035

41-
PGDLLEXPORT Datum
42-
_pgr_maxflow(PG_FUNCTION_ARGS);
43-
44-
static
45-
void
46-
process(
47-
char *edges_sql,
48-
char *combinations_sql,
49-
ArrayType *starts,
50-
ArrayType *ends,
51-
int algorithm,
52-
bool only_flow,
53-
Flow_t **result_tuples,
54-
size_t *result_count) {
55-
if (algorithm < 1 || algorithm > 3) {
56-
elog(ERROR, "Unknown algorithm");
57-
}
58-
59-
pgr_SPI_connect();
60-
char* log_msg = NULL;
61-
char* notice_msg = NULL;
62-
char* err_msg = NULL;
63-
64-
clock_t start_t = clock();
65-
pgr_do_max_flow(
66-
edges_sql,
67-
combinations_sql,
68-
starts, ends,
69-
70-
algorithm,
71-
only_flow,
72-
73-
result_tuples, result_count,
74-
75-
&log_msg,
76-
&notice_msg,
77-
&err_msg);
78-
79-
if (only_flow) {
80-
time_msg("pgr_maxFlow(many to many)", start_t, clock());
81-
} else if (algorithm == 1) {
82-
time_msg("pgr_maxFlowPushRelabel(many to many)", start_t, clock());
83-
} else if (algorithm == 3) {
84-
time_msg("pgr_maxFlowEdmondsKarp(many to many)", start_t, clock());
85-
} else {
86-
time_msg("pgr_maxFlowBoykovKolmogorov(many to many)", start_t, clock());
87-
}
88-
89-
if (err_msg && (*result_tuples)) {
90-
pfree(*result_tuples);
91-
(*result_tuples) = NULL;
92-
(*result_count) = 0;
93-
}
36+
#include "process/maxFlow_process.h"
9437

95-
pgr_global_report(&log_msg, &notice_msg, &err_msg);
96-
97-
pgr_SPI_finish();
98-
}
38+
PGDLLEXPORT Datum _pgr_maxflow(PG_FUNCTION_ARGS);
39+
PG_FUNCTION_INFO_V1(_pgr_maxflow);
9940

10041

101-
PG_FUNCTION_INFO_V1(_pgr_maxflow);
102-
PGDLLEXPORT Datum
103-
_pgr_maxflow(PG_FUNCTION_ARGS) {
104-
FuncCallContext *funcctx;
105-
TupleDesc tuple_desc;
42+
PGDLLEXPORT Datum _pgr_maxflow(PG_FUNCTION_ARGS) {
43+
FuncCallContext *funcctx;
44+
TupleDesc tuple_desc;
10645

107-
/**************************************************************************/
108-
Flow_t *result_tuples = 0;
46+
Flow_t *result_tuples = NULL;
10947
size_t result_count = 0;
110-
/**************************************************************************/
11148

11249
if (SRF_IS_FIRSTCALL()) {
113-
MemoryContext oldcontext;
50+
MemoryContext oldcontext;
11451
funcctx = SRF_FIRSTCALL_INIT();
11552
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
11653

117-
118-
/**********************************************************************/
119-
12054
if (PG_NARGS() == 5) {
12155
/*
12256
* many to many
12357
*/
124-
process(
58+
pgr_process_maxFlow(
12559
text_to_cstring(PG_GETARG_TEXT_P(0)),
12660
NULL,
61+
12762
PG_GETARG_ARRAYTYPE_P(1),
12863
PG_GETARG_ARRAYTYPE_P(2),
129-
PG_GETARG_INT32(3),
130-
PG_GETARG_BOOL(4),
64+
PG_GETARG_BOOL(4)? MAXFLOW : MAXFLOW + PG_GETARG_INT32(3),
13165
&result_tuples,
13266
&result_count);
13367

13468
} else if (PG_NARGS() == 4) {
13569
/*
13670
* combinations
13771
*/
138-
process(
72+
pgr_process_maxFlow(
13973
text_to_cstring(PG_GETARG_TEXT_P(0)),
14074
text_to_cstring(PG_GETARG_TEXT_P(1)),
75+
14176
NULL,
14277
NULL,
143-
PG_GETARG_INT32(2),
144-
PG_GETARG_BOOL(3),
78+
79+
PG_GETARG_BOOL(3)? MAXFLOW : MAXFLOW + PG_GETARG_INT32(2),
14580
&result_tuples,
14681
&result_count);
14782
}
14883

149-
/* */
150-
/**********************************************************************/
151-
15284
funcctx->max_calls = result_count;
15385
funcctx->user_fctx = result_tuples;
15486
if (get_call_result_type(fcinfo, NULL, &tuple_desc)
@@ -168,14 +100,13 @@ _pgr_maxflow(PG_FUNCTION_ARGS) {
168100
result_tuples = (Flow_t *) funcctx->user_fctx;
169101

170102
if (funcctx->call_cntr < funcctx->max_calls) {
171-
HeapTuple tuple;
172-
Datum result;
173-
Datum *values;
174-
bool *nulls;
103+
HeapTuple tuple;
104+
Datum result;
105+
Datum *values;
106+
bool* nulls;
107+
175108
size_t call_cntr = funcctx->call_cntr;
176109

177-
/**********************************************************************/
178-
/* MODIFY AS NEEDED */
179110
values = palloc(6 * sizeof(Datum));
180111
nulls = palloc(6 * sizeof(bool));
181112

@@ -190,7 +121,6 @@ _pgr_maxflow(PG_FUNCTION_ARGS) {
190121
values[3] = Int64GetDatum(result_tuples[call_cntr].target);
191122
values[4] = Int64GetDatum(result_tuples[call_cntr].flow);
192123
values[5] = Int64GetDatum(result_tuples[call_cntr].residual_capacity);
193-
/**********************************************************************/
194124

195125
tuple = heap_form_tuple(tuple_desc, values, nulls);
196126
result = HeapTupleGetDatum(tuple);

src/max_flow/maxflow.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,25 @@ PgrFlowGraph::PgrFlowGraph(
4040
const std::vector<Edge_t> &edges,
4141
const std::set<int64_t> &source_vertices,
4242
const std::set<int64_t> &sink_vertices,
43-
int algorithm) {
43+
Which algorithm) {
4444
add_vertices(edges, source_vertices, sink_vertices);
4545

4646
capacity = get(boost::edge_capacity, graph);
4747
rev = get(boost::edge_reverse, graph);
4848
residual_capacity = get(boost::edge_residual_capacity, graph);
4949

50-
if (algorithm == 1) {
51-
insert_edges_push_relabel(edges);
52-
} else {
53-
insert_edges(edges);
50+
switch (algorithm) {
51+
case MAXFLOW:
52+
case PUSHRELABEL:
53+
insert_edges_push_relabel(edges);
54+
break;
55+
case BOYKOV:
56+
case EDMONDSKARP:
57+
insert_edges(edges);
58+
break;
59+
default:
60+
;
61+
/* Maybe do a throw */
5462
}
5563
}
5664

0 commit comments

Comments
 (0)