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

Merge branch 'sort_arrays' into 'master'

Allocate a separate sort array for each dimensions.

As discussed, fixes #334.

See merge request !380
parents a1006b75 bb71e883
......@@ -1279,7 +1279,11 @@ void cell_check_multipole(struct cell *c, void *data) {
*/
void cell_clean(struct cell *c) {
free(c->sort);
for (int i = 0; i < 13; i++)
if (c->sort[i] != NULL) {
free(c->sort[i]);
c->sort[i] = NULL;
}
/* Recurse */
for (int k = 0; k < 8; k++)
......
......@@ -123,7 +123,7 @@ struct cell {
struct spart *sparts;
/*! Pointer for the sorted indices. */
struct entry *sort;
struct entry *sort[13];
/*! Pointers to the next level of cells. */
struct cell *progeny[8];
......
......@@ -361,13 +361,14 @@ void runner_do_sort(struct runner *r, struct cell *c, int flags, int cleanup,
if (c->sorted == 0) c->ti_sort = r->e->ti_current;
#endif
/* start by allocating the entry arrays. */
if (c->sort == NULL) {
if ((c->sort = (struct entry *)malloc(sizeof(struct entry) * (count + 1) *
13)) == NULL)
/* start by allocating the entry arrays in the requested dimensions. */
for (int j = 0; j < 13; j++) {
if ((flags & (1 << j)) && c->sort[j] == NULL) {
if ((c->sort[j] = (struct entry *)malloc(sizeof(struct entry) *
(count + 1))) == NULL)
error("Failed to allocate sort memory.");
}
struct entry *sort = c->sort;
}
/* Does this cell have any progeny? */
if (c->split) {
......@@ -409,7 +410,7 @@ void runner_do_sort(struct runner *r, struct cell *c, int flags, int cleanup,
for (int k = 0; k < 8; k++) {
inds[k] = k;
if (c->progeny[k] != NULL && c->progeny[k]->count > 0) {
fingers[k] = &c->progeny[k]->sort[j * (c->progeny[k]->count + 1)];
fingers[k] = c->progeny[k]->sort[j];
buff[k] = fingers[k]->d;
off[k] = off[k];
} else
......@@ -426,7 +427,7 @@ void runner_do_sort(struct runner *r, struct cell *c, int flags, int cleanup,
}
/* For each entry in the new sort list. */
finger = &sort[j * (count + 1)];
finger = c->sort[j];
for (int ind = 0; ind < count; ind++) {
/* Copy the minimum into the new sort array. */
......@@ -447,8 +448,8 @@ void runner_do_sort(struct runner *r, struct cell *c, int flags, int cleanup,
} /* Merge. */
/* Add a sentinel. */
sort[j * (count + 1) + count].d = FLT_MAX;
sort[j * (count + 1) + count].i = 0;
c->sort[j][count].d = FLT_MAX;
c->sort[j][count].i = 0;
/* Mark as sorted. */
atomic_or(&c->sorted, 1 << j);
......@@ -484,8 +485,8 @@ void runner_do_sort(struct runner *r, struct cell *c, int flags, int cleanup,
const double px[3] = {parts[k].x[0], parts[k].x[1], parts[k].x[2]};
for (int j = 0; j < 13; j++)
if (flags & (1 << j)) {
sort[j * (count + 1) + k].i = k;
sort[j * (count + 1) + k].d = px[0] * runner_shift[j][0] +
c->sort[j][k].i = k;
c->sort[j][k].d = px[0] * runner_shift[j][0] +
px[1] * runner_shift[j][1] +
px[2] * runner_shift[j][2];
}
......@@ -494,9 +495,9 @@ void runner_do_sort(struct runner *r, struct cell *c, int flags, int cleanup,
/* Add the sentinel and sort. */
for (int j = 0; j < 13; j++)
if (flags & (1 << j)) {
sort[j * (count + 1) + count].d = FLT_MAX;
sort[j * (count + 1) + count].i = 0;
runner_do_sort_ascending(&sort[j * (count + 1)], count);
c->sort[j][count].d = FLT_MAX;
c->sort[j][count].i = 0;
runner_do_sort_ascending(c->sort[j], count);
atomic_or(&c->sorted, 1 << j);
}
}
......@@ -505,7 +506,7 @@ void runner_do_sort(struct runner *r, struct cell *c, int flags, int cleanup,
/* Verify the sorting. */
for (int j = 0; j < 13; j++) {
if (!(flags & (1 << j))) continue;
finger = &sort[j * (count + 1)];
finger = c->sort[j];
for (int k = 1; k < count; k++) {
if (finger[k].d < finger[k - 1].d)
error("Sorting failed, ascending array.");
......
......@@ -640,7 +640,7 @@ void DOPAIR_SUBSET(struct runner *r, struct cell *restrict ci,
error("Interacting unsorted cells.");
/* Pick-out the sorted lists. */
const struct entry *restrict sort_j = &cj->sort[sid * (cj->count + 1)];
const struct entry *restrict sort_j = cj->sort[sid];
const float dxj = cj->dx_max_sort;
/* Parts are on the left? */
......@@ -906,8 +906,8 @@ void DOPAIR1(struct runner *r, struct cell *ci, struct cell *cj, const int sid,
for (int k = 0; k < 3; k++) rshift += shift[k] * runner_shift[sid][k];
/* Pick-out the sorted lists. */
const struct entry *restrict sort_i = &ci->sort[sid * (ci->count + 1)];
const struct entry *restrict sort_j = &cj->sort[sid * (cj->count + 1)];
const struct entry *restrict sort_i = ci->sort[sid];
const struct entry *restrict sort_j = cj->sort[sid];
#ifdef SWIFT_DEBUG_CHECKS
/* Check that the dx_max_sort values in the cell are indeed an upper
......@@ -1205,8 +1205,8 @@ void DOPAIR2(struct runner *r, struct cell *ci, struct cell *cj) {
for (int k = 0; k < 3; k++) rshift += shift[k] * runner_shift[sid][k];
/* Pick-out the sorted lists. */
struct entry *restrict sort_i = &ci->sort[sid * (ci->count + 1)];
struct entry *restrict sort_j = &cj->sort[sid * (cj->count + 1)];
struct entry *restrict sort_i = ci->sort[sid];
struct entry *restrict sort_j = cj->sort[sid];
#ifdef SWIFT_DEBUG_CHECKS
/* Check that the dx_max_sort values in the cell are indeed an upper
......
......@@ -630,8 +630,8 @@ void runner_dopair1_density_vec(struct runner *r, struct cell *ci,
for (int k = 0; k < 3; k++) rshift += shift[k] * runner_shift[sid][k];
/* Pick-out the sorted lists. */
const struct entry *restrict sort_i = &ci->sort[sid * (ci->count + 1)];
const struct entry *restrict sort_j = &cj->sort[sid * (cj->count + 1)];
const struct entry *restrict sort_i = ci->sort[sid];
const struct entry *restrict sort_j = cj->sort[sid];
#ifdef SWIFT_DEBUG_CHECKS
/* Check that the dx_max_sort values in the cell are indeed an upper
......
......@@ -233,9 +233,10 @@ void space_rebuild_recycle_mapper(void *map_data, int num_elements,
c->xparts = NULL;
c->gparts = NULL;
c->sparts = NULL;
if (c->sort != NULL) {
free(c->sort);
c->sort = NULL;
for (int i = 0; i < 13; i++)
if (c->sort[i] != NULL) {
free(c->sort[i]);
c->sort[i] = NULL;
}
#if WITH_MPI
c->recv_xv = NULL;
......@@ -1804,9 +1805,10 @@ void space_gparts_sort_mapper(void *map_data, int num_elements,
*/
void space_map_clearsort(struct cell *c, void *data) {
if (c->sort != NULL) {
free(c->sort);
c->sort = NULL;
for (int i = 0; i < 13; i++)
if (c->sort[i] != NULL) {
free(c->sort[i]);
c->sort[i] = NULL;
}
}
......@@ -2305,7 +2307,8 @@ void space_recycle(struct space *s, struct cell *c) {
error("Failed to destroy spinlocks.");
/* Clear this cell's sort arrays. */
if (c->sort != NULL) free(c->sort);
for (int i = 0; i < 13; i++)
if (c->sort[i] != NULL) free(c->sort[i]);
/* Lock the space. */
lock_lock(&s->lock);
......@@ -2357,7 +2360,8 @@ void space_recycle_list(struct space *s, struct cell *cell_list_begin,
error("Failed to destroy spinlocks.");
/* Clear this cell's sort arrays. */
if (c->sort != NULL) free(c->sort);
for (int i = 0; i < 13; i++)
if (c->sort[i] != NULL) free(c->sort[i]);
/* Count this cell. */
count += 1;
......
......@@ -142,10 +142,7 @@ const char *configuration_options(void) {
static int initialised = 0;
static const char *config = SWIFT_CONFIG_FLAGS;
if (!initialised) {
if (strlen(config) < 1024 - 2)
sprintf(buf, "'%s'", config);
else
error("SWIFT_CONFIG_FLAGS string longer than buffer");
snprintf(buf, 1024, "'%s'", config);
initialised = 1;
}
return buf;
......@@ -161,10 +158,7 @@ const char *compilation_cflags(void) {
static int initialised = 0;
static const char *cflags = SWIFT_CFLAGS;
if (!initialised) {
if (strlen(cflags) < 1024 - 2)
sprintf(buf, "'%s'", cflags);
else
error("SWIFT_CFLAGS string longer than buffer");
snprintf(buf, 1024, "'%s'", cflags);
initialised = 1;
}
return buf;
......
......@@ -340,7 +340,8 @@ struct cell *make_cell(size_t n, const double offset[3], double size, double h,
// shuffle_particles(cell->parts, cell->count);
cell->sorted = 0;
cell->sort = NULL;
for (int k = 0; k < 13; k++)
cell->sort[k] = NULL;
return cell;
}
......@@ -348,7 +349,9 @@ struct cell *make_cell(size_t n, const double offset[3], double size, double h,
void clean_up(struct cell *ci) {
free(ci->parts);
free(ci->xparts);
free(ci->sort);
for (int k = 0; k < 13; k++)
if (ci->sort[k] != NULL)
free(ci->sort[k]);
free(ci);
}
......
......@@ -178,14 +178,17 @@ struct cell *make_cell(size_t n, double *offset, double size, double h,
shuffle_particles(cell->parts, cell->count);
cell->sorted = 0;
cell->sort = NULL;
for (int k = 0; k < 13; k++)
cell->sort[k] = NULL;
return cell;
}
void clean_up(struct cell *ci) {
free(ci->parts);
free(ci->sort);
for (int k = 0; k < 13; k++)
if (ci->sort[k] != NULL)
free(ci->sort[k]);
free(ci);
}
......
......@@ -121,14 +121,17 @@ struct cell *make_cell(size_t n, double *offset, double size, double h,
shuffle_particles(cell->parts, cell->count);
cell->sorted = 0;
cell->sort = NULL;
for (int k = 0; k < 13; k++)
cell->sort[k] = NULL;
return cell;
}
void clean_up(struct cell *ci) {
free(ci->parts);
free(ci->sort);
for (int k = 0; k < 13; k++)
if (ci->sort[k] != NULL)
free(ci->sort[k]);
free(ci);
}
......
......@@ -177,14 +177,17 @@ struct cell *make_cell(size_t n, double *offset, double size, double h,
shuffle_particles(cell->parts, cell->count);
cell->sorted = 0;
cell->sort = NULL;
for (int k = 0; k < 13; k++)
cell->sort[k] = NULL;
return cell;
}
void clean_up(struct cell *ci) {
free(ci->parts);
free(ci->sort);
for (int k = 0; k < 13; k++)
if (ci->sort[k] != NULL)
free(ci->sort[k]);
free(ci);
}
......
......@@ -81,7 +81,8 @@ struct cell *make_cell(size_t N, float cellSize, int offset[3], int id_offset) {
cell->ti_end_max = 1;
cell->sorted = 0;
cell->sort = NULL;
for (int k = 0; k < 13; k++)
cell->sort[k] = NULL;
return cell;
}
......@@ -211,7 +212,9 @@ int main() {
for (int j = 0; j < 27; ++j) {
free(cells[j]->parts);
free(cells[j]->xparts);
free(cells[j]->sort);
for (int k = 0; k < 13; k++)
if (cells[j]->sort[k] != NULL)
free(cells[j]->sort[k]);
free(cells[j]);
}
......
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