Skip to content

Commit

Permalink
Mult result conversion (#2405)
Browse files Browse the repository at this point in the history
* CodeQL: trying to fix issues with multiplication results conversion

This avoids potential overflow when low precision data is multiplied
and then store in higher precision variable: size_t = int * int
Focusing on issues in the library for now, unit-tests will be fixed
later.

Signed-off-by: Luc Berger-Vergiat <lberge@sandia.gov>

* Applying clang-format

Signed-off-by: Luc Berger-Vergiat <lberge@sandia.gov>

* Switching a few static_cast to size_t for clarity

After discussion in the PR, these changes should not result in issues
when passed to the view constructors and improve clarity for future
maintenance.

Signed-off-by: Luc Berger-Vergiat <lberge@sandia.gov>

---------

Signed-off-by: Luc Berger-Vergiat <lberge@sandia.gov>
  • Loading branch information
lucbv authored Oct 29, 2024
1 parent e1265ec commit 5ee9a1d
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion graph/impl/KokkosGraph_Distance2Color_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ class GraphColorDistance2 {
const lno_t numVerts = this->nr;
const lno_t numCols = this->nc;
// note: relying on forbidden and colors_out being initialized to 0
forbidden_view forbidden("Forbidden", batch * numCols);
forbidden_view forbidden("Forbidden", static_cast<size_t>(batch) * numCols);
int iter = 0;
Kokkos::Timer timer;
lno_t currentWork = this->nr;
Expand Down
4 changes: 2 additions & 2 deletions sparse/src/KokkosSparse_IOUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void kk_sparseMatrix_generate(OrdinalType nrows, OrdinalType ncols, SizeType &nn
}
// Sample each value from uniform (-50, 50) for real types, or (-50 - 50i, 50
// + 50i) for complex types.
Kokkos::View<ScalarType *, Kokkos::HostSpace> valuesView(values, nnz * block_elem_count);
Kokkos::View<ScalarType *, Kokkos::HostSpace> valuesView(values, nnz * static_cast<size_t>(block_elem_count));
ScalarType randStart, randEnd;
KokkosKernels::Impl::getRandomBounds(50.0, randStart, randEnd);
Kokkos::Random_XorShift64_Pool<Kokkos::DefaultHostExecutionSpace> pool(13718);
Expand Down Expand Up @@ -888,7 +888,7 @@ int read_mtx(const char *fileName, lno_t *nrows, lno_t *ncols, size_type *ne, si
if (mtx_format == COORDINATE)
ss >> nnz;
else
nnz = nr * nc;
nnz = static_cast<size_type>(nr) * nc;
size_type numEdges = nnz;
symmetrize = symmetrize || mtx_sym != GENERAL;
if (symmetrize && nr != nc) {
Expand Down
2 changes: 1 addition & 1 deletion sparse/src/KokkosSparse_Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ bsrMat_t transpose_bsr_matrix(const bsrMat_t &A) {
rowmap_t AT_rowmap("Transpose rowmap", A.numCols() + 1);
entries_t AT_entries(Kokkos::view_alloc(Kokkos::WithoutInitializing, "Transpose entries"), A.nnz());
values_t AT_values(Kokkos::view_alloc(Kokkos::WithoutInitializing, "Transpose values"),
A.nnz() * A.blockDim() * A.blockDim());
A.nnz() * static_cast<typename values_t::size_type>(A.blockDim()) * A.blockDim());
transpose_bsr_matrix<c_rowmap_t, c_entries_t, c_values_t, rowmap_t, entries_t, values_t,
typename bsrMat_t::execution_space>(A.numRows(), A.numCols(), A.blockDim(), A.graph.row_map,
A.graph.entries, A.values, AT_rowmap, AT_entries, AT_values);
Expand Down
2 changes: 1 addition & 1 deletion sparse/src/KokkosSparse_sptrsv_supernode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1947,7 +1947,7 @@ void split_crsmat(KernelHandle *kernelHandleL, host_crsmat_t superluL) {
}
// allocate for all the subgraphs
row_map_view_t total_rowmap_view(Kokkos::view_alloc(Kokkos::WithoutInitializing, "rowmap_view"),
2 * nlevels * (nrows + 1));
static_cast<typename row_map_view_t::size_type>(2 * nlevels) * (nrows + 1));
cols_view_t total_column_view(Kokkos::view_alloc(Kokkos::WithoutInitializing, "colmap_view"), newNnz);
values_view_t total_values_view(Kokkos::view_alloc(Kokkos::WithoutInitializing, "values_view"), newNnz);
// create host-mirrors
Expand Down

0 comments on commit 5ee9a1d

Please sign in to comment.