Skip to content

Commit

Permalink
1) fix Fortran API for Redistribution; 2) improve unit test (with Wil…
Browse files Browse the repository at this point in the history
…lem)
  • Loading branch information
sbrdar committed Nov 8, 2023
1 parent 51e11c2 commit 5a4354b
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/atlas/redistribution/Redistribution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ Redistribution::Redistribution(const FunctionSpace& sourceFunctionSpace, const F
}()) {}

void Redistribution::execute(const Field& source, Field& target) const {
get()->execute(source, target);
get()->execute(source.get(), target.get());
return;
}

void Redistribution::execute(const FieldSet& source, FieldSet& target) const {
get()->execute(source, target);
get()->execute(source.get(), target.get());
return;
}

Expand Down
32 changes: 17 additions & 15 deletions src/atlas/redistribution/detail/RedistributeGeneric.cc
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ void RedistributeGeneric::do_setup() {
std::tie(targetLocalIdx_, targetDisps_) = getUidIntersection(mpi_comm_, targetUidVec, sourceGlobalUids, sourceGlobalDisps);
}

void RedistributeGeneric::execute(const Field& sourceField, Field& targetField) const {
void RedistributeGeneric::execute(const field::FieldImpl* sf, field::FieldImpl* tf) const {
const field::FieldImpl& sourceField = *sf;
field::FieldImpl& targetField = *tf;
//Check functionspaces match.
ATLAS_ASSERT(sourceField.functionspace().type() == source().type());
ATLAS_ASSERT(targetField.functionspace().type() == target().type());
Expand All @@ -299,23 +301,23 @@ void RedistributeGeneric::execute(const Field& sourceField, Field& targetField)
}

// Perform redistribution.
do_execute(sourceField, targetField);
do_execute(sf, tf);
}

