diff --git a/src/comm.cpp b/src/comm.cpp index 87e7a47ab0..2d64ec3bf5 100644 --- a/src/comm.cpp +++ b/src/comm.cpp @@ -37,6 +37,10 @@ #include "error.h" #include "memory.h" +#ifdef LMP_OPENMP +#include "omp.h" +#endif + using namespace LAMMPS_NS; #define BUFFACTOR 1.5 @@ -68,7 +72,24 @@ Comm::Comm(LAMMPS *lmp) : Pointers(lmp) cutghostuser = 0.0; ghost_velocity = 0; + // use of OpenMP threads + // query OpenMP for number of threads/process set by user at run-time + // need to be in a parallel area for this operation + nthreads = 1; +#ifdef LMP_OPENMP +#pragma omp parallel default(shared) + { +#pragma omp master + { nthreads = omp_get_num_threads(); } + } + if (me == 0) { + if (screen) + fprintf(screen," using %d OpenMP thread(s) per MPI task\n",nthreads); + if (logfile) + fprintf(logfile," using %d OpenMP thread(s) per MPI task\n",nthreads); + } +#endif // initialize comm buffers & exchange memory diff --git a/src/input.cpp b/src/input.cpp index ff0c227381..fa10ea0064 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -46,6 +46,10 @@ #include "error.h" #include "memory.h" +#ifdef LMP_OPENMP +#include "omp.h" +#endif + using namespace LAMMPS_NS; #define MAXLINE 2048 @@ -1136,10 +1140,25 @@ void Input::package() delete [] fixarg; } else if (strcmp(arg[0],"omp") == 0) { + +#ifdef LMP_OPENMP if (narg != 2) error->all("Illegal package command"); comm->nthreads = atoi(arg[1]); if (comm->nthreads < 1) error->all("Illegal package command"); + omp_set_num_threads(nthr); + if (me == 0) { + if (screen) + fprintf(screen," reset %d OpenMP thread(s) per MPI task\n", + comm->nthreads); + if (logfile) + fprintf(logfile," reset %d OpenMP thread(s) per MPI task\n", + comm->nthreads); + } +#else + error->all("Cannot use package omp command with no OpenMP support"); +#endif + } else error->all("Illegal package command"); }