(git:2bb242e)
Loading...
Searching...
No Matches
machine.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief Machine interface based on Fortran 2003 and POSIX
10!> \par History
11!> JGH (05.07.2001) : added G95 interface
12!> - m_flush added (12.06.2002,MK)
13!> - Missing print_memory added (24.09.2002,MK)
14!> - Migrate to generic implementation based on F2003 + POSIX (2014, Ole Schuett)
15!> \author APSI, JGH, Ole Schuett
16! **************************************************************************************************
17MODULE machine
18 USE iso_c_binding, ONLY: c_char, &
19 c_int, &
20 c_int64_t, &
21 c_size_t, &
22 c_ptr, &
23 c_null_char, &
24 c_associated
25 USE iso_fortran_env, ONLY: error_unit, &
26 input_unit, &
27 output_unit
28 USE omp_lib, ONLY: omp_get_wtime
29 USE kinds, ONLY: default_path_length, &
31 dp, &
32 int_8
33#if defined(__LIBXS)
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, &
38 c_null_ptr
39#endif
40
41 IMPLICIT NONE
42
43 ! Except for some error handling code, all code should
44 ! get a unit number from the print keys or from the logger, in order
45 ! to guarantee correct output behavior,
46 ! for example in farming or path integral runs
47 ! default_input_unit should never be used
48 ! but we need to know what it is, as we should not try to open it for output
49 INTEGER, PUBLIC, PARAMETER :: default_error_unit = error_unit, &
50 default_input_unit = input_unit, &
51 default_output_unit = output_unit, &
53
54#include "machine_cpuid.h"
55 ! Enumerates the target architectures or instruction set extensions.
56 ! A feature is present if within range for the respective architecture.
57 ! For example, to check for MACHINE_X86_AVX the following is true:
58 ! MACHINE_X86_AVX <= m_cpuid() and MACHINE_X86 >= m_cpuid().
59 ! For example, to check for MACHINE_ARM_SOME the following is true:
60 ! MACHINE_ARM_SOME <= m_cpuid() and MACHINE_ARM >= m_cpuid().
61 INTEGER, PUBLIC, PARAMETER :: &
62 machine_cpu_generic = cp_machine_cpu_generic, &
63 !
64 machine_x86_sse4 = cp_machine_x86_sse4, &
65 machine_x86_avx = cp_machine_x86_avx, &
66 machine_x86_avx2 = cp_machine_x86_avx2, &
67 machine_x86_avx512 = cp_machine_x86_avx512, &
69 !
70 machine_arm_arch64 = cp_machine_arm_arch64, &
71 machine_arm_sve128 = cp_machine_arm_sve128, &
72 machine_arm_sve256 = cp_machine_arm_sve256, &
73 machine_arm_sve512 = cp_machine_arm_sve512, &
75 !
76 ! other archs to be added as needed
77 machine_cpu_unknown = cp_machine_unknown ! marks end of range
78
79 PRIVATE
80
86
87 INTERFACE
88 ! **********************************************************************************************
89 !> \brief Target architecture or instruction set extension according to compiler target flags.
90 !> \return cpuid according to MACHINE_* integer-parameter.
91 !> \par History
92 !> 04.2019 created [Hans Pabst]
93 ! **********************************************************************************************
94 PURE FUNCTION m_cpuid_static() BIND(C)
95 IMPORT :: c_int
96 INTEGER(C_INT) :: m_cpuid_static
97 END FUNCTION m_cpuid_static
98
99 ! **********************************************************************************************
100 !> \brief Check OpenMP constructs if environment variable CP2K_OMP_TRACE is set
101 !> (see base/openmp_trace.c for the levels).
102 !> \return Number of OpenMP errors encountered (negative if OMPT disabled).
103 !> \par History
104 !> 11.2024 created [Hans Pabst]
105 ! **********************************************************************************************
106 FUNCTION m_omp_trace_issues() BIND(C, name="openmp_trace_issues")
107 IMPORT :: c_int
108 INTEGER(C_INT) :: m_omp_trace_issues
109 END FUNCTION m_omp_trace_issues
110 END INTERFACE
111
112 ! Flushing is enabled by default because without it crash reports can get lost.
113 ! For performance reasons it can be disabled via the input in &GLOBAL.
114 LOGICAL, SAVE, PUBLIC :: flush_should_flush = .true.
115
116 INTEGER(KIND=int_8), SAVE, PUBLIC :: m_memory_max = 0
117
118CONTAINS
119
120! **************************************************************************************************
121!> \brief flushes units if the &GLOBAL flag is set accordingly
122!> \param lunit ...
123!> \par History
124!> 10.2008 created [Joost VandeVondele]
125!> \note
126!> flushing might degrade performance significantly (30% and more)
127! **************************************************************************************************
128 SUBROUTINE m_flush(lunit)
129 INTEGER, INTENT(IN) :: lunit
130
131 IF (flush_should_flush) FLUSH (lunit)
132
133 END SUBROUTINE m_flush
134
135! **************************************************************************************************
136!> \brief returns time from a real-time clock, protected against rolling
137!> early/easily
138!> \return ...
139!> \par History
140!> 03.2006 created [Joost VandeVondele]
141!> \note
142!> same implementation for all machines.
143!> might still roll, if not called multiple times per count_max/count_rate
144! **************************************************************************************************
145 FUNCTION m_walltime() RESULT(wt)
146 REAL(kind=dp) :: wt
147
148#if defined(__LIBXS)
149 wt = libxs_timer_duration(0_int_8, libxs_timer_tick())
150#else
151 wt = omp_get_wtime()
152#endif
153 END FUNCTION m_walltime
154
155! **************************************************************************************************
156!> \brief reads /proc/cpuinfo if it exists (i.e. Linux) to return relevant info
157!> \param model_name as obtained from the 'model name' field, UNKNOWN otherwise
158! **************************************************************************************************
159 SUBROUTINE m_cpuinfo(model_name)
160 CHARACTER(LEN=default_string_length), INTENT(OUT) :: model_name
161
162 INTEGER, PARAMETER :: bufferlen = 2048
163
164 CHARACTER(LEN=bufferlen) :: buffer
165 INTEGER :: i, icol, iline, stat, unit_nr
166
167 model_name = "UNKNOWN"
168 buffer = ""
169 OPEN (newunit=unit_nr, file="/proc/cpuinfo", action="READ", status="OLD", access="STREAM", iostat=stat)
170 IF (stat == 0) THEN
171 DO i = 1, bufferlen
172 READ (unit_nr, END=999) buffer(I:I)
173 END DO
174999 CLOSE (unit_nr)
175 i = index(buffer, "model name")
176 IF (i > 0) THEN
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)
181 END IF
182 END IF
183 END SUBROUTINE m_cpuinfo
184
185! **************************************************************************************************
186!> \brief Target architecture or instruction set extension according to CPU-check at runtime.
187!> \return cpuid according to MACHINE_* integer-parameter.
188!> \par History
189!> 04.2019 created [Hans Pabst]
190!> 09.2024 update+arm [Hans Pabst]
191! **************************************************************************************************
192 PURE FUNCTION m_cpuid()
193 INTEGER :: m_cpuid
194#if !defined(__LIBXS)
196#else
197
198 INTEGER :: archid
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
209 END IF
210
211 IF (m_cpuid == machine_cpu_unknown) 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
220 END IF
221 END IF
222
223 IF (m_cpuid == machine_cpu_unknown .AND. libxs_target_arch_generic <= archid .AND. archid <= 2999) THEN
225 END IF
226#endif
227 END FUNCTION m_cpuid
228
229! **************************************************************************************************
230!> \brief Determine name of target architecture for a given CPUID.
231!> \param cpuid integer value (MACHINE_*)
232!> \return short name of ISA extension.
233!> \par History
234!> 06.2019 created [Hans Pabst]
235!> 09.2024 update+arm [Hans Pabst]
236! **************************************************************************************************
237 PURE FUNCTION m_cpuid_name(cpuid)
238 INTEGER, INTENT(IN), OPTIONAL :: cpuid
239 CHARACTER(LEN=default_string_length) :: m_cpuid_name
240
241 INTEGER :: isa
242
243 IF (PRESENT(cpuid)) THEN
244 isa = cpuid
245 ELSE
246 isa = m_cpuid()
247 END IF
248
249 SELECT CASE (isa)
251 m_cpuid_name = "generic"
252 CASE (machine_x86_sse4)
253 m_cpuid_name = "x86_sse4"
254 CASE (machine_x86_avx)
255 m_cpuid_name = "x86_avx"
256 CASE (machine_x86_avx2)
257 m_cpuid_name = "x86_avx2"
258 CASE (machine_x86_avx512)
259 m_cpuid_name = "x86_avx512"
260 CASE (machine_arm_arch64)
261 m_cpuid_name = "arm_arch64"
262 CASE (machine_arm_sve128)
263 m_cpuid_name = "arm_sve128"
264 CASE (machine_arm_sve256)
265 m_cpuid_name = "arm_sve256"
266 CASE (machine_arm_sve512)
267 m_cpuid_name = "arm_sve512"
268 CASE DEFAULT
269 m_cpuid_name = "unknown"
270 END SELECT
271 END FUNCTION m_cpuid_name
272
273! **************************************************************************************************
274!> \brief Determine vector-length for a given CPUID.
275!> \param cpuid integer value (MACHINE_*)
276!> \param typesize number of bytes of scalar type
277!> \return vector-length in number of elements.
278!> \par History
279!> 12.2024 created [Hans Pabst]
280! **************************************************************************************************
281 PURE FUNCTION m_cpuid_vlen(cpuid, typesize)
282 INTEGER, INTENT(IN), OPTIONAL :: cpuid, typesize
283 INTEGER :: m_cpuid_vlen
284
285 INTEGER :: isa, nbytes
286
287 IF (PRESENT(typesize)) THEN
288 nbytes = typesize
289 ELSE
290 nbytes = 8 ! double-precision
291 END IF
292
293 IF (0 < nbytes .AND. nbytes <= 16) THEN ! sanity check
294 IF (PRESENT(cpuid)) THEN
295 isa = cpuid
296 ELSE
297 isa = m_cpuid()
298 END IF
299
300 SELECT CASE (isa)
301 CASE (machine_x86_sse4)
302 m_cpuid_vlen = 16/nbytes
303 CASE (machine_arm_arch64) ! NEON
304 m_cpuid_vlen = 16/nbytes
305 CASE (machine_arm_sve128)
306 m_cpuid_vlen = 16/nbytes
307 CASE (machine_x86_avx)
308 m_cpuid_vlen = 32/nbytes
309 CASE (machine_x86_avx2)
310 m_cpuid_vlen = 32/nbytes
311 CASE (machine_arm_sve256)
312 m_cpuid_vlen = 32/nbytes
313 CASE (machine_x86_avx512)
314 m_cpuid_vlen = 64/nbytes
315 CASE (machine_arm_sve512)
316 m_cpuid_vlen = 64/nbytes
317 CASE DEFAULT ! unknown or generic
318 m_cpuid_vlen = 1 ! scalar
319 END SELECT
320 ELSE ! fallback
321 m_cpuid_vlen = 1 ! scalar
322 END IF
323 END FUNCTION m_cpuid_vlen
324
325! **************************************************************************************************
326!> \brief returns the energy used since some time in the past.
327!> The precise meaning depends on the infrastructure is available.
328!> In the cray_pm_energy case, this is the energy used by the node in kJ.
329!> \return ...
330!> \par History
331!> 09.2013 created [Joost VandeVondele, Ole Schuett]
332! **************************************************************************************************
333 FUNCTION m_energy() RESULT(wt)
334 REAL(kind=dp) :: wt
335
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")
340#else
341 wt = 0.0_dp ! fallback default
342#endif
343
344 END FUNCTION m_energy
345
346#if defined(__CRAY_PM_ACCEL_ENERGY) || defined(__CRAY_PM_ENERGY)
347! **************************************************************************************************
348!> \brief reads energy values from the sys-filesystem
349!> \param filename ...
350!> \return ...
351!> \par History
352!> 09.2013 created [Joost VandeVondele, Ole Schuett]
353! **************************************************************************************************
354 FUNCTION read_energy(filename) RESULT(wt)
355 CHARACTER(LEN=*) :: filename
356 REAL(kind=dp) :: wt
357
358 CHARACTER(LEN=80) :: data
359 INTEGER :: i, iostat, unit_nr
360 INTEGER(KIND=int_8) :: raw
361
362 OPEN (newunit=unit_nr, file=filename, action="READ", status="OLD", access="STREAM")
363 DO i = 1, 80
364 READ (unit_nr, END=999) DATA(I:I)
365 END DO
366999 CLOSE (unit_nr)
367 DATA(i:80) = ""
368 READ (DATA, *, iostat=iostat) raw
369 IF (iostat /= 0) THEN
370 wt = 0.0_dp
371 ELSE
372 ! convert from J to kJ
373 wt = raw/1000.0_dp
374 END IF
375 END FUNCTION read_energy
376#endif
377
378! **************************************************************************************************
379!> \brief Returns a human readable timestamp
380!> \param timestamp Timestamp string
381!> \par History
382!> 10.2009 created [Joost VandeVondele]
383!> 08.2025 modified [Matthias Krack]
384! **************************************************************************************************
385 SUBROUTINE m_timestamp(timestamp)
386 CHARACTER(LEN=timestamp_length), INTENT(OUT) :: timestamp
387
388 CHARACTER(LEN=10) :: time
389 CHARACTER(LEN=8) :: date
390
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)
394
395 END SUBROUTINE m_timestamp
396
397! **************************************************************************************************
398!> \brief Can be used to get a nice core
399! **************************************************************************************************
400 SUBROUTINE m_abort()
401 INTERFACE
402 SUBROUTINE abort() BIND(C, name="abort")
403 END SUBROUTINE abort
404 END INTERFACE
405
406 CALL abort()
407 END SUBROUTINE m_abort
408
409! **************************************************************************************************
410!> \brief Returns if a process is running on the local machine
411!> 1 if yes and 0 if not
412!> \param pid ...
413!> \return ...
414! **************************************************************************************************
415 FUNCTION m_procrun(pid) RESULT(run_on)
416 INTEGER, INTENT(IN) :: pid
417 INTEGER :: run_on
418
419 INTEGER :: istat
420 INTERFACE
421 FUNCTION kill(pid, sig) RESULT(errno) BIND(C, name="kill")
422 IMPORT
423 INTEGER(KIND=C_INT), VALUE :: pid, sig
424 INTEGER(KIND=C_INT) :: errno
425 END FUNCTION kill
426 END INTERFACE
427
428 ! If sig is 0, then no signal is sent, but error checking is still
429 ! performed; this can be used to check for the existence of a process
430 ! ID or process group ID.
431
432 istat = kill(pid=pid, sig=0)
433 IF (istat == 0) THEN
434 run_on = 1 ! no error, process exists
435 ELSE
436 run_on = 0 ! error, process probably does not exist
437 END IF
438 END FUNCTION m_procrun
439
440! **************************************************************************************************
441!> \brief Returns the total amount of memory [bytes] in use, if known, zero otherwise
442!> \param mem ...
443! **************************************************************************************************
444 SUBROUTINE m_memory(mem)
445
446 INTEGER(KIND=int_8), OPTIONAL, INTENT(OUT) :: mem
447 INTEGER(KIND=int_8) :: mem_local
448
449 ! __NO_STATM_ACCESS can be used to disable the stuff, if getpagesize
450 ! lead to linking errors or /proc/self/statm can not be opened
451 !
452#if defined(__NO_STATM_ACCESS)
453 mem_local = 0
454#else
455 INTEGER(KIND=int_8) :: m1, m2, m3
456 CHARACTER(LEN=80) :: data
457 INTEGER :: iostat, i, unit_nr
458
459 ! the size of a page, might not be available everywhere
460 INTERFACE
461 FUNCTION getpagesize() BIND(C, name="getpagesize") RESULT(RES)
462 IMPORT
463 INTEGER(C_INT) :: res
464 END FUNCTION getpagesize
465 END INTERFACE
466
467 ! reading from statm
468 !
469 mem_local = -1
470 DATA = ""
471 OPEN (newunit=unit_nr, file="/proc/self/statm", action="READ", status="OLD", access="STREAM")
472 DO i = 1, 80
473 READ (unit_nr, END=999) DATA(I:I)
474 END DO
475999 CLOSE (unit_nr)
476 DATA(i:80) = ""
477 ! m1 = total
478 ! m2 = resident
479 ! m3 = shared
480 READ (DATA, *, iostat=iostat) m1, m2, m3
481 IF (iostat /= 0) THEN
482 mem_local = 0
483 ELSE
484 mem_local = m2
485#if defined(__STATM_TOTAL)
486 mem_local = m1
487#endif
488#if defined(__STATM_RESIDENT)
489 mem_local = m2
490#endif
491 mem_local = mem_local*getpagesize()
492 END IF
493#endif
494
495 m_memory_max = max(mem_local, m_memory_max)
496 IF (PRESENT(mem)) mem = mem_local
497
498 END SUBROUTINE m_memory
499
500! **************************************************************************************************
501!> \brief get more detailed memory info, all units are bytes.
502!> the only 'useful' option is MemLikelyFree which is an estimate of remaining memory
503!> assumed to give info like /proc/meminfo while MeMLikelyFree is the amount of
504!> memory we're likely to be able to allocate, but not necessarily in one chunk
505!> zero means not available...
506!> \param MemTotal ...
507!> \param MemFree ...
508!> \param Buffers ...
509!> \param Cached ...
510!> \param Slab ...
511!> \param SReclaimable ...
512!> \param MemLikelyFree ...
513! **************************************************************************************************
514 SUBROUTINE m_memory_details(MemTotal, MemFree, Buffers, Cached, Slab, SReclaimable, MemLikelyFree)
515
516 INTEGER(KIND=int_8), OPTIONAL :: memtotal, memfree, buffers, cached, slab, sreclaimable, memlikelyfree
517
518 INTEGER, PARAMETER :: nbuffer = 10000
519 CHARACTER(LEN=Nbuffer) :: meminfo
520
521 INTEGER :: i, unit_nr
522 INTEGER(KIND=C_INT64_T) :: c_free, c_likely_free, c_total
523
524 INTERFACE
525 SUBROUTINE m_memory_details_c(total, free, likely_free) BIND(C, name="m_memory_details_c")
526 IMPORT :: c_int64_t
527 INTEGER(KIND=C_INT64_T) :: total, free, likely_free
528 END SUBROUTINE m_memory_details_c
529 END INTERFACE
530
531 memtotal = 0
532 memfree = 0
533 buffers = 0
534 cached = 0
535 slab = 0
536 sreclaimable = 0
537 memlikelyfree = 0
538 meminfo = ""
539
540 OPEN (newunit=unit_nr, file="/proc/meminfo", access="STREAM", err=902)
541 i = 0
542 DO
543 i = i + 1
544 IF (i > nbuffer) EXIT
545 READ (unit_nr, END=900, ERR=900) meminfo(i:i)
546 END DO
547900 CONTINUE
548 meminfo(i:nbuffer) = ""
549 CLOSE (unit_nr, err=902)
550902 CONTINUE
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:')
557 ! the kernel's own estimate (Linux 3.14 and later), since Cached includes memory that is not
558 ! reclaimable (Shmem, e.g., shared-memory segments of MPI)
559 memlikelyfree = get_field_value_in_bytes('MemAvailable:')
560 IF (memlikelyfree <= 0) memlikelyfree = memfree + buffers + cached + sreclaimable
561
562 ! no /proc/meminfo (macOS): ask the system directly, see machine_memory.c
563 IF (memtotal <= 0) THEN
564 CALL m_memory_details_c(c_total, c_free, c_likely_free)
565 memtotal = c_total
566 memfree = c_free
567 memlikelyfree = c_likely_free
568 END IF
569
570 CONTAINS
571
572! **************************************************************************************************
573!> \brief ...
574!> \param field ...
575!> \return ...
576! **************************************************************************************************
577 INTEGER(int_8) FUNCTION get_field_value_in_bytes(field)
578 CHARACTER(LEN=*) :: field
579
580 INTEGER :: start
581 INTEGER(KIND=int_8) :: value
582
583 get_field_value_in_bytes = 0
584 start = index(meminfo, field)
585 IF (start /= 0) THEN
586 start = start + len_trim(field)
587 IF (start < nbuffer) THEN
588 READ (meminfo(start:), *, err=999, END=999) value
589 ! XXXXXXX convert from Kb to bytes XXXXXXXX
590 get_field_value_in_bytes = value*1024
591999 CONTINUE
592 END IF
593 END IF
594 END FUNCTION get_field_value_in_bytes
595 END SUBROUTINE m_memory_details
596
597! **************************************************************************************************
598!> \brief ...
599!> \param hname ...
600! **************************************************************************************************
601 SUBROUTINE m_hostnm(hname)
602 CHARACTER(LEN=*), INTENT(OUT) :: hname
603 INTEGER :: istat, i
604 CHARACTER(LEN=default_path_length) :: buf
605
606 INTERFACE
607 FUNCTION gethostname(buf, buflen) BIND(C, name="gethostname") RESULT(errno)
608 IMPORT
609 CHARACTER(KIND=C_CHAR), DIMENSION(*) :: buf
610 INTEGER(KIND=C_INT), VALUE :: buflen
611 INTEGER(KIND=C_INT) :: errno
612 END FUNCTION gethostname
613 END INTERFACE
614
615 istat = gethostname(buf, len(buf))
616 IF (istat /= 0) THEN
617 WRITE (*, *) "m_hostnm failed"
618 CALL m_abort()
619 END IF
620 i = index(buf, c_null_char) - 1
621 hname = buf(1:i)
622 END SUBROUTINE m_hostnm
623
624! **************************************************************************************************
625!> \brief ...
626!> \param curdir ...
627! **************************************************************************************************
628 SUBROUTINE m_getcwd(curdir)
629 CHARACTER(LEN=*), INTENT(OUT) :: curdir
630 TYPE(c_ptr) :: stat
631 INTEGER :: i
632 CHARACTER(LEN=default_path_length), TARGET :: tmp
633
634 INTERFACE
635 FUNCTION getcwd(buf, buflen) BIND(C, name="getcwd") RESULT(stat)
636 IMPORT
637 CHARACTER(KIND=C_CHAR), DIMENSION(*) :: buf
638 INTEGER(KIND=C_INT), VALUE :: buflen
639 TYPE(c_ptr) :: stat
640 END FUNCTION getcwd
641 END INTERFACE
642
643 stat = getcwd(tmp, len(tmp))
644 IF (.NOT. c_associated(stat)) THEN
645 WRITE (*, *) "m_getcwd failed"
646 CALL m_abort()
647 END IF
648 i = index(tmp, c_null_char) - 1
649 curdir = tmp(1:i)
650 END SUBROUTINE m_getcwd
651
652! **************************************************************************************************
653!> \brief Returns the path of the running executable
654!> \param exe_path ...
655! **************************************************************************************************
656 SUBROUTINE m_getexe(exe_path)
657 CHARACTER(LEN=*), INTENT(OUT) :: exe_path
658
659 CHARACTER(LEN=default_path_length), TARGET :: tmp
660 INTEGER :: n
661
662 INTERFACE
663 FUNCTION readlink(path, buf, bufsiz) BIND(C, name="readlink") RESULT(nbytes)
664 IMPORT
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
669 END INTERFACE
670
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
673 exe_path = tmp(1:n)
674 ELSE
675 CALL get_command_argument(0, exe_path)
676 END IF
677 END SUBROUTINE m_getexe
678
679! **************************************************************************************************
680!> \brief ...
681!> \param dir ...
682!> \param ierror ...
683! **************************************************************************************************
684 SUBROUTINE m_chdir(dir, ierror)
685 CHARACTER(LEN=*), INTENT(IN) :: dir
686 INTEGER, INTENT(OUT) :: ierror
687
688 INTERFACE
689 FUNCTION chdir(path) BIND(C, name="chdir") RESULT(errno)
690 IMPORT
691 CHARACTER(KIND=C_CHAR), DIMENSION(*) :: path
692 INTEGER(KIND=C_INT) :: errno
693 END FUNCTION chdir
694 END INTERFACE
695
696 ierror = chdir(trim(dir)//c_null_char)
697 END SUBROUTINE m_chdir
698
699! **************************************************************************************************
700!> \brief ...
701!> \param pid ...
702! **************************************************************************************************
703 SUBROUTINE m_getpid(pid)
704 INTEGER, INTENT(OUT) :: pid
705
706 INTERFACE
707 FUNCTION getpid() BIND(C, name="getpid") RESULT(pid)
708 IMPORT
709 INTEGER(KIND=C_INT) :: pid
710 END FUNCTION getpid
711 END INTERFACE
712
713 pid = getpid()
714 END SUBROUTINE m_getpid
715
716! **************************************************************************************************
717!> \brief ...
718!> \param path ...
719!> \return ...
720! **************************************************************************************************
721 FUNCTION m_unlink(path) RESULT(istat)
722
723 CHARACTER(LEN=*), INTENT(IN) :: path
724
725 INTEGER :: istat
726
727 INTERFACE
728 FUNCTION unlink(path) BIND(C, name="unlink") RESULT(errno)
729 IMPORT
730 CHARACTER(KIND=C_CHAR), DIMENSION(*) :: path
731 INTEGER(KIND=C_INT) :: errno
732 END FUNCTION unlink
733 END INTERFACE
734
735 istat = unlink(trim(path)//c_null_char)
736 END FUNCTION m_unlink
737
738! **************************************************************************************************
739!> \brief ...
740!> \param source ...
741!> \param TARGET ...
742! **************************************************************************************************
743 SUBROUTINE m_mov(source, TARGET)
744
745 CHARACTER(LEN=*), INTENT(IN) :: source, target
746
747 INTEGER :: istat
748 LOGICAL :: src_exists
749
750 INTERFACE
751 FUNCTION rename(src, dest) BIND(C, name="rename") RESULT(errno)
752 IMPORT
753 CHARACTER(KIND=C_CHAR), DIMENSION(*) :: src, dest
754 INTEGER(KIND=C_INT) :: errno
755 END FUNCTION rename
756 END INTERFACE
757
758 IF (TARGET == source) THEN
759 WRITE (*, *) "Warning: m_mov ", trim(TARGET), " equals ", trim(source)
760 RETURN
761 END IF
762
763 ! first remove target (needed on windows)
764 istat = m_unlink(TARGET)
765 ! ignore istat of unlink
766
767 ! now move
768 istat = rename(trim(source)//c_null_char, trim(TARGET)//c_null_char)
769 IF (istat /= 0) THEN
770 ! Source already moved by a concurrent rank: benign, nothing to back up.
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"
776 CALL m_abort()
777 END IF
778 END SUBROUTINE m_mov
779
780! **************************************************************************************************
781!> \brief ...
782!> \param user ...
783! **************************************************************************************************
784 SUBROUTINE m_getlog(user)
785
786 CHARACTER(LEN=*), INTENT(OUT) :: user
787
788 INTEGER :: istat
789
790 ! on a posix system LOGNAME should be defined
791 CALL get_environment_variable("LOGNAME", value=user, status=istat)
792 ! nope, check alternative
793 IF (istat /= 0) THEN
794 CALL get_environment_variable("USER", value=user, status=istat)
795 END IF
796 ! nope, check alternative
797 IF (istat /= 0) THEN
798 CALL get_environment_variable("USERNAME", value=user, status=istat)
799 END IF
800 ! fall back
801 IF (istat /= 0) THEN
802 user = "<unknown>"
803 END IF
804
805 END SUBROUTINE m_getlog
806
807! **************************************************************************************************
808!> \brief Retrieve environment variable OMP_STACKSIZE
809!> \param omp_stacksize Value of OMP_STACKSIZE
810! **************************************************************************************************
811 SUBROUTINE m_omp_get_stacksize(omp_stacksize)
812 CHARACTER(LEN=*), INTENT(OUT) :: omp_stacksize
813
814 INTEGER :: istat
815
816 omp_stacksize = ""
817 CALL get_environment_variable("OMP_STACKSIZE", value=omp_stacksize, status=istat)
818 ! Fall back, if OMP_STACKSIZE is not set
819 IF (istat /= 0) omp_stacksize = "default"
820
821 END SUBROUTINE m_omp_get_stacksize
822
823END MODULE machine
Target architecture or instruction set extension according to compiler target flags.
Definition machine.F:94
Check OpenMP constructs if environment variable CP2K_OMP_TRACE is set (see base/openmp_trace....
Definition machine.F:106
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.
Definition kinds.F:23
integer, parameter, public int_8
Definition kinds.F:54
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
integer, parameter, public default_path_length
Definition kinds.F:58
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
integer, parameter, public machine_x86_avx
Definition machine.F:61
logical, save, public flush_should_flush
Definition machine.F:114
subroutine, public m_getpid(pid)
...
Definition machine.F:704
integer, parameter, public machine_arm_sve512
Definition machine.F:61
integer, parameter, public default_output_unit
Definition machine.F:49
integer, parameter, public machine_x86_sse4
Definition machine.F:61
subroutine, public m_memory(mem)
Returns the total amount of memory [bytes] in use, if known, zero otherwise.
Definition machine.F:445
integer, parameter, public machine_cpu_generic
Definition machine.F:61
integer, parameter, public default_input_unit
Definition machine.F:49
integer, parameter, public timestamp_length
Definition machine.F:49
subroutine, public m_flush(lunit)
flushes units if the &GLOBAL flag is set accordingly
Definition machine.F:129
integer, parameter, public machine_arm_sve256
Definition machine.F:61
integer, parameter, public machine_x86_avx2
Definition machine.F:61
subroutine, public m_abort()
Can be used to get a nice core.
Definition machine.F:401
subroutine, public m_timestamp(timestamp)
Returns a human readable timestamp.
Definition machine.F:386
integer function, public m_procrun(pid)
Returns if a process is running on the local machine 1 if yes and 0 if not.
Definition machine.F:416
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...
Definition machine.F:515
pure integer function, public m_cpuid()
Target architecture or instruction set extension according to CPU-check at runtime.
Definition machine.F:193
subroutine, public m_getcwd(curdir)
...
Definition machine.F:629
integer, parameter, public machine_x86
Definition machine.F:61
subroutine, public m_chdir(dir, ierror)
...
Definition machine.F:685
pure integer function, public m_cpuid_vlen(cpuid, typesize)
Determine vector-length for a given CPUID.
Definition machine.F:282
real(kind=dp) function, public m_energy()
returns the energy used since some time in the past. The precise meaning depends on the infrastructur...
Definition machine.F:334
integer, parameter, public machine_arm
Definition machine.F:61
integer function, public m_unlink(path)
...
Definition machine.F:722
subroutine, public m_cpuinfo(model_name)
reads /proc/cpuinfo if it exists (i.e. Linux) to return relevant info
Definition machine.F:160
subroutine, public m_mov(source, target)
...
Definition machine.F:744
integer, parameter, public default_error_unit
Definition machine.F:49
pure character(len=default_string_length) function, public m_cpuid_name(cpuid)
Determine name of target architecture for a given CPUID.
Definition machine.F:238
integer, parameter, public machine_x86_avx512
Definition machine.F:61
subroutine, public m_getexe(exe_path)
Returns the path of the running executable.
Definition machine.F:657
integer, parameter, public machine_arm_arch64
Definition machine.F:61
subroutine, public m_omp_get_stacksize(omp_stacksize)
Retrieve environment variable OMP_STACKSIZE.
Definition machine.F:812
integer, parameter, public machine_cpu_unknown
Definition machine.F:61
integer(kind=int_8), save, public m_memory_max
Definition machine.F:116
integer, parameter, public machine_arm_sve128
Definition machine.F:61
subroutine, public m_getlog(user)
...
Definition machine.F:785
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Definition machine.F:146
subroutine, public m_hostnm(hname)
...
Definition machine.F:602