Skip to content

Commit

Permalink
Replace snprintf and string concat with fmt::format
Browse files Browse the repository at this point in the history
  • Loading branch information
pjanevskiTT committed Sep 16, 2024
1 parent f7b1ce0 commit 02b2624
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 73 deletions.
9 changes: 4 additions & 5 deletions device/cpuset_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <thread>
#include "device/tt_device.h"
#include <filesystem>
#include "fmt/core.h"
namespace tt {

namespace fs = std::filesystem;
Expand Down Expand Up @@ -97,7 +98,7 @@ bool tt_cpuset_allocator::init_find_tt_pci_devices_packages_numanodes(){
m_num_tt_device_by_pci_device_id_map[device_id_revision] += 1;

std::string pci_bus_id_str = get_pci_bus_id(pci_device_obj);
std::string pci_device_dir = "/sys/bus/pci/devices/" + pci_bus_id_str + "/tenstorrent/";
std::string pci_device_dir = fmt::format("/sys/bus/pci/devices/{}/tenstorrent/", pci_bus_id_str);
int physical_device_id = -1;

log_trace(LogSiliconDriver, "Found TT device with pci_bus_id_str: {} num_devices_by_pci_device_id: {}", pci_bus_id_str, m_num_tt_device_by_pci_device_id_map[device_id_revision]);
Expand Down Expand Up @@ -376,11 +377,9 @@ std::string tt_cpuset_allocator::get_pci_bus_id(hwloc_obj_t pci_device_obj){

std::string pci_bus_id_str = "";

if (hwloc_obj_type_is_io(pci_device_obj->type)) {
char pci_bus_id[14];
if (hwloc_obj_type_is_io(pci_device_obj->type)) {
auto attrs = pci_device_obj->attr->pcidev;
snprintf(pci_bus_id, sizeof(pci_bus_id), "%04x:%02x:%02x.%01x", attrs.domain, attrs.bus, attrs.dev, attrs.func);
pci_bus_id_str = (std::string) pci_bus_id;
pci_bus_id_str = fmt::format("{:04x}{:02x}{:02x}{:01x}", attrs.domain, attrs.bus, attrs.dev, attrs.func);
}

return pci_bus_id_str;
Expand Down
4 changes: 3 additions & 1 deletion device/tt_cluster_descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "common/logger.hpp"
#include "yaml-cpp/yaml.h"

#include "fmt/core.h"

using namespace tt;
bool tt_ClusterDescriptor::ethernet_core_has_active_ethernet_link(chip_id_t local_chip, ethernet_channel_t local_ethernet_channel) const {
return this->ethernet_connections.find(local_chip) != this->ethernet_connections.end() &&
Expand Down Expand Up @@ -297,7 +299,7 @@ std::unique_ptr<tt_ClusterDescriptor> tt_ClusterDescriptor::create_from_yaml(con

std::ifstream fdesc(cluster_descriptor_file_path);
if (fdesc.fail()) {
throw std::runtime_error("Error: cluster connectivity descriptor file " + cluster_descriptor_file_path + " does not exist!");
throw std::runtime_error(fmt::format("Error: cluster connectivity descriptor file {} does not exist!", cluster_descriptor_file_path));
}
fdesc.close();

Expand Down
16 changes: 9 additions & 7 deletions device/tt_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "device/tlb.h"
#include "device/tt_io.hpp"

#include "fmt/core.h"

using TLB_DATA = tt::umd::tlb_data;


Expand Down Expand Up @@ -124,7 +126,7 @@ struct tt_version {
patch = version & 0xfff;
}
std::string str() const {
return std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(patch);
return fmt::format("{}.{}.{}", major, minor, patch);
}
};

Expand All @@ -147,7 +149,7 @@ struct tt_device_params {
if (dump_core == "*") {
for (size_t x = 0; x < grid_size.x; x++) {
for (size_t y = 0; y < grid_size.y; y++) {
std::string current_core_coord(std::to_string(x) + "-" + std::to_string(y));
std::string current_core_coord = fmt::format("{}-{}", x, y);
if (std::find(std::begin(unrolled_dump_core), std::end(unrolled_dump_core), current_core_coord) == std::end(unrolled_dump_core)) {
unrolled_dump_core.push_back(current_core_coord);
}
Expand All @@ -167,22 +169,22 @@ struct tt_device_params {
if (core_dim_x == "*" && core_dim_y == "*") {
for (size_t x = 0; x < grid_size.x; x++) {
for (size_t y = 0; y < grid_size.y; y++) {
std::string current_core_coord(std::to_string(x) + "-" + std::to_string(y));
std::string current_core_coord = fmt::format("{}-{}", x, y);
if (std::find(std::begin(unrolled_dump_core), std::end(unrolled_dump_core), current_core_coord) == std::end(unrolled_dump_core)) {
unrolled_dump_core.push_back(current_core_coord);
}
}
}
} else if (core_dim_x == "*") {
for (size_t x = 0; x < grid_size.x; x++) {
std::string current_core_coord(std::to_string(x) + "-" + core_dim_y);
std::string current_core_coord = fmt::format("{}-{}", x, core_dim_y);
if (std::find(std::begin(unrolled_dump_core), std::end(unrolled_dump_core), current_core_coord) == std::end(unrolled_dump_core)) {
unrolled_dump_core.push_back(current_core_coord);
}
}
} else if (core_dim_y == "*") {
for (size_t y = 0; y < grid_size.y; y++) {
std::string current_core_coord(core_dim_x + "-" + std::to_string(y));
std::string current_core_coord = fmt::format("{}-{}", core_dim_x, y);
if (std::find(std::begin(unrolled_dump_core), std::end(unrolled_dump_core), current_core_coord) == std::end(unrolled_dump_core)) {
unrolled_dump_core.push_back(current_core_coord);
}
Expand All @@ -196,8 +198,8 @@ struct tt_device_params {

std::vector<std::string> expand_plusargs() const {
std::vector<std::string> all_plusargs {
"+enable_perf_scoreboard=" + std::to_string(enable_perf_scoreboard),
"+register_monitor=" + std::to_string(register_monitor)
fmt::format("+enable_perf_scoreboard={}", enable_perf_scoreboard),
fmt::format("+register_monitor={}", register_monitor)
};

all_plusargs.insert(all_plusargs.end(), plusargs.begin(), plusargs.end());
Expand Down
Loading

0 comments on commit 02b2624

Please sign in to comment.