Skip to content

Commit 1e10fa9

Browse files
authored
Merge pull request #4113 from lindsayad/lock-petsc-matrix
Lock at PetscMatrix level when doing add/set
2 parents 59f2eca + c0bd9ec commit 1e10fa9

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

src/numerics/petsc_matrix.C

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ void PetscMatrix<T>::add_matrix(const DenseMatrix<T> & dm,
738738
libmesh_assert_equal_to (rows.size(), n_rows);
739739
libmesh_assert_equal_to (cols.size(), n_cols);
740740

741+
std::scoped_lock lock(this->_petsc_matrix_mutex);
741742
LibmeshPetscCall(MatSetValues(this->_mat,
742743
n_rows, numeric_petsc_cast(rows.data()),
743744
n_cols, numeric_petsc_cast(cols.data()),
@@ -778,6 +779,7 @@ void PetscMatrix<T>::add_block_matrix(const DenseMatrix<T> & dm,
778779
libmesh_assert_equal_to (blocksize, static_cast<numeric_index_type>(petsc_blocksize));
779780
#endif
780781

782+
std::scoped_lock lock(this->_petsc_matrix_mutex);
781783
// These casts are required for PETSc <= 2.1.5
782784
LibmeshPetscCall(MatSetValuesBlocked(this->_mat,
783785
n_brows, numeric_petsc_cast(brows.data()),
@@ -968,6 +970,7 @@ void PetscMatrix<T>::set (const numeric_index_type i,
968970
PetscInt i_val=i, j_val=j;
969971

970972
PetscScalar petsc_value = static_cast<PetscScalar>(value);
973+
std::scoped_lock lock(this->_petsc_matrix_mutex);
971974
LibmeshPetscCall(MatSetValues(this->_mat, 1, &i_val, 1, &j_val,
972975
&petsc_value, INSERT_VALUES));
973976
}
@@ -984,6 +987,7 @@ void PetscMatrix<T>::add (const numeric_index_type i,
984987
PetscInt i_val=i, j_val=j;
985988

986989
PetscScalar petsc_value = static_cast<PetscScalar>(value);
990+
std::scoped_lock lock(this->_petsc_matrix_mutex);
987991
LibmeshPetscCall(MatSetValues(this->_mat, 1, &i_val, 1, &j_val,
988992
&petsc_value, ADD_VALUES));
989993
}
@@ -1190,12 +1194,7 @@ void PetscMatrix<T>::get_row (numeric_index_type i_in,
11901194
// (through petsc_cols) during data copy in another thread. So
11911195
// the safe thing to do is to lock the whole method
11921196

1193-
#ifdef LIBMESH_HAVE_CXX11_THREAD
1194-
std::lock_guard<std::mutex>
1195-
#else
1196-
Threads::spin_mutex::scoped_lock
1197-
#endif
1198-
lock(_petsc_matrix_mutex);
1197+
std::lock_guard<std::mutex> lock(_petsc_matrix_mutex);
11991198

12001199
LibmeshPetscCall(MatGetRow(this->_mat, i_val, &ncols, &petsc_cols, &petsc_row));
12011200

0 commit comments

Comments
 (0)