CYNQ  0.3.0
Framework to develop FPGA applications in C++ with the easiness of PYNQ
accelerator.hpp
1 /*
2  * See LICENSE for more information about licensing
3  *
4  * Copyright 2023
5  * Author: Luis G. Leon-Vega <luis.leon@ieee.org>
6  * Diego Arturo Avila Torres <diego.avila@uned.cr>
7  *
8  */
9 #pragma once
10 #include <memory>
11 #include <string>
12 
13 // cynq headers
14 #include <cynq/enums.hpp>
15 #include <cynq/execution-graph.hpp>
16 #include <cynq/memory.hpp>
17 #include <cynq/status.hpp>
18 
19 namespace cynq {
20 
21 struct HardwareParameters;
22 
29  virtual ~AcceleratorParameters() = default;
30 };
31 
37 class IAccelerator {
38  public:
44  virtual ~IAccelerator() = default;
50  enum Type {
52  None = 0,
56  XRT
57  };
58 
70  virtual Status Start(const StartMode mode) = 0;
71 
86  virtual Status Start(std::shared_ptr<IExecutionGraph> graph,
87  const StartMode mode);
88 
97  virtual Status Stop() = 0;
98 
111  virtual Status Stop(std::shared_ptr<IExecutionGraph> graph);
112 
120  virtual Status Sync() = 0;
121 
134  virtual Status Sync(std::shared_ptr<IExecutionGraph> graph);
135 
143  virtual DeviceStatus GetStatus() = 0;
144 
165  static std::shared_ptr<IAccelerator> Create(IAccelerator::Type impl,
166  const uint64_t addr);
190  static std::shared_ptr<IAccelerator> Create(
191  IAccelerator::Type impl, const std::string &kernelname,
192  const std::shared_ptr<HardwareParameters> hwparams);
193 
213  template <typename T>
214  Status Write(const uint64_t address, const T *data,
215  const size_t elements = 1) {
216  return this->WriteRegister(address, reinterpret_cast<const uint8_t *>(data),
217  elements * sizeof(T));
218  }
219 
244  template <typename T>
245  Status Write(std::shared_ptr<IExecutionGraph> graph, const uint64_t address,
246  const T *data, const size_t elements = 1) {
247  return this->WriteRegister(graph, address,
248  reinterpret_cast<const uint8_t *>(data),
249  elements * sizeof(T));
250  }
251 
271  template <typename T>
272  Status Read(const uint64_t address, T *data, const size_t elements = 1) {
273  return this->ReadRegister(address, reinterpret_cast<uint8_t *>(data),
274  elements * sizeof(T));
275  }
276 
301  template <typename T>
302  Status Read(std::shared_ptr<IExecutionGraph> graph, const uint64_t address,
303  T *data, const size_t elements = 1) {
304  return this->ReadRegister(graph, address, reinterpret_cast<uint8_t *>(data),
305  elements * sizeof(T));
306  }
307 
320  virtual int GetMemoryBank(const uint pos) = 0;
321 
345  template <typename T>
346  Status Attach(const uint64_t index, T *data,
347  const RegisterAccess access = RegisterAccess::WO,
348  const size_t elements = 1) {
349  return this->AttachRegister(index, reinterpret_cast<uint8_t *>(data),
350  access, elements * sizeof(T));
351  }
352 
359  template <typename T>
360  Status Attach(const uint64_t index, T *data, const size_t elements,
361  const RegisterAccess access = RegisterAccess::WO) {
362  return this->AttachRegister(index, reinterpret_cast<uint8_t *>(data),
363  access, elements * sizeof(T));
364  }
365 
378  virtual Status Attach(const uint64_t addr, std::shared_ptr<IMemory> mem) = 0;
379 
380  protected:
394  virtual Status WriteRegister(const uint64_t address, const uint8_t *data,
395  const size_t size) = 0;
408  virtual Status ReadRegister(const uint64_t address, uint8_t *data,
409  const size_t size) = 0;
410 
426  virtual Status WriteRegister(std::shared_ptr<IExecutionGraph> graph,
427  const uint64_t address, const uint8_t *data,
428  const size_t size);
444  virtual Status ReadRegister(std::shared_ptr<IExecutionGraph> graph,
445  const uint64_t address, uint8_t *data,
446  const size_t size);
447 
463  virtual Status AttachRegister(const uint64_t index, uint8_t *data,
464  const RegisterAccess access,
465  const size_t size) = 0;
466 };
467 } // namespace cynq
Interface for standardising the API for any Accelerator device: XRTAccelerator.
Definition: accelerator.hpp:37
virtual Status WriteRegister(const uint64_t address, const uint8_t *data, const size_t size)=0
Opaque Write Register method Writes to the register of the accelerator.
virtual Status ReadRegister(const uint64_t address, uint8_t *data, const size_t size)=0
Opaque Read Register method.
virtual ~IAccelerator()=default
~IAccelerator destructor method Destroy the IAccelerator object.
virtual Status AttachRegister(const uint64_t index, uint8_t *data, const RegisterAccess access, const size_t size)=0
Opaque Attach Register method.
Type
Type Type of runtime supported by the IAccelerator.
Definition: accelerator.hpp:50
@ XRT
Definition: accelerator.hpp:56
@ None
Definition: accelerator.hpp:52
@ MMIO
Definition: accelerator.hpp:54
Status Write(const uint64_t address, const T *data, const size_t elements=1)
Write method Performs a write operation to the accelerator through a register.
Definition: accelerator.hpp:214
virtual Status Stop()=0
Stop method This asynchronously turns off the accelerator by removing the autorestart and start bits ...
static std::shared_ptr< IAccelerator > Create(IAccelerator::Type impl, const uint64_t addr)
Create method Factory method used for creating specific subclasses of IAccelerator.
Definition: accelerator.cpp:16
Status Write(std::shared_ptr< IExecutionGraph > graph, const uint64_t address, const T *data, const size_t elements=1)
Write method (asynchronous) Performs a write operation to the accelerator through a register in an ex...
Definition: accelerator.hpp:245
Status Read(const uint64_t address, T *data, const size_t elements=1)
Read method Performs a write operation to the accelerator through a register.
Definition: accelerator.hpp:272
Status Read(std::shared_ptr< IExecutionGraph > graph, const uint64_t address, T *data, const size_t elements=1)
Read method (asynchronous) Performs a write operation to the accelerator through a register in an exe...
Definition: accelerator.hpp:302
Status Attach(const uint64_t index, T *data, const size_t elements, const RegisterAccess access=RegisterAccess::WO)
Overload of the Attach<T>()
Definition: accelerator.hpp:360
virtual Status Start(const StartMode mode)=0
Start method This method starts the accelerator in either once or continuous mode (with the autoresta...
virtual Status Sync()=0
Sync method This synchronises the execution and wait until the kernel/accelerator finishes its execut...
Status Attach(const uint64_t index, T *data, const RegisterAccess access=RegisterAccess::WO, const size_t elements=1)
Attach an argument Performs an attachment of the argument and the respective pointer....
Definition: accelerator.hpp:346
virtual Status Attach(const uint64_t addr, std::shared_ptr< IMemory > mem)=0
Attach a memory argument Performs an attachment of the argument and the respective pointer....
virtual DeviceStatus GetStatus()=0
GetStatus method This returns the accelerator state by using the DeviceStatus. This reads the control...
virtual int GetMemoryBank(const uint pos)=0
Get the memory bank ID.
Define an abstract representation of the accelerator parameters with some prefilled fields.
Definition: accelerator.hpp:27
virtual ~AcceleratorParameters()=default
Structure to define the return characteristics of each function.
Definition: status.hpp:19