Debian Codegraph @

src/atlas/util/Allocate.cc

raw
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
12 #include "Allocate.h"
13
14 #include "eckit/log/CodeLocation.h"
15
16 #include "pluto/pluto.h"
17
18 #include "atlas/library/config.h"
19 #include "atlas/runtime/Exception.h"
20
21 namespace atlas {
22 namespace util {
23
24 //------------------------------------------------------------------------------
25 namespace detail {
26 //------------------------------------------------------------------------------
27
28 void allocate_managed(void** ptr, size_t bytes) {
29 if constexpr (not ATLAS_HAVE_GPU) {
30 return allocate_host(ptr, bytes);
31 }
32 *ptr = pluto::managed_resource()->allocate(bytes, pluto::default_alignment());
33 }
34
35 void deallocate_managed(void* ptr, size_t bytes) {
36 if constexpr (not ATLAS_HAVE_GPU) {
37 return deallocate_host(ptr, bytes);
38 }
39 pluto::wait();
40 pluto::managed_resource()->deallocate(ptr, bytes, pluto::default_alignment());
41 }
42
43 void allocate_device(void** ptr, size_t bytes) {
44 if constexpr (not ATLAS_HAVE_GPU) {
45 return allocate_host(ptr, bytes);
46 }
47 *ptr = pluto::device_resource()->allocate(bytes, pluto::default_alignment());
48 }
49
50 void deallocate_device(void* ptr, size_t bytes) {
51 if constexpr (not ATLAS_HAVE_GPU) {
52 return deallocate_host(ptr, bytes);
53 }
54 pluto::wait();
55 pluto::device_resource()->deallocate(ptr, bytes, pluto::default_alignment());
56 }
57
58 void allocate_host(void** ptr, size_t bytes) {
59 *ptr = pluto::host_resource()->allocate(bytes, pluto::default_alignment());
60 }
61
62 void deallocate_host(void* ptr, size_t bytes) {
63 pluto::host_resource()->deallocate(ptr, bytes, pluto::default_alignment());
64 }
65
66 //------------------------------------------------------------------------------
67 } // namespace detail
68 //------------------------------------------------------------------------------
69
70 extern "C" {
71 void atlas__allocate_managedmem_double(double*& a, size_t N) {
72 allocate_managedmem(a, N);
73 }
74 void atlas__allocate_managedmem_float(float*& a, size_t N) {
75 allocate_managedmem(a, N);
76 }
77 void atlas__allocate_managedmem_int(int*& a, size_t N) {
78 allocate_managedmem(a, N);
79 }
80 void atlas__allocate_managedmem_long(long*& a, size_t N) {
81 allocate_managedmem(a, N);
82 }
83 void atlas__deallocate_managedmem_double(double*& a, size_t N) {
84 delete_managedmem(a, N);
85 }
86 void atlas__deallocate_managedmem_float(float*& a, size_t N) {
87 delete_managedmem(a, N);
88 }
89 void atlas__deallocate_managedmem_int(int*& a, size_t N) {
90 delete_managedmem(a, N);
91 }
92 void atlas__deallocate_managedmem_long(long*& a, size_t N) {
93 delete_managedmem(a, N);
94 }
95 }
96
97 //------------------------------------------------------------------------------
98
99 } // namespace util
100 } // namespace atlas