diff --git a/src/engine.c b/src/engine.c index 89e9afd698f935c7618fb0774becd4f5e2284b4b..40e0a69f6b839b914310a51671d3bb18cc57214f 100644 --- a/src/engine.c +++ b/src/engine.c @@ -1007,7 +1007,8 @@ void engine_repartition(struct engine *e) { /* Partitioning requires copies of the particles, so we need to reduce the * memory in use to the minimum, we can free the sorting indices and the - * tasks as these will be regenerated at the next rebuild. */ + * tasks as these will be regenerated at the next rebuild. Also the foreign + * particle arrays can go as these will be regenerated in proxy exchange. */ /* Sorting indices. */ if (e->s->cells_top != NULL) space_free_cells(e->s); @@ -1015,6 +1016,9 @@ void engine_repartition(struct engine *e) { /* Task arrays. */ scheduler_free_tasks(&e->sched); + /* Foreign parts. */ + space_free_foreign_parts(e->s); + /* Now comes the tricky part: Exchange particles between all nodes. This is done in two steps, first allreducing a matrix of how many particles go from where to where, then re-allocating diff --git a/src/space.c b/src/space.c index 4b568cf3e877f8504c3e7e166ae324b0f49bdefb..0afb3a1fddbf7fa94d1ff1c1522a6fc1387e7b9b 100644 --- a/src/space.c +++ b/src/space.c @@ -294,6 +294,8 @@ void space_rebuild_recycle_mapper(void *map_data, int num_elements, /** * @brief Free up any allocated cells. + * + * @param s The #space. */ void space_free_cells(struct space *s) { @@ -308,6 +310,32 @@ void space_free_cells(struct space *s) { clocks_getunit()); } +/** + * @brief Free any memory in use for foreign particles. + * + * @param s The #space. + */ +void space_free_foreign_parts(struct space *s) { + +#ifdef WITH_MPI + if (s->parts_foreign != NULL) { + free(s->parts_foreign); + s->size_parts_foreign = 0; + s->parts_foreign = NULL; + } + if (s->gparts_foreign != NULL) { + free(s->gparts_foreign); + s->size_gparts_foreign = 0; + s->gparts_foreign = NULL; + } + if (s->sparts_foreign != NULL) { + free(s->sparts_foreign); + s->size_sparts_foreign = 0; + s->sparts_foreign = NULL; + } +#endif +} + /** * @brief Re-build the top-level cell grid. * diff --git a/src/space.h b/src/space.h index 924c3efaf81f88cb82a8c23707d42883832aa071..272ee41c3569cf3580678c0de885a41075f0e1b4 100644 --- a/src/space.h +++ b/src/space.h @@ -329,6 +329,8 @@ void space_reset_task_counters(struct space *s); void space_clean(struct space *s); void space_free_cells(struct space *s); +void space_free_foreign_parts(struct space *s); + void space_struct_dump(struct space *s, FILE *stream); void space_struct_restore(struct space *s, FILE *stream); void space_write_cell_hierarchy(const struct space *s);