Skip to content

Commit 79bfbe1

Browse files
Liang-N1Liang-N1
authored andcommitted
Fix #1667 Potential crash for the OpenMP multi-thread running.
Initialize the omp_partitions vector with the number of points, skip the for loop if the last partition has been calculated. A light-weight checker is added to verify the validation of the partition vector.
1 parent 52160fc commit 79bfbe1

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

Common/src/linear_algebra/CSysMatrix.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ void CSysMatrix<ScalarType>::Initialize(unsigned long npoint, unsigned long npoi
190190

191191
/*--- This is akin to the row_ptr. ---*/
192192
omp_partitions = new unsigned long [omp_num_parts+1];
193+
for (unsigned long i = 0; i <= omp_num_parts; ++i) omp_partitions[i] = nPointDomain;
193194

194195
/*--- Work estimate based on non-zeros to produce balanced partitions. ---*/
195196

@@ -202,7 +203,17 @@ void CSysMatrix<ScalarType>::Initialize(unsigned long npoint, unsigned long npoi
202203
if (row_ptr_prec[iPoint] >= part*nnz_per_part)
203204
omp_partitions[part++] = iPoint;
204205
}
205-
omp_partitions[omp_num_parts] = nPointDomain;
206+
207+
for (unsigned long thread = 0; thread < omp_num_parts; ++thread) {
208+
const unsigned long begin = omp_partitions[thread];
209+
const unsigned long end = omp_partitions[thread + 1];
210+
if (begin > end) {
211+
SU2_MPI::Error("Invalid nodes are distributed in the thread " + to_string(thread) + ".", CURRENT_FUNCTION);
212+
} else if (begin == end) {
213+
cout << "WARNING: Redundent thread has been detected. Performance could be impacted." << endl;
214+
break;
215+
}
216+
}
206217

207218
/*--- Generate MKL Kernels ---*/
208219

@@ -707,6 +718,7 @@ void CSysMatrix<ScalarType>::BuildILUPreconditioner() {
707718
{
708719
const auto begin = omp_partitions[thread];
709720
const auto end = omp_partitions[thread+1];
721+
if (begin == end) continue;
710722

711723
/*--- Each thread will work on the submatrix defined from row/col "begin"
712724
* to row/col "end-1" (i.e. the range [begin,end[). Which is exactly
@@ -784,6 +796,7 @@ void CSysMatrix<ScalarType>::ComputeILUPreconditioner(const CSysVector<ScalarTyp
784796
{
785797
const auto begin = omp_partitions[thread];
786798
const auto end = omp_partitions[thread+1];
799+
if (begin == end) continue;
787800

788801
ScalarType aux_vec[MAXNVAR];
789802

@@ -846,6 +859,7 @@ void CSysMatrix<ScalarType>::ComputeLU_SGSPreconditioner(const CSysVector<Scalar
846859
{
847860
const auto begin = omp_partitions[thread];
848861
const auto end = omp_partitions[thread+1];
862+
if (begin == end) continue;
849863

850864
/*--- Each thread will work on the submatrix defined from row/col "begin"
851865
* to row/col "end-1", except the last thread that also considers halos.
@@ -876,6 +890,8 @@ void CSysMatrix<ScalarType>::ComputeLU_SGSPreconditioner(const CSysVector<Scalar
876890
{
877891
const auto begin = omp_partitions[thread];
878892
const auto row_end = omp_partitions[thread+1];
893+
if (begin == row_end) continue;
894+
879895
/*--- On the last thread partition the upper
880896
* product should consider halo columns. ---*/
881897
const auto col_end = (row_end==nPointDomain)? nPoint : row_end;

0 commit comments

Comments
 (0)