Skip to content

Commit

Permalink
Fortran: detect the availability of command line arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuck Atkins committed Oct 22, 2020
1 parent eef9a70 commit 2dce6b4
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 35 deletions.
22 changes: 22 additions & 0 deletions cmake/DetectOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ endif()
if(CMAKE_Fortran_COMPILER_LOADED)
set(ADIOS2_HAVE_Fortran TRUE)
list(APPEND mpi_find_components Fortran)

include(CheckFortranSourceCompiles)
check_fortran_source_compiles("
program testargs
integer :: n
character(len=256) :: v
n = command_argument_count()
call get_command_argument(0, v)
end program testargs"
ADIOS2_HAVE_FORTRAN_F03_ARGS
SRC_EXT F90
)
check_fortran_source_compiles("
program testargs
integer :: n
character(len=256) :: v
n = iargc()
call getarg(0, v)
end program testargs"
ADIOS2_HAVE_FORTRAN_GNU_ARGS
SRC_EXT F90
)
endif()

# MPI
Expand Down
4 changes: 4 additions & 0 deletions examples/hello/insituMPI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

if(ADIOS2_HAVE_Fortran)
add_library(helloInsituArgs OBJECT helloInsituArgs.F90)
target_compile_definitions(helloInsituArgs PRIVATE
$<$<BOOL:${ADIOS2_HAVE_FORTRAN_F03_ARGS}>:ADIOS2_HAVE_FORTRAN_F03_ARGS>
$<$<BOOL:${ADIOS2_HAVE_FORTRAN_GNU_ARGS}>:ADIOS2_HAVE_FORTRAN_GNU_ARGS>
)

add_executable(hello_insituMPIWriter_f
helloInsituMPIWriter.f90
Expand Down
30 changes: 17 additions & 13 deletions examples/hello/insituMPI/helloInsituArgs.F90
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ end subroutine usage
!!***************************
subroutine processArgs(rank, nproc, isWriter)

#if defined(_CRAYFTN) || !defined(__GFORTRAN__) && !defined(__GNUC__)
interface
integer function iargc()
end function iargc
end interface
#if defined(ADIOS2_HAVE_FORTRAN_F03_ARGS)
# define ADIOS2_ARGC() command_argument_count()
# define ADIOS2_ARGV(i, v) call get_command_argument(i, v)
#elif defined(ADIOS2_HAVE_FORTRAN_GNU_ARGS)
# define ADIOS2_ARGC() iargc()
# define ADIOS2_ARGV(i, v) call getarg(i, v)
#else
# define ADIOS2_ARGC() 1
# define ADIOS2_ARGV(i, v)
#endif

integer, intent(in) :: rank
Expand All @@ -57,20 +61,20 @@ end function iargc
endif

!! process arguments
numargs = iargc()
numargs = ADIOS2_ARGC()
!print *,"Number of arguments:",numargs
if ( numargs < expargs ) then
call usage(isWriter)
call exit(1)
endif
call getarg(1, xmlfile)
call getarg(2, npx_str)
call getarg(3, npy_str)
ADIOS2_ARGV(1, xmlfile)
ADIOS2_ARGV(2, npx_str)
ADIOS2_ARGV(3, npy_str)
if (isWriter) then
call getarg(4, ndx_str)
call getarg(5, ndy_str)
call getarg(6, steps_str)
call getarg(7, time_str)
ADIOS2_ARGV(4, ndx_str)
ADIOS2_ARGV(5, ndy_str)
ADIOS2_ARGV(6, steps_str)
ADIOS2_ARGV(7, time_str)
endif
read (npx_str,'(i5)') npx
read (npy_str,'(i5)') npy
Expand Down
8 changes: 8 additions & 0 deletions testing/adios2/engine/staging-common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,17 @@ if(ADIOS2_HAVE_Fortran)
TestCommonReadF.F90
$<TARGET_OBJECTS:TestCommonData_f>
)
target_compile_definitions(TestCommonRead_f PRIVATE
$<$<BOOL:${ADIOS2_HAVE_FORTRAN_F03_ARGS}>:ADIOS2_HAVE_FORTRAN_F03_ARGS>
$<$<BOOL:${ADIOS2_HAVE_FORTRAN_GNU_ARGS}>:ADIOS2_HAVE_FORTRAN_GNU_ARGS>
)
set_target_properties(TestCommonWrite_f TestCommonRead_f PROPERTIES
LINKER_LANGUAGE Fortran
)
target_compile_definitions(TestCommonWrite_f PRIVATE
$<$<BOOL:${ADIOS2_HAVE_FORTRAN_F03_ARGS}>:ADIOS2_HAVE_FORTRAN_F03_ARGS>
$<$<BOOL:${ADIOS2_HAVE_FORTRAN_GNU_ARGS}>:ADIOS2_HAVE_FORTRAN_GNU_ARGS>
)
if(ADIOS2_HAVE_MPI)
target_link_libraries(TestCommonWrite_f adios2_fortran_mpi MPI::MPI_Fortran)
target_link_libraries(TestCommonRead_f adios2_fortran_mpi MPI::MPI_Fortran)
Expand Down
24 changes: 13 additions & 11 deletions testing/adios2/engine/staging-common/TestCommonReadF.F90
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ program TestSstRead
use adios2
implicit none

#ifndef __GFORTRAN__
#ifndef __GNUC__
interface
integer function iargc()
end function iargc
end interface
#endif
#if defined(ADIOS2_HAVE_FORTRAN_F03_ARGS)
# define ADIOS2_ARGC() command_argument_count()
# define ADIOS2_ARGV(i, v) call get_command_argument(i, v)
#elif defined(ADIOS2_HAVE_FORTRAN_GNU_ARGS)
# define ADIOS2_ARGC() iargc()
# define ADIOS2_ARGV(i, v) call getarg(i, v)
#else
# define ADIOS2_ARGC() 1
# define ADIOS2_ARGV(i, v)
#endif

integer :: numargs
Expand All @@ -43,18 +45,18 @@ end function iargc
integer(kind = 8), dimension(:), allocatable::shape_in
integer::key, color

numargs = iargc()
numargs = ADIOS2_ARGC()

if ( numargs < 2 ) then
call usage()
call exit(1)
endif


call getarg(1, engine)
call getarg(2, filename)
ADIOS2_ARGV(1, engine)
ADIOS2_ARGV(2, filename)
if ( numargs > 2 ) then
call getarg(3, params)
ADIOS2_ARGV(3, params)
endif

insteps = 0;
Expand Down
26 changes: 15 additions & 11 deletions testing/adios2/engine/staging-common/TestCommonWriteF.F90
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ program TestSstWrite
use adios2
implicit none

#ifndef __GFORTRAN__
#ifndef __GNUC__
interface
integer function iargc()
end function iargc
end interface
#endif
#if defined(ADIOS2_HAVE_FORTRAN_F03_ARGS)
# define ADIOS2_ARGC() command_argument_count()
# define ADIOS2_ARGV(i, v) call get_command_argument(i, v)
#elif defined(ADIOS2_HAVE_FORTRAN_GNU_ARGS)
# define ADIOS2_ARGC() iargc()
# define ADIOS2_ARGV(i, v) call getarg(i, v)
#else
# define ADIOS2_ARGC() 1
# define ADIOS2_ARGV(i, v)
#endif

integer :: numargs
Expand All @@ -42,17 +44,17 @@ end function iargc
integer(kind = 8)::localtime
integer::color, key, testComm

numargs = iargc()
numargs = ADIOS2_ARGC()
if ( numargs < 2 ) then
call usage()
call exit(1)
endif


call getarg(1, engine)
call getarg(2, filename)
ADIOS2_ARGV(1, engine)
ADIOS2_ARGV(2, filename)
if ( numargs > 2 ) then
call getarg(3, params)
ADIOS2_ARGV(3, params)
endif

#if ADIOS2_USE_MPI
Expand Down Expand Up @@ -184,8 +186,10 @@ end function iargc
call adios2_put(sstWriter, variables(6), data_R64, ierr)
call adios2_put(sstWriter, variables(7), data_R64_2d, ierr)
call adios2_put(sstWriter, variables(8), data_R64_2d_rev, ierr)
#if !defined(_CRAYFTN)
localtime = 0 ! should be time(), but non-portable and value is unused
call adios2_put(sstWriter, variables(9), loc(localtime), ierr)
#endif
call adios2_put(sstWriter, variables(10), data_C32, ierr)
call adios2_put(sstWriter, variables(11), data_C64, ierr)
call adios2_end_step(sstWriter, ierr)
Expand Down

0 comments on commit 2dce6b4

Please sign in to comment.