| 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 <vector> |
| 14 |
|
| 15 |
#include "eckit/utils/Translator.h" |
| 16 |
|
| 17 |
#include "atlas/array/ArrayView.h" |
| 18 |
#include "atlas/parallel/GatherScatter.h" |
| 19 |
#include "atlas/parallel/mpi/mpi.h" |
| 20 |
#include "atlas/util/Checksum.h" |
| 21 |
#include "atlas/util/Object.h" |
| 22 |
#include "atlas/util/ObjectHandle.h" |
| 23 |
|
| 24 |
namespace atlas { |
| 25 |
namespace parallel { |
| 26 |
|
| 27 |
class Checksum : public util::Object { |
| 28 |
public: |
| 29 |
Checksum(); |
| 30 |
Checksum(const std::string& name); |
| 31 |
virtual ~Checksum() {} |
| 32 |
|
| 33 |
public: // methods |
| 34 |
std::string name() const { return name_; } |
| 35 |
|
| 36 |
/// @brief Setup |
| 37 |
/// @param [in] mpi_comm MPI communicator |
| 38 |
/// @param [in] part List of partitions |
| 39 |
/// @param [in] remote_idx List of local indices on remote partitions |
| 40 |
/// @param [in] base values of remote_idx start at "base" |
| 41 |
/// @param [in] glb_idx List of global indices |
| 42 |
/// @param [in] max_glb_idx maximum glb index we want to Checksum. |
| 43 |
/// To Checksum everything, set to val > max value in |
| 44 |
/// domain |
| 45 |
/// @param [in] parsize size of given lists |
| 46 |
void setup(const std::string& mpi_comm, const int part[], const idx_t remote_idx[], const int base, const gidx_t glb_idx[], const int parsize); |
| 47 |
|
| 48 |
/// @brief Setup |
| 49 |
/// @param [in] part List of partitions |
| 50 |
/// @param [in] remote_idx List of local indices on remote partitions |
| 51 |
/// @param [in] base values of remote_idx start at "base" |
| 52 |
/// @param [in] glb_idx List of global indices |
| 53 |
/// @param [in] max_glb_idx maximum glb index we want to Checksum. |
| 54 |
/// To Checksum everything, set to val > max value in |
| 55 |
/// domain |
| 56 |
/// @param [in] parsize size of given lists |
| 57 |
void setup(const int part[], const idx_t remote_idx[], const int base, const gidx_t glb_idx[], const int parsize); |
| 58 |
|
| 59 |
/// @brief Setup |
| 60 |
/// @param [in] mpi_comm MPI communicator |
| 61 |
/// @param [in] part List of partitions |
| 62 |
/// @param [in] remote_idx List of local indices on remote partitions |
| 63 |
/// @param [in] base values of remote_idx start at "base" |
| 64 |
/// @param [in] glb_idx List of global indices |
| 65 |
/// @param [in] mask Mask indices not to include in the communication |
| 66 |
/// pattern (0=include,1=exclude) |
| 67 |
/// @param [in] parsize size of given lists |
| 68 |
void setup(const std::string& mpi_comm, const int part[], const idx_t remote_idx[], const int base, const gidx_t glb_idx[], const int mask[], |
| 69 |
const int parsize); |
| 70 |
|
| 71 |
/// @brief Setup |
| 72 |
/// @param [in] part List of partitions |
| 73 |
/// @param [in] remote_idx List of local indices on remote partitions |
| 74 |
/// @param [in] base values of remote_idx start at "base" |
| 75 |
/// @param [in] glb_idx List of global indices |
| 76 |
/// @param [in] mask Mask indices not to include in the communication |
| 77 |
/// pattern (0=include,1=exclude) |
| 78 |
/// @param [in] parsize size of given lists |
| 79 |
void setup(const int part[], const idx_t remote_idx[], const int base, const gidx_t glb_idx[], const int mask[], |
| 80 |
const int parsize); |
| 81 |
|
| 82 |
/// @brief Setup |
| 83 |
void setup(const util::ObjectHandle<GatherScatter>&); |
| 84 |
|
| 85 |
template <typename DATA_TYPE> |
| 86 |
std::string execute(const DATA_TYPE lfield[], const int lvar_strides[], const int lvar_extents[], |
| 87 |
const int lvar_rank) const; |
| 88 |
|
| 89 |
template <typename DATA_TYPE> |
| 90 |
std::string execute(DATA_TYPE lfield[], const int nb_vars) const; |
| 91 |
|
| 92 |
template <typename DATA_TYPE, int LRANK> |
| 93 |
std::string execute(const array::ArrayView<DATA_TYPE, LRANK>& lfield) const; |
| 94 |
|
| 95 |
template <typename DATA_TYPE, int RANK> |
| 96 |
void var_info(const array::ArrayView<DATA_TYPE, RANK>& arr, std::vector<int>& varstrides, |
| 97 |
std::vector<int>& varextents) const; |
| 98 |
|
| 99 |
private: // data |
| 100 |
std::string name_; |
| 101 |
util::ObjectHandle<GatherScatter> gather_; |
| 102 |
bool is_setup_; |
| 103 |
size_t parsize_; |
| 104 |
}; |
| 105 |
|
| 106 |
template <typename DATA_TYPE> |
| 107 |
std::string Checksum::execute(const DATA_TYPE data[], const int var_strides[], const int var_extents[], |
| 108 |
const int var_rank) const { |
| 109 |
idx_t root = 0; |
| 110 |
|
| 111 |
if (!is_setup_) { |
| 112 |
throw_Exception("Checksum was not setup", Here()); |
| 113 |
} |
| 114 |
std::vector<util::checksum_t> local_checksums(parsize_); |
| 115 |
int var_size = var_extents[0] * var_strides[0]; |
| 116 |
|
| 117 |
for (size_t pp = 0; pp < parsize_; ++pp) { |
| 118 |
local_checksums[pp] = util::checksum(data + pp * var_size, var_size); |
| 119 |
} |
| 120 |
|
| 121 |
std::vector<util::checksum_t> global_checksums(gather_->comm().rank() == root ? gather_->glb_dof() : 0); |
| 122 |
parallel::Field<util::checksum_t const> loc(local_checksums.data(), 1); |
| 123 |
parallel::Field<util::checksum_t> glb(global_checksums.data(), 1); |
| 124 |
gather_->gather(&loc, &glb, 1); |
| 125 |
|
| 126 |
util::checksum_t glb_checksum = util::checksum(global_checksums.data(), global_checksums.size()); |
| 127 |
|
| 128 |
gather_->comm().broadcast(glb_checksum, root); |
| 129 |
|
| 130 |
return eckit::Translator<util::checksum_t, std::string>()(glb_checksum); |
| 131 |
} |
| 132 |
|
| 133 |
template <typename DATA_TYPE> |
| 134 |
std::string Checksum::execute(DATA_TYPE lfield[], const int nb_vars) const { |
| 135 |
int strides[] = {1}; |
| 136 |
int extents[] = {nb_vars}; |
| 137 |
return execute(lfield, strides, extents, 1); |
| 138 |
} |
| 139 |
|
| 140 |
template <typename DATA_TYPE, int RANK> |
| 141 |
void Checksum::var_info(const array::ArrayView<DATA_TYPE, RANK>& arr, std::vector<int>& varstrides, |
| 142 |
std::vector<int>& varshape) const { |
| 143 |
int rank = std::max(1, RANK - 1); |
| 144 |
varstrides.resize(rank); |
| 145 |
varshape.resize(rank); |
| 146 |
|
| 147 |
if (RANK > 1) { |
| 148 |
size_t stride = 1; |
| 149 |
for (int j = RANK - 1; j > 0; --j) { |
| 150 |
varstrides[j - 1] = stride; |
| 151 |
varshape[j - 1] = arr.shape(j); |
| 152 |
stride *= varshape[j - 1]; |
| 153 |
} |
| 154 |
// varstrides.assign(arr.strides()+1,arr.strides()+RANK); |
| 155 |
// varshape.assign(arr.shape()+1,arr.shape()+RANK); |
| 156 |
} |
| 157 |
else { |
| 158 |
varstrides[0] = 1; |
| 159 |
varshape[0] = 1; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
template <typename DATA_TYPE, int LRANK> |
| 164 |
std::string Checksum::execute(const array::ArrayView<DATA_TYPE, LRANK>& lfield) const { |
| 165 |
if (size_t(lfield.shape(0)) == parsize_) { |
| 166 |
std::vector<int> lvarstrides, lvarextents; |
| 167 |
var_info(lfield, lvarstrides, lvarextents); |
| 168 |
return execute(lfield.data(), lvarstrides.data(), lvarextents.data(), lvarstrides.size()); |
| 169 |
} |
| 170 |
else { |
| 171 |
ATLAS_NOTIMPLEMENTED; // Need to implement with parallel ranks > 1 |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
// ------------------------------------------------------------------ |
| 176 |
// C wrapper interfaces to C++ routines |
| 177 |
extern "C" { |
| 178 |
Checksum* atlas__Checksum__new(); |
| 179 |
void atlas__Checksum__delete(Checksum* This); |
| 180 |
void atlas__Checksum__setup32(Checksum* This, int part[], idx_t remote_idx[], int base, int glb_idx[], int parsize); |
| 181 |
void atlas__Checksum__setup64(Checksum* This, int part[], idx_t remote_idx[], int base, long glb_idx[], int parsize); |
| 182 |
void atlas__Checksum__execute_strided_int(Checksum* This, int lfield[], int lvar_strides[], int lvar_extents[], |
| 183 |
int lvar_rank, char* checksum); |
| 184 |
void atlas__Checksum__execute_strided_float(Checksum* This, float lfield[], int lvar_strides[], int lvar_extents[], |
| 185 |
int lvar_rank, char* checksum); |
| 186 |
void atlas__Checksum__execute_strided_double(Checksum* This, double lfield[], int lvar_strides[], int lvar_extents[], |
| 187 |
int lvar_rank, char* checksum); |
| 188 |
void atlas__Checksum__execute_strided_long(Checksum* This, long lfield[], int lvar_strides[], int lvar_extents[], |
| 189 |
int lvar_rank, char* checksum); |
| 190 |
} |
| 191 |
// ------------------------------------------------------------------ |
| 192 |
|
| 193 |
} // namespace parallel |
| 194 |
} // namespace atlas |