Skip to content

Commit 5e50d77

Browse files
committed
enh: no memory leaks for blacs_exit and allow re-call
This should enable ScaLAPACK/BLACS to be exited and restarted at will without memory leaks or problems. When called with blacs_exit(true), the comm_world will be retained and MPI_Finalized won't be called. Only when blacs_exit(false) is called will the BI_COMM_WORLD be de-allocated. So one should expect a memory leak of an integer if the hosting application calls MPI_Finalize.
1 parent 6c76aaa commit 5e50d77

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

BLACS/SRC/blacs_exit_.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ F_VOID_FUNC blacs_exit_(Int *NotDone)
1212
Int BI_BuffIsFree(BLACBUFF *, Int);
1313
BLACBUFF *bp;
1414
extern BLACBUFF *BI_ReadyB, *BI_ActiveQ, BI_AuxBuff;
15+
extern MPI_Status *BI_Stats;
1516
Int i;
1617
extern Int BI_MaxNCtxt, BI_Np;
1718
extern BLACSCONTEXT **BI_MyContxts;
@@ -30,6 +31,7 @@ F_VOID_FUNC blacs_exit_(Int *NotDone)
3031
free(bp);
3132
}
3233
free (BI_AuxBuff.Aops);
34+
free (BI_Stats);
3335

3436
/*
3537
* Reset parameters to initial values
@@ -39,7 +41,12 @@ F_VOID_FUNC blacs_exit_(Int *NotDone)
3941
BI_Np = -1;
4042
if (!Mpval(NotDone))
4143
{
42-
MPI_Finalize();
44+
free(BI_COMM_WORLD);
45+
BI_COMM_WORLD = NULL;
46+
MPI_Finalize();
4347
}
4448
BI_ReadyB = NULL;
49+
BI_ActiveQ = NULL;
50+
BI_AuxBuff.Aops = NULL;
51+
BI_Stats = NULL;
4552
}

BLACS/SRC/blacs_pinfo_.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ F_VOID_FUNC blacs_pinfo_(Int *mypnum, Int *nprocs)
2020

2121
BI_COMM_WORLD = (Int *) malloc(sizeof(Int));
2222
*BI_COMM_WORLD = MPI_Comm_c2f(MPI_COMM_WORLD);
23-
MPI_Comm_size(MPI_COMM_WORLD, &Np);
24-
MPI_Comm_rank(MPI_COMM_WORLD, &Iam);
2523
}
24+
MPI_Comm_size(MPI_COMM_WORLD, &Np);
25+
MPI_Comm_rank(MPI_COMM_WORLD, &Iam);
2626
*mypnum = BI_Iam = Iam;
2727
*nprocs = BI_Np = Np;
2828
}

0 commit comments

Comments
 (0)