Skip to content

Commit

Permalink
core: Fix compiler warnings
Browse files Browse the repository at this point in the history
Fix type conversion warnings and Clang-Tidy diagnostics from Clang 14.
  • Loading branch information
jngrad committed May 27, 2022
1 parent 2ffd662 commit ba037e5
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 37 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ target_compile_options(
INTERFACE -Wall
-Wextra
$<$<BOOL:${WARNINGS_ARE_ERRORS}>:-Werror>
$<$<CXX_COMPILER_ID:Clang>:-Wno-unknown-cuda-version>
# add extra warnings
$<$<CXX_COMPILER_ID:Clang>:-Wextern-initializer>
$<$<CXX_COMPILER_ID:Clang>:-Wrange-loop-analysis>
Expand Down
26 changes: 15 additions & 11 deletions src/core/accumulators/Correlator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ void Correlator::initialize() {
compressB_name + "' for second observable");
}

using index_type = decltype(result)::index;

A.resize(std::array<int, 2>{{m_hierarchy_depth, m_tau_lin + 1}});
std::fill_n(A.data(), A.num_elements(), std::vector<double>(dim_A, 0));
B.resize(std::array<int, 2>{{m_hierarchy_depth, m_tau_lin + 1}});
Expand All @@ -287,11 +289,9 @@ void Correlator::initialize() {
n_vals = std::vector<long>(m_hierarchy_depth, 0);

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

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;
for (index_type i = 0; i < static_cast<index_type>(n_result); i++) {
for (index_type j = 0; j < static_cast<index_type>(m_dim_corr); j++) {
result[i][j] = 0.;
}
}

Expand Down Expand Up @@ -377,6 +377,7 @@ void Correlator::update() {
B_accumulated_average[k] += B[0][newest[0]][k];
}

using index_type = decltype(result)::index;
// Now update the lowest level correlation estimates
for (long j = 0; j < min(m_tau_lin + 1, n_vals[0]); j++) {
auto const index_new = newest[0];
Expand All @@ -386,7 +387,7 @@ void Correlator::update() {
assert(temp.size() == m_dim_corr);

n_sweeps[j]++;
for (std::size_t k = 0; k < m_dim_corr; k++) {
for (index_type k = 0; k < static_cast<index_type>(m_dim_corr); k++) {
result[j][k] += temp[k];
}
}
Expand All @@ -403,14 +404,15 @@ void Correlator::update() {
assert(temp.size() == m_dim_corr);

n_sweeps[index_res]++;
for (std::size_t k = 0; k < m_dim_corr; k++) {
for (index_type k = 0; k < static_cast<index_type>(m_dim_corr); k++) {
result[index_res][k] += temp[k];
}
}
}
}

int Correlator::finalize() {
using index_type = decltype(result)::index;
if (finalized) {
throw std::runtime_error("Correlator::finalize() can only be called once.");
}
Expand Down Expand Up @@ -479,7 +481,7 @@ int Correlator::finalize() {
assert(temp.size() == m_dim_corr);

n_sweeps[index_res]++;
for (std::size_t k = 0; k < m_dim_corr; k++) {
for (index_type k = 0; k < static_cast<index_type>(m_dim_corr); k++) {
result[index_res][k] += temp[k];
}
}
Expand All @@ -490,14 +492,16 @@ int Correlator::finalize() {
}

std::vector<double> Correlator::get_correlation() {
using index_type = decltype(result)::index;
auto const n_result = n_values();
std::vector<double> res(n_result * m_dim_corr);

for (std::size_t i = 0; i < n_result; i++) {
auto const index = m_dim_corr * i;
for (std::size_t k = 0; k < m_dim_corr; k++) {
auto const index = static_cast<index_type>(m_dim_corr * i);
for (index_type k = 0; k < static_cast<index_type>(m_dim_corr); k++) {
if (n_sweeps[i]) {
res[index + k] = result[i][k] / static_cast<double>(n_sweeps[i]);
res[index + k] = result[static_cast<index_type>(i)][k] /
static_cast<double>(n_sweeps[i]);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/core/cell_system/AtomDecomposition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
*
* For a more detailed discussion please see @cite plimpton95a.
*/
// NOLINTNEXTLINE(bugprone-exception-escape)
class AtomDecomposition : public ParticleDecomposition {
boost::mpi::communicator comm;
std::vector<Cell> cells;
Expand Down
2 changes: 2 additions & 0 deletions src/core/cell_system/CellStructure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ struct Distance {
Utils::Vector3d vec21;
double dist2;
};

namespace detail {
// NOLINTNEXTLINE(bugprone-exception-escape)
struct MinimalImageDistance {
const BoxGeometry box;

Expand Down
1 change: 1 addition & 0 deletions src/core/cell_system/RegularDecomposition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
* communication! For single sided ghost communication one would need
* some ghost-ghost cell interaction as well, which we do not need!
*/
// NOLINTNEXTLINE(bugprone-exception-escape)
struct RegularDecomposition : public ParticleDecomposition {
/** Grid dimensions per node. */
Utils::Vector3i cell_grid = {};
Expand Down
1 change: 1 addition & 0 deletions src/core/cells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ get_pairs_of_types(double const distance, std::vector<int> const &types) {
detail::search_distance_sanity_check(distance);
auto pairs = get_pairs_filtered(distance, [types](Particle const &p) {
return std::any_of(types.begin(), types.end(),
// NOLINTNEXTLINE(bugprone-exception-escape)
[p](int const type) { return p.type() == type; });
});
Utils::Mpi::gather_buffer(pairs, comm_cart);
Expand Down
8 changes: 5 additions & 3 deletions src/core/cuda_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,18 @@ static void add_forces_and_torques(ParticleRange particles,
void cuda_mpi_send_forces(const ParticleRange &particles,
Utils::Span<float> host_forces,
Utils::Span<float> host_torques) {
auto const n_elements = 3 * particles.size();

auto const size = 3 * particles.size();
auto const n_elements = static_cast<int>(size);

if (this_node > 0) {
static std::vector<float> buffer_forces;
static std::vector<float> buffer_torques;

buffer_forces.resize(n_elements);
buffer_forces.resize(size);
Utils::Mpi::scatter_buffer(buffer_forces.data(), n_elements, comm_cart);
#ifdef ROTATION
buffer_torques.resize(n_elements);
buffer_torques.resize(size);
Utils::Mpi::scatter_buffer(buffer_torques.data(), n_elements, comm_cart);
#endif
add_forces_and_torques(particles, buffer_forces, buffer_torques);
Expand Down
1 change: 1 addition & 0 deletions src/core/energy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ static std::shared_ptr<Observable_stat> calculate_energy_local() {

obs_energy.mpi_reduce();
return obs_energy_ptr;
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
}

REGISTER_CALLBACK_MAIN_RANK(calculate_energy_local)
Expand Down
8 changes: 4 additions & 4 deletions src/core/grid_based_algorithms/electrokinetics_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -3612,10 +3612,10 @@ void ek_print_lbpar() {
printf(" float agrid = %f;\n", lbpar_gpu.agrid);
printf(" float tau = %f;\n", lbpar_gpu.tau);
printf(" float bulk_viscosity = %f;\n", lbpar_gpu.bulk_viscosity);
printf(" unsigned int dim_x = %d;\n", lbpar_gpu.dim[0]);
printf(" unsigned int dim_y = %d;\n", lbpar_gpu.dim[1]);
printf(" unsigned int dim_z = %d;\n", lbpar_gpu.dim[2]);
printf(" unsigned int number_of_nodes = %d;\n", lbpar_gpu.number_of_nodes);
printf(" unsigned int dim_x = %u;\n", lbpar_gpu.dim[0]);
printf(" unsigned int dim_y = %u;\n", lbpar_gpu.dim[1]);
printf(" unsigned int dim_z = %u;\n", lbpar_gpu.dim[2]);
printf(" unsigned int number_of_nodes = %u;\n", lbpar_gpu.number_of_nodes);
printf(" bool external_force_density = %d;\n",
static_cast<int>(lbpar_gpu.external_force_density));
printf(" float ext_force_density[3] = {%f, %f, %f};\n",
Expand Down
21 changes: 9 additions & 12 deletions src/core/grid_based_algorithms/lb_boundaries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ void lb_init_boundaries() {
std::vector<int> host_boundary_index_list;
std::size_t size_of_index;

for (int z = 0; z < int(lbpar_gpu.dim[2]); z++) {
for (int y = 0; y < int(lbpar_gpu.dim[1]); y++) {
for (int x = 0; x < int(lbpar_gpu.dim[0]); x++) {
for (unsigned z = 0; z < lbpar_gpu.dim[2]; z++) {
for (unsigned y = 0; y < lbpar_gpu.dim[1]; y++) {
for (unsigned x = 0; x < lbpar_gpu.dim[0]; x++) {
auto const pos = static_cast<double>(lbpar_gpu.agrid) *
(Utils::Vector3d{1. * x, 1. * y, 1. * z} +
Utils::Vector3d::broadcast(0.5));
Expand All @@ -198,12 +198,10 @@ void lb_init_boundaries() {
host_boundary_node_list.resize(size_of_index);
host_boundary_index_list.resize(size_of_index);
host_boundary_node_list[number_of_boundnodes] =
x + lbpar_gpu.dim[0] * y +
lbpar_gpu.dim[0] * lbpar_gpu.dim[1] * z;
auto const boundary_number =
std::distance(lbboundaries.begin(), boundary.base()) - 1;
host_boundary_index_list[number_of_boundnodes] =
boundary_number + 1;
static_cast<int>(x + lbpar_gpu.dim[0] * y +
lbpar_gpu.dim[0] * lbpar_gpu.dim[1] * z);
host_boundary_index_list[number_of_boundnodes] = static_cast<int>(
std::distance(lbboundaries.begin(), boundary.base()));
number_of_boundnodes++;
}
}
Expand Down Expand Up @@ -257,10 +255,9 @@ void lb_init_boundaries() {
[&pos](auto const lbb) { return lbb->shape().is_inside(pos); });
auto const index = get_linear_index(x, y, z, lblattice.halo_grid);
if (boundary != boost::rend(lbboundaries)) {
auto const boundary_number =
std::distance(lbboundaries.begin(), boundary.base()) - 1;
auto &node = lbfields[index];
node.boundary = static_cast<int>(boundary_number) + 1;
node.boundary = static_cast<int>(
std::distance(lbboundaries.begin(), boundary.base()));
node.slip_velocity = (*boundary)->velocity() * vel_conv;
} else {
lbfields[index].boundary = 0;
Expand Down
7 changes: 4 additions & 3 deletions src/core/io/writer/h5md_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ void File::write(const ParticleRange &particles, double time, int step,
datasets["particles/atoms/lees_edwards/normal/value"]);
}

int const n_part_local = particles.size();
auto const n_part_local = static_cast<int>(particles.size());
// calculate count and offset
int prefix = 0;
// calculate prefix for write of the current process
Expand Down Expand Up @@ -438,10 +438,11 @@ void File::write(const ParticleRange &particles, double time, int step,
}

void File::write_connectivity(const ParticleRange &particles) {
using index_type = MultiArray3i::index;
MultiArray3i bond(boost::extents[0][0][0]);
int particle_index = 0;
for (auto const &p : particles) {
int nbonds_local = bond.shape()[1];
auto nbonds_local = static_cast<index_type>(bond.shape()[1]);
for (auto const b : p.bonds()) {
auto const partner_ids = b.partner_ids();
if (partner_ids.size() == 1) {
Expand All @@ -454,7 +455,7 @@ void File::write_connectivity(const ParticleRange &particles) {
particle_index++;
}

int n_bonds_local = bond.shape()[1];
auto const n_bonds_local = static_cast<int>(bond.shape()[1]);
int prefix_bonds = 0;
BOOST_MPI_CHECK_RESULT(
MPI_Exscan, (&n_bonds_local, &prefix_bonds, 1, MPI_INT, MPI_SUM, m_comm));
Expand Down
1 change: 1 addition & 0 deletions src/script_interface/GlobalContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class GlobalContext : public Context {
std::shared_ptr<LocalContext> node_local_context)
: m_local_objects(), m_node_local_context(std::move(node_local_context)),
m_is_head_node(callbacks.comm().rank() == 0),
// NOLINTNEXTLINE(bugprone-throw-keyword-missing)
m_parallel_exception_handler(callbacks.comm()),
cb_make_handle(&callbacks,
[this](ObjectId id, const std::string &name,
Expand Down
1 change: 1 addition & 0 deletions src/script_interface/LocalContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class LocalContext : public Context {
LocalContext(Utils::Factory<ObjectHandle> factory,
boost::mpi::communicator const &comm)
: m_factory(std::move(factory)), m_is_head_node(comm.rank() == 0),
// NOLINTNEXTLINE(bugprone-throw-keyword-missing)
m_parallel_exception_handler(comm) {}

const Utils::Factory<ObjectHandle> &factory() const { return m_factory; }
Expand Down
4 changes: 2 additions & 2 deletions src/shapes/src/Rhomboid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void Rhomboid::calculate_dist(const Utils::Vector3d &pos, double &dist,
auto const A = a * d;
auto const B = b * d;
auto const C = c * d;
if (op1(A, 0) & op2(B, 0) & op3(C, 0)) {
if (op1(A, 0) and op2(B, 0) and op3(C, 0)) {
vec = d;
dist = m_direction * vec.norm();
return true;
Expand Down Expand Up @@ -92,7 +92,7 @@ void Rhomboid::calculate_dist(const Utils::Vector3d &pos, double &dist,
Utils::Vector3d const &edge) {
auto const A = (d * axis1) / dir1_dot_axis1;
auto const B = (d * axis2) / dir2_dot_axis2;
if (op1(A, 0) & op2(B, 0)) {
if (op1(A, 0) and op2(B, 0)) {
auto const tmp = (d * edge) / edge.norm2();
vec = d - edge * tmp;
dist = m_direction * vec.norm();
Expand Down
7 changes: 5 additions & 2 deletions src/utils/tests/serialization_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ auto to_vector(std::stringstream &buffer_in) {
std::vector<std::stringstream::char_type> buffer_out;
buffer_in.seekg(0);
while (!buffer_in.eof()) {
buffer_out.push_back(buffer_in.get());
std::stringstream::char_type c = '\0';
buffer_in.get(c);
buffer_out.push_back(c);
}
return buffer_out;
}
Expand Down Expand Up @@ -241,7 +243,8 @@ BOOST_AUTO_TEST_CASE(serialization_level_test) {
for (std::size_t i = 0; i < Testing::N; ++i) {
auto constexpr N = sizeof(Testing::T);
auto const offset = sizeof(std::size_t) + i * N;
auto const array_data = sorted_view<N>(buffer.begin() + offset);
auto const array_data =
sorted_view<N>(buffer.begin() + static_cast<long>(offset));
auto const array_data_ref = std::array<Testing::Serial_T, N>{
{static_cast<Testing::Serial_T>(Testing::values[i])}};
BOOST_TEST(array_data == array_data_ref, boost::test_tools::per_element());
Expand Down

0 comments on commit ba037e5

Please sign in to comment.