program test #include "petsc/finclude/petscsys.h" use petscsys implicit none MPI_Comm :: SUBCOMM PetscMPIInt :: size, rank, subrank, subsize, nproc_per_sub PetscMPIInt :: nsubs, sub PetscViewer :: subOutput PetscErrorCode :: ioerr, ierr character(len=200) :: cstring, ologfile, seed character(len=12) :: x1 PetscMPIInt, parameter :: i0=0 ! Initialize Petsc call PetscInitialize(PETSC_NULL_CHARACTER,ierr) ioerr = 0 nsubs = 2 seed='tst' x1='01' call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr) call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr) if (mod(size, nsubs) /= 0) then cstring='The total number of MPI processes is not a multiple of nsubcomm\n\n' call PetscPrintf(PETSC_COMM_WORLD,TRIM(cstring),ierr) ioerr = 1 goto 999 end if ! Initiate sub-communicators nsubs = min(size,nsubs) nproc_per_sub = size / nsubs sub = min(rank / nproc_per_sub, nsubs - 1) ! Create subcommunicator and get team ranks call MPI_Comm_split(PETSC_COMM_WORLD, sub, i0, SUBCOMM, ierr) call MPI_Comm_rank(SUBCOMM,subrank,ierr) call MPI_Comm_size(SUBCOMM,subsize,ierr) if (nsubs < 10) then write(x1,'(I1.1)') sub+1 elseif (nsubs < 100) then write(x1,'(I2.2)') sub+1 elseif (nsubs < 1000) then write(x1,'(I3.3)') sub+1 else write(x1,'(I10)') sub+1 end if ologfile=TRIM(seed)//'_subcomm_'//TRIM(ADJUSTL(x1))//'.log' call PetscViewerASCIIOpen(SUBCOMM,TRIM(ologfile),subOutput,ierr) call MPI_Allreduce(ierr, ioerr, 1, MPIU_INTEGER, MPI_SUM, PETSC_COMM_WORLD, ierr) if (ioerr /= 0) then cstring='Unable to open log files, please check folder/file permissions\n\n' call PetscPrintf(PETSC_COMM_WORLD,TRIM(cstring),ierr) goto 999 end if 999 continue ! finalize petsc call PetscFinalize(ierr) end program test