-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPage.h
91 lines (70 loc) · 3.06 KB
/
Page.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
/*
* Page.h
*
* Created on: Feb 1, 2021
* Author: tugrul
*/
#ifndef PAGE_H_
#define PAGE_H_
#include<vector>
#include<memory>
#include"ClCommandQueue.h"
#include"ClContext.h"
#include"AlignedCpuArray.h"
// active page that holds data in RAM by pinned buffers
template<typename T>
class Page
{
public:
// do not use this
Page(){arr=nullptr; edited=false;targetGpuPage=-1;}
// allocates pinned array for a "page", uses opencl way of pinning the array and is meant to be used in its own command queue
Page(int sz,ClContext ctxP, ClCommandQueue cqP, const bool usePinnedArraysOnly=true){ arr=std::shared_ptr<AlignedCpuArray<T>>(new AlignedCpuArray<T>(*ctxP.ctxPtr(),cqP.getQueue(),sz,4096,usePinnedArraysOnly)); edited=false; targetGpuPage=-1;}
// reading an element of virtual array
// i: index of element
T get(const int & i) const noexcept { return arr->getArray()[i]; }
// reading multiple elements, exclusively used for "read-only access",
const std::vector<T> getN(const int & i, const int & n) const noexcept {
const auto ptr = arr->getArray()+i;
return std::vector<T>(ptr,ptr+n);
}
// writing to an element of virtual array
// i: index of element
// val: element value to be written
void edit(const int & i, const T & val) const noexcept { arr->getArray()[i]=val;}
// writing multiple elements, exclusively used for "write-only access",
void editN(const int & i, const std::vector<T> & val, const size_t & valIndex, const size_t & n) const noexcept
{
std::copy(val.begin()+valIndex,val.begin()+valIndex+n,arr->getArray()+i);
}
// only copying, no allocation
void readN(T * const out, const int & i, const int & n) const noexcept {
const auto ptr = arr->getArray()+i;
std::copy(ptr,ptr+n,out);
}
// only copying, no allocation
void writeN(T * const in, const int & i, const size_t & n) const noexcept
{
std::copy(in,in+n,arr->getArray()+i);
}
void markAsEdited() noexcept { edited=true; }
// checks if page is edited (if true, paging system will upload data to graphics card in case of a storage requirement of a different frozen page)
bool isEdited() const noexcept { return edited; }
// clears "edited" status, only used after getting fresh data from a frozen page
void reset() noexcept { edited=false; }
// this changes index of frozen page (in a graphics card) is being written/read
void setTargetGpuPage(size_t g) noexcept { targetGpuPage=g; }
// this returns index of frozen page (in a graphics card) is being written/read
size_t getTargetGpuPage() const noexcept { return targetGpuPage; }
// internal logic, for get/set methods of VirtualArray
T * const ptr() const noexcept { return arr->getArray(); }
Page<T>& operator=(Page<T>&&) = default;
Page<T>& operator=(Page<T>&) = default;
Page(Page<T>&) = default;
~Page(){}
private:
std::shared_ptr<AlignedCpuArray<T>> arr;
bool edited;// todo: should make this smart pointer too, if a different paging algorithm is going to be used
size_t targetGpuPage; // todo: should make this smart pointer too, if a different paging algorithm is going to be used
};
#endif /* PAGE_H_ */