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

Fix Sonarqube warnings in examples/benchmarks #569

Merged
merged 1 commit into from
Jun 16, 2020
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
34 changes: 19 additions & 15 deletions benchmark/matrix_statistics/matrix_statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,21 @@ void compute_summary(const std::vector<gko::size_type> &dist,
// clang-format on

add_or_set_member(out, "min", dist[0], allocator);
add_or_set_member(out, "q1",
coefs[r][0] * dist[positions[r][0]] +
coefs[r][1] * dist[positions[r][1]],
allocator);
add_or_set_member(out, "median",
coefs[r][2] * dist[positions[r][2]] +
coefs[r][3] * dist[positions[r][3]],
allocator);
add_or_set_member(out, "q3",
coefs[r][4] * dist[positions[r][4]] +
coefs[r][5] * dist[positions[r][5]],
allocator);
add_or_set_member(
out, "q1",
coefs[r][0] * static_cast<double>(dist[positions[r][0]]) +
coefs[r][1] * static_cast<double>(dist[positions[r][1]]),
allocator);
add_or_set_member(
out, "median",
coefs[r][2] * static_cast<double>(dist[positions[r][2]]) +
coefs[r][3] * static_cast<double>(dist[positions[r][3]]),
allocator);
add_or_set_member(
out, "q3",
coefs[r][4] * static_cast<double>(dist[positions[r][4]]) +
coefs[r][5] * static_cast<double>(dist[positions[r][5]]),
allocator);
add_or_set_member(out, "max", dist[dist.size() - 1], allocator);
}

Expand All @@ -94,11 +97,12 @@ double compute_moment(int degree, const std::vector<gko::size_type> &dist,
if (normalization == 0.0) {
return 0.0;
}
auto moment = 0.0;
double moment = 0.0;
for (const auto &x : dist) {
moment += std::pow(x - center, degree);
moment += std::pow(static_cast<double>(x) - center, degree);
}
return moment / dist.size() / std::pow(normalization, degree);
return moment / static_cast<double>(dist.size()) /
std::pow(normalization, static_cast<double>(degree));
}


Expand Down
9 changes: 6 additions & 3 deletions examples/ginkgo-overhead/ginkgo-overhead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ int main(int argc, char *argv[])
auto time = std::chrono::duration_cast<std::chrono::nanoseconds>(tac - tic);
std::cout << "Running " << num_iters
<< " iterations of the CG solver took a total of "
<< 1.0 * time.count() / std::nano::den << " seconds." << std::endl
<< static_cast<double>(time.count()) /
static_cast<double>(std::nano::den)
<< " seconds." << std::endl
<< "\tAverage library overhead: "
<< 1.0 * time.count() / num_iters << " [nanoseconds / iteration]"
<< std::endl;
<< static_cast<double>(time.count()) /
static_cast<double>(num_iters)
<< " [nanoseconds / iteration]" << std::endl;
}
11 changes: 7 additions & 4 deletions examples/nine-pt-stencil-solver/nine-pt-stencil-solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,17 @@ int main(int argc, char *argv[])
values.data(), rhs.data(), u.data(), reduction_factor);
auto stop_time = std::chrono::steady_clock::now();
auto runtime_duration =
std::chrono::duration_cast<std::chrono::nanoseconds>(stop_time -
start_time)
.count() *
static_cast<double>(
std::chrono::duration_cast<std::chrono::nanoseconds>(stop_time -
start_time)
.count()) *
1e-6;

print_solution(dp, u.data());
std::cout << "The average relative error is "
<< calculate_error(dp, u.data(), correct_u) / dp_2 << std::endl;
<< calculate_error(dp, u.data(), correct_u) /
static_cast<gko::remove_complex<ValueType>>(dp_2)
<< std::endl;
std::cout << "The runtime is " << std::to_string(runtime_duration) << " ms"
<< std::endl;
}
11 changes: 6 additions & 5 deletions examples/poisson-solver/poisson-solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ void generate_rhs(Closure f, ValueType u0, ValueType u1,
{
const auto discretization_points = rhs->get_size()[0];
auto values = rhs->get_values();
const ValueType h = 1.0 / (discretization_points + 1);
const ValueType h = 1.0 / static_cast<ValueType>(discretization_points + 1);
for (gko::size_type i = 0; i < discretization_points; ++i) {
const auto xi = ValueType(i + 1) * h;
tcojean marked this conversation as resolved.
Show resolved Hide resolved
const auto xi = static_cast<ValueType>(i + 1) * h;
values[i] = -f(xi) * h * h;
}
values[0] += u0;
Expand Down Expand Up @@ -99,11 +99,11 @@ gko::remove_complex<ValueType> calculate_error(
int discretization_points, const gko::matrix::Dense<ValueType> *u,
Closure correct_u)
{
const ValueType h = 1.0 / (discretization_points + 1);
const ValueType h = 1.0 / static_cast<ValueType>(discretization_points + 1);
auto error = 0.0;
for (int i = 0; i < discretization_points; ++i) {
using std::abs;
const auto xi = ValueType(i + 1) * h;
const auto xi = static_cast<ValueType>(i + 1) * h;
error +=
abs(u->get_const_values()[i] - correct_u(xi)) / abs(correct_u(xi));
}
Expand Down Expand Up @@ -180,6 +180,7 @@ int main(int argc, char *argv[])
print_solution<ValueType>(u0, u1, lend(u));
std::cout << "The average relative error is "
<< calculate_error(discretization_points, lend(u), correct_u) /
discretization_points
static_cast<gko::remove_complex<ValueType>>(
discretization_points)
<< std::endl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,17 @@ int main(int argc, char *argv[])
auto stop_time = std::chrono::steady_clock::now();

const auto runtime_duration =
std::chrono::duration_cast<std::chrono::nanoseconds>(stop_time -
start_time)
.count() *
static_cast<double>(
std::chrono::duration_cast<std::chrono::nanoseconds>(stop_time -
start_time)
.count()) *
1e-6;

print_solution<ValueType, IndexType>(dp, u.data());
std::cout << "The average relative error is "
<< calculate_error(dp, u.data(), correct_u) / dp_3 << std::endl;
<< calculate_error(dp, u.data(), correct_u) /
static_cast<gko::remove_complex<ValueType>>(dp_3)
<< std::endl;

std::cout << "The runtime is " << std::to_string(runtime_duration) << " ms"
<< std::endl;
Expand Down