Reputation: 65
I am trying to execute this program using MPI that I have written in fortran:
Program CartesianGrid2D
Implicit None
Include 'mpif.h'
!----------------------------------------!
! Setting of the computational grid !
Integer, Parameter :: NDIM = 2 ! number of space dimensions
Integer, Parameter :: IMAX = 200 ! number of grid points in x-direction
Integer, Parameter :: JMAX = 200 ! number of grid points in y-direction
Real, Parameter :: x0=0.0 !min x-coord
Real, Parameter :: x1=1.0 !max x-coord
Real, Parameter :: y0=0.0 !min y-coord
Real, Parameter :: y1=1.0 !max y-coord
!----------------------------------------!
! Local variable declaration
TYPE tMPI
Integer :: myrank
Integer :: nCPU
Integer :: status(MPI_STATUS_SIZE)
Integer :: iStart, iEnd !idx of starting and ending cell in x-dir
Integer :: jStart, jEnd !idx of starting and ending cell in y-dir
Integer :: imax, jmax !number of cells within each rank
Integer, Allocatable :: mycoords(:) !point coords of the subgrid
Integer :: iErr !flag for errors in x-dir
Integer :: x_thread !number of CPUs in x-dir
Integer :: y_thread !number of CPUs in y-dir
End TYPE tMPI
TYPE(tMPI) :: MPI
Logical, Allocatable :: periods(:)
Integer, Allocatable :: dims(:)
Integer :: TCPU, BCPU, RCPU, LCPU !neighbor ranks of myrank
Integer :: i, j, idx, jdx, source
Integer :: COMM_CART !cartesian MPI communicator
Real :: dx, dy
Real, Allocatable :: x(:), y(:) ! grid coordinates
!----------------------------------------!
! 1) MPI initialization
CALL MPI_INIT(MPI%iErr)
CALL MPI_COMM_RANK(MPI_COMM_WORLD, MPI%myrank, MPI%iErr)
CALL MPI_COMM_SIZE(MPI_COMM_WORLD,MPI%nCPU,MPI%iErr)
! check the number of CPUs
If(MOD(MPI%nCPU,2).ne.0) then
print *, 'ERROR. Number of CPU must be even!'
CALL MPI_FINALIZE(MPI%iErr)
Stop
End if
CALL MPI_BARRIER(MPI_COMM_WORLD,MPI%iErr)
! 2) Create a Cartesian topology
! check the number of cells
If(MOD(IMAX,2).ne.0) then
print *, 'ERROR. Number of x-cells must be even!'
CALL MPI_FINALIZE(MPI%iErr)
Stop
End if
If(MOD(JMAX,2).ne.0) then
print *, 'ERROR. Number of y-cells must be even!'
CALL MPI_FINALIZE(MPI%iErr)
Stop
End if
! Domain decomposition
MPI%x_thread = MPI%nCPU/2
MPI%y_thread = MPI%nCPU - MPI%x_thread
Allocate(dims(NDIM), periods(NDIM), MPI%mycoords(NDIM))
dims = (/ MPI%x_thread, MPI%y_thread /)
periods = .FALSE.
CALL MPI_CART_CREATE(MPI_COMM_WORLD,NDIM,dims,periods,.TRUE.,COMM_CART,MPI%iErr)
CALL MPI_COMM_RANK(MPI_COMM_WORLD, MPI%myrank, MPI%iErr)
! 2.3) Find CPU neighbords
CALL MPI_CART_SHIFT(COMM_CART,0,1,source,RCPU,MPI%iErr)
CALL MPI_CART_SHIFT(COMM_CART,0,-1,source,LCPU,MPI%iErr)
CALL MPI_CART_SHIFT(COMM_CART,1,0,source,TCPU,MPI%iErr)
CALL MPI_CART_SHIFT(COMM_CART,-1,0,source,BCPU,MPI%iErr)
! coordinates of the subgrid
CALL MPI_CART_COORDS(COMM_CART,MPI%myrank,NDIM,MPI%mycoords,MPI%iErr)
MPI%imax = IMAX/MPI%x_thread
MPI%jmax = JMAX/MPI%y_thread
MPI%iStart = 1 + MPI%mycoords(1)*MPI%imax
MPI%iEnd = MPI%iStart + MPI%imax - 1
MPI%jStart = 1 + MPI%mycoords(2)*MPI%jmax
MPI%jEnd = MPI%jStart + MPI%jmax - 1
! 3) Comoute the real mesh
dx = (x1-x0)/Real(IMAX-1)
dy = (y1-y0)/Real(JMAX-1)
Allocate(x(MPI%IMAX))
Allocate(y(MPI%JMAX))
idx = 0
Do i = MPI%iStart, MPI%iEnd
idx = idx + 1
x(idx) = (x0-dx/2.) + (i-1)*dx
End do
jdx = 0
Do i = MPI%jStart, MPI%jEnd
jdx = jdx + 1
y(jdx) = (y0-dy/2.) + (j-1)*dy
End do
! 4) Plot the output and finalize the program
CALL ASCII_Output(x,y,MPI%imax,MPI%jmax,MPI%myrank)
CALL MPI_FINALIZE(MPI%iErr)
End program CartesianGrid2D
Subroutine ASCII_Output(x,y,imax,jmax,myrank)
!-----------------------------------------!
Implicit None
!-----------------------------------------!
Integer :: imax, jmax, myrank
Real :: x(imax), y(jmax)
Integer :: i, j, DataUnit
Character(len=10) :: cmyrank
Character(len=200) :: IOFileName
!-----------------------------------------!
Write(cmyrank,'(I4.4)') myrank
IOFileName = 'CartesianGrid_output-'//TRIM(cmyrank)//'.dat'
DataUnit = 100+myrank
Open(Unit=DataUnit, File=Trim(IOFileName), Status='Unknown', Action='Write')
Write(DataUnit,*) imax
Write(DataUnit,*) jmax
Do i = 1, imax
Write(DataUnit,*) x(i)
End do
Do j = 1, jmax
Write(DataUnit,*) y(j)
End do
Close(DataUnit)
End Subroutine ASCII_Output
However whenever I try to execute I have got this list of errors popping up:
Abort(795947788) on node 0 (rank 0 in comm 0): Fatal error in internal_Cart_create: Invalid argument, error stack:
internal_Cart_create(102): MPI_Cart_create(MPI_COMM_WORLD, ndims=2, dims=0x60000384c9f0, periods=0x60000384c9e0, reorder=1, comm_cart=0x16f8532a0) failed
MPIR_Cart_create_impl(43): Size of the communicator (6) is smaller than the size of the Cartesian topology (9)
Abort(863056652) on node 1 (rank 1 in comm 0): Fatal error in internal_Cart_create: Invalid argument, error stack:
internal_Cart_create(102): MPI_Cart_create(MPI_COMM_WORLD, ndims=2, dims=0x600003030760, periods=0x600003030860, reorder=1, comm_cart=0x16ef332a0) failed
MPIR_Cart_create_impl(43): Size of the communicator (6) is smaller than the size of the Cartesian topology (9)
Abort(460403468) on node 2 (rank 2 in comm 0): Fatal error in internal_Cart_create: Invalid argument, error stack:
internal_Cart_create(102): MPI_Cart_create(MPI_COMM_WORLD, ndims=2, dims=0x6000039647d0, periods=0x6000039647e0, reorder=1, comm_cart=0x16d05f2a0) failed
MPIR_Cart_create_impl(43): Size of the communicator (6) is smaller than the size of the Cartesian topology (9)
Abort(191968012) on node 3 (rank 3 in comm 0): Fatal error in internal_Cart_create: Invalid argument, error stack:
internal_Cart_create(102): MPI_Cart_create(MPI_COMM_WORLD, ndims=2, dims=0x600000b78470, periods=0x600000b784e0, reorder=1, comm_cart=0x16f75f2a0) failed
MPIR_Cart_create_impl(43): Size of the communicator (6) is smaller than the size of the Cartesian topology (9)
Abort(997274380) on node 4 (rank 4 in comm 0): Fatal error in internal_Cart_create: Invalid argument, error stack:
internal_Cart_create(102): MPI_Cart_create(MPI_COMM_WORLD, ndims=2, dims=0x6000039843e0, periods=0x6000039843d0, reorder=1, comm_cart=0x16ba532a0) failed
MPIR_Cart_create_impl(43): Size of the communicator (6) is smaller than the size of the Cartesian topology (9)
Abort(259076876) on node 5 (rank 5 in comm 0): Fatal error in internal_Cart_create: Invalid argument, error stack:
internal_Cart_create(102): MPI_Cart_create(MPI_COMM_WORLD, ndims=2, dims=0x6000016cab90, periods=0x6000016caaa0, reorder=1, comm_cart=0x16d3232a0) failed
MPIR_Cart_create_impl(43): Size of the communicator (6) is smaller than the size of the Cartesian topology (9)
What I first do is : mpif90 -cpp -lmpi NameOfTheProgram.f90
and then whenever I execute the a.out I do mpirun -np 6 ./a.out
Running this on a MacBook Air M1 (Whenever I have to run fortran I usually use a gfortran compiler).
Upvotes: 0
Views: 355
Reputation: 5810
Your computation
MPI%x_thread = MPI%nCPU/2
MPI%y_thread = MPI%nCPU - MPI%x_thread
makes no sense. As the error message indicates, the product of x_thread
and y_thread
is not equal to your communicator size.
Please use MPI_Dims_create
to set these parameters.
Upvotes: 1