!--------------------------------------------
!CAF=/usr/lib/x86_64-linux-gnu/open-coarrays/openmpi/lib  (locate libcaf_mpi) 
!mpifort -fcoarray=lib 02_blacsCAF.f90  -lscalapack-openmpi -L$CAF -lcaf_mpi -o m ; mpirun -n 4 ./m | sort
!                                                                                   mpirun -n 9 ./m | sort
!                                                                                   mpirun -n 16  --host $(hostname):16 ./m | sort
! If too many processes, then:   
!  n=4;  mpirun -n $n --host $(hostname):$n ./m | 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
 integer            :: myimg , nimg

 call blacs_pinfo(myrank,nprocs)

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

 if(nprocs /= nprow * npcol) error stop

 call blacs_get(-1,0, context)
 call blacs_gridinit(context,'Col-major', nprow, npcol)
 call blacs_gridinfo(context,nprow,npcol,myprow,mypcol)

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

 call blacs_barrier(context,'A') ! A = All, R = Row, C = Column
 
!Co-arrays do the work now... Calling blacs_exit results in an error, since one attampts to mpi_finalize twice.
!call blacs_exit(0)

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