From b2a77e4fa53e1a2654cc1e0edd5d2c18595d8862 Mon Sep 17 00:00:00 2001 From: Thilina Rathnayake Date: Sat, 20 Mar 2021 12:24:57 -0500 Subject: [PATCH] Enable warnings and a small refactor (#35) --- src/genmap-components.c | 98 +++++++++++++++++++++++++++++++++++++- src/genmap-impl.h | 1 + src/genmap-rsb.c | 102 +--------------------------------------- tests/t230-flex-cg.c | 1 - tests/t235-project-pf.c | 1 - tests/t240-rqi.c | 1 - 6 files changed, 100 insertions(+), 104 deletions(-) diff --git a/src/genmap-components.c b/src/genmap-components.c index d418c157..7b40e1f4 100644 --- a/src/genmap-components.c +++ b/src/genmap-components.c @@ -4,7 +4,7 @@ #include #include -#include +#include struct unmarked { uint index; @@ -130,3 +130,99 @@ sint get_components(sint *component, struct rsb_element *elements, return count; } + +void split_and_repair_partitions(genmap_handle h, struct comm *lc, int level) { + sint np = lc->np; + int bin = 1; + if (lc->id < (np + 1) / 2) + bin = 0; + struct comm tc; + genmap_comm_split(lc, bin, lc->id, &tc); + + struct rsb_element *e = genmap_get_elements(h); + uint nelt = genmap_get_nel(h); + int nv = genmap_get_nvertices(h); + + /* Check for disconnected components */ + GenmapInitLaplacianWeighted(h, &tc); + + sint *comp_ids = NULL; + GenmapMalloc(nelt, &comp_ids); + sint ncomp_global, ncomp, buf; + ncomp_global = ncomp = get_components(comp_ids, e, &tc, &h->buf, nelt, nv); + comm_allreduce(lc, gs_int, gs_max, &ncomp_global, 1, &buf); + + while (ncomp_global > 1) { + if (lc->id == 0) { + printf("\tWarning: There are %d disconnected components in level = %d!\n", + ncomp_global, level); + fflush(stdout); + } + + sint *comp_count = NULL; + GenmapCalloc(2 * ncomp_global, &comp_count); + uint i; + for (i = 0; i < nelt; i++) + comp_count[comp_ids[i]]++; + comm_allreduce(&tc, gs_int, gs_add, comp_count, ncomp_global, + &comp_count[ncomp_global]); + + sint min_count = INT_MAX, min_id = -1; + for (i = 0; i < ncomp; i++) { + if (comp_count[i] < min_count) { + min_count = comp_count[i]; + min_id = i; + } + } + sint min_count_global = min_count; + comm_allreduce(lc, gs_int, gs_min, &min_count_global, 1, &buf); + sint id_global = (min_count_global == min_count) ? lc->id : lc->np; + comm_allreduce(lc, gs_int, gs_min, &id_global, 1, &buf); + + struct crystal cr; + crystal_init(&cr, lc); + + for (i = 0; i < nelt; i++) + e[i].proc = lc->id; + + sint low_np = (np + 1) / 2; + sint high_np = np - low_np; + sint start = !bin * low_np; + sint P = bin * low_np + !bin * high_np; + sint size = (min_count_global + P - 1) / P; + + sint current = 0; + if (min_count_global == min_count && lc->id == id_global) { + for (i = 0; i < nelt; i++) { + if (comp_ids[i] == min_id) { + e[i].proc = start + current / size; + current++; + } + } + assert(min_count == current && "min_count != current"); + } + + sarray_transfer(struct rsb_element, h->elements, proc, 1, &cr); + crystal_free(&cr); + + // do a load balanced sort in each partition + parallel_sort(struct rsb_element, h->elements, fiedler, gs_double, 0, 1, + &tc, &h->buf); + + genmap_comm_scan(h, &tc); + + e = genmap_get_elements(h); + nelt = genmap_get_nel(h); + GenmapRealloc(nelt, &comp_ids); + GenmapInitLaplacianWeighted(h, &tc); + ncomp_global = ncomp = get_components(comp_ids, e, &tc, &h->buf, nelt, nv); + comm_allreduce(lc, gs_int, gs_max, &ncomp_global, 1, &buf); + + GenmapFree(comp_count); + } + + GenmapFree(comp_ids); + comm_free(lc); + comm_dup(lc, &tc); + comm_free(&tc); +} diff --git a/src/genmap-impl.h b/src/genmap-impl.h index cf8df62c..2c8c0c66 100644 --- a/src/genmap-impl.h +++ b/src/genmap-impl.h @@ -139,6 +139,7 @@ typedef struct { sint get_components(sint *component, struct rsb_element *elements, struct comm *c, buffer *buf, uint nelt, uint nv); +void split_and_repair_partitions(genmap_handle h, struct comm *lc, int level); /* Matrix inverse */ void matrix_inverse(int N, double *A); diff --git a/src/genmap-rsb.c b/src/genmap-rsb.c index ecf03f62..817d32e7 100644 --- a/src/genmap-rsb.c +++ b/src/genmap-rsb.c @@ -4,7 +4,6 @@ #include #include -#include #include static int dump_fiedler_if_discon(genmap_handle h, int level, int max_levels) { @@ -48,102 +47,6 @@ static int dump_fiedler_if_discon(genmap_handle h, int level, int max_levels) { } } -static void split_and_repair_partitions(genmap_handle h, struct comm *lc, - int level) { - sint np = lc->np; - int bin = 1; - if (lc->id < (np + 1) / 2) - bin = 0; - struct comm tc; - genmap_comm_split(lc, bin, lc->id, &tc); - - struct rsb_element *e = genmap_get_elements(h); - uint nelt = genmap_get_nel(h); - int nv = genmap_get_nvertices(h); - - /* Check for disconnected components */ - sint *comp_ids = NULL; - GenmapMalloc(nelt, &comp_ids); - GenmapInitLaplacianWeighted(h, &tc); - sint ncomp_global, ncomp, buf; - ncomp_global = ncomp = get_components(comp_ids, e, &tc, &h->buf, nelt, nv); - comm_allreduce(lc, gs_int, gs_max, &ncomp_global, 1, &buf); - - while (ncomp_global > 1) { - if (lc->id == 0) { - printf("\tWarning: There are %d disconnected components in level = %d!\n", - ncomp_global, level); - fflush(stdout); - } - - sint *comp_count = NULL; - GenmapCalloc(2 * ncomp_global, &comp_count); - uint i; - for (i = 0; i < nelt; i++) - comp_count[comp_ids[i]]++; - comm_allreduce(&tc, gs_int, gs_add, comp_count, ncomp_global, - &comp_count[ncomp_global]); - - sint min_count = INT_MAX, min_id = -1; - for (i = 0; i < ncomp; i++) { - if (comp_count[i] < min_count) { - min_count = comp_count[i]; - min_id = i; - } - } - sint min_count_global = min_count; - comm_allreduce(lc, gs_int, gs_min, &min_count_global, 1, &buf); - sint id_global = (min_count_global == min_count) ? lc->id : lc->np; - comm_allreduce(lc, gs_int, gs_min, &id_global, 1, &buf); - - struct crystal cr; - crystal_init(&cr, lc); - - for (i = 0; i < nelt; i++) - e[i].proc = lc->id; - - sint low_np = (np + 1) / 2; - sint high_np = np - low_np; - sint start = !bin * low_np; - sint P = bin * low_np + !bin * high_np; - sint size = (min_count_global + P - 1) / P; - - sint current = 0; - if (min_count_global == min_count && lc->id == id_global) { - for (i = 0; i < nelt; i++) { - if (comp_ids[i] == min_id) { - e[i].proc = start + current / size; - current++; - } - } - assert(min_count == current && "min_count != current"); - } - - sarray_transfer(struct rsb_element, h->elements, proc, 1, &cr); - crystal_free(&cr); - - // do a load balanced sort in each partition - parallel_sort(struct rsb_element, h->elements, fiedler, gs_double, 0, 1, - &tc, &h->buf); - - genmap_comm_scan(h, &tc); - - e = genmap_get_elements(h); - nelt = genmap_get_nel(h); - GenmapRealloc(nelt, &comp_ids); - GenmapInitLaplacianWeighted(h, &tc); - ncomp_global = ncomp = get_components(comp_ids, e, &tc, &h->buf, nelt, nv); - comm_allreduce(lc, gs_int, gs_max, &ncomp_global, 1, &buf); - - GenmapFree(comp_count); - } - - GenmapFree(comp_ids); - comm_free(lc); - comm_dup(lc, &tc); - comm_free(&tc); -} - int genmap_rsb(genmap_handle h) { int verbose = h->options->debug_level > 1; int max_iter = 50; @@ -221,7 +124,6 @@ int genmap_rsb(genmap_handle h) { level++; } -#if 0 /* Check if Fidler converged */ sint converged = 1; for (i = 0; i < metric_get_levels(); i++) { @@ -234,8 +136,8 @@ int genmap_rsb(genmap_handle h) { } comm_allreduce(gc, gs_int, gs_min, &converged, 1, bfr); // min if (converged == 0 && gc->id == 0) - printf("\tWARNING: Lanczos failed to converge while partitioning, Level=%d!\n", level); -#endif + printf("\tWARNING: Failed to converge while partitioning, Level=%d!\n", + level); return 0; } diff --git a/tests/t230-flex-cg.c b/tests/t230-flex-cg.c index 57ce2e91..85ed2663 100644 --- a/tests/t230-flex-cg.c +++ b/tests/t230-flex-cg.c @@ -5,7 +5,6 @@ #include #include #include -#include int main(int argc, char *argv[]) { MPI_Init(&argc, &argv); diff --git a/tests/t235-project-pf.c b/tests/t235-project-pf.c index 20a02ffc..f9de1edb 100644 --- a/tests/t235-project-pf.c +++ b/tests/t235-project-pf.c @@ -5,7 +5,6 @@ #include #include #include -#include int main(int argc, char *argv[]) { MPI_Init(&argc, &argv); diff --git a/tests/t240-rqi.c b/tests/t240-rqi.c index faaf3d4d..0d067dd0 100644 --- a/tests/t240-rqi.c +++ b/tests/t240-rqi.c @@ -5,7 +5,6 @@ #include #include #include -#include int main(int argc, char *argv[]) { MPI_Init(&argc, &argv);