| 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 |
|
| 15 |
#include "hic/hic.h" |
| 16 |
|
| 17 |
namespace atlas { |
| 18 |
namespace util { |
| 19 |
|
| 20 |
template <typename Base> |
| 21 |
struct GPUClonable { |
| 22 |
GPUClonable(Base* base_ptr): base_ptr_(base_ptr), gpu_object_ptr_(nullptr) { |
| 23 |
if constexpr (ATLAS_HAVE_GPU) { |
| 24 |
HIC_CALL(hicMalloc(&gpu_object_ptr_, sizeof(Base))); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
~GPUClonable() { |
| 29 |
if (gpu_object_ptr_ != nullptr) { |
| 30 |
HIC_CALL(hicFree(gpu_object_ptr_)); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
Base* gpu_object_ptr() { return static_cast<Base*>(gpu_object_ptr_); } |
| 35 |
|
| 36 |
void updateDevice() { |
| 37 |
if constexpr (ATLAS_HAVE_GPU) { |
| 38 |
HIC_CALL(hicMemcpy(gpu_object_ptr_, base_ptr_, sizeof(Base), hicMemcpyHostToDevice)); |
| 39 |
} |
| 40 |
} |
| 41 |
void updateHost() { |
| 42 |
if constexpr (ATLAS_HAVE_GPU) { |
| 43 |
HIC_CALL(hicMemcpy(base_ptr_, gpu_object_ptr_, sizeof(Base), hicMemcpyDeviceToHost)); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
Base* base_ptr_; |
| 48 |
void* gpu_object_ptr_; |
| 49 |
}; |
| 50 |
|
| 51 |
} // namespace util |
| 52 |
} // namespace atlas |