Skip to content

Commit ca4cc94

Browse files
authored
refactor: add an eye-catched warning when setted-threads larger than hardware availability (#1813)
1 parent 2d92ad7 commit ca4cc94

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

source/src_parallel/parallel_global.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,26 +168,41 @@ void Parallel_Global::read_mpi_parameters(int argc,char **argv)
168168
MPI_Comm_rank(MPI_COMM_WORLD, &GlobalV::MY_RANK);
169169

170170
// determining appropriate thread number for OpenMP
171-
#ifdef _OPENMP
172171
const int max_thread_num = std::thread::hardware_concurrency(); // Consider Hyperthreading disabled.
172+
#ifdef _OPENMP
173173
int current_thread_num = omp_get_max_threads();
174+
#else
175+
int current_thread_num = 1;
176+
#endif
174177
MPI_Comm shmcomm;
175178
MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, 0, MPI_INFO_NULL, &shmcomm);
176179
int process_num, local_rank;
177180
MPI_Comm_size(shmcomm, &process_num);
178181
MPI_Comm_rank(shmcomm, &local_rank);
179182
MPI_Comm_free(&shmcomm);
180-
if (current_thread_num * process_num != max_thread_num && local_rank==0)
183+
if (current_thread_num * process_num > max_thread_num && local_rank==0)
181184
{
182-
// only output info in local rank 0
185+
std::stringstream mess;
186+
mess << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl;
187+
mess << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl;
188+
mess << "%% WARNING: Total thread number(" << current_thread_num * process_num << ") "
189+
<< "is larger than hardware availability(" << max_thread_num << ")." << std::endl;
190+
mess << "%% WARNING: The results may be INCORRECT. Please be sure what you are doing." << std::endl;
191+
mess << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl;
192+
mess << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl;
193+
std::cerr << mess.str() << std::endl;
194+
}
195+
else if (current_thread_num * process_num < max_thread_num && local_rank==0)
196+
{
197+
// only output info in local rank 0
183198
std::cerr << "WARNING: Total thread number on this node mismatches with hardware availability. "
184199
"This may cause poor performance."<< std::endl;
185200
std::cerr << "Info: Local MPI proc number: " << process_num << ","
186201
<< "OpenMP thread number: " << current_thread_num << ","
187202
<< "Total thread number: " << current_thread_num * process_num << ","
188203
<< "Local thread limit: " << max_thread_num << std::endl;
189204
}
190-
#endif
205+
191206

192207
if (GlobalV::MY_RANK == 0)
193208
{

0 commit comments

Comments
 (0)