| 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/library/config.h" |
| 14 |
#include "atlas/array/ArrayDataStore.h" |
| 15 |
#include "atlas/array/gridtools/GridToolsTraits.h" |
| 16 |
|
| 17 |
#if ATLAS_HAVE_ACC |
| 18 |
#include "atlas_acc_support/atlas_acc.h" |
| 19 |
#endif |
| 20 |
|
| 21 |
//------------------------------------------------------------------------------ |
| 22 |
|
| 23 |
namespace atlas { |
| 24 |
namespace array { |
| 25 |
namespace gridtools { |
| 26 |
|
| 27 |
template <typename gt_DataStore> |
| 28 |
struct GridToolsDataStore : ArrayDataStore { |
| 29 |
using Value = typename gt_DataStore::data_t; |
| 30 |
|
| 31 |
explicit GridToolsDataStore(gt_DataStore const* ds): data_store_(ds) {} |
| 32 |
|
| 33 |
~GridToolsDataStore() { |
| 34 |
assert(data_store_); |
| 35 |
delete data_store_; |
| 36 |
} |
| 37 |
|
| 38 |
void updateDevice() const override { |
| 39 |
assert(data_store_); |
| 40 |
allocateDevice(); |
| 41 |
data_store_->clone_to_device(); |
| 42 |
} |
| 43 |
|
| 44 |
void updateHost() const override { data_store_->clone_from_device(); } |
| 45 |
|
| 46 |
bool valid() const override { return data_store_->valid(); } |
| 47 |
|
| 48 |
void syncHostDevice() const override { data_store_->sync(); } |
| 49 |
|
| 50 |
void allocateDevice() const override { |
| 51 |
accMap(); |
| 52 |
} |
| 53 |
|
| 54 |
void deallocateDevice() const override { |
| 55 |
accUnmap(); |
| 56 |
} |
| 57 |
|
| 58 |
bool deviceAllocated() const override { return ATLAS_HAVE_GPU; } |
| 59 |
|
| 60 |
bool hostNeedsUpdate() const override { return data_store_->host_needs_update(); } |
| 61 |
|
| 62 |
bool deviceNeedsUpdate() const override { return data_store_->device_needs_update(); } |
| 63 |
|
| 64 |
void setHostNeedsUpdate(bool v) const override { |
| 65 |
auto state_machine = data_store_->get_storage_ptr()->get_state_machine_ptr_impl(); |
| 66 |
if (state_machine) { |
| 67 |
state_machine->m_hnu = v; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
void setDeviceNeedsUpdate(bool v) const override { |
| 72 |
auto state_machine = data_store_->get_storage_ptr()->get_state_machine_ptr_impl(); |
| 73 |
if (state_machine) { |
| 74 |
state_machine->m_dnu = v; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
void reactivateDeviceWriteViews() const override { data_store_->reactivate_target_write_views(); } |
| 79 |
|
| 80 |
void reactivateHostWriteViews() const override { data_store_->reactivate_host_write_views(); } |
| 81 |
|
| 82 |
void* voidDataStore() override { return static_cast<void*>(const_cast<gt_DataStore*>(data_store_)); } |
| 83 |
|
| 84 |
void* voidHostData() override { return host_data(); } |
| 85 |
|
| 86 |
void* voidDeviceData() override { return device_data(); } |
| 87 |
|
| 88 |
void accMap() const override { |
| 89 |
#if ATLAS_HAVE_ACC |
| 90 |
if (not acc_mapped_) { |
| 91 |
ATLAS_ASSERT(deviceAllocated(),"Could not accMap as device data is not allocated"); |
| 92 |
atlas_acc_map_data(host_data(), device_data(), bytes()); |
| 93 |
acc_mapped_ = true; |
| 94 |
} |
| 95 |
#endif |
| 96 |
} |
| 97 |
|
| 98 |
bool accMapped() const override { |
| 99 |
return acc_mapped_; |
| 100 |
} |
| 101 |
|
| 102 |
void accUnmap() const override { |
| 103 |
#if ATLAS_HAVE_ACC |
| 104 |
if (acc_mapped_) { |
| 105 |
atlas_acc_unmap_data(host_data()); |
| 106 |
acc_mapped_ = false; |
| 107 |
} |
| 108 |
#endif |
| 109 |
} |
| 110 |
|
| 111 |
private: |
| 112 |
void* host_data() const { |
| 113 |
return ::gridtools::make_host_view<::gridtools::access_mode::read_only>(*data_store_).data(); |
| 114 |
} |
| 115 |
void* device_data() const { |
| 116 |
#if ATLAS_HAVE_GPU |
| 117 |
return ::gridtools::make_device_view<::gridtools::access_mode::read_only>(*data_store_).data(); |
| 118 |
#else |
| 119 |
return ::gridtools::make_host_view<::gridtools::access_mode::read_only>(*data_store_).data(); |
| 120 |
#endif |
| 121 |
} |
| 122 |
|
| 123 |
size_t bytes() const { |
| 124 |
auto storage_info_ptr = data_store_->get_storage_info_ptr().get(); |
| 125 |
return storage_info_ptr->padded_total_length() * sizeof(Value); |
| 126 |
} |
| 127 |
|
| 128 |
private: |
| 129 |
gt_DataStore const* data_store_; |
| 130 |
mutable bool acc_mapped_{false}; |
| 131 |
}; |
| 132 |
|
| 133 |
} // namespace gridtools |
| 134 |
} // namespace array |
| 135 |
} // namespace atlas |