| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include <type_traits> |
| 14 |
#include <utility> |
| 15 |
#include <sstream> |
| 16 |
|
| 17 |
#include "atlas/field/Field.h" |
| 18 |
#include "atlas/array/helpers/ArrayForEach.h" |
| 19 |
#include "atlas/runtime/Exception.h" |
| 20 |
|
| 21 |
namespace atlas { |
| 22 |
namespace field { |
| 23 |
namespace detail { |
| 24 |
|
| 25 |
template <typename Function> // primary template |
| 26 |
struct function_traits : |
| 27 |
public function_traits<decltype(&std::remove_reference<Function>::type::operator())> { }; |
| 28 |
|
| 29 |
template <typename ClassType, typename ReturnType, typename... Arguments> |
| 30 |
struct function_traits<ReturnType(ClassType::*)(Arguments...) const> : |
| 31 |
function_traits<ReturnType(*)(Arguments...)> { }; |
| 32 |
|
| 33 |
template <typename ClassType, typename ReturnType, typename... Arguments> |
| 34 |
struct function_traits<ReturnType(ClassType::*)(Arguments...)> : |
| 35 |
function_traits<ReturnType(*)(Arguments...)> { }; |
| 36 |
|
| 37 |
template <typename ReturnType, typename... Arguments> |
| 38 |
struct function_traits<ReturnType(*)(Arguments...)> { |
| 39 |
using result_type = ReturnType; |
| 40 |
|
| 41 |
template <std::size_t Index> |
| 42 |
using arg_t = typename std::tuple_element< |
| 43 |
Index, |
| 44 |
std::tuple<Arguments...> |
| 45 |
>::type; |
| 46 |
|
| 47 |
static constexpr std::size_t arity = sizeof...(Arguments); |
| 48 |
}; |
| 49 |
|
| 50 |
template <typename Function> |
| 51 |
using first_argument = typename function_traits<Function>::template arg_t<0>; |
| 52 |
|
| 53 |
template <typename Function, typename Value> |
| 54 |
constexpr bool valid_value_function() { |
| 55 |
return std::is_same_v<std::decay_t<first_argument<Function>>,std::decay_t<Value>>; |
| 56 |
} |
| 57 |
|
| 58 |
template <typename Function> |
| 59 |
using function_argument_data_type = typename std::decay_t<typename std::decay_t<first_argument<Function>>::value_type>; |
| 60 |
|
| 61 |
template <typename Function> |
| 62 |
constexpr idx_t function_argument_rank() { |
| 63 |
return std::decay_t<first_argument<Function>>::rank(); |
| 64 |
} |
| 65 |
|
| 66 |
template <typename Function, typename Value, int Rank> |
| 67 |
constexpr bool valid_column_function() { |
| 68 |
using value_type = std::decay_t<Value>; |
| 69 |
return function_argument_rank<Function>() == Rank && std::is_same_v<function_argument_data_type<Function>,value_type>; |
| 70 |
} |
| 71 |
|
| 72 |
template <typename Config, typename Mask, typename Function, typename... Views, std::size_t... Dims> |
| 73 |
void for_each_value_masked_view(std::index_sequence<Dims...>, const Config& config, const Mask& mask, const Function& function, std::tuple<Views...>&& views ) { |
| 74 |
if constexpr (std::is_invocable_r_v<int, Mask, decltype (Dims)...>) { |
| 75 |
array::helpers::ArrayForEach<Dims...>::apply(config,std::move(views),mask,function); |
| 76 |
} |
| 77 |
else { |
| 78 |
ATLAS_THROW_EXCEPTION("Invalid mask function passed"); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
template <int FieldIdx, typename Value, int Rank, typename... Field> |
| 83 |
auto make_view_tuple(std::tuple<Field...>&& fields) { |
| 84 |
constexpr auto num_fields = std::tuple_size_v<std::tuple<Field...>>; |
| 85 |
if constexpr (FieldIdx < num_fields) { |
| 86 |
return std::tuple_cat(std::make_tuple( |
| 87 |
array::make_view<Value, Rank>(std::get<FieldIdx>(fields))), |
| 88 |
make_view_tuple<FieldIdx + 1, Value, Rank>(std::move(fields))); |
| 89 |
} else { |
| 90 |
return std::make_tuple(); |
| 91 |
} |
| 92 |
ATLAS_UNREACHABLE(); |
| 93 |
} |
| 94 |
|
| 95 |
template <typename Value, int Rank, typename Config, typename Mask, typename... Field, typename Function> |
| 96 |
void for_each_value_masked_rank(const Config& config, const Mask& mask, std::tuple<Field...>&& fields, const Function& function ) { |
| 97 |
constexpr auto dims = std::make_index_sequence<Rank>(); |
| 98 |
if constexpr (valid_value_function<Function,Value>()) { |
| 99 |
return for_each_value_masked_view(dims, config, mask, function, make_view_tuple<0, Value, Rank>(std::move(fields))); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
template <typename Config, typename Mask, typename Function, typename... Views > |
| 104 |
void for_each_column_masked_view(const Config& config, const Mask& mask, const std::vector<idx_t>& h_dim, const Function& function, std::tuple<Views...>&& views ) { |
| 105 |
using View = std::decay_t<std::tuple_element_t<0, std::tuple<Views...>>>; |
| 106 |
using value_type = typename View::value_type; |
| 107 |
constexpr int rank = View::rank(); |
| 108 |
|
| 109 |
if (h_dim.size()>=rank) { |
| 110 |
ATLAS_THROW_EXCEPTION("Cannot extract column for Rank="<<rank<<" field and " << h_dim.size() << " horizontal indices"); |
| 111 |
} |
| 112 |
for (auto h : h_dim) { |
| 113 |
if (h < 0 || h >= rank) { |
| 114 |
ATLAS_THROW_EXCEPTION("Invalid horizontal_dimension " << h_dim[0] << " must less rank " << rank); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
auto has_duplicates = [](const auto &v) { |
| 119 |
for (const auto& i : v) { |
| 120 |
for (const auto & j : v) { |
| 121 |
if (&i == &j) break; |
| 122 |
if (i == j) return true; |
| 123 |
} |
| 124 |
} |
| 125 |
return false; |
| 126 |
}; |
| 127 |
if (has_duplicates(h_dim)) { |
| 128 |
ATLAS_THROW_EXCEPTION("horizontal_dimension contains duplicates"); |
| 129 |
} |
| 130 |
|
| 131 |
if constexpr (rank==1) { |
| 132 |
ATLAS_THROW_EXCEPTION("Cannot use for_each_column with Rank=1 fields"); |
| 133 |
} |
| 134 |
if constexpr (rank==2) { |
| 135 |
if constexpr (valid_column_function<Function,value_type,rank-1>() && |
| 136 |
std::is_invocable_r_v<int, Mask, idx_t>) { |
| 137 |
switch (h_dim[0]) { |
| 138 |
case 0: array::helpers::ArrayForEach<0>::apply(config,std::move(views),mask,function); return; |
| 139 |
case 1: array::helpers::ArrayForEach<1>::apply(config,std::move(views),mask,function); return; |
| 140 |
default: break; |
| 141 |
} |
| 142 |
ATLAS_THROW_EXCEPTION("Not implemented for horizontal_dimension = " << h_dim); |
| 143 |
} |
| 144 |
ATLAS_THROW_EXCEPTION("Invalid function passed"); |
| 145 |
} |
| 146 |
if constexpr (rank==3) { |
| 147 |
if (h_dim.size() == 1) { |
| 148 |
if constexpr (valid_column_function<Function,value_type,rank-1>() && |
| 149 |
std::is_invocable_r_v<int, Mask, idx_t>) { |
| 150 |
switch (h_dim[0]) { |
| 151 |
case 0: array::helpers::ArrayForEach<0>::apply(config,std::move(views),mask,function); return; |
| 152 |
case 1: array::helpers::ArrayForEach<1>::apply(config,std::move(views),mask,function); return; |
| 153 |
case 2: array::helpers::ArrayForEach<2>::apply(config,std::move(views),mask,function); return; |
| 154 |
default: break; |
| 155 |
} |
| 156 |
ATLAS_THROW_EXCEPTION("Not implemented for horizontal_dimension = " << h_dim); |
| 157 |
} |
| 158 |
ATLAS_THROW_EXCEPTION("Invalid function passed"); |
| 159 |
} |
| 160 |
else if (h_dim.size() == 2) { |
| 161 |
if constexpr (valid_column_function<Function,value_type,rank-2>() && |
| 162 |
std::is_invocable_r_v<int, Mask, idx_t, idx_t>) { |
| 163 |
switch (h_dim[0]) { |
| 164 |
case 0: { |
| 165 |
switch (h_dim[1]) { |
| 166 |
case 1: array::helpers::ArrayForEach<0,1>::apply(config,std::move(views),mask,function); return; |
| 167 |
case 2: array::helpers::ArrayForEach<0,2>::apply(config,std::move(views),mask,function); return; |
| 168 |
default: break; |
| 169 |
} |
| 170 |
break; |
| 171 |
} |
| 172 |
case 1: { |
| 173 |
switch (h_dim[1]) { |
| 174 |
case 2: array::helpers::ArrayForEach<1,2>::apply(config,std::move(views),mask,function); return; |
| 175 |
default: break; |
| 176 |
} |
| 177 |
break; |
| 178 |
} |
| 179 |
} |
| 180 |
ATLAS_THROW_EXCEPTION("Not implemented for horizontal_dimension = " << h_dim); |
| 181 |
} |
| 182 |
ATLAS_THROW_EXCEPTION("Invalid function passed"); |
| 183 |
} |
| 184 |
ATLAS_THROW_EXCEPTION("Not implemented for h_dim = " << h_dim); |
| 185 |
} |
| 186 |
if constexpr (rank==4) { |
| 187 |
if (h_dim.size() == 1) { |
| 188 |
if constexpr (valid_column_function<Function,value_type,rank-1>() && |
| 189 |
std::is_invocable_r_v<int, Mask, idx_t>) { |
| 190 |
switch (h_dim[0]) { |
| 191 |
case 0: array::helpers::ArrayForEach<0>::apply(config,std::move(views),mask,function); return; |
| 192 |
case 1: array::helpers::ArrayForEach<1>::apply(config,std::move(views),mask,function); return; |
| 193 |
case 2: array::helpers::ArrayForEach<2>::apply(config,std::move(views),mask,function); return; |
| 194 |
case 3: array::helpers::ArrayForEach<3>::apply(config,std::move(views),mask,function); return; |
| 195 |
default: break; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
else if (h_dim.size() == 2) { |
| 200 |
if constexpr (valid_column_function<Function,value_type,rank-2>() && |
| 201 |
std::is_invocable_r_v<int, Mask, idx_t, idx_t>) { |
| 202 |
switch (h_dim[0]) { |
| 203 |
case 0: { |
| 204 |
switch (h_dim[1]) { |
| 205 |
case 1: array::helpers::ArrayForEach<0,1>::apply(config,std::move(views),mask,function); return; |
| 206 |
case 2: array::helpers::ArrayForEach<0,2>::apply(config,std::move(views),mask,function); return; |
| 207 |
case 3: array::helpers::ArrayForEach<0,3>::apply(config,std::move(views),mask,function); return; |
| 208 |
default: break; |
| 209 |
} |
| 210 |
break; |
| 211 |
} |
| 212 |
case 1: { |
| 213 |
switch (h_dim[1]) { |
| 214 |
case 2: array::helpers::ArrayForEach<1,2>::apply(config,std::move(views),mask,function); return; |
| 215 |
case 3: array::helpers::ArrayForEach<1,3>::apply(config,std::move(views),mask,function); return; |
| 216 |
default: break; |
| 217 |
} |
| 218 |
break; |
| 219 |
} |
| 220 |
case 2: { |
| 221 |
switch (h_dim[1]) { |
| 222 |
case 3: array::helpers::ArrayForEach<2,3>::apply(config,std::move(views),mask,function); return; |
| 223 |
default: break; |
| 224 |
} |
| 225 |
break; |
| 226 |
} |
| 227 |
default: break; |
| 228 |
} |
| 229 |
ATLAS_THROW_EXCEPTION("Not implemented for horizontal_dimension = " << h_dim); |
| 230 |
} |
| 231 |
ATLAS_THROW_EXCEPTION("Invalid function passed"); |
| 232 |
} |
| 233 |
ATLAS_THROW_EXCEPTION("Not implemented for h_dim = " << h_dim); |
| 234 |
} |
| 235 |
ATLAS_THROW_EXCEPTION("Not implemented for rank="<<rank); |
| 236 |
} |
| 237 |
|
| 238 |
template <int Rank, typename Mask, typename... Fields, typename Function> |
| 239 |
void for_each_column_masked_rank(const eckit::Parametrisation& config, const Mask& mask, std::tuple<Fields...>&& fields, const Function& function) { |
| 240 |
|
| 241 |
constexpr auto num_fields = std::tuple_size_v<std::tuple<Fields...>>; |
| 242 |
ATLAS_ASSERT(num_fields == detail::function_traits<Function>::arity); |
| 243 |
|
| 244 |
using value_type = detail::function_argument_data_type<Function>; |
| 245 |
ATLAS_ASSERT( std::get<0>(fields).datatype() == array::make_datatype<value_type>() ); |
| 246 |
|
| 247 |
auto h_dim = std::get<0>(fields).horizontal_dimension(); |
| 248 |
ATLAS_ASSERT(Rank == detail::function_argument_rank<Function>() + h_dim.size()); |
| 249 |
|
| 250 |
return for_each_column_masked_view(config, mask, h_dim, function, make_view_tuple<0, value_type, Rank>(std::move(fields))); |
| 251 |
} |
| 252 |
|
| 253 |
} // namespace detail |
| 254 |
} // namespace field |
| 255 |
} // namespace atlas |