program test_gridinit

implicit none

integer :: ierr, rank, nprocs, ictxt,sqrtnp, nprow, npcol,i 
real(8) :: t0,t1

call blacs_pinfo(rank,nprocs )     
 
! get grid size:
sqrtnp = int( sqrt( dble(nprocs) ) + 1 )
do i=1,sqrtnp
    if(mod(nprocs,i).eq.0) nprow = i
end do
npcol = nprocs/nprow   

! get blacs context and initialize grid :  
call blacs_get(0,0,ictxt)              
call cpu_time(t0)  
call blacs_gridinit(ictxt,'r',nprow,npcol)  
call cpu_time(t1) 

if (rank == 0) write(*,'(a40,3(i6,1x),1x,f8.3)') ' nprocs,nprow,npcol,blacs_gridinit (s): ',nprocs,nprow,npcol,t1    

call blacs_barrier(ictxt,'All') 

end program
