| 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 "atlas/array/LocalView.h" |
| 14 |
#include "atlas/parallel/mpi/mpi.h" |
| 15 |
|
| 16 |
namespace atlas { |
| 17 |
namespace mpi { |
| 18 |
|
| 19 |
/// @brief Buffer<DATA_TYPE,SHAPE> |
| 20 |
/// |
| 21 |
/// Class that keeps allocation of a MPI buffer including |
| 22 |
/// counts and displacements, but with added index operator[] |
| 23 |
/// that returns an array::ArrayView<DATA_TYPE,SHAPE> of the part |
| 24 |
/// of the buffer for a processor index. |
| 25 |
template <typename DATA_TYPE, int SHAPE> |
| 26 |
struct Buffer; |
| 27 |
|
| 28 |
// ---------------------------------------------------------------------------------- |
| 29 |
|
| 30 |
template <typename DATA_TYPE, int SHAPE> |
| 31 |
struct Buffer : eckit::mpi::Buffer<DATA_TYPE> {}; |
| 32 |
|
| 33 |
template <typename DATA_TYPE> |
| 34 |
class BufferView { |
| 35 |
public: |
| 36 |
BufferView(DATA_TYPE* data, size_t size): data_(data), size_(size) {} |
| 37 |
size_t size() const { return size_; } |
| 38 |
const DATA_TYPE& operator()(const size_t i) const { return data_[i]; } |
| 39 |
const DATA_TYPE& operator[](const size_t i) const { return data_[i]; } |
| 40 |
|
| 41 |
private: |
| 42 |
DATA_TYPE* data_; |
| 43 |
size_t size_; |
| 44 |
}; |
| 45 |
|
| 46 |
template <typename DATA_TYPE> |
| 47 |
struct Buffer<DATA_TYPE, 1> : public eckit::mpi::Buffer<DATA_TYPE> { |
| 48 |
Buffer(size_t size): eckit::mpi::Buffer<DATA_TYPE>(size) {} |
| 49 |
|
| 50 |
// array::make_shape( |
| 51 |
// eckit::mpi::Buffer<DATA_TYPE>::counts[p] |
| 52 |
// ) ); |
| 53 |
// } |
| 54 |
BufferView<DATA_TYPE> operator[](int p) { |
| 55 |
return BufferView<DATA_TYPE>( |
| 56 |
eckit::mpi::Buffer<DATA_TYPE>::buffer.data() + eckit::mpi::Buffer<DATA_TYPE>::displs[p], |
| 57 |
eckit::mpi::Buffer<DATA_TYPE>::counts[p]); |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
// ---------------------------------------------------------------------------------- |
| 62 |
|
| 63 |
} // namespace mpi |
| 64 |
} // namespace atlas |