Skip to content

Commit

Permalink
Hide simulation device/host impl details
Browse files Browse the repository at this point in the history
  • Loading branch information
vtangTT committed Oct 31, 2024
1 parent 84d2e84 commit 331cfd0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 46 deletions.
61 changes: 32 additions & 29 deletions device/simulation/tt_simulation_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,44 @@
#include <string>
#include <vector>

#include <uv.h>
#include <nng/nng.h>

#include "common/logger.hpp"
#include "common/assert.hpp"
#include "device/driver_atomics.h"
#include "device/tt_cluster_descriptor.h"

#include "tt_simulation_device.h"
#include "uv.h"
#include "tt_simulation_device_generated.h"

flatbuffers::FlatBufferBuilder create_flatbuffer(DEVICE_COMMAND rw, std::vector<uint32_t> vec, tt_cxy_pair core_, uint64_t addr, uint64_t size_=0){
flatbuffers::FlatBufferBuilder builder;
auto data = builder.CreateVector(vec);
auto core = tt_vcs_core(core_.x, core_.y);
uint64_t size = size_ == 0 ? size = vec.size()*sizeof(uint32_t) : size = size_;
auto device_cmd = CreateDeviceRequestResponse(builder, rw, data, &core, addr, size);
builder.Finish(device_cmd);
return builder;
}

void print_flatbuffer(const DeviceRequestResponse *buf){
std::vector<uint32_t> data_vec(buf->data()->begin(), buf->data()->end());
uint64_t addr = buf->address();
uint32_t size = buf->size();
tt_cxy_pair core = {0, buf->core()->x(), buf->core()->y()};

std::stringstream ss;
ss << std::hex << reinterpret_cast<uintptr_t>(addr);
std::string addr_hex = ss.str();
log_info(tt::LogEmulationDriver, "{} bytes @ address {} in core ({}, {})", size, addr_hex, core.x, core.y);
for(int i = 0; i < data_vec.size(); i++){
std::ios_base::fmtflags save = std::cout.flags();
std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << data_vec[i] << " ";
std::cout.flags(save);
}
std::cout << std::endl;
}

