diff --git a/src/cell.c b/src/cell.c index 9fd32ed8c6402d51358bcdc888aefaad16abe7c2..15ea5fc6981d671fff163b52efb3bc36e371aaba 100644 --- a/src/cell.c +++ b/src/cell.c @@ -1815,14 +1815,14 @@ void cell_clean(struct cell *c) { /* Hydro */ for (int i = 0; i < 13; i++) if (c->hydro.sort[i] != NULL) { - free(c->hydro.sort[i]); + swift_free("hydro.sorts", c->hydro.sort[i]); c->hydro.sort[i] = NULL; } /* Stars */ for (int i = 0; i < 13; i++) if (c->stars.sort[i] != NULL) { - free(c->stars.sort[i]); + swift_free("stars.sort", c->stars.sort[i]); c->stars.sort[i] = NULL; } @@ -4299,7 +4299,7 @@ void cell_clear_stars_sort_flags(struct cell *c, const int is_super) { #endif for (int i = 0; i < 13; i++) { - free(c->stars.sort[i]); + swift_free("stars.sort", c->stars.sort[i]); } } diff --git a/src/engine.c b/src/engine.c index db5368a4e6667bb3af4097416dfc955d54b821de..17b4c50dae2b54cb749238bc100f216b975ff16e 100644 --- a/src/engine.c +++ b/src/engine.c @@ -632,7 +632,7 @@ void engine_redistribute(struct engine *e) { error("Failed to allocate counts temporary buffer."); int *dest; - if ((dest = (int *)malloc(sizeof(int) * nr_parts)) == NULL) + if ((dest = (int *)swift_malloc("dest", sizeof(int) * nr_parts)) == NULL) error("Failed to allocate dest temporary buffer."); /* Simple index of node IDs, used for mappers over nodes. */ @@ -695,7 +695,7 @@ void engine_redistribute(struct engine *e) { threadpool_map(&e->threadpool, engine_redistribute_savelink_mapper_part, nodes, nr_nodes, sizeof(int), 0, &savelink_data); } - free(dest); + swift_free("dest", dest); /* Get destination of each s-particle */ int *s_counts; @@ -703,7 +703,7 @@ void engine_redistribute(struct engine *e) { error("Failed to allocate s_counts temporary buffer."); int *s_dest; - if ((s_dest = (int *)malloc(sizeof(int) * nr_sparts)) == NULL) + if ((s_dest = (int *)swift_malloc("s_dest", sizeof(int) * nr_sparts)) == NULL) error("Failed to allocate s_dest temporary buffer."); redist_data.counts = s_counts; @@ -753,7 +753,7 @@ void engine_redistribute(struct engine *e) { threadpool_map(&e->threadpool, engine_redistribute_savelink_mapper_spart, nodes, nr_nodes, sizeof(int), 0, &savelink_data); } - free(s_dest); + swift_free("s_dest", s_dest); /* Get destination of each g-particle */ int *g_counts; @@ -761,7 +761,7 @@ void engine_redistribute(struct engine *e) { error("Failed to allocate g_gcount temporary buffer."); int *g_dest; - if ((g_dest = (int *)malloc(sizeof(int) * nr_gparts)) == NULL) + if ((g_dest = (int *)swift_malloc("g_dest", sizeof(int) * nr_gparts)) == NULL) error("Failed to allocate g_dest temporary buffer."); redist_data.counts = g_counts; @@ -801,7 +801,7 @@ void engine_redistribute(struct engine *e) { } #endif - free(g_dest); + swift_free("g_dest", g_dest); /* Get all the counts from all the nodes. */ if (MPI_Allreduce(MPI_IN_PLACE, counts, nr_nodes * nr_nodes, MPI_INT, MPI_SUM, @@ -4005,7 +4005,8 @@ void engine_collect_stars_counter(struct engine *e) { } /* Get all sparticles */ - struct spart *sparts = (struct spart *)malloc(total * sizeof(struct spart)); + struct spart *sparts = + (struct spart *)swift_malloc("sparts", total * sizeof(struct spart)); err = MPI_Allgatherv(e->s->sparts_foreign, e->s->nr_sparts_foreign, spart_mpi_type, sparts, n_sparts_int, displs, spart_mpi_type, MPI_COMM_WORLD); @@ -5392,7 +5393,7 @@ void engine_clean(struct engine *e) { output_list_clean(&e->output_list_stats); output_list_clean(&e->output_list_stf); - free(e->links); + swift_free("links", e->links); #if defined(WITH_LOGGER) logger_clean(e->logger); free(e->logger); diff --git a/src/engine_maketasks.c b/src/engine_maketasks.c index d1858f87ff0bfdfee878f2e53b81e100812fd0a5..35759581b625a27bb95677e9c6e00e415b86e710 100644 --- a/src/engine_maketasks.c +++ b/src/engine_maketasks.c @@ -2289,7 +2289,7 @@ void engine_maketasks(struct engine *e) { #endif /* Free the old list of cell-task links. */ - if (e->links != NULL) free(e->links); + if (e->links != NULL) swift_free("links", e->links); e->size_links = e->sched.nr_tasks * e->links_per_tasks; /* Make sure that we have space for more links than last time. */ @@ -2297,8 +2297,8 @@ void engine_maketasks(struct engine *e) { e->size_links = e->nr_links * engine_rebuild_link_alloc_margin; /* Allocate the new link list */ - if ((e->links = (struct link *)malloc(sizeof(struct link) * e->size_links)) == - NULL) + if ((e->links = (struct link *)swift_malloc( + "links", sizeof(struct link) * e->size_links)) == NULL) error("Failed to allocate cell-task links."); e->nr_links = 0; diff --git a/src/memuse.h b/src/memuse.h index 334e84a4cecc6d6aabfad77bc632b91b2ff5f098..e9bb16733c2cd0b1fe8440e4fbbb41356afdcd4b 100644 --- a/src/memuse.h +++ b/src/memuse.h @@ -34,11 +34,16 @@ const char *memuse_process(int inmb); void memuse_log_dump(const char *filename); void memuse_log_allocation(const char *label, void *ptr, int allocated, size_t size); +#else + +/* No-op when not reporting. */ +#define memuse_log_allocation(label, ptr, allocated, size) #endif /** * @brief allocate aligned memory. The use and results are the same as the - * posix_memalign function. + * posix_memalign function. This function should be used for any + * significant allocations and consistently labelled. * * @param label a symbolic label for the memory, i.e. "parts". * @param memptr pointer to the allocated memory. @@ -57,12 +62,32 @@ __attribute__((always_inline)) inline int swift_memalign(const char *label, return result; } +/** + * @brief allocate memory. The use and results are the same as the + * malloc function. This function should be used for any + * _significant_ allocations and consistently labelled. + * Do not use this function for small or high frequency + * allocations in production code. + * + * @param label a symbolic label for the memory, i.e. "parts". + * @param size the quantity of bytes to allocate. + * @result pointer to the allocated memory or NULL on failure. + */ +__attribute__((always_inline)) inline void *swift_malloc(const char *label, + size_t size) { + void *memptr = malloc(size); +#ifdef SWIFT_MEMUSE_REPORTS + if (memptr != NULL) memuse_log_allocation(label, memptr, 1, size); +#endif + return memptr; +} + /** * @brief free aligned memory. The use and results are the same as the - * free function. + * free function. The label should match a prior call to swift_memalign + * or swift_malloc. * - * @param label a symbolic label for the memory, i.e. "parts", should match - * call used to allocate the memory. + * @param label a symbolic label for the memory, i.e. "parts". * @param ptr pointer to the allocated memory. */ __attribute__((always_inline)) inline void swift_free(const char *label, diff --git a/src/mesh_gravity.c b/src/mesh_gravity.c index e7005b083c94e20f5218923e443f71464ab383e1..bf0519842eb620e0ff4d8cd0cda05a9395c9722f 100644 --- a/src/mesh_gravity.c +++ b/src/mesh_gravity.c @@ -389,6 +389,8 @@ void pm_mesh_compute_potential(struct pm_mesh* mesh, const struct space* s, (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * N * N * (N_half + 1)); if (frho == NULL) error("Error allocating memory for transform of density mesh"); + memuse_log_allocation("fftw_frho", frho, 1, + sizeof(fftw_complex) * N * N * (N_half + 1)); /* Prepare the FFT library */ fftw_plan forward_plan = fftw_plan_dft_r2c_3d( @@ -537,6 +539,7 @@ void pm_mesh_compute_potential(struct pm_mesh* mesh, const struct space* s, /* Clean-up the mess */ fftw_destroy_plan(forward_plan); fftw_destroy_plan(inverse_plan); + memuse_log_allocation("fftw_frho", frho, 0, 0); fftw_free(frho); #else @@ -636,6 +639,9 @@ void pm_mesh_init(struct pm_mesh* mesh, const struct gravity_props* props, mesh->potential = (double*)fftw_malloc(sizeof(double) * N * N * N); if (mesh->potential == NULL) error("Error allocating memory for the long-range gravity mesh."); + memuse_log_allocation("fftw_mesh.potential", mesh->potential, 1, + sizeof(double) * N * N * N); + #else error("No FFTW library found. Cannot compute periodic long-range forces."); #endif @@ -674,7 +680,10 @@ void pm_mesh_clean(struct pm_mesh* mesh) { fftw_cleanup_threads(); #endif - if (mesh->potential) free(mesh->potential); + if (mesh->potential) { + memuse_log_allocation("fftw_mesh.potential", mesh->potential, 0, 0); + free(mesh->potential); + } mesh->potential = 0; } @@ -718,6 +727,8 @@ void pm_mesh_struct_restore(struct pm_mesh* mesh, FILE* stream) { mesh->potential = (double*)fftw_malloc(sizeof(double) * N * N * N); if (mesh->potential == NULL) error("Error allocating memory for the long-range gravity mesh."); + memuse_log_allocation("fftw_mesh.potential", mesh->potential, 1, + sizeof(double) * N * N * N); #else error("No FFTW library found. Cannot compute periodic long-range forces."); #endif diff --git a/src/parallel_io.c b/src/parallel_io.c index d83a4c566ffdc2624c536731e799ce44df0971c0..e6547245877b7e2c7cfc8a6a455f0714cb173479 100644 --- a/src/parallel_io.c +++ b/src/parallel_io.c @@ -473,7 +473,7 @@ void writeArray_chunk(struct engine* e, hid_t h_data, /* Allocate temporary buffer */ void* temp = NULL; - if (swift_memalign("temp", (void**)&temp, IO_BUFFER_ALIGNMENT, + if (swift_memalign("writetemp", (void**)&temp, IO_BUFFER_ALIGNMENT, num_elements * typeSize) != 0) error("Unable to allocate temporary i/o buffer"); @@ -557,7 +557,7 @@ void writeArray_chunk(struct engine* e, hid_t h_data, #endif /* Free and close everything */ - swift_free("temp", temp); + swift_free("writetemp", temp); H5Sclose(h_memspace); H5Sclose(h_filespace); } diff --git a/src/proxy.c b/src/proxy.c index 6de044f4009173182da0bee41ae6c1d9d2ccb0d6..d9f27ad63cc4c888c14048de83a1576000aacac6 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -650,30 +650,30 @@ void proxy_parts_exchange_second(struct proxy *p) { do { p->size_parts_in *= proxy_buffgrow; } while (p->nr_parts_in > p->size_parts_in); - free(p->parts_in); - free(p->xparts_in); - if ((p->parts_in = (struct part *)malloc(sizeof(struct part) * - p->size_parts_in)) == NULL || - (p->xparts_in = (struct xpart *)malloc(sizeof(struct xpart) * - p->size_parts_in)) == NULL) + swift_free("parts_in", p->parts_in); + swift_free("xparts_in", p->xparts_in); + if ((p->parts_in = (struct part *)swift_malloc( + "parts_in", sizeof(struct part) * p->size_parts_in)) == NULL || + (p->xparts_in = (struct xpart *)swift_malloc( + "xparts_in", sizeof(struct xpart) * p->size_parts_in)) == NULL) error("Failed to re-allocate parts_in buffers."); } if (p->nr_gparts_in > p->size_gparts_in) { do { p->size_gparts_in *= proxy_buffgrow; } while (p->nr_gparts_in > p->size_gparts_in); - free(p->gparts_in); - if ((p->gparts_in = (struct gpart *)malloc(sizeof(struct gpart) * - p->size_gparts_in)) == NULL) + swift_free("gparts_in", p->gparts_in); + if ((p->gparts_in = (struct gpart *)swift_malloc( + "gparts_in", sizeof(struct gpart) * p->size_gparts_in)) == NULL) error("Failed to re-allocate gparts_in buffers."); } if (p->nr_sparts_in > p->size_sparts_in) { do { p->size_sparts_in *= proxy_buffgrow; } while (p->nr_sparts_in > p->size_sparts_in); - free(p->sparts_in); - if ((p->sparts_in = (struct spart *)malloc(sizeof(struct spart) * - p->size_sparts_in)) == NULL) + swift_free("sparts_in", p->sparts_in); + if ((p->sparts_in = (struct spart *)swift_malloc( + "sparts_in", sizeof(struct spart) * p->size_sparts_in)) == NULL) error("Failed to re-allocate sparts_in buffers."); } @@ -729,15 +729,15 @@ void proxy_parts_load(struct proxy *p, const struct part *parts, } while (p->nr_parts_out + N > p->size_parts_out); struct part *tp = NULL; struct xpart *txp = NULL; - if ((tp = (struct part *)malloc(sizeof(struct part) * p->size_parts_out)) == - NULL || - (txp = (struct xpart *)malloc(sizeof(struct xpart) * - p->size_parts_out)) == NULL) + if ((tp = (struct part *)swift_malloc( + "parts_out", sizeof(struct part) * p->size_parts_out)) == NULL || + (txp = (struct xpart *)swift_malloc( + "xparts_out", sizeof(struct xpart) * p->size_parts_out)) == NULL) error("Failed to re-allocate parts_out buffers."); memcpy(tp, p->parts_out, sizeof(struct part) * p->nr_parts_out); memcpy(txp, p->xparts_out, sizeof(struct xpart) * p->nr_parts_out); - free(p->parts_out); - free(p->xparts_out); + swift_free("parts_out", p->parts_out); + swift_free("xparts_out", p->xparts_out); p->parts_out = tp; p->xparts_out = txp; } @@ -765,11 +765,11 @@ void proxy_gparts_load(struct proxy *p, const struct gpart *gparts, int N) { p->size_gparts_out *= proxy_buffgrow; } while (p->nr_gparts_out + N > p->size_gparts_out); struct gpart *tp; - if ((tp = (struct gpart *)malloc(sizeof(struct gpart) * - p->size_gparts_out)) == NULL) + if ((tp = (struct gpart *)swift_malloc( + "gparts_out", sizeof(struct gpart) * p->size_gparts_out)) == NULL) error("Failed to re-allocate gparts_out buffers."); memcpy(tp, p->gparts_out, sizeof(struct gpart) * p->nr_gparts_out); - free(p->gparts_out); + swift_free("gparts_out", p->gparts_out); p->gparts_out = tp; } @@ -795,11 +795,11 @@ void proxy_sparts_load(struct proxy *p, const struct spart *sparts, int N) { p->size_sparts_out *= proxy_buffgrow; } while (p->nr_sparts_out + N > p->size_sparts_out); struct spart *tp; - if ((tp = (struct spart *)malloc(sizeof(struct spart) * - p->size_sparts_out)) == NULL) + if ((tp = (struct spart *)swift_malloc( + "sparts_out", sizeof(struct spart) * p->size_sparts_out)) == NULL) error("Failed to re-allocate sparts_out buffers."); memcpy(tp, p->sparts_out, sizeof(struct spart) * p->nr_sparts_out); - free(p->sparts_out); + swift_free("sparts_out", p->sparts_out); p->sparts_out = tp; } @@ -848,19 +848,19 @@ void proxy_init(struct proxy *p, int mynodeID, int nodeID) { /* Allocate the part send and receive buffers, if needed. */ if (p->parts_in == NULL) { p->size_parts_in = proxy_buffinit; - if ((p->parts_in = (struct part *)malloc(sizeof(struct part) * - p->size_parts_in)) == NULL || - (p->xparts_in = (struct xpart *)malloc(sizeof(struct xpart) * - p->size_parts_in)) == NULL) + if ((p->parts_in = (struct part *)swift_malloc( + "parts_in", sizeof(struct part) * p->size_parts_in)) == NULL || + (p->xparts_in = (struct xpart *)swift_malloc( + "xparts_in", sizeof(struct xpart) * p->size_parts_in)) == NULL) error("Failed to allocate parts_in buffers."); } p->nr_parts_in = 0; if (p->parts_out == NULL) { p->size_parts_out = proxy_buffinit; - if ((p->parts_out = (struct part *)malloc(sizeof(struct part) * - p->size_parts_out)) == NULL || - (p->xparts_out = (struct xpart *)malloc(sizeof(struct xpart) * - p->size_parts_out)) == NULL) + if ((p->parts_out = (struct part *)swift_malloc( + "parts_out", sizeof(struct part) * p->size_parts_out)) == NULL || + (p->xparts_out = (struct xpart *)swift_malloc( + "xparts_out", sizeof(struct xpart) * p->size_parts_out)) == NULL) error("Failed to allocate parts_out buffers."); } p->nr_parts_out = 0; @@ -868,15 +868,15 @@ void proxy_init(struct proxy *p, int mynodeID, int nodeID) { /* Allocate the gpart send and receive buffers, if needed. */ if (p->gparts_in == NULL) { p->size_gparts_in = proxy_buffinit; - if ((p->gparts_in = (struct gpart *)malloc(sizeof(struct gpart) * - p->size_gparts_in)) == NULL) + if ((p->gparts_in = (struct gpart *)swift_malloc( + "gparts_in", sizeof(struct gpart) * p->size_gparts_in)) == NULL) error("Failed to allocate gparts_in buffers."); } p->nr_gparts_in = 0; if (p->gparts_out == NULL) { p->size_gparts_out = proxy_buffinit; - if ((p->gparts_out = (struct gpart *)malloc(sizeof(struct gpart) * - p->size_gparts_out)) == NULL) + if ((p->gparts_out = (struct gpart *)swift_malloc( + "gparts_out", sizeof(struct gpart) * p->size_gparts_out)) == NULL) error("Failed to allocate gparts_out buffers."); } p->nr_gparts_out = 0; @@ -884,15 +884,15 @@ void proxy_init(struct proxy *p, int mynodeID, int nodeID) { /* Allocate the spart send and receive buffers, if needed. */ if (p->sparts_in == NULL) { p->size_sparts_in = proxy_buffinit; - if ((p->sparts_in = (struct spart *)malloc(sizeof(struct spart) * - p->size_sparts_in)) == NULL) + if ((p->sparts_in = (struct spart *)swift_malloc( + "sparts_in", sizeof(struct spart) * p->size_sparts_in)) == NULL) error("Failed to allocate sparts_in buffers."); } p->nr_sparts_in = 0; if (p->sparts_out == NULL) { p->size_sparts_out = proxy_buffinit; - if ((p->sparts_out = (struct spart *)malloc(sizeof(struct spart) * - p->size_sparts_out)) == NULL) + if ((p->sparts_out = (struct spart *)swift_malloc( + "sparts_out", sizeof(struct spart) * p->size_sparts_out)) == NULL) error("Failed to allocate sparts_out buffers."); } p->nr_sparts_out = 0; diff --git a/src/runner.c b/src/runner.c index 35db5e945e1cdcb06833b49248395d7c85a935f3..4cac34538858da3882f1ac6a6c70d45d9842a022 100644 --- a/src/runner.c +++ b/src/runner.c @@ -816,8 +816,9 @@ void runner_do_hydro_sort(struct runner *r, struct cell *c, int flags, /* start by allocating the entry arrays in the requested dimensions. */ for (int j = 0; j < 13; j++) { if ((flags & (1 << j)) && c->hydro.sort[j] == NULL) { - if ((c->hydro.sort[j] = (struct entry *)malloc(sizeof(struct entry) * - (count + 1))) == NULL) + if ((c->hydro.sort[j] = (struct entry *) + swift_malloc("hydro.sort", sizeof(struct entry) * + (count + 1))) == NULL) error("Failed to allocate sort memory."); } } @@ -1041,8 +1042,8 @@ void runner_do_stars_sort(struct runner *r, struct cell *c, int flags, /* start by allocating the entry arrays in the requested dimensions. */ for (int j = 0; j < 13; j++) { if ((flags & (1 << j)) && c->stars.sort[j] == NULL) { - if ((c->stars.sort[j] = (struct entry *)malloc(sizeof(struct entry) * - (count + 1))) == NULL) + if ((c->stars.sort[j] = (struct entry *)swift_malloc( + "stars.sort", sizeof(struct entry) * (count + 1))) == NULL) error("Failed to allocate sort memory."); } } diff --git a/src/scheduler.c b/src/scheduler.c index 97288d267a8c958cecd9d67c49edf683f063f9d5..6187949c2e12e00a6caf51813aeaa46959dd0374 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -68,9 +68,10 @@ static void scheduler_extend_unlocks(struct scheduler *s) { /* Allocate the new buffer. */ const int size_unlocks_new = s->size_unlocks * 2; - struct task **unlocks_new = - (struct task **)malloc(sizeof(struct task *) * size_unlocks_new); - int *unlock_ind_new = (int *)malloc(sizeof(int) * size_unlocks_new); + struct task **unlocks_new = (struct task **)swift_malloc( + "unlocks", sizeof(struct task *) * size_unlocks_new); + int *unlock_ind_new = + (int *)swift_malloc("unlock_ind", sizeof(int) * size_unlocks_new); if (unlocks_new == NULL || unlock_ind_new == NULL) error("Failed to re-allocate unlocks."); @@ -1322,7 +1323,8 @@ void scheduler_set_unlocks(struct scheduler *s) { /* Store the counts for each task. */ short int *counts; - if ((counts = (short int *)malloc(sizeof(short int) * s->nr_tasks)) == NULL) + if ((counts = (short int *)swift_malloc( + "counts", sizeof(short int) * s->nr_tasks)) == NULL) error("Failed to allocate temporary counts array."); bzero(counts, sizeof(short int) * s->nr_tasks); for (int k = 0; k < s->nr_unlocks; k++) { @@ -1340,7 +1342,8 @@ void scheduler_set_unlocks(struct scheduler *s) { /* Compute the offset for each unlock block. */ int *offsets; - if ((offsets = (int *)malloc(sizeof(int) * (s->nr_tasks + 1))) == NULL) + if ((offsets = (int *)swift_malloc("offsets", + sizeof(int) * (s->nr_tasks + 1))) == NULL) error("Failed to allocate temporary offsets array."); offsets[0] = 0; for (int k = 0; k < s->nr_tasks; k++) { @@ -1354,8 +1357,8 @@ void scheduler_set_unlocks(struct scheduler *s) { /* Create and fill a temporary array with the sorted unlocks. */ struct task **unlocks; - if ((unlocks = (struct task **)malloc(sizeof(struct task *) * - s->size_unlocks)) == NULL) + if ((unlocks = (struct task **)swift_malloc( + "unlocks", sizeof(struct task *) * s->size_unlocks)) == NULL) error("Failed to allocate temporary unlocks array."); for (int k = 0; k < s->nr_unlocks; k++) { const int ind = s->unlock_ind[k]; @@ -1396,8 +1399,8 @@ void scheduler_set_unlocks(struct scheduler *s) { #endif /* Clean up. */ - free(counts); - free(offsets); + swift_free("counts", counts); + swift_free("offsets", offsets); } /** @@ -1483,10 +1486,12 @@ void scheduler_reset(struct scheduler *s, int size) { size * sizeof(struct task)) != 0) error("Failed to allocate task array."); - if ((s->tasks_ind = (int *)malloc(sizeof(int) * size)) == NULL) + if ((s->tasks_ind = (int *)swift_malloc("tasks_ind", sizeof(int) * size)) == + NULL) error("Failed to allocate task lists."); - if ((s->tid_active = (int *)malloc(sizeof(int) * size)) == NULL) + if ((s->tid_active = + (int *)swift_malloc("tid_active", sizeof(int) * size)) == NULL) error("Failed to allocate aactive task lists."); } @@ -2161,10 +2166,11 @@ void scheduler_init(struct scheduler *s, struct space *space, int nr_tasks, error("Failed to initialize sleep barrier."); /* Init the unlocks. */ - if ((s->unlocks = (struct task **)malloc( - sizeof(struct task *) * scheduler_init_nr_unlocks)) == NULL || - (s->unlock_ind = - (int *)malloc(sizeof(int) * scheduler_init_nr_unlocks)) == NULL) + if ((s->unlocks = (struct task **)swift_malloc( + "unlocks", sizeof(struct task *) * scheduler_init_nr_unlocks)) == + NULL || + (s->unlock_ind = (int *)swift_malloc( + "unlock_ind", sizeof(int) * scheduler_init_nr_unlocks)) == NULL) error("Failed to allocate unlocks."); s->nr_unlocks = 0; s->size_unlocks = scheduler_init_nr_unlocks; diff --git a/src/serial_io.c b/src/serial_io.c index 1f1ba013175f42094144f846b0053d2259ba2e3c..dc30f3007cb8597f6c5063e5c10b2c8053c24938 100644 --- a/src/serial_io.c +++ b/src/serial_io.c @@ -365,7 +365,7 @@ void writeArray(const struct engine* e, hid_t grp, char* fileName, /* Allocate temporary buffer */ void* temp = NULL; - if (swift_memalign("temp", (void**)&temp, IO_BUFFER_ALIGNMENT, + if (swift_memalign("writetemp", (void**)&temp, IO_BUFFER_ALIGNMENT, num_elements * typeSize) != 0) error("Unable to allocate temporary i/o buffer"); @@ -416,7 +416,7 @@ void writeArray(const struct engine* e, hid_t grp, char* fileName, if (h_err < 0) error("Error while writing data array '%s'.", props.name); /* Free and close everything */ - swift_free("temp", temp); + swift_free("writetemp", temp); H5Dclose(h_data); H5Sclose(h_memspace); H5Sclose(h_filespace); diff --git a/src/single_io.c b/src/single_io.c index 5264403cc9a919f1a9dbdfcec87692ef32b28b5c..b770ef95a0246710dcdd2005f8f057eec43f8e1d 100644 --- a/src/single_io.c +++ b/src/single_io.c @@ -241,7 +241,7 @@ void writeArray(const struct engine* e, hid_t grp, char* fileName, /* Allocate temporary buffer */ void* temp = NULL; - if (swift_memalign("temp", (void**)&temp, IO_BUFFER_ALIGNMENT, + if (swift_memalign("writetemp", (void**)&temp, IO_BUFFER_ALIGNMENT, num_elements * typeSize) != 0) error("Unable to allocate temporary i/o buffer"); @@ -333,7 +333,7 @@ void writeArray(const struct engine* e, hid_t grp, char* fileName, io_write_attribute_s(h_data, "Conversion factor", buffer); /* Free and close everything */ - free(temp); + swift_free("writetemp", temp); H5Pclose(h_prop); H5Dclose(h_data); H5Sclose(h_space); diff --git a/src/space.c b/src/space.c index bbe889e2cd7a98d8bff68ae753d560a7fcc39e16..b98c1f86c3f1e01fa2553e6dc80a8bd857fb9fc5 100644 --- a/src/space.c +++ b/src/space.c @@ -261,13 +261,14 @@ void space_rebuild_recycle_mapper(void *map_data, int num_elements, #endif if (s->with_self_gravity) bzero(c->grav.multipole, sizeof(struct gravity_tensors)); + for (int i = 0; i < 13; i++) { if (c->hydro.sort[i] != NULL) { - free(c->hydro.sort[i]); + swift_free("hydro.sort", c->hydro.sort[i]); c->hydro.sort[i] = NULL; } if (c->stars.sort[i] != NULL) { - free(c->stars.sort[i]); + swift_free("stars.sort", c->stars.sort[i]); c->stars.sort[i] = NULL; } } @@ -416,7 +417,8 @@ void space_regrid(struct space *s, int verbose) { oldwidth[1] = s->width[1]; oldwidth[2] = s->width[2]; - if ((oldnodeIDs = (int *)malloc(sizeof(int) * s->nr_cells)) == NULL) + if ((oldnodeIDs = + (int *)swift_malloc("nodeIDs", sizeof(int) * s->nr_cells)) == NULL) error("Failed to allocate temporary nodeIDs."); int cid = 0; @@ -609,7 +611,7 @@ void space_regrid(struct space *s, int verbose) { engine_makeproxies(s->e); /* Finished with these. */ - free(oldnodeIDs); + swift_free("nodeIDs", oldnodeIDs); } else if (no_regrid && s->e != NULL) { /* If we have created the top-levels cells and not done an initial @@ -1033,16 +1035,19 @@ void space_rebuild(struct space *s, int repartitioned, int verbose) { /* Allocate arrays to store the indices of the cells where particles belong. We allocate extra space to allow for particles we may receive from other nodes */ - int *h_index = (int *)malloc(sizeof(int) * h_index_size); - int *g_index = (int *)malloc(sizeof(int) * g_index_size); - int *s_index = (int *)malloc(sizeof(int) * s_index_size); + int *h_index = (int *)swift_malloc("h_index", sizeof(int) * h_index_size); + int *g_index = (int *)swift_malloc("g_index", sizeof(int) * g_index_size); + int *s_index = (int *)swift_malloc("s_index", sizeof(int) * s_index_size); if (h_index == NULL || g_index == NULL || s_index == NULL) error("Failed to allocate temporary particle indices."); /* Allocate counters of particles that will land in each cell */ - int *cell_part_counts = (int *)malloc(sizeof(int) * s->nr_cells); - int *cell_gpart_counts = (int *)malloc(sizeof(int) * s->nr_cells); - int *cell_spart_counts = (int *)malloc(sizeof(int) * s->nr_cells); + int *cell_part_counts = + (int *)swift_malloc("cell_part_counts", sizeof(int) * s->nr_cells); + int *cell_gpart_counts = + (int *)swift_malloc("cell_gpart_counts", sizeof(int) * s->nr_cells); + int *cell_spart_counts = + (int *)swift_malloc("cell_spart_counts", sizeof(int) * s->nr_cells); if (cell_part_counts == NULL || cell_gpart_counts == NULL || cell_spart_counts == NULL) error("Failed to allocate cell particle count buffer."); @@ -1288,20 +1293,22 @@ void space_rebuild(struct space *s, int repartitioned, int verbose) { /* Re-allocate the index array for the parts if needed.. */ if (s->nr_parts + 1 > h_index_size) { int *ind_new; - if ((ind_new = (int *)malloc(sizeof(int) * (s->nr_parts + 1))) == NULL) + if ((ind_new = (int *)swift_malloc( + "h_index", sizeof(int) * (s->nr_parts + 1))) == NULL) error("Failed to allocate temporary particle indices."); memcpy(ind_new, h_index, sizeof(int) * nr_parts); - free(h_index); + swift_free("h_index", h_index); h_index = ind_new; } /* Re-allocate the index array for the sparts if needed.. */ if (s->nr_sparts + 1 > s_index_size) { int *sind_new; - if ((sind_new = (int *)malloc(sizeof(int) * (s->nr_sparts + 1))) == NULL) + if ((sind_new = (int *)swift_malloc( + "s_index", sizeof(int) * (s->nr_sparts + 1))) == NULL) error("Failed to allocate temporary s-particle indices."); memcpy(sind_new, s_index, sizeof(int) * nr_sparts); - free(s_index); + swift_free("s_index", s_index); s_index = sind_new; } @@ -1430,20 +1437,21 @@ void space_rebuild(struct space *s, int repartitioned, int verbose) { } /* We no longer need the indices as of here. */ - free(h_index); - free(cell_part_counts); - free(s_index); - free(cell_spart_counts); + swift_free("h_index", h_index); + swift_free("cell_part_counts", cell_part_counts); + swift_free("s_index", s_index); + swift_free("cell_spart_counts", cell_spart_counts); #ifdef WITH_MPI /* Re-allocate the index array for the gparts if needed.. */ if (s->nr_gparts + 1 > g_index_size) { int *gind_new; - if ((gind_new = (int *)malloc(sizeof(int) * (s->nr_gparts + 1))) == NULL) + if ((gind_new = (int *)swift_malloc( + "g_index", sizeof(int) * (s->nr_gparts + 1))) == NULL) error("Failed to allocate temporary g-particle indices."); memcpy(gind_new, g_index, sizeof(int) * nr_gparts); - free(g_index); + swift_free("g_index", g_index); g_index = gind_new; } @@ -1517,8 +1525,8 @@ void space_rebuild(struct space *s, int repartitioned, int verbose) { } /* We no longer need the indices as of here. */ - free(g_index); - free(cell_gpart_counts); + swift_free("g_index", g_index); + swift_free("cell_gpart_counts", cell_gpart_counts); #ifdef SWIFT_DEBUG_CHECKS /* Verify that the links are correct */ @@ -2430,11 +2438,11 @@ void space_map_clearsort(struct cell *c, void *data) { for (int i = 0; i < 13; i++) { if (c->hydro.sort[i] != NULL) { - free(c->hydro.sort[i]); + swift_free("hydro.sort", c->hydro.sort[i]); c->hydro.sort[i] = NULL; } if (c->stars.sort[i] != NULL) { - free(c->stars.sort[i]); + swift_free("hydro.sort", c->stars.sort[i]); c->stars.sort[i] = NULL; } } @@ -3217,8 +3225,8 @@ void space_getcells(struct space *s, int nr_cells, struct cell **cells) { /* Init some things in the cell we just got. */ for (int j = 0; j < nr_cells; j++) { for (int k = 0; k < 13; k++) { - if (cells[j]->hydro.sort[k] != NULL) free(cells[j]->hydro.sort[k]); - if (cells[j]->stars.sort[k] != NULL) free(cells[j]->stars.sort[k]); + if (cells[j]->hydro.sort[k] != NULL) swift_free("hydro.sort", cells[j]->hydro.sort[k]); + if (cells[j]->stars.sort[k] != NULL) swift_free("stars.sort", cells[j]->stars.sort[k]); } struct gravity_tensors *temp = cells[j]->grav.multipole; bzero(cells[j], sizeof(struct cell)); @@ -3242,11 +3250,11 @@ void space_free_buff_sort_indices(struct space *s) { finger = finger->next) { for (int k = 0; k < 13; k++) { if (finger->hydro.sort[k] != NULL) { - free(finger->hydro.sort[k]); + swift_free("hydro.sort", finger->hydro.sort[k]); finger->hydro.sort[k] = NULL; } if (finger->stars.sort[k] != NULL) { - free(finger->stars.sort[k]); + swift_free("stars.sort", finger->stars.sort[k]); finger->stars.sort[k] = NULL; } }