Skip to content

Commit

Permalink
Fix floating point exception (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
thilinarmtb authored Feb 28, 2024
1 parent 5709372 commit b8b6f44
Show file tree
Hide file tree
Showing 26 changed files with 393 additions and 815 deletions.
12 changes: 6 additions & 6 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortLambdasOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CC ?= mpicc
CFLAGS ?= -Wall -Wextra -Wpedantic -Wno-unused-function -Wno-unused-parameter -std=c99
CFLAGS ?= -Wall -Wextra -Wpedantic -Wno-unused-function -Wno-unused-parameter -std=c99 -g
LDFLAGS ?=
DEBUG ?= 0
MPI ?= 1
Expand Down
9 changes: 3 additions & 6 deletions examples/gencon.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ static void test_parcon(unsigned int neltp, long long *vlp, char *name,
struct gs_data *gsh = gs_setup(vls, size, &c, 0, gs_pairwise, 0);

uint i;
for (i = 0; i < size; i++)
minp[i] = maxp[i] = vlp[i];
for (i = 0; i < size; i++) minp[i] = maxp[i] = vlp[i];

buffer bfr;
buffer_init(&bfr, 1024);
Expand All @@ -40,8 +39,7 @@ static void test_parcon(unsigned int neltp, long long *vlp, char *name,

gsh = gs_setup(vlp, size, &c, 0, gs_pairwise, 0);

for (i = 0; i < size; i++)
minp[i] = maxp[i] = vls[i];
for (i = 0; i < size; i++) minp[i] = maxp[i] = vls[i];

gs(minp, gs_long, gs_min, 0, gsh, &bfr);
gs(maxp, gs_long, gs_max, 0, gsh, &bfr);
Expand Down Expand Up @@ -98,8 +96,7 @@ int main(int argc, char *argv[]) {
}

// Turns on testing if test is on
if (in->test)
test_parcon(nelt, vl, in->mesh, world);
if (in->test) test_parcon(nelt, vl, in->mesh, world);

// Free resources
free(vl), free(coord), free(bcs);
Expand Down
3 changes: 1 addition & 2 deletions examples/genmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ int main(int argc, char *argv[]) {
int rank, size;
MPI_Comm_rank(world, &rank);
MPI_Comm_size(world, &size);
if (in->nactive > size)
in->nactive = size;
if (in->nactive > size) in->nactive = size;

MPI_Comm comm;
MPI_Comm_split(world, rank < in->nactive, rank, &comm);
Expand Down
60 changes: 20 additions & 40 deletions src/components.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@ uint get_components(sint *component, struct array *elems, unsigned nv,
comm_scan(out, c, gs_long, gs_add, &in, 1, wrk);
ulong nelg = out[1][0], start = out[0][0];

if (nelg == 0)
return 0;
if (nelg == 0) return 0;

uint nev = nelt * nv;
slong *p = tcalloc(slong, nev);
slong *ids = tcalloc(slong, nev);

int null_input = (component == NULL);
if (null_input)
component = tcalloc(sint, nelt);
if (null_input) component = tcalloc(sint, nelt);

for (uint e = 0; e < nelt; e++)
component[e] = -1;
for (uint e = 0; e < nelt; e++) component[e] = -1;

struct unmarked {
uint index;
Expand Down Expand Up @@ -55,8 +52,7 @@ uint get_components(sint *component, struct array *elems, unsigned nv,
if (bin == 1) {
// Initialize p
for (uint e = 0; e < arr.n; e++)
for (uint d = 0; d < nv; d++)
p[e * nv + d] = 0;
for (uint d = 0; d < nv; d++) p[e * nv + d] = 0;

// Mark the first non-marked element as seed
struct unmarked *ptr = (struct unmarked *)arr.ptr;
Expand All @@ -65,8 +61,7 @@ uint get_components(sint *component, struct array *elems, unsigned nv,
comm_allreduce(&cc, gs_long, gs_min, &mfirst, 1, wrk);

if (mfirst == first) {
for (uint d = 0; d < nv; d++)
p[0 * nv + d] = 1;
for (uint d = 0; d < nv; d++) p[0 * nv + d] = 1;
}

// Setup gs
Expand All @@ -91,8 +86,7 @@ uint get_components(sint *component, struct array *elems, unsigned nv,
}
// There was one non-zero vertex in the element
if (d < nv) {
for (d = 0; d < nv; d++)
p[e * nv + d] = 1;
for (d = 0; d < nv; d++) p[e * nv + d] = 1;
}
}

Expand All @@ -110,8 +104,7 @@ uint get_components(sint *component, struct array *elems, unsigned nv,
array_free(&arr);

free(p), free(ids);
if (null_input == 1)
free(component);
if (null_input == 1) free(component);

return count;
}
Expand Down Expand Up @@ -148,8 +141,7 @@ static sint find_or_insert(struct array *cids, struct cmp_t *t) {
}

uint n = mid;
if (t->c > pc[mid].c)
n = mid + 1;
if (t->c > pc[mid].c) n = mid + 1;

struct cmp_t t0 = *t, t1;
for (; n < cids->n; n++) {
Expand All @@ -160,8 +152,7 @@ static sint find_or_insert(struct array *cids, struct cmp_t *t) {
pc[n] = t0, cids->n++;

// Sanity check.
for (unsigned i = 1; i < cids->n; i++)
assert(pc[i - 1].c < pc[i].c);
for (unsigned i = 1; i < cids->n; i++) assert(pc[i - 1].c < pc[i].c);

return -1;
}
Expand All @@ -175,8 +166,7 @@ uint get_components_v2(sint *component, struct array *elems, unsigned nv,
comm_scan(out, ci, gs_long, gs_add, &in, 1, wrk);
ulong nelg = out[1][0];

if (nelg == 0)
return 0;
if (nelg == 0) return 0;

const uint nev = nelt * nv;
sint *p0 = tcalloc(sint, nev);
Expand All @@ -185,11 +175,9 @@ uint get_components_v2(sint *component, struct array *elems, unsigned nv,
uint *inds = tcalloc(uint, nev);

int null_input = (component == NULL);
if (null_input)
component = tcalloc(sint, nelt);
if (null_input) component = tcalloc(sint, nelt);

for (uint e = 0; e < nelt; e++)
component[e] = -1;
for (uint e = 0; e < nelt; e++) component[e] = -1;

struct comm c;
ulong nmkd = 0;
Expand All @@ -200,8 +188,7 @@ uint get_components_v2(sint *component, struct array *elems, unsigned nv,
for (uint e = 0; e < nelt; e++) {
if (component[e] == -1) {
inds[unmkd] = e;
for (uint v = 0; v < nv; v++)
ids[unmkd * nv + v] = pe[e].vertices[v];
for (uint v = 0; v < nv; v++) ids[unmkd * nv + v] = pe[e].vertices[v];
unmkd++;
}
}
Expand All @@ -212,22 +199,19 @@ uint get_components_v2(sint *component, struct array *elems, unsigned nv,
slong nnzg = 0, ncg = 0;
if (bin == 1) {
// Mark the first unmarked element as seed for the component c.id.
for (uint v = 0; v < nv; v++)
p[0 * nv + v] = c.id;
for (uint v = 0; v < nv; v++) p[0 * nv + v] = c.id;

// Initialize the rest of p.
for (uint e = 1; e < unmkd; e++)
for (uint v = 0; v < nv; v++)
p[e * nv + v] = -1;
for (uint v = 0; v < nv; v++) p[e * nv + v] = -1;

// Setup gather-scatter to do BFS.
struct gs_data *gsh = gs_setup(ids, unmkd * nv, &c, 0, gs_pairwise, 0);

// Perform BFS.
sint changed;
do {
for (uint i = 0; i < unmkd * nv; i++)
p0[i] = p[i];
for (uint i = 0; i < unmkd * nv; i++) p0[i] = p[i];

gs(p, gs_int, gs_max, 0, gsh, bfr);

Expand All @@ -248,8 +232,7 @@ uint get_components_v2(sint *component, struct array *elems, unsigned nv,
// the element with that value.
if (v0 > -1) {
sint c = p[e * nv + v0];
for (uint v = 0; v < nv; v++)
p[e * nv + v] = c;
for (uint v = 0; v < nv; v++) p[e * nv + v] = c;
nnz++;
}

Expand Down Expand Up @@ -296,8 +279,7 @@ uint get_components_v2(sint *component, struct array *elems, unsigned nv,
cnt++;
struct cmp_t *pc = (struct cmp_t *)cids.ptr;
for (uint i = 1; i < cids.n; i++) {
if (pc[i].c > pc[i - 1].c)
cnt++;
if (pc[i].c > pc[i - 1].c) cnt++;
}
}

Expand All @@ -310,8 +292,7 @@ uint get_components_v2(sint *component, struct array *elems, unsigned nv,
struct cmp_t *pc = (struct cmp_t *)cids.ptr;
pc[0].uid = s;
for (uint i = 1; i < cids.n; i++) {
if (pc[i].c > pc[i - 1].c)
s++;
if (pc[i].c > pc[i - 1].c) s++;
pc[i].uid = s;
}
}
Expand Down Expand Up @@ -340,8 +321,7 @@ uint get_components_v2(sint *component, struct array *elems, unsigned nv,
nc += ncg;
} while (nmkd < nelg);

if (null_input == 1)
free(component);
if (null_input == 1) free(component);
free(p0), free(p), free(ids), free(inds);

return nc;
Expand Down
12 changes: 4 additions & 8 deletions src/con-check.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,15 @@ static VToEMap *getVToEMap(Mesh m, struct comm *c, buffer *bfr) {
crystal_free(&cr);

// create the map
if (a.n == 0)
return NULL;
if (a.n == 0) return NULL;

VToEMap *map = calloc(1, sizeof(VToEMap));
map->elements = calloc(a.n, sizeof(ulong));

uint nGIds = 1, prev = 0;
vertex *aPtr = (vertex *)a.ptr;
for (i = 1; i < a.n; i++) {
if (aPtr[i].vertexId != aPtr[prev].vertexId)
nGIds++;
if (aPtr[i].vertexId != aPtr[prev].vertexId) nGIds++;
prev = i;
}

Expand Down Expand Up @@ -185,8 +183,7 @@ static uint getPosition(VToEMap *map, ulong key) {
begin = mid;
};

if (globalIds[mid] != key)
return UINT_MAX;
if (globalIds[mid] != key) return UINT_MAX;
return mid;
}

Expand Down Expand Up @@ -275,8 +272,7 @@ int element_check(Mesh mesh, struct comm *c, buffer *bfr) {
uint i, j;
int err = 0;
for (i = 0; i < nelt && err == 0; i++) {
for (j = 0; j < nv; j++)
globalIds[j].id = ptr[i * nv + j].globalId + 1;
for (j = 0; j < nv; j++) globalIds[j].id = ptr[i * nv + j].globalId + 1;

sarray_sort(LongID, globalIds, nv, id, 1, bfr);

Expand Down
42 changes: 14 additions & 28 deletions src/con-periodic.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,15 @@ static int compressPeriodicVertices(Mesh mesh, struct comm *c, buffer *bfr) {
comm_scan(out, c, gs_long, gs_add, in, 1, buf);
slong start = out[0][0];

for (i = 0; i < npoints; i++)
points[i].globalId += start;
for (i = 0; i < npoints; i++) points[i].globalId += start;

return 0;
}

static ulong findMinBelowI(ulong min, uint I, struct array *arr) {
struct mpair_t *ptr = (struct mpair_t *)arr->ptr;
for (uint i = 0; i < I; i++)
if (ptr[i].orig == min)
return ptr[i].min;
if (ptr[i].orig == min) return ptr[i].min;
return min;
}

Expand All @@ -71,43 +69,34 @@ static int renumberPeriodicVertices(Mesh mesh, struct comm *c,
*mcur = tcalloc(slong, size1);

struct point_t *pe = (struct point_t *)mesh->elements.ptr;
for (uint i = 0; i < size1; i++)
mids[i] = pe[i].globalId;
for (uint i = 0; i < size1; i++) mids[i] = pe[i].globalId;
struct mpair_t *pm = (struct mpair_t *)matched->ptr;
for (uint i = 0; i < size2; i++)
mids[size1 + i] = pm[i].orig;
for (uint i = 0; i < size2; i++) mids[size1 + i] = pm[i].orig;
struct gs_data *gsh = gs_setup(mids, size1 + size2, c, 0, gs_pairwise, 0);

for (uint i = 0; i < size1; i++)
mnew[i] = pe[i].globalId;
for (uint i = 0; i < size2; i++)
mnew[size1 + i] = pm[i].min;
for (uint i = 0; i < size1; i++) mnew[i] = pe[i].globalId;
for (uint i = 0; i < size2; i++) mnew[size1 + i] = pm[i].min;
gs(mnew, gs_long, gs_min, 0, gsh, bfr);

sint changed, wrk;
do {
for (uint i = 0; i < size1; i++)
mcur[i] = mnew[i];
for (uint i = 0; i < size2; i++)
mids[size1 + size2 + i] = -mnew[size1 + i];
for (uint i = 0; i < size1; i++) mcur[i] = mnew[i];
for (uint i = 0; i < size2; i++) mids[size1 + size2 + i] = -mnew[size1 + i];
struct gs_data *gsh1 =
gs_setup(mids, size1 + 2 * size2, c, 0, gs_pairwise, 0);

gs(mnew, gs_long, gs_min, 0, gsh1, bfr);
gs_free(gsh1);

for (uint i = 0; i < size2; i++)
mnew[size1 + i] = mnew[size1 + size2 + i];
for (uint i = 0; i < size2; i++) mnew[size1 + i] = mnew[size1 + size2 + i];
gs(mnew, gs_long, gs_min, 0, gsh, bfr);

changed = 0;
for (uint i = 0; i < size1; i++)
changed += (mnew[i] != mcur[i]);
for (uint i = 0; i < size1; i++) changed += (mnew[i] != mcur[i]);
comm_allreduce(c, gs_int, gs_max, &changed, 1, &wrk);
} while (changed);

for (uint i = 0; i < size1; i++)
pe[i].globalId = mcur[i];
for (uint i = 0; i < size1; i++) pe[i].globalId = mcur[i];

gs_free(gsh);
free(mids), free(mnew), free(mcur);
Expand Down Expand Up @@ -229,13 +218,11 @@ static int gatherMatchingPeriodicFaces(Mesh mesh, struct comm *c) {
static int setPeriodicFaceCoordinates(Mesh mesh, struct comm *c, buffer *buf) {
BoundaryFace bPtr = mesh->boundary.ptr;
sint bSize = mesh->boundary.n;
if (bSize == 0)
return 0;
if (bSize == 0) return 0;

Point ePtr = mesh->elements.ptr;
sint eSize = mesh->elements.n;
if (eSize == 0)
return 0;
if (eSize == 0) return 0;

/* Need boundary array to be sorted by elementId */
sarray_sort(struct boundary_t, bPtr, bSize, elementId, 1, buf);
Expand All @@ -252,8 +239,7 @@ static int setPeriodicFaceCoordinates(Mesh mesh, struct comm *c, buffer *buf) {
sint i = 0, k = 0;
int nv = mesh->nv, nvf = mesh->nv / 2, j;
while (i < bSize) {
while (k < eSize && ePtr[k].elementId < bPtr[i].elementId)
k += nv;
while (k < eSize && ePtr[k].elementId < bPtr[i].elementId) k += nv;
// copy vertices to boundary face
if (k < eSize && ePtr[k].elementId == bPtr[i].elementId) {
int faceId = bPtr[i].faceId;
Expand Down
Loading

0 comments on commit b8b6f44

Please sign in to comment.