Repository navigation
Expand file tree
/
Copy pathStorageObjects.h
More file actions
299 lines (258 loc) · 11.5 KB
/
Copy pathStorageObjects.h
File metadata and controls
299 lines (258 loc) · 11.5 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
Copyright [2024] [Yao Yao]
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.
*/
#pragma once
#include <memory>
#include "cpp_utils.h"
#include "CudaMemPool.h"
#include <fstream>
#include "CudaArray.h"
#include "CudaEventPool.h"
#include "StorageFwd.h"
#define ENABLE_BLOCK_LINEAR_STORAGE 1
namespace cudapp
{
namespace storage
{
class ObjStorageBase
{
public:
// Do not call clear() in this destructor, because clear calls virtual functions
virtual ~ObjStorageBase();
virtual size_t getElemBytes() const = 0;
// in number of elements
size_t getSize() const {return mSize;}
bool empty() const { return getSize() == 0; }
void resize(size_t size, cudaStream_t stream);
void clear(cudaStream_t stream);
// not necessary, just for sanity check
std::shared_ptr<size_t> sizeInStream() const {return mSizeInStream;}
protected:
ObjStorageBase(size_t size);
ObjStorageBase(const ObjStorageBase&) = delete;
ObjStorageBase& operator=(const ObjStorageBase&) = delete;
ObjStorageBase(ObjStorageBase&& other);
ObjStorageBase& operator=(ObjStorageBase&& other);
void setSize(size_t size, cudaStream_t stream);
private:
// Caller must call cudaStreamWaitEvent(stream, getReadyEvent(), 0) / cudaEventRecord(getReadyEvent(), stream) before/after this two APIs.
virtual void allocInStreamImpl(size_t size, cudaStream_t stream) = 0;
virtual void freeInStreamImpl(cudaStream_t stream) = 0;
private:
size_t mSize = 0; // in number of elements
std::shared_ptr<size_t> mSizeInStream = std::make_shared<size_t>(0u); // not necessary, just for assertions
};
template <CudaMemType memType, typename ElemType>
class CudaMemStorage : public ObjStorageBase
{
public:
using CudaMemPoolType = CudaMemPool<memType>;
public:
CudaMemStorage(CudaMemPoolType& pool)
: ObjStorageBase(0)
, mPool(&pool)
, mData{nullptr, {}}
{}
CudaMemStorage(typename CudaMemPool<memType>::template PooledCudaMem<ElemType>&& mem, size_t size)
: ObjStorageBase(size)
, mPool {mem.get_deleter().pool}
, mData {std::move(mem)}
{}
size_t getElemBytes() const override {return sizeof(ElemType);}
const CudaMemPoolType& getMemPool() const {return mPool;}
void notifyMigratedToStream(cudaStream_t stream) {
mData.get_deleter().stream = stream;
}
// do not call data() from a callback in stream. It should be called in host
ElemType* data() const {return mData.get();}
private:
void allocInStreamImpl(size_t size, cudaStream_t stream) final{
mData = mPool->template alloc<ElemType, true>(size, stream);// data pointer is available immediately in host, but the memory is not until previous work in stream is finished
}
void freeInStreamImpl(cudaStream_t stream) final {
REQUIRE(stream == mData.get_deleter().stream);
mData.reset();
}
private:
CudaMemPoolType* mPool = nullptr;
typename CudaMemPoolType::template PooledCudaMem<ElemType> mData;
};
template <typename ElemType>
using CudaDevMemStorage = CudaMemStorage<CudaMemType::kDevice, ElemType>;
template <typename ElemType>
using CudaPinnedMemStorage = CudaMemStorage<CudaMemType::kPinned, ElemType>;
template <typename ElemType>
using CudaSysMemStorage = CudaMemStorage<CudaMemType::kSystem, ElemType>;
template <typename ElemType>
class DiskStorage : public ObjStorageBase
{
public:
using ObjStorageBase::getSize;
public:
DiskStorage(const fs::path& path, size_t size)
: ObjStorageBase(size)
, mPath(std::make_shared<const fs::path>(path))
{
}
size_t getElemBytes() const override {return sizeof(ElemType);}
fs::path data() const {return *mPath;}
// memory referred by data must be valid until finish of the async callback.
void write(const ElemType* data, size_t size, cudaStream_t stream){
REQUIRE(getSize() == size);
launchCudaHostFunc(stream, [inStreamSize{sizeInStream()}, path{mPath}, data, size]{
REQUIRE(*inStreamSize == size);
std::ofstream fout;
fout.exceptions(std::ios::badbit | std::ios::failbit);
fout.open(*path, std::ios::binary | std::ios::trunc);
fout.write(reinterpret_cast<const char*>(data), static_cast<std::streamsize>(sizeof(ElemType) * size));
fout.close();
});
}
// memory referred by data must be valid until finish of the async callback.
void read(ElemType* data, size_t size, cudaStream_t stream) const{
REQUIRE(getSize() == size);
launchCudaHostFunc(stream, [inStreamSize{sizeInStream()}, path{mPath}, data, size]{
REQUIRE(size == *inStreamSize);
REQUIRE(fs::exists(*path) && fs::file_size(*path) == sizeof(ElemType) * size);
std::ifstream fin;
fin.exceptions(std::ios::badbit | std::ios::failbit | std::ios::eofbit);
fin.open(*path, std::ios::binary);
fin.read(reinterpret_cast<char*>(data), static_cast<std::streamsize>(sizeof(ElemType) * size));
});
}
private:
void allocInStreamImpl(size_t size, cudaStream_t stream) override{
unused(size, stream);
}
void freeInStreamImpl(cudaStream_t stream) override {
launchCudaHostFunc(stream, [inStreamSize{sizeInStream()}, path{mPath}](){
REQUIRE(*inStreamSize != 0);
fs::remove(*path);
});
}
private:
std::shared_ptr<const fs::path> mPath;
};
#if ENABLE_BLOCK_LINEAR_STORAGE
template <typename ElemType>
class BlockLinearStorage : public ObjStorageBase
{
public:
using CudaMemPoolType = CudaMemPool<CudaMemType::kDevice>;
BlockLinearStorage(CudaMemPoolType& pool, size_t width, size_t height, unsigned arrayFlags = cudaArrayDefault)
: ObjStorageBase{0}
, mPool{pool}
, mAttributes{cudaCreateChannelDesc<ElemType>(), {width, height, 0}, static_cast<unsigned int>(arrayFlags)}
{}
BlockLinearStorage(PooledCudaArray&& array)
: ObjStorageBase(getCudaArrayNbPixels(array.get())) // This is the number of elements used to compute size for pinned/sys mem and disk file)
, mPool {*array.get_deleter().pool}
, mAttributes{getCudaArrayAttributes(array.get())}
, mArray {std::move(array)}
{}
void notifyMigratedToStream(cudaStream_t stream) {
mArray.get_deleter().stream = stream;
}
size_t getElemBytes() const override {return sizeof(ElemType);}
const CudaMemPoolType& getMemPool() const {return mPool;}
// do not call data() from a callback in stream. It should be called in host
cudaArray_t data() const {return mArray.get();}
CudaArrayAttributes getAttributes() const {return mAttributes;}
private:
void allocInStreamImpl(size_t size, cudaStream_t stream) final{
ASSERT(size == mAttributes.extent.width * mAttributes.extent.height && mAttributes.extent.depth == 0);
mArray = mPool.allocArray(mAttributes, stream); // cuda array is available immediately in host, but the memory is not until previous work in stream is finished
}
void freeInStreamImpl(cudaStream_t stream) final {
ASSERT(stream == mArray.get_deleter().stream);
mArray.reset();
}
private:
CudaMemPoolType& mPool;
CudaArrayAttributes mAttributes{};
PooledCudaArray mArray;
};
#endif
// caller must guarantee that src data and dst space is available in the specified stream
template <typename ElemType, CudaMemType srcType, CudaMemType dstType>
std::enable_if_t<(srcType == CudaMemType::kDevice && dstType == CudaMemType::kPinned) || (srcType == CudaMemType::kPinned && dstType == CudaMemType::kDevice), void>
storageMemcpyAsync(const CudaMemStorage<srcType, ElemType>& src, CudaMemStorage<dstType, ElemType>& dst, cudaStream_t stream)
{
cudaCheck(cudaMemcpyAsync(dst.data(), src.data(), sizeof(ElemType)*src.getSize(), dstType == CudaMemType::kPinned ? cudaMemcpyDeviceToHost : cudaMemcpyHostToDevice, stream));
}
// caller must guarantee that src data and dst space is available in the specified stream
template <typename ElemType, CudaMemType srcType, CudaMemType dstType>
std::enable_if_t<(srcType == CudaMemType::kPinned && dstType == CudaMemType::kSystem) || (srcType == CudaMemType::kSystem && dstType == CudaMemType::kPinned), void>
storageMemcpyAsync(const CudaMemStorage<srcType, ElemType>& src, CudaMemStorage<dstType, ElemType>& dst, cudaStream_t stream){
const size_t size = src.getSize();
REQUIRE(dst.getSize() == size);
const auto srcPtr = src.data();
const auto dstPtr = dst.data();
launchCudaHostFunc(stream, [srcPtr, dstPtr, size, inStreamSize{dst.sizeInStream()}](){
REQUIRE(*inStreamSize == size);
std::copy_n(srcPtr, size, dstPtr);
});
}
template <typename ElemType, CudaMemType srcType>
std::enable_if_t<srcType == CudaMemType::kPinned || srcType == CudaMemType::kSystem, void>
storageMemcpyAsync(const CudaMemStorage<srcType, ElemType>& src, DiskStorage<ElemType>& dst, cudaStream_t stream) {
REQUIRE(src.getSize() == dst.getSize());
dst.write(src.data(), src.getSize(), stream);
}
template <typename ElemType, CudaMemType dstType>
std::enable_if_t<dstType == CudaMemType::kPinned || dstType == CudaMemType::kSystem, void>
storageMemcpyAsync(const DiskStorage<ElemType>& src, CudaMemStorage<dstType, ElemType>& dst, cudaStream_t stream) {
REQUIRE(src.getSize() == dst.getSize());
src.read(dst.data(), dst.getSize(), stream);
}
#if ENABLE_BLOCK_LINEAR_STORAGE
// caller must guarantee that src data and dst space is available in the specified stream
template <typename ElemType>
void storageMemcpyAsync(const BlockLinearStorage<ElemType>& src, CudaMemStorage<CudaMemType::kPinned, ElemType>& dst, cudaStream_t stream)
{
const auto srcAttr = src.getAttributes();
cudaCheck(cudaMemcpy2DFromArrayAsync(dst.data(), sizeof(ElemType) * srcAttr.extent.width, src.data(), 0, 0, sizeof(ElemType) * srcAttr.extent.width, srcAttr.extent.height, cudaMemcpyDeviceToHost, stream));
}
template <typename ElemType>
void storageMemcpyAsync(const CudaMemStorage<CudaMemType::kPinned, ElemType>& src, BlockLinearStorage<ElemType>& dst, cudaStream_t stream)
{
const auto dstAttr = dst.getAttributes();
cudaCheck(cudaMemcpy2DToArrayAsync(dst.data(), 0, 0, src.data(), sizeof(ElemType) * dstAttr.extent.width, sizeof(ElemType) * dstAttr.extent.width, dstAttr.extent.height, cudaMemcpyHostToDevice, stream));
}
#endif
// mayOmit = true: omit if dst is not empty
template <typename SrcStorageType, typename DstStorageType>
void migrateStorage(bool mayOmit, bool keepSrc, SrcStorageType& src, DstStorageType& dst, cudaStream_t stream)
{
REQUIRE(!src.empty());
if constexpr (std::is_same_v<SrcStorageType, DstStorageType>){
unused(stream);
REQUIRE(mayOmit && keepSrc);
REQUIRE(&src == &dst);
return;
}
else {
if (!mayOmit)
{
REQUIRE(dst.empty());
}
if (dst.empty()){ // (!mayOmit || (mayOmit && dst.empty())), optimized with if(!mayOmit){REQUIRE(dst.empty();}
dst.resize(src.getSize(), stream);
storageMemcpyAsync(src, dst, stream);
}
if (!keepSrc){
src.clear(stream);
}
}
}
} // namespace storage
} // namespace cudapp