From 6f5ed31b7b73c6e9689348953886e048c5320c99 Mon Sep 17 00:00:00 2001 From: Slavko Brdar Date: Tue, 31 Oct 2023 16:31:51 +0100 Subject: [PATCH] 1) add a unit test for Fortran API redistribution; 2) fix compile bugs (tnx Willem) --- .../detail/RedistributionInterface.cc | 19 ++++-- .../detail/RedistributionInterface.h | 42 +++--------- .../atlas_Redistribution_module.F90 | 25 +++++-- src/tests/redistribution/CMakeLists.txt | 9 +++ .../redistribution/fctest_redistribution.F90 | 67 +++++++++++++++++++ 5 files changed, 119 insertions(+), 43 deletions(-) create mode 100644 src/tests/redistribution/fctest_redistribution.F90 diff --git a/src/atlas/redistribution/detail/RedistributionInterface.cc b/src/atlas/redistribution/detail/RedistributionInterface.cc index cc0e0d2c4..ac010842c 100644 --- a/src/atlas/redistribution/detail/RedistributionInterface.cc +++ b/src/atlas/redistribution/detail/RedistributionInterface.cc @@ -10,12 +10,13 @@ #include +#include "RedistributionImpl.h" #include "RedistributionInterface.h" -//#include "atlas/functionspace/FunctionSpace.h" -#include "atlas/functionspace/detail/FunctionSpaceImpl.h" +#include "atlas/redistribution/detail/RedistributionImplFactory.h" namespace atlas { +namespace redistribution { // ---------------------------------------------------------------------------- // Fortran interfaces @@ -23,13 +24,21 @@ namespace atlas { extern "C" { -const Redistribution* atlas__Redistribution__new( - const functionspace::FunctionSpaceImpl* fspace1, const functionspace::FunctionSpaceImpl* fspace2) { - return new Redistribution(fspace1, fspace2); +detail::RedistributionImpl* atlas__Redistribution__new__config( + const functionspace::FunctionSpaceImpl* fspace1, const functionspace::FunctionSpaceImpl* fspace2, + const eckit::Configuration* config) { + ATLAS_ASSERT(config != nullptr); + std::string type; + config->get("type", type); + auto redist = redistribution::detail::RedistributionImplFactory::build(type); + redist->setup(fspace1, fspace2); + return redist; } + } // ---------------------------------------------------------------------------- +} // namespace redistribution } // namespace atlas diff --git a/src/atlas/redistribution/detail/RedistributionInterface.h b/src/atlas/redistribution/detail/RedistributionInterface.h index 0518d4273..e1fe3504c 100644 --- a/src/atlas/redistribution/detail/RedistributionInterface.h +++ b/src/atlas/redistribution/detail/RedistributionInterface.h @@ -9,53 +9,31 @@ */ #pragma once -//#include "atlas/functionspace/detail/FunctionSpaceImpl.h" -#include "atlas/redistribution/Redistribution.h" +namespace eckit { +class Configuration; +} namespace atlas { namespace functionspace{ class FunctionSpaceImpl; } -namespace field { -class FieldSetImpl; -} -namespace grid { -class DistributionImpl; -} -} // namespace atlas - -namespace atlas { -namespace grid { -namespace detail { -namespace grid { -class Grid; -} // namespace grid -} // namespace detail -} // namespace grid -using GridImpl = grid::detail::grid::Grid; } // namespace atlas namespace atlas { -namespace grid { +namespace redistribution { namespace detail { -namespace partitioner { -class Partitioner; -} // namespace partitioner -} // namespace detail -} // namespace grid -using PartitionerImpl = grid::detail::partitioner::Partitioner; -} // namespace atlas - - -namespace atlas { +class RedistributionImpl; +} // ------------------------------------------------------------------- // C wrapper interfaces to C++ routines extern "C" { -const Redistribution* atlas__Redistribution__new( - const functionspace::FunctionSpaceImpl* fspace1, const functionspace::FunctionSpaceImpl* fspace2); +detail::RedistributionImpl* atlas__Redistribution__new__config( + const functionspace::FunctionSpaceImpl* fspace1, const functionspace::FunctionSpaceImpl* fspace2, + const eckit::Configuration* config); } +} // namespace redistribution } // namespace atlas diff --git a/src/atlas_f/redistribution/atlas_Redistribution_module.F90 b/src/atlas_f/redistribution/atlas_Redistribution_module.F90 index 08005e659..c67d1a3d4 100644 --- a/src/atlas_f/redistribution/atlas_Redistribution_module.F90 +++ b/src/atlas_f/redistribution/atlas_Redistribution_module.F90 @@ -11,6 +11,7 @@ module atlas_Redistribution_module use, intrinsic :: iso_c_binding, only : c_ptr +use atlas_config_module, only : atlas_Config use atlas_functionspace_module, only : atlas_FunctionSpace use fckit_owned_object_module, only: fckit_owned_object @@ -49,8 +50,8 @@ module atlas_Redistribution_module !------------------------------------------------------------------------------ interface atlas_Redistribution - module procedure atlas_Redistribution__cptr - module procedure atlas_Redistribution__ctor + module procedure ctor_cptr + module procedure ctor_create end interface private :: c_ptr @@ -62,7 +63,13 @@ module atlas_Redistribution_module ! ----------------------------------------------------------------------------- ! Redistribution routines -function atlas_Redistribution__cptr( cptr ) result(this) +function empty_config() result(config) + type(atlas_Config) :: config + config = atlas_Config() + call config%return() +end function + +function ctor_cptr( cptr ) result(this) use atlas_redistribution_c_binding type(atlas_Redistribution) :: this type(c_ptr), intent(in) :: cptr @@ -70,11 +77,17 @@ function atlas_Redistribution__cptr( cptr ) result(this) call this%return() end function -function atlas_Redistribution__ctor( fspace1, fspace2 ) result(this) +function ctor_create( fspace1, fspace2, redist_name ) result(this) use atlas_redistribution_c_binding - type(atlas_FunctionSpace), intent(in) :: fspace1, fspace2 + class(atlas_FunctionSpace), intent(in) :: fspace1, fspace2 + character(len=*), intent(in), optional :: redist_name type(atlas_Redistribution) :: this - call this%reset_c_ptr( atlas__Redistribution__new(fspace1%CPTR_PGIBUG_A, fspace2%CPTR_PGIBUG_A) ) + type(atlas_Config) :: config + config = empty_config() + call config%set("type", "RedistributeGeneric") + if (present(redist_name)) call config%set("type", redist_name) + call this%reset_c_ptr( atlas__Redistribution__new__config(fspace1%CPTR_PGIBUG_A, fspace2%CPTR_PGIBUG_A, config%CPTR_PGIBUG_B) ) + call config%final() call this%return() end function diff --git a/src/tests/redistribution/CMakeLists.txt b/src/tests/redistribution/CMakeLists.txt index 0a3a5a836..e188b2d8b 100644 --- a/src/tests/redistribution/CMakeLists.txt +++ b/src/tests/redistribution/CMakeLists.txt @@ -7,6 +7,15 @@ if( atlas_HAVE_ATLAS_FUNCTIONSPACE ) +add_fctest( TARGET atlas_fctest_redistribution + MPI 4 + CONDITION eckit_HAVE_MPI + LINKER_LANGUAGE Fortran + SOURCES fctest_redistribution.F90 + LIBS atlas_f + ENVIRONMENT ${ATLAS_TEST_ENVIRONMENT} +) + ecbuild_add_test( TARGET atlas_test_redistribution_structured SOURCES test_redistribution_structured.cc MPI 8 diff --git a/src/tests/redistribution/fctest_redistribution.F90 b/src/tests/redistribution/fctest_redistribution.F90 new file mode 100644 index 000000000..dfa02a29e --- /dev/null +++ b/src/tests/redistribution/fctest_redistribution.F90 @@ -0,0 +1,67 @@ +! (C) Copyright 2013 ECMWF. +! This software is licensed under the terms of the Apache Licence Version 2.0 +! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. +! In applying this licence, ECMWF does not waive the privileges and immunities +! granted to it by virtue of its status as an intergovernmental organisation nor +! does it submit to any jurisdiction. + +! This File contains Unit Tests for testing the +! C++ / Fortran Interfaces to the State Datastructure +! +! @author Willem Deconinck +! @author Slavko Brdar + +#include "fckit/fctest.h" + +! ----------------------------------------------------------------------------- + +module fcta_Redistribution_fxt +use atlas_module +use, intrinsic :: iso_c_binding +implicit none +character(len=1024) :: msg +contains + +end module + +! ----------------------------------------------------------------------------- + +TESTSUITE_WITH_FIXTURE(fcta_Redistribution,fcta_Redistribution_fxt) + +! ----------------------------------------------------------------------------- + +TESTSUITE_INIT + call atlas_library%initialise() +END_TESTSUITE_INIT + +! ----------------------------------------------------------------------------- + +TESTSUITE_FINALIZE + call atlas_library%finalise() +END_TESTSUITE_FINALIZE + +TEST( test_resitribution ) +use atlas_module +use atlas_redistribution_module + +implicit none +type(atlas_StructuredGrid) :: grid +type(atlas_functionspace_StructuredColumns) :: fspace1, fspace2 +type(atlas_Redistribution) :: redist +type(c_ptr) :: cptr +grid = atlas_StructuredGrid("O16") +fspace1 = atlas_functionspace_StructuredColumns(grid, atlas_Partitioner("equal_regions")) +fspace2 = atlas_functionspace_StructuredColumns(grid, atlas_Partitioner("regular_bands")) + +redist = atlas_Redistribution(fspace1, fspace2) + +call redist%final() +call fspace2%final() +call fspace1%final() +call grid%final() +END_TEST + +! ----------------------------------------------------------------------------- + +END_TESTSUITE +