forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kokkos_Serial.cpp
182 lines (140 loc) · 5.58 KB
/
Kokkos_Serial.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//@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
#ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
#define KOKKOS_IMPL_PUBLIC_INCLUDE
#endif
#include <Kokkos_Core.hpp>
#include <Serial/Kokkos_Serial.hpp>
#include <impl/Kokkos_Traits.hpp>
#include <impl/Kokkos_Error.hpp>
#include <impl/Kokkos_ExecSpaceManager.hpp>
#include <impl/Kokkos_SharedAlloc.hpp>
#include <cstdlib>
#include <iostream>
#include <sstream>
/*--------------------------------------------------------------------------*/
namespace Kokkos {
namespace Impl {
bool SerialInternal::is_initialized() { return m_is_initialized; }
void SerialInternal::initialize() {
if (is_initialized()) return;
Impl::SharedAllocationRecord<void, void>::tracking_enable();
m_is_initialized = true;
}
void SerialInternal::finalize() {
if (m_thread_team_data.scratch_buffer()) {
m_thread_team_data.disband_team();
m_thread_team_data.disband_pool();
Kokkos::HostSpace space;
space.deallocate(m_thread_team_data.scratch_buffer(),
m_thread_team_data.scratch_bytes());
m_thread_team_data.scratch_assign(nullptr, 0, 0, 0, 0, 0);
}
m_is_initialized = false;
}
SerialInternal& SerialInternal::singleton() {
static SerialInternal* self = nullptr;
if (!self) {
self = new SerialInternal();
}
return *self;
}
// Resize thread team data scratch memory
void SerialInternal::resize_thread_team_data(size_t pool_reduce_bytes,
size_t team_reduce_bytes,
size_t team_shared_bytes,
size_t thread_local_bytes) {
if (pool_reduce_bytes < 512) pool_reduce_bytes = 512;
if (team_reduce_bytes < 512) team_reduce_bytes = 512;
const size_t old_pool_reduce = m_thread_team_data.pool_reduce_bytes();
const size_t old_team_reduce = m_thread_team_data.team_reduce_bytes();
const size_t old_team_shared = m_thread_team_data.team_shared_bytes();
const size_t old_thread_local = m_thread_team_data.thread_local_bytes();
const size_t old_alloc_bytes = m_thread_team_data.scratch_bytes();
// Allocate if any of the old allocation is tool small:
const bool allocate = (old_pool_reduce < pool_reduce_bytes) ||
(old_team_reduce < team_reduce_bytes) ||
(old_team_shared < team_shared_bytes) ||
(old_thread_local < thread_local_bytes);
if (allocate) {
Kokkos::HostSpace space;
if (old_alloc_bytes) {
m_thread_team_data.disband_team();
m_thread_team_data.disband_pool();
space.deallocate("Kokkos::Serial::scratch_mem",
m_thread_team_data.scratch_buffer(),
m_thread_team_data.scratch_bytes());
}
if (pool_reduce_bytes < old_pool_reduce) {
pool_reduce_bytes = old_pool_reduce;
}
if (team_reduce_bytes < old_team_reduce) {
team_reduce_bytes = old_team_reduce;
}
if (team_shared_bytes < old_team_shared) {
team_shared_bytes = old_team_shared;
}
if (thread_local_bytes < old_thread_local) {
thread_local_bytes = old_thread_local;
}
const size_t alloc_bytes =
HostThreadTeamData::scratch_size(pool_reduce_bytes, team_reduce_bytes,
team_shared_bytes, thread_local_bytes);
void* ptr = nullptr;
try {
ptr = space.allocate("Kokkos::Serial::scratch_mem", alloc_bytes);
} catch (Kokkos::Experimental::RawMemoryAllocationFailure const& failure) {
// For now, just rethrow the error message the existing way
Kokkos::Impl::throw_runtime_exception(failure.get_error_message());
}
m_thread_team_data.scratch_assign(static_cast<char*>(ptr), alloc_bytes,
pool_reduce_bytes, team_reduce_bytes,
team_shared_bytes, thread_local_bytes);
HostThreadTeamData* pool[1] = {&m_thread_team_data};
m_thread_team_data.organize_pool(pool, 1);
m_thread_team_data.organize_team(1);
}
}
} // namespace Impl
Serial::Serial()
: m_space_instance(&Impl::SerialInternal::singleton(),
[](Impl::SerialInternal*) {}) {}
Serial::Serial(NewInstance)
: m_space_instance(new Impl::SerialInternal, [](Impl::SerialInternal* ptr) {
ptr->finalize();
delete ptr;
}) {}
void Serial::print_configuration(std::ostream& os, bool /*verbose*/) const {
os << "Host Serial Execution Space:\n";
os << " KOKKOS_ENABLE_SERIAL: yes\n";
#ifdef KOKKOS_ENABLE_ATOMICS_BYPASS
os << "Kokkos atomics disabled\n";
#endif
os << "\nSerial Runtime Configuration:\n";
}
bool Serial::impl_is_initialized() {
return Impl::SerialInternal::singleton().is_initialized();
}
void Serial::impl_initialize(InitializationSettings const&) {
Impl::SerialInternal::singleton().initialize();
}
void Serial::impl_finalize() { Impl::SerialInternal::singleton().finalize(); }
const char* Serial::name() { return "Serial"; }
namespace Impl {
int g_serial_space_factory_initialized =
initialize_space_factory<Serial>("100_Serial");
} // namespace Impl
} // namespace Kokkos