Skip to content

Commit

Permalink
Merge pull request lammps#4442 from akohlmey/freeze-fmt-lib-prepare-s…
Browse files Browse the repository at this point in the history
…td_format

Freeze fmt library at version 10.2.1 and add changes that prepare LAMMPS for transition to std::format
  • Loading branch information
akohlmey authored Jan 24, 2025
2 parents 0dfd5d8 + a30b5f9 commit a0fcbc9
Show file tree
Hide file tree
Showing 99 changed files with 7,247 additions and 7,253 deletions.
1 change: 1 addition & 0 deletions .github/workflows/style-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ jobs:
make check-permissions
make check-homepage
make check-errordocs
make check-fmtlib
19 changes: 11 additions & 8 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,24 @@ check_for_autogen_files(${LAMMPS_SOURCE_DIR})
#####################################################################
include(CheckIncludeFileCXX)

# set required compiler flags, apply checks, and compiler/CPU arch specific optimizations
# set required compiler flags and compiler/CPU arch specific optimizations
if(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
# Intel classic compilers version 19 are broken and fail to compile the embedded fmtlib
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 20.0)
message(ERROR "Intel classic compiler version ${CMAKE_CXX_COMPILER_VERSION} is too old")
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
if(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Qrestrict")
endif()
set(CMAKE_TUNE_DEFAULT "/QxHost")
if(CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 17.3 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 17.4)
set(CMAKE_TUNE_DEFAULT "/QxCOMMON-AVX512")
else()
set(CMAKE_TUNE_DEFAULT "/QxHost")
endif()
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -restrict")
set(CMAKE_TUNE_DEFAULT "-xHost -fp-model fast=2 -no-prec-div -qoverride-limits -diag-disable=10441 -diag-disable=11074 -diag-disable=11076 -diag-disable=2196")
if(CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 17.3 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 17.4)
set(CMAKE_TUNE_DEFAULT "-xCOMMON-AVX512")
else()
set(CMAKE_TUNE_DEFAULT "-xHost -fp-model fast=2 -no-prec-div -qoverride-limits -diag-disable=10441 -diag-disable=11074 -diag-disable=11076 -diag-disable=2196")
endif()
endif()
endif()

Expand Down
35 changes: 19 additions & 16 deletions doc/src/Developer_code_design.rst
Original file line number Diff line number Diff line change
Expand Up @@ -300,30 +300,33 @@ Formatting with the {fmt} library

The LAMMPS source code includes a copy of the `{fmt} library
<https://fmt.dev>`_, which is preferred over formatting with the
"printf()" family of functions. The primary reason is that it allows
a typesafe default format for any type of supported data. This is
"printf()" family of functions. The primary reason is that it allows a
typesafe default format for any type of supported data. This is
particularly useful for formatting integers of a given size (32-bit or
64-bit) which may require different format strings depending on
compile time settings or compilers/operating systems. Furthermore,
{fmt} gives better performance, has more functionality, a familiar
formatting syntax that has similarities to ``format()`` in Python, and
provides a facility that can be used to integrate format strings and a
variable number of arguments into custom functions in a much simpler
way than the varargs mechanism of the C library. Finally, {fmt} has
been included into the C++20 language standard, so changes to adopt it
are future-proof.
64-bit) which may require different format strings depending on compile
time settings or compilers/operating systems. Furthermore, {fmt} gives
better performance, has more functionality, a familiar formatting syntax
that has similarities to ``format()`` in Python, and provides a facility
that can be used to integrate format strings and a variable number of
arguments into custom functions in a much simpler way than the varargs
mechanism of the C library. Finally, {fmt} has been included into the
C++20 language standard as ``std::format()``, so changes to adopt it are
future-proof, for as long as they are not using any extensions that are
not (yet) included into C++.

