diff --git a/tools/lammps-gui/lammpsgui.cpp b/tools/lammps-gui/lammpsgui.cpp index f101006f61..50f9a0b735 100644 --- a/tools/lammps-gui/lammpsgui.cpp +++ b/tools/lammps-gui/lammpsgui.cpp @@ -1095,9 +1095,9 @@ void LammpsGui::preferences() // must delete LAMMPS instance after preferences have changed that require // using different command line flags when creating the LAMMPS instance like // suffixes or package commands + int newthreads = settings.value("nthreads", 1).toInt(); if ((oldaccel != settings.value("accelerator", AcceleratorTab::None).toInt()) || - (oldthreads != settings.value("nthreads", 1).toInt()) || - (oldecho != settings.value("echo", 0).toBool()) || + (oldthreads != newthreads) || (oldecho != settings.value("echo", 0).toBool()) || (oldcite != settings.value("cite", 0).toBool())) { if (lammps.is_running()) { stop_run(); @@ -1106,6 +1106,10 @@ void LammpsGui::preferences() } lammps.close(); lammpsstatus->hide(); +#if defined(_OPENMP) + qputenv("OMP_NUM_THREADS", std::to_string(newthreads).c_str()); + omp_set_num_threads(newthreads); +#endif } if (imagewindow) imagewindow->createImage(); } diff --git a/tools/lammps-gui/preferences.cpp b/tools/lammps-gui/preferences.cpp index bdb7d39dbf..91d8026e3f 100644 --- a/tools/lammps-gui/preferences.cpp +++ b/tools/lammps-gui/preferences.cpp @@ -39,6 +39,7 @@ #include #include #include +#include #if defined(_OPENMP) #include @@ -115,8 +116,13 @@ void Preferences::accept() // store number of threads QLineEdit *field = tabWidget->findChild("nthreads"); - if (field) - if (field->hasAcceptableInput()) settings->setValue("nthreads", field->text()); + if (field) { + int accel = settings->value("accelerator", AcceleratorTab::None).toInt(); + if ((accel == AcceleratorTab::None) || (accel == AcceleratorTab::Opt)) + settings->setValue("nthreads", 1); + else if (field->hasAcceptableInput()) + settings->setValue("nthreads", field->text()); + } // store image width, height, zoom, and rendering settings @@ -366,11 +372,11 @@ AcceleratorTab::AcceleratorTab(QSettings *_settings, LammpsWrapper *_lammps, QWi int maxthreads = 1; #if defined(_OPENMP) - maxthreads = omp_get_max_threads(); + maxthreads = QThread::idealThreadCount(); #endif auto *choices = new QFrame; auto *choiceLayout = new QVBoxLayout; - auto *ntlabel = new QLabel("Number of threads:"); + auto *ntlabel = new QLabel(QString("Number of threads (max %1):").arg(maxthreads)); auto *ntchoice = new QLineEdit(settings->value("nthreads", maxthreads).toString()); auto *intval = new QIntValidator(1, maxthreads, this); ntchoice->setValidator(intval);