[Openmp-commits] [openmp] [openmp] using h/w clock tick to measure time instead of gettimeofday… (PR #85474)

David CARLIER via Openmp-commits openmp-commits at lists.llvm.org
Sat Mar 23 13:01:14 PDT 2024


https://github.com/devnexen updated https://github.com/llvm/llvm-project/pull/85474

>From 4b7a636ad2e1f78727b42f6920e521bf3a1f9a93 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Fri, 15 Mar 2024 22:00:27 +0000
Subject: [PATCH 1/4] [openmp] using h/w clock tick to measure time instead of
 gettimeofday on arm64.

using counter timer frequency register for that purpose, available in user mode.
---
 openmp/runtime/src/kmp_lock.cpp  | 5 ++++-
 openmp/runtime/src/z_Linux_asm.S | 9 +++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/openmp/runtime/src/kmp_lock.cpp b/openmp/runtime/src/kmp_lock.cpp
index 0ad14f862bcb9b..26f4dcdb4d2644 100644
--- a/openmp/runtime/src/kmp_lock.cpp
+++ b/openmp/runtime/src/kmp_lock.cpp
@@ -2644,11 +2644,14 @@ static void __kmp_set_drdpa_lock_flags(kmp_drdpa_lock_t *lck,
 #define __kmp_tsc() __kmp_hardware_timestamp()
 // Runtime's default backoff parameters
 kmp_backoff_t __kmp_spin_backoff_params = {1, 4096, 100};
+#elif KMP_ARCH_AARCH64
+#define __kmp_tsc() __kmp_hardware_timestamp()
+kmp_backoff_t __kmp_spin_backoff_params = {1, 256, 100};
 #else
 // Use nanoseconds for other platforms
 extern kmp_uint64 __kmp_now_nsec();
-kmp_backoff_t __kmp_spin_backoff_params = {1, 256, 100};
 #define __kmp_tsc() __kmp_now_nsec()
+kmp_backoff_t __kmp_spin_backoff_params = {1, 256, 100};
 #endif
 
 // A useful predicate for dealing with timestamps that may wrap.
diff --git a/openmp/runtime/src/z_Linux_asm.S b/openmp/runtime/src/z_Linux_asm.S
index 14987c298fa5f9..af147292599bab 100644
--- a/openmp/runtime/src/z_Linux_asm.S
+++ b/openmp/runtime/src/z_Linux_asm.S
@@ -1360,6 +1360,15 @@ KMP_LABEL(kmp_1):
 	DEBUG_INFO __kmp_invoke_microtask
 // -- End  __kmp_invoke_microtask
 
+// kmp_uint64
+// __kmp_hardware_timestamp(void)
+   PROC __kmp_hardware_timestamp
+   mrs x0, cntfrq_el0
+   mov x30, x0
+   ret
+   DEBUG_INFO __kmp_hardware_timestamp
+// -- End  __kmp_hardware_timestamp
+
 #endif /* (KMP_OS_LINUX || KMP_OS_DARWIN || KMP_OS_WINDOWS) && KMP_ARCH_AARCH64 */
 
 #if (KMP_OS_LINUX || KMP_OS_DARWIN || KMP_OS_WINDOWS) && KMP_ARCH_ARM

>From 178418bfccea1a811fe74ac0499729bcca0d8500 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Sat, 16 Mar 2024 21:16:11 +0000
Subject: [PATCH 2/4] fix __kmp_hardware_timestamp calculation.

we need both frequency and counter in fact.
---
 openmp/runtime/src/z_Linux_asm.S | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/openmp/runtime/src/z_Linux_asm.S b/openmp/runtime/src/z_Linux_asm.S
index af147292599bab..5f0d42d11736f2 100644
--- a/openmp/runtime/src/z_Linux_asm.S
+++ b/openmp/runtime/src/z_Linux_asm.S
@@ -1362,9 +1362,14 @@ KMP_LABEL(kmp_1):
 
 // kmp_uint64
 // __kmp_hardware_timestamp(void)
+// we load the current frequency of the system counter
+// then the system counter. frequency * counter gives
+// current time
    PROC __kmp_hardware_timestamp
    mrs x0, cntfrq_el0
-   mov x30, x0
+   mrs x1, cntvct_el0
+   mul x2, x1, x0
+   mov x30, x2
    ret
    DEBUG_INFO __kmp_hardware_timestamp
 // -- End  __kmp_hardware_timestamp

>From 91b4160bb16ea930c35a6df8cf16c07a703c5574 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Sat, 16 Mar 2024 23:35:05 +0000
Subject: [PATCH 3/4] disabling for macos

---
 openmp/runtime/src/kmp_lock.cpp  | 2 +-
 openmp/runtime/src/z_Linux_asm.S | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/openmp/runtime/src/kmp_lock.cpp b/openmp/runtime/src/kmp_lock.cpp
index 26f4dcdb4d2644..2c04a2436e139d 100644
--- a/openmp/runtime/src/kmp_lock.cpp
+++ b/openmp/runtime/src/kmp_lock.cpp
@@ -2644,7 +2644,7 @@ static void __kmp_set_drdpa_lock_flags(kmp_drdpa_lock_t *lck,
 #define __kmp_tsc() __kmp_hardware_timestamp()
 // Runtime's default backoff parameters
 kmp_backoff_t __kmp_spin_backoff_params = {1, 4096, 100};
-#elif KMP_ARCH_AARCH64
+#elif KMP_ARCH_AARCH64 && !KMP_OS_DARWIN
 #define __kmp_tsc() __kmp_hardware_timestamp()
 kmp_backoff_t __kmp_spin_backoff_params = {1, 256, 100};
 #else
diff --git a/openmp/runtime/src/z_Linux_asm.S b/openmp/runtime/src/z_Linux_asm.S
index 5f0d42d11736f2..a861b397c04a0a 100644
--- a/openmp/runtime/src/z_Linux_asm.S
+++ b/openmp/runtime/src/z_Linux_asm.S
@@ -1360,6 +1360,7 @@ KMP_LABEL(kmp_1):
 	DEBUG_INFO __kmp_invoke_microtask
 // -- End  __kmp_invoke_microtask
 
+#if !KMP_OS_DARWIN
 // kmp_uint64
 // __kmp_hardware_timestamp(void)
 // we load the current frequency of the system counter
@@ -1373,6 +1374,7 @@ KMP_LABEL(kmp_1):
    ret
    DEBUG_INFO __kmp_hardware_timestamp
 // -- End  __kmp_hardware_timestamp
+#endif
 
 #endif /* (KMP_OS_LINUX || KMP_OS_DARWIN || KMP_OS_WINDOWS) && KMP_ARCH_AARCH64 */
 

>From c4b7c575b91b5c30c161aaebe2152ce538ff1d47 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Fri, 15 Mar 2024 22:00:27 +0000
Subject: [PATCH 4/4] [openmp] using h/w clock tick to measure time instead of
 gettimeofday on Linux/arm64.

using counter timer frequency register for that purpose, available in user mode.
---
 openmp/runtime/src/kmp_lock.cpp  | 2 +-
 openmp/runtime/src/z_Linux_asm.S | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/openmp/runtime/src/kmp_lock.cpp b/openmp/runtime/src/kmp_lock.cpp
index 2c04a2436e139d..42210780606ad7 100644
--- a/openmp/runtime/src/kmp_lock.cpp
+++ b/openmp/runtime/src/kmp_lock.cpp
@@ -2644,7 +2644,7 @@ static void __kmp_set_drdpa_lock_flags(kmp_drdpa_lock_t *lck,
 #define __kmp_tsc() __kmp_hardware_timestamp()
 // Runtime's default backoff parameters
 kmp_backoff_t __kmp_spin_backoff_params = {1, 4096, 100};
-#elif KMP_ARCH_AARCH64 && !KMP_OS_DARWIN
+#elif KMP_ARCH_AARCH64 && !KMP_OS_DARWIN && !KMP_OS_WINDOWS
 #define __kmp_tsc() __kmp_hardware_timestamp()
 kmp_backoff_t __kmp_spin_backoff_params = {1, 256, 100};
 #else
diff --git a/openmp/runtime/src/z_Linux_asm.S b/openmp/runtime/src/z_Linux_asm.S
index a861b397c04a0a..e9d2530583d894 100644
--- a/openmp/runtime/src/z_Linux_asm.S
+++ b/openmp/runtime/src/z_Linux_asm.S
@@ -1360,7 +1360,7 @@ KMP_LABEL(kmp_1):
 	DEBUG_INFO __kmp_invoke_microtask
 // -- End  __kmp_invoke_microtask
 
-#if !KMP_OS_DARWIN
+#if !KMP_OS_DARWIN && !KMP_OS_WINDOWS
 // kmp_uint64
 // __kmp_hardware_timestamp(void)
 // we load the current frequency of the system counter



More information about the Openmp-commits mailing list