!mpifort 01_blacs.f90 -lscalapack-openmpi -o c; mpirun -n 4 ./c | sort
!
!--------------------------------------------
program     test_scalapack
 use, intrinsic :: iso_fortran_env
 implicit none
 integer, parameter :: dp = real64
 integer            :: context
 integer            ::  nprow,  npcol, nprocs
 integer            :: myprow, mypcol, myrank
 logical            :: iamingrid

 call blacs_pinfo(myrank,nprocs)                        ! Get process information at runtime

 nprow = sqrt(nprocs+1.0e-6_dp); npcol = nprow
 print *,'00 ', myrank,nprocs, nprow, npcol

 if(nprocs /= nprow * npcol) error stop

 call blacs_get(-1,0, context)                          ! Initialize context
 call blacs_gridinit(context,'Col-major', nprow, npcol) !Grid creation: nprow x npcol  
 call blacs_gridinfo(context,nprow,npcol,myprow,mypcol) !Process coordinates: (myprow,mypcol)

 iamingrid = (myprow >=0) .and. (myprow < nprow) .and. &
             (mypcol >=0) .and. (mypcol < npcol)

 if(.not. iamingrid) then
  call blacs_exit(0)
  stop
 end if

 print *,'01', myrank,myprow,mypcol,nprocs,nprow,npcol

 call blacs_barrier(context,'A')                        ! sync: A = All, R = Row, C = Column
 
 call blacs_exit(0)                                     ! Shutdown parallel environment - compulsory

end program test_scalapack
!--------------------------------------------
