18 USE iso_c_binding,
ONLY: c_char, &
25 USE iso_fortran_env,
ONLY: error_unit, &
28 USE omp_lib,
ONLY: omp_get_wtime
34 USE libxs,
ONLY: libxs_timer_tick, libxs_timer_duration, libxs_cpuid, &
35 libxs_target_arch_generic, libxs_x86_sse42, libxs_x86_avx, libxs_x86_avx2, &
36 libxs_x86_avx512, libxs_aarch64, libxs_aarch64_sve128, &
37 libxs_aarch64_sve256, libxs_aarch64_sve512, &
54#include "machine_cpuid.h"
61 INTEGER,
PUBLIC,
PARAMETER :: &
129 INTEGER,
INTENT(IN) :: lunit
149 wt = libxs_timer_duration(0_int_8, libxs_timer_tick())
160 CHARACTER(LEN=default_string_length),
INTENT(OUT) :: model_name
162 INTEGER,
PARAMETER :: bufferlen = 2048
164 CHARACTER(LEN=bufferlen) :: buffer
165 INTEGER :: i, icol, iline, stat, unit_nr
167 model_name =
"UNKNOWN"
169 OPEN (newunit=unit_nr, file=
"/proc/cpuinfo", action=
"READ", status=
"OLD", access=
"STREAM", iostat=stat)
172 READ (unit_nr,
END=999) buffer(I:I)
175 i = index(buffer,
"model name")
177 icol = i - 1 + index(buffer(i:),
":")
178 iline = icol - 1 + index(buffer(icol:), new_line(
'A'))
179 IF (iline == icol - 1) iline = bufferlen + 1
180 model_name = buffer(icol + 1:iline - 1)
200 archid = libxs_cpuid(c_null_ptr)
201 IF (libxs_x86_sse42 <= archid .AND. archid < libxs_x86_avx)
THEN
203 ELSE IF (libxs_x86_avx <= archid .AND. archid < libxs_x86_avx2)
THEN
205 ELSE IF (libxs_x86_avx2 <= archid .AND. archid < libxs_x86_avx512)
THEN
207 ELSE IF (libxs_x86_avx512 <= archid .AND. archid <= 1999)
THEN
212 IF (libxs_aarch64 <= archid .AND. archid < libxs_aarch64_sve128)
THEN
214 ELSE IF (libxs_aarch64_sve128 <= archid .AND. archid < libxs_aarch64_sve256)
THEN
216 ELSE IF (libxs_aarch64_sve256 <= archid .AND. archid < libxs_aarch64_sve512)
THEN
218 ELSE IF (libxs_aarch64_sve512 <= archid .AND. archid <= 2999)
THEN
238 INTEGER,
INTENT(IN),
OPTIONAL :: cpuid
243 IF (
PRESENT(cpuid))
THEN
282 INTEGER,
INTENT(IN),
OPTIONAL :: cpuid, typesize
285 INTEGER :: isa, nbytes
287 IF (
PRESENT(typesize))
THEN
293 IF (0 < nbytes .AND. nbytes <= 16)
THEN
294 IF (
PRESENT(cpuid))
THEN
336#if defined(__CRAY_PM_ENERGY)
337 wt = read_energy(
"/sys/cray/pm_counters/energy")
338#elif defined(__CRAY_PM_ACCEL_ENERGY)
339 wt = read_energy(
"/sys/cray/pm_counters/accel_energy")
346#if defined(__CRAY_PM_ACCEL_ENERGY) || defined(__CRAY_PM_ENERGY)
354 FUNCTION read_energy(filename)
RESULT(wt)
355 CHARACTER(LEN=*) :: filename
358 CHARACTER(LEN=80) :: data
359 INTEGER :: i, iostat, unit_nr
360 INTEGER(KIND=int_8) :: raw
362 OPEN (newunit=unit_nr, file=filename, action=
"READ", status=
"OLD", access=
"STREAM")
364 READ (unit_nr,
END=999) DATA(I:I)
368 READ (
DATA, *, iostat=iostat) raw
369 IF (iostat /= 0)
THEN
375 END FUNCTION read_energy
386 CHARACTER(LEN=timestamp_length),
INTENT(OUT) :: timestamp
388 CHARACTER(LEN=10) :: time
389 CHARACTER(LEN=8) :: date
391 CALL date_and_time(date=date, time=time)
392 timestamp = date(1:4)//
"-"//date(5:6)//
"-"//date(7:8)//
" "// &
393 time(1:2)//
":"//time(3:4)//
":"//time(5:10)
402 SUBROUTINE abort()
BIND(C, name="abort")
416 INTEGER,
INTENT(IN) :: pid
421 FUNCTION kill(pid, sig)
RESULT(errno)
BIND(C, name="kill")
423 INTEGER(KIND=C_INT),
VALUE :: pid, sig
424 INTEGER(KIND=C_INT) :: errno
432 istat = kill(pid=pid, sig=0)
446 INTEGER(KIND=int_8),
OPTIONAL,
INTENT(OUT) :: mem
447 INTEGER(KIND=int_8) :: mem_local
452#if defined(__NO_STATM_ACCESS)
455 INTEGER(KIND=int_8) :: m1, m2, m3
456 CHARACTER(LEN=80) :: data
457 INTEGER :: iostat, i, unit_nr
461 FUNCTION getpagesize()
BIND(C, name="getpagesize") RESULT(RES)
463 INTEGER(C_INT) :: res
464 END FUNCTION getpagesize
471 OPEN (newunit=unit_nr, file=
"/proc/self/statm", action=
"READ", status=
"OLD", access=
"STREAM")
473 READ (unit_nr,
END=999) DATA(I:I)
480 READ (
DATA, *, iostat=iostat) m1, m2, m3
481 IF (iostat /= 0)
THEN
485#if defined(__STATM_TOTAL)
488#if defined(__STATM_RESIDENT)
491 mem_local = mem_local*getpagesize()
496 IF (
PRESENT(mem)) mem = mem_local
514 SUBROUTINE m_memory_details(MemTotal, MemFree, Buffers, Cached, Slab, SReclaimable, MemLikelyFree)
516 INTEGER(KIND=int_8),
OPTIONAL :: memtotal, memfree, buffers, cached, slab, sreclaimable, memlikelyfree
518 INTEGER,
PARAMETER :: nbuffer = 10000
519 CHARACTER(LEN=Nbuffer) :: meminfo
521 INTEGER :: i, unit_nr
522 INTEGER(KIND=C_INT64_T) :: c_free, c_likely_free, c_total
525 SUBROUTINE m_memory_details_c(total, free, likely_free)
BIND(C, name="m_memory_details_c")
527 INTEGER(KIND=C_INT64_T) :: total, free, likely_free
540 OPEN (newunit=unit_nr, file=
"/proc/meminfo", access=
"STREAM", err=902)
544 IF (i > nbuffer)
EXIT
545 READ (unit_nr,
END=900, ERR=900) meminfo(i:i)
548 meminfo(i:nbuffer) =
""
549 CLOSE (unit_nr, err=902)
551 memtotal = get_field_value_in_bytes(
'MemTotal:')
552 memfree = get_field_value_in_bytes(
'MemFree:')
553 buffers = get_field_value_in_bytes(
'Buffers:')
554 cached = get_field_value_in_bytes(
'Cached:')
555 slab = get_field_value_in_bytes(
'Slab:')
556 sreclaimable = get_field_value_in_bytes(
'SReclaimable:')
559 memlikelyfree = get_field_value_in_bytes(
'MemAvailable:')
560 IF (memlikelyfree <= 0) memlikelyfree = memfree + buffers + cached + sreclaimable
563 IF (memtotal <= 0)
THEN
567 memlikelyfree = c_likely_free
577 INTEGER(int_8) FUNCTION get_field_value_in_bytes(field)
578 CHARACTER(LEN=*) :: field
581 INTEGER(KIND=int_8) :: value
583 get_field_value_in_bytes = 0
584 start = index(meminfo, field)
586 start = start + len_trim(field)
587 IF (start < nbuffer)
THEN
588 READ (meminfo(start:), *, err=999,
END=999) value
590 get_field_value_in_bytes =
value*1024
594 END FUNCTION get_field_value_in_bytes
602 CHARACTER(LEN=*),
INTENT(OUT) :: hname
604 CHARACTER(LEN=default_path_length) :: buf
607 FUNCTION gethostname(buf, buflen)
BIND(C, name="gethostname") RESULT(errno)
609 CHARACTER(KIND=C_CHAR),
DIMENSION(*) :: buf
610 INTEGER(KIND=C_INT),
VALUE :: buflen
611 INTEGER(KIND=C_INT) :: errno
612 END FUNCTION gethostname
615 istat = gethostname(buf, len(buf))
617 WRITE (*, *)
"m_hostnm failed"
620 i = index(buf, c_null_char) - 1
629 CHARACTER(LEN=*),
INTENT(OUT) :: curdir
632 CHARACTER(LEN=default_path_length),
TARGET :: tmp
635 FUNCTION getcwd(buf, buflen)
BIND(C, name="getcwd") RESULT(stat)
637 CHARACTER(KIND=C_CHAR),
DIMENSION(*) :: buf
638 INTEGER(KIND=C_INT),
VALUE :: buflen
643 stat = getcwd(tmp, len(tmp))
644 IF (.NOT. c_associated(stat))
THEN
645 WRITE (*, *)
"m_getcwd failed"
648 i = index(tmp, c_null_char) - 1
657 CHARACTER(LEN=*),
INTENT(OUT) :: exe_path
659 CHARACTER(LEN=default_path_length),
TARGET :: tmp
663 FUNCTION readlink(path, buf, bufsiz)
BIND(C, name="readlink") RESULT(nbytes)
665 CHARACTER(KIND=C_CHAR),
DIMENSION(*) :: path, buf
666 INTEGER(KIND=C_SIZE_T),
VALUE :: bufsiz
667 INTEGER(KIND=C_SIZE_T) :: nbytes
668 END FUNCTION readlink
671 n = int(readlink(
"/proc/self/exe"//c_null_char, tmp, int(len(tmp), c_size_t)))
672 IF (n > 0 .AND. n < len(tmp))
THEN
675 CALL get_command_argument(0, exe_path)
685 CHARACTER(LEN=*),
INTENT(IN) :: dir
686 INTEGER,
INTENT(OUT) :: ierror
689 FUNCTION chdir(path)
BIND(C, name="chdir") RESULT(errno)
691 CHARACTER(KIND=C_CHAR),
DIMENSION(*) :: path
692 INTEGER(KIND=C_INT) :: errno
696 ierror = chdir(trim(dir)//c_null_char)
704 INTEGER,
INTENT(OUT) :: pid
707 FUNCTION getpid()
BIND(C, name="getpid") RESULT(pid)
709 INTEGER(KIND=C_INT) :: pid
723 CHARACTER(LEN=*),
INTENT(IN) :: path
728 FUNCTION unlink(path)
BIND(C, name="unlink") RESULT(errno)
730 CHARACTER(KIND=C_CHAR),
DIMENSION(*) :: path
731 INTEGER(KIND=C_INT) :: errno
735 istat = unlink(trim(path)//c_null_char)
745 CHARACTER(LEN=*),
INTENT(IN) :: source, target
748 LOGICAL :: src_exists
751 FUNCTION rename(src, dest)
BIND(C, name="rename") RESULT(errno)
753 CHARACTER(KIND=C_CHAR),
DIMENSION(*) :: src, dest
754 INTEGER(KIND=C_INT) :: errno
758 IF (
TARGET == source)
THEN
759 WRITE (*, *)
"Warning: m_mov ", trim(
TARGET),
" equals ", trim(source)
768 istat = rename(trim(source)//c_null_char, trim(
TARGET)//c_null_char)
771 INQUIRE (file=source, exist=src_exists)
772 IF (.NOT. src_exists)
RETURN
773 WRITE (*, *)
"Trying to move "//trim(source)//
" to "//trim(
TARGET)//
"."
774 WRITE (*, *)
"rename returned status: ", istat
775 WRITE (*, *)
"Problem moving file"
786 CHARACTER(LEN=*),
INTENT(OUT) :: user
791 CALL get_environment_variable(
"LOGNAME",
value=user, status=istat)
794 CALL get_environment_variable(
"USER",
value=user, status=istat)
798 CALL get_environment_variable(
"USERNAME",
value=user, status=istat)
812 CHARACTER(LEN=*),
INTENT(OUT) :: omp_stacksize
817 CALL get_environment_variable(
"OMP_STACKSIZE",
value=omp_stacksize, status=istat)
819 IF (istat /= 0) omp_stacksize =
"default"
Target architecture or instruction set extension according to compiler target flags.
Check OpenMP constructs if environment variable CP2K_OMP_TRACE is set (see base/openmp_trace....
void m_memory_details_c(int64_t *total, int64_t *free_bytes, int64_t *likely_free)
Physical memory where /proc/meminfo does not exist (macOS), in bytes: the total, the free pages,...
Defines the basic variable types.
integer, parameter, public int_8
integer, parameter, public dp
integer, parameter, public default_string_length
integer, parameter, public default_path_length
Machine interface based on Fortran 2003 and POSIX.
integer, parameter, public machine_x86_avx
logical, save, public flush_should_flush
subroutine, public m_getpid(pid)
...
integer, parameter, public machine_arm_sve512
integer, parameter, public default_output_unit
integer, parameter, public machine_x86_sse4
subroutine, public m_memory(mem)
Returns the total amount of memory [bytes] in use, if known, zero otherwise.
integer, parameter, public machine_cpu_generic
integer, parameter, public default_input_unit
integer, parameter, public timestamp_length
subroutine, public m_flush(lunit)
flushes units if the &GLOBAL flag is set accordingly
integer, parameter, public machine_arm_sve256
integer, parameter, public machine_x86_avx2
subroutine, public m_abort()
Can be used to get a nice core.
subroutine, public m_timestamp(timestamp)
Returns a human readable timestamp.
integer function, public m_procrun(pid)
Returns if a process is running on the local machine 1 if yes and 0 if not.
subroutine, public m_memory_details(memtotal, memfree, buffers, cached, slab, sreclaimable, memlikelyfree)
get more detailed memory info, all units are bytes. the only 'useful' option is MemLikelyFree which i...
pure integer function, public m_cpuid()
Target architecture or instruction set extension according to CPU-check at runtime.
subroutine, public m_getcwd(curdir)
...
integer, parameter, public machine_x86
subroutine, public m_chdir(dir, ierror)
...
pure integer function, public m_cpuid_vlen(cpuid, typesize)
Determine vector-length for a given CPUID.
real(kind=dp) function, public m_energy()
returns the energy used since some time in the past. The precise meaning depends on the infrastructur...
integer, parameter, public machine_arm
integer function, public m_unlink(path)
...
subroutine, public m_cpuinfo(model_name)
reads /proc/cpuinfo if it exists (i.e. Linux) to return relevant info
subroutine, public m_mov(source, target)
...
integer, parameter, public default_error_unit
pure character(len=default_string_length) function, public m_cpuid_name(cpuid)
Determine name of target architecture for a given CPUID.
integer, parameter, public machine_x86_avx512
subroutine, public m_getexe(exe_path)
Returns the path of the running executable.
integer, parameter, public machine_arm_arch64
subroutine, public m_omp_get_stacksize(omp_stacksize)
Retrieve environment variable OMP_STACKSIZE.
integer, parameter, public machine_cpu_unknown
integer(kind=int_8), save, public m_memory_max
integer, parameter, public machine_arm_sve128
subroutine, public m_getlog(user)
...
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
subroutine, public m_hostnm(hname)
...