xaiefal
Loading...
Searching...
No Matches
xaiefal-rsc-base.hpp
Go to the documentation of this file.
1// Copyright(C) 2020 - 2021 by Xilinx, Inc. All rights reserved.
2// SPDX-License-Identifier: MIT
7
8#include <fstream>
9#include <functional>
10#include <map>
11#include <memory>
12#include <unordered_map>
13#include <vector>
14#include <xaiengine.h>
15#include <xaiefal/common/xaiefal-base.hpp>
16#include <xaiefal/common/xaiefal-log.hpp>
17
18#pragma once
19
20namespace xaiefal {
21 class XAieRsc;
22
27 struct XAieUserRsc {
29 XAie_LocType Loc {
30 XAie_TileLoc(XAIE_LOC_ANY, XAIE_LOC_ANY)
31 };
32
36
40
41 uint32_t RscId {
42 static_cast<uint32_t>(XAIE_RSC_ID_ANY)
43 };
44 };
45
50 struct XAieRscState {
51 uint8_t Initialized: 1;
52 uint8_t Configured: 1;
53 uint8_t Reserved: 1;
54 uint8_t Prereserved: 1;
61 uint8_t Running: 1;
62 };
63
69 class XAieRsc {
70 public:
71 XAieRsc() = delete;
72 XAieRsc(std::shared_ptr<XAieDevHandle> DevHd):
73 State(), AieHd(DevHd) {
74 if (!DevHd) {
75 throw std::invalid_argument("aie rsc: empty device handle");
76 }
78 Type = static_cast<XAieRscType>(XAIE_RSC_TYPE_ANY);
79 }
80 XAieRsc(std::shared_ptr<XAieDevHandle> DevHd, XAieRscType Type):
81 State(), Type(Type), AieHd(DevHd) {
82 if (!DevHd) {
83 throw std::invalid_argument("aie rsc: empty device handle");
84 }
86 }
88 State(), AieHd(Dev.getDevHandle()) {
90 Type = static_cast<XAieRscType>(XAIE_RSC_TYPE_ANY);
91 }
92 virtual ~XAieRsc() {
93 if (State.Running == 1) {
94 stop();
95 } else if (State.Reserved == 1) {
96 if (State.Prereserved == 1) {
97 free();
98 } else {
99 release();
100 }
101 }
102 }
103
113
114 AieRC setPreferredId(uint32_t id, bool isStaticAllocated = false) {
115 AieRC RC;
116
117 if (State.Reserved == 1) {
118 RC = XAIE_INVALID_ARGS;
119 } else {
120 preferredId = id;
121 RC = XAIE_OK;
122 if (isStaticAllocated) {
123 if (id == XAIE_RSC_ID_ANY) {
124 RC = XAIE_INVALID_ARGS;
125 Logger::log(LogLevel::FAL_ERROR) << __func__ << " " <<
126 typeid(*this).name() <<
127 " If Rsc ID is any, cannot be statically allocated" << std::endl;
128 } else {
129 State.Prereserved = 1;
130 }
131 }
132 }
133
134 return RC;
135 }
136
141 uint32_t getPreferredId() {
142 return preferredId;
143 }
144
161 AieRC reserve() {
162 AieRC RC;
163
164 if (State.Reserved == 1) {
165 Logger::log(LogLevel::FAL_ERROR) << __func__ << " " <<
166 typeid(*this).name() << " resource has been allocated." << std::endl;
167 RC = XAIE_ERR;
168 } else if (State.Initialized == 0) {
169 Logger::log(LogLevel::FAL_ERROR) << __func__ << " " <<
170 typeid(*this).name() << " resource not configured." << std::endl;
171 RC = XAIE_ERR;
172 } else {
173 RC = _reserve();
174 if (RC == XAIE_OK) {
175 State.Reserved = 1;
176 }
177 }
178 return RC;
179 }
180
188 AieRC release() {
189 AieRC RC = XAIE_OK;
190
191 if (State.Running == 1) {
192 Logger::log(LogLevel::FAL_ERROR) << __func__ << " " <<
193 typeid(*this).name() << "resource is running." << std::endl;
194 RC = XAIE_ERR;
195 } else if (State.Reserved == 1) {
196 RC = _release();
197 State.Reserved = 0;
198 State.Prereserved = 0;
199 }
200 return RC;
201 }
202
213 AieRC free() {
214 AieRC RC = XAIE_OK;
215
216 if (State.Prereserved == 0) {
217 Logger::log(LogLevel::FAL_ERROR) << __func__ << " " <<
218 typeid(*this).name() << " resource is not preserved." << std::endl;
219 RC = XAIE_INVALID_ARGS;
220 } else {
221 if (State.Running == 1) {
222 Logger::log(LogLevel::FAL_ERROR) << __func__ << " " <<
223 typeid(*this).name() << " resource is running." << std::endl;
224 RC = XAIE_INVALID_ARGS;
225 } else if (State.Reserved == 1) {
226 RC = _free();
227 State.Reserved = 0;
228 }
229 }
230 return RC;
231 }
232
239 AieRC start() {
240 AieRC RC = XAIE_OK;
241
242 if (State.Running == 1) {
243 RC = XAIE_OK;
244 } else if (State.Reserved == 1) {
245 if (State.Configured == 1) {
246 RC = _start();
247 if (RC == XAIE_OK) {
248 State.Running = 1;
249 }
250 } else {
251 Logger::log(LogLevel::FAL_ERROR) << __func__ << " " <<
252 typeid(*this).name() << " resource is not configured." << std::endl;
253 RC = XAIE_ERR;
254 }
255 } else {
256 Logger::log(LogLevel::FAL_ERROR) << __func__ << " " <<
257 typeid(*this).name() << " resource is not allocated." << std::endl;
258 RC = XAIE_ERR;
259 }
260 return RC;
261 }
262
269 AieRC stop() {
270 AieRC RC = XAIE_OK;
271
272 if (State.Running == 0) {
273 RC = XAIE_OK;
274 } else {
275 RC = _stop();
276 if (RC == XAIE_OK) {
277 State.Running = 0;
278 }
279 }
280 return RC;
281 }
282
287 bool isInitialized() const {
288 return State.Initialized;
289 }
290
295 bool isReserved() const {
296 return State.Reserved;
297 }
298
304 bool isConfigured() const {
305 return State.Configured;
306 }
307
312 bool isRunning() const {
313 return State.Running;
314 }
315
321 void getReservedRscs(std::vector<XAieUserRsc> &vOutRscs) const {
322 if (State.Reserved != 0) {
323 _getReservedRscs(vOutRscs);
324 }
325 }
326
332 AieRC getRscs(std::vector<XAieUserRsc> &vOutRscs) {
333 _getRscs(vOutRscs);
334 return XAIE_OK;
335 }
336
342 AieRC setRscs(std::vector<XAieUserRsc> &vInRscs) {
343 _setRscs(vInRscs);
344 return XAIE_OK;
345 }
346
351 virtual XAieRscType getRscType() const {
352 return Type;
353 }
354
362 virtual XAieRscStat getRscStat(const std::string &GName) const {
363 XAieRscStat RscStat(GName);
364 std::string rName(typeid(*this).name());
365 (void) GName;
366
367 throw std::invalid_argument("get rsc stat not supported of rsc" +
368 rName);
369 }
370
373 XAie_DevInst *dev() {
374 return AieHd->dev();
375 }
376
381 virtual uint32_t getAvailManagedRscs() {
382 std::string rName(typeid(*this).name());
383 throw std::invalid_argument("get max rsc num not supported of rsc " +
384 rName);
385 }
386
392 std::string rName(typeid(*this).name());
393 throw std::invalid_argument("get managed rsc type not supported of rsc " +
394 rName);
395 }
396
397 protected:
400 std::shared_ptr<XAieDevHandle> AieHd;
401 uint32_t preferredId;
402 uint32_t reservedId;
403 std::vector<XAieUserRsc> vRscs;
404 private:
411 virtual AieRC _reserve() {return XAIE_OK;}
418 virtual AieRC _release() {return XAIE_OK;}
425 virtual AieRC _free() {return XAIE_OK;}
432 virtual AieRC _start() {return XAIE_OK;}
439 virtual AieRC _stop() {return XAIE_OK;}
440
446 virtual void _getReservedRscs(std::vector<XAieUserRsc> &vOutRscs) const {
447 std::string rName(typeid(*this).name());
448
449 (void)vOutRscs;
450 throw std::invalid_argument("get resource not supported of rsc" +
451 rName);
452 }
458 virtual void _getRscs(std::vector<XAieUserRsc> &vOutRscs) {
459 vOutRscs.insert(vOutRscs.end(), vRscs.begin(), vRscs.end());
460 }
466 virtual void _setRscs(std::vector<XAieUserRsc> &vInRscs) {
467 vRscs = vInRscs;
468 }
469 };
470
476 public:
478 XAieSingleTileRsc(std::shared_ptr<XAieDevHandle> DevHd,
479 XAie_LocType Loc, XAie_ModuleType Mod):
480 XAieRsc(DevHd), Loc(Loc), Mod(Mod) {
481 if (XAie_CheckModule(AieHd->dev(), Loc, Mod) !=
482 XAIE_OK) {
483 throw std::invalid_argument("invalid module tile");
484 }
485 }
486 XAieSingleTileRsc(std::shared_ptr<XAieDevHandle> DevHd,
487 XAie_LocType Loc, XAie_ModuleType Mod, XAieRscType Type):
488 XAieRsc(DevHd, Type), Loc(Loc), Mod(Mod) {
489 if (XAie_CheckModule(AieHd->dev(), Loc, Mod) !=
490 XAIE_OK) {
491 throw std::invalid_argument("invalid module tile");
492 }
493 }
495 XAie_LocType Loc, XAie_ModuleType Mod):
496 XAieSingleTileRsc(Dev.getDevHandle(), Loc, Mod) {}
498 XAie_LocType Loc, XAie_ModuleType Mod, XAieRscType Type):
499 XAieSingleTileRsc(Dev.getDevHandle(), Loc, Mod, Type) {}
500 XAieSingleTileRsc(std::shared_ptr<XAieDevHandle> DevHd,
501 XAie_LocType Loc):
502 XAieRsc(DevHd), Loc(Loc) {
503 uint8_t TType = XAie_GetTileTypefromLoc(
504 AieHd->dev(), Loc);
505 if (TType == XAIEGBL_TILE_TYPE_MAX) {
506 throw std::invalid_argument("Invalid tile");
507 }
508 if (TType == XAIEGBL_TILE_TYPE_AIETILE) {
509 Mod = XAIE_CORE_MOD;
510 } else if (TType == XAIEGBL_TILE_TYPE_SHIMPL ||
511 TType == XAIEGBL_TILE_TYPE_SHIMNOC) {
512 Mod = XAIE_PL_MOD;
513 } else {
514 Mod = XAIE_MEM_MOD;
515 }
516 }
517 XAieSingleTileRsc(std::shared_ptr<XAieDevHandle> DevHd,
518 XAie_LocType Loc, XAieRscType Type):
519 XAieRsc(DevHd, Type), Loc(Loc){
520 uint8_t TType = XAie_GetTileTypefromLoc(
521 AieHd->dev(), Loc);
522 if (TType == XAIEGBL_TILE_TYPE_MAX) {
523 throw std::invalid_argument("Invalid tile");
524 }
525 if (TType == XAIEGBL_TILE_TYPE_AIETILE) {
526 Mod = XAIE_CORE_MOD;
527 } else if (TType == XAIEGBL_TILE_TYPE_SHIMPL ||
528 TType == XAIEGBL_TILE_TYPE_SHIMNOC) {
529 Mod = XAIE_PL_MOD;
530 } else {
531 Mod = XAIE_MEM_MOD;
532 }
533 }
535 XAieSingleTileRsc(Dev.getDevHandle(), Loc, Type) {}
542 XAie_LocType loc() const {
543 return Loc;
544 }
545
551 return Mod;
552 }
553
561 AieRC getRscId(XAie_LocType &OutLoc, XAie_ModuleType &OutMod,
562 uint32_t &OutRscId) {
563 AieRC RC = XAIE_ERR;
564
565 if (State.Reserved == 0) {
566 Logger::log(LogLevel::FAL_ERROR) << typeid(*this).name() << " " <<
567 __func__ << "(" <<
568 (uint32_t)Loc.Col << "," << (uint32_t)Loc.Row << ")" <<
569 " Expect Mod= " << Mod <<
570 " resource not reserved." << std::endl;
571 } else {
572 if (vRscs.size() > 0) {
573 OutMod = vRscs[0].Mod;
574 OutRscId = reservedId;
575 }
576 OutLoc = Loc;
577 RC = XAIE_OK;
578 }
579 return RC;
580 }
581 virtual XAieRscStat getRscStat(const std::string &GName) const {
582 XAieRscStat RscStat(GName);
583 (void) GName;
584
586 return AieHd->getRscGroup(GName).getRscStat(Loc,
587 Mod, getRscType());
588 } else {
589 return AieHd->getRscGroup(GName).getRscStat(Loc,
591 }
592 }
593 protected:
594 XAie_LocType Loc;
596 private:
602 virtual void _getReservedRscs(std::vector<XAieUserRsc> &vOutRscs) const {
603 if (vRscs.size() > 0) {
604 vOutRscs.push_back(vRscs[0]);
605 }
606 }
607 };
608
611 XAieRscGetRscsWrapper(std::shared_ptr<XAieRsc> &Rsc,
612 std::vector<XAieUserRsc> &Rscs) {
613 Rsc->getReservedRscs(Rscs);
614 }
616 };
617}
static std::ostream & log(LogLevel Lvl=LogLevel::FAL_MAX)
Definition xaiefal-log.hpp:50
AI engine device class. It defines AI engine device operations.
Definition xaiefal-base.hpp:259
Base AI engine resource class. It defines resource operations and maintains resource state machine.
Definition xaiefal-rsc-base.hpp:69
virtual XAieRscStat getRscStat(const std::string &GName) const
Definition xaiefal-rsc-base.hpp:362
XAieRscType Type
Definition xaiefal-rsc-base.hpp:399
std::vector< XAieUserRsc > vRscs
Definition xaiefal-rsc-base.hpp:403
XAieRscState State
Definition xaiefal-rsc-base.hpp:398
uint32_t getPreferredId()
Definition xaiefal-rsc-base.hpp:141
AieRC free()
Definition xaiefal-rsc-base.hpp:213
XAieRsc(XAieDev &Dev)
Definition xaiefal-rsc-base.hpp:87
virtual XAieRscType getRscType() const
Definition xaiefal-rsc-base.hpp:351
XAieRsc(std::shared_ptr< XAieDevHandle > DevHd, XAieRscType Type)
Definition xaiefal-rsc-base.hpp:80
bool isReserved() const
Definition xaiefal-rsc-base.hpp:295
XAie_DevInst * dev()
Definition xaiefal-rsc-base.hpp:373
virtual XAieRscType getManagedRscsType()
Definition xaiefal-rsc-base.hpp:391
AieRC release()
Definition xaiefal-rsc-base.hpp:188
virtual uint32_t getAvailManagedRscs()
Definition xaiefal-rsc-base.hpp:381
AieRC setPreferredId(uint32_t id, bool isStaticAllocated=false)
Definition xaiefal-rsc-base.hpp:114
AieRC reserve()
Definition xaiefal-rsc-base.hpp:161
AieRC start()
Definition xaiefal-rsc-base.hpp:239
void getReservedRscs(std::vector< XAieUserRsc > &vOutRscs) const
Definition xaiefal-rsc-base.hpp:321
uint32_t preferredId
Definition xaiefal-rsc-base.hpp:401
bool isConfigured() const
Definition xaiefal-rsc-base.hpp:304
uint32_t reservedId
Definition xaiefal-rsc-base.hpp:402
bool isRunning() const
Definition xaiefal-rsc-base.hpp:312
std::shared_ptr< XAieDevHandle > AieHd
Definition xaiefal-rsc-base.hpp:400
AieRC getRscs(std::vector< XAieUserRsc > &vOutRscs)
Definition xaiefal-rsc-base.hpp:332
AieRC stop()
Definition xaiefal-rsc-base.hpp:269
XAieRsc(std::shared_ptr< XAieDevHandle > DevHd)
Definition xaiefal-rsc-base.hpp:72
virtual ~XAieRsc()
Definition xaiefal-rsc-base.hpp:92
AieRC setRscs(std::vector< XAieUserRsc > &vInRscs)
Definition xaiefal-rsc-base.hpp:342
bool isInitialized() const
Definition xaiefal-rsc-base.hpp:287
XAieSingleTileRsc(std::shared_ptr< XAieDevHandle > DevHd, XAie_LocType Loc)
Definition xaiefal-rsc-base.hpp:500
XAieSingleTileRsc(XAieDev &Dev, XAie_LocType Loc, XAie_ModuleType Mod)
Definition xaiefal-rsc-base.hpp:494
virtual XAieRscStat getRscStat(const std::string &GName) const
Definition xaiefal-rsc-base.hpp:581
XAieSingleTileRsc(std::shared_ptr< XAieDevHandle > DevHd, XAie_LocType Loc, XAie_ModuleType Mod)
Definition xaiefal-rsc-base.hpp:478
XAie_ModuleType mod() const
Definition xaiefal-rsc-base.hpp:550
XAieSingleTileRsc(std::shared_ptr< XAieDevHandle > DevHd, XAie_LocType Loc, XAie_ModuleType Mod, XAieRscType Type)
Definition xaiefal-rsc-base.hpp:486
virtual ~XAieSingleTileRsc()
Definition xaiefal-rsc-base.hpp:536
XAieSingleTileRsc(XAieDev &Dev, XAie_LocType Loc, XAieRscType Type)
Definition xaiefal-rsc-base.hpp:534
XAieSingleTileRsc(std::shared_ptr< XAieDevHandle > DevHd, XAie_LocType Loc, XAieRscType Type)
Definition xaiefal-rsc-base.hpp:517
XAie_LocType loc() const
Definition xaiefal-rsc-base.hpp:542
XAie_LocType Loc
Definition xaiefal-rsc-base.hpp:594
XAie_ModuleType Mod
Definition xaiefal-rsc-base.hpp:595
XAieSingleTileRsc(XAieDev &Dev, XAie_LocType Loc, XAie_ModuleType Mod, XAieRscType Type)
Definition xaiefal-rsc-base.hpp:497
AieRC getRscId(XAie_LocType &OutLoc, XAie_ModuleType &OutMod, uint32_t &OutRscId)
Definition xaiefal-rsc-base.hpp:561
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition license.txt:197
Definition xaiefal-base.hpp:23
XAieRscType
Enums representing all requestable resources.
Definition xaiefal-rsc-group.hpp:27
@ FAL_ERROR
Definition xaiefal-log.hpp:14
~XAieRscGetRscsWrapper()
Definition xaiefal-rsc-base.hpp:615
XAieRscGetRscsWrapper(std::shared_ptr< XAieRsc > &Rsc, std::vector< XAieUserRsc > &Rscs)
Definition xaiefal-rsc-base.hpp:611
struct of resource usage statistics
Definition xaiefal-rsc-group.hpp:66
Definition xaiefal-rsc-base.hpp:50
uint8_t Prereserved
Definition xaiefal-rsc-base.hpp:54
uint8_t Running
Definition xaiefal-rsc-base.hpp:61
uint8_t Initialized
Definition xaiefal-rsc-base.hpp:51
uint8_t Configured
Definition xaiefal-rsc-base.hpp:52
uint8_t Reserved
Definition xaiefal-rsc-base.hpp:53
Data needed to make a resource request.
Definition xaiefal-rsc-base.hpp:27
uint32_t RscId
Definition xaiefal-rsc-base.hpp:41
XAieRscType RscType
Definition xaiefal-rsc-base.hpp:37
XAie_LocType Loc
Definition xaiefal-rsc-base.hpp:29
XAie_ModuleType Mod
Definition xaiefal-rsc-base.hpp:33
#define XAIE_RSC_TYPE_ANY
Definition xaiefal-common.hpp:8
#define XAIE_LOC_ANY
Definition xaiefal-common.hpp:10
#define XAIE_MOD_ANY
Definition xaiefal-common.hpp:9
#define XAIE_RSC_ID_ANY
Definition xaiefal-common.hpp:7