Commit a2eaae42 authored by David Gibson's avatar David Gibson

aga,agar: Non-equal edge costs for parallel test graph

At the moment the "parallel" test graph just uses the default cost of 1
for all the links between the two nodes.  This patch changes that so that
the links have cost 2, except (optionally) one with cost 1.  This provides
a useful test for the Dijkstra's algorithm implementation to ensure that
it picks the correct link for the shortest path.
Signed-off-by: default avatarDavid Gibson <david@gibson.dropbear.id.au>
parent 1e742b68
...@@ -67,7 +67,7 @@ int main(void) ...@@ -67,7 +67,7 @@ int main(void)
trivial_graph_init(&tg); trivial_graph_init(&tg);
test_adjacency("trivial", &tg.sg, trivial_adjacency); test_adjacency("trivial", &tg.sg, trivial_adjacency);
parallel_graph_init(&pg, 3); parallel_graph_init(&pg, 3, 0);
test_adjacency("parallel nlinks 3", &pg.sg, test_adjacency("parallel nlinks 3", &pg.sg,
parallel_adjacency_nlinks3); parallel_adjacency_nlinks3);
......
...@@ -49,7 +49,7 @@ int main(void) ...@@ -49,7 +49,7 @@ int main(void)
trivial_graph_init(&tg); trivial_graph_init(&tg);
test_bfs(&tg.sg, 1, 1); test_bfs(&tg.sg, 1, 1);
parallel_graph_init(&pg, 3); parallel_graph_init(&pg, 3, 0);
test_bfs(&pg.sg, 1, 1, 2); test_bfs(&pg.sg, 1, 1, 2);
full_graph_init(&fg, 5); full_graph_init(&fg, 5);
......
...@@ -49,7 +49,7 @@ int main(void) ...@@ -49,7 +49,7 @@ int main(void)
trivial_graph_init(&tg); trivial_graph_init(&tg);
test_dfs(&tg.sg, 1, 1); test_dfs(&tg.sg, 1, 1);
parallel_graph_init(&pg, 3); parallel_graph_init(&pg, 3, 0);
test_dfs(&pg.sg, 1, 1, 2); test_dfs(&pg.sg, 1, 1, 2);
full_graph_init(&fg, 5); full_graph_init(&fg, 5);
......
...@@ -35,8 +35,9 @@ static void test_parallel(void) ...@@ -35,8 +35,9 @@ static void test_parallel(void)
struct parallel_graph pg; struct parallel_graph pg;
aga_icost_t cost; aga_icost_t cost;
struct aga_node *node; struct aga_node *node;
const void *edge;
parallel_graph_init(&pg, 3); parallel_graph_init(&pg, 3, 0);
ok1(aga_dijkstra_start(&pg.sg.g, &pg.sg.nodes[1]) == 0); ok1(aga_dijkstra_start(&pg.sg.g, &pg.sg.nodes[1]) == 0);
ok1(aga_dijkstra_step(&pg.sg.g) == &pg.sg.nodes[1]); ok1(aga_dijkstra_step(&pg.sg.g) == &pg.sg.nodes[1]);
...@@ -45,7 +46,7 @@ static void test_parallel(void) ...@@ -45,7 +46,7 @@ static void test_parallel(void)
ok1(aga_dijkstra_path(&pg.sg.g, &pg.sg.nodes[1], &cost, NULL, NULL)); ok1(aga_dijkstra_path(&pg.sg.g, &pg.sg.nodes[1], &cost, NULL, NULL));
ok1(cost == 0); ok1(cost == 0);
ok1(aga_dijkstra_path(&pg.sg.g, &pg.sg.nodes[2], &cost, &node, NULL)); ok1(aga_dijkstra_path(&pg.sg.g, &pg.sg.nodes[2], &cost, &node, NULL));
ok1(cost == 1); ok1(cost == 2);
ok1(node == &pg.sg.nodes[1]); ok1(node == &pg.sg.nodes[1]);
aga_finish(&pg.sg.g); aga_finish(&pg.sg.g);
...@@ -56,6 +57,15 @@ static void test_parallel(void) ...@@ -56,6 +57,15 @@ static void test_parallel(void)
ok1(cost == 0); ok1(cost == 0);
ok1(!aga_dijkstra_path(&pg.sg.g, &pg.sg.nodes[1], NULL, NULL, NULL)); ok1(!aga_dijkstra_path(&pg.sg.g, &pg.sg.nodes[1], NULL, NULL, NULL));
aga_finish(&pg.sg.g); aga_finish(&pg.sg.g);
parallel_graph_init(&pg, 3, 2);
ok1(aga_dijkstra_start(&pg.sg.g, &pg.sg.nodes[1]) == 0);
ok1(aga_dijkstra_path(&pg.sg.g, &pg.sg.nodes[2], &cost, &node, &edge));
ok1(cost == 1);
ok1(node == &pg.sg.nodes[1]);
ok1(ptr2int(edge) == 2);
aga_finish(&pg.sg.g);
} }
#define FULL_LEN 4 #define FULL_LEN 4
...@@ -206,7 +216,7 @@ static void test_traversal1(void) ...@@ -206,7 +216,7 @@ static void test_traversal1(void)
int main(void) int main(void)
{ {
plan_tests(7 + 15 plan_tests(7 + 20
+ FULL_LEN * (1 + FULL_LEN*4) + FULL_LEN * (1 + FULL_LEN*4)
+ CHAIN_LEN * (1 + CHAIN_LEN*2) + CHAIN_LEN * (1 + CHAIN_LEN*2)
+ 12 + 32); + 12 + 32);
......
...@@ -50,12 +50,17 @@ static int parallel_edge_info(const struct aga_graph *g, const struct aga_node * ...@@ -50,12 +50,17 @@ static int parallel_edge_info(const struct aga_graph *g, const struct aga_node *
assert(n == &pg->sg.nodes[1]); assert(n == &pg->sg.nodes[1]);
ei->to = &pg->sg.nodes[2]; ei->to = &pg->sg.nodes[2];
if (ptr2int(edge) == pg->cheaplink)
ei->icost = 1;
else
ei->icost = 2;
return 0; return 0;
} }
void parallel_graph_init(struct parallel_graph *pg, int nlinks) void parallel_graph_init(struct parallel_graph *pg, int nlinks, int cheaplink)
{ {
pg->nlinks = nlinks; pg->nlinks = nlinks;
pg->cheaplink = cheaplink;
simple_graph_init(&pg->sg, parallel_first_edge, parallel_next_edge, simple_graph_init(&pg->sg, parallel_first_edge, parallel_next_edge,
parallel_edge_info); parallel_edge_info);
......
...@@ -52,9 +52,10 @@ static const struct adjacency_list trivial_adjacency[] = { ...@@ -52,9 +52,10 @@ static const struct adjacency_list trivial_adjacency[] = {
*/ */
struct parallel_graph { struct parallel_graph {
int nlinks; int nlinks;
int cheaplink;
struct simple_graph sg; struct simple_graph sg;
}; };
void parallel_graph_init(struct parallel_graph *pg, int nlinks); void parallel_graph_init(struct parallel_graph *pg, int nlinks, int cheaplink);
static const struct adjacency_list parallel_adjacency_nlinks3[] = { static const struct adjacency_list parallel_adjacency_nlinks3[] = {
{1, {2, 2, 2}}, {1, {2, 2, 2}},
{2, {}}, {2, {}},
......
...@@ -61,7 +61,7 @@ int main(void) ...@@ -61,7 +61,7 @@ int main(void)
trivial_graphr_init(&tgr); trivial_graphr_init(&tgr);
test_adjacency("trivial", &tgr.gr, trivial_adjacencyr); test_adjacency("trivial", &tgr.gr, trivial_adjacencyr);
parallel_graphr_init(&pgr, 3); parallel_graphr_init(&pgr, 3, 0);
test_adjacency("parallel nlinks 3", &pgr.gr, test_adjacency("parallel nlinks 3", &pgr.gr,
parallel_adjacencyr_nlinks3); parallel_adjacencyr_nlinks3);
......
...@@ -53,7 +53,7 @@ int main(void) ...@@ -53,7 +53,7 @@ int main(void)
trivial_graphr_init(&tgr); trivial_graphr_init(&tgr);
test_bfs(&tgr.gr, 1, 1); test_bfs(&tgr.gr, 1, 1);
parallel_graphr_init(&pgr, 3); parallel_graphr_init(&pgr, 3, 0);
test_bfs(&pgr.gr, 1, 1, 2); test_bfs(&pgr.gr, 1, 1, 2);
full_graphr_init(&fgr, 5); full_graphr_init(&fgr, 5);
......
...@@ -53,7 +53,7 @@ int main(void) ...@@ -53,7 +53,7 @@ int main(void)
trivial_graphr_init(&tgr); trivial_graphr_init(&tgr);
test_dfs(&tgr.gr, 1, 1); test_dfs(&tgr.gr, 1, 1);
parallel_graphr_init(&pgr, 3); parallel_graphr_init(&pgr, 3, 0);
test_dfs(&pgr.gr, 1, 1, 2); test_dfs(&pgr.gr, 1, 1, 2);
full_graphr_init(&fgr, 5); full_graphr_init(&fgr, 5);
......
...@@ -35,9 +35,9 @@ static void test_parallel(void) ...@@ -35,9 +35,9 @@ static void test_parallel(void)
struct parallel_graphr pgr; struct parallel_graphr pgr;
struct agar_state *sr; struct agar_state *sr;
aga_icost_t cost; aga_icost_t cost;
const void *node; const void *node, *edge;
parallel_graphr_init(&pgr, 3); parallel_graphr_init(&pgr, 3, 0);
ok1(sr = agar_dijkstra_new(NULL, &pgr.gr, int2ptr(1))); ok1(sr = agar_dijkstra_new(NULL, &pgr.gr, int2ptr(1)));
ok1(agar_dijkstra_step(sr, &node)); ok1(agar_dijkstra_step(sr, &node));
...@@ -48,7 +48,7 @@ static void test_parallel(void) ...@@ -48,7 +48,7 @@ static void test_parallel(void)
ok1(agar_dijkstra_path(sr, int2ptr(1), &cost, NULL, NULL)); ok1(agar_dijkstra_path(sr, int2ptr(1), &cost, NULL, NULL));
ok1(cost == 0); ok1(cost == 0);
ok1(agar_dijkstra_path(sr, int2ptr(2), &cost, &node, NULL)); ok1(agar_dijkstra_path(sr, int2ptr(2), &cost, &node, NULL));
ok1(cost == 1); ok1(cost == 2);
ok1(node == int2ptr(1)); ok1(node == int2ptr(1));
tal_free(sr); tal_free(sr);
...@@ -60,6 +60,14 @@ static void test_parallel(void) ...@@ -60,6 +60,14 @@ static void test_parallel(void)
ok1(cost == 0); ok1(cost == 0);
ok1(!agar_dijkstra_path(sr, int2ptr(1), NULL, NULL, NULL)); ok1(!agar_dijkstra_path(sr, int2ptr(1), NULL, NULL, NULL));
tal_free(sr); tal_free(sr);
parallel_graphr_init(&pgr, 3, 2);
ok1(sr = agar_dijkstra_new(NULL, &pgr.gr, int2ptr(1)));
ok1(agar_dijkstra_path(sr, int2ptr(2), &cost, &node, &edge));
ok1(cost == 1);
ok1(ptr2int(node) == 1);
ok1(ptr2int(edge) == 2);
tal_free(sr);
} }
#define FULL_LEN 4 #define FULL_LEN 4
...@@ -213,7 +221,7 @@ static void test_traversal1(void) ...@@ -213,7 +221,7 @@ static void test_traversal1(void)
int main(void) int main(void)
{ {
plan_tests(6 + 18 plan_tests(6 + 23
+ FULL_LEN * (FULL_LEN*4 - 1) + FULL_LEN * (FULL_LEN*4 - 1)
+ CHAIN_LEN * (1 + CHAIN_LEN*2) + CHAIN_LEN * (1 + CHAIN_LEN*2)
+ 12 + 32); + 12 + 32);
......
...@@ -47,15 +47,23 @@ static int parallel_edge_info_r(const struct agar_graph *gr, ...@@ -47,15 +47,23 @@ static int parallel_edge_info_r(const struct agar_graph *gr,
const void *nr, const void *edge, const void *nr, const void *edge,
struct agar_edge_info *eir) struct agar_edge_info *eir)
{ {
const struct parallel_graphr *pgr
= container_of(gr, struct parallel_graphr, gr);
assert(ptr2int(nr) == 1); assert(ptr2int(nr) == 1);
eir->to = int2ptr(2); eir->to = int2ptr(2);
if (ptr2int(edge) == pgr->cheaplink)
eir->icost = 1;
else
eir->icost = 2;
return 0; return 0;
} }
void parallel_graphr_init(struct parallel_graphr *pgr, int nlinks) void parallel_graphr_init(struct parallel_graphr *pgr, int nlinks,
int cheaplink)
{ {
pgr->nlinks = nlinks; pgr->nlinks = nlinks;
pgr->cheaplink = cheaplink;
agar_init_graph(&pgr->gr, parallel_first_edge_r, parallel_next_edge_r, agar_init_graph(&pgr->gr, parallel_first_edge_r, parallel_next_edge_r,
parallel_edge_info_r); parallel_edge_info_r);
......
...@@ -37,9 +37,11 @@ static const struct adjacency_listr trivial_adjacencyr[] = { ...@@ -37,9 +37,11 @@ static const struct adjacency_listr trivial_adjacencyr[] = {
*/ */
struct parallel_graphr { struct parallel_graphr {
int nlinks; int nlinks;
int cheaplink;
struct agar_graph gr; struct agar_graph gr;
}; };
void parallel_graphr_init(struct parallel_graphr *pgr, int nlinks); void parallel_graphr_init(struct parallel_graphr *pgr, int nlinks,
int cheaplink);
static const struct adjacency_listr parallel_adjacencyr_nlinks3[] = { static const struct adjacency_listr parallel_adjacencyr_nlinks3[] = {
{1, {2, 2, 2}}, {1, {2, 2, 2}},
{2, {}}, {2, {}},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment