Skip to content

Commit

Permalink
Fortran example using MPI and Kokkos
Browse files Browse the repository at this point in the history
  • Loading branch information
anagainaru committed Jan 22, 2024
1 parent 519be77 commit 71ad750
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 2 deletions.
29 changes: 27 additions & 2 deletions examples/hello/bpStepsWriteReadKokkos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ if(NOT TARGET adios2_core)
set(CMAKE_CXX_COMPILER "${Kokkos_CXX_COMPILER}")
endif()

include(CheckLanguage)
check_language(Fortran)
if(CMAKE_Fortran_COMPILER)
enable_language(Fortran)
endif()
if(CMAKE_Fortran_COMPILER_LOADED)
list(APPEND _components Fortran)
endif()

find_package(MPI QUIET COMPONENTS ${_components})
if(MPI_FOUND)
# Workaround for various MPI implementations forcing the link of C++ bindings
Expand All @@ -33,22 +42,38 @@ else()
endif()
endif()

# flcl package needed for the Kokkos Fortran example
find_package(flcl QUIET)

# C++ Kokkos example using default layouts and different memory spaces
add_executable(adios2_hello_bpStepsWriteReadKokkos bpStepsWriteReadKokkos.cpp)
kokkos_compilation(SOURCE bpStepsWriteReadKokkos.cpp)
if(ADIOS2_HAVE_MPI)
target_link_libraries(adios2_hello_bpStepsWriteReadKokkos adios2::cxx11_mpi MPI::MPI_C Kokkos::kokkos)
else()
target_link_libraries(adios2_hello_bpStepsWriteReadKokkos adios2::cxx11 Kokkos::kokkos)
endif()
install(TARGETS adios2_hello_bpStepsWriteReadKokkos RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(TARGETS adios2_hello_bpStepsWriteReadKokkos RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

# C++ Kokkos example using default memory space and layouts
add_executable(adios2_hello_bpWriteReadKokkosView bpWriteReadKokkosView.cpp)
kokkos_compilation(SOURCE bpStepsWriteReadKokkos.cpp)
if(ADIOS2_HAVE_MPI)
target_link_libraries(adios2_hello_bpWriteReadKokkosView adios2::cxx11_mpi MPI::MPI_C Kokkos::kokkos)
else()
target_link_libraries(adios2_hello_bpWriteReadKokkosView adios2::cxx11 Kokkos::kokkos)
endif()
install(TARGETS adios2_hello_bpWriteReadKokkosView RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(TARGETS adios2_hello_bpWriteReadKokkosView RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

# Fortran Kokkos example using default layouts and different memory spaces
if(ADIOS2_HAVE_MPI AND ADIOS2_HAVE_Fortran AND flcl_FOUND)
add_executable(adios2_hello_bpStepsWriteReadKokkos_f bpStepsWriteReadKokkos.F90 view-f.f90 view-cxx.cc)
target_link_libraries(adios2_hello_bpStepsWriteReadKokkos_f adios2::fortran_mpi MPI::MPI_Fortran flcl::flcl)
if (CMAKE_Fortran_COMPILER_ID STREQUAL "XL")
target_link_options(adios2_hello_bpStepsWriteReadKokkos_f PRIVATE LINKER:-lxlf90_r)
endif()
if (CMAKE_Fortran_COMPILER_ID STREQUAL "Intel" OR CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
set_target_properties(adios2_hello_bpStepsWriteReadKokkos_f PROPERTIES LINKER_LANGUAGE Fortran)
endif()
install(TARGETS adios2_hello_bpStepsWriteReadKokkos_f RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
80 changes: 80 additions & 0 deletions examples/hello/bpStepsWriteReadKokkos/bpStepsWriteReadKokkos.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
program TestBPWriteReadHeatMap2D use mpi use adios2

implicit none

integer(kind = 8)::sum_i1,
sum_i2 type(adios2_adios)::adios type(adios2_io)::ioPut, ioGet type(adios2_engine)::bpWriter,
bpReader type(adios2_variable), dimension(1)::var_g,
var_gIn

integer(kind = 2),
dimension(
:,
:),
allocatable ::g, &sel_g integer(kind = 8),
dimension(2)::ishape, istart, icount integer(kind = 8),
dimension(2)::sel_start, sel_count integer ::ierr, irank, isize,
step_status integer ::in1, in2 integer ::i1,
i2

call MPI_INIT(ierr) call MPI_COMM_RANK(MPI_COMM_WORLD, irank, ierr) call
MPI_COMM_SIZE(MPI_COMM_WORLD, isize, ierr)

in1 = 3 in2 = 4 icount = (/ in1, in2 /) istart = (/ 0, in2 *irank /) ishape = (/ in1,
in2 *isize /)

allocate(g(in1, in2)) do i2 = 1,
in2 do i1 = 1,
in1 g(i1, i2) = irank + i1 end do end do

!Start adios2 Writer call adios2_init(adios, MPI_COMM_WORLD, ierr) call
adios2_declare_io(ioPut, adios, 'WriteIO', ierr)

call adios2_define_variable(var_g(1), ioPut, &'bpFloats',
adios2_type_integer2, &2, ishape, istart,
icount, &adios2_constant_dims, ierr)

call
adios2_open(bpWriter, ioPut, 'BPFortranKokkos.bp', adios2_mode_write, &ierr)

call adios2_put(bpWriter, var_g(1), g, ierr)

call adios2_close(bpWriter, ierr)

if (allocated(g)) deallocate(g)

!Start adios2 Reader in rank 0 if (irank == 0) then

call adios2_declare_io(ioGet, adios, 'ReadIO', ierr)

call adios2_open(bpReader, ioGet, 'BPFortranKokkos.bp',
&adios2_mode_read, MPI_COMM_SELF, ierr)

call
adios2_begin_step(bpReader, adios2_step_mode_read, -1., &step_status, ierr)

call adios2_inquire_variable(var_gIn(1), ioGet, &'bpFloats', ierr)

sel_start = (/ 0, 0 /) sel_count = (/ ishape(1), ishape(2) /)

allocate(sel_g(ishape(1), ishape(2))) sel_g = 0

call adios2_set_selection(var_gIn(1), 2, sel_start, sel_count, &ierr) call
adios2_get(bpReader, var_gIn(1), sel_g, ierr)

call adios2_end_step(bpReader, ierr)

call adios2_close(bpReader, ierr)

do i2 = 1,
INT(sel_count(2), 4) do i1 = 1,
INT(sel_count(1), 4) write(6, "(i8)", advance = "no") sel_g(i1, i2) end do write(6, *) end
do

if (allocated(sel_g)) deallocate(sel_g)

end if

call adios2_finalize(adios, ierr) call MPI_Finalize(ierr)

end program TestBPWriteReadHeatMap2D
41 changes: 41 additions & 0 deletions examples/hello/bpStepsWriteReadKokkos/view-cxx.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "flcl-cxx.hpp"
#include <Kokkos_Core.hpp>

using view_type = flcl::view_i32_2d_t;

extern "C" {

void c_init_view(view_type **v_x, int *val)
{
using flcl::view_from_ndarray;

view_type x = **v_x;
int d_val = *val;
Kokkos::parallel_for("initBuffer", x.extent(1), KOKKOS_LAMBDA(const size_t idy) {
for (size_t idx = 0; idx < x.extent(0); idx++)
x(idx, idy) = d_val + idx;
});
Kokkos::fence();

return;
}

void c_print_view(view_type **v_x)
{
using flcl::view_from_ndarray;

view_type x = **v_x;

Kokkos::parallel_for("printBuffer", 1, KOKKOS_LAMBDA(const size_t id) {
for (size_t idx = 0; idx < x.extent(0); idx++)
{
for (size_t idy = 0; idy < x.extent(1); idy++)
Kokkos::printf("%d ", x(idx, idy));
Kokkos::printf("\n");
}
});
Kokkos::fence();

return;
}
}
39 changes: 39 additions & 0 deletions examples/hello/bpStepsWriteReadKokkos/view-f.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module view_f_mod use, intrinsic ::iso_c_binding use,
intrinsic ::iso_fortran_env

use ::flcl_mod

implicit none

public

interface subroutine f_init_view(x, val) &
&bind(c, name = 'c_init_view') use,
intrinsic ::iso_c_binding use ::flcl_mod type(c_ptr), intent(in)::x integer(c_int),
intent(in)::val end subroutine f_init_view

subroutine f_print_view(x) &
&bind(c, name = 'c_print_view') use,
intrinsic ::iso_c_binding use ::flcl_mod type(c_ptr),
intent(in)::x end subroutine f_print_view end interface

contains

subroutine init_view(x, val) use,
intrinsic ::iso_c_binding use ::flcl_mod implicit none type(view_i32_2d_t),
intent(inout)::x integer(c_int),
intent(in)::val

call f_init_view(x % ptr(), val)

end subroutine init_view

subroutine print_view(x) use,
intrinsic ::iso_c_binding use ::flcl_mod implicit none type(view_i32_2d_t),
intent(in)::x

call f_print_view(x % ptr())

end subroutine print_view

end module view_f_mod

0 comments on commit 71ad750

Please sign in to comment.