Commit 3e0e0c69 authored by Peter W. Draper's avatar Peter W. Draper
Browse files

Merge branch 'local_variable_cleanup' into 'master'

Local variable cleanup

First batch of formatting fixes, submitting these before things get massively out of sync again.

I've tried to apply the following rules throughout the code:

 * Local variables should be declared within the scope in which they are used.
 * Read-only variables should be declared `const`.
 * Local variables used in only a limit range of the scope should get their own scope.

I've also tried being a bit more explicit in variable and function naming.

See merge request !113
parents 1f9e985c 1b316fc5
......@@ -60,11 +60,12 @@ int cell_next_tag = 0;
int cell_getsize(struct cell *c) {
int k, count = 1;
/* Number of cells in this subtree. */
int count = 1;
/* Sum up the progeny if split. */
if (c->split)
for (k = 0; k < 8; k++)
for (int k = 0; k < 8; k++)
if (c->progeny[k] != NULL) count += cell_getsize(c->progeny[k]);
/* Return the final count. */
......@@ -83,9 +84,6 @@ int cell_getsize(struct cell *c) {
int cell_unpack(struct pcell *pc, struct cell *c, struct space *s) {
int k, count = 1;
struct cell *temp;
/* Unpack the current pcell. */
c->h_max = pc->h_max;
c->ti_end_min = pc->ti_end_min;
......@@ -94,9 +92,10 @@ int cell_unpack(struct pcell *pc, struct cell *c, struct space *s) {
c->tag = pc->tag;
/* Fill the progeny recursively, depth-first. */
for (k = 0; k < 8; k++)
int count = 1;
for (int k = 0; k < 8; k++)
if (pc->progeny[k] >= 0) {
temp = space_getcell(s);
struct cell *temp = space_getcell(s);
temp->count = 0;
temp->loc[0] = c->loc[0];
temp->loc[1] = c->loc[1];
......@@ -133,14 +132,16 @@ int cell_unpack(struct pcell *pc, struct cell *c, struct space *s) {
int cell_link(struct cell *c, struct part *parts) {
int k, ind = 0;
c->parts = parts;
/* Fill the progeny recursively, depth-first. */
if (c->split)
for (k = 0; k < 8; k++)
if (c->progeny[k] != NULL) ind += cell_link(c->progeny[k], &parts[ind]);
if (c->split) {
int offset = 0;
for (int k = 0; k < 8; k++) {
if (c->progeny[k] != NULL)
offset += cell_link(c->progeny[k], &parts[offset]);
}
}
/* Return the total number of unpacked cells. */
return c->count;
......@@ -158,8 +159,6 @@ int cell_link(struct cell *c, struct part *parts) {
int cell_pack(struct cell *c, struct pcell *pc) {
int k, count = 1;
/* Start by packing the data of the current cell. */
pc->h_max = c->h_max;
pc->ti_end_min = c->ti_end_min;
......@@ -168,7 +167,8 @@ int cell_pack(struct cell *c, struct pcell *pc) {
c->tag = pc->tag = atomic_inc(&cell_next_tag) % cell_max_tag;
/* Fill in the progeny, depth-first recursion. */
for (k = 0; k < 8; k++)
int count = 1;
for (int k = 0; k < 8; k++)
if (c->progeny[k] != NULL) {
pc->progeny[k] = count;
count += cell_pack(c->progeny[k], &pc[count]);
......@@ -187,7 +187,6 @@ int cell_pack(struct cell *c, struct pcell *pc) {
int cell_locktree(struct cell *c) {
struct cell *finger, *finger2;
TIMER_TIC
/* First of all, try to lock this cell. */
......@@ -208,6 +207,7 @@ int cell_locktree(struct cell *c) {
}
/* Climb up the tree and lock/hold/unlock. */
struct cell *finger;
for (finger = c->parent; finger != NULL; finger = finger->parent) {
/* Lock this cell. */
......@@ -230,7 +230,8 @@ int cell_locktree(struct cell *c) {
else {
/* Undo the holds up to finger. */
for (finger2 = c->parent; finger2 != finger; finger2 = finger2->parent)
for (struct cell *finger2 = c->parent; finger2 != finger;
finger2 = finger2->parent)
__sync_fetch_and_sub(&finger2->hold, 1);
/* Unlock this cell. */
......@@ -244,7 +245,6 @@ int cell_locktree(struct cell *c) {
int cell_glocktree(struct cell *c) {
struct cell *finger, *finger2;
TIMER_TIC
/* First of all, try to lock this cell. */
......@@ -265,6 +265,7 @@ int cell_glocktree(struct cell *c) {
}
/* Climb up the tree and lock/hold/unlock. */
struct cell *finger;
for (finger = c->parent; finger != NULL; finger = finger->parent) {
/* Lock this cell. */
......@@ -287,7 +288,8 @@ int cell_glocktree(struct cell *c) {
else {
/* Undo the holds up to finger. */
for (finger2 = c->parent; finger2 != finger; finger2 = finger2->parent)
for (struct cell *finger2 = c->parent; finger2 != finger;
finger2 = finger2->parent)
__sync_fetch_and_sub(&finger2->ghold, 1);
/* Unlock this cell. */
......@@ -307,14 +309,13 @@ int cell_glocktree(struct cell *c) {
void cell_unlocktree(struct cell *c) {
struct cell *finger;
TIMER_TIC
/* First of all, try to unlock this cell. */
if (lock_unlock(&c->lock) != 0) error("Failed to unlock cell.");
/* Climb up the tree and unhold the parents. */
for (finger = c->parent; finger != NULL; finger = finger->parent)
for (struct cell *finger = c->parent; finger != NULL; finger = finger->parent)
__sync_fetch_and_sub(&finger->hold, 1);
TIMER_TOC(timer_locktree);
......@@ -322,14 +323,13 @@ void cell_unlocktree(struct cell *c) {
void cell_gunlocktree(struct cell *c) {
struct cell *finger;
TIMER_TIC
/* First of all, try to unlock this cell. */
if (lock_unlock(&c->glock) != 0) error("Failed to unlock cell.");
/* Climb up the tree and unhold the parents. */
for (finger = c->parent; finger != NULL; finger = finger->parent)
for (struct cell *finger = c->parent; finger != NULL; finger = finger->parent)
__sync_fetch_and_sub(&finger->ghold, 1);
TIMER_TOC(timer_locktree);
......@@ -343,15 +343,16 @@ void cell_gunlocktree(struct cell *c) {
void cell_split(struct cell *c) {
int i, j, k, count = c->count, gcount = c->gcount;
struct part temp, *parts = c->parts;
struct xpart xtemp, *xparts = c->xparts;
struct gpart gtemp, *gparts = c->gparts;
int i, j;
const int count = c->count, gcount = c->gcount;
struct part* parts = c->parts;
struct xpart* xparts = c->xparts;
struct gpart* gparts = c->gparts;
int left[8], right[8];
double pivot[3];
/* Init the pivots. */
for (k = 0; k < 3; k++) pivot[k] = c->loc[k] + c->h[k] / 2;
for (int k = 0; k < 3; k++) pivot[k] = c->loc[k] + c->h[k] / 2;
/* Split along the x-axis. */
i = 0;
......@@ -360,10 +361,10 @@ void cell_split(struct cell *c) {
while (i <= count - 1 && parts[i].x[0] <= pivot[0]) i += 1;
while (j >= 0 && parts[j].x[0] > pivot[0]) j -= 1;
if (i < j) {
temp = parts[i];
struct part temp = parts[i];
parts[i] = parts[j];
parts[j] = temp;
xtemp = xparts[i];
struct xpart xtemp = xparts[i];
xparts[i] = xparts[j];
xparts[j] = xtemp;
}
......@@ -380,17 +381,17 @@ void cell_split(struct cell *c) {
right[0] = j;
/* Split along the y axis, twice. */
for (k = 1; k >= 0; k--) {
for (int k = 1; k >= 0; k--) {
i = left[k];
j = right[k];
while (i <= j) {
while (i <= right[k] && parts[i].x[1] <= pivot[1]) i += 1;
while (j >= left[k] && parts[j].x[1] > pivot[1]) j -= 1;
if (i < j) {
temp = parts[i];
struct part temp = parts[i];
parts[i] = parts[j];
parts[j] = temp;
xtemp = xparts[i];
struct xpart xtemp = xparts[i];
xparts[i] = xparts[j];
xparts[j] = xtemp;
}
......@@ -410,17 +411,17 @@ void cell_split(struct cell *c) {
}
/* Split along the z axis, four times. */
for (k = 3; k >= 0; k--) {
for (int k = 3; k >= 0; k--) {
i = left[k];
j = right[k];
while (i <= j) {
while (i <= right[k] && parts[i].x[2] <= pivot[2]) i += 1;
while (j >= left[k] && parts[j].x[2] > pivot[2]) j -= 1;
if (i < j) {
temp = parts[i];
struct part temp = parts[i];
parts[i] = parts[j];
parts[j] = temp;
xtemp = xparts[i];
struct xpart xtemp = xparts[i];
xparts[i] = xparts[j];
xparts[j] = xtemp;
}
......@@ -442,14 +443,14 @@ void cell_split(struct cell *c) {
}
/* Store the counts and offsets. */
for (k = 0; k < 8; k++) {
for (int k = 0; k < 8; k++) {
c->progeny[k]->count = right[k] - left[k] + 1;
c->progeny[k]->parts = &c->parts[left[k]];
c->progeny[k]->xparts = &c->xparts[left[k]];
}
/* Re-link the gparts. */
for (k = 0; k < count; k++)
for (int k = 0; k < count; k++)
if (parts[k].gpart != NULL) parts[k].gpart->part = &parts[k];
/* Verify that _all_ the parts have been assigned to a cell. */
......@@ -463,17 +464,17 @@ void cell_split(struct cell *c) {
error( "Particle sorting failed (right edge)." ); */
/* Verify a few sub-cells. */
/* for ( k = 0 ; k < c->progeny[0]->count ; k++ )
/* for (int k = 0 ; k < c->progeny[0]->count ; k++ )
if ( c->progeny[0]->parts[k].x[0] > pivot[0] ||
c->progeny[0]->parts[k].x[1] > pivot[1] ||
c->progeny[0]->parts[k].x[2] > pivot[2] )
error( "Sorting failed (progeny=0)." );
for ( k = 0 ; k < c->progeny[1]->count ; k++ )
for (int k = 0 ; k < c->progeny[1]->count ; k++ )
if ( c->progeny[1]->parts[k].x[0] > pivot[0] ||
c->progeny[1]->parts[k].x[1] > pivot[1] ||
c->progeny[1]->parts[k].x[2] <= pivot[2] )
error( "Sorting failed (progeny=1)." );
for ( k = 0 ; k < c->progeny[2]->count ; k++ )
for (int k = 0 ; k < c->progeny[2]->count ; k++ )
if ( c->progeny[2]->parts[k].x[0] > pivot[0] ||
c->progeny[2]->parts[k].x[1] <= pivot[1] ||
c->progeny[2]->parts[k].x[2] > pivot[2] )
......@@ -488,7 +489,7 @@ void cell_split(struct cell *c) {
while (i <= gcount - 1 && gparts[i].x[0] <= pivot[0]) i += 1;
while (j >= 0 && gparts[j].x[0] > pivot[0]) j -= 1;
if (i < j) {
gtemp = gparts[i];
struct gpart gtemp = gparts[i];
gparts[i] = gparts[j];
gparts[j] = gtemp;
}
......@@ -499,14 +500,14 @@ void cell_split(struct cell *c) {
right[0] = j;
/* Split along the y axis, twice. */
for (k = 1; k >= 0; k--) {
for (int k = 1; k >= 0; k--) {
i = left[k];
j = right[k];
while (i <= j) {
while (i <= right[k] && gparts[i].x[1] <= pivot[1]) i += 1;
while (j >= left[k] && gparts[j].x[1] > pivot[1]) j -= 1;
if (i < j) {
gtemp = gparts[i];
struct gpart gtemp = gparts[i];
gparts[i] = gparts[j];
gparts[j] = gtemp;
}
......@@ -518,14 +519,14 @@ void cell_split(struct cell *c) {
}
/* Split along the z axis, four times. */
for (k = 3; k >= 0; k--) {
for (int k = 3; k >= 0; k--) {
i = left[k];
j = right[k];
while (i <= j) {
while (i <= right[k] && gparts[i].x[2] <= pivot[2]) i += 1;
while (j >= left[k] && gparts[j].x[2] > pivot[2]) j -= 1;
if (i < j) {
gtemp = gparts[i];
struct gpart gtemp = gparts[i];
gparts[i] = gparts[j];
gparts[j] = gtemp;
}
......@@ -537,13 +538,13 @@ void cell_split(struct cell *c) {
}
/* Store the counts and offsets. */
for (k = 0; k < 8; k++) {
for (int k = 0; k < 8; k++) {
c->progeny[k]->gcount = right[k] - left[k] + 1;
c->progeny[k]->gparts = &c->gparts[left[k]];
}
/* Re-link the parts. */
for (k = 0; k < gcount; k++)
for (int k = 0; k < gcount; k++)
if (gparts[k].id > 0) gparts[k].part->gpart = &gparts[k];
}
......
......@@ -359,9 +359,7 @@ FILE* prepareXMFfile() {
if (tempFile == NULL) error("Unable to open temporary file.");
int i = 0;
while (fgets(buffer, 1024, tempFile) != NULL && i < counter - 3) {
i++;
for (int i = 0; fgets(buffer, 1024, tempFile) != NULL && i < counter - 3; i++) {
fprintf(xmfFile, "%s", buffer);
}
fprintf(xmfFile, "\n");
......
......@@ -53,14 +53,15 @@
void printParticle(struct part *parts, struct xpart *xparts, long long int id,
size_t N) {
int i, found = 0;
int found = 0;
/* Look for the particle. */
for (i = 0; i < N; i++)
for (size_t i = 0; i < N; i++)
if (parts[i].id == id) {
printf("## Particle[%d]:\n id=%lld", i, parts[i].id);
printf("## Particle[%zd]:\n id=%lld", i, parts[i].id);
hydro_debug_particle(&parts[i], &xparts[i]);
found = 1;
break;
}
if (!found) printf("## Particles[???] id=%lld not found\n", id);
......@@ -68,13 +69,13 @@ void printParticle(struct part *parts, struct xpart *xparts, long long int id,
void printgParticle(struct gpart *parts, long long int id, size_t N) {
int i, found = 0;
int found = 0;
/* Look for the particle. */
for (i = 0; i < N; i++)
for (size_t i = 0; i < N; i++)
if (parts[i].id == -id || (parts[i].id > 0 && parts[i].part->id == id)) {
printf(
"## gParticle[%d]: id=%lld, x=[%.16e,%.16e,%.16e], "
"## gParticle[%zd]: id=%lld, x=[%.16e,%.16e,%.16e], "
"v=[%.3e,%.3e,%.3e], a=[%.3e,%.3e,%.3e], m=%.3e, t_begin=%d, "
"t_end=%d\n",
i, parts[i].part->id, parts[i].x[0], parts[i].x[1], parts[i].x[2],
......@@ -82,6 +83,7 @@ void printgParticle(struct gpart *parts, long long int id, size_t N) {
parts[i].a[1], parts[i].a[2], parts[i].mass, parts[i].ti_begin,
parts[i].ti_end);
found = 1;
break;
}
if (!found) printf("## Particles[???] id=%lld not found\n", id);
......@@ -133,8 +135,6 @@ void dumpMETISGraph(const char *prefix, idx_t nvertices, idx_t nvertexweights,
FILE *simplefile = NULL;
FILE *weightfile = NULL;
char fname[200];
idx_t i;
idx_t j;
int haveedgeweight = 0;
int havevertexsize = 0;
int havevertexweight = 0;
......@@ -142,7 +142,7 @@ void dumpMETISGraph(const char *prefix, idx_t nvertices, idx_t nvertexweights,
nseq++;
if (vertexweights != NULL) {
for (i = 0; i < nvertices * nvertexweights; i++) {
for (idx_t i = 0; i < nvertices * nvertexweights; i++) {
if (vertexweights[i] != 1) {
havevertexweight = 1;
break;
......@@ -151,7 +151,7 @@ void dumpMETISGraph(const char *prefix, idx_t nvertices, idx_t nvertexweights,
}
if (vertexsizes != NULL) {
for (i = 0; i < nvertices; i++) {
for (idx_t i = 0; i < nvertices; i++) {
if (vertexsizes[i] != 1) {
havevertexsize = 1;
break;
......@@ -160,7 +160,7 @@ void dumpMETISGraph(const char *prefix, idx_t nvertices, idx_t nvertexweights,
}
if (edgeweights != NULL) {
for (i = 0; i < cellconruns[nvertices]; i++) {
for (idx_t i = 0; i < cellconruns[nvertices]; i++) {
if (edgeweights[i] != 1) {
haveedgeweight = 1;
break;
......@@ -200,7 +200,7 @@ void dumpMETISGraph(const char *prefix, idx_t nvertices, idx_t nvertexweights,
}
/* Write the rest of the graph. */
for (i = 0; i < nvertices; i++) {
for (idx_t i = 0; i < nvertices; i++) {
fprintf(stdfile, "\n");
fprintf(simplefile, "\n");
if (weightfile != NULL) {
......@@ -213,13 +213,13 @@ void dumpMETISGraph(const char *prefix, idx_t nvertices, idx_t nvertexweights,
}
if (havevertexweight) {
for (j = 0; j < nvertexweights; j++) {
for (idx_t j = 0; j < nvertexweights; j++) {
fprintf(stdfile, " %" PRIDX, vertexweights[i * nvertexweights + j]);
fprintf(weightfile, " %" PRIDX, vertexweights[i * nvertexweights + j]);
}
}
for (j = cellconruns[i]; j < cellconruns[i + 1]; j++) {
for (idx_t j = cellconruns[i]; j < cellconruns[i + 1]; j++) {
fprintf(stdfile, " %" PRIDX, cellcon[j] + 1);
fprintf(simplefile, " %" PRIDX, cellcon[j] + 1);
if (haveedgeweight) {
......
This diff is collapsed.
Supports Markdown
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