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

Tpetra: fixing matrix-matrix multiply for GPUs #2332

Merged
merged 1 commit into from
Mar 5, 2018
Merged
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
22 changes: 13 additions & 9 deletions packages/tpetra/core/ext/TpetraExt_MatrixMatrix_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2268,7 +2268,7 @@ void mult_A_B_newmatrix(
Cimport = Bimport;
Ccolmap = Bview.colMap;
// Bcol2Ccol is trivial
Kokkos::parallel_for(range_type(0,Bview.colMap->getNodeNumElements()),KOKKOS_LAMBDA(const size_t i) {
Kokkos::parallel_for("Tpetra::mult_A_B_newmatrix::Bcol2Ccol_fill",range_type(0,Bview.colMap->getNodeNumElements()),KOKKOS_LAMBDA(const size_t i) {
Bcol2Ccol(i) = Teuchos::as<LO>(i);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Teuchos::as is not a device function. Also, in a debug build (Teuchos_ENABLE_DEBUG:BOOL=ON), it may throw an exception on overflow.

});

Expand Down Expand Up @@ -2308,10 +2308,10 @@ void mult_A_B_newmatrix(
// local index - to - local index look-up tables.
Tpetra::MatrixMatrix::Kokkos_resize_1DView(Icol2Ccol,Bview.importMatrix->getColMap()->getNodeNumElements());
local_map_type Ccolmap_local = Ccolmap->getLocalMap();
Kokkos::parallel_for(range_type(0,Bview.origMatrix->getColMap()->getNodeNumElements()),KOKKOS_LAMBDA(const LO i) {
Kokkos::parallel_for("Tpetra::mult_A_B_newmatrix::Bcol2Ccol_getGlobalElement",range_type(0,Bview.origMatrix->getColMap()->getNodeNumElements()),KOKKOS_LAMBDA(const LO i) {
Bcol2Ccol(i) = Ccolmap_local.getLocalElement(Bcolmap_local.getGlobalElement(i));
});
Kokkos::parallel_for(range_type(0,Bview.importMatrix->getColMap()->getNodeNumElements()),KOKKOS_LAMBDA(const LO i) {
Kokkos::parallel_for("Tpetra::mult_A_B_newmatrix::Icol2Ccol_getGlobalElement",range_type(0,Bview.importMatrix->getColMap()->getNodeNumElements()),KOKKOS_LAMBDA(const LO i) {
Icol2Ccol(i) = Ccolmap_local.getLocalElement(Icolmap_local.getGlobalElement(i));
});

Expand Down Expand Up @@ -2341,7 +2341,8 @@ void mult_A_B_newmatrix(
// Run through all the hash table lookups once and for all
lo_view_t targetMapToOrigRow(Kokkos::ViewAllocateWithoutInitializing("targetMapToOrigRow"),Aview.colMap->getNodeNumElements());
lo_view_t targetMapToImportRow(Kokkos::ViewAllocateWithoutInitializing("targetMapToImportRow"),Aview.colMap->getNodeNumElements());
Kokkos::parallel_for(range_type(Aview.colMap->getMinLocalIndex(), Aview.colMap->getMaxLocalIndex()+1),KOKKOS_LAMBDA(const LO i) {
Kokkos::fence();
Kokkos::parallel_for("Tpetra::mult_A_B_newmatrix::construct_tables",range_type(Aview.colMap->getMinLocalIndex(), Aview.colMap->getMaxLocalIndex()+1),KOKKOS_LAMBDA(const LO i) {
GO aidx = Acolmap_local.getGlobalElement(i);
LO B_LID = Browmap_local.getLocalElement(aidx);
if (B_LID != LO_INVALID) {
Expand Down Expand Up @@ -3698,6 +3699,8 @@ merge_matrices(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview
RCP<const KCRS> Ik_;
if(!Bview.importMatrix.is_null()) Ik_ = Teuchos::rcpFromRef<const KCRS>(Bview.importMatrix->getLocalMatrix());
const KCRS * Ik = Bview.importMatrix.is_null() ? 0 : &*Ik_;
KCRS Iks;
if(Ik!=0) Iks = *Ik;
size_t merge_numrows = Ak.numCols();
lno_view_t Mrowptr("Mrowptr", merge_numrows + 1);

Expand All @@ -3706,14 +3709,15 @@ merge_matrices(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview
// Use a Kokkos::parallel_scan to build the rowptr
typedef typename Node::execution_space execution_space;
typedef Kokkos::RangePolicy<execution_space, size_t> range_type;
Kokkos::parallel_scan ("Tpetra_MatrixMatrix_merge_matrices_buildRowptr", range_type (0, merge_numrows), KOKKOS_LAMBDA(const size_t i, size_t& update, const bool final) {
Kokkos::parallel_scan ("Tpetra_MatrixMatrix_merge_matrices_buildRowptr", range_type (0, merge_numrows),
KOKKOS_LAMBDA(const size_t i, size_t& update, const bool final) {
if(final) Mrowptr(i) = update;
// Get the row count
size_t ct=0;
if(Acol2Brow(i)!=LO_INVALID)
ct = Bk.graph.row_map(Acol2Brow(i)+1) - Bk.graph.row_map(Acol2Brow(i));
else
ct = Ik->graph.row_map(Acol2Irow(i)+1) - Ik->graph.row_map(Acol2Irow(i));
ct = Iks.graph.row_map(Acol2Irow(i)+1) - Iks.graph.row_map(Acol2Irow(i));
update+=ct;

if(final && i+1==merge_numrows)
Expand All @@ -3738,10 +3742,10 @@ merge_matrices(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview
}
else {
size_t row = Acol2Irow(i);
size_t start = Ik->graph.row_map(row);
size_t start = Iks.graph.row_map(row);
for(size_t j= Mrowptr(i); j<Mrowptr(i+1); j++) {
Mvalues(j) = Ik->values(j-Mrowptr(i)+start);
Mcolind(j) = Icol2Ccol(Ik->graph.entries(j-Mrowptr(i)+start));
Mvalues(j) = Iks.values(j-Mrowptr(i)+start);
Mcolind(j) = Icol2Ccol(Iks.graph.entries(j-Mrowptr(i)+start));
}
}
});
Expand Down