| 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 |
#include <sstream> |
| 12 |
|
| 13 |
#include "pluto/pluto.h" |
| 14 |
|
| 15 |
#include "atlas/array.h" |
| 16 |
#include "atlas/array/ArrayView.h" |
| 17 |
#include "atlas/array/IndexView.h" |
| 18 |
#include "atlas/library/config.h" |
| 19 |
#include "atlas/runtime/Exception.h" |
| 20 |
|
| 21 |
namespace atlas { |
| 22 |
namespace array { |
| 23 |
|
| 24 |
namespace { |
| 25 |
template <typename Value, int Rank> |
| 26 |
inline static void check_metadata(const Array& array) { |
| 27 |
if (array.rank() != Rank) { |
| 28 |
std::stringstream err; |
| 29 |
err << "Number of dimensions do not match: template argument " << Rank << " expected to be " << array.rank(); |
| 30 |
throw_Exception(err.str(), Here()); |
| 31 |
} |
| 32 |
if (array.datatype() != array::DataType::create<Value>()) { |
| 33 |
std::stringstream err; |
| 34 |
err << "Data Type does not match: template argument expected to be " << array.datatype().str(); |
| 35 |
throw_Exception(err.str(), Here()); |
| 36 |
} |
| 37 |
} |
| 38 |
} // namespace |
| 39 |
|
| 40 |
//------------------------------------------------------------------------------ |
| 41 |
|
| 42 |
template <typename Value, int Rank> |
| 43 |
ArrayView<Value, Rank> make_host_view(Array& array) { |
| 44 |
return ArrayView<Value, Rank>(array.host_data<Value>(), array.shape(), array.strides()); |
| 45 |
} |
| 46 |
|
| 47 |
template <typename Value, int Rank> |
| 48 |
ArrayView<const Value, Rank> make_host_view(const Array& array) { |
| 49 |
return ArrayView<const Value, Rank>(array.host_data<const Value>(), array.shape(), array.strides()); |
| 50 |
} |
| 51 |
|
| 52 |
template <typename Value, int Rank> |
| 53 |
ArrayView<Value, Rank> make_device_view(Array& array) { |
| 54 |
#if ATLAS_HAVE_GPU |
| 55 |
if (pluto::devices()) { |
| 56 |
ATLAS_ASSERT(array.deviceAllocated() || pluto::devices(), "make_device_view: Array not allocated on device"); |
| 57 |
} |
| 58 |
return ArrayView<Value, Rank>((array.device_data<Value>()), array.shape(), array.device_strides()); |
| 59 |
#else |
| 60 |
return make_host_view<Value, Rank>(array); |
| 61 |
#endif |
| 62 |
} |
| 63 |
|
| 64 |
template <typename Value, int Rank> |
| 65 |
ArrayView<const Value, Rank> make_device_view(const Array& array) { |
| 66 |
#if ATLAS_HAVE_GPU |
| 67 |
if (pluto::devices()) { |
| 68 |
ATLAS_ASSERT(array.deviceAllocated(), "make_device_view: Array not allocated on device"); |
| 69 |
} |
| 70 |
return ArrayView<const Value, Rank>(array.device_data<const Value>(), array.shape(), array.device_strides()); |
| 71 |
#else |
| 72 |
return make_host_view<const Value, Rank>(array); |
| 73 |
#endif |
| 74 |
} |
| 75 |
|
| 76 |
template <typename Value, int Rank> |
| 77 |
IndexView<Value, Rank> make_host_indexview(Array& array) { |
| 78 |
return IndexView<Value, Rank>((Value*)(array.storage()), array.shape().data()); |
| 79 |
} |
| 80 |
|
| 81 |
template <typename Value, int Rank> |
| 82 |
IndexView<const Value, Rank> make_host_indexview(const Array& array) { |
| 83 |
return IndexView<const Value, Rank>((const Value*)(array.storage()), array.shape().data()); |
| 84 |
} |
| 85 |
|
| 86 |
template <typename Value, int Rank> |
| 87 |
IndexView<Value, Rank> make_indexview(Array& array) { |
| 88 |
check_metadata<Value, Rank>(array); |
| 89 |
return make_host_indexview<Value, Rank>(array); |
| 90 |
} |
| 91 |
|
| 92 |
template <typename Value, int Rank> |
| 93 |
IndexView<const Value, Rank> make_indexview(const Array& array) { |
| 94 |
check_metadata<Value, Rank>(array); |
| 95 |
return make_host_indexview<const Value, Rank>(array); |
| 96 |
} |
| 97 |
|
| 98 |
template <typename Value, int Rank> |
| 99 |
ArrayView<Value, Rank> make_view(Array& array) { |
| 100 |
check_metadata<Value, Rank>(array); |
| 101 |
return make_host_view<Value, Rank>(array); |
| 102 |
} |
| 103 |
|
| 104 |
template <typename Value, int Rank> |
| 105 |
ArrayView<const Value, Rank> make_view(const Array& array) { |
| 106 |
check_metadata<Value, Rank>(array); |
| 107 |
return make_host_view<const Value, Rank>(array); |
| 108 |
} |
| 109 |
|
| 110 |
// -------------------------------------------------------------------------------------------- |
| 111 |
|
| 112 |
} // namespace array |
| 113 |
} // namespace atlas |
| 114 |
|
| 115 |
//----------------------------------------------------------------------- |
| 116 |
// Explicit instantiation |
| 117 |
namespace atlas { |
| 118 |
namespace array { |
| 119 |
|
| 120 |
#define EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(TYPE, RANK) \ |
| 121 |
template ArrayView<TYPE, RANK> make_view<TYPE, RANK>(Array&); \ |
| 122 |
template ArrayView<const TYPE, RANK> make_view<const TYPE, RANK>(Array&); \ |
| 123 |
template ArrayView<const TYPE, RANK> make_view<TYPE, RANK>(const Array&); \ |
| 124 |
template ArrayView<const TYPE, RANK> make_view<const TYPE, RANK>(const Array&); \ |
| 125 |
\ |
| 126 |
template ArrayView<TYPE, RANK> make_host_view<TYPE, RANK>(Array&); \ |
| 127 |
template ArrayView<const TYPE, RANK> make_host_view<const TYPE, RANK>(Array&); \ |
| 128 |
template ArrayView<const TYPE, RANK> make_host_view<TYPE, RANK>(const Array&); \ |
| 129 |
template ArrayView<const TYPE, RANK> make_host_view<const TYPE, RANK>(const Array&); \ |
| 130 |
\ |
| 131 |
template ArrayView<TYPE, RANK> make_device_view<TYPE, RANK>(Array&); \ |
| 132 |
template ArrayView<const TYPE, RANK> make_device_view<const TYPE, RANK>(Array&); \ |
| 133 |
template ArrayView<const TYPE, RANK> make_device_view<TYPE, RANK>(const Array&); \ |
| 134 |
template ArrayView<const TYPE, RANK> make_device_view<const TYPE, RANK>(const Array&); |
| 135 |
|
| 136 |
#define EXPLICIT_TEMPLATE_INSTATIATION(RANK) \ |
| 137 |
EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(int, RANK) \ |
| 138 |
EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(long, RANK) \ |
| 139 |
EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(float, RANK) \ |
| 140 |
EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(double, RANK) \ |
| 141 |
EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(unsigned int, RANK) \ |
| 142 |
EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(unsigned long, RANK) |
| 143 |
|
| 144 |
|
| 145 |
EXPLICIT_TEMPLATE_INSTATIATION(1) |
| 146 |
EXPLICIT_TEMPLATE_INSTATIATION(2) |
| 147 |
EXPLICIT_TEMPLATE_INSTATIATION(3) |
| 148 |
EXPLICIT_TEMPLATE_INSTATIATION(4) |
| 149 |
EXPLICIT_TEMPLATE_INSTATIATION(5) |
| 150 |
EXPLICIT_TEMPLATE_INSTATIATION(6) |
| 151 |
EXPLICIT_TEMPLATE_INSTATIATION(7) |
| 152 |
EXPLICIT_TEMPLATE_INSTATIATION(8) |
| 153 |
EXPLICIT_TEMPLATE_INSTATIATION(9) |
| 154 |
|
| 155 |
#undef EXPLICIT_TEMPLATE_INSTATIATION_TYPE_RANK |
| 156 |
#undef EXPLICIT_TEMPLATE_INSTATIATION |
| 157 |
|
| 158 |
#define EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW_TYPE_RANK(TYPE, RANK) \ |
| 159 |
template IndexView<TYPE, RANK> make_indexview<TYPE, RANK>(Array&); \ |
| 160 |
template IndexView<const TYPE, RANK> make_indexview<const TYPE, RANK>(Array&); \ |
| 161 |
template IndexView<const TYPE, RANK> make_indexview<TYPE, RANK>(const Array&); \ |
| 162 |
template IndexView<const TYPE, RANK> make_indexview<const TYPE, RANK>(const Array&); |
| 163 |
|
| 164 |
#define EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW(RANK) \ |
| 165 |
EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW_TYPE_RANK(int, RANK) \ |
| 166 |
EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW_TYPE_RANK(long, RANK) |
| 167 |
|
| 168 |
EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW(1) |
| 169 |
// EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW(2) |
| 170 |
|
| 171 |
#undef EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW |
| 172 |
#undef EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW |
| 173 |
|
| 174 |
} // namespace array |
| 175 |
} // namespace atlas |