Skip to content

Commit

Permalink
[cpp] Start replacing printf with libfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Jul 14, 2024
1 parent 3e13923 commit bd2b3a9
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 125 deletions.
1 change: 0 additions & 1 deletion src/cpp/main_2d/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "pffdtd/video.hpp"

#include <fmt/format.h>
#include <fmt/os.h>

#include <concepts>

Expand Down
1 change: 0 additions & 1 deletion src/cpp/main_2d/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <CLI/CLI.hpp>

#include <fmt/format.h>
#include <fmt/os.h>

#include <chrono>
#include <filesystem>
Expand Down
1 change: 0 additions & 1 deletion src/cpp/main_3d/engine_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "pffdtd/utility.hpp"

#include <fmt/format.h>
#include <fmt/os.h>

#include <omp.h>

Expand Down
1 change: 0 additions & 1 deletion src/cpp/main_3d/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#endif

#include <fmt/format.h>
#include <fmt/os.h>

#include <chrono>

Expand Down
66 changes: 34 additions & 32 deletions src/cpp/pffdtd/progress.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "progress.hpp"

#include <fmt/format.h>

#include <cstdio>
#include <ctime>
#include <sys/ioctl.h> //terminal width
Expand Down Expand Up @@ -36,30 +38,30 @@ void print_progress(
if (n > 0) {
// back up
for (int nl = 0; nl < nlines; nl++) {
printf("\033[1A");
fmt::print("\033[1A");
}
// clear lines
for (int nl = 0; nl < nlines; nl++) {
for (int cc = 0; cc < ncols; cc++) {
printf(" ");
fmt::print(" ");
}
printf("\n");
fmt::print("\n");
}
// back up
for (int nl = 0; nl < nlines; nl++) {
printf("\033[1A");
fmt::print("\033[1A");
}
}
printf("\n");
fmt::print("\n");
// progress bar
printf("Running [%.1f%%]", pcnt);
fmt::print("Running [{:.1f}%]", pcnt);
if (ncols >= ncolsl) {
for (int cc = 0; cc < (0.01 * pcnt * ncolsl); cc++) {
printf("=");
fmt::print("=");
}
printf(">");
fmt::print(">");
for (int cc = (0.01 * pcnt * ncolsl); cc < ncolsl; cc++) {
printf(".");
fmt::print(".");
}
}
double est_total = time_elapsed * Nt / n;
Expand All @@ -76,33 +78,33 @@ void print_progress(
s_t = (sec - (3600 * h_t) - (m_t * 60));

// clang-format off
printf("[");
printf("%02d:%02d:%02d<%02d:%02d:%02d]", h_e, m_e, s_e, h_t, m_t, s_t);
printf("\n");
printf("T: %06.1f", 1e-6 * Npts * n / time_elapsed); //"total" Mvox/s (averaged up to current time)
printf(" - ");
printf("I: %06.1f", 1e-6 * Npts / time_elapsed_sample); // instantaneous Mvox/s (per time-step)
printf(" | ");
printf("TPW: %06.1f", 1e-6 * Npts * n / time_elapsed / num_workers); // total per worker
printf(" - ");
printf("IPW: %06.1f", 1e-6 * Npts / time_elapsed_sample / num_workers); // inst per worker
printf("\n");
fmt::print("[");
fmt::print("{:02d}:{:02d}:{:02d}<{:02d}:{:02d}:{:02d}]", h_e, m_e, s_e, h_t, m_t, s_t);
fmt::println("");
fmt::print("T: {:06.1f}", 1e-6 * Npts * n / time_elapsed); //"total" Mvox/s (averaged up to current time)
fmt::print(" - ");
fmt::print("I: {:06.1f}", 1e-6 * Npts / time_elapsed_sample); // instantaneous Mvox/s (per time-step)
fmt::print(" | ");
fmt::print("TPW: {:06.1f}", 1e-6 * Npts * n / time_elapsed / num_workers); // total per worker
fmt::print(" - ");
fmt::print("IPW: {:06.1f}", 1e-6 * Npts / time_elapsed_sample / num_workers); // inst per worker
fmt::println("");

printf("TA: %06.1f", 1e-6 * Npts * n / time_elapsed_air); // total for air bit
printf(" - ");
printf("IA: %06.1f", 1e-6 * Npts / time_elapsed_sample_air); // inst for air bit
fmt::print("TA: {:06.1f}", 1e-6 * Npts * n / time_elapsed_air); // total for air bit
fmt::print(" - ");
fmt::print("IA: {:06.1f}", 1e-6 * Npts / time_elapsed_sample_air); // inst for air bit

printf("\n");
printf("TB: %06.1f", 1e-6 * Nb * n / time_elapsed_bn); // total for bn
printf(" - ");
printf("IB: %06.1f", 1e-6 * Nb / time_elapsed_sample_bn); // inst for bn
fmt::println("");
fmt::print("TB: {:06.1f}", 1e-6 * Nb * n / time_elapsed_bn); // total for bn
fmt::print(" - ");
fmt::print("IB: {:06.1f}", 1e-6 * Nb / time_elapsed_sample_bn); // inst for bn

printf("\n");
fmt::println("");

printf("T: %02.1f%%", 100.0 * time_elapsed_air / time_elapsed); //% for air (total)
printf(" - ");
printf("I: %02.1f%%", 100.0 * time_elapsed_sample_air / time_elapsed_sample); //% for air (inst)
printf("\n");
fmt::print("T: {:02.1f}%", 100.0 * time_elapsed_air / time_elapsed); //% for air (total)
fmt::print(" - ");
fmt::print("I: {:02.1f}%", 100.0 * time_elapsed_sample_air / time_elapsed_sample); //% for air (inst)
fmt::println("");
// clang-format on

fflush(stdout);
Expand Down
1 change: 0 additions & 1 deletion src/cpp/pffdtd/simulation_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "pffdtd/hdf.hpp"

#include <fmt/format.h>
#include <fmt/os.h>

namespace pffdtd {

Expand Down
85 changes: 5 additions & 80 deletions src/cpp/pffdtd/simulation_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "simulation_3d.hpp"

#include <fmt/format.h>

#include <algorithm>
#include <cassert>
#include <cmath>
Expand Down Expand Up @@ -132,7 +134,6 @@ void loadSimulation3D(Simulation3D& sim) {
readH5Constant(file, dset_str, (void*)&fcc_flag, INT8);
printf("fcc_flag=%d\n", fcc_flag);
assert((fcc_flag >= 0) && (fcc_flag <= 2));
// printf("%s", fcc>0 ? "fcc=true\n" : "fcc=false\n");

if (H5Fclose(file) != 0) {
printf("error closing file %s", filename);
Expand Down Expand Up @@ -213,12 +214,6 @@ void loadSimulation3D(Simulation3D& sim) {
readH5Dataset(file, dset_str, expected_ndims, dims, (void**)&bn_ixyz, INT64);
assert((int64_t)dims[0] == Nb);

// printf("bn_ixyz=");
// for (int64_t i=0; i<Nb; i++) {
// printf("%ld, ",bn_ixyz[i]);
//}
// printf("\n");

//////////////////
// adj_bn dataset
//////////////////
Expand All @@ -228,16 +223,6 @@ void loadSimulation3D(Simulation3D& sim) {
assert((int64_t)dims[0] == Nb);
assert(dims[1] == (hsize_t)NN);

// printf("adj_bn=");
// for (int64_t i=0; i<Nb; i++) {
// printf("[");
// for (int8_t j=0; j<NN; j++) {
// printf("%d,",adj_bn_bool[i*NN+j]);
//}
// printf("] -- ");
//}
// printf("\n");

//////////////////
// mat_bn dataset
//////////////////
Expand All @@ -264,12 +249,6 @@ void loadSimulation3D(Simulation3D& sim) {
}
free(saf_bn);

// printf("saf=");
// for (int64_t i=0; i<Nb; i++) {
// printf("%.16g, ",saf_bn[i]);
//}
// printf("\n");

if (H5Fclose(file) != 0) {
printf("error closing file %s", filename);
assert(true == false);
Expand Down Expand Up @@ -318,12 +297,6 @@ void loadSimulation3D(Simulation3D& sim) {
readH5Dataset(file, dset_str, expected_ndims, dims, (void**)&in_ixyz, INT64);
assert((int64_t)dims[0] == Ns);

// printf("in_ixyz=");
// for (int64_t i=0; i<Ns; i++) {
// printf("%ld, ",in_ixyz[i]);
//}
// printf("\n");

//////////////////
// out_ixyz dataset
//////////////////
Expand All @@ -344,12 +317,6 @@ void loadSimulation3D(Simulation3D& sim) {
);
assert((int64_t)dims[0] == Nr);

// printf("out_ixyz=");
// for (int64_t i=0; i<Nr; i++) {
// printf("%ld, ",out_ixyz[i]);
//}
// printf("\n");

//////////////////
// in_sigs dataset
//////////////////
Expand All @@ -359,16 +326,6 @@ void loadSimulation3D(Simulation3D& sim) {
assert((int64_t)dims[0] == Ns);
assert((int64_t)dims[1] == Nt);

// printf("in_sigs=");
// for (int64_t i=0; i<Ns; i++) {
// printf("in %ld: ",i);
// for (int64_t j=0; j<Nt; j++) {
// printf("%.16g, ",in_sigs[i*Nt + j]);
//}
// printf("\n");
//}
// printf("\n");

if (H5Fclose(file) != 0) {
printf("error closing file %s", filename);
assert(true == false);
Expand Down Expand Up @@ -418,9 +375,10 @@ void loadSimulation3D(Simulation3D& sim) {
allocate_zeros((void**)&mat_beta, Nm * sizeof(Real));
for (int8_t i = 0; i < Nm; i++) {
double* DEF; // for one material
sprintf(dset_str, "mat_%02d_DEF", i);
// sprintf(dset_str, "mat_%02d_DEF", i);
auto id = fmt::format("mat_{:02d}_DEF", i);
expected_ndims = 2;
readH5Dataset(file, dset_str, expected_ndims, dims, (void**)&DEF, FLOAT64);
readH5Dataset(file, id.data(), expected_ndims, dims, (void**)&DEF, FLOAT64);
assert((int8_t)dims[0] == Mb[i]);
assert((int8_t)dims[1] == 3);
assert(Mb[i] <= MMb);
Expand Down Expand Up @@ -454,39 +412,6 @@ void loadSimulation3D(Simulation3D& sim) {
}
free(DEF);
}
/*
for (int8_t i=0; i<Nm; i++) {
printf("b[%d]=",i);
for (int8_t j=0; j<Mb[i]; j++) {
int32_t mij = (int32_t)MMb*i+j;
printf(" %.16g, ",mat_quads[mij].b);
}
printf("\n");
printf("bd[%d]=",i);
for (int8_t j=0; j<Mb[i]; j++) {
int32_t mij = (int32_t)MMb*i+j;
printf(" %.16g, ",mat_quads[mij].bd);
}
printf("\n");
printf("bDh[%d]=",i);
for (int8_t j=0; j<Mb[i]; j++) {
int32_t mij = (int32_t)MMb*i+j;
printf(" %.16g, ",mat_quads[mij].bDh);
}
printf("\n");
printf("bFh[%d]=",i);
for (int8_t j=0; j<Mb[i]; j++) {
int32_t mij = (int32_t)MMb*i+j;
printf(" %.16g, ",mat_quads[mij].bFh);
}
printf("\n");
printf("mat_beta[%d]= %.16g \n",i,mat_beta[i]);
}
for (int8_t i=0; i<Nm; i++) {
printf("Mb[%d]=%d\n",i,Mb[i]);
}
*/

if (H5Fclose(file) != 0) {
printf("error closing file %s", filename);
Expand Down
1 change: 0 additions & 1 deletion src/cpp/pffdtd/sycl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <sycl/sycl.hpp>

#include <fmt/format.h>
#include <fmt/os.h>

#include <string>
#include <utility>
Expand Down
12 changes: 5 additions & 7 deletions src/cpp/pffdtd/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

#include "utility.hpp"

#include <cassert>
#include <cstdio>
#include "pffdtd/exception.hpp"

#include <cstdlib>
#include <cstring>

Expand All @@ -25,11 +25,10 @@
void allocate_zeros(void** arr, uint64_t Nbytes) {
*arr = malloc(Nbytes);
if (*arr == NULL) {
printf("Memory allocation failed");
assert(true == false); // to break
pffdtd::raise<std::bad_alloc>();
}
// initialise to zero
memset(*arr, 0, (size_t)Nbytes);
std::memset(*arr, 0, (size_t)Nbytes);
}

// for sorting int64 arrays and returning keys
Expand All @@ -52,8 +51,7 @@ void sort_keys(int64_t* val_arr, int64_t* key_arr, int64_t N) {
sort_int64_struct* struct_arr;
struct_arr = (sort_int64_struct*)malloc(N * sizeof(sort_int64_struct));
if (struct_arr == NULL) {
printf("Memory allocation failed");
assert(true == false); // to break
pffdtd::raise<std::bad_alloc>();
}
for (int64_t i = 0; i < N; i++) {
struct_arr[i].val = val_arr[i];
Expand Down

0 comments on commit bd2b3a9

Please sign in to comment.