Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * Copyright (C) 2022 Jiho Chu <jiho.chu@samsung.com>
4 : *
5 : * @file cache_elem.h
6 : * @date 28 Nov 2022
7 : * @see https://github.com/nnstreamer/nntrainer
8 : * @author Jiho Chu <jiho.chu@samsung.com>
9 : * @bug No known bugs except for NYI items
10 : * @brief Cache elem class
11 : *
12 : */
13 :
14 : #ifndef __CACHE_ELEM_H__
15 : #define __CACHE_ELEM_H__
16 :
17 : #include <memory_data.h>
18 : #include <swap_device.h>
19 :
20 : namespace nntrainer {
21 :
22 : enum CachePolicy {
23 : WRITE_BACK = 0b0001, /**< invalidate will write to device */
24 : NO_WRITE_BACK = 0b0010, /**< invalidate will not write to device */
25 : READ_CONSIST = 0b0100, /**< validate will read from device */
26 : NO_READ_CONSIST = 0b1000, /**< validate will not read from device */
27 : ALWAYS_SYNCED =
28 : (READ_CONSIST | WRITE_BACK), /**< Always synchronized with device */
29 : TEMPORAL = (NO_READ_CONSIST |
30 : NO_WRITE_BACK), /**< Will not be synchronized with device */
31 : FIRST_LAST_SKIP = 0b10000,
32 : /**< Will skip first read and last write */
33 : FRIST_WRITE_CONSIST = 0b100000, /**< First invalidate will write to device */
34 : ITERATION_CONSIST = (FIRST_LAST_SKIP | ALWAYS_SYNCED),
35 : /**< Will skip first read and last write. other behaviors will be same as
36 : ALWAYS_SYNCED */
37 : SYNC_ONCE = (FRIST_WRITE_CONSIST | READ_CONSIST | NO_WRITE_BACK),
38 : /**< Will sync at first from the device, and the value will always consist */
39 : };
40 :
41 : /**
42 : * @class CacheElem
43 : * @brief Cache element containing swap address
44 : */
45 : class CacheElem {
46 : public:
47 : enum Options {
48 : NONE = 0b0000, /**< No option */
49 : FIRST_ACCESS = 0x0001, /**< First Access */
50 : LAST_ACCESS = 0x0010, /**< Last Access */
51 : FIRST_WRITE = 0x0100, /**< First Write */
52 : FIRST_ACCESS_WRITE = FIRST_ACCESS | FIRST_WRITE,
53 : /**< First access & write */
54 : };
55 :
56 : /**
57 : * @brief CacheElem default constructor
58 : *
59 : */
60 : explicit CacheElem(std::shared_ptr<SwapDevice> dev, unsigned int mem_id,
61 : size_t off, size_t len, std::shared_ptr<MemoryData> data,
62 : CachePolicy pol = CachePolicy::ALWAYS_SYNCED,
63 0 : void *ptr = nullptr) :
64 0 : initial_opt(Options::FIRST_ACCESS_WRITE),
65 : device(dev),
66 0 : active(false),
67 0 : id(mem_id),
68 0 : offset(off),
69 0 : length(len),
70 0 : policy(pol),
71 : mem_data(data),
72 0 : memory_ptr(ptr),
73 0 : load_task_id(-1),
74 0 : unload_task_id(-1) {}
75 :
76 : /**
77 : * @brief CacheElem destructor
78 : *
79 : */
80 0 : virtual ~CacheElem() {}
81 :
82 : /**
83 : * @brief load data from swap device
84 : *
85 : * @param alloc_only only allocate buffer without reading data
86 : */
87 : void swapIn(Options opt = Options::NONE);
88 :
89 : /**
90 : * @brief unload data to swap device
91 : *
92 : * @param dealloc_only only deallocate buffer without writing data
93 : */
94 : void swapOut(Options opt = Options::NONE);
95 :
96 : /**
97 : * @brief unload data to swap device
98 : *
99 : * @return active status
100 : */
101 0 : bool isActive() const { return active; }
102 :
103 : /**
104 : * @brief inactive the elem
105 : *
106 : */
107 0 : void inActive() { active = false; }
108 : /**
109 : * @brief get length of cache element
110 : *
111 : * @return length of cache element in byte
112 : */
113 : size_t getLength() const { return length; }
114 :
115 : /**
116 : * @brief get id of cache element
117 : *
118 : * @return cache element id
119 : */
120 : unsigned int getId() const { return id; }
121 :
122 : /**
123 : * @brief reset access count
124 : *
125 : */
126 0 : void reset() { initial_opt = Options::FIRST_ACCESS_WRITE; }
127 :
128 : /**
129 : * @brief set Load Task ID for Unload
130 : *
131 : */
132 0 : void setLoadTaskID(int id) { load_task_id = id; }
133 :
134 : /**
135 : * @brief getter of Load Task ID
136 : *
137 : * @return load Task id
138 : */
139 0 : int getLoadTaskID() { return load_task_id; }
140 :
141 : /**
142 : * @brief set Unload Task ID for Load
143 : *
144 : */
145 0 : void setUnloadTaskID(int id) { unload_task_id = id; }
146 :
147 : /**
148 : * @brief getter of Unload Task ID
149 : *
150 : * @return unload Task id
151 : */
152 0 : int getUnloadTaskID() { return unload_task_id; }
153 :
154 : private:
155 : Options initial_opt; /**< accessed */
156 : std::shared_ptr<SwapDevice> device; /**< swap device */
157 : bool active; /**< element is loaded */
158 : unsigned int id; /**< memory id */
159 : size_t offset; /**< element offset from swap device */
160 : size_t length; /**< element size */
161 : CachePolicy policy; /**< cache policy */
162 : std::shared_ptr<MemoryData> mem_data; /**< allocated memory data */
163 : void *memory_ptr; /** memory ptr*/
164 : int load_task_id; /** load task id*/
165 : int unload_task_id; /** unload task id*/
166 : };
167 :
168 : } // namespace nntrainer
169 :
170 : #endif /** __CACHE_ELEM_H__ */
|