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.cpp
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 : #include "cache_elem.h"
15 :
16 : #include <profiler.h>
17 :
18 : namespace nntrainer {
19 :
20 : namespace {
21 :
22 : std::map<CachePolicy, std::string> policyToStr = {
23 : {WRITE_BACK, "WRITE_BACK"},
24 : {NO_WRITE_BACK, "NO_WRITE_BACK"},
25 : {READ_CONSIST, "READ_CONSIST"},
26 : {NO_READ_CONSIST, "NO_READ_CONSIST"},
27 : {ALWAYS_SYNCED, "ALWAYS_SYNCED"},
28 : {TEMPORAL, "TEMPORAL"},
29 : {FIRST_LAST_SKIP, "FIRST_LAST_SKIP"},
30 : {ITERATION_CONSIST, "ITER_CONSIST"},
31 : {SYNC_ONCE, "SYNC_ONCE"}};
32 :
33 : inline bool checkAllocOnly(CachePolicy policy, CacheElem::Options opt) {
34 0 : return ((policy & CachePolicy::NO_READ_CONSIST) ||
35 0 : ((opt & CacheElem::Options::FIRST_ACCESS) &&
36 : (policy & CachePolicy::FIRST_LAST_SKIP)));
37 : }
38 :
39 : inline bool checkDeallocOnly(CachePolicy policy, CacheElem::Options opt) {
40 0 : return ((policy & CachePolicy::NO_READ_CONSIST) ||
41 0 : ((opt & CacheElem::Options::LAST_ACCESS) &&
42 0 : (policy & CachePolicy::FIRST_LAST_SKIP)) ||
43 0 : ((policy & FRIST_WRITE_CONSIST) &&
44 : !(opt & CacheElem::Options::FIRST_WRITE)));
45 : }
46 :
47 : } // namespace
48 :
49 0 : void CacheElem::swapIn(Options opt) {
50 0 : opt = static_cast<Options>(opt | initial_opt);
51 0 : bool alloc_only = checkAllocOnly(policy, opt);
52 0 : void *buf = device->getBuffer(offset, length, memory_ptr, id - 1, alloc_only);
53 :
54 0 : initial_opt = static_cast<Options>(initial_opt & ~Options::FIRST_ACCESS);
55 : mem_data->setAddr((void *)buf);
56 : mem_data->setValid(true);
57 0 : active = true;
58 : #ifdef PROFILE
59 : std::string msg("CacheElem(");
60 : msg += device->getDevicePath() + ") #" + std::to_string(id);
61 : PROFILE_CACHE_ALLOC(buf, length, msg, policyToStr[policy], !alloc_only);
62 : #endif
63 0 : }
64 :
65 0 : void CacheElem::swapOut(Options opt) {
66 0 : opt = static_cast<Options>(opt | initial_opt);
67 0 : bool dealloc_only = checkDeallocOnly(policy, opt);
68 : void *buf = (void *)mem_data->getAddr();
69 0 : initial_opt = static_cast<Options>(initial_opt & ~Options::FIRST_WRITE);
70 0 : device->putBuffer(buf, dealloc_only);
71 : mem_data->setAddr(nullptr);
72 : mem_data->setValid(false);
73 0 : active = false;
74 : #ifdef PROFILE
75 : PROFILE_CACHE_DEALLOC(buf, policyToStr[policy], !dealloc_only);
76 : #endif
77 0 : }
78 :
79 : } // namespace nntrainer
|