From 41f45fa176661fb26069f1d492f48a07e0bb88eb Mon Sep 17 00:00:00 2001
From: "Peter W. Draper" <p.w.draper@durham.ac.uk>
Date: Mon, 2 Mar 2020 12:14:09 +0000
Subject: [PATCH] Fix a memory allocation bug (*nlogs) and stop allocating all
 the buffer memory upfront

Not allocating upfront will reduce the overall memory footprint, but may slow down the send and recv loops
---
 swiftmpistepsim.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/swiftmpistepsim.c b/swiftmpistepsim.c
index 394dea0..8a4a257 100644
--- a/swiftmpistepsim.c
+++ b/swiftmpistepsim.c
@@ -127,6 +127,7 @@ static void *inject_thread(void *arg) {
     /* Differences to SWIFT: MPI_BYTE not the MPI_Type. */
     int err = 0;
     if (log->type == task_type_send) {
+      log->data = calloc(log->size, 1);
       err = MPI_Isend(log->data, log->size, MPI_BYTE, log->otherrank, log->tag,
                       subtypeMPI_comms[log->subtype], &log->req);
 
@@ -136,6 +137,7 @@ static void *inject_thread(void *arg) {
       atomic_inc(&todo_send);
 
     } else {
+      log->data = calloc(log->size, 1);
       err = MPI_Irecv(log->data, log->size, MPI_BYTE, log->otherrank, log->tag,
                       subtypeMPI_comms[log->subtype], &log->req);
 
@@ -325,7 +327,7 @@ static void pick_logs(void) {
       nlogs, sizeof(struct mpiuse_log_entry *));
   nr_reqs = 0;
   sends_queue = (struct mpiuse_log_entry **)calloc(
-      nlogs, sizeof(struct mpiuse_log_entry *) * nlogs);
+      nlogs, sizeof(struct mpiuse_log_entry *));
   nr_sends = 0;
   recvs_queue = (struct mpiuse_log_entry **)calloc(
       nlogs, sizeof(struct mpiuse_log_entry *));
@@ -336,10 +338,8 @@ static void pick_logs(void) {
     if (log->rank == myrank && log->activation) {
       if (log->type == task_type_send || log->type == task_type_recv) {
 
-        /* Allocate space for data. */
-        log->data = calloc(log->size, 1);
-
         /* And keep this log. */
+        log->data = NULL;
         reqs_queue[nr_reqs] = log;
         nr_reqs++;
 
-- 
GitLab