Skip to content

Commit

Permalink
Improve code portability (#4314)
Browse files Browse the repository at this point in the history
Description of changes:
- include what you use (#3977)
- use the namespaced version of `std::size_t` and `std::ptrdiff_t` (#4313)
- remove C-style struct typedefs: they are useless in C++, trigger a warning on Clang 11 and later versions, and will break in C++20 for structs containing static members or lambdas ([cppreference.com:  typedef-name for linkage purposes](https://en.cppreference.com/w/cpp/language/typedef#typedef-name_for_linkage_purposes))
  • Loading branch information
jngrad authored Aug 2, 2021
2 parents 1d27884 + ed4395f commit a0a3165
Show file tree
Hide file tree
Showing 204 changed files with 793 additions and 591 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ if(WITH_STOKESIAN_DYNAMICS)
FetchContent_Declare(
stokesian_dynamics
GIT_REPOSITORY https://github.com/hmenke/espresso-stokesian-dynamics.git
GIT_TAG c14e57655e929)
GIT_TAG 862a7537a366f0c32f0c25e46bd107bea590faea)
FetchContent_GetProperties(stokesian_dynamics)
set(STOKESIAN_DYNAMICS 1)
if(NOT stokesian_dynamics_POPULATED)
Expand Down Expand Up @@ -348,6 +348,10 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "8.0.0")
target_compile_options(cxx_interface INTERFACE -Wimplicit-float-conversion)
endif()
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang"
AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "11.0.0")
target_compile_options(cxx_interface INTERFACE -Wnon-c-typedef-for-linkage)
endif()
endif()

if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_VERSION
Expand Down
3 changes: 2 additions & 1 deletion src/core/AtomDecomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ GhostCommunicator AtomDecomposition::prepare_comm() {
return GhostCommunicator{comm, 0};
}

auto ghost_comm = GhostCommunicator{comm, static_cast<size_t>(comm.size())};
auto ghost_comm =
GhostCommunicator{comm, static_cast<std::size_t>(comm.size())};
/* every node has its dedicated comm step */
for (int n = 0; n < comm.size(); n++) {
ghost_comm.communications[n].part_lists.resize(1);
Expand Down
9 changes: 6 additions & 3 deletions src/core/BondList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
#include <boost/serialization/array.hpp>
#include <boost/version.hpp>

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <memory>
#include <type_traits>

/**
Expand Down Expand Up @@ -104,7 +107,7 @@ class BondList {
friend boost::serialization::access;
template <class Archive> void serialize(Archive &ar, long int /* version */) {
if (Archive::is_loading::value) {
size_t size{};
std::size_t size{};
ar &size;
m_storage.resize(size);
}
Expand Down Expand Up @@ -146,8 +149,8 @@ class BondList {
using value_type = BondView;
using reference = std::add_lvalue_reference_t<BondView>;
using const_reference = std::add_const_t<reference>;
using size_type = size_t;
using difference_type = ptrdiff_t;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using iterator = Iterator;
using const_iterator = Iterator;

Expand Down
2 changes: 2 additions & 0 deletions src/core/BoxGeometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <bitset>
#include <cassert>
#include <cmath>
#include <limits>
#include <utility>

namespace detail {
/**
Expand Down
2 changes: 2 additions & 0 deletions src/core/Cell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

#include <boost/range/iterator_range.hpp>

#include <algorithm>
#include <functional>
#include <utility>
#include <vector>

template <class CellRef> class Neighbors {
Expand Down
6 changes: 6 additions & 0 deletions src/core/CellStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@

#include <utils/contains.hpp>

#include <algorithm>
#include <iterator>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>

void CellStructure::check_particle_index() {
auto const max_id = get_max_local_particle_id();
Expand Down
4 changes: 4 additions & 0 deletions src/core/CellStructure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
#include <boost/range/algorithm/find_if.hpp>
#include <boost/range/algorithm/transform.hpp>

#include <algorithm>
#include <cassert>
#include <memory>
#include <utility>
#include <vector>

/** Cell Structure */
Expand Down
6 changes: 4 additions & 2 deletions src/core/CudaDeviceAllocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ template <class T> struct CudaDeviceAllocator {
return false;
}

pointer allocate(const size_t n) const { return thrust::device_malloc<T>(n); }
void deallocate(pointer p, size_t) const noexcept {
pointer allocate(const std::size_t n) const {
return thrust::device_malloc<T>(n);
}
void deallocate(pointer p, std::size_t) const noexcept {
try {
thrust::device_free(p);
} catch (thrust::system::system_error const &) {
Expand Down
5 changes: 3 additions & 2 deletions src/core/DomainDecomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <functional>
#include <iterator>
#include <utility>
#include <vector>

/** Returns pointer to the cell which corresponds to the position if the
* position is in the nodes spatial domain otherwise a nullptr pointer.
Expand Down Expand Up @@ -210,7 +211,7 @@ void DomainDecomposition::resort(bool global,
exchange_neighbors(displaced_parts, diff);

auto left_over = boost::mpi::all_reduce(m_comm, displaced_parts.size(),
std::plus<size_t>());
std::plus<std::size_t>());

if (left_over == 0) {
break;
Expand Down Expand Up @@ -474,7 +475,7 @@ GhostCommunicator DomainDecomposition::prepare_comm() {
auto const node_neighbors = Utils::Mpi::cart_neighbors<3>(m_comm);

/* calculate number of communications */
size_t num = 0;
std::size_t num = 0;
for (dir = 0; dir < 3; dir++) {
for (lr = 0; lr < 2; lr++) {
/* No communication for border of non periodic direction */
Expand Down
2 changes: 2 additions & 0 deletions src/core/MpiCallbacks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@
#include <boost/optional.hpp>
#include <boost/range/algorithm/remove_if.hpp>

#include <cassert>
#include <functional>
#include <initializer_list>
#include <memory>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>

namespace Communication {

Expand Down
9 changes: 6 additions & 3 deletions src/core/Observable_stat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@

#include <utils/Span.hpp>

#include <cassert>
#include <cstddef>
#include <vector>

Observable_stat::Observable_stat(size_t chunk_size) : m_chunk_size(chunk_size) {
Observable_stat::Observable_stat(std::size_t chunk_size)
: m_chunk_size(chunk_size) {
// number of chunks for different interaction types
auto constexpr n_coulomb = 2;
auto constexpr n_dipolar = 2;
Expand All @@ -40,8 +43,8 @@ Observable_stat::Observable_stat(size_t chunk_size) : m_chunk_size(chunk_size) {
#endif
auto const n_bonded = bonded_ia_params.size();
auto const n_non_bonded = max_non_bonded_pairs();
constexpr size_t n_ext_fields = 1; // energies from all fields: accumulated
constexpr size_t n_kinetic = 1; // linear+angular kinetic energy: accumulated
constexpr std::size_t n_ext_fields = 1; // reduction over all fields
constexpr std::size_t n_kinetic = 1; // linear+angular kinetic contributions

// resize vector
auto const total = n_kinetic + n_bonded + 2 * n_non_bonded + n_coulomb +
Expand Down
10 changes: 5 additions & 5 deletions src/core/Observable_stat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class Observable_stat {
/** Array for observables on each node. */
std::vector<double> m_data;
/** Number of doubles per data item */
size_t m_chunk_size;
std::size_t m_chunk_size;

/** Calculate the maximal number of non-bonded interaction pairs in the
* system.
*/
static size_t max_non_bonded_pairs() {
static std::size_t max_non_bonded_pairs() {
extern int max_seen_particle_type;
return static_cast<size_t>(
return static_cast<std::size_t>(
(max_seen_particle_type * (max_seen_particle_type + 1)) / 2);
}

Expand All @@ -57,7 +57,7 @@ class Observable_stat {
}

public:
explicit Observable_stat(size_t chunk_size);
explicit Observable_stat(std::size_t chunk_size);

auto chunk_size() const { return m_chunk_size; }
Utils::Span<double> data_() { return {m_data.data(), m_data.size()}; }
Expand All @@ -70,7 +70,7 @@ class Observable_stat {
* @param column Which column to sum up (only relevant for multi-dimensional
* observables).
*/
double accumulate(double acc = 0.0, size_t column = 0) const {
double accumulate(double acc = 0.0, std::size_t column = 0) const {
assert(column < m_chunk_size);
if (m_chunk_size == 1)
return boost::accumulate(m_data, acc);
Expand Down
2 changes: 1 addition & 1 deletion src/core/PartCfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void PartCfg::update() {
auto const ids = get_particle_ids();
auto const chunk_size = fetch_cache_max_size();

for (size_t offset = 0; offset < ids.size();) {
for (std::size_t offset = 0; offset < ids.size();) {
auto const this_size =
boost::algorithm::clamp(chunk_size, 0, ids.size() - offset);
auto const chunk_ids =
Expand Down
2 changes: 1 addition & 1 deletion src/core/PartCfg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class PartCfg {
public:
/** Number of particles in the config.
*/
size_t size() {
std::size_t size() {
if (!m_valid)
update();

Expand Down
2 changes: 2 additions & 0 deletions src/core/Particle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <boost/serialization/vector.hpp>

#include <cstdint>
#include <stdexcept>
#include <vector>

enum : uint8_t {
ROTATION_FIXED = 0u,
Expand Down
3 changes: 3 additions & 0 deletions src/core/ParticleIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
#define CORE_PARTICLE_ITERATOR_HPP

#include <boost/iterator/iterator_facade.hpp>

#include <cassert>
#include <iterator>
#include <utility>

namespace detail {
/* Detect the particle iterator type for a given cell iterator type. */
Expand Down
2 changes: 2 additions & 0 deletions src/core/RuntimeErrorCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using boost::mpi::all_reduce;
using boost::mpi::communicator;
Expand Down
2 changes: 2 additions & 0 deletions src/core/RuntimeErrorStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "RuntimeErrorStream.hpp"

#include "RuntimeErrorCollector.hpp"

#include <string>
#include <utility>

namespace ErrorHandling {
Expand Down
2 changes: 1 addition & 1 deletion src/core/accumulators/AccumulatorBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AccumulatorBase {

virtual void update() = 0;
/** Dimensions needed to reshape the flat array returned by the accumulator */
virtual std::vector<size_t> shape() const = 0;
virtual std::vector<std::size_t> shape() const = 0;

private:
// Number of timesteps between automatic updates.
Expand Down
28 changes: 15 additions & 13 deletions src/core/accumulators/Correlator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include <numeric>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

namespace {
int min(int i, unsigned int j) { return std::min(i, static_cast<int>(j)); }
Expand Down Expand Up @@ -149,7 +151,7 @@ std::vector<double> fcs_acf(std::vector<double> const &A,

std::vector<double> C(C_size, 0);

for (size_t i = 0; i < C_size; i++) {
for (std::size_t i = 0; i < C_size; i++) {
for (int j = 0; j < 3; j++) {
auto const &a = A[3 * i + j];
auto const &b = B[3 * i + j];
Expand Down Expand Up @@ -281,19 +283,19 @@ void Correlator::initialize() {
B_accumulated_average = std::vector<double>(dim_B, 0);

auto const n_result = n_values();
n_sweeps = std::vector<size_t>(n_result, 0);
n_sweeps = std::vector<std::size_t>(n_result, 0);
n_vals = std::vector<unsigned int>(m_hierarchy_depth, 0);

result.resize(std::array<size_t, 2>{{n_result, m_dim_corr}});
result.resize(std::array<std::size_t, 2>{{n_result, m_dim_corr}});

for (size_t i = 0; i < n_result; i++) {
for (size_t j = 0; j < m_dim_corr; j++) {
for (std::size_t i = 0; i < n_result; i++) {
for (std::size_t j = 0; j < m_dim_corr; j++) {
// and initialize the values
result[i][j] = 0;
}
}

newest = std::vector<size_t>(m_hierarchy_depth, m_tau_lin);
newest = std::vector<std::size_t>(m_hierarchy_depth, m_tau_lin);

tau.resize(n_result);
for (int i = 0; i < m_tau_lin + 1; i++) {
Expand Down Expand Up @@ -363,11 +365,11 @@ void Correlator::update() {

// Now we update the cumulated averages and variances of A and B
n_data++;
for (size_t k = 0; k < dim_A; k++) {
for (std::size_t k = 0; k < dim_A; k++) {
A_accumulated_average[k] += A[0][newest[0]][k];
}

for (size_t k = 0; k < dim_B; k++) {
for (std::size_t k = 0; k < dim_B; k++) {
B_accumulated_average[k] += B[0][newest[0]][k];
}

Expand All @@ -380,7 +382,7 @@ void Correlator::update() {
assert(temp.size() == m_dim_corr);

n_sweeps[j]++;
for (size_t k = 0; k < m_dim_corr; k++) {
for (std::size_t k = 0; k < m_dim_corr; k++) {
result[j][k] += temp[k];
}
}
Expand All @@ -397,7 +399,7 @@ void Correlator::update() {
assert(temp.size() == m_dim_corr);

n_sweeps[index_res]++;
for (size_t k = 0; k < m_dim_corr; k++) {
for (std::size_t k = 0; k < m_dim_corr; k++) {
result[index_res][k] += temp[k];
}
}
Expand Down Expand Up @@ -478,7 +480,7 @@ int Correlator::finalize() {
assert(temp.size() == m_dim_corr);

n_sweeps[index_res]++;
for (size_t k = 0; k < m_dim_corr; k++) {
for (std::size_t k = 0; k < m_dim_corr; k++) {
result[index_res][k] += temp[k];
}
}
Expand All @@ -492,9 +494,9 @@ std::vector<double> Correlator::get_correlation() {
auto const n_result = n_values();
std::vector<double> res(n_result * m_dim_corr);

for (size_t i = 0; i < n_result; i++) {
for (std::size_t i = 0; i < n_result; i++) {
auto const index = m_dim_corr * i;
for (size_t k = 0; k < m_dim_corr; k++) {
for (std::size_t k = 0; k < m_dim_corr; k++) {
if (n_sweeps[i]) {
res[index + k] = result[i][k] / static_cast<double>(n_sweeps[i]);
}
Expand Down
Loading

0 comments on commit a0a3165

Please sign in to comment.