| 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 nor |
| 8 |
* does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include <cstddef> |
| 14 |
|
| 15 |
namespace atlas { |
| 16 |
namespace util { |
| 17 |
|
| 18 |
//------------------------------------------------------------------------------ |
| 19 |
|
| 20 |
namespace detail { |
| 21 |
void allocate_managed(void** ptr, size_t bytes); |
| 22 |
void deallocate_managed(void* ptr, size_t bytes); |
| 23 |
|
| 24 |
void allocate_device(void** ptr, size_t bytes); |
| 25 |
void deallocate_device(void* ptr, size_t bytes); |
| 26 |
|
| 27 |
void allocate_host(void** ptr, size_t bytes); |
| 28 |
void deallocate_host(void* ptr, size_t bytes); |
| 29 |
|
| 30 |
} // namespace detail |
| 31 |
|
| 32 |
template <typename T> |
| 33 |
void allocate_managedmem(T*& data, size_t N) { |
| 34 |
if (N != 0) { |
| 35 |
detail::allocate_managed(reinterpret_cast<void**>(&data), N * sizeof(T)); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
template <typename T> |
| 40 |
void delete_managedmem(T*& data, size_t N) { |
| 41 |
if (data) { |
| 42 |
detail::deallocate_managed(data, N * sizeof(T)); |
| 43 |
data = nullptr; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
template <typename T> |
| 48 |
void allocate_devicemem(T*& data, size_t N) { |
| 49 |
if (N != 0) { |
| 50 |
detail::allocate_device(reinterpret_cast<void**>(&data), N * sizeof(T)); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
template <typename T> |
| 55 |
void delete_devicemem(T*& data, size_t N) { |
| 56 |
if (data) { |
| 57 |
detail::deallocate_device(data, N * sizeof(T)); |
| 58 |
data = nullptr; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
template <typename T> |
| 63 |
void allocate_hostmem(T*& data, size_t N) { |
| 64 |
if (N != 0) { |
| 65 |
detail::allocate_host(reinterpret_cast<void**>(&data), N * sizeof(T)); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
template <typename T> |
| 70 |
void delete_hostmem(T*& data, size_t N) { |
| 71 |
if (data) { |
| 72 |
detail::deallocate_host(data, N * sizeof(T)); |
| 73 |
data = nullptr; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
//------------------------------------------------------------------------------ |
| 79 |
|
| 80 |
extern "C" { |
| 81 |
void atlas__allocate_managedmem_double(double*& a, size_t N); |
| 82 |
void atlas__allocate_managedmem_float(float*& a, size_t N); |
| 83 |
void atlas__allocate_managedmem_int(int*& a, size_t N); |
| 84 |
void atlas__allocate_managedmem_long(long*& a, size_t N); |
| 85 |
void atlas__deallocate_managedmem_double(double*& a, size_t N); |
| 86 |
void atlas__deallocate_managedmem_float(float*& a, size_t N); |
| 87 |
void atlas__deallocate_managedmem_int(int*& a, size_t N); |
| 88 |
void atlas__deallocate_managedmem_long(long*& a, size_t N); |
| 89 |
} |
| 90 |
|
| 91 |
//------------------------------------------------------------------------------ |
| 92 |
|
| 93 |
} // namespace util |
| 94 |
} // namespace atlas |