30module fms_affinity_mod
32 use,
intrinsic :: iso_c_binding, only: c_int, c_bool
36 use mpp_mod,
only: input_nml_file, mpp_pe, stdlog
37 use fms_mod,
only: fms_init, check_nml_error, write_version_number, &
38 error_mesg, fatal, note
56 integer(KIND=c_int) function get_cpuset(fsz, output, pe, debug)
bind(c, name="get_cpuset")
58 integer(KIND=c_int),
value,
intent(in) :: fsz, pe
59 integer(KIND=c_int),
dimension(*),
intent(inout) :: output
60 logical(KIND=c_bool),
value :: debug
68 integer(KIND=c_int),
value,
intent(in) :: cpu
73 logical:: affinity = .true.
74 logical:: strict = .true.
75 logical:: debug_affinity = .false.
76 logical(c_bool):: debug_cpuset = .false.
77 namelist /fms_affinity_nml/ affinity, strict, debug_affinity, debug_cpuset
83#include <file_version.h>
85 logical :: module_is_initialized = .false.
97 if (module_is_initialized)
return
103 read(input_nml_file, fms_affinity_nml, iostat=io_stat)
104 ierr = check_nml_error(io_stat,
'fms_affinity_nml')
107 call write_version_number(
"fms_affinity_mod", version)
109 write(iunit,nml=fms_affinity_nml)
111 module_is_initialized = .true.
119 character(len=*),
intent(in):: component
120 logical,
intent(in):: use_hyper_thread
121 integer,
intent(in):: nthreads
124 integer(c_int):: cpuset_sz
125 integer(c_int),
dimension(:),
allocatable:: cpu_set
126 integer(c_int):: retcode
129 character(len=32):: h_name
135 if (.not. affinity)
return
146 if (use_hyper_thread)
then
149 cpuset_sz = nthreads * 2
151 allocate (cpu_set(0:cpuset_sz-1))
154 retcode =
get_cpuset(cpuset_sz, cpu_set, mpp_pe(), debug_cpuset)
155 if (retcode == -1)
then
156 call error_mesg(
'fms_affinity_set',trim(component)//
' cpu_set size > allocated storage',fatal)
157 elseif ( (retcode == cpuset_sz/2) .and. (retcode == nthreads) )
then
158 call error_mesg(
'fms_affinity_set',trim(component)//
' affinity assumes hyper-threading hardware disabled',note)
159 elseif (retcode < cpuset_sz)
then
160 call error_mesg(
'fms_affinity_set',trim(component)//
' cpu_set size smaller than expected',msg_type)
165 if (retcode == -1)
then
166 call error_mesg(
'fms_affinity_set',trim(component)//
': issue setting cpu affinity', fatal)
197end module fms_affinity_mod
subroutine, public fms_affinity_init()
Initialization routine for affinity handling.
subroutine, public fms_affinity_set(component, use_hyper_thread, nthreads)
Routine to set affinity for a component.
Private interface to retrieve this groups CPU set and it's size.
Private interface to set CPU afinity to a given core.
Interface to get affinity from the current component.