Formatted strings are frequently created by calling the
``fmt::format()`` function, which will return a string as a
``std::string`` class instance. In contrast to the ``%`` placeholder in
``printf()``, the {fmt} library uses ``{}`` to embed format descriptors.
In the simplest case, no additional characters are needed, as {fmt} will
choose the default format based on the data type of the argument.
Otherwise, the ``fmt::print()`` function may be used instead of
``printf()`` or ``fprintf()``. In addition, several LAMMPS output
functions, that originally accepted a single string as argument have
been overloaded to accept a format string with optional arguments as
well (e.g., ``Error::all()``, ``Error::one()``, ``utils::logmesg()``).
Otherwise, the :cpp:func:`utils::print() <LAMMPS_NS::utils::print>`
function may be used instead of ``printf()`` or ``fprintf()``. In
addition, several LAMMPS output functions, that originally accepted a
single string as argument have been overloaded to accept a format string
with optional arguments as well (e.g., ``Error::all()``,
``Error::one()``, :cpp:func:`utils::logmesg()
<LAMMPS_NS::utils::logmesg>`).

Summary of the {fmt} format syntax
==================================
Expand Down
6 changes: 6 additions & 0 deletions doc/src/Developer_utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ Convenience functions
.. doxygenfunction:: logmesg(LAMMPS *lmp, const std::string &mesg)
:project: progguide

.. doxygenfunction:: print(FILE *fp, const std::string &format, Args&&... args)
:project: progguide

.. doxygenfunction:: print(FILE *fp, const std::string &mesg)
:project: progguide

.. doxygenfunction:: errorurl
:project: progguide

Expand Down
4 changes: 2 additions & 2 deletions doc/src/Developer_write_fix.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ Here the we specify which methods of the fix should be called during
MPI_Allreduce(localAvgVel, globalAvgVel, 4, MPI_DOUBLE, MPI_SUM, world);
scale3(1.0 / globalAvgVel[3], globalAvgVel);
if ((comm->me == 0) && screen) {
fmt::print(screen,"{}, {}, {}\n",
globalAvgVel[0], globalAvgVel[1], globalAvgVel[2]);
utils::print(screen, "{}, {}, {}\n",
globalAvgVel[0], globalAvgVel[1], globalAvgVel[2]);
}
}

Expand Down
1 change: 1 addition & 0 deletions doc/utils/sphinx-config/false_positives.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,7 @@ fp
fphi
fPIC
fplo
fprintf
Fqq
Fraige
framerate
Expand Down
4 changes: 2 additions & 2 deletions src/AMOEBA/fix_amoeba_pitorsion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,9 @@ bigint FixAmoebaPiTorsion::read_data_skip_lines(char *keyword)

void FixAmoebaPiTorsion::write_data_header(FILE *fp, int mth)
{
if (mth == 0) fmt::print(fp,"{} pitorsions\n",npitorsions);
if (mth == 0) utils::print(fp,"{} pitorsions\n",npitorsions);
else if (mth == 1)
fmt::print(fp, "{} pitorsion types\n",npitorsion_types);
utils::print(fp, "{} pitorsion types\n",npitorsion_types);
}

