-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxn.h
266 lines (237 loc) · 6.81 KB
/
txn.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//
// Created by Shujian Qian on 2023-08-19.
//
#ifndef TXN_H
#define TXN_H
#include <cstdint>
#include <cstdlib>
#include <cassert>
#include <type_traits>
#include "gpu_txn.h"
#include "util_log.h"
#include "util_device_type.h"
#include "util_math.h"
#include <util_memory.h>
namespace epic {
struct BaseTxn
{
uint32_t txn_type;
uint8_t data[];
};
template<typename Txn>
struct BaseTxnSize
{
static constexpr size_t value = sizeof(BaseTxn) + sizeof(Txn);
};
template<typename Txn>
class TxnInputArray
{
public:
static constexpr size_t kBaseTxnSize = BaseTxnSize<Txn>::value;
void *txns = nullptr;
size_t num_txns;
size_t epochs;
TxnInputArray(size_t num_txns, size_t epochs)
: num_txns(num_txns)
, epochs(epochs)
{
txns = Malloc(kBaseTxnSize * num_txns * epochs);
}
~TxnInputArray()
{
Free(txns);
}
inline BaseTxn *getTxn(size_t epoch, size_t index)
{
assert(index < num_txns);
assert(epoch < epochs);
size_t offset = (epoch * num_txns + index) * kBaseTxnSize;
return reinterpret_cast<BaseTxn *>(reinterpret_cast<uint8_t *>(txns) + offset);
}
};
template<typename TxnType>
class TxnArray
{
public:
static constexpr size_t kBaseTxnSize = BaseTxnSize<TxnType>::value;
void *txns = nullptr;
size_t num_txns = 0;
DeviceType device = DeviceType::CPU;
TxnArray() = default;
explicit TxnArray(size_t num_txns)
: num_txns(num_txns)
, device(DeviceType::CPU)
{}
TxnArray(size_t num_txns, DeviceType device_type, bool initialize = true)
: num_txns(num_txns)
, device(device_type)
{
if (!initialize)
{
return;
}
if (device_type == DeviceType::CPU)
{
txns = Malloc(kBaseTxnSize * num_txns);
}
#ifdef EPIC_CUDA_AVAILABLE
else if (device_type == DeviceType::GPU)
{
txns = createGpuTxnArrayStorage(kBaseTxnSize * num_txns);
}
#endif
else
{
auto &logger = Logger::GetInstance();
logger.Error("Unsupported device type");
exit(-1);
}
}
void Initialize()
{
auto &logger = Logger::GetInstance();
if (txns != nullptr)
{
logger.Error("TxnArray already initialized");
exit(-1);
}
if (device == DeviceType::CPU)
{
logger.Trace("Allocating {} bytes for {} txns on CPU", formatSizeBytes(kBaseTxnSize * num_txns), num_txns);
txns = Malloc(kBaseTxnSize * num_txns);
}
#ifdef EPIC_CUDA_AVAILABLE
else if (device == DeviceType::GPU)
{
logger.Trace("Allocating {} bytes for {} txns on GPU", formatSizeBytes(kBaseTxnSize * num_txns), num_txns);
txns = createGpuTxnArrayStorage(kBaseTxnSize * num_txns);
}
#endif // EPIC_CUDA_AVAILABLE
else
{
logger.Error("Unsupported device type");
exit(-1);
}
}
void Destroy()
{
auto &logger = Logger::GetInstance();
if (txns == nullptr)
{
logger.Error("TxnArray already destroyed");
exit(-1);
}
if (device == DeviceType::CPU)
{
logger.Trace(
"Deallocating {} bytes for {} txns on CPU", formatSizeBytes(kBaseTxnSize * num_txns), num_txns);
Free(txns);
txns = nullptr;
}
#ifdef EPIC_CUDA_AVAILABLE
else if (device == DeviceType::GPU)
{
logger.Trace(
"Deallocating {} bytes for {} txns on GPU", formatSizeBytes(kBaseTxnSize * num_txns), num_txns);
txns = destroyGpuTxnArrayStorage(txns);
}
#endif
}
inline BaseTxn *getTxn(size_t index)
{
assert(index < num_txns);
size_t offset = index * kBaseTxnSize;
return reinterpret_cast<BaseTxn *>(reinterpret_cast<uint8_t *>(txns) + offset);
}
friend class TxnBridge;
};
class TxnRunner
{
public:
void run(BaseTxn *txn);
};
template<typename TxnType>
class PackedTxnArray
{
public:
static constexpr uint32_t kBaseTxnSize = BaseTxnSize<TxnType>::value;
uint32_t num_txns;
uint8_t *txns = nullptr;
uint32_t *index = nullptr; // num_txns + 1 int, the last one is the total size
uint32_t capacity;
uint32_t size = 0;
DeviceType device = DeviceType::CPU;
PackedTxnArray() = default;
explicit PackedTxnArray(uint32_t num_txns)
: num_txns(num_txns)
, capacity(kBaseTxnSize * num_txns)
{}
PackedTxnArray(uint32_t num_txns, DeviceType device, bool initialize = true)
: num_txns(num_txns)
, capacity(kBaseTxnSize * num_txns)
, device(device)
{
if (!initialize)
{
return;
}
if (device == DeviceType::CPU)
{
txns = static_cast<uint8_t *>(Malloc(capacity));
index = static_cast<uint32_t *>(Malloc((num_txns + 1) * sizeof(uint32_t)));
}
#ifdef EPIC_CUDA_AVAILABLE
else if (device == DeviceType::GPU)
{
txns = static_cast<uint8_t *>(createGpuTxnArrayStorage(capacity));
index = static_cast<uint32_t *>(createGpuTxnArrayStorage((num_txns + 1) * sizeof(uint32_t)));
}
#endif
else
{
throw std::runtime_error("Unsupported device type");
}
}
void Initialize()
{
auto &logger = Logger::GetInstance();
if (txns != nullptr)
{
logger.Error("TxnArray already initialized");
exit(-1);
}
if (device == DeviceType::CPU)
{
logger.Trace("Allocating {} bytes for {} txns on CPU", formatSizeBytes(capacity), num_txns);
txns = static_cast<uint8_t *>(Malloc(capacity));
index = static_cast<uint32_t *>(Malloc((num_txns + 1) * sizeof(uint32_t)));
}
#ifdef EPIC_CUDA_AVAILABLE
else if (device == DeviceType::GPU)
{
logger.Trace("Allocating {} bytes for {} txns on GPU", formatSizeBytes(capacity), num_txns);
txns = static_cast<uint8_t *>(createGpuTxnArrayStorage(capacity));
index = static_cast<uint32_t *>(createGpuTxnArrayStorage((num_txns + 1) * sizeof(uint32_t)));
}
#endif // EPIC_CUDA_AVAILABLE
else
{
throw std::runtime_error("Unsupported device type");
}
}
inline BaseTxn *getTxn(size_t txn_id)
{
if (device != DeviceType::CPU)
{
throw std::runtime_error("Unsupported device type");
}
assert(txn_id < num_txns);
assert(index[txn_id] < size);
return reinterpret_cast<BaseTxn *>(&txns[index[txn_id]]);
}
friend class TxnBridge;
friend class PackedTxnBridge;
friend class PackedTxnArrayBuilder;
};
} // namespace epic
#endif // TXN_H