This repository was archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathAbstractBuffer.h
159 lines (135 loc) · 4.57 KB
/
AbstractBuffer.h
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
/*
* Copyright 2017 MapD Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file AbstractBuffer.h
* @author Steven Stewart <[email protected]>
* @author Todd Mostak <[email protected]>
*/
#pragma once
#include <atomic>
#include <memory>
#ifdef BUFFER_MUTEX
#include <boost/thread/locks.hpp>
#include <boost/thread/shared_mutex.hpp>
#endif
#include "Encoder.h"
#include "MemoryLevel.h"
#include "IR/Type.h"
#include "Logger/Logger.h"
#include "Shared/types.h"
namespace Data_Namespace {
/**
* @class AbstractBuffer
* @brief An AbstractBuffer is a unit of data management for a data manager.
*/
// enum BufferType {FILE_BUFFER, CPU_BUFFER, GPU_BUFFER};
class AbstractDataToken {
public:
virtual ~AbstractDataToken() = default;
virtual const int8_t* getMemoryPtr() const = 0;
virtual size_t getSize() const = 0;
virtual const hdk::ir::Type* getType() const = 0;
};
class AbstractBuffer {
public:
AbstractBuffer(const int device_id)
: encoder_(nullptr)
, size_(0)
, device_id_(device_id)
, is_dirty_(false)
, is_appended_(false)
, is_updated_(false) {}
AbstractBuffer(const int device_id, const hdk::ir::Type* type)
: size_(0)
, device_id_(device_id)
, is_dirty_(false)
, is_appended_(false)
, is_updated_(false) {
initEncoder(type);
}
virtual ~AbstractBuffer() {}
virtual void read(int8_t* const dst,
const size_t num_bytes,
const size_t offset = 0,
const MemoryLevel dst_buffer_type = CPU_LEVEL,
const int dst_device_id = -1) = 0;
virtual void write(int8_t* src,
const size_t num_bytes,
const size_t offset = 0,
const MemoryLevel src_buffer_type = CPU_LEVEL,
const int src_device_id = -1) = 0;
virtual void reserve(size_t num_bytes) = 0;
virtual void append(int8_t* src,
const size_t num_bytes,
const MemoryLevel src_buffer_type = CPU_LEVEL,
const int device_id = -1) = 0;
virtual int8_t* getMemoryPtr() = 0;
virtual void setMemoryPtr(int8_t* new_ptr) { CHECK(false); }
virtual size_t pageCount() const = 0;
virtual size_t pageSize() const = 0;
virtual size_t reservedSize() const = 0;
virtual MemoryLevel getType() const = 0;
// Next three methods are dummy methods so FileBuffer does not implement these
virtual inline int pin() { return 0; }
virtual inline int unPin() { return 0; }
virtual inline int getPinCount() { return 0; }
virtual inline void deleteWhenUnpinned() {}
// These getters should not vary when inherited and therefore don't need to be virtual.
inline size_t size() const { return size_; }
inline int getDeviceId() const { return device_id_; }
inline bool isDirty() const { return is_dirty_; }
inline bool isAppended() const { return is_appended_; }
inline bool isUpdated() const { return is_updated_; }
inline bool hasEncoder() const { return (encoder_ != nullptr); }
inline const hdk::ir::Type* type() const { return type_; }
inline Encoder* getEncoder() const {
CHECK(hasEncoder());
return encoder_.get();
}
inline void setDirty() { is_dirty_ = true; }
inline void setUpdated() {
is_updated_ = true;
is_dirty_ = true;
}
inline void setAppended() {
is_appended_ = true;
is_dirty_ = true;
}
inline void setSize(const size_t size) { size_ = size; }
inline void clearDirtyBits() {
is_appended_ = false;
is_updated_ = false;
is_dirty_ = false;
}
void initEncoder(const hdk::ir::Type* tmp_type);
void syncEncoder(const AbstractBuffer* src_buffer);
void copyTo(AbstractBuffer* destination_buffer, const size_t num_bytes = 0);
void resetToEmpty();
protected:
std::unique_ptr<Encoder> encoder_;
const hdk::ir::Type* type_;
size_t size_;
int device_id_;
private:
bool is_dirty_;
bool is_appended_;
bool is_updated_;
#ifdef BUFFER_MUTEX
boost::shared_mutex read_write_mutex_;
boost::shared_mutex append_mutex_;
#endif
};
} // namespace Data_Namespace