CIRCT 21.0.0git
Loading...
Searching...
No Matches
Cosim.cpp
Go to the documentation of this file.
1//===- Cosim.cpp - Connection to ESI simulation via GRPC ------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// DO NOT EDIT!
10// This file is distributed as part of an ESI package. The source for this file
11// should always be modified within CIRCT
12// (lib/dialect/ESI/runtime/cpp/lib/backends/Cosim.cpp).
13//
14//===----------------------------------------------------------------------===//
15
16#include "esi/backends/Cosim.h"
17#include "esi/Engines.h"
18#include "esi/Services.h"
19#include "esi/Utils.h"
20
21#include "cosim.grpc.pb.h"
22
23#include <grpc/grpc.h>
24#include <grpcpp/channel.h>
25#include <grpcpp/client_context.h>
26#include <grpcpp/create_channel.h>
27#include <grpcpp/security/credentials.h>
28
29#include <fstream>
30#include <iostream>
31#include <set>
32
33using namespace esi;
34using namespace esi::cosim;
35using namespace esi::services;
36using namespace esi::backends::cosim;
37
38using grpc::Channel;
39using grpc::ClientContext;
40using grpc::ClientReader;
41using grpc::ClientReaderWriter;
42using grpc::ClientWriter;
43using grpc::Status;
44
45static void checkStatus(Status s, const std::string &msg) {
46 if (!s.ok())
47 throw std::runtime_error(msg + ". Code " + to_string(s.error_code()) +
48 ": " + s.error_message() + " (" +
49 s.error_details() + ")");
50}
51
52/// Hack around C++ not having a way to forward declare a nested class.
54 StubContainer(std::unique_ptr<ChannelServer::Stub> stub)
55 : stub(std::move(stub)) {}
56 std::unique_ptr<ChannelServer::Stub> stub;
57
58 /// Get the type ID for a channel name.
59 bool getChannelDesc(const std::string &channelName,
60 esi::cosim::ChannelDesc &desc);
61};
63
64/// Parse the connection std::string and instantiate the accelerator. Support
65/// the traditional 'host:port' syntax and a path to 'cosim.cfg' which is output
66/// by the cosimulation when it starts (which is useful when it chooses its own
67/// port).
68std::unique_ptr<AcceleratorConnection>
69CosimAccelerator::connect(Context &ctxt, std::string connectionString) {
70 std::string portStr;
71 std::string host = "localhost";
72
73 size_t colon;
74 if ((colon = connectionString.find(':')) != std::string::npos) {
75 portStr = connectionString.substr(colon + 1);
76 host = connectionString.substr(0, colon);
77 } else if (connectionString.ends_with("cosim.cfg")) {
78 std::ifstream cfg(connectionString);
79 std::string line, key, value;
80
81 while (getline(cfg, line))
82 if ((colon = line.find(":")) != std::string::npos) {
83 key = line.substr(0, colon);
84 value = line.substr(colon + 1);
85 if (key == "port")
86 portStr = value;
87 else if (key == "host")
88 host = value;
89 }
90
91 if (portStr.size() == 0)
92 throw std::runtime_error("port line not found in file");
93 } else if (connectionString == "env") {
94 char *hostEnv = getenv("ESI_COSIM_HOST");
95 if (hostEnv)
96 host = hostEnv;
97 else
98 host = "localhost";
99 char *portEnv = getenv("ESI_COSIM_PORT");
100 if (portEnv)
101 portStr = portEnv;
102 else
103 throw std::runtime_error("ESI_COSIM_PORT environment variable not set");
104 } else {
105 throw std::runtime_error("Invalid connection std::string '" +
106 connectionString + "'");
107 }
108 uint16_t port = stoul(portStr);
109 auto conn = make_unique<CosimAccelerator>(ctxt, host, port);
110
111 // Using the MMIO manifest method is really only for internal debugging, so it
112 // doesn't need to be part of the connection string.
113 char *manifestMethod = getenv("ESI_COSIM_MANIFEST_MMIO");
114 if (manifestMethod != nullptr)
115 conn->setManifestMethod(ManifestMethod::MMIO);
116
117 return conn;
118}
119
120/// Construct and connect to a cosim server.
121CosimAccelerator::CosimAccelerator(Context &ctxt, std::string hostname,
122 uint16_t port)
123 : AcceleratorConnection(ctxt) {
124 // Connect to the simulation.
125 auto channel = grpc::CreateChannel(hostname + ":" + std::to_string(port),
126 grpc::InsecureChannelCredentials());
127 rpcClient = new StubContainer(ChannelServer::NewStub(channel));
128}
130 disconnect();
131 if (rpcClient)
132 delete rpcClient;
133 channels.clear();
134}
135
136namespace {
137class CosimSysInfo : public SysInfo {
138public:
139 CosimSysInfo(CosimAccelerator &conn, ChannelServer::Stub *rpcClient)
140 : SysInfo(conn), rpcClient(rpcClient) {}
141
142 uint32_t getEsiVersion() const override {
143 ::esi::cosim::Manifest response = getManifest();
144 return response.esi_version();
145 }
146
147 std::vector<uint8_t> getCompressedManifest() const override {
148 ::esi::cosim::Manifest response = getManifest();
149 std::string compressedManifestStr = response.compressed_manifest();
150 return std::vector<uint8_t>(compressedManifestStr.begin(),
151 compressedManifestStr.end());
152 }
153
154private:
155 ::esi::cosim::Manifest getManifest() const {
156 ::esi::cosim::Manifest response;
157 // To get around the a race condition where the manifest may not be set yet,
158 // loop until it is. TODO: fix this with the DPI API change.
159 do {
160 ClientContext context;
161 VoidMessage arg;
162 Status s = rpcClient->GetManifest(&context, arg, &response);
163 checkStatus(s, "Failed to get manifest");
164 std::this_thread::sleep_for(std::chrono::milliseconds(10));
165 } while (response.esi_version() < 0);
166 return response;
167 }
168
169 esi::cosim::ChannelServer::Stub *rpcClient;
170};
171} // namespace
172
173namespace {
174/// Cosim client implementation of a write channel port.
175class WriteCosimChannelPort : public WriteChannelPort {
176public:
177 WriteCosimChannelPort(AcceleratorConnection &conn,
178 ChannelServer::Stub *rpcClient, const ChannelDesc &desc,
179 const Type *type, std::string name)
180 : WriteChannelPort(type), conn(conn), rpcClient(rpcClient), desc(desc),
181 name(name) {}
182 ~WriteCosimChannelPort() = default;
183
184 void connectImpl(std::optional<unsigned> bufferSize) override {
185 if (desc.dir() != ChannelDesc::Direction::ChannelDesc_Direction_TO_SERVER)
186 throw std::runtime_error("Channel '" + name +
187 "' is not a to server channel");
188 assert(desc.name() == name);
189 }
190
191 /// Send a write message to the server.
192 void write(const MessageData &data) override {
193 // Add trace logging before sending the message.
194 conn.getLogger().trace(
195 [this,
196 &data](std::string &subsystem, std::string &msg,
197 std::unique_ptr<std::map<std::string, std::any>> &details) {
198 subsystem = "cosim_write";
199 msg = "Writing message to channel '" + name + "'";
200 details = std::make_unique<std::map<std::string, std::any>>();
201 (*details)["channel"] = name;
202 (*details)["data_size"] = data.getSize();
203 (*details)["message_data"] = data.toHex();
204 });
205
206 ClientContext context;
207 AddressedMessage msg;
208 msg.set_channel_name(name);
209 msg.mutable_message()->set_data(data.getBytes(), data.getSize());
210 VoidMessage response;
211 grpc::Status sendStatus = rpcClient->SendToServer(&context, msg, &response);
212 if (!sendStatus.ok())
213 throw std::runtime_error("Failed to write to channel '" + name +
214 "': " + std::to_string(sendStatus.error_code()) +
215 " " + sendStatus.error_message() +
216 ". Details: " + sendStatus.error_details());
217 }
218
219 bool tryWrite(const MessageData &data) override {
220 write(data);
221 return true;
222 }
223
224protected:
226 ChannelServer::Stub *rpcClient;
227 /// The channel description as provided by the server.
228 ChannelDesc desc;
229 /// The name of the channel from the manifest.
230 std::string name;
231};
232} // namespace
233
234namespace {
235/// Cosim client implementation of a read channel port. Since gRPC read protocol
236/// streams messages back, this implementation is quite complex.
237class ReadCosimChannelPort
238 : public ReadChannelPort,
239 public grpc::ClientReadReactor<esi::cosim::Message> {
240public:
241 ReadCosimChannelPort(AcceleratorConnection &conn,
242 ChannelServer::Stub *rpcClient, const ChannelDesc &desc,
243 const Type *type, std::string name)
244 : ReadChannelPort(type), conn(conn), rpcClient(rpcClient), desc(desc),
245 name(name), context(nullptr) {}
246 virtual ~ReadCosimChannelPort() { disconnect(); }
247
248 void connectImpl(std::optional<unsigned> bufferSize) override {
249 // Sanity checking.
250 if (desc.dir() != ChannelDesc::Direction::ChannelDesc_Direction_TO_CLIENT)
251 throw std::runtime_error("Channel '" + name +
252 "' is not a to client channel");
253 assert(desc.name() == name);
254
255 // Initiate a stream of messages from the server.
256 if (context)
257 return;
258 context = new ClientContext();
259 rpcClient->async()->ConnectToClientChannel(context, &desc, this);
260 StartCall();
261 StartRead(&incomingMessage);
262 }
263
264 /// Gets called when there's a new message from the server. It'll be stored in
265 /// `incomingMessage`.
266 void OnReadDone(bool ok) override {
267 if (!ok)
268 // This happens when we are disconnecting since we are canceling the call.
269 return;
270
271 // Read the delivered message and push it onto the queue.
272 const std::string &messageString = incomingMessage.data();
273 MessageData data(reinterpret_cast<const uint8_t *>(messageString.data()),
274 messageString.size());
275
276 // Add trace logging for the received message.
277 conn.getLogger().trace(
278 [this,
279 &data](std::string &subsystem, std::string &msg,
280 std::unique_ptr<std::map<std::string, std::any>> &details) {
281 subsystem = "cosim_read";
282 msg = "Received message from channel '" + name + "'";
283 details = std::make_unique<std::map<std::string, std::any>>();
284 (*details)["channel"] = name;
285 (*details)["data_size"] = data.getSize();
286 (*details)["message_data"] = data.toHex();
287 });
288
289 while (!callback(data))
290 // Blocking here could cause deadlocks in specific situations.
291 // TODO: Implement a way to handle this better.
292 std::this_thread::sleep_for(std::chrono::milliseconds(10));
293
294 // Log the message consumption.
295 conn.getLogger().trace(
296 [this](std::string &subsystem, std::string &msg,
297 std::unique_ptr<std::map<std::string, std::any>> &details) {
298 subsystem = "cosim_read";
299 msg = "Message from channel '" + name + "' consumed";
300 });
301
302 // Initiate the next read.
303 StartRead(&incomingMessage);
304 }
305
306 /// Disconnect this channel from the server.
307 void disconnect() override {
308 Logger &logger = conn.getLogger();
309 logger.debug("cosim_read", "Disconnecting channel " + name);
310 if (!context)
311 return;
312 context->TryCancel();
313 // Don't delete the context since gRPC still hold a reference to it.
314 // TODO: figure out how to delete it.
316 }
317
318protected:
320 ChannelServer::Stub *rpcClient;
321 /// The channel description as provided by the server.
322 ChannelDesc desc;
323 /// The name of the channel from the manifest.
324 std::string name;
325
326 ClientContext *context;
327 /// Storage location for the incoming message.
328 esi::cosim::Message incomingMessage;
329};
330
331} // namespace
332
333/// Get the channel description for a channel name. Iterate through the list
334/// each time. Since this will only be called a small number of times on a small
335/// list, it's not worth doing anything fancy.
336bool StubContainer::getChannelDesc(const std::string &channelName,
337 ChannelDesc &desc) {
338 ClientContext context;
339 VoidMessage arg;
340 ListOfChannels response;
341 Status s = stub->ListChannels(&context, arg, &response);
342 checkStatus(s, "Failed to list channels");
343 for (const auto &channel : response.channels())
344 if (channel.name() == channelName) {
345 desc = channel;
346 return true;
347 }
348 return false;
349}
350
351namespace {
352class CosimMMIO : public MMIO {
353public:
354 CosimMMIO(CosimAccelerator &conn, Context &ctxt, StubContainer *rpcClient,
355 const HWClientDetails &clients)
356 : MMIO(conn, clients) {
357 // We have to locate the channels ourselves since this service might be used
358 // to retrieve the manifest.
359 ChannelDesc cmdArg, cmdResp;
360 if (!rpcClient->getChannelDesc("__cosim_mmio_read_write.arg", cmdArg) ||
361 !rpcClient->getChannelDesc("__cosim_mmio_read_write.result", cmdResp))
362 throw std::runtime_error("Could not find MMIO channels");
363
364 const esi::Type *i64Type = getType(ctxt, new UIntType(cmdResp.type(), 64));
365 const esi::Type *cmdType =
366 getType(ctxt, new StructType(cmdArg.type(),
367 {{"write", new BitsType("i1", 1)},
368 {"offset", new UIntType("ui32", 32)},
369 {"data", new BitsType("i64", 64)}}));
370
371 // Get ports, create the function, then connect to it.
372 cmdArgPort = std::make_unique<WriteCosimChannelPort>(
373 conn, rpcClient->stub.get(), cmdArg, cmdType,
374 "__cosim_mmio_read_write.arg");
375 cmdRespPort = std::make_unique<ReadCosimChannelPort>(
376 conn, rpcClient->stub.get(), cmdResp, i64Type,
377 "__cosim_mmio_read_write.result");
378 auto *bundleType = new BundleType(
379 "cosimMMIO", {{"arg", BundleType::Direction::To, cmdType},
380 {"result", BundleType::Direction::From, i64Type}});
381 cmdMMIO.reset(FuncService::Function::get(AppID("__cosim_mmio"), bundleType,
382 *cmdArgPort, *cmdRespPort));
383 cmdMMIO->connect();
384 }
385
386#pragma pack(push, 1)
387 struct MMIOCmd {
388 uint64_t data;
389 uint32_t offset;
390 bool write;
391 };
392#pragma pack(pop)
393
394 // Call the read function and wait for a response.
395 uint64_t read(uint32_t addr) const override {
396 MMIOCmd cmd{.offset = addr, .write = false};
397 auto arg = MessageData::from(cmd);
398 std::future<MessageData> result = cmdMMIO->call(arg);
399 result.wait();
400 uint64_t ret = *result.get().as<uint64_t>();
401 conn.getLogger().trace(
402 [addr, ret](std::string &subsystem, std::string &msg,
403 std::unique_ptr<std::map<std::string, std::any>> &details) {
404 subsystem = "cosim_mmio";
405 msg = "MMIO[0x" + toHex(addr) + "] = 0x" + toHex(ret);
406 });
407 return ret;
408 }
409
410 void write(uint32_t addr, uint64_t data) override {
411 conn.getLogger().trace(
412 [addr,
413 data](std::string &subsystem, std::string &msg,
414 std::unique_ptr<std::map<std::string, std::any>> &details) {
415 subsystem = "cosim_mmio";
416 msg = "MMIO[0x" + toHex(addr) + "] <- 0x" + toHex(data);
417 });
418 MMIOCmd cmd{.data = data, .offset = addr, .write = true};
419 auto arg = MessageData::from(cmd);
420 std::future<MessageData> result = cmdMMIO->call(arg);
421 result.wait();
422 }
423
424private:
425 const esi::Type *getType(Context &ctxt, esi::Type *type) {
426 if (auto t = ctxt.getType(type->getID())) {
427 delete type;
428 return *t;
429 }
430 ctxt.registerType(type);
431 return type;
432 }
433 std::unique_ptr<WriteCosimChannelPort> cmdArgPort;
434 std::unique_ptr<ReadCosimChannelPort> cmdRespPort;
435 std::unique_ptr<FuncService::Function> cmdMMIO;
436};
437
438#pragma pack(push, 1)
439struct HostMemReadReq {
440 uint8_t tag;
441 uint32_t length;
442 uint64_t address;
443};
444
445struct HostMemReadResp {
446 uint64_t data;
447 uint8_t tag;
448};
449
450struct HostMemWriteReq {
451 uint8_t valid_bytes;
452 uint64_t data;
453 uint8_t tag;
454 uint64_t address;
455};
456
457using HostMemWriteResp = uint8_t;
458#pragma pack(pop)
459
460class CosimHostMem : public HostMem {
461public:
462 CosimHostMem(AcceleratorConnection &acc, Context &ctxt,
463 StubContainer *rpcClient)
464 : HostMem(acc), acc(acc), ctxt(ctxt), rpcClient(rpcClient) {}
465
466 void start() override {
467 // We have to locate the channels ourselves since this service might be used
468 // to retrieve the manifest.
469
470 if (writeRespPort)
471 return;
472
473 // TODO: The types here are WRONG. They need to be wrapped in Channels! Fix
474 // this in a subsequent PR.
475
476 // Setup the read side callback.
477 ChannelDesc readArg, readResp;
478 if (!rpcClient->getChannelDesc("__cosim_hostmem_read_req.data", readArg) ||
479 !rpcClient->getChannelDesc("__cosim_hostmem_read_resp.data", readResp))
480 throw std::runtime_error("Could not find HostMem read channels");
481
482 const esi::Type *readRespType =
483 getType(ctxt, new StructType(readResp.type(),
484 {{"tag", new UIntType("ui8", 8)},
485 {"data", new BitsType("i64", 64)}}));
486 const esi::Type *readReqType =
487 getType(ctxt, new StructType(readArg.type(),
488 {{"address", new UIntType("ui64", 64)},
489 {"length", new UIntType("ui32", 32)},
490 {"tag", new UIntType("ui8", 8)}}));
491
492 // Get ports. Unfortunately, we can't model this as a callback since there
493 // will sometimes be multiple responses per request.
494 readRespPort = std::make_unique<WriteCosimChannelPort>(
495 conn, rpcClient->stub.get(), readResp, readRespType,
496 "__cosim_hostmem_read_resp.data");
497 readReqPort = std::make_unique<ReadCosimChannelPort>(
498 conn, rpcClient->stub.get(), readArg, readReqType,
499 "__cosim_hostmem_read_req.data");
500 readReqPort->connect(
501 [this](const MessageData &req) { return serviceRead(req); });
502
503 // Setup the write side callback.
504 ChannelDesc writeArg, writeResp;
505 if (!rpcClient->getChannelDesc("__cosim_hostmem_write.arg", writeArg) ||
506 !rpcClient->getChannelDesc("__cosim_hostmem_write.result", writeResp))
507 throw std::runtime_error("Could not find HostMem write channels");
508
509 const esi::Type *writeRespType =
510 getType(ctxt, new UIntType(writeResp.type(), 8));
511 const esi::Type *writeReqType =
512 getType(ctxt, new StructType(writeArg.type(),
513 {{"address", new UIntType("ui64", 64)},
514 {"tag", new UIntType("ui8", 8)},
515 {"data", new BitsType("i64", 64)}}));
516
517 // Get ports, create the function, then connect to it.
518 writeRespPort = std::make_unique<WriteCosimChannelPort>(
519 conn, rpcClient->stub.get(), writeResp, writeRespType,
520 "__cosim_hostmem_write.result");
521 writeReqPort = std::make_unique<ReadCosimChannelPort>(
522 conn, rpcClient->stub.get(), writeArg, writeReqType,
523 "__cosim_hostmem_write.arg");
524 auto *bundleType = new BundleType(
525 "cosimHostMem",
526 {{"arg", BundleType::Direction::To, writeReqType},
527 {"result", BundleType::Direction::From, writeRespType}});
528 write.reset(CallService::Callback::get(acc, AppID("__cosim_hostmem_write"),
529 bundleType, *writeRespPort,
530 *writeReqPort));
531 write->connect([this](const MessageData &req) { return serviceWrite(req); },
532 true);
533 }
534
535 // Service the read request as a callback. Simply reads the data from the
536 // location specified. TODO: check that the memory has been mapped.
537 bool serviceRead(const MessageData &reqBytes) {
538 const HostMemReadReq *req = reqBytes.as<HostMemReadReq>();
539 acc.getLogger().trace(
540 [&](std::string &subsystem, std::string &msg,
541 std::unique_ptr<std::map<std::string, std::any>> &details) {
542 subsystem = "hostmem";
543 msg = "Read request: addr=0x" + toHex(req->address) +
544 " len=" + std::to_string(req->length) +
545 " tag=" + std::to_string(req->tag);
546 });
547 // Send one response per 8 bytes.
548 uint64_t *dataPtr = reinterpret_cast<uint64_t *>(req->address);
549 for (uint32_t i = 0, e = (req->length + 7) / 8; i < e; ++i) {
550 HostMemReadResp resp{.data = dataPtr[i], .tag = req->tag};
551 acc.getLogger().trace(
552 [&](std::string &subsystem, std::string &msg,
553 std::unique_ptr<std::map<std::string, std::any>> &details) {
554 subsystem = "HostMem";
555 msg = "Read result: data=0x" + toHex(resp.data) +
556 " tag=" + std::to_string(resp.tag);
557 });
558 readRespPort->write(MessageData::from(resp));
559 }
560 return true;
561 }
562
563 // Service a write request as a callback. Simply write the data to the
564 // location specified. TODO: check that the memory has been mapped.
565 MessageData serviceWrite(const MessageData &reqBytes) {
566 const HostMemWriteReq *req = reqBytes.as<HostMemWriteReq>();
567 acc.getLogger().trace(
568 [&](std::string &subsystem, std::string &msg,
569 std::unique_ptr<std::map<std::string, std::any>> &details) {
570 subsystem = "hostmem";
571 msg = "Write request: addr=0x" + toHex(req->address) + " data=0x" +
572 toHex(req->data) +
573 " valid_bytes=" + std::to_string(req->valid_bytes) +
574 " tag=" + std::to_string(req->tag);
575 });
576 uint8_t *dataPtr = reinterpret_cast<uint8_t *>(req->address);
577 for (uint8_t i = 0; i < req->valid_bytes; ++i)
578 dataPtr[i] = (req->data >> (i * 8)) & 0xFF;
579 HostMemWriteResp resp = req->tag;
580 return MessageData::from(resp);
581 }
582
583 struct CosimHostMemRegion : public HostMemRegion {
584 CosimHostMemRegion(std::size_t size) {
585 ptr = malloc(size);
586 memset(ptr, 0xFF, size);
587 this->size = size;
588 }
589 virtual ~CosimHostMemRegion() { free(ptr); }
590 virtual void *getPtr() const override { return ptr; }
591 virtual std::size_t getSize() const override { return size; }
592
593 private:
594 void *ptr;
595 std::size_t size;
596 };
597
598 virtual std::unique_ptr<HostMemRegion>
599 allocate(std::size_t size, HostMem::Options opts) const override {
600 auto ret = std::unique_ptr<HostMemRegion>(new CosimHostMemRegion(size));
601 acc.getLogger().debug(
602 [&](std::string &subsystem, std::string &msg,
603 std::unique_ptr<std::map<std::string, std::any>> &details) {
604 subsystem = "HostMem";
605 msg = "Allocated host memory region at 0x" + toHex(ret->getPtr()) +
606 " of size " + std::to_string(size);
607 });
608 return ret;
609 }
610 virtual bool mapMemory(void *ptr, std::size_t size,
611 HostMem::Options opts) const override {
612 return true;
613 }
614 virtual void unmapMemory(void *ptr) const override {}
615
616private:
617 const esi::Type *getType(Context &ctxt, esi::Type *type) {
618 if (auto t = ctxt.getType(type->getID())) {
619 delete type;
620 return *t;
621 }
622 ctxt.registerType(type);
623 return type;
624 }
626 Context &ctxt;
627 StubContainer *rpcClient;
628 std::unique_ptr<WriteCosimChannelPort> readRespPort;
629 std::unique_ptr<ReadCosimChannelPort> readReqPort;
630 std::unique_ptr<CallService::Callback> read;
631 std::unique_ptr<WriteCosimChannelPort> writeRespPort;
632 std::unique_ptr<ReadCosimChannelPort> writeReqPort;
633 std::unique_ptr<CallService::Callback> write;
634};
635} // namespace
636
637namespace esi::backends::cosim {
638/// Implement the magic cosim channel communication.
639class CosimEngine : public Engine {
640public:
642 const ServiceImplDetails &details, const HWClientDetails &clients)
643 : Engine(conn), conn(conn) {
644 // Compute our parents idPath path.
645 AppIDPath prefix = std::move(idPath);
646 if (prefix.size() > 0)
647 prefix.pop_back();
648
649 for (auto client : clients) {
650 AppIDPath fullClientPath = prefix + client.relPath;
651 std::map<std::string, std::string> channelAssignments;
652 for (auto assignment : client.channelAssignments)
653 if (assignment.second.type == "cosim")
654 channelAssignments[assignment.first] = std::any_cast<std::string>(
655 assignment.second.implOptions.at("name"));
656 clientChannelAssignments[fullClientPath] = std::move(channelAssignments);
657 }
658 }
659
660 std::unique_ptr<ChannelPort> createPort(AppIDPath idPath,
661 const std::string &channelName,
663 const Type *type) override;
664
665private:
667 std::map<AppIDPath, std::map<std::string, std::string>>
669};
670} // namespace esi::backends::cosim
671
672std::unique_ptr<ChannelPort>
673CosimEngine::createPort(AppIDPath idPath, const std::string &channelName,
674 BundleType::Direction dir, const Type *type) {
675
676 // Find the client details for the port at 'fullPath'.
677 auto f = clientChannelAssignments.find(idPath);
678 if (f == clientChannelAssignments.end())
679 throw std::runtime_error("Could not find port for '" + idPath.toStr() +
680 "." + channelName + "'");
681 const std::map<std::string, std::string> &channelAssignments = f->second;
682 auto cosimChannelNameIter = channelAssignments.find(channelName);
683 if (cosimChannelNameIter == channelAssignments.end())
684 throw std::runtime_error("Could not find channel '" + idPath.toStr() + "." +
685 channelName + "' in cosimulation");
686
687 // Get the endpoint, which may or may not exist. Construct the port.
688 // Everything is validated when the client calls 'connect()' on the port.
689 ChannelDesc chDesc;
690 if (!conn.rpcClient->getChannelDesc(cosimChannelNameIter->second, chDesc))
691 throw std::runtime_error("Could not find channel '" + idPath.toStr() + "." +
692 channelName + "' in cosimulation");
693
694 std::unique_ptr<ChannelPort> port;
695 std::string fullChannelName = idPath.toStr() + "." + channelName;
696 if (BundlePort::isWrite(dir))
697 port = std::make_unique<WriteCosimChannelPort>(
698 conn, conn.rpcClient->stub.get(), chDesc, type, fullChannelName);
699 else
700 port = std::make_unique<ReadCosimChannelPort>(
701 conn, conn.rpcClient->stub.get(), chDesc, type, fullChannelName);
702 return port;
703}
704
705void CosimAccelerator::createEngine(const std::string &engineTypeName,
706 AppIDPath idPath,
707 const ServiceImplDetails &details,
708 const HWClientDetails &clients) {
709
710 std::unique_ptr<Engine> engine = nullptr;
711 if (engineTypeName == "cosim")
712 engine = std::make_unique<CosimEngine>(*this, idPath, details, clients);
713 else
714 engine = ::esi::registry::createEngine(*this, engineTypeName, idPath,
715 details, clients);
716 registerEngine(idPath, std::move(engine), clients);
717}
719 AppIDPath idPath, std::string implName,
720 const ServiceImplDetails &details,
721 const HWClientDetails &clients) {
722 if (svcType == typeid(services::MMIO)) {
723 return new CosimMMIO(*this, getCtxt(), rpcClient, clients);
724 } else if (svcType == typeid(services::HostMem)) {
725 return new CosimHostMem(*this, getCtxt(), rpcClient);
726 } else if (svcType == typeid(SysInfo)) {
727 switch (manifestMethod) {
729 return new CosimSysInfo(*this, rpcClient->stub.get());
731 return new MMIOSysInfo(getService<services::MMIO>());
732 }
733 }
734 return nullptr;
735}
736
740
#define REGISTER_ACCELERATOR(Name, TAccelerator)
assert(baseType &&"element must be base type")
static void checkStatus(Status s, const std::string &msg)
Definition Cosim.cpp:45
Abstract class representing a connection to an accelerator.
Definition Accelerator.h:79
virtual void disconnect()
Disconnect from the accelerator cleanly.
Context & getCtxt() const
Definition Accelerator.h:83
Context & ctxt
ESI accelerator context.
void registerEngine(AppIDPath idPath, std::unique_ptr< Engine > engine, const HWClientDetails &clients)
If createEngine is overridden, this method should be called to register the engine and all of the cha...
Logger & getLogger() const
Definition Accelerator.h:84
std::string toStr() const
Definition Manifest.cpp:733
static bool isWrite(BundleType::Direction bundleDir)
Compute the direction of a channel given the bundle direction and the bundle port's direction.
Definition Ports.h:230
Bundles represent a collection of channels.
Definition Types.h:44
AcceleratorConnections, Accelerators, and Manifests must all share a context.
Definition Context.h:31
Engines implement the actual channel communication between the host and the accelerator.
Definition Engines.h:42
void debug(const std::string &subsystem, const std::string &msg, const std::map< std::string, std::any > *details=nullptr)
Report a debug message.
Definition Logging.h:83
void trace(const std::string &subsystem, const std::string &msg, const std::map< std::string, std::any > *details=nullptr)
Log a trace message.
Definition Logging.h:106
A logical chunk of data representing serialized data.
Definition Common.h:103
const T * as() const
Cast to a type.
Definition Common.h:119
static MessageData from(T &t)
Cast from a type to its raw bytes.
Definition Common.h:129
A ChannelPort which reads data from the accelerator.
Definition Ports.h:124
virtual void disconnect() override
Definition Ports.h:129
Structs are an ordered collection of fields, each with a name and a type.
Definition Types.h:137
Root class of the ESI type system.
Definition Types.h:27
ID getID() const
Definition Types.h:33
Unsigned integer.
Definition Types.h:131
A ChannelPort which sends data to the accelerator.
Definition Ports.h:77
Connect to an ESI simulation.
Definition Cosim.h:38
void createEngine(const std::string &engineTypeName, AppIDPath idPath, const ServiceImplDetails &details, const HWClientDetails &clients) override
Create a new engine for channel communication with the accelerator.
Definition Cosim.cpp:705
void setManifestMethod(ManifestMethod method)
Definition Cosim.cpp:737
static std::unique_ptr< AcceleratorConnection > connect(Context &, std::string connectionString)
Parse the connection std::string and instantiate the accelerator.
Definition Cosim.cpp:69
virtual Service * createService(Service::Type service, AppIDPath path, std::string implName, const ServiceImplDetails &details, const HWClientDetails &clients) override
Called by getServiceImpl exclusively.
Definition Cosim.cpp:718
CosimAccelerator(Context &, std::string hostname, uint16_t port)
Construct and connect to a cosim server.
Definition Cosim.cpp:121
std::set< std::unique_ptr< ChannelPort > > channels
Definition Cosim.h:76
Implement the magic cosim channel communication.
Definition Cosim.cpp:639
CosimEngine(CosimAccelerator &conn, AppIDPath idPath, const ServiceImplDetails &details, const HWClientDetails &clients)
Definition Cosim.cpp:641
std::map< AppIDPath, std::map< std::string, std::string > > clientChannelAssignments
Definition Cosim.cpp:668
std::unique_ptr< ChannelPort > createPort(AppIDPath idPath, const std::string &channelName, BundleType::Direction dir, const Type *type) override
Each engine needs to know how to create a ports.
Definition Cosim.cpp:673
static Callback * get(AcceleratorConnection &acc, AppID id, BundleType *type, WriteChannelPort &result, ReadChannelPort &arg)
Definition Services.cpp:237
static Function * get(AppID id, BundleType *type, WriteChannelPort &arg, ReadChannelPort &result)
Definition Services.cpp:194
Implement the SysInfo API for a standard MMIO protocol.
Definition Services.h:184
Parent class of all APIs modeled as 'services'.
Definition Services.h:46
const std::type_info & Type
Definition Services.h:48
Information about the Accelerator system.
Definition Services.h:100
std::unique_ptr< Engine > createEngine(AcceleratorConnection &conn, const std::string &dmaEngineName, AppIDPath idPath, const ServiceImplDetails &details, const HWClientDetails &clients)
Create an engine by name.
Definition Engines.cpp:507
Definition esi.py:1
std::map< std::string, std::any > ServiceImplDetails
Definition Common.h:98
std::string toHex(void *val)
Definition Common.cpp:37
std::vector< HWClientDetail > HWClientDetails
Definition Common.h:97
Hack around C++ not having a way to forward declare a nested class.
Definition Cosim.cpp:53
std::unique_ptr< ChannelServer::Stub > stub
Definition Cosim.cpp:56
bool getChannelDesc(const std::string &channelName, esi::cosim::ChannelDesc &desc)
Get the type ID for a channel name.
Definition Cosim.cpp:336
StubContainer(std::unique_ptr< ChannelServer::Stub > stub)
Definition Cosim.cpp:54
Options for allocating host memory.
Definition Services.h:226