Tannic
A C++ Tensor Library
Loading...
Searching...
No Matches
resources.hpp
Go to the documentation of this file.
1// Copyright 2025 Eric Hermosis
2//
3// This file is part of the Tannic Tensor Library.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17
18#ifndef RESOURCES_HPP
19#define RESOURCES_HPP
20
42#include <cstdint>
43#include <cstddef>
44#include <span>
45#include <vector>
46#include <variant>
47
48namespace tannic {
49
60class Host {
61public:
62
67 Host() = default;
68
76 void* allocate(std::size_t nbytes) const;
77
85 void deallocate(void* address, std::size_t nbytes) const;
86
91 int id() const { return -1; }
92
93 bool pageable() const {
94 return pageable_;
95 }
96
97 bool pinned() const {
98 return pinned_;
99 }
100
101private:
102 bool pageable_ = true;
103 bool pinned_ = false;
104};
105
106
120class Devices {
121private:
122 int count_ = 0;
123 Devices();
124
125public:
126 Devices(const Devices&) = delete;
127 Devices& operator=(const Devices&) = delete;
128 Devices(Devices&&) = delete;
129 Devices& operator=(Devices&&) = delete;
130 ~Devices() = default;
131
132 static Devices& instance() {
133 static Devices instance;
134 return instance;
135 }
136
137 static int count() {
138 Devices& devices = instance();
139 return devices.count_;
140 }
141};
142
143
156class Device {
157public:
158
164 Device(int id = 0, bool blocking = false);
165
171 void* allocate(std::size_t nbytes) const;
172
173
179 void deallocate(void* address, std::size_t nbytes) const;
180
185 int id() const noexcept {
186 return id_;
187 }
188
189 bool blocking() const {
190 return blocking_;
191 }
192
193private:
194 int id_ = 0;
195 bool blocking_ = false;
196};
197
198
204using Environment = std::variant<Host, Device>;
205
206} // namespace tannic
207
208#endif // RESOURCES_HPP
Device memory domain.
Definition: resources.hpp:156
bool blocking() const
Definition: resources.hpp:189
int id() const noexcept
Device identifier.
Definition: resources.hpp:185
Device(int id=0, bool blocking=false)
Constructs a device memory environment.
void deallocate(void *address, std::size_t nbytes) const
Releases device memory.
void * allocate(std::size_t nbytes) const
Allocates device memory
Device enumeration singleton.
Definition: resources.hpp:120
static Devices & instance()
Definition: resources.hpp:132
Devices(const Devices &)=delete
~Devices()=default
static int count()
Definition: resources.hpp:137
Devices(Devices &&)=delete
Devices & operator=(Devices &&)=delete
Devices & operator=(const Devices &)=delete
Host memory domain.
Definition: resources.hpp:60
Host()=default
Constructs a host memory environment
bool pinned() const
Definition: resources.hpp:97
bool pageable() const
Definition: resources.hpp:93
int id() const
Domain identifier constant.
Definition: resources.hpp:91
void deallocate(void *address, std::size_t nbytes) const
Releases host memory resources.
void * allocate(std::size_t nbytes) const
Allocates memory in host address space.
Definition: buffer.hpp:41
std::variant< Host, Device > Environment
Memory environment variant type.
Definition: resources.hpp:204