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