-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbuffer.cpp
More file actions
71 lines (60 loc) · 1.83 KB
/
buffer.cpp
File metadata and controls
71 lines (60 loc) · 1.83 KB
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <libspdl/core/buffer.h>
#include "libspdl/common/logging.h"
#include <fmt/format.h>
#include <glog/logging.h>
namespace spdl::core {
////////////////////////////////////////////////////////////////////////////////
// CPUBuffer
////////////////////////////////////////////////////////////////////////////////
void* CPUBuffer::data() {
return storage->data();
}
////////////////////////////////////////////////////////////////////////////////
// Factory functions
////////////////////////////////////////////////////////////////////////////////
namespace {
inline size_t prod(const std::vector<size_t>& shape) {
size_t val = 1;
for (auto& v : shape) {
val *= v;
}
return val;
}
} // namespace
CPUBufferPtr cpu_buffer(
const std::vector<size_t>& shape,
ElemClass elem_class,
size_t depth,
std::shared_ptr<CPUStorage> storage) {
size_t size = depth * prod(shape);
VLOG(5) << fmt::format(
"Allocating {} bytes. (shape: {}, elem: {})",
size,
fmt::join(shape, ", "),
depth);
if (storage) {
if (storage->size < size) [[unlikely]] {
SPDL_FAIL(
fmt::format(
"The provided storage does not have enough capacity. ({} < {})",
storage->size,
size));
}
}
// The following does not compile on Apple clang 15
// return std::make_unique<CPUBuffer>(storage, shape, elem_class, depth);
auto ret = std::make_unique<CPUBuffer>();
ret->storage = storage ? storage : std::make_shared<CPUStorage>(size);
ret->shape = shape;
ret->elem_class = elem_class;
ret->depth = depth;
return ret;
}
} // namespace spdl::core