void RedistributeGeneric::execute(const FieldSet& sourceFieldSet, FieldSet& targetFieldSet) const {
void RedistributeGeneric::execute(const field::FieldSetImpl* sourceFieldSet, field::FieldSetImpl* targetFieldSet) const {
// Check field set sizes match.
ATLAS_ASSERT(sourceFieldSet.size() == targetFieldSet.size());
ATLAS_ASSERT(sourceFieldSet->size() == targetFieldSet->size());

// Redistribute fields.
for (idx_t i = 0; i < sourceFieldSet.size(); ++i) {
execute(sourceFieldSet[i], targetFieldSet[i]);
for (idx_t i = 0; i < sourceFieldSet->size(); ++i) {
execute(&sourceFieldSet[i], &targetFieldSet[i]);
}
}

// Determine datatype.
void RedistributeGeneric::do_execute(const Field& sourceField, Field& targetField) const {
void RedistributeGeneric::do_execute(const field::FieldImpl* sourceField, field::FieldImpl* targetField) const {
// Available datatypes defined in array/LocalView.cc
switch (sourceField.datatype().kind()) {
switch (sourceField->datatype().kind()) {
case array::DataType::KIND_REAL64: {
return do_execute<double>(sourceField, targetField);
}
Expand All @@ -329,16 +331,16 @@ void RedistributeGeneric::do_execute(const Field& sourceField, Field& targetFiel
return do_execute<int>(sourceField, targetField);
}
default: {
ATLAS_THROW_EXCEPTION("No implementation for data type " + sourceField.datatype().str());
ATLAS_THROW_EXCEPTION("No implementation for data type " + sourceField->datatype().str());
}
}
}

// Determine rank.
template <typename Value>
void RedistributeGeneric::do_execute(const Field& sourceField, Field& targetField) const {
void RedistributeGeneric::do_execute(const field::FieldImpl* sourceField, field::FieldImpl* targetField) const {
// Available ranks defined in array/LocalView.cc
switch (sourceField.rank()) {
switch (sourceField->rank()) {
case 1: {
return do_execute<Value, 1>(sourceField, targetField);
}
Expand Down Expand Up @@ -367,17 +369,17 @@ void RedistributeGeneric::do_execute(const Field& sourceField, Field& targetFiel
return do_execute<Value, 9>(sourceField, targetField);
}
default: {
ATLAS_THROW_EXCEPTION("No implementation for rank " + std::to_string(sourceField.rank()));
ATLAS_THROW_EXCEPTION("No implementation for rank " + std::to_string(sourceField->rank()));
}
}
}

// Perform redistribution.
template <typename Value, int Rank>
void RedistributeGeneric::do_execute(const Field& sourceField, Field& targetField) const {
void RedistributeGeneric::do_execute(const field::FieldImpl* sourceField, field::FieldImpl* targetField) const {
// Get array views.
auto sourceView = array::make_view<Value, Rank>(sourceField);
auto targetView = array::make_view<Value, Rank>(targetField);
auto sourceView = array::make_view<Value, Rank>(*sourceField);
auto targetView = array::make_view<Value, Rank>(*targetField);

const auto& comm = mpi::comm(mpi_comm_);
auto mpi_size = comm.size();
Expand Down
11 changes: 6 additions & 5 deletions src/atlas/redistribution/detail/RedistributeGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <string>

#include "atlas/field/detail/FieldImpl.h"
#include "atlas/redistribution/detail/RedistributionImpl.h"

namespace atlas {
Expand All @@ -23,19 +24,19 @@ class RedistributeGeneric : public RedistributionImpl {

void do_setup() override;

void execute(const Field& source, Field& target) const override;
void execute(const field::FieldImpl* source, field::FieldImpl* target) const override;

void execute(const FieldSet& source, FieldSet& target) const override;
void execute(const field::FieldSetImpl* source, field::FieldSetImpl* target) const override;

private:
// Determine datatype.
void do_execute(const Field& source, Field& target) const;
void do_execute(const field::FieldImpl* source, field::FieldImpl* target) const;
// Determine rank.
template <typename Value>
void do_execute(const Field& source, Field& target) const;
void do_execute(const field::FieldImpl* source, field::FieldImpl* target) const;
// Perform redistribution.
template <typename Value, int Rank>
void do_execute(const Field& source, Field& target) const;
void do_execute(const field::FieldImpl* source, field::FieldImpl* target) const;

// Local indices to send to each PE
std::vector<idx_t> sourceLocalIdx_{};
Expand Down
23 changes: 14 additions & 9 deletions src/atlas/redistribution/detail/RedistributeStructuredColumns.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ void RedistributeStructuredColumns::do_setup() {
return;
}

void RedistributeStructuredColumns::execute(const Field& sourceField, Field& targetField) const {
void RedistributeStructuredColumns::execute(const field::FieldImpl* sf, field::FieldImpl* tf) const {
const field::FieldImpl& sourceField = *sf;
field::FieldImpl& targetField = *tf;
// Assert that fields are defined on StructuredColumns.
ATLAS_ASSERT(functionspace::StructuredColumns(sourceField.functionspace()));
ATLAS_ASSERT(functionspace::StructuredColumns(targetField.functionspace()));
Expand Down Expand Up @@ -165,15 +167,18 @@ void RedistributeStructuredColumns::execute(const Field& sourceField, Field& tar
return;
}

void RedistributeStructuredColumns::execute(const FieldSet& sourceFieldSet, FieldSet& targetFieldSet) const {
void RedistributeStructuredColumns::execute(const field::FieldSetImpl* sourceFieldSet, field::FieldSetImpl* targetFieldSet) const {
// Check that both FieldSets are the same size.
ATLAS_ASSERT(sourceFieldSet.size() == targetFieldSet.size());
ATLAS_ASSERT(sourceFieldSet->size() == targetFieldSet->size());

auto targetFieldSetIt = targetFieldSet.begin();
std::for_each(sourceFieldSet.cbegin(), sourceFieldSet.cend(), [&](const Field& sourceField) {
execute(sourceField, *targetFieldSetIt++);
return;
});
auto targetFieldSetIt = targetFieldSet->begin();
for (int i = 0; i < sourceFieldSet->size(); i++) {
execute(&sourceFieldSet[i], &targetFieldSet[i]);
}
//std::for_each(sourceFieldSet->cbegin(), sourceFieldSet->cend(), [&](const field::FieldImpl* sourceField) {
// execute(sourceField, targetFieldSetIt++);
// return;
//});

return;
}
Expand All @@ -183,7 +188,7 @@ void RedistributeStructuredColumns::execute(const FieldSet& sourceFieldSet, Fiel
//========================================================================

template <typename fieldType>
void RedistributeStructuredColumns::do_execute(const Field& sourceField, Field& targetField) const {
void RedistributeStructuredColumns::do_execute(const field::FieldImpl& sourceField, field::FieldImpl& targetField) const {
// Make Atlas view objects.
const auto sourceView = array::make_view<fieldType, 2>(sourceField);
auto targetView = array::make_view<fieldType, 2>(targetField);
Expand Down
10 changes: 5 additions & 5 deletions src/atlas/redistribution/detail/RedistributeStructuredColumns.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

namespace atlas {

class Field;
class FieldSet;
class FieldImpl;
class FieldSetImpl;
class FunctionSpace;

namespace functionspace {
Expand Down Expand Up @@ -63,7 +63,7 @@ class RedistributeStructuredColumns : public RedistributionImpl {
///
/// \param[in] source input field matching sourceFunctionSpace.
/// \param[out] target output field matching targetFunctionSpace.
void execute(const Field& source, Field& target) const override;
void execute(const field::FieldImpl* source, field::FieldImpl* target) const override;

/// \brief Redistributes source field set to target fields set.
///
Expand All @@ -72,12 +72,12 @@ class RedistributeStructuredColumns : public RedistributionImpl {
///
/// \param[in] source input field set.
/// \param[out] target output field set.
void execute(const FieldSet& source, FieldSet& target) const override;
void execute(const field::FieldSetImpl* source, field::FieldSetImpl* target) const override;

private:
// Generic execute call to handle different field types.
template <typename fieldType>
void do_execute(const Field& source, Field& target) const;
void do_execute(const field::FieldImpl& source, field::FieldImpl& target) const;

// FunctionSpaces recast to StructuredColumns.
functionspace::StructuredColumns source_;
Expand Down
12 changes: 8 additions & 4 deletions src/atlas/redistribution/detail/RedistributionImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
#include "atlas/util/Object.h"

namespace atlas {
class Field;
class FieldSet;
namespace field {
class FieldImpl;
class FieldSetImpl;
}
namespace functionspace {
class FunctionSpace;
}
} // namespace atlas

namespace atlas {
Expand Down Expand Up @@ -42,10 +46,10 @@ class RedistributionImpl : public util::Object {
virtual std::string type() const = 0;

/// \brief Maps source field to target field.
virtual void execute(const Field& source, Field& target) const = 0;
virtual void execute(const field::FieldImpl* source, field::FieldImpl* target) const = 0;

/// \brief Maps source field set to target field set.
virtual void execute(const FieldSet& source, FieldSet& target) const = 0;
virtual void execute(const field::FieldSetImpl* source, field::FieldSetImpl* target) const = 0;

/// \brief Get const reference to source function space.
const FunctionSpace& source() const;
Expand Down
13 changes: 6 additions & 7 deletions src/atlas/redistribution/detail/RedistributionInterface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,18 @@ detail::RedistributionImpl* atlas__Redistribution__new__config(
}

void atlas__Redistribution__execute(
const detail::RedistributionImpl* This, const Field* field_1, Field* field_2) {
printf("redist execute");
This->execute(*field_1, *field_2);
const detail::RedistributionImpl* This, const field::FieldImpl* field_1, field::FieldImpl* field_2) {
This->execute(field_1, field_2);
}

const FunctionSpace* atlas__Redistribution__source(
const functionspace::FunctionSpaceImpl* atlas__Redistribution__source(
const detail::RedistributionImpl* This) {
return &This->source();
return This->source().get();
}

const FunctionSpace* atlas__Redistribution__target(
const functionspace::FunctionSpaceImpl* atlas__Redistribution__target(
const detail::RedistributionImpl* This) {
return &This->target();
return This->target().get();
}

}
Expand Down
12 changes: 6 additions & 6 deletions src/atlas/redistribution/detail/RedistributionInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class Configuration;

namespace atlas {
namespace functionspace{
class FunctionSpace;
class FunctionSpaceImpl;
}
namespace field{
class Field;
class FieldSet;
class FieldImpl;
class FieldSetImpl;
}
} // namespace atlas

Expand All @@ -37,12 +37,12 @@ RedistributionImpl* atlas__Redistribution__new__config(
const eckit::Configuration* config);

void atlas__Redistribution__execute(
const RedistributionImpl* This, const Field* field_1, Field* field_2);
const RedistributionImpl* This, const field::FieldImpl* field_1, field::FieldImpl* field_2);

const FunctionSpace* atlas__Redistribution__source(
const functionspace::FunctionSpaceImpl* atlas__Redistribution__source(
const RedistributionImpl* This);

const FunctionSpace* atlas__Redistribution__target(
const functionspace::FunctionSpaceImpl* atlas__Redistribution__target(
const RedistributionImpl* This);

}
Expand Down
4 changes: 2 additions & 2 deletions src/atlas_f/redistribution/atlas_Redistribution_module.F90
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ subroutine atlas_Redistribution__execute(this, field_1, field_2)
use atlas_redistribution_c_binding
use atlas_Field_module
class(atlas_Redistribution), intent(in) :: this
type(atlas_Field), intent(in) :: field_1
type(atlas_Field), intent(inout) :: field_2
class(atlas_Field), intent(in) :: field_1
class(atlas_Field), intent(inout) :: field_2
call atlas__Redistribution__execute(this%CPTR_PGIBUG_A, field_1%CPTR_PGIBUG_A, field_2%CPTR_PGIBUG_A)
end subroutine

Expand Down
2 changes: 1 addition & 1 deletion src/tests/redistribution/fctest_redistribution.F90
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module fcta_Redistribution_fxt
call atlas_library%finalise()
END_TESTSUITE_FINALIZE

TEST( test_resitribution )
TEST( test_redistribution )
use atlas_module
use atlas_redistribution_module

Expand Down

0 comments on commit 5a4354b

Please sign in to comment.