Skip to content

Conversation

@zerothi
Copy link
Contributor

@zerothi zerothi commented Aug 16, 2021

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.

For instance the blacs_exit test example can now be extended to this:

      PROGRAM HELLO
*     -- BLACS example code --
*     Written by Clint Whaley 7/26/94
*     Performs a simple check-in type hello world
*     ..
*     .. External Functions ..
      INTEGER BLACS_PNUM
      EXTERNAL BLACS_PNUM
*     ..
*     .. Variable Declaration ..
      INTEGER CONTXT, IAM, NPROCS, NPROW, NPCOL, MYPROW, MYPCOL
      INTEGER ICALLER, I, J, HISROW, HISCOL

      INTEGER :: loop
*
*     Determine my process number and the number of processes in
*     machine
*
      do loop = 1, 3
        CALL BLACS_PINFO(IAM, NPROCS)
        print * , '1: iam ', iam, ' nprocs ', nprocs
*
*     If in PVM, create virtual machine if it doesn't exist
*
      IF (NPROCS .LT. 1) THEN
	 IF (IAM .EQ. 0) THEN
	    WRITE(*, 1000)
	    READ(*, 2000) NPROCS
         END IF
         CALL BLACS_SETUP(IAM, NPROCS)
      END IF
        print * , '2: iam ', iam, ' nprocs ', nprocs
*
*     Set up process grid that is as close to square as possible
*     Set up process grid that is as close to square as possible
*
      NPROW = INT( SQRT( REAL(NPROCS) ) )
      NPCOL = NPROCS / NPROW
*
*     Get default system context, and define grid
*
      CALL BLACS_GET(0, 0, CONTXT)
      CALL BLACS_GRIDINIT(CONTXT, 'Row', NPROW, NPCOL)
      CALL BLACS_GRIDINFO(CONTXT, NPROW, NPCOL, MYPROW, MYPCOL)
*
*     If I'm not in grid, go to end of program
*
      IF ( (MYPROW.GE.NPROW) .OR. (MYPCOL.GE.NPCOL) ) GOTO 30
*
*     Get my process ID from my grid coordinates
*
      ICALLER = BLACS_PNUM(CONTXT, MYPROW, MYPCOL)
*
*     If I am process {0,0}, receive check-in messages from
*     all nodes
*
      IF ( (MYPROW.EQ.0) .AND. (MYPCOL.EQ.0) ) THEN

         WRITE(*,*) ' '
         DO 20 I = 0, NPROW-1
	    DO 10 J = 0, NPCOL-1

	       IF ( (I.NE.0) .OR. (J.NE.0) ) THEN
		  CALL IGERV2D(CONTXT, 1, 1, ICALLER, 1, I, J) 
               ENDIF
*
*              Make sure ICALLER is where we think in process grid
*
               CALL BLACS_PCOORD(CONTXT, ICALLER, HISROW, HISCOL)
               IF ( (HISROW.NE.I) .OR. (HISCOL.NE.J) ) THEN
                  WRITE(*,*) 'Grid error!  Halting . . .'
                  STOP
               END IF
	       WRITE(*, 3000) I, J, ICALLER

10          CONTINUE
20       CONTINUE
         WRITE(*,*) ' '
         WRITE(*,*) 'All processes checked in.  Run finished.'
*
*     All processes but {0,0} send process ID as a check-in
*
      ELSE
	 CALL IGESD2D(CONTXT, 1, 1, ICALLER, 1, 0, 0)
      END IF

30    CONTINUE

      if ( loop == 3 ) then
        CALL BLACS_EXIT(.false.)
      else
        CALL BLACS_EXIT(.true.)
      end if
      end do

1000  FORMAT('How many processes in machine?')
2000  FORMAT(i4)
3000  FORMAT('Process {',i2,',',i2,'} (node number =',I0,
     $       ') has checked in.')

      STOP
      END

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.
@langou
Copy link
Contributor

langou commented Aug 20, 2021

Thanks Nick!

@langou langou merged commit 1d2d42d into Reference-ScaLAPACK:master Aug 20, 2021
@julielangou julielangou added this to the 2.2 release milestone Feb 2, 2022
@zerothi zerothi deleted the exit-clean-up branch February 3, 2022 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants