forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kokkos_OpenACCSpace.cpp
117 lines (99 loc) · 4.23 KB
/
Kokkos_OpenACCSpace.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER
#define KOKKOS_IMPL_PUBLIC_INCLUDE
#include <OpenACC/Kokkos_OpenACC.hpp>
#include <OpenACC/Kokkos_OpenACCSpace.hpp>
#include <OpenACC/Kokkos_OpenACC_DeepCopy.hpp>
#include <impl/Kokkos_MemorySpace.hpp>
#include <impl/Kokkos_Profiling_Interface.hpp>
#include <impl/Kokkos_Error.hpp>
#include <openacc.h>
void *Kokkos::Experimental::OpenACCSpace::allocate(
const Kokkos::Experimental::OpenACC &exec_space,
const size_t arg_alloc_size) const {
return allocate(exec_space, "[unlabeled]", arg_alloc_size);
}
void *Kokkos::Experimental::OpenACCSpace::allocate(
const size_t arg_alloc_size) const {
return allocate("[unlabeled]", arg_alloc_size);
}
void *Kokkos::Experimental::OpenACCSpace::allocate(
const Kokkos::Experimental::OpenACC &exec_space, const char *arg_label,
const size_t arg_alloc_size, const size_t arg_logical_size) const {
return impl_allocate(exec_space, arg_label, arg_alloc_size, arg_logical_size);
}
void *Kokkos::Experimental::OpenACCSpace::allocate(
const char *arg_label, const size_t arg_alloc_size,
const size_t arg_logical_size) const {
return impl_allocate(arg_label, arg_alloc_size, arg_logical_size);
}
void *Kokkos::Experimental::OpenACCSpace::impl_allocate(
const Kokkos::Experimental::OpenACC &exec_space, const char *arg_label,
const size_t arg_alloc_size, const size_t arg_logical_size,
const Kokkos::Tools::SpaceHandle arg_handle) const {
(void)exec_space;
return impl_allocate(arg_label, arg_alloc_size, arg_logical_size, arg_handle);
}
void *Kokkos::Experimental::OpenACCSpace::impl_allocate(
const char *arg_label, const size_t arg_alloc_size,
const size_t arg_logical_size,
const Kokkos::Tools::SpaceHandle arg_handle) const {
static_assert(sizeof(void *) == sizeof(uintptr_t),
"Error sizeof(void*) != sizeof(uintptr_t)");
void *ptr = nullptr;
ptr = acc_malloc(arg_alloc_size);
if (!ptr) {
size_t alignment = 1; // OpenACC does not handle alignment
using Kokkos::Experimental::RawMemoryAllocationFailure;
auto failure_mode =
arg_alloc_size > 0
? RawMemoryAllocationFailure::FailureMode::OutOfMemoryError
: RawMemoryAllocationFailure::FailureMode::InvalidAllocationSize;
auto alloc_mechanism =
RawMemoryAllocationFailure::AllocationMechanism::OpenACCMalloc;
throw RawMemoryAllocationFailure(arg_alloc_size, alignment, failure_mode,
alloc_mechanism);
}
if (Kokkos::Profiling::profileLibraryLoaded()) {
const size_t reported_size =
(arg_logical_size > 0) ? arg_logical_size : arg_alloc_size;
Kokkos::Profiling::allocateData(arg_handle, arg_label, ptr, reported_size);
}
return ptr;
}
void Kokkos::Experimental::OpenACCSpace::deallocate(
void *const arg_alloc_ptr, const size_t arg_alloc_size) const {
deallocate("[unlabeled]", arg_alloc_ptr, arg_alloc_size);
}
void Kokkos::Experimental::OpenACCSpace::deallocate(
const char *arg_label, void *const arg_alloc_ptr,
const size_t arg_alloc_size, const size_t arg_logical_size) const {
impl_deallocate(arg_label, arg_alloc_ptr, arg_alloc_size, arg_logical_size);
}
void Kokkos::Experimental::OpenACCSpace::impl_deallocate(
const char *arg_label, void *const arg_alloc_ptr,
const size_t arg_alloc_size, const size_t arg_logical_size,
const Kokkos::Tools::SpaceHandle arg_handle) const {
if (Kokkos::Profiling::profileLibraryLoaded()) {
const size_t reported_size =
(arg_logical_size > 0) ? arg_logical_size : arg_alloc_size;
Kokkos::Profiling::deallocateData(arg_handle, arg_label, arg_alloc_ptr,
reported_size);
}
if (arg_alloc_ptr) {
acc_free(arg_alloc_ptr);
}
}