/* ----------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions src/BODY/body_nparticle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,22 +261,22 @@ int BodyNparticle::write_data_body(FILE *fp, double *buf)

// atomID ninteger ndouble

fmt::print(fp,"{} {} {}\n",ubuf(buf[m]).i,ubuf(buf[m+1]).i,ubuf(buf[m+2]).i);
utils::print(fp,"{} {} {}\n",ubuf(buf[m]).i,ubuf(buf[m+1]).i,ubuf(buf[m+2]).i);
m += 3;

const int nsub = (int) ubuf(buf[m++]).i;
fmt::print(fp,"{}\n",nsub);
utils::print(fp,"{}\n",nsub);

// inertia

fmt::print(fp,"{} {} {} {} {} {}\n",
utils::print(fp,"{} {} {} {} {} {}\n",
buf[m+0],buf[m+1],buf[m+2],buf[m+3],buf[m+4],buf[m+5]);
m += 6;

// nsub vertices

for (int i = 0; i < nsub; i++) {
fmt::print(fp,"{} {} {}\n",buf[m],buf[m+1],buf[m+2]);
utils::print(fp,"{} {} {}\n",buf[m],buf[m+1],buf[m+2]);
m += 3;
}

Expand Down
10 changes: 5 additions & 5 deletions src/BODY/body_rounded_polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,27 +398,27 @@ int BodyRoundedPolygon::write_data_body(FILE *fp, double *buf)

// atomID ninteger ndouble

fmt::print(fp,"{} {} {}\n",ubuf(buf[m]).i,ubuf(buf[m+1]).i,ubuf(buf[m+2]).i);
utils::print(fp,"{} {} {}\n",ubuf(buf[m]).i,ubuf(buf[m+1]).i,ubuf(buf[m+2]).i);
m += 3;

const int nsub = (int) ubuf(buf[m++]).i;
fmt::print(fp,"{}\n",nsub);
utils::print(fp,"{}\n",nsub);

// inertia

fmt::print(fp,"{} {} {} {} {} {}\n",
utils::print(fp,"{} {} {} {} {} {}\n",
buf[m+0],buf[m+1],buf[m+2],buf[m+3],buf[m+4],buf[m+5]);
m += 6;

// nsub vertices

for (int i = 0; i < nsub; i++, m+=3)
fmt::print(fp,"{} {} {}\n",buf[m],buf[m+1],buf[m+2]);
utils::print(fp,"{} {} {}\n",buf[m],buf[m+1],buf[m+2]);

// rounded diameter

double diameter = buf[m++];
fmt::print(fp,"{}\n",diameter);
utils::print(fp,"{}\n",diameter);

return m;
}
Expand Down
14 changes: 7 additions & 7 deletions src/BODY/body_rounded_polyhedron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,43 +476,43 @@ int BodyRoundedPolyhedron::write_data_body(FILE *fp, double *buf)

// atomID ninteger ndouble

fmt::print(fp,"{} {} {}\n",ubuf(buf[m]).i,ubuf(buf[m+1]).i,ubuf(buf[m+2]).i);
utils::print(fp,"{} {} {}\n",ubuf(buf[m]).i,ubuf(buf[m+1]).i,ubuf(buf[m+2]).i);
m += 3;

// nvert, nedge, nface

const int nsub = (int) ubuf(buf[m++]).i;
const int nedge = (int) ubuf(buf[m++]).i;
const int nface = (int) ubuf(buf[m++]).i;
fmt::print(fp,"{} {} {}\n",nsub,nedge,nface);
utils::print(fp,"{} {} {}\n",nsub,nedge,nface);

// inertia

fmt::print(fp,"{} {} {} {} {} {}\n",
utils::print(fp,"{} {} {} {} {} {}\n",
buf[m+0],buf[m+1],buf[m+2],buf[m+3],buf[m+4],buf[m+5]);
m += 6;

// nsub vertices

for (int i = 0; i < nsub; i++, m+=3)
fmt::print(fp,"{} {} {}\n",buf[m],buf[m+1],buf[m+2]);
utils::print(fp,"{} {} {}\n",buf[m],buf[m+1],buf[m+2]);

// nedge 2-tuples and nface 4-tuples
// unless nsub = 1 or 2

if (nsub > 2) {
for (int i = 0; i < nedge; i++, m+=2)
fmt::print(fp,"{} {}\n",static_cast<int> (buf[m]),static_cast<int> (buf[m+1]));
utils::print(fp,"{} {}\n",static_cast<int> (buf[m]),static_cast<int> (buf[m+1]));
for (int i = 0; i < nface; i++, m+=4)
fmt::print(fp,"{} {} {} {}\n",
utils::print(fp,"{} {} {} {}\n",
static_cast<int> (buf[m]),static_cast<int> (buf[m+1]),
static_cast<int> (buf[m+2]),static_cast<int> (buf[m+3]));
}

// rounded diameter

double diameter = buf[m++];
fmt::print(fp,"{}\n",diameter);
utils::print(fp,"{}\n",diameter);

return m;
}
Expand Down
21 changes: 14 additions & 7 deletions src/DPD-REACT/fix_rx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ FixRX::FixRX(LAMMPS *lmp, int narg, char **arg) :
+ " expected \"sparse\" or \"dense\"\n");

if (comm->me == 0 && Verbosity > 1)
error->message(FLERR, fmt::format("FixRX: matrix format is {}",word));
error->message(FLERR, fmt::format("FixRX: matrix format is {}", word));
}

// Determine the ODE solver/stepper strategy in arg[6].
Expand Down Expand Up @@ -157,7 +157,7 @@ FixRX::FixRX(LAMMPS *lmp, int narg, char **arg) :
minSteps = utils::inumeric(FLERR,arg[iarg++],false,lmp);

if (comm->me == 0 && Verbosity > 1)
error->message(FLERR,fmt::format("FixRX: RK4 numSteps= {}", minSteps));
error->message(FLERR, fmt::format("FixRX: RK4 numSteps= {}", minSteps));
} else if (odeIntegrationFlag == ODE_LAMMPS_RK4 && narg>8) {
error->all(FLERR,"Illegal fix rx command. Too many arguments for RK4 solver.");
} else if (odeIntegrationFlag == ODE_LAMMPS_RKF45) {
Expand Down Expand Up @@ -307,12 +307,19 @@ void FixRX::post_constructor()
id_fix_species = utils::strdup(std::string(id)+"_SPECIES");
id_fix_species_old = utils::strdup(std::string(id)+"_SPECIES_OLD");

const std::string fmtstr = "{} {} property/atom ";
auto newcmd1 = fmt::format(fmtstr,id_fix_species,group->names[igroup]);
auto newcmd2 = fmt::format(fmtstr,id_fix_species_old,group->names[igroup]);
std::string newcmd1 = id_fix_species;
newcmd1 += " ";
newcmd1 += group->names[igroup];
newcmd1 += " property/atom ";

std::string newcmd2 = id_fix_species_old;
newcmd2 += " ";
newcmd2 += group->names[igroup];
newcmd2 += " property/atom ";

for (int ii=0; ii<nspecies; ii++) {
newcmd1 += fmt::format(" d_{}",tmpspecies[ii]);
newcmd2 += fmt::format(" d_{}Old",tmpspecies[ii]);
newcmd1 += fmt::format(" d_{}", tmpspecies[ii]);
newcmd2 += fmt::format(" d_{}Old", tmpspecies[ii]);
}
newcmd1 += " ghost yes";
newcmd2 += " ghost yes";
Expand Down
4 changes: 2 additions & 2 deletions src/ELECTRODE/fix_electrode_conp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1363,10 +1363,10 @@ int FixElectrodeConp::setmask()
void FixElectrodeConp::write_to_file(FILE *file, const std::vector<tagint> &tags,
const std::vector<std::vector<double>> &mat)
{
for (const auto &t : tags) fmt::print(file, "{:20}", t);
for (const auto &t : tags) utils::print(file, "{:20}", t);
fputs("\n", file);
for (const auto &vec : mat) {
for (const auto &x : vec) fmt::print(file, "{:20.11e}", x);
for (const auto &x : vec) utils::print(file, "{:20.11e}", x);
fputs("\n", file);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/EXTRA-COMMAND/group2ndx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void Group2Ndx::write_group(FILE *fp, int gid)
if (gid == 0) {
fputs("[ System ]\n", fp);
} else {
fmt::print(fp, "[ {} ]\n", group->names[gid]);
utils::print(fp, "[ {} ]\n", group->names[gid]);
}
width = log10((double) atom->natoms) + 2;
cols = 80 / width;
Expand Down Expand Up @@ -142,7 +142,7 @@ void Group2Ndx::write_group(FILE *fp, int gid)
if (fp) {
int i, j;
for (i = 0, j = 0; i < gcount; ++i) {
fmt::print(fp, "{:>{}}", recvlist[i], width);
utils::print(fp, "{:>{}}", recvlist[i], width);
++j;
if (j == cols) {
fputs("\n", fp);
Expand Down
26 changes: 13 additions & 13 deletions src/EXTRA-DUMP/dump_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,31 +94,31 @@ void DumpYAML::write_header(bigint ndump)

if (comm->me == 0) {
const std::string boundary(boundstr);
fmt::print(fp, "---\ncreator: LAMMPS\ntimestep: {}\n", update->ntimestep);
if (unit_flag) fmt::print(fp, "units: {}\n", update->unit_style);
if (time_flag) fmt::print(fp, "time: {:.16g}\n", compute_time());
utils::print(fp, "---\ncreator: LAMMPS\ntimestep: {}\n", update->ntimestep);
if (unit_flag) utils::print(fp, "units: {}\n", update->unit_style);
if (time_flag) utils::print(fp, "time: {:.16g}\n", compute_time());

fmt::print(fp, "natoms: {}\n", ndump);
utils::print(fp, "natoms: {}\n", ndump);
fputs("boundary: [ ", fp);
for (const auto &bflag : boundary) {
if (bflag == ' ') continue;
fmt::print(fp, "{}, ", bflag);
utils::print(fp, "{}, ", bflag);
}
fputs("]\n", fp);

if (thermo) fmt::print(fp, thermo_data);
if (thermo) utils::print(fp, thermo_data);

fmt::print(fp, "box:\n - [ {}, {} ]\n", boxxlo, boxxhi);
fmt::print(fp, " - [ {}, {} ]\n", boxylo, boxyhi);
fmt::print(fp, " - [ {}, {} ]\n", boxzlo, boxzhi);
if (domain->triclinic) fmt::print(fp, " - [ {}, {}, {} ]\n", boxxy, boxxz, boxyz);
utils::print(fp, "box:\n - [ {}, {} ]\n", boxxlo, boxxhi);
utils::print(fp, " - [ {}, {} ]\n", boxylo, boxyhi);
utils::print(fp, " - [ {}, {} ]\n", boxzlo, boxzhi);
if (domain->triclinic) utils::print(fp, " - [ {}, {}, {} ]\n", boxxy, boxxz, boxyz);

fmt::print(fp, "keywords: [ ");
utils::print(fp, "keywords: [ ");
for (const auto &item : utils::split_words(columns)) {
if (item.find_first_of(special_chars) == std::string::npos)
fmt::print(fp, "{}, ", item);
utils::print(fp, "{}, ", item);
else
fmt::print(fp, "'{}', ", item);
utils::print(fp, "'{}', ", item);
}
fputs(" ]\ndata:\n", fp);
} else // reset so that the remainder of the output is not multi-proc
Expand Down
2 changes: 1 addition & 1 deletion src/EXTRA-FIX/fix_ave_correlate_long.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ void FixAveCorrelateLong::end_of_step()
if (fp && comm->me == 0) {
clearerr(fp);
if (overwrite) (void) platform::fseek(fp,filepos);
fmt::print(fp,"# Timestep: {}\n", ntimestep);
utils::print(fp,"# Timestep: {}\n", ntimestep);
for (unsigned int i=0; i < npcorr; ++i) {
fprintf(fp, "%lg ", t[i]*update->dt*nevery);
for (int j=0; j < npair; ++j) {
Expand Down
2 changes: 1 addition & 1 deletion src/EXTRA-FIX/fix_tmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ void FixTMD::initial_integrate(int /*vflag*/)
work_lambda += lambda*(rho_target - rho_old);
if (!(update->ntimestep % nfileevery) &&
(previous_stat != update->ntimestep)) {
fmt::print(fp, "{} {} {} {} {} {} {} {}\n", update->ntimestep,rho_target,rho_old,
utils::print(fp, "{} {} {} {} {} {} {} {}\n", update->ntimestep,rho_target,rho_old,
gamma_back,gamma_forward,lambda,work_lambda,work_analytical);
fflush(fp);
previous_stat = update->ntimestep;
Expand Down
2 changes: 1 addition & 1 deletion src/EXTRA-FIX/fix_ttm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ void FixTTM::write_electron_temperatures(const std::string &filename)
FILE *fp = fopen(filename.c_str(),"w");
if (!fp) error->one(FLERR,"Fix ttm could not open output file {}: {}",
filename,utils::getsyserror());
fmt::print(fp,"# DATE: {} UNITS: {} COMMENT: Electron temperature on "
utils::print(fp,"# DATE: {} UNITS: {} COMMENT: Electron temperature on "
"{}x{}x{} grid at step {} - created by fix {}\n", utils::current_date(),
update->unit_style, nxgrid, nygrid, nzgrid, update->ntimestep, style);

Expand Down
2 changes: 1 addition & 1 deletion src/EXTRA-FIX/fix_ttm_grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ void FixTTMGrid::write_restart_file(const char *file)
if (fpout == nullptr)
error->one(FLERR,"Cannot open fix ttm/grid restart file {}: {}",outfile,utils::getsyserror());

fmt::print(fpout,"# DATE: {} UNITS: {} COMMENT: "
utils::print(fpout,"# DATE: {} UNITS: {} COMMENT: "
"Electron temperature on {}x{}x{} grid at step {} - "
"created by fix {}\n",
utils::current_date(),update->unit_style,
Expand Down
Loading

0 comments on commit a0fcbc9

Please sign in to comment.