Skip to content

Commit ba15781

Browse files
committed
Updated implementation to use 1 OpenMP thread by default and give a warning that the 'OMP_NUM_THREADS' environment varibale is unset. This allows an unaware user to run their codes without any changes, and they will see the warning and can adjust accordingly.
1 parent 51f7161 commit ba15781

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/pyclaw/classic/solver.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,12 @@ def __init__(self, riemann_solver=None, claw_package=None):
612612
self.work = None
613613

614614
import os
615-
import multiprocessing
616-
self.nthreads = int(os.environ.get('OMP_NUM_THREADS',\
617-
multiprocessing.cpu_count()))
615+
import warnings
616+
if 'OMP_NUM_THREADS' in os.environ:
617+
pass # Using OpenMP
618+
else:
619+
warnings.warn("The environment variable 'OMP_NUM_THREADS' is unset. Set this variable to use OpenMP with more than 1 thread (i.e. export OMP_NUM_THREADS=4).")
620+
self.nthreads = int(os.environ.get('OMP_NUM_THREADS',1))
618621

619622
super(ClawSolver3D,self).__init__(riemann_solver, claw_package)
620623

src/pyclaw/classic/step3.f90

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ subroutine step3(maxm,nthreads,num_eqn,num_waves,num_ghost,mx,my, &
4040
dimension method(7),mthlim(num_waves)
4141
dimension work(mwork)
4242
logical :: use_fwave
43+
character :: env_value
4344

4445
! These block sizes must be at least 2 to avoid multiple threads
4546
! trying to update the same grid cell
@@ -61,6 +62,10 @@ subroutine step3(maxm,nthreads,num_eqn,num_waves,num_ghost,mx,my, &
6162
! flux3 routine. Find starting index of each piece:
6263

6364
nthreads=1 ! Serial
65+
CALL GET_ENVIRONMENT_VARIABLE('OMP_NUM_THREADS',env_value,nlen,nstat)
66+
IF(nstat >= 1)THEN
67+
!$ call omp_set_num_threads(1) ! set default to 1 thread
68+
END IF
6469
!$omp parallel
6570
!$omp single
6671
!$ nthreads = omp_get_num_threads()

src/pyclaw/classic/step3ds.f90

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ subroutine step3ds(maxm,nthreads,num_eqn,num_waves,num_ghost,mx,my, &
5454
dimension method(7),mthlim(num_waves)
5555
dimension work(mwork)
5656
logical :: use_fwave
57+
character :: env_value
5758

5859
common /comxyzt/ dtcom,dxcom,dycom,dzcom,tcom,icom,jcom,kcom
5960

@@ -71,6 +72,10 @@ subroutine step3ds(maxm,nthreads,num_eqn,num_waves,num_ghost,mx,my, &
7172
! flux3 routine. Find starting index of each piece:
7273

7374
nthreads=1 ! Serial
75+
CALL GET_ENVIRONMENT_VARIABLE('OMP_NUM_THREADS',env_value,nlen,nstat)
76+
IF(nstat >= 1)THEN
77+
!$ call omp_set_num_threads(1) ! set default to 1 thread
78+
END IF
7479
!$OMP PARALLEL
7580
!$OMP SINGLE
7681
!$ nthreads = omp_get_num_threads()

0 commit comments

Comments
 (0)