Skip to content

Commit

Permalink
Restructure the offline driver.
Browse files Browse the repository at this point in the history
There is now only one executable being built: either cable-mpi or cable, depending if MPI support is available or not. When MPI support is available, the code will now automatically run the serial driver if using only one MPI process, instead of stopping with an error like before.
  • Loading branch information
micaeljtoliveira committed Nov 11, 2024
1 parent 17ed25c commit b2e57a7
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 53 deletions.
19 changes: 11 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ add_library(
src/offline/cable_plume_mip.F90
src/offline/cable_read.F90
src/offline/cable_site.F90
src/offline/cable_serial.F90
src/offline/cable_soil_params.F90
src/offline/cable_weathergenerator.F90
src/offline/cable_write.F90
Expand Down Expand Up @@ -158,22 +159,24 @@ if(CABLE_MPI)
target_link_libraries(cable_common PRIVATE MPI::MPI_Fortran)
endif()

add_executable(
cable
src/offline/cable_driver.F90
)
target_link_libraries(cable PRIVATE cable_common)
install(TARGETS cable RUNTIME)

if(CABLE_MPI)
add_executable(
cable-mpi
src/offline/cable_mpicommon.F90
src/offline/cable_mpidrv.F90
src/offline/cable_mpimaster.F90
src/offline/cable_mpiworker.F90
src/science/pop/pop_mpi.F90
src/offline/cable_offline_driver.F90
)
target_link_libraries(cable-mpi PRIVATE cable_common MPI::MPI_Fortran)
install(TARGETS cable-mpi RUNTIME)
else()
add_executable(
cable
src/offline/cable_mpimaster_stub.F90
src/offline/cable_mpiworker_stub.F90
src/offline/cable_offline_driver.F90
)
target_link_libraries(cable PRIVATE cable_common)
install(TARGETS cable RUNTIME)
endif()
32 changes: 32 additions & 0 deletions src/offline/cable_mpimaster_stub.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
!==============================================================================
! This source code is part of the
! Australian Community Atmosphere Biosphere Land Exchange (CABLE) model.
! This work is licensed under the CSIRO Open Source Software License
! Agreement (variation of the BSD / MIT License).
!
! You may not use this file except in compliance with this License.
! A copy of the License (CSIRO_BSD_MIT_License_v2.0_CABLE.txt) is located
! in each directory containing CABLE code.
!
! ==============================================================================
! Purpose: Stub for the master driver when MPI is not available.
!
! ==============================================================================
!
MODULE cable_mpimaster
IMPLICIT NONE

PRIVATE
PUBLIC :: mpidrv_master

CONTAINS

SUBROUTINE mpidrv_master(comm)
INTEGER, INTENT(IN) :: comm

! This should never be called!
STOP

END SUBROUTINE mpidrv_master

END MODULE cable_mpimaster
32 changes: 32 additions & 0 deletions src/offline/cable_mpiworker_stub.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
!==============================================================================
! This source code is part of the
! Australian Community Atmosphere Biosphere Land Exchange (CABLE) model.
! This work is licensed under the CSIRO Open Source Software License
! Agreement (variation of the BSD / MIT License).
!
! You may not use this file except in compliance with this License.
! A copy of the License (CSIRO_BSD_MIT_License_v2.0_CABLE.txt) is located
! in each directory containing CABLE code.
!
! ==============================================================================
! Purpose: Stub for the worker driver when MPI is not available.
!
! ==============================================================================
!
MODULE cable_mpiworker
IMPLICIT NONE

PRIVATE
PUBLIC :: mpidrv_worker

CONTAINS

SUBROUTINE mpidrv_worker(comm)
INTEGER, INTENT(IN) :: comm

! This should never be called!
STOP

END SUBROUTINE mpidrv_worker

END MODULE cable_mpiworker
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,10 @@
! A copy of the License (CSIRO_BSD_MIT_License_v2.0_CABLE.txt) is located
! in each directory containing CABLE code.
!
! ==============================================================================
! Purpose: Bare bones MPI driver for CABLE
!
! Contact: [email protected]
!
! History: MPI wrapper developed by Maciej Golebiewski (2012)
!
! ==============================================================================
!
PROGRAM mpi_driver
USE mpi
USE cable_mpi_mod, ONLY : mpi_grp_t, mpi_mod_init, mpi_mod_end, mpi_check_error
PROGRAM cable_offline_driver
USE cable_mpi_mod, ONLY : mpi_grp_t, mpi_mod_init, mpi_mod_end
USE cable_driver_init_mod

USE cable_serial
USE cable_mpimaster
USE cable_mpiworker

Expand All @@ -35,20 +25,19 @@ PROGRAM mpi_driver

CALL cable_driver_init(mpi_grp)

IF (mpi_grp%size < 2) THEN
WRITE(*,*) 'This program needs at least 2 processes to run!'
CALL mpi_grp%abort()
END IF

IF (mpi_grp%rank == 0) THEN
CALL mpidrv_master (mpi_grp%comm)
IF (mpi_grp%size == 1) THEN
CALL serialdrv()
ELSE
CALL mpidrv_worker (mpi_grp%comm)
IF (mpi_grp%rank == 0) THEN
CALL mpidrv_master(mpi_grp%comm)
ELSE
CALL mpidrv_worker(mpi_grp%comm)
END IF
END IF

CALL mpi_mod_end()

CALL CPU_TIME(etime)
PRINT *, 'Finished. ', etime, ' seconds needed for '

END PROGRAM mpi_driver
END PROGRAM cable_offline_driver
45 changes: 22 additions & 23 deletions src/offline/cable_driver.F90 → src/offline/cable_serial.F90
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@
! poolcnpOut.csv -- from CASA-CNP
!==============================================================================

PROGRAM cable_offline_driver
USE cable_mpi_mod, ONLY : mpi_grp_t, mpi_mod_init, mpi_mod_end
MODULE cable_serial
USE cable_driver_init_mod, ONLY : &
cable_driver_init, &
vegparmnew, &
Expand Down Expand Up @@ -147,6 +146,14 @@ PROGRAM cable_offline_driver
USE casa_offline_inout_module, ONLY : WRITE_CASA_RESTART_NC, WRITE_CASA_OUTPUT_NC
IMPLICIT NONE

PRIVATE
PUBLIC :: serialdrv

CONTAINS

SUBROUTINE serialdrv()


! CABLE namelist: model configuration, runtime/user switches
!CHARACTER(LEN=200), PARAMETER :: CABLE_NAMELIST='cable.nml'
! try to read in namelist from command line argument
Expand Down Expand Up @@ -254,7 +261,7 @@ PROGRAM cable_offline_driver
new_sumbal = 0.0, &
new_sumfpn = 0.0, &
new_sumfe = 0.0
!For consistency w JAC
!For consistency w JAC
REAL,ALLOCATABLE, SAVE :: c1(:,:)
REAL,ALLOCATABLE, SAVE :: rhoch(:,:)
REAL,ALLOCATABLE, SAVE :: xk(:,:)
Expand All @@ -263,26 +270,18 @@ PROGRAM cable_offline_driver
INTEGER :: ioerror
INTEGER :: count_bal = 0

! for landuse
integer mlon,mlat, mpx
real(r_2), dimension(:,:,:), allocatable, save :: luc_atransit
real(r_2), dimension(:,:), allocatable, save :: luc_fharvw
real(r_2), dimension(:,:,:), allocatable, save :: luc_xluh2cable
real(r_2), dimension(:), allocatable, save :: arealand
integer, dimension(:,:), allocatable, save :: landmask
integer, dimension(:), allocatable, save :: cstart,cend,nap
real(r_2), dimension(:,:,:), allocatable, save :: patchfrac_new

! mpi group info
TYPE(mpi_grp_t) :: mpi_grp
! for landuse
integer mlon,mlat, mpx
real(r_2), dimension(:,:,:), allocatable, save :: luc_atransit
real(r_2), dimension(:,:), allocatable, save :: luc_fharvw
real(r_2), dimension(:,:,:), allocatable, save :: luc_xluh2cable
real(r_2), dimension(:), allocatable, save :: arealand
integer, dimension(:,:), allocatable, save :: landmask
integer, dimension(:), allocatable, save :: cstart,cend,nap
real(r_2), dimension(:,:,:), allocatable, save :: patchfrac_new

! END header

CALL mpi_mod_init()
mpi_grp = mpi_grp_t()

CALL cable_driver_init(mpi_grp)

cable_runtime%offline = .TRUE.

! Open, read and close the consistency check file.
Expand Down Expand Up @@ -1234,9 +1233,7 @@ PROGRAM cable_offline_driver
CALL CPU_TIME(etime)
PRINT *, 'Finished. ', etime, ' seconds needed for ', kend,' hours'

CALL mpi_mod_end()

END PROGRAM cable_offline_driver
END SUBROUTINE serialdrv


SUBROUTINE prepareFiles(ncciy)
Expand Down Expand Up @@ -1372,3 +1369,5 @@ SUBROUTINE LUCdriver( casabiome,casapool, &
CALL POPLUC_weights_transfer(POPLUC,POP,LUC_EXPT)

END SUBROUTINE LUCdriver

END MODULE cable_serial

0 comments on commit b2e57a7

Please sign in to comment.