Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace snprintf and string concat with fmt::format #60

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 10 additions & 10 deletions device/pcie/pci_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void PCIDevice::setup_device() {
mappings.query_mappings.in.output_mapping_count = 8;

if (ioctl(device_fd, TENSTORRENT_IOCTL_QUERY_MAPPINGS, &mappings.query_mappings) == -1) {
throw std::runtime_error(std::string("Query mappings failed on device ") + std::to_string(device_id) + ".");
throw std::runtime_error(fmt::format("Query mappings failed on device {}.", device_id));
}

// Mapping resource to BAR
Expand Down Expand Up @@ -203,7 +203,7 @@ void PCIDevice::setup_device() {
}

if (bar0_uc_mapping.mapping_id != TENSTORRENT_MAPPING_RESOURCE0_UC) {
throw std::runtime_error(std::string("Device ") + std::to_string(device_id) + " has no BAR0 UC mapping.");
throw std::runtime_error(fmt::format("Device {} has no BAR0 UC mapping.", device_id));
}

auto wc_mapping_size = arch == tt::ARCH::BLACKHOLE ? BH_BAR0_WC_MAPPING_SIZE : GS_BAR0_WC_MAPPING_SIZE;
Expand Down Expand Up @@ -231,7 +231,7 @@ void PCIDevice::setup_device() {
bar0_uc = mmap(NULL, bar0_uc_size, PROT_READ | PROT_WRITE, MAP_SHARED, device_fd, bar0_uc_mapping.mapping_base + bar0_uc_offset);

if (bar0_uc == MAP_FAILED) {
throw std::runtime_error(std::string("BAR0 UC memory mapping failed for device ") + std::to_string(device_id) + ".");
throw std::runtime_error(fmt::format("BAR0 UC mapping failed for device {}.", device_id));
}

if (!bar0_wc) {
Expand All @@ -240,34 +240,34 @@ void PCIDevice::setup_device() {

if (arch == tt::ARCH::WORMHOLE_B0) {
if (bar4_uc_mapping.mapping_id != TENSTORRENT_MAPPING_RESOURCE2_UC) {
throw std::runtime_error(std::string("Device ") + std::to_string(device_id) + " has no BAR4 UC mapping.");
throw std::runtime_error(fmt::format("Device {} has no BAR4 UC mapping.", device_id));
}

system_reg_mapping_size = bar4_uc_mapping.mapping_size;

system_reg_mapping = mmap(NULL, bar4_uc_mapping.mapping_size, PROT_READ | PROT_WRITE, MAP_SHARED, device_fd, bar4_uc_mapping.mapping_base);

if (system_reg_mapping == MAP_FAILED) {
throw std::runtime_error(std::string("BAR4 UC memory mapping failed for device ") + std::to_string(device_id) + ".");
throw std::runtime_error(fmt::format("BAR4 UC mapping failed for device {}.", device_id));
}

system_reg_start_offset = (512 - 16) * 1024*1024;
system_reg_offset_adjust = (512 - 32) * 1024*1024;
} else if(arch == tt::ARCH::BLACKHOLE) {
if (bar2_uc_mapping.mapping_id != TENSTORRENT_MAPPING_RESOURCE1_UC) {
throw std::runtime_error(std::string("Device ") + std::to_string(device_id) + " has no BAR2 UC mapping.");
throw std::runtime_error(fmt::format("Device {} has no BAR2 UC mapping.", device_id));
}

// Using UnCachable memory mode. This is used for accessing registers on Blackhole.
bar2_uc_size = bar2_uc_mapping.mapping_size;
bar2_uc = mmap(NULL, bar2_uc_mapping.mapping_size, PROT_READ | PROT_WRITE, MAP_SHARED, device_fd, bar2_uc_mapping.mapping_base);

if (bar2_uc == MAP_FAILED) {
throw std::runtime_error(std::string("BAR2 UC memory mapping failed for device ") + std::to_string(device_id) + ".");
throw std::runtime_error(fmt::format("BAR2 UC mapping failed for device {}.", device_id));
}

if (bar4_wc_mapping.mapping_id != TENSTORRENT_MAPPING_RESOURCE2_WC) {
throw std::runtime_error(std::string("Device ") + std::to_string(device_id) + " has no BAR4 WC mapping.");
throw std::runtime_error(fmt::format("Device {} has no BAR4 WC mapping.", device_id));
}

// Using Write-Combine memory mode. This is used for accessing DRAM on Blackhole.
Expand All @@ -276,7 +276,7 @@ void PCIDevice::setup_device() {
bar4_wc = mmap(NULL, bar4_wc_mapping.mapping_size, PROT_READ | PROT_WRITE, MAP_SHARED, device_fd, bar4_wc_mapping.mapping_base);

if (bar4_wc == MAP_FAILED) {
throw std::runtime_error(std::string("BAR4 WC memory mapping failed for device ") + std::to_string(device_id) + ".");
throw std::runtime_error(fmt::format("BAR4 WC mapping failed for device {}.", device_id));
}
}

Expand Down Expand Up @@ -332,7 +332,7 @@ void PCIDevice::open_hugepage_per_host_mem_ch(uint32_t num_host_mem_channels) {
log_debug(LogSiliconDriver, "Opening device_fd_per_host_ch device index: {} ch: {} (num_host_mem_channels: {})", device_id, ch, num_host_mem_channels);
int device_fd_for_host_mem = find_device(device_id);
if (device_fd_for_host_mem == -1) {
throw std::runtime_error(std::string("Failed opening a host memory device handle for device ") + std::to_string(device_id));
throw std::runtime_error(fmt::format("Failed opening a host memory device handle for device {}.", device_id));
}
device_fd_per_host_ch.push_back(device_fd_for_host_mem);
}
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));
joelsmithTT marked this conversation as resolved.
Show resolved Hide resolved
}
fdesc.close();

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

#include "pcie/pci_device.hpp"
#include "fmt/core.h"

using TLB_DATA = tt::umd::tlb_data;

Expand Down Expand Up @@ -126,7 +127,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 @@ -149,7 +150,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 @@ -169,22 +170,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 @@ -198,8 +199,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
Loading