tt_SimulationDevice::tt_SimulationDevice(const std::string &sdesc_path) : tt_device(sdesc_path){
log_info(tt::LogEmulationDriver, "Instantiating simulation device");
Expand Down Expand Up @@ -202,31 +233,3 @@ std::uint32_t tt_SimulationDevice::get_num_host_channels(std::uint32_t device_id

std::uint32_t tt_SimulationDevice::get_host_channel_size(std::uint32_t device_id, std::uint32_t channel) {return 0;}
std::uint32_t tt_SimulationDevice::get_numa_node_for_pcie_device(std::uint32_t device_id) {return 0;}

flatbuffers::FlatBufferBuilder tt_SimulationDevice::create_flatbuffer(DEVICE_COMMAND rw, std::vector<uint32_t> vec, tt_cxy_pair core_, uint64_t addr, uint64_t size_){
flatbuffers::FlatBufferBuilder builder;
auto data = builder.CreateVector(vec);
auto core = tt_vcs_core(core_.x, core_.y);
uint64_t size = size_ == 0 ? size = vec.size()*sizeof(uint32_t) : size = size_;
auto device_cmd = CreateDeviceRequestResponse(builder, rw, data, &core, addr, size);
builder.Finish(device_cmd);
return builder;
}

void tt_SimulationDevice::print_flatbuffer(const DeviceRequestResponse *buf){
std::vector<uint32_t> data_vec(buf->data()->begin(), buf->data()->end());
uint64_t addr = buf->address();
uint32_t size = buf->size();
tt_cxy_pair core = {0, buf->core()->x(), buf->core()->y()};

std::stringstream ss;
ss << std::hex << reinterpret_cast<uintptr_t>(addr);
std::string addr_hex = ss.str();
log_info(tt::LogEmulationDriver, "{} bytes @ address {} in core ({}, {})", size, addr_hex, core.x, core.y);
for(int i = 0; i < data_vec.size(); i++){
std::ios_base::fmtflags save = std::cout.flags();
std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << data_vec[i] << " ";
std::cout.flags(save);
}
std::cout << std::endl;
}
4 changes: 0 additions & 4 deletions device/simulation/tt_simulation_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <vector>

#include "device/tt_device.h"
#include "tt_simulation_device_generated.h"
#include "device/simulation/tt_simulation_host.hpp"

class tt_SimulationDevice: public tt_device {
Expand Down Expand Up @@ -69,7 +68,4 @@ class tt_SimulationDevice: public tt_device {
std::set<chip_id_t> target_remote_chips = {};
tt::ARCH arch_name;
std::shared_ptr<tt_ClusterDescriptor> ndesc;

flatbuffers::FlatBufferBuilder create_flatbuffer(DEVICE_COMMAND rw, std::vector<uint32_t> vec, tt_cxy_pair core_, uint64_t addr, uint64_t size_=0);
void print_flatbuffer(const DeviceRequestResponse *buf);
};
21 changes: 14 additions & 7 deletions device/simulation/tt_simulation_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@
#include <cassert>
#include <cstdlib>

#include <nng/nng.h>
#include <nng/protocol/pair1/pair.h>

#include "common/logger.hpp"
#include "common/assert.hpp"
#include "tt_simulation_host.hpp"

tt_SimulationHost::tt_SimulationHost() {
// Initialize socket and dialer
host_socket = std::make_unique<nng_socket>();
host_dialer = std::make_unique<nng_dialer>();

// Get current date time string
std::time_t time = std::time(nullptr);
std::tm local_time = *std::localtime(&time);
Expand All @@ -32,21 +39,21 @@ tt_SimulationHost::tt_SimulationHost() {

// Open socket and create dialer
log_info(tt::LogEmulationDriver, "Dialing: {}", nng_socket_addr);
nng_pair1_open(&host_socket);
int rv = nng_dialer_create(&host_dialer, host_socket, nng_socket_addr);
nng_pair1_open(host_socket.get());
int rv = nng_dialer_create(host_dialer.get(), *host_socket, nng_socket_addr);
TT_ASSERT(rv == 0, "Failed to create dialer: {} {}", nng_strerror(rv), nng_socket_addr);
}

tt_SimulationHost::~tt_SimulationHost() {
nng_dialer_close(host_dialer);
nng_close(host_socket);
nng_dialer_close(*host_dialer);
nng_close(*host_socket);
}

void tt_SimulationHost::start_host() {
// Establish connection with remote VCS simulator
int rv;
do {
rv = nng_dialer_start(host_dialer, 0);
rv = nng_dialer_start(*host_dialer, 0);
if (rv != 0) {
log_info(tt::LogEmulationDriver, "Waiting for remote: {}", nng_strerror(rv));
std::this_thread::sleep_for(std::chrono::seconds(1));
Expand All @@ -61,7 +68,7 @@ void tt_SimulationHost::send_to_device(uint8_t *buf, size_t buf_size) {
void *msg = nng_alloc(buf_size);
std::memcpy(msg, buf, buf_size);

rv = nng_send(host_socket, msg, buf_size, NNG_FLAG_ALLOC);
rv = nng_send(*host_socket, msg, buf_size, NNG_FLAG_ALLOC);
log_debug(tt::LogEmulationDriver, "Message sent.");
if (rv != 0) {
log_info(tt::LogEmulationDriver, "Failed to send message to remote: {}", nng_strerror(rv));
Expand All @@ -72,7 +79,7 @@ size_t tt_SimulationHost::recv_from_device(void **data_ptr) {
int rv;
size_t data_size;
log_debug(tt::LogEmulationDriver, "Receiving messsage from remote..");
rv = nng_recv(host_socket, data_ptr, &data_size, NNG_FLAG_ALLOC);
rv = nng_recv(*host_socket, data_ptr, &data_size, NNG_FLAG_ALLOC);
log_debug(tt::LogEmulationDriver, "Message received.");
if (rv != 0) {
log_info(tt::LogEmulationDriver, "Failed to receive message from remote: {}", nng_strerror(rv));
Expand Down
11 changes: 6 additions & 5 deletions device/simulation/tt_simulation_host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
// SPDX-License-Identifier: Apache-2.0
#include <vector>
#include <cstdint>

#include <nng/nng.h>
#include <nng/protocol/pair1/pair.h>
#include <memory>

#include "device/tt_xy_pair.h"

#define NNG_SOCKET_PREFIX "ipc:///tmp/"

typedef struct nng_socket_s nng_socket;
typedef struct nng_dialer_s nng_dialer;

class tt_SimulationHost {
public:
tt_SimulationHost();
Expand All @@ -20,6 +21,6 @@ class tt_SimulationHost {
void send_to_device(uint8_t *buf, size_t buf_size);
size_t recv_from_device(void **data_ptr);
private:
nng_socket host_socket;
nng_dialer host_dialer;
std::unique_ptr<nng_socket> host_socket;
std::unique_ptr<nng_dialer> host_dialer;
};
4 changes: 3 additions & 1 deletion tests/simulation/device_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "tt_simulation_device.h"
#include "common/logger.hpp"
#include "tests/test_utils/generate_cluster_desc.hpp"

#include <nng/nng.h>
#include <nng/protocol/pipeline0/pull.h>
Expand All @@ -17,8 +18,9 @@
class SimulationDeviceFixture : public ::testing::Test {
protected:
static void SetUpTestSuite() {
// default_params and yaml path are both dummy and won't change test behavior
tt_device_params default_params;
device = std::make_unique<tt_SimulationDevice>("");
device = std::make_unique<tt_SimulationDevice>(test_utils::GetAbsPath("tests/soc_descs/grayskull_10x12.yaml"));
device->start_device(default_params);
}

Expand Down

0 comments on commit 331cfd0

Please sign in to comment.