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