21 #include <pybind11/pybind11.h>
24 #include <pybind11/stl.h>
33 static const void *
get(
const ChannelPort *port,
const std::type_info *&type) {
49 py::class_<Type>(m,
"Type")
51 .def(
"__repr__", [](
Type &t) {
return "<" + t.
getID() +
">"; });
52 py::class_<ChannelType, Type>(m,
"ChannelType")
54 py::return_value_policy::reference);
55 py::enum_<BundleType::Direction>(m,
"Direction")
56 .value(
"To", BundleType::Direction::To)
57 .value(
"From", BundleType::Direction::From)
59 py::class_<BundleType, Type>(m,
"BundleType")
61 py::return_value_policy::reference);
62 py::class_<VoidType, Type>(m,
"VoidType");
63 py::class_<AnyType, Type>(m,
"AnyType");
64 py::class_<BitVectorType, Type>(m,
"BitVectorType")
66 py::class_<BitsType, BitVectorType>(m,
"BitsType");
67 py::class_<IntegerType, BitVectorType>(m,
"IntegerType");
68 py::class_<SIntType, IntegerType>(m,
"SIntType");
69 py::class_<UIntType, IntegerType>(m,
"UIntType");
70 py::class_<StructType, Type>(m,
"StructType")
72 py::return_value_policy::reference);
73 py::class_<ArrayType, Type>(m,
"ArrayType")
75 py::return_value_policy::reference)
78 py::class_<ModuleInfo>(m,
"ModuleInfo")
79 .def_property_readonly(
"name", [](
ModuleInfo &info) {
return info.
name; })
80 .def_property_readonly(
"summary",
82 .def_property_readonly(
"version",
84 .def_property_readonly(
"repo", [](
ModuleInfo &info) {
return info.
repo; })
85 .def_property_readonly(
"commit_hash",
90 std::stringstream os(ret);
95 py::class_<SysInfo>(m,
"SysInfo")
99 py::class_<services::MMIO>(m,
"MMIO")
103 py::class_<services::HostMem::HostMemRegion>(m,
"HostMemRegion")
104 .def_property_readonly(
"ptr",
106 return reinterpret_cast<uintptr_t
>(mem.
getPtr());
108 .def_property_readonly(
"size",
111 py::class_<services::HostMem::Options>(m,
"HostMemOptions")
114 .def_readwrite(
"use_large_pages",
117 std::string ret =
"HostMemOptions(";
121 ret +=
"use_large_pages";
126 py::class_<services::HostMem>(m,
"HostMem")
129 py::return_value_policy::take_ownership)
133 return self.mapMemory(
reinterpret_cast<void *
>(ptr), size, opts);
135 py::arg(
"ptr"), py::arg(
"size"),
139 [](
HostMem &
self, uintptr_t ptr) {
140 return self.unmapMemory(
reinterpret_cast<void *
>(ptr));
144 py::class_<AppID>(m,
"AppID")
145 .def(py::init<std::string, std::optional<uint32_t>>(), py::arg(
"name"),
146 py::arg(
"idx") = std::nullopt)
147 .def_property_readonly(
"name", [](
AppID &
id) {
return id.name; })
148 .def_property_readonly(
"idx",
149 [](
AppID &
id) -> py::object {
151 return py::cast(
id.idx);
156 std::string ret =
"<" +
id.name;
158 ret = ret +
"[" + std::to_string(*
id.idx) +
"]";
162 .def(
"__eq__", [](
AppID &a,
AppID &b) {
return a == b; })
163 .def(
"__hash__", [](
AppID &
id) {
165 return std::hash<std::string>{}(
id.name) ^
166 (std::hash<uint32_t>{}(
id.idx.value_or(-1)) << 1);
169 py::class_<std::future<MessageData>>(m,
"MessageDataFuture")
170 .def(
"valid", &std::future<MessageData>::valid)
171 .def(
"wait", &std::future<MessageData>::wait)
172 .def(
"get", [](std::future<MessageData> &f) {
174 return py::bytearray((
const char *)data.getBytes(), data.getSize());
177 py::class_<ChannelPort>(m,
"ChannelPort")
179 py::arg(
"buffer_size") = std::nullopt)
181 py::return_value_policy::reference);
183 py::class_<WriteChannelPort, ChannelPort>(m,
"WriteChannelPort")
185 py::buffer_info info(py::buffer(data).request());
186 std::vector<uint8_t> dataVec((uint8_t *)info.ptr,
187 (uint8_t *)info.ptr + info.size);
190 py::class_<ReadChannelPort, ChannelPort>(m,
"ReadChannelPort")
196 return py::bytearray((
const char *)data.getBytes(), data.getSize());
198 "Read data from the channel. Blocking.")
201 py::class_<BundlePort>(m,
"BundlePort")
204 py::return_value_policy::reference)
206 py::return_value_policy::reference)
208 py::return_value_policy::reference);
210 py::class_<ServicePort, BundlePort>(m,
"ServicePort");
211 py::class_<FuncService::Function, ServicePort>(m,
"Function")
215 py::bytearray msg) -> std::future<MessageData> {
216 py::buffer_info info(py::buffer(msg).request());
217 std::vector<uint8_t> dataVec((uint8_t *)info.ptr,
218 (uint8_t *)info.ptr + info.size);
220 return self.call(data);
222 py::return_value_policy::take_ownership)
228 py::class_<HWModule>(m,
"HWModule")
231 py::return_value_policy::reference);
234 py::class_<Instance, HWModule>(m,
"Instance")
237 py::class_<Accelerator, HWModule>(m,
"Accelerator");
242 py::return_value_policy::reference);
244 auto accConn = py::class_<AcceleratorConnection>(m,
"AcceleratorConnection");
246 py::class_<Context>(m,
"Context")
256 py::return_value_policy::reference)
262 py::return_value_policy::reference)
264 "get_service_hostmem",
268 py::return_value_policy::reference);
270 py::class_<Manifest>(m,
"Manifest")
271 .def(py::init<Context &, std::string>())
274 py::return_value_policy::take_ownership)
Abstract class representing a connection to an accelerator.
ServiceClass * getService(AppIDPath id={}, std::string implName={}, ServiceImplDetails details={}, HWClientDetails clients={})
Get a typed reference to a particular service type.
const Type * getElementType() const
uint64_t getWidth() const
ReadChannelPort & getRawRead(const std::string &name) const
WriteChannelPort & getRawWrite(const std::string &name) const
Get access to the raw byte streams of a channel.
const std::map< std::string, ChannelPort & > & getChannels() const
AppID getID() const
Get the ID of the port.
const ChannelVector & getChannels() const
Unidirectional channels are the basic communication primitive between the host and accelerator.
virtual void connect(std::optional< unsigned > bufferSize=std::nullopt)
Set up a connection to the accelerator.
const Type * getType() const
const Type * getInner() const
std::unique_ptr< AcceleratorConnection > connect(std::string backend, std::string connection)
Connect to an accelerator backend.
const std::map< AppID, Instance * > & getChildren() const
Access the module's children by ID.
std::optional< ModuleInfo > getInfo() const
Access the module's metadata, if any.
const std::map< AppID, const BundlePort & > & getPorts() const
Access the module's ports by ID.
const AppID getID() const
Get the instance's ID, which it will always have.
std::unique_ptr< Accelerator > buildAccelerator(AcceleratorConnection &acc) const
std::vector< ModuleInfo > getModuleInfos() const
uint32_t getApiVersion() const
const std::vector< const Type * > & getTypeTable() const
The Type Table is an ordered list of types.
A logical chunk of data representing serialized data.
A ChannelPort which reads data from the accelerator.
virtual std::future< MessageData > readAsync()
Asynchronous read.
const FieldVector & getFields() const
Root class of the ESI type system.
A ChannelPort which sends data to the accelerator.
virtual void write(const MessageData &)=0
A very basic write API. Will likely change for performance reasons.
A function call which gets attached to a service port.
virtual std::unique_ptr< HostMemRegion > allocate(std::size_t size, Options opts) const =0
Allocate a region of host memory in accelerator accessible address space.
virtual uint64_t read(uint32_t addr) const =0
virtual void write(uint32_t addr, uint64_t data)=0
Information about the Accelerator system.
virtual std::string getJsonManifest() const
Return the JSON-formatted system manifest.
virtual uint32_t getEsiVersion() const =0
Get the ESI version number to check version compatibility.
PYBIND11_MODULE(esiCppAccel, m)
std::unique_ptr< AcceleratorConnection > connect(Context &ctxt, std::string backend, std::string connection)
const std::optional< std::string > commitHash
const std::optional< std::string > repo
const std::optional< std::string > version
const std::optional< std::string > summary
const std::optional< std::string > name
RAII memory region for host memory.
virtual void * getPtr() const =0
virtual std::size_t getSize() const =0
Options for allocating host memory.
static const void * get(const ChannelPort *port, const std::type_info *&type)