public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [patch] libgompd: Add thread handles
@ 2022-09-27  1:12 Ahmed Sayed Mousse
  2022-09-27  1:20 ` Ahmed Sayed Mousse
  0 siblings, 1 reply; 9+ messages in thread
From: Ahmed Sayed Mousse @ 2022-09-27  1:12 UTC (permalink / raw)
  To: gcc-patches; +Cc: jakub


[-- Attachment #1.1: Type: text/plain, Size: 485 bytes --]

/This patch is the initial implementation of OpenMP-API specs book section //20.5.5 with title "Thread Handles". //libgomp/ChangeLog ///2022-07-01 Ahmed Sayed <ahmedsayedmousse@gmail.com> ///* Makefile.am (libgompd_la_SOURCES): Add ompd-threads.c.///* Makefile.in: Regenerate. * team.c (gomp_free_thread): Call 
ompd_bp_thread_end./* ompd-support.c (gompd_thread_initial_tls_bias): 
New variable. (gompd_load): Initialize gompd_thread_initial_tls_bias. * 
ompd-threads.c: New file.///

[-- Attachment #2: diff.txt --]
[-- Type: text/plain, Size: 10889 bytes --]

diff --git a/libgomp/Makefile.am b/libgomp/Makefile.am
index 6d913a93e7f..23f5bede1bf 100644
--- a/libgomp/Makefile.am
+++ b/libgomp/Makefile.am
@@ -94,7 +94,7 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c error.c \
 	priority_queue.c affinity-fmt.c teams.c allocator.c oacc-profiling.c \
 	oacc-target.c ompd-support.c
 
-libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c
+libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c ompd-threads.c
 
 include $(top_srcdir)/plugin/Makefrag.am
 
diff --git a/libgomp/Makefile.in b/libgomp/Makefile.in
index 40f896b5f03..7acdcbf31d5 100644
--- a/libgomp/Makefile.in
+++ b/libgomp/Makefile.in
@@ -233,7 +233,8 @@ am_libgomp_la_OBJECTS = alloc.lo atomic.lo barrier.lo critical.lo \
 	affinity-fmt.lo teams.lo allocator.lo oacc-profiling.lo \
 	oacc-target.lo ompd-support.lo $(am__objects_1)
 libgomp_la_OBJECTS = $(am_libgomp_la_OBJECTS)
-am_libgompd_la_OBJECTS = ompd-init.lo ompd-helper.lo ompd-icv.lo
+am_libgompd_la_OBJECTS = ompd-init.lo ompd-helper.lo ompd-icv.lo \
+	ompd-threads.lo
 libgompd_la_OBJECTS = $(am_libgompd_la_OBJECTS)
 AM_V_P = $(am__v_P_@AM_V@)
 am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
@@ -583,7 +584,7 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c \
 	oacc-async.c oacc-plugin.c oacc-cuda.c priority_queue.c \
 	affinity-fmt.c teams.c allocator.c oacc-profiling.c \
 	oacc-target.c ompd-support.c $(am__append_7)
-libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c
+libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c ompd-threads.c
 
 # Nvidia PTX OpenACC plugin.
 @PLUGIN_NVPTX_TRUE@libgomp_plugin_nvptx_version_info = -version-info $(libtool_VERSION)
@@ -801,6 +802,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-icv.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-init.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-support.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-threads.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ordered.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/parallel.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/priority_queue.Plo@am__quote@
diff --git a/libgomp/ompd-support.c b/libgomp/ompd-support.c
index 27c5ad148e0..5b1afd37788 100644
--- a/libgomp/ompd-support.c
+++ b/libgomp/ompd-support.c
@@ -33,6 +33,8 @@ const unsigned short gompd_sizeof_gomp_thread_handle
   __attribute__ ((used)) OMPD_SECTION = 0;
 #endif
 
+unsigned long gompd_thread_initial_tls_bias __attribute__ ((used));
+
 /* Get offset of the member m in struct t.  */
 #define gompd_get_offset(t, m) \
   const unsigned short gompd_access_##t##_##m __attribute__ ((used)) \
@@ -67,6 +69,11 @@ gompd_load (void)
   gompd_state |= OMPD_ENABLED;
   ompd_dll_locations = &ompd_dll_locations_array[0];
   ompd_dll_locations_valid ();
+
+  #if defined(LIBGOMP_USE_PTHREADS) && !defined(GOMP_NEEDS_THREAD_HANDLE)
+  gompd_thread_initial_tls_bias = (unsigned long) ((char *) &gomp_tls_data
+						   - (char *) pthread_self ());
+  #endif
 }
 
 #ifndef __ELF__
diff --git a/libgomp/ompd-threads.c b/libgomp/ompd-threads.c
new file mode 100644
index 00000000000..723ef740181
--- /dev/null
+++ b/libgomp/ompd-threads.c
@@ -0,0 +1,222 @@
+/* Copyright (C) The GNU Toolchain Authors.
+   Contributed by Ahmed Sayed <ahmedsayedmousse@gmail.com>.
+   This file is part of the GNU Offloading and Multi Processing Library
+   (libgomp).
+
+   Libgomp is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
+   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+   more details.
+
+   Under Section 7 of GPL version 3, you are granted additional
+   permissions described in the GCC Runtime Library Exception, version
+   3.1, as published by the Free Software Foundation.
+
+   You should have received a copy of the GNU General Public License and
+   a copy of the GCC Runtime Library Exception along with this program;
+   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/* This file contains the implementation of functions defined in
+   Section 5.5 ThreadHandles. */
+
+
+#include "ompd-helper.h"
+
+ompd_rc_t
+ompd_get_thread_in_parallel (ompd_parallel_handle_t *parallel_handle,
+			     int thread_num,
+			     ompd_thread_handle_t **thread_handle)
+{
+
+  if (parallel_handle == NULL)
+    return ompd_rc_stale_handle;
+  CHECK (parallel_handle->ah);
+
+  ompd_address_space_context_t *context = parallel_handle->ah->context;
+  ompd_rc_t ret;
+
+  ompd_word_t team_size_var = 1;
+  if (parallel_handle->th.address)
+    gompd_get_team_size (parallel_handle, &team_size_var);
+
+  if (thread_num < 0 || thread_num >= team_size_var)
+    return ompd_rc_bad_input;
+
+  ompd_word_t temp_offset;
+  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
+  ompd_addr_t temp_addr;
+
+  ACCESS_VALUE (context, NULL, "gompd_access_gomp_thread_pool_threads",
+		temp_offset, 1, ret, symbol_addr, temp_symbol_addr, temp_addr);
+
+  symbol_addr.address += thread_num * target_sizes.sizeof_pointer;
+
+  DEREFERENCE (context, NULL, symbol_addr, target_sizes.sizeof_pointer, 1,
+	       temp_addr, ret, 1);
+
+  ret = callbacks->alloc_memory (sizeof (ompd_thread_handle_t),
+				 (void **) thread_handle);
+
+  CHECK_RET (ret);
+
+  if (symbol_addr.address == 0)
+    return ompd_rc_unsupported;
+
+  (*thread_handle)->th = symbol_addr;
+  (*thread_handle)->ah = parallel_handle->ah;
+  return ret;
+}
+
+/* The ompd_get_thread_handle function that maps a native thread to an
+   OMPD thread handle.  */
+
+ompd_rc_t
+ompd_get_thread_handle (ompd_address_space_handle_t *handle,
+			ompd_thread_id_t kind, ompd_size_t sizeof_thread_id,
+			const void *thread_id,
+			ompd_thread_handle_t **thread_handle)
+{
+  CHECK (handle);
+  if (kind != OMPD_THREAD_ID_PTHREAD)
+    return ompd_rc_unsupported;
+
+  ompd_address_space_context_t *context = handle->context;
+  ompd_thread_context_t *tcontext;
+  ompd_rc_t ret;
+
+  ret = callbacks->get_thread_context_for_thread_id (context, kind,
+						     sizeof_thread_id,
+						     thread_id, &tcontext);
+  CHECK_RET (ret);
+
+  ompd_size_t temp_symbol_size, symbol_size;
+  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
+
+  GET_VALUE (context, NULL, "gompd_sizeof_gomp_thread", symbol_size,
+	     temp_symbol_size, target_sizes.sizeof_short, 1, ret,
+	     temp_symbol_addr);
+
+  GET_VALUE (context, tcontext, "gomp_tls_data", symbol_addr.address,
+	     temp_symbol_addr.address, symbol_size, 1, ret, symbol_addr);
+
+  ret = callbacks->alloc_memory (sizeof (ompd_thread_handle_t),
+				 (void **) thread_handle);
+
+  CHECK_RET (ret);
+
+  (*thread_handle)->ah = handle;
+  (*thread_handle)->th = symbol_addr;
+  (*thread_handle)->thread_context = tcontext;
+  return ret;
+}
+
+
+ompd_rc_t
+ompd_rel_thread_handle (ompd_thread_handle_t *thread_handle)
+{
+  if (thread_handle == NULL)
+    return ompd_rc_stale_handle;
+
+  ompd_rc_t ret;
+  ret = callbacks->free_memory ((void *) thread_handle);
+  if (ret != ompd_rc_ok)
+  return ret;
+
+  return ompd_rc_ok;
+}
+
+
+/* Return -1, 0 or 1 for thread_handle_1 <, == or > thread_handle_2.  */
+ompd_rc_t
+ompd_thread_handle_compare (ompd_thread_handle_t *thread_handle_1,
+			    ompd_thread_handle_t *thread_handle_2,
+			    int	*cmp_value)
+{
+
+  if (thread_handle_1 == NULL || thread_handle_2 == NULL)
+    return ompd_rc_stale_handle;
+  if (cmp_value == NULL)
+    return ompd_rc_bad_input;
+  if (thread_handle_1->ah->kind != thread_handle_2->ah->kind)
+    return ompd_rc_bad_input;
+
+  if (thread_handle_1->th.address < thread_handle_2->th.address)
+    *cmp_value = -1;
+  else if (thread_handle_1->th.address > thread_handle_2->th.address)
+    *cmp_value = 1;
+  else
+    *cmp_value = 0;
+
+  return ompd_rc_ok;
+}
+
+
+ompd_rc_t
+ompd_get_thread_id (ompd_thread_handle_t *thread_handle, ompd_thread_id_t kind,
+		    ompd_size_t sizeof_thread_id, void *thread_id)
+{
+  if (kind != OMPD_THREAD_ID_PTHREAD)
+    return ompd_rc_unsupported;
+  if (thread_id == NULL)
+    return ompd_rc_bad_input;
+  if (thread_handle == NULL)
+    return ompd_rc_stale_handle;
+
+  CHECK (thread_handle->ah);
+  ompd_address_space_context_t *context = thread_handle->ah->context;
+
+  ompd_rc_t ret;
+  ompd_address_t taddr = thread_handle->th;
+  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
+  ompd_size_t temp_symbol_size, symbol_size;
+  ompd_word_t temp_offset, offset;
+
+  GET_VALUE (context, NULL, "gompd_sizeof_gomp_thread_handle", symbol_size,
+	     temp_symbol_size, target_sizes.sizeof_short, 1, ret, symbol_addr);
+
+  if (symbol_size == 0)
+    {
+      GET_VALUE (context, NULL, "gompd_thread_initial_tls_bias", offset,
+		 temp_offset, target_sizes.sizeof_long, 1, ret, symbol_addr);
+
+      ret = callbacks->symbol_addr_lookup (context, NULL,"gomp_tls_data",
+					   &symbol_addr, NULL);
+      ret = callbacks->device_to_host (context, &temp_symbol_addr.address,
+				       target_sizes.sizeof_long_long, 1,
+				       &symbol_addr.address);
+      CHECK_RET (ret);
+
+      taddr.address = symbol_addr.address + offset;
+      ret = callbacks->read_memory (context, NULL, &taddr,
+				    target_sizes.sizeof_long_long, thread_id);
+    }
+  else
+    {
+      if (sizeof_thread_id != symbol_size)
+        return ompd_rc_bad_input;
+
+      GET_VALUE (context, NULL, "gompd_access_gomp_thread_handle", offset,
+		 temp_offset, target_sizes.sizeof_short, 1, ret, symbol_addr);
+      taddr.address += offset;
+
+      ret = callbacks->read_memory (context, NULL, &taddr, symbol_size,
+				    thread_id);
+    }
+  return ret;
+}
+
+
+/* OMPD doesn't support GPUs for now.  */
+ompd_rc_t ompd_get_device_from_thread (ompd_thread_handle_t *thread_handle,
+				       ompd_address_space_handle_t **device)
+{
+  if (thread_handle == NULL)
+    return ompd_rc_stale_handle;
+  return ompd_rc_unsupported;
+}
diff --git a/libgomp/team.c b/libgomp/team.c
index d53246961b7..9a84dc18bdb 100644
--- a/libgomp/team.c
+++ b/libgomp/team.c
@@ -77,6 +77,7 @@ gomp_thread_start (void *xdata)
   void *local_data;
 
   ompd_bp_thread_begin ();
+
 #if defined HAVE_TLS || defined USE_EMUTLS
   thr = &gomp_tls_data;
 #else
@@ -313,6 +314,8 @@ gomp_free_thread (void *arg __attribute__((unused)))
       gomp_end_task ();
       free (task);
     }
+
+  ompd_bp_thread_end ();
 }
 
 /* Launch a team.  */

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [patch] libgompd: Add thread handles
  2022-09-27  1:12 [patch] libgompd: Add thread handles Ahmed Sayed Mousse
@ 2022-09-27  1:20 ` Ahmed Sayed Mousse
  2022-09-27  6:40   ` Bernhard Reutner-Fischer
  0 siblings, 1 reply; 9+ messages in thread
From: Ahmed Sayed Mousse @ 2022-09-27  1:20 UTC (permalink / raw)
  To: gcc-patches; +Cc: jakub

[-- Attachment #1: Type: text/plain, Size: 620 bytes --]

For some reason my mail client keeps missing things up. Maybe it is my 
fault.

Anyway this is it again with the right format I hope.

/This patch is the initial implementation of OpenMP-API specs book section //20.5.5 with title "Thread Handles". //libgomp/ChangeLog ///2022-07-01 Ahmed Sayed <ahmedsayedmousse@gmail.com> ///* Makefile.am (libgompd_la_SOURCES): Add ompd-threads.c.///* Makefile.in: Regenerate. * team.c (gomp_free_thread): Call 
ompd_bp_thread_end./* ompd-support.c (gompd_thread_initial_tls_bias): 
New variable. (gompd_load): Initialize gompd_thread_initial_tls_bias. * 
ompd-threads.c: New file.///

[-- Attachment #2: diff.txt --]
[-- Type: text/plain, Size: 10889 bytes --]

diff --git a/libgomp/Makefile.am b/libgomp/Makefile.am
index 6d913a93e7f..23f5bede1bf 100644
--- a/libgomp/Makefile.am
+++ b/libgomp/Makefile.am
@@ -94,7 +94,7 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c error.c \
 	priority_queue.c affinity-fmt.c teams.c allocator.c oacc-profiling.c \
 	oacc-target.c ompd-support.c
 
-libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c
+libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c ompd-threads.c
 
 include $(top_srcdir)/plugin/Makefrag.am
 
diff --git a/libgomp/Makefile.in b/libgomp/Makefile.in
index 40f896b5f03..7acdcbf31d5 100644
--- a/libgomp/Makefile.in
+++ b/libgomp/Makefile.in
@@ -233,7 +233,8 @@ am_libgomp_la_OBJECTS = alloc.lo atomic.lo barrier.lo critical.lo \
 	affinity-fmt.lo teams.lo allocator.lo oacc-profiling.lo \
 	oacc-target.lo ompd-support.lo $(am__objects_1)
 libgomp_la_OBJECTS = $(am_libgomp_la_OBJECTS)
-am_libgompd_la_OBJECTS = ompd-init.lo ompd-helper.lo ompd-icv.lo
+am_libgompd_la_OBJECTS = ompd-init.lo ompd-helper.lo ompd-icv.lo \
+	ompd-threads.lo
 libgompd_la_OBJECTS = $(am_libgompd_la_OBJECTS)
 AM_V_P = $(am__v_P_@AM_V@)
 am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
@@ -583,7 +584,7 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c \
 	oacc-async.c oacc-plugin.c oacc-cuda.c priority_queue.c \
 	affinity-fmt.c teams.c allocator.c oacc-profiling.c \
 	oacc-target.c ompd-support.c $(am__append_7)
-libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c
+libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c ompd-threads.c
 
 # Nvidia PTX OpenACC plugin.
 @PLUGIN_NVPTX_TRUE@libgomp_plugin_nvptx_version_info = -version-info $(libtool_VERSION)
@@ -801,6 +802,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-icv.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-init.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-support.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-threads.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ordered.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/parallel.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/priority_queue.Plo@am__quote@
diff --git a/libgomp/ompd-support.c b/libgomp/ompd-support.c
index 27c5ad148e0..5b1afd37788 100644
--- a/libgomp/ompd-support.c
+++ b/libgomp/ompd-support.c
@@ -33,6 +33,8 @@ const unsigned short gompd_sizeof_gomp_thread_handle
   __attribute__ ((used)) OMPD_SECTION = 0;
 #endif
 
+unsigned long gompd_thread_initial_tls_bias __attribute__ ((used));
+
 /* Get offset of the member m in struct t.  */
 #define gompd_get_offset(t, m) \
   const unsigned short gompd_access_##t##_##m __attribute__ ((used)) \
@@ -67,6 +69,11 @@ gompd_load (void)
   gompd_state |= OMPD_ENABLED;
   ompd_dll_locations = &ompd_dll_locations_array[0];
   ompd_dll_locations_valid ();
+
+  #if defined(LIBGOMP_USE_PTHREADS) && !defined(GOMP_NEEDS_THREAD_HANDLE)
+  gompd_thread_initial_tls_bias = (unsigned long) ((char *) &gomp_tls_data
+						   - (char *) pthread_self ());
+  #endif
 }
 
 #ifndef __ELF__
diff --git a/libgomp/ompd-threads.c b/libgomp/ompd-threads.c
new file mode 100644
index 00000000000..723ef740181
--- /dev/null
+++ b/libgomp/ompd-threads.c
@@ -0,0 +1,222 @@
+/* Copyright (C) The GNU Toolchain Authors.
+   Contributed by Ahmed Sayed <ahmedsayedmousse@gmail.com>.
+   This file is part of the GNU Offloading and Multi Processing Library
+   (libgomp).
+
+   Libgomp is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
+   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+   more details.
+
+   Under Section 7 of GPL version 3, you are granted additional
+   permissions described in the GCC Runtime Library Exception, version
+   3.1, as published by the Free Software Foundation.
+
+   You should have received a copy of the GNU General Public License and
+   a copy of the GCC Runtime Library Exception along with this program;
+   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/* This file contains the implementation of functions defined in
+   Section 5.5 ThreadHandles. */
+
+
+#include "ompd-helper.h"
+
+ompd_rc_t
+ompd_get_thread_in_parallel (ompd_parallel_handle_t *parallel_handle,
+			     int thread_num,
+			     ompd_thread_handle_t **thread_handle)
+{
+
+  if (parallel_handle == NULL)
+    return ompd_rc_stale_handle;
+  CHECK (parallel_handle->ah);
+
+  ompd_address_space_context_t *context = parallel_handle->ah->context;
+  ompd_rc_t ret;
+
+  ompd_word_t team_size_var = 1;
+  if (parallel_handle->th.address)
+    gompd_get_team_size (parallel_handle, &team_size_var);
+
+  if (thread_num < 0 || thread_num >= team_size_var)
+    return ompd_rc_bad_input;
+
+  ompd_word_t temp_offset;
+  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
+  ompd_addr_t temp_addr;
+
+  ACCESS_VALUE (context, NULL, "gompd_access_gomp_thread_pool_threads",
+		temp_offset, 1, ret, symbol_addr, temp_symbol_addr, temp_addr);
+
+  symbol_addr.address += thread_num * target_sizes.sizeof_pointer;
+
+  DEREFERENCE (context, NULL, symbol_addr, target_sizes.sizeof_pointer, 1,
+	       temp_addr, ret, 1);
+
+  ret = callbacks->alloc_memory (sizeof (ompd_thread_handle_t),
+				 (void **) thread_handle);
+
+  CHECK_RET (ret);
+
+  if (symbol_addr.address == 0)
+    return ompd_rc_unsupported;
+
+  (*thread_handle)->th = symbol_addr;
+  (*thread_handle)->ah = parallel_handle->ah;
+  return ret;
+}
+
+/* The ompd_get_thread_handle function that maps a native thread to an
+   OMPD thread handle.  */
+
+ompd_rc_t
+ompd_get_thread_handle (ompd_address_space_handle_t *handle,
+			ompd_thread_id_t kind, ompd_size_t sizeof_thread_id,
+			const void *thread_id,
+			ompd_thread_handle_t **thread_handle)
+{
+  CHECK (handle);
+  if (kind != OMPD_THREAD_ID_PTHREAD)
+    return ompd_rc_unsupported;
+
+  ompd_address_space_context_t *context = handle->context;
+  ompd_thread_context_t *tcontext;
+  ompd_rc_t ret;
+
+  ret = callbacks->get_thread_context_for_thread_id (context, kind,
+						     sizeof_thread_id,
+						     thread_id, &tcontext);
+  CHECK_RET (ret);
+
+  ompd_size_t temp_symbol_size, symbol_size;
+  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
+
+  GET_VALUE (context, NULL, "gompd_sizeof_gomp_thread", symbol_size,
+	     temp_symbol_size, target_sizes.sizeof_short, 1, ret,
+	     temp_symbol_addr);
+
+  GET_VALUE (context, tcontext, "gomp_tls_data", symbol_addr.address,
+	     temp_symbol_addr.address, symbol_size, 1, ret, symbol_addr);
+
+  ret = callbacks->alloc_memory (sizeof (ompd_thread_handle_t),
+				 (void **) thread_handle);
+
+  CHECK_RET (ret);
+
+  (*thread_handle)->ah = handle;
+  (*thread_handle)->th = symbol_addr;
+  (*thread_handle)->thread_context = tcontext;
+  return ret;
+}
+
+
+ompd_rc_t
+ompd_rel_thread_handle (ompd_thread_handle_t *thread_handle)
+{
+  if (thread_handle == NULL)
+    return ompd_rc_stale_handle;
+
+  ompd_rc_t ret;
+  ret = callbacks->free_memory ((void *) thread_handle);
+  if (ret != ompd_rc_ok)
+  return ret;
+
+  return ompd_rc_ok;
+}
+
+
+/* Return -1, 0 or 1 for thread_handle_1 <, == or > thread_handle_2.  */
+ompd_rc_t
+ompd_thread_handle_compare (ompd_thread_handle_t *thread_handle_1,
+			    ompd_thread_handle_t *thread_handle_2,
+			    int	*cmp_value)
+{
+
+  if (thread_handle_1 == NULL || thread_handle_2 == NULL)
+    return ompd_rc_stale_handle;
+  if (cmp_value == NULL)
+    return ompd_rc_bad_input;
+  if (thread_handle_1->ah->kind != thread_handle_2->ah->kind)
+    return ompd_rc_bad_input;
+
+  if (thread_handle_1->th.address < thread_handle_2->th.address)
+    *cmp_value = -1;
+  else if (thread_handle_1->th.address > thread_handle_2->th.address)
+    *cmp_value = 1;
+  else
+    *cmp_value = 0;
+
+  return ompd_rc_ok;
+}
+
+
+ompd_rc_t
+ompd_get_thread_id (ompd_thread_handle_t *thread_handle, ompd_thread_id_t kind,
+		    ompd_size_t sizeof_thread_id, void *thread_id)
+{
+  if (kind != OMPD_THREAD_ID_PTHREAD)
+    return ompd_rc_unsupported;
+  if (thread_id == NULL)
+    return ompd_rc_bad_input;
+  if (thread_handle == NULL)
+    return ompd_rc_stale_handle;
+
+  CHECK (thread_handle->ah);
+  ompd_address_space_context_t *context = thread_handle->ah->context;
+
+  ompd_rc_t ret;
+  ompd_address_t taddr = thread_handle->th;
+  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
+  ompd_size_t temp_symbol_size, symbol_size;
+  ompd_word_t temp_offset, offset;
+
+  GET_VALUE (context, NULL, "gompd_sizeof_gomp_thread_handle", symbol_size,
+	     temp_symbol_size, target_sizes.sizeof_short, 1, ret, symbol_addr);
+
+  if (symbol_size == 0)
+    {
+      GET_VALUE (context, NULL, "gompd_thread_initial_tls_bias", offset,
+		 temp_offset, target_sizes.sizeof_long, 1, ret, symbol_addr);
+
+      ret = callbacks->symbol_addr_lookup (context, NULL,"gomp_tls_data",
+					   &symbol_addr, NULL);
+      ret = callbacks->device_to_host (context, &temp_symbol_addr.address,
+				       target_sizes.sizeof_long_long, 1,
+				       &symbol_addr.address);
+      CHECK_RET (ret);
+
+      taddr.address = symbol_addr.address + offset;
+      ret = callbacks->read_memory (context, NULL, &taddr,
+				    target_sizes.sizeof_long_long, thread_id);
+    }
+  else
+    {
+      if (sizeof_thread_id != symbol_size)
+        return ompd_rc_bad_input;
+
+      GET_VALUE (context, NULL, "gompd_access_gomp_thread_handle", offset,
+		 temp_offset, target_sizes.sizeof_short, 1, ret, symbol_addr);
+      taddr.address += offset;
+
+      ret = callbacks->read_memory (context, NULL, &taddr, symbol_size,
+				    thread_id);
+    }
+  return ret;
+}
+
+
+/* OMPD doesn't support GPUs for now.  */
+ompd_rc_t ompd_get_device_from_thread (ompd_thread_handle_t *thread_handle,
+				       ompd_address_space_handle_t **device)
+{
+  if (thread_handle == NULL)
+    return ompd_rc_stale_handle;
+  return ompd_rc_unsupported;
+}
diff --git a/libgomp/team.c b/libgomp/team.c
index d53246961b7..9a84dc18bdb 100644
--- a/libgomp/team.c
+++ b/libgomp/team.c
@@ -77,6 +77,7 @@ gomp_thread_start (void *xdata)
   void *local_data;
 
   ompd_bp_thread_begin ();
+
 #if defined HAVE_TLS || defined USE_EMUTLS
   thr = &gomp_tls_data;
 #else
@@ -313,6 +314,8 @@ gomp_free_thread (void *arg __attribute__((unused)))
       gomp_end_task ();
       free (task);
     }
+
+  ompd_bp_thread_end ();
 }
 
 /* Launch a team.  */

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [patch] libgompd: Add thread handles
  2022-09-27  1:20 ` Ahmed Sayed Mousse
@ 2022-09-27  6:40   ` Bernhard Reutner-Fischer
  0 siblings, 0 replies; 9+ messages in thread
From: Bernhard Reutner-Fischer @ 2022-09-27  6:40 UTC (permalink / raw)
  To: Ahmed Sayed Mousse via Gcc-patches; +Cc: rep.dot.nop, Ahmed Sayed Mousse, jakub

On Tue, 27 Sep 2022 03:20:51 +0200
Ahmed Sayed Mousse via Gcc-patches <gcc-patches@gcc.gnu.org> wrote:

> diff --git a/libgomp/Makefile.am b/libgomp/Makefile.am
> index 6d913a93e7f..23f5bede1bf 100644
> --- a/libgomp/Makefile.am
> +++ b/libgomp/Makefile.am
> @@ -94,7 +94,7 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c error.c \
>  	priority_queue.c affinity-fmt.c teams.c allocator.c oacc-profiling.c \
>  	oacc-target.c ompd-support.c
>  
> -libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c
> +libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c ompd-threads.c
>  
>  include $(top_srcdir)/plugin/Makefrag.am
>  
> diff --git a/libgomp/Makefile.in b/libgomp/Makefile.in
> index 40f896b5f03..7acdcbf31d5 100644
> --- a/libgomp/Makefile.in
> +++ b/libgomp/Makefile.in
> @@ -233,7 +233,8 @@ am_libgomp_la_OBJECTS = alloc.lo atomic.lo barrier.lo critical.lo \
>  	affinity-fmt.lo teams.lo allocator.lo oacc-profiling.lo \
>  	oacc-target.lo ompd-support.lo $(am__objects_1)
>  libgomp_la_OBJECTS = $(am_libgomp_la_OBJECTS)
> -am_libgompd_la_OBJECTS = ompd-init.lo ompd-helper.lo ompd-icv.lo
> +am_libgompd_la_OBJECTS = ompd-init.lo ompd-helper.lo ompd-icv.lo \
> +	ompd-threads.lo
>  libgompd_la_OBJECTS = $(am_libgompd_la_OBJECTS)
>  AM_V_P = $(am__v_P_@AM_V@)
>  am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
> @@ -583,7 +584,7 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c \
>  	oacc-async.c oacc-plugin.c oacc-cuda.c priority_queue.c \
>  	affinity-fmt.c teams.c allocator.c oacc-profiling.c \
>  	oacc-target.c ompd-support.c $(am__append_7)
> -libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c
> +libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c ompd-threads.c
>  
>  # Nvidia PTX OpenACC plugin.
>  @PLUGIN_NVPTX_TRUE@libgomp_plugin_nvptx_version_info = -version-info $(libtool_VERSION)
> @@ -801,6 +802,7 @@ distclean-compile:
>  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-icv.Plo@am__quote@
>  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-init.Plo@am__quote@
>  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-support.Plo@am__quote@
> +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-threads.Plo@am__quote@
>  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ordered.Plo@am__quote@
>  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/parallel.Plo@am__quote@
>  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/priority_queue.Plo@am__quote@
> diff --git a/libgomp/ompd-support.c b/libgomp/ompd-support.c
> index 27c5ad148e0..5b1afd37788 100644
> --- a/libgomp/ompd-support.c
> +++ b/libgomp/ompd-support.c
> @@ -33,6 +33,8 @@ const unsigned short gompd_sizeof_gomp_thread_handle
>    __attribute__ ((used)) OMPD_SECTION = 0;
>  #endif
>  
> +unsigned long gompd_thread_initial_tls_bias __attribute__ ((used));
> +
>  /* Get offset of the member m in struct t.  */
>  #define gompd_get_offset(t, m) \
>    const unsigned short gompd_access_##t##_##m __attribute__ ((used)) \
> @@ -67,6 +69,11 @@ gompd_load (void)
>    gompd_state |= OMPD_ENABLED;
>    ompd_dll_locations = &ompd_dll_locations_array[0];
>    ompd_dll_locations_valid ();
> +
> +  #if defined(LIBGOMP_USE_PTHREADS) && !defined(GOMP_NEEDS_THREAD_HANDLE)
> +  gompd_thread_initial_tls_bias = (unsigned long) ((char *) &gomp_tls_data
> +						   - (char *) pthread_self ());
> +  #endif
>  }
>  
>  #ifndef __ELF__
> diff --git a/libgomp/ompd-threads.c b/libgomp/ompd-threads.c
> new file mode 100644
> index 00000000000..723ef740181
> --- /dev/null
> +++ b/libgomp/ompd-threads.c
> @@ -0,0 +1,222 @@
> +/* Copyright (C) The GNU Toolchain Authors.
> +   Contributed by Ahmed Sayed <ahmedsayedmousse@gmail.com>.
> +   This file is part of the GNU Offloading and Multi Processing Library
> +   (libgomp).
> +
> +   Libgomp is free software; you can redistribute it and/or modify it
> +   under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3, or (at your option)
> +   any later version.
> +
> +   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
> +   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
> +   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
> +   more details.
> +
> +   Under Section 7 of GPL version 3, you are granted additional
> +   permissions described in the GCC Runtime Library Exception, version
> +   3.1, as published by the Free Software Foundation.
> +
> +   You should have received a copy of the GNU General Public License and
> +   a copy of the GCC Runtime Library Exception along with this program;
> +   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
> +   <http://www.gnu.org/licenses/>.  */
> +
> +/* This file contains the implementation of functions defined in
> +   Section 5.5 ThreadHandles. */
> +
> +
> +#include "ompd-helper.h"
> +
> +ompd_rc_t
> +ompd_get_thread_in_parallel (ompd_parallel_handle_t *parallel_handle,
> +			     int thread_num,
> +			     ompd_thread_handle_t **thread_handle)
> +{
> +
> +  if (parallel_handle == NULL)
> +    return ompd_rc_stale_handle;
> +  CHECK (parallel_handle->ah);
> +
> +  ompd_address_space_context_t *context = parallel_handle->ah->context;
> +  ompd_rc_t ret;
> +
> +  ompd_word_t team_size_var = 1;
> +  if (parallel_handle->th.address)
> +    gompd_get_team_size (parallel_handle, &team_size_var);
> +
> +  if (thread_num < 0 || thread_num >= team_size_var)
> +    return ompd_rc_bad_input;
> +
> +  ompd_word_t temp_offset;
> +  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
> +  ompd_addr_t temp_addr;
> +
> +  ACCESS_VALUE (context, NULL, "gompd_access_gomp_thread_pool_threads",
> +		temp_offset, 1, ret, symbol_addr, temp_symbol_addr, temp_addr);
> +
> +  symbol_addr.address += thread_num * target_sizes.sizeof_pointer;
> +
> +  DEREFERENCE (context, NULL, symbol_addr, target_sizes.sizeof_pointer, 1,
> +	       temp_addr, ret, 1);
> +
> +  ret = callbacks->alloc_memory (sizeof (ompd_thread_handle_t),
> +				 (void **) thread_handle);
> +
> +  CHECK_RET (ret);
> +
> +  if (symbol_addr.address == 0)
> +    return ompd_rc_unsupported;

Does the above leak the allocated memory, i.e. do you have to move the
check to right before the DEREFERENCE?

> +
> +  (*thread_handle)->th = symbol_addr;
> +  (*thread_handle)->ah = parallel_handle->ah;
> +  return ret;
> +}
> +
> +/* The ompd_get_thread_handle function that maps a native thread to an
> +   OMPD thread handle.  */
> +
> +ompd_rc_t
> +ompd_get_thread_handle (ompd_address_space_handle_t *handle,
> +			ompd_thread_id_t kind, ompd_size_t sizeof_thread_id,
> +			const void *thread_id,
> +			ompd_thread_handle_t **thread_handle)
> +{
> +  CHECK (handle);
> +  if (kind != OMPD_THREAD_ID_PTHREAD)
> +    return ompd_rc_unsupported;
> +
> +  ompd_address_space_context_t *context = handle->context;
> +  ompd_thread_context_t *tcontext;
> +  ompd_rc_t ret;
> +
> +  ret = callbacks->get_thread_context_for_thread_id (context, kind,
> +						     sizeof_thread_id,
> +						     thread_id, &tcontext);
> +  CHECK_RET (ret);
> +
> +  ompd_size_t temp_symbol_size, symbol_size;
> +  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
> +
> +  GET_VALUE (context, NULL, "gompd_sizeof_gomp_thread", symbol_size,
> +	     temp_symbol_size, target_sizes.sizeof_short, 1, ret,
> +	     temp_symbol_addr);
> +
> +  GET_VALUE (context, tcontext, "gomp_tls_data", symbol_addr.address,
> +	     temp_symbol_addr.address, symbol_size, 1, ret, symbol_addr);
> +
> +  ret = callbacks->alloc_memory (sizeof (ompd_thread_handle_t),
> +				 (void **) thread_handle);
> +
> +  CHECK_RET (ret);
> +
> +  (*thread_handle)->ah = handle;
> +  (*thread_handle)->th = symbol_addr;
> +  (*thread_handle)->thread_context = tcontext;
> +  return ret;
> +}
> +
> +
> +ompd_rc_t
> +ompd_rel_thread_handle (ompd_thread_handle_t *thread_handle)
> +{
> +  if (thread_handle == NULL)
> +    return ompd_rc_stale_handle;
> +
> +  ompd_rc_t ret;
> +  ret = callbacks->free_memory ((void *) thread_handle);
> +  if (ret != ompd_rc_ok)
> +  return ret;

You seem to usually use CHECK_RET for the above.

> +
> +  return ompd_rc_ok;
> +}
> +
> +
> +/* Return -1, 0 or 1 for thread_handle_1 <, == or > thread_handle_2.  */
> +ompd_rc_t
> +ompd_thread_handle_compare (ompd_thread_handle_t *thread_handle_1,
> +			    ompd_thread_handle_t *thread_handle_2,
> +			    int	*cmp_value)
> +{
> +
> +  if (thread_handle_1 == NULL || thread_handle_2 == NULL)
> +    return ompd_rc_stale_handle;
> +  if (cmp_value == NULL)
> +    return ompd_rc_bad_input;
> +  if (thread_handle_1->ah->kind != thread_handle_2->ah->kind)
> +    return ompd_rc_bad_input;
> +
> +  if (thread_handle_1->th.address < thread_handle_2->th.address)
> +    *cmp_value = -1;
> +  else if (thread_handle_1->th.address > thread_handle_2->th.address)
> +    *cmp_value = 1;
> +  else
> +    *cmp_value = 0;
> +
> +  return ompd_rc_ok;
> +}
> +
> +
> +ompd_rc_t
> +ompd_get_thread_id (ompd_thread_handle_t *thread_handle, ompd_thread_id_t kind,
> +		    ompd_size_t sizeof_thread_id, void *thread_id)
> +{
> +  if (kind != OMPD_THREAD_ID_PTHREAD)
> +    return ompd_rc_unsupported;
> +  if (thread_id == NULL)
> +    return ompd_rc_bad_input;
> +  if (thread_handle == NULL)
> +    return ompd_rc_stale_handle;
> +
> +  CHECK (thread_handle->ah);
> +  ompd_address_space_context_t *context = thread_handle->ah->context;
> +
> +  ompd_rc_t ret;
> +  ompd_address_t taddr = thread_handle->th;
> +  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
> +  ompd_size_t temp_symbol_size, symbol_size;
> +  ompd_word_t temp_offset, offset;
> +
> +  GET_VALUE (context, NULL, "gompd_sizeof_gomp_thread_handle", symbol_size,
> +	     temp_symbol_size, target_sizes.sizeof_short, 1, ret, symbol_addr);
> +
> +  if (symbol_size == 0)
> +    {
> +      GET_VALUE (context, NULL, "gompd_thread_initial_tls_bias", offset,
> +		 temp_offset, target_sizes.sizeof_long, 1, ret, symbol_addr);
> +
> +      ret = callbacks->symbol_addr_lookup (context, NULL,"gomp_tls_data",
> +					   &symbol_addr, NULL);

The above can never fail, no?
thanks,

> +      ret = callbacks->device_to_host (context, &temp_symbol_addr.address,
> +				       target_sizes.sizeof_long_long, 1,
> +				       &symbol_addr.address);
> +      CHECK_RET (ret);
> +
> +      taddr.address = symbol_addr.address + offset;
> +      ret = callbacks->read_memory (context, NULL, &taddr,
> +				    target_sizes.sizeof_long_long, thread_id);
> +    }
> +  else
> +    {
> +      if (sizeof_thread_id != symbol_size)
> +        return ompd_rc_bad_input;
> +
> +      GET_VALUE (context, NULL, "gompd_access_gomp_thread_handle", offset,
> +		 temp_offset, target_sizes.sizeof_short, 1, ret, symbol_addr);
> +      taddr.address += offset;
> +
> +      ret = callbacks->read_memory (context, NULL, &taddr, symbol_size,
> +				    thread_id);
> +    }
> +  return ret;
> +}
> +
> +
> +/* OMPD doesn't support GPUs for now.  */
> +ompd_rc_t ompd_get_device_from_thread (ompd_thread_handle_t *thread_handle,
> +				       ompd_address_space_handle_t **device)
> +{
> +  if (thread_handle == NULL)
> +    return ompd_rc_stale_handle;
> +  return ompd_rc_unsupported;
> +}
> diff --git a/libgomp/team.c b/libgomp/team.c
> index d53246961b7..9a84dc18bdb 100644
> --- a/libgomp/team.c
> +++ b/libgomp/team.c
> @@ -77,6 +77,7 @@ gomp_thread_start (void *xdata)
>    void *local_data;
>  
>    ompd_bp_thread_begin ();
> +
>  #if defined HAVE_TLS || defined USE_EMUTLS
>    thr = &gomp_tls_data;
>  #else
> @@ -313,6 +314,8 @@ gomp_free_thread (void *arg __attribute__((unused)))
>        gomp_end_task ();
>        free (task);
>      }
> +
> +  ompd_bp_thread_end ();
>  }
>  
>  /* Launch a team.  */


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [patch] libgompd: Add thread handles
  2022-07-04 20:34 Ahmed Sayed Mousse
  2022-07-27 12:11 ` Mohamed Atef
@ 2022-07-29 11:22 ` Jakub Jelinek
  1 sibling, 0 replies; 9+ messages in thread
From: Jakub Jelinek @ 2022-07-29 11:22 UTC (permalink / raw)
  To: Ahmed Sayed Mousse; +Cc: gcc-patches

On Mon, Jul 04, 2022 at 10:34:03PM +0200, Ahmed Sayed Mousse wrote:
> *This patch is the initial implementation of OpenMP-API specs book section
> **20.5.5 with title "Thread Handles".*

Sorry for the delay, have been on vacation.

> *I have fixed the first version after revising the notes on it.*
> 
> *libgomp/ChangeLog
> 
> 2022-07-01  Ahmed Sayed  <ahmedsayedmousse@gmail.com
> <ahmedsayedmousse@gmail.com>>
> *
> 
> ** Makefile.am (libgompd_la_SOURCES): Add ompd-threads.c.*
> 
> ** Makefile.in: Regenerate.*
> 
> ** team.c ( gomp_free_thread ): Called ompd_bp_thread_end ().*
> 
> ** ompd-support.c ( gompd_thread_initial_tls_bias ): New Variable.*
> 
> *  	  (gompd_load): Initialize gompd_thread_initial_tls_bias.*
> 
> ** ompd-threads.c: New File.*

The ChangeLog formatting is wrong, so wouldn't go through the commit
hook checking.  Unclear what part of it is just a fault of your mailer
setting and what is really wrong.  But
There should be just > after gmail.com, not another email address,
all the non-empty lines should be indented by a single tab,
there shouldn't be an extra * at the start of end of lines.
There shouldn't be empty lines in between the different changes, just
between the date/name/email line and the ret.
There shouldn't be spaces after ( or before ).
Instead of Called ompd_bp_thread_end (). say just Call ompd_bp_thread_end.
New variable. rather than New Variable.
The line with (gompd_load) is weirdly extra indented.
New file. rather than New File.

> diff --git a/libgomp/Makefile.am b/libgomp/Makefile.am
> index 6d913a93e7f..23f5bede1bf 100644
> --- a/libgomp/Makefile.am
> +++ b/libgomp/Makefile.am
> @@ -94,7 +94,7 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c error.c \
>  	priority_queue.c affinity-fmt.c teams.c allocator.c oacc-profiling.c \
>  	oacc-target.c ompd-support.c
>  
> -libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c
> +libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c ompd-threads.c
>  
>  include $(top_srcdir)/plugin/Makefrag.am
>  

You've just changed libgompd_la_SOURCES but there are many changes
in the generated file, that means either you didn't use the right
libtool version (1.15.1) or something else wrong is happening.

> diff --git a/libgomp/Makefile.in b/libgomp/Makefile.in
> index 40f896b5f03..8bbc46cca25 100644
> --- a/libgomp/Makefile.in
> +++ b/libgomp/Makefile.in
> @@ -133,21 +133,8 @@ target_triplet = @target@
>  @USE_FORTRAN_TRUE@am__append_7 = openacc.f90
>  subdir = .
>  ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
> -am__aclocal_m4_deps = $(top_srcdir)/../config/acx.m4 \
> -	$(top_srcdir)/../config/ax_count_cpus.m4 \
> -	$(top_srcdir)/../config/depstand.m4 \
> -	$(top_srcdir)/../config/enable.m4 \
> -	$(top_srcdir)/../config/futex.m4 \
> -	$(top_srcdir)/../config/lead-dot.m4 \
> -	$(top_srcdir)/../config/lthostflags.m4 \
> -	$(top_srcdir)/../config/multi.m4 \
> -	$(top_srcdir)/../config/override.m4 \
> -	$(top_srcdir)/../config/tls.m4 \
> -	$(top_srcdir)/../config/toolexeclibdir.m4 \
> -	$(top_srcdir)/../ltoptions.m4 $(top_srcdir)/../ltsugar.m4 \
> -	$(top_srcdir)/../ltversion.m4 $(top_srcdir)/../lt~obsolete.m4 \
> -	$(top_srcdir)/acinclude.m4 $(top_srcdir)/../libtool.m4 \
> -	$(top_srcdir)/../config/cet.m4 \
> +am__aclocal_m4_deps = $(top_srcdir)/acinclude.m4 \
> +	$(top_srcdir)/../libtool.m4 $(top_srcdir)/../config/cet.m4 \
>  	$(top_srcdir)/plugin/configfrag.ac $(top_srcdir)/configure.ac

The above certainly shouldn't be changed.

>  am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
>  	$(ACLOCAL_M4)
> @@ -233,7 +220,8 @@ am_libgomp_la_OBJECTS = alloc.lo atomic.lo barrier.lo critical.lo \
>  	affinity-fmt.lo teams.lo allocator.lo oacc-profiling.lo \
>  	oacc-target.lo ompd-support.lo $(am__objects_1)
>  libgomp_la_OBJECTS = $(am_libgomp_la_OBJECTS)
> -am_libgompd_la_OBJECTS = ompd-init.lo ompd-helper.lo ompd-icv.lo
> +am_libgompd_la_OBJECTS = ompd-init.lo ompd-helper.lo ompd-icv.lo \
> +	ompd-threads.lo
>  libgompd_la_OBJECTS = $(am_libgompd_la_OBJECTS)
>  AM_V_P = $(am__v_P_@AM_V@)
>  am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)

The above yes.

> @@ -485,7 +473,6 @@ dvidir = @dvidir@
>  enable_shared = @enable_shared@
>  enable_static = @enable_static@
>  exec_prefix = @exec_prefix@
> -get_gcc_base_ver = @get_gcc_base_ver@
>  host = @host@
>  host_alias = @host_alias@
>  host_cpu = @host_cpu@
> @@ -501,10 +488,8 @@ libtool_VERSION = @libtool_VERSION@
>  link_gomp = @link_gomp@
>  localedir = @localedir@
>  localstatedir = @localstatedir@
> -lt_host_flags = @lt_host_flags@
>  mandir = @mandir@
>  mkdir_p = @mkdir_p@
> -multi_basedir = @multi_basedir@
>  offload_additional_lib_paths = @offload_additional_lib_paths@
>  offload_additional_options = @offload_additional_options@
>  offload_plugins = @offload_plugins@
> @@ -514,6 +499,7 @@ pdfdir = @pdfdir@
>  prefix = @prefix@
>  program_transform_name = @program_transform_name@
>  psdir = @psdir@
> +runstatedir = @runstatedir@
>  sbindir = @sbindir@
>  sharedstatedir = @sharedstatedir@
>  srcdir = @srcdir@

The above shouldn't be changed.

> @@ -583,7 +569,7 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c \
>  	oacc-async.c oacc-plugin.c oacc-cuda.c priority_queue.c \
>  	affinity-fmt.c teams.c allocator.c oacc-profiling.c \
>  	oacc-target.c ompd-support.c $(am__append_7)
> -libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c
> +libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c ompd-threads.c
>  
>  # Nvidia PTX OpenACC plugin.
>  @PLUGIN_NVPTX_TRUE@libgomp_plugin_nvptx_version_info = -version-info $(libtool_VERSION)
> @@ -801,6 +787,7 @@ distclean-compile:
>  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-icv.Plo@am__quote@
>  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-init.Plo@am__quote@
>  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-support.Plo@am__quote@
> +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-threads.Plo@am__quote@
>  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ordered.Plo@am__quote@
>  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/parallel.Plo@am__quote@
>  @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/priority_queue.Plo@am__quote@

The above should.

> --- a/libgomp/aclocal.m4
> +++ b/libgomp/aclocal.m4

This shouldn't change at all.
> --- a/libgomp/configure
> +++ b/libgomp/configure

Neither should this.

> --- a/libgomp/ompd-support.c
> +++ b/libgomp/ompd-support.c
> @@ -33,6 +33,8 @@ const unsigned short gompd_sizeof_gomp_thread_handle
>    __attribute__ ((used)) OMPD_SECTION = 0;
>  #endif
>  
> +unsigned long gompd_thread_initial_tls_bias __attribute__ ((used));
> +
>  /* Get offset of the member m in struct t.  */
>  #define gompd_get_offset(t, m) \
>    const unsigned short gompd_access_##t##_##m __attribute__ ((used)) \
> @@ -67,6 +69,9 @@ gompd_load (void)
>    gompd_state |= OMPD_ENABLED;
>    ompd_dll_locations = &ompd_dll_locations_array[0];
>    ompd_dll_locations_valid ();
> +
> +  gompd_thread_initial_tls_bias = (unsigned long) ((char *) &gomp_tls_data
> +  						   - (char *) pthread_self ());

This should be done only when GOMP_NEEDS_THREAD_HANDLE is not defined.
Otherwise gompd_thread_initial_tls_bias should be initialized to some magic
value (that isn't otherwise possible) that ompd-threads.c will handle as
request not to use the tls bias and instead read struct gomp_thread's 
handle member.
Looking at your patch later, you already use sizeof the handle == 0 as
sign of that, so just wrap the above store in
#if defined(LIBGOMP_USE_PTHREADS) && !defined(GOMP_NEEDS_THREAD_HANDLE)

> --- /dev/null
> +++ b/libgomp/ompd-threads.c
> @@ -0,0 +1,216 @@
> +#include "ompd-helper.h"
> +
> +ompd_rc_t
> +ompd_get_thread_in_parallel (ompd_parallel_handle_t *parallel_handle,
> +			     int thread_num,
> +			     ompd_thread_handle_t **thread_handle)
> +{
> +
> +  if (parallel_handle == NULL)
> +    return ompd_rc_stale_handle;
> +  CHECK (parallel_handle->ah);
> +
> +  ompd_address_space_context_t *context = parallel_handle->ah->context;
> +  ompd_rc_t ret;
> +
> +  ompd_word_t team_size_var = 1;
> +  if (parallel_handle->th.address)
> +    gompd_get_team_size(parallel_handle, &team_size_var);

Space before (

> +/* The ompd_get_thread_handle function that maps a native thread to an
> +   OMPD thread handle.  */
> +
> +ompd_rc_t
> +ompd_get_thread_handle (ompd_address_space_handle_t *handle,
> +			ompd_thread_id_t kind, ompd_size_t sizeof_thread_id,
> +			const void *thread_id,
> +			ompd_thread_handle_t **thread_handle)
> +{
> +  CHECK (handle);
> +  if (kind != OMPD_THREAD_ID_PTHREAD)
> +    return ompd_rc_unsupported;
> +
> +  ompd_address_space_context_t *context = handle->context;
> +  ompd_thread_context_t *tcontext;
> +  ompd_rc_t ret;
> +
> +  ret = callbacks->get_thread_context_for_thread_id (context, kind,
> +						     sizeof_thread_id,
> +						     thread_id, &tcontext);
> +  CHECK_RET (ret);
> +
> +  ompd_size_t temp_symbol_size, symbol_size;
> +  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
> +
> +  GET_VALUE (context, NULL, "gompd_sizeof_gomp_thread", symbol_size,
> +	     temp_symbol_size, target_sizes.sizeof_short, 1, ret,
> +	     temp_symbol_addr);
> +
> +  GET_VALUE (context, tcontext, "gomp_tls_data", symbol_addr.address,
> +	     temp_symbol_addr.address, symbol_size, 1, ret, symbol_addr);
> +
> +  ret = callbacks->alloc_memory (sizeof (ompd_thread_handle_t),
> +				 (void **) thread_handle);
> +
> +  CHECK_RET (ret);
> +
> +  (*thread_handle)->ah = handle;
> +  (*thread_handle)->th = symbol_addr;
> +  (*thread_handle)->thread_context = tcontext;
> +  return ret;
> +}
> +
> +
> +ompd_rc_t
> +ompd_rel_thread_handle (ompd_thread_handle_t *thread_handle)
> +{
> +  if (thread_handle == NULL)
> +    return ompd_rc_stale_handle;
> +
> +  ompd_rc_t ret;
> +  ret = callbacks->free_memory ((void *) thread_handle);
> +  if (ret != ompd_rc_ok)
> +  return ret;
> +
> +  return ompd_rc_ok;
> +}
> +
> +
> +/* return -1, 0 or 1 for thread_handle_1 <, == or > thread_handle_2.  */

Capital R, i.e. Return

> +ompd_rc_t
> +ompd_thread_handle_compare (ompd_thread_handle_t *thread_handle_1,
> +			    ompd_thread_handle_t *thread_handle_2,
> +			    int	*cmp_value )

No space before )
> +{
> +
> +  if (thread_handle_1 == NULL || thread_handle_2 == NULL)
> +    return ompd_rc_stale_handle;
> +  if (cmp_value == NULL)
> +    return ompd_rc_bad_input;
> +  if (thread_handle_1->ah->kind != thread_handle_2->ah->kind)
> +    return ompd_rc_bad_input;
> +
> +  *cmp_value = thread_handle_1->th.address - thread_handle_2->th.address;

This looks incorrect.  address is I believe ompd_addr_t, 64-bit unsigned
integer, you subtract 2 64-bit integers and store the difference into
int, usually 32-bit.  Whether that compares < 0, or > 0, or == 0 is a
lottery.
Furthermore, you document -1, 0, 1, not < 0, 0, > 0.

So, better do
  if (thread_handle_1->th.address < thread_handle_2->th.address)
    *cmp_value = -1;
  else if (thread_handle_1->th.address > thread_handle_2->th.address)
    *cmp_value = 1;
  else
    *cmp_value = 0;

> +  return ompd_rc_ok;
> +}
> +
> +
> +ompd_rc_t
> +ompd_get_thread_id (ompd_thread_handle_t *thread_handle, ompd_thread_id_t kind,
> +		    ompd_size_t sizeof_thread_id, void *thread_id)
> +{
> +  if (kind != OMPD_THREAD_ID_PTHREAD)
> +    return ompd_rc_unsupported;
> +  if (thread_id == NULL)
> +    return ompd_rc_bad_input;
> +  if (thread_handle == NULL)
> +    return ompd_rc_stale_handle;
> +
> +  CHECK (thread_handle->ah);
> +  ompd_address_space_context_t *context = thread_handle->ah->context;
> +
> +  ompd_rc_t ret;
> +  ompd_address_t taddr = thread_handle->th;
> +  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
> +  ompd_size_t temp_symbol_size, symbol_size;
> +  ompd_word_t temp_offset, offset;
> +
> +  GET_VALUE (context, NULL, "gompd_sizeof_gomp_thread_handle", symbol_size,
> +	     temp_symbol_size, target_sizes.sizeof_short, 1, ret, symbol_addr);
> +
> +  if (symbol_size == 0)
> +    goto use_tls_bias;
> +
> +  if (sizeof_thread_id != symbol_size)
> +    return ompd_rc_bad_input;
> +
> +  GET_VALUE (context, NULL, "gompd_access_gomp_thread_handle", offset,
> +	     temp_offset, target_sizes.sizeof_short, 1, ret, symbol_addr);
> +  taddr.address += offset;
> +
> +  ret = callbacks->read_memory (context, NULL, &taddr, symbol_size, thread_id);
> +  return ret;
> +
> +use_tls_bias:

I don't see the need to use goto and label here, just do
  if (symbol_size == 0)
    {
      TLS bias handling
    }
  else
    {
      thread handle handling
    }
> +
> +  GET_VALUE (context, NULL, "gompd_thread_initial_tls_bias", offset, temp_offset,
> +	     target_sizes.sizeof_long, 1, ret, symbol_addr);
> +
> +  ret = callbacks->symbol_addr_lookup (context, NULL,"gomp_tls_data",
> +				       &symbol_addr, NULL);
> +  ret = callbacks->device_to_host (context, &temp_symbol_addr.address,
> +				   target_sizes.sizeof_long_long, 1,
> +				   &symbol_addr.address);
> +  CHECK_RET (ret);
> +
> +  taddr.address = symbol_addr.address + offset;
> +  ret = callbacks->read_memory (context, NULL, &taddr,
> +				target_sizes.sizeof_long_long, thread_id);
> +  return ret;
> +}
> +
> +
> +/* OMPD doesn't support GPUs for now.  */
> +ompd_rc_t ompd_get_device_from_thread (ompd_thread_handle_t *thread_handle,
> +				       ompd_address_space_handle_t **device)
> +{
> +  if (thread_handle == NULL)
> +    return ompd_rc_stale_handle;
> +  return ompd_rc_unsupported;
> +}
> diff --git a/libgomp/team.c b/libgomp/team.c
> index d53246961b7..8e18fd6af63 100644
> --- a/libgomp/team.c
> +++ b/libgomp/team.c
> @@ -77,6 +77,7 @@ gomp_thread_start (void *xdata)
>    void *local_data;
>  
>    ompd_bp_thread_begin ();
> +
>  #if defined HAVE_TLS || defined USE_EMUTLS
>    thr = &gomp_tls_data;
>  #else
> @@ -313,6 +314,9 @@ gomp_free_thread (void *arg __attribute__((unused)))
>        gomp_end_task ();
>        free (task);
>      }
> +
> +  ompd_bp_thread_end ();
> +

No need for the empty line after the call/macro.
>  }
>  
>  /* Launch a team.  */
> diff --git a/libgomp/testsuite/Makefile.in b/libgomp/testsuite/Makefile.in
> index 048844f0a40..76cd09b0faf 100644
> --- a/libgomp/testsuite/Makefile.in
> +++ b/libgomp/testsuite/Makefile.in

This file shouldn't be changed at all.

	Jakub


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [patch] libgompd: Add thread handles
  2022-07-04 20:34 Ahmed Sayed Mousse
@ 2022-07-27 12:11 ` Mohamed Atef
  2022-07-29 11:22 ` Jakub Jelinek
  1 sibling, 0 replies; 9+ messages in thread
From: Mohamed Atef @ 2022-07-27 12:11 UTC (permalink / raw)
  To: Ahmed Sayed Mousse; +Cc: gcc-patches, jakub

Ping

في الاثنين، ٤ يوليو، ٢٠٢٢ ١٠:٣٤ م Ahmed Sayed Mousse <
ahmedsayedmousse@gmail.com> كتب:

> *This patch is the initial implementation of OpenMP-API specs book section
> **20.5.5 with title "Thread Handles".*
>
> *I have fixed the first version after revising the notes on it.*
>
> *libgomp/ChangeLog
>
> 2022-07-01  Ahmed Sayed  <ahmedsayedmousse@gmail.com <ahmedsayedmousse@gmail.com>>
> *
>
> ** Makefile.am (libgompd_la_SOURCES): Add ompd-threads.c.*
>
> ** Makefile.in: Regenerate.*
>
> ** team.c ( gomp_free_thread ): Called ompd_bp_thread_end ().*
>
> ** ompd-support.c ( gompd_thread_initial_tls_bias ): New Variable.*
>
> *  	  (gompd_load): Initialize gompd_thread_initial_tls_bias.*
>
> ** ompd-threads.c: New File.*
>
>
>
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [patch] libgompd: Add thread handles
@ 2022-07-04 20:34 Ahmed Sayed Mousse
  2022-07-27 12:11 ` Mohamed Atef
  2022-07-29 11:22 ` Jakub Jelinek
  0 siblings, 2 replies; 9+ messages in thread
From: Ahmed Sayed Mousse @ 2022-07-04 20:34 UTC (permalink / raw)
  To: gcc-patches; +Cc: jakub

[-- Attachment #1: Type: text/plain, Size: 604 bytes --]

*This patch is the initial implementation of OpenMP-API specs book section
**20.5.5 with title "Thread Handles".*

*I have fixed the first version after revising the notes on it.*

*libgomp/ChangeLog

2022-07-01  Ahmed Sayed  <ahmedsayedmousse@gmail.com
<ahmedsayedmousse@gmail.com>>
*

** Makefile.am (libgompd_la_SOURCES): Add ompd-threads.c.*

** Makefile.in: Regenerate.*

** team.c ( gomp_free_thread ): Called ompd_bp_thread_end ().*

** ompd-support.c ( gompd_thread_initial_tls_bias ): New Variable.*

*  	  (gompd_load): Initialize gompd_thread_initial_tls_bias.*

** ompd-threads.c: New File.*

[-- Attachment #2: diff.txt --]
[-- Type: text/plain, Size: 25990 bytes --]

diff --git a/libgomp/Makefile.am b/libgomp/Makefile.am
index 6d913a93e7f..23f5bede1bf 100644
--- a/libgomp/Makefile.am
+++ b/libgomp/Makefile.am
@@ -94,7 +94,7 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c error.c \
 	priority_queue.c affinity-fmt.c teams.c allocator.c oacc-profiling.c \
 	oacc-target.c ompd-support.c
 
-libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c
+libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c ompd-threads.c
 
 include $(top_srcdir)/plugin/Makefrag.am
 
diff --git a/libgomp/Makefile.in b/libgomp/Makefile.in
index 40f896b5f03..8bbc46cca25 100644
--- a/libgomp/Makefile.in
+++ b/libgomp/Makefile.in
@@ -133,21 +133,8 @@ target_triplet = @target@
 @USE_FORTRAN_TRUE@am__append_7 = openacc.f90
 subdir = .
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
-am__aclocal_m4_deps = $(top_srcdir)/../config/acx.m4 \
-	$(top_srcdir)/../config/ax_count_cpus.m4 \
-	$(top_srcdir)/../config/depstand.m4 \
-	$(top_srcdir)/../config/enable.m4 \
-	$(top_srcdir)/../config/futex.m4 \
-	$(top_srcdir)/../config/lead-dot.m4 \
-	$(top_srcdir)/../config/lthostflags.m4 \
-	$(top_srcdir)/../config/multi.m4 \
-	$(top_srcdir)/../config/override.m4 \
-	$(top_srcdir)/../config/tls.m4 \
-	$(top_srcdir)/../config/toolexeclibdir.m4 \
-	$(top_srcdir)/../ltoptions.m4 $(top_srcdir)/../ltsugar.m4 \
-	$(top_srcdir)/../ltversion.m4 $(top_srcdir)/../lt~obsolete.m4 \
-	$(top_srcdir)/acinclude.m4 $(top_srcdir)/../libtool.m4 \
-	$(top_srcdir)/../config/cet.m4 \
+am__aclocal_m4_deps = $(top_srcdir)/acinclude.m4 \
+	$(top_srcdir)/../libtool.m4 $(top_srcdir)/../config/cet.m4 \
 	$(top_srcdir)/plugin/configfrag.ac $(top_srcdir)/configure.ac
 am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
 	$(ACLOCAL_M4)
@@ -233,7 +220,8 @@ am_libgomp_la_OBJECTS = alloc.lo atomic.lo barrier.lo critical.lo \
 	affinity-fmt.lo teams.lo allocator.lo oacc-profiling.lo \
 	oacc-target.lo ompd-support.lo $(am__objects_1)
 libgomp_la_OBJECTS = $(am_libgomp_la_OBJECTS)
-am_libgompd_la_OBJECTS = ompd-init.lo ompd-helper.lo ompd-icv.lo
+am_libgompd_la_OBJECTS = ompd-init.lo ompd-helper.lo ompd-icv.lo \
+	ompd-threads.lo
 libgompd_la_OBJECTS = $(am_libgompd_la_OBJECTS)
 AM_V_P = $(am__v_P_@AM_V@)
 am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
@@ -485,7 +473,6 @@ dvidir = @dvidir@
 enable_shared = @enable_shared@
 enable_static = @enable_static@
 exec_prefix = @exec_prefix@
-get_gcc_base_ver = @get_gcc_base_ver@
 host = @host@
 host_alias = @host_alias@
 host_cpu = @host_cpu@
@@ -501,10 +488,8 @@ libtool_VERSION = @libtool_VERSION@
 link_gomp = @link_gomp@
 localedir = @localedir@
 localstatedir = @localstatedir@
-lt_host_flags = @lt_host_flags@
 mandir = @mandir@
 mkdir_p = @mkdir_p@
-multi_basedir = @multi_basedir@
 offload_additional_lib_paths = @offload_additional_lib_paths@
 offload_additional_options = @offload_additional_options@
 offload_plugins = @offload_plugins@
@@ -514,6 +499,7 @@ pdfdir = @pdfdir@
 prefix = @prefix@
 program_transform_name = @program_transform_name@
 psdir = @psdir@
+runstatedir = @runstatedir@
 sbindir = @sbindir@
 sharedstatedir = @sharedstatedir@
 srcdir = @srcdir@
@@ -583,7 +569,7 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c \
 	oacc-async.c oacc-plugin.c oacc-cuda.c priority_queue.c \
 	affinity-fmt.c teams.c allocator.c oacc-profiling.c \
 	oacc-target.c ompd-support.c $(am__append_7)
-libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c
+libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c ompd-threads.c
 
 # Nvidia PTX OpenACC plugin.
 @PLUGIN_NVPTX_TRUE@libgomp_plugin_nvptx_version_info = -version-info $(libtool_VERSION)
@@ -801,6 +787,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-icv.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-init.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-support.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-threads.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ordered.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/parallel.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/priority_queue.Plo@am__quote@
diff --git a/libgomp/aclocal.m4 b/libgomp/aclocal.m4
index 55d9d71895a..41915216beb 100644
--- a/libgomp/aclocal.m4
+++ b/libgomp/aclocal.m4
@@ -626,6 +626,25 @@ if test x"${install_sh+set}" != xset; then
 fi
 AC_SUBST([install_sh])])
 
+# Copyright (C) 2003-2017 Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# Check whether the underlying file-system supports filenames
+# with a leading dot.  For instance MS-DOS doesn't.
+AC_DEFUN([AM_SET_LEADING_DOT],
+[rm -rf .tst 2>/dev/null
+mkdir .tst 2>/dev/null
+if test -d .tst; then
+  am__leading_dot=.
+else
+  am__leading_dot=_
+fi
+rmdir .tst 2>/dev/null
+AC_SUBST([am__leading_dot])])
+
 # Add --enable-maintainer-mode option to configure.         -*- Autoconf -*-
 # From Jim Meyering
 
@@ -1167,19 +1186,4 @@ AC_SUBST([am__tar])
 AC_SUBST([am__untar])
 ]) # _AM_PROG_TAR
 
-m4_include([../config/acx.m4])
-m4_include([../config/ax_count_cpus.m4])
-m4_include([../config/depstand.m4])
-m4_include([../config/enable.m4])
-m4_include([../config/futex.m4])
-m4_include([../config/lead-dot.m4])
-m4_include([../config/lthostflags.m4])
-m4_include([../config/multi.m4])
-m4_include([../config/override.m4])
-m4_include([../config/tls.m4])
-m4_include([../config/toolexeclibdir.m4])
-m4_include([../ltoptions.m4])
-m4_include([../ltsugar.m4])
-m4_include([../ltversion.m4])
-m4_include([../lt~obsolete.m4])
 m4_include([acinclude.m4])
diff --git a/libgomp/configure b/libgomp/configure
index b251a389d89..9682a836029 100755
--- a/libgomp/configure
+++ b/libgomp/configure
@@ -720,7 +720,6 @@ am__nodep
 AMDEPBACKSLASH
 AMDEP_FALSE
 AMDEP_TRUE
-am__quote
 am__include
 DEPDIR
 OBJEXT
@@ -811,7 +810,8 @@ PACKAGE_VERSION
 PACKAGE_TARNAME
 PACKAGE_NAME
 PATH_SEPARATOR
-SHELL'
+SHELL
+am__quote'
 ac_subst_files=''
 ac_user_opts='
 enable_option_checking
@@ -2870,7 +2870,7 @@ target_alias=${target_alias-$host_alias}
 #  -Wall:  turns on all automake warnings...
 #  -Wno-portability:  ...except this one, since GNU make is required.
 #  -Wno-override: ... and this one, since we do want this in testsuite.
-am__api_version='1.15'
+am__api_version='1.16'
 
 # Find a good install program.  We prefer a C program (faster),
 # so one script is as good as another.  But avoid the broken or
@@ -3386,8 +3386,8 @@ MAKEINFO=${MAKEINFO-"${am_missing_run}makeinfo"}
 
 # For better backward compatibility.  To be removed once Automake 1.9.x
 # dies out for good.  For more background, see:
-# <http://lists.gnu.org/archive/html/automake/2012-07/msg00001.html>
-# <http://lists.gnu.org/archive/html/automake/2012-07/msg00014.html>
+# <https://lists.gnu.org/archive/html/automake/2012-07/msg00001.html>
+# <https://lists.gnu.org/archive/html/automake/2012-07/msg00014.html>
 mkdir_p='$(MKDIR_P)'
 
 # We need awk for the "check" target (and possibly the TAP driver).  The
@@ -3438,7 +3438,7 @@ END
 Aborting the configuration process, to ensure you take notice of the issue.
 
 You can download and install GNU coreutils to get an 'rm' implementation
-that behaves properly: <http://www.gnu.org/software/coreutils/>.
+that behaves properly: <https://www.gnu.org/software/coreutils/>.
 
 If you want to complete the configuration process using your problematic
 'rm' anyway, export the environment variable ACCEPT_INFERIOR_RM_PROGRAM
@@ -4399,45 +4399,45 @@ DEPDIR="${am__leading_dot}deps"
 
 ac_config_commands="$ac_config_commands depfiles"
 
-
-am_make=${MAKE-make}
-cat > confinc << 'END'
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ${MAKE-make} supports the include directive" >&5
+$as_echo_n "checking whether ${MAKE-make} supports the include directive... " >&6; }
+cat > confinc.mk << 'END'
 am__doit:
-	@echo this is the am__doit target
+	@echo this is the am__doit target >confinc.out
 .PHONY: am__doit
 END
-# If we don't find an include directive, just comment out the code.
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for style of include used by $am_make" >&5
-$as_echo_n "checking for style of include used by $am_make... " >&6; }
 am__include="#"
 am__quote=
-_am_result=none
-# First try GNU make style include.
-echo "include confinc" > confmf
-# Ignore all kinds of additional output from 'make'.
-case `$am_make -s -f confmf 2> /dev/null` in #(
-*the\ am__doit\ target*)
-  am__include=include
-  am__quote=
-  _am_result=GNU
-  ;;
-esac
-# Now try BSD make style include.
-if test "$am__include" = "#"; then
-   echo '.include "confinc"' > confmf
-   case `$am_make -s -f confmf 2> /dev/null` in #(
-   *the\ am__doit\ target*)
-     am__include=.include
-     am__quote="\""
-     _am_result=BSD
+# BSD make does it like this.
+echo '.include "confinc.mk" # ignored' > confmf.BSD
+# Other make implementations (GNU, Solaris 10, AIX) do it like this.
+echo 'include confinc.mk # ignored' > confmf.GNU
+_am_result=no
+for s in GNU BSD; do
+  { echo "$as_me:$LINENO: ${MAKE-make} -f confmf.$s && cat confinc.out" >&5
+   (${MAKE-make} -f confmf.$s && cat confinc.out) >&5 2>&5
+   ac_status=$?
+   echo "$as_me:$LINENO: \$? = $ac_status" >&5
+   (exit $ac_status); }
+  case $?:`cat confinc.out 2>/dev/null` in #(
+  '0:this is the am__doit target') :
+    case $s in #(
+  BSD) :
+    am__include='.include' am__quote='"' ;; #(
+  *) :
+    am__include='include' am__quote='' ;;
+esac ;; #(
+  *) :
      ;;
-   esac
-fi
-
-
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $_am_result" >&5
-$as_echo "$_am_result" >&6; }
-rm -f confinc confmf
+esac
+  if test "$am__include" != "#"; then
+    _am_result="yes ($s style)"
+    break
+  fi
+done
+rm -f confinc.* confmf.*
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: ${_am_result}" >&5
+$as_echo "${_am_result}" >&6; }
 
 # Check whether --enable-dependency-tracking was given.
 if test "${enable_dependency_tracking+set}" = set; then :
@@ -17759,7 +17759,7 @@ CC="$CC"
 CXX="$CXX"
 GFORTRAN="$GFORTRAN"
 GDC="$GDC"
-AMDEP_TRUE="$AMDEP_TRUE" ac_aux_dir="$ac_aux_dir"
+AMDEP_TRUE="$AMDEP_TRUE" MAKE="${MAKE-make}"
 
 
 # The HP-UX ksh and POSIX shell print the target directory to stdout
@@ -18751,29 +18751,35 @@ esac ;;
   # Older Autoconf quotes --file arguments for eval, but not when files
   # are listed without --file.  Let's play safe and only enable the eval
   # if we detect the quoting.
-  case $CONFIG_FILES in
-  *\'*) eval set x "$CONFIG_FILES" ;;
-  *)   set x $CONFIG_FILES ;;
-  esac
+  # TODO: see whether this extra hack can be removed once we start
+  # requiring Autoconf 2.70 or later.
+  case $CONFIG_FILES in #(
+  *\'*) :
+    eval set x "$CONFIG_FILES" ;; #(
+  *) :
+    set x $CONFIG_FILES ;; #(
+  *) :
+     ;;
+esac
   shift
-  for mf
+  # Used to flag and report bootstrapping failures.
+  am_rc=0
+  for am_mf
   do
     # Strip MF so we end up with the name of the file.
-    mf=`echo "$mf" | sed -e 's/:.*$//'`
-    # Check whether this is an Automake generated Makefile or not.
-    # We used to match only the files named 'Makefile.in', but
-    # some people rename them; so instead we look at the file content.
-    # Grep'ing the first line is not enough: some people post-process
-    # each Makefile.in and add a new line on top of each file to say so.
-    # Grep'ing the whole file is not good either: AIX grep has a line
+    am_mf=`$as_echo "$am_mf" | sed -e 's/:.*$//'`
+    # Check whether this is an Automake generated Makefile which includes
+    # dependency-tracking related rules and includes.
+    # Grep'ing the whole file directly is not great: AIX grep has a line
     # limit of 2048, but all sed's we know have understand at least 4000.
-    if sed -n 's,^#.*generated by automake.*,X,p' "$mf" | grep X >/dev/null 2>&1; then
-      dirpart=`$as_dirname -- "$mf" ||
-$as_expr X"$mf" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
-	 X"$mf" : 'X\(//\)[^/]' \| \
-	 X"$mf" : 'X\(//\)$' \| \
-	 X"$mf" : 'X\(/\)' \| . 2>/dev/null ||
-$as_echo X"$mf" |
+    sed -n 's,^am--depfiles:.*,X,p' "$am_mf" | grep X >/dev/null 2>&1 \
+      || continue
+    am_dirpart=`$as_dirname -- "$am_mf" ||
+$as_expr X"$am_mf" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
+	 X"$am_mf" : 'X\(//\)[^/]' \| \
+	 X"$am_mf" : 'X\(//\)$' \| \
+	 X"$am_mf" : 'X\(/\)' \| . 2>/dev/null ||
+$as_echo X"$am_mf" |
     sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
 	    s//\1/
 	    q
@@ -18791,53 +18797,48 @@ $as_echo X"$mf" |
 	    q
 	  }
 	  s/.*/./; q'`
-    else
-      continue
-    fi
-    # Extract the definition of DEPDIR, am__include, and am__quote
-    # from the Makefile without running 'make'.
-    DEPDIR=`sed -n 's/^DEPDIR = //p' < "$mf"`
-    test -z "$DEPDIR" && continue
-    am__include=`sed -n 's/^am__include = //p' < "$mf"`
-    test -z "$am__include" && continue
-    am__quote=`sed -n 's/^am__quote = //p' < "$mf"`
-    # Find all dependency output files, they are included files with
-    # $(DEPDIR) in their names.  We invoke sed twice because it is the
-    # simplest approach to changing $(DEPDIR) to its actual value in the
-    # expansion.
-    for file in `sed -n "
-      s/^$am__include $am__quote\(.*(DEPDIR).*\)$am__quote"'$/\1/p' <"$mf" | \
-	 sed -e 's/\$(DEPDIR)/'"$DEPDIR"'/g'`; do
-      # Make sure the directory exists.
-      test -f "$dirpart/$file" && continue
-      fdir=`$as_dirname -- "$file" ||
-$as_expr X"$file" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
-	 X"$file" : 'X\(//\)[^/]' \| \
-	 X"$file" : 'X\(//\)$' \| \
-	 X"$file" : 'X\(/\)' \| . 2>/dev/null ||
-$as_echo X"$file" |
-    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
-	    s//\1/
-	    q
-	  }
-	  /^X\(\/\/\)[^/].*/{
+    am_filepart=`$as_basename -- "$am_mf" ||
+$as_expr X/"$am_mf" : '.*/\([^/][^/]*\)/*$' \| \
+	 X"$am_mf" : 'X\(//\)$' \| \
+	 X"$am_mf" : 'X\(/\)' \| . 2>/dev/null ||
+$as_echo X/"$am_mf" |
+    sed '/^.*\/\([^/][^/]*\)\/*$/{
 	    s//\1/
 	    q
 	  }
-	  /^X\(\/\/\)$/{
+	  /^X\/\(\/\/\)$/{
 	    s//\1/
 	    q
 	  }
-	  /^X\(\/\).*/{
+	  /^X\/\(\/\).*/{
 	    s//\1/
 	    q
 	  }
 	  s/.*/./; q'`
-      as_dir=$dirpart/$fdir; as_fn_mkdir_p
-      # echo "creating $dirpart/$file"
-      echo '# dummy' > "$dirpart/$file"
-    done
+    { echo "$as_me:$LINENO: cd "$am_dirpart" \
+      && sed -e '/# am--include-marker/d' "$am_filepart" \
+        | $MAKE -f - am--depfiles" >&5
+   (cd "$am_dirpart" \
+      && sed -e '/# am--include-marker/d' "$am_filepart" \
+        | $MAKE -f - am--depfiles) >&5 2>&5
+   ac_status=$?
+   echo "$as_me:$LINENO: \$? = $ac_status" >&5
+   (exit $ac_status); } || am_rc=$?
   done
+  if test $am_rc -ne 0; then
+    { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
+$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
+as_fn_error $? "Something went wrong bootstrapping makefile fragments
+    for automatic dependency tracking.  Try re-running configure with the
+    '--disable-dependency-tracking' option to at least be able to build
+    the package (albeit without support for automatic dependency tracking).
+See \`config.log' for more details" "$LINENO" 5; }
+  fi
+  { am_dirpart=; unset am_dirpart;}
+  { am_filepart=; unset am_filepart;}
+  { am_mf=; unset am_mf;}
+  { am_rc=; unset am_rc;}
+  rm -f conftest-deps.mk
 }
  ;;
     "libtool":C)
diff --git a/libgomp/ompd-support.c b/libgomp/ompd-support.c
index 27c5ad148e0..e769e55fd0f 100644
--- a/libgomp/ompd-support.c
+++ b/libgomp/ompd-support.c
@@ -33,6 +33,8 @@ const unsigned short gompd_sizeof_gomp_thread_handle
   __attribute__ ((used)) OMPD_SECTION = 0;
 #endif
 
+unsigned long gompd_thread_initial_tls_bias __attribute__ ((used));
+
 /* Get offset of the member m in struct t.  */
 #define gompd_get_offset(t, m) \
   const unsigned short gompd_access_##t##_##m __attribute__ ((used)) \
@@ -67,6 +69,9 @@ gompd_load (void)
   gompd_state |= OMPD_ENABLED;
   ompd_dll_locations = &ompd_dll_locations_array[0];
   ompd_dll_locations_valid ();
+
+  gompd_thread_initial_tls_bias = (unsigned long) ((char *) &gomp_tls_data
+  						   - (char *) pthread_self ());
 }
 
 #ifndef __ELF__
diff --git a/libgomp/ompd-threads.c b/libgomp/ompd-threads.c
new file mode 100644
index 00000000000..436a7d617fe
--- /dev/null
+++ b/libgomp/ompd-threads.c
@@ -0,0 +1,216 @@
+/* Copyright (C) The GNU Toolchain Authors.
+   Contributed by Ahmed Sayed <ahmedsayedmousse@gmail.com>.
+   This file is part of the GNU Offloading and Multi Processing Library
+   (libgomp).
+
+   Libgomp is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
+   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+   more details.
+
+   Under Section 7 of GPL version 3, you are granted additional
+   permissions described in the GCC Runtime Library Exception, version
+   3.1, as published by the Free Software Foundation.
+
+   You should have received a copy of the GNU General Public License and
+   a copy of the GCC Runtime Library Exception along with this program;
+   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/* This file contains the implementation of functions defined in
+   Section 5.5 ThreadHandles. */
+
+
+#include "ompd-helper.h"
+
+ompd_rc_t
+ompd_get_thread_in_parallel (ompd_parallel_handle_t *parallel_handle,
+			     int thread_num,
+			     ompd_thread_handle_t **thread_handle)
+{
+
+  if (parallel_handle == NULL)
+    return ompd_rc_stale_handle;
+  CHECK (parallel_handle->ah);
+
+  ompd_address_space_context_t *context = parallel_handle->ah->context;
+  ompd_rc_t ret;
+
+  ompd_word_t team_size_var = 1;
+  if (parallel_handle->th.address)
+    gompd_get_team_size(parallel_handle, &team_size_var);
+
+  if (thread_num < 0 || thread_num >= team_size_var)
+    return ompd_rc_bad_input;
+
+  ompd_word_t temp_offset;
+  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
+  ompd_addr_t temp_addr;
+
+  ACCESS_VALUE (context, NULL, "gompd_access_gomp_thread_pool_threads",
+		temp_offset, 1, ret, symbol_addr, temp_symbol_addr, temp_addr);
+
+  symbol_addr.address += thread_num * target_sizes.sizeof_pointer;
+
+  DEREFERENCE (context, NULL, symbol_addr, target_sizes.sizeof_pointer, 1,
+	       temp_addr, ret, 1);
+
+  ret = callbacks->alloc_memory (sizeof (ompd_thread_handle_t),
+				 (void **) thread_handle);
+
+  CHECK_RET (ret);
+
+  if (symbol_addr.address == 0)
+    return ompd_rc_unsupported;
+
+  (*thread_handle)->th = symbol_addr;
+  (*thread_handle)->ah = parallel_handle->ah;
+  return ret;
+}
+
+/* The ompd_get_thread_handle function that maps a native thread to an
+   OMPD thread handle.  */
+
+ompd_rc_t
+ompd_get_thread_handle (ompd_address_space_handle_t *handle,
+			ompd_thread_id_t kind, ompd_size_t sizeof_thread_id,
+			const void *thread_id,
+			ompd_thread_handle_t **thread_handle)
+{
+  CHECK (handle);
+  if (kind != OMPD_THREAD_ID_PTHREAD)
+    return ompd_rc_unsupported;
+
+  ompd_address_space_context_t *context = handle->context;
+  ompd_thread_context_t *tcontext;
+  ompd_rc_t ret;
+
+  ret = callbacks->get_thread_context_for_thread_id (context, kind,
+						     sizeof_thread_id,
+						     thread_id, &tcontext);
+  CHECK_RET (ret);
+
+  ompd_size_t temp_symbol_size, symbol_size;
+  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
+
+  GET_VALUE (context, NULL, "gompd_sizeof_gomp_thread", symbol_size,
+	     temp_symbol_size, target_sizes.sizeof_short, 1, ret,
+	     temp_symbol_addr);
+
+  GET_VALUE (context, tcontext, "gomp_tls_data", symbol_addr.address,
+	     temp_symbol_addr.address, symbol_size, 1, ret, symbol_addr);
+
+  ret = callbacks->alloc_memory (sizeof (ompd_thread_handle_t),
+				 (void **) thread_handle);
+
+  CHECK_RET (ret);
+
+  (*thread_handle)->ah = handle;
+  (*thread_handle)->th = symbol_addr;
+  (*thread_handle)->thread_context = tcontext;
+  return ret;
+}
+
+
+ompd_rc_t
+ompd_rel_thread_handle (ompd_thread_handle_t *thread_handle)
+{
+  if (thread_handle == NULL)
+    return ompd_rc_stale_handle;
+
+  ompd_rc_t ret;
+  ret = callbacks->free_memory ((void *) thread_handle);
+  if (ret != ompd_rc_ok)
+  return ret;
+
+  return ompd_rc_ok;
+}
+
+
+/* return -1, 0 or 1 for thread_handle_1 <, == or > thread_handle_2.  */
+ompd_rc_t
+ompd_thread_handle_compare (ompd_thread_handle_t *thread_handle_1,
+			    ompd_thread_handle_t *thread_handle_2,
+			    int	*cmp_value )
+{
+
+  if (thread_handle_1 == NULL || thread_handle_2 == NULL)
+    return ompd_rc_stale_handle;
+  if (cmp_value == NULL)
+    return ompd_rc_bad_input;
+  if (thread_handle_1->ah->kind != thread_handle_2->ah->kind)
+    return ompd_rc_bad_input;
+
+  *cmp_value = thread_handle_1->th.address - thread_handle_2->th.address;
+  return ompd_rc_ok;
+}
+
+
+ompd_rc_t
+ompd_get_thread_id (ompd_thread_handle_t *thread_handle, ompd_thread_id_t kind,
+		    ompd_size_t sizeof_thread_id, void *thread_id)
+{
+  if (kind != OMPD_THREAD_ID_PTHREAD)
+    return ompd_rc_unsupported;
+  if (thread_id == NULL)
+    return ompd_rc_bad_input;
+  if (thread_handle == NULL)
+    return ompd_rc_stale_handle;
+
+  CHECK (thread_handle->ah);
+  ompd_address_space_context_t *context = thread_handle->ah->context;
+
+  ompd_rc_t ret;
+  ompd_address_t taddr = thread_handle->th;
+  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
+  ompd_size_t temp_symbol_size, symbol_size;
+  ompd_word_t temp_offset, offset;
+
+  GET_VALUE (context, NULL, "gompd_sizeof_gomp_thread_handle", symbol_size,
+	     temp_symbol_size, target_sizes.sizeof_short, 1, ret, symbol_addr);
+
+  if (symbol_size == 0)
+    goto use_tls_bias;
+
+  if (sizeof_thread_id != symbol_size)
+    return ompd_rc_bad_input;
+
+  GET_VALUE (context, NULL, "gompd_access_gomp_thread_handle", offset,
+	     temp_offset, target_sizes.sizeof_short, 1, ret, symbol_addr);
+  taddr.address += offset;
+
+  ret = callbacks->read_memory (context, NULL, &taddr, symbol_size, thread_id);
+  return ret;
+
+use_tls_bias:
+
+  GET_VALUE (context, NULL, "gompd_thread_initial_tls_bias", offset, temp_offset,
+	     target_sizes.sizeof_long, 1, ret, symbol_addr);
+
+  ret = callbacks->symbol_addr_lookup (context, NULL,"gomp_tls_data",
+				       &symbol_addr, NULL);
+  ret = callbacks->device_to_host (context, &temp_symbol_addr.address,
+				   target_sizes.sizeof_long_long, 1,
+				   &symbol_addr.address);
+  CHECK_RET (ret);
+
+  taddr.address = symbol_addr.address + offset;
+  ret = callbacks->read_memory (context, NULL, &taddr,
+				target_sizes.sizeof_long_long, thread_id);
+  return ret;
+}
+
+
+/* OMPD doesn't support GPUs for now.  */
+ompd_rc_t ompd_get_device_from_thread (ompd_thread_handle_t *thread_handle,
+				       ompd_address_space_handle_t **device)
+{
+  if (thread_handle == NULL)
+    return ompd_rc_stale_handle;
+  return ompd_rc_unsupported;
+}
diff --git a/libgomp/team.c b/libgomp/team.c
index d53246961b7..8e18fd6af63 100644
--- a/libgomp/team.c
+++ b/libgomp/team.c
@@ -77,6 +77,7 @@ gomp_thread_start (void *xdata)
   void *local_data;
 
   ompd_bp_thread_begin ();
+
 #if defined HAVE_TLS || defined USE_EMUTLS
   thr = &gomp_tls_data;
 #else
@@ -313,6 +314,9 @@ gomp_free_thread (void *arg __attribute__((unused)))
       gomp_end_task ();
       free (task);
     }
+
+  ompd_bp_thread_end ();
+
 }
 
 /* Launch a team.  */
diff --git a/libgomp/testsuite/Makefile.in b/libgomp/testsuite/Makefile.in
index 048844f0a40..76cd09b0faf 100644
--- a/libgomp/testsuite/Makefile.in
+++ b/libgomp/testsuite/Makefile.in
@@ -90,21 +90,8 @@ host_triplet = @host@
 target_triplet = @target@
 subdir = testsuite
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
-am__aclocal_m4_deps = $(top_srcdir)/../config/acx.m4 \
-	$(top_srcdir)/../config/ax_count_cpus.m4 \
-	$(top_srcdir)/../config/depstand.m4 \
-	$(top_srcdir)/../config/enable.m4 \
-	$(top_srcdir)/../config/futex.m4 \
-	$(top_srcdir)/../config/lead-dot.m4 \
-	$(top_srcdir)/../config/lthostflags.m4 \
-	$(top_srcdir)/../config/multi.m4 \
-	$(top_srcdir)/../config/override.m4 \
-	$(top_srcdir)/../config/tls.m4 \
-	$(top_srcdir)/../config/toolexeclibdir.m4 \
-	$(top_srcdir)/../ltoptions.m4 $(top_srcdir)/../ltsugar.m4 \
-	$(top_srcdir)/../ltversion.m4 $(top_srcdir)/../lt~obsolete.m4 \
-	$(top_srcdir)/acinclude.m4 $(top_srcdir)/../libtool.m4 \
-	$(top_srcdir)/../config/cet.m4 \
+am__aclocal_m4_deps = $(top_srcdir)/acinclude.m4 \
+	$(top_srcdir)/../libtool.m4 $(top_srcdir)/../config/cet.m4 \
 	$(top_srcdir)/plugin/configfrag.ac $(top_srcdir)/configure.ac
 am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
 	$(ACLOCAL_M4)
@@ -249,7 +236,6 @@ dvidir = @dvidir@
 enable_shared = @enable_shared@
 enable_static = @enable_static@
 exec_prefix = @exec_prefix@
-get_gcc_base_ver = @get_gcc_base_ver@
 host = @host@
 host_alias = @host_alias@
 host_cpu = @host_cpu@
@@ -265,10 +251,8 @@ libtool_VERSION = @libtool_VERSION@
 link_gomp = @link_gomp@
 localedir = @localedir@
 localstatedir = @localstatedir@
-lt_host_flags = @lt_host_flags@
 mandir = @mandir@
 mkdir_p = @mkdir_p@
-multi_basedir = @multi_basedir@
 offload_additional_lib_paths = @offload_additional_lib_paths@
 offload_additional_options = @offload_additional_options@
 offload_plugins = @offload_plugins@
@@ -278,6 +262,7 @@ pdfdir = @pdfdir@
 prefix = @prefix@
 program_transform_name = @program_transform_name@
 psdir = @psdir@
+runstatedir = @runstatedir@
 sbindir = @sbindir@
 sharedstatedir = @sharedstatedir@
 srcdir = @srcdir@

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [patch] libgompd: Add thread handles
@ 2022-07-01  0:00 Ahmed Sayed Mousse
  0 siblings, 0 replies; 9+ messages in thread
From: Ahmed Sayed Mousse @ 2022-07-01  0:00 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 464 bytes --]

/This patch is the initial implementation of OpenMP-API specs book section //20.5.5 with title "Thread Handles". /libgomp/ChangeLog /2022-07-01 
Ahmed Sayed <ahmedsayedmousse@gmail.com> //	* Makefile.am 
(libgompd_la_SOURCES): Add ompd-threads.c.///* Makefile.in: Regenerate. * team.c ( gomp_free_thread ): Called 
ompd_bp_thread_end ()./* ompd-support.c ( gompd_thread_initial_tls_bias 
): New Variable. (gompd_load): Initialize gompd_thread_initial_tls_bias.///

[-- Attachment #2: diff.txt --]
[-- Type: text/plain, Size: 25990 bytes --]

diff --git a/libgomp/Makefile.am b/libgomp/Makefile.am
index 6d913a93e7f..23f5bede1bf 100644
--- a/libgomp/Makefile.am
+++ b/libgomp/Makefile.am
@@ -94,7 +94,7 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c error.c \
 	priority_queue.c affinity-fmt.c teams.c allocator.c oacc-profiling.c \
 	oacc-target.c ompd-support.c
 
-libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c
+libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c ompd-threads.c
 
 include $(top_srcdir)/plugin/Makefrag.am
 
diff --git a/libgomp/Makefile.in b/libgomp/Makefile.in
index 40f896b5f03..8bbc46cca25 100644
--- a/libgomp/Makefile.in
+++ b/libgomp/Makefile.in
@@ -133,21 +133,8 @@ target_triplet = @target@
 @USE_FORTRAN_TRUE@am__append_7 = openacc.f90
 subdir = .
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
-am__aclocal_m4_deps = $(top_srcdir)/../config/acx.m4 \
-	$(top_srcdir)/../config/ax_count_cpus.m4 \
-	$(top_srcdir)/../config/depstand.m4 \
-	$(top_srcdir)/../config/enable.m4 \
-	$(top_srcdir)/../config/futex.m4 \
-	$(top_srcdir)/../config/lead-dot.m4 \
-	$(top_srcdir)/../config/lthostflags.m4 \
-	$(top_srcdir)/../config/multi.m4 \
-	$(top_srcdir)/../config/override.m4 \
-	$(top_srcdir)/../config/tls.m4 \
-	$(top_srcdir)/../config/toolexeclibdir.m4 \
-	$(top_srcdir)/../ltoptions.m4 $(top_srcdir)/../ltsugar.m4 \
-	$(top_srcdir)/../ltversion.m4 $(top_srcdir)/../lt~obsolete.m4 \
-	$(top_srcdir)/acinclude.m4 $(top_srcdir)/../libtool.m4 \
-	$(top_srcdir)/../config/cet.m4 \
+am__aclocal_m4_deps = $(top_srcdir)/acinclude.m4 \
+	$(top_srcdir)/../libtool.m4 $(top_srcdir)/../config/cet.m4 \
 	$(top_srcdir)/plugin/configfrag.ac $(top_srcdir)/configure.ac
 am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
 	$(ACLOCAL_M4)
@@ -233,7 +220,8 @@ am_libgomp_la_OBJECTS = alloc.lo atomic.lo barrier.lo critical.lo \
 	affinity-fmt.lo teams.lo allocator.lo oacc-profiling.lo \
 	oacc-target.lo ompd-support.lo $(am__objects_1)
 libgomp_la_OBJECTS = $(am_libgomp_la_OBJECTS)
-am_libgompd_la_OBJECTS = ompd-init.lo ompd-helper.lo ompd-icv.lo
+am_libgompd_la_OBJECTS = ompd-init.lo ompd-helper.lo ompd-icv.lo \
+	ompd-threads.lo
 libgompd_la_OBJECTS = $(am_libgompd_la_OBJECTS)
 AM_V_P = $(am__v_P_@AM_V@)
 am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
@@ -485,7 +473,6 @@ dvidir = @dvidir@
 enable_shared = @enable_shared@
 enable_static = @enable_static@
 exec_prefix = @exec_prefix@
-get_gcc_base_ver = @get_gcc_base_ver@
 host = @host@
 host_alias = @host_alias@
 host_cpu = @host_cpu@
@@ -501,10 +488,8 @@ libtool_VERSION = @libtool_VERSION@
 link_gomp = @link_gomp@
 localedir = @localedir@
 localstatedir = @localstatedir@
-lt_host_flags = @lt_host_flags@
 mandir = @mandir@
 mkdir_p = @mkdir_p@
-multi_basedir = @multi_basedir@
 offload_additional_lib_paths = @offload_additional_lib_paths@
 offload_additional_options = @offload_additional_options@
 offload_plugins = @offload_plugins@
@@ -514,6 +499,7 @@ pdfdir = @pdfdir@
 prefix = @prefix@
 program_transform_name = @program_transform_name@
 psdir = @psdir@
+runstatedir = @runstatedir@
 sbindir = @sbindir@
 sharedstatedir = @sharedstatedir@
 srcdir = @srcdir@
@@ -583,7 +569,7 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c \
 	oacc-async.c oacc-plugin.c oacc-cuda.c priority_queue.c \
 	affinity-fmt.c teams.c allocator.c oacc-profiling.c \
 	oacc-target.c ompd-support.c $(am__append_7)
-libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c
+libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c ompd-threads.c
 
 # Nvidia PTX OpenACC plugin.
 @PLUGIN_NVPTX_TRUE@libgomp_plugin_nvptx_version_info = -version-info $(libtool_VERSION)
@@ -801,6 +787,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-icv.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-init.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-support.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-threads.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ordered.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/parallel.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/priority_queue.Plo@am__quote@
diff --git a/libgomp/aclocal.m4 b/libgomp/aclocal.m4
index 55d9d71895a..41915216beb 100644
--- a/libgomp/aclocal.m4
+++ b/libgomp/aclocal.m4
@@ -626,6 +626,25 @@ if test x"${install_sh+set}" != xset; then
 fi
 AC_SUBST([install_sh])])
 
+# Copyright (C) 2003-2017 Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# Check whether the underlying file-system supports filenames
+# with a leading dot.  For instance MS-DOS doesn't.
+AC_DEFUN([AM_SET_LEADING_DOT],
+[rm -rf .tst 2>/dev/null
+mkdir .tst 2>/dev/null
+if test -d .tst; then
+  am__leading_dot=.
+else
+  am__leading_dot=_
+fi
+rmdir .tst 2>/dev/null
+AC_SUBST([am__leading_dot])])
+
 # Add --enable-maintainer-mode option to configure.         -*- Autoconf -*-
 # From Jim Meyering
 
@@ -1167,19 +1186,4 @@ AC_SUBST([am__tar])
 AC_SUBST([am__untar])
 ]) # _AM_PROG_TAR
 
-m4_include([../config/acx.m4])
-m4_include([../config/ax_count_cpus.m4])
-m4_include([../config/depstand.m4])
-m4_include([../config/enable.m4])
-m4_include([../config/futex.m4])
-m4_include([../config/lead-dot.m4])
-m4_include([../config/lthostflags.m4])
-m4_include([../config/multi.m4])
-m4_include([../config/override.m4])
-m4_include([../config/tls.m4])
-m4_include([../config/toolexeclibdir.m4])
-m4_include([../ltoptions.m4])
-m4_include([../ltsugar.m4])
-m4_include([../ltversion.m4])
-m4_include([../lt~obsolete.m4])
 m4_include([acinclude.m4])
diff --git a/libgomp/configure b/libgomp/configure
index b251a389d89..9682a836029 100755
--- a/libgomp/configure
+++ b/libgomp/configure
@@ -720,7 +720,6 @@ am__nodep
 AMDEPBACKSLASH
 AMDEP_FALSE
 AMDEP_TRUE
-am__quote
 am__include
 DEPDIR
 OBJEXT
@@ -811,7 +810,8 @@ PACKAGE_VERSION
 PACKAGE_TARNAME
 PACKAGE_NAME
 PATH_SEPARATOR
-SHELL'
+SHELL
+am__quote'
 ac_subst_files=''
 ac_user_opts='
 enable_option_checking
@@ -2870,7 +2870,7 @@ target_alias=${target_alias-$host_alias}
 #  -Wall:  turns on all automake warnings...
 #  -Wno-portability:  ...except this one, since GNU make is required.
 #  -Wno-override: ... and this one, since we do want this in testsuite.
-am__api_version='1.15'
+am__api_version='1.16'
 
 # Find a good install program.  We prefer a C program (faster),
 # so one script is as good as another.  But avoid the broken or
@@ -3386,8 +3386,8 @@ MAKEINFO=${MAKEINFO-"${am_missing_run}makeinfo"}
 
 # For better backward compatibility.  To be removed once Automake 1.9.x
 # dies out for good.  For more background, see:
-# <http://lists.gnu.org/archive/html/automake/2012-07/msg00001.html>
-# <http://lists.gnu.org/archive/html/automake/2012-07/msg00014.html>
+# <https://lists.gnu.org/archive/html/automake/2012-07/msg00001.html>
+# <https://lists.gnu.org/archive/html/automake/2012-07/msg00014.html>
 mkdir_p='$(MKDIR_P)'
 
 # We need awk for the "check" target (and possibly the TAP driver).  The
@@ -3438,7 +3438,7 @@ END
 Aborting the configuration process, to ensure you take notice of the issue.
 
 You can download and install GNU coreutils to get an 'rm' implementation
-that behaves properly: <http://www.gnu.org/software/coreutils/>.
+that behaves properly: <https://www.gnu.org/software/coreutils/>.
 
 If you want to complete the configuration process using your problematic
 'rm' anyway, export the environment variable ACCEPT_INFERIOR_RM_PROGRAM
@@ -4399,45 +4399,45 @@ DEPDIR="${am__leading_dot}deps"
 
 ac_config_commands="$ac_config_commands depfiles"
 
-
-am_make=${MAKE-make}
-cat > confinc << 'END'
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ${MAKE-make} supports the include directive" >&5
+$as_echo_n "checking whether ${MAKE-make} supports the include directive... " >&6; }
+cat > confinc.mk << 'END'
 am__doit:
-	@echo this is the am__doit target
+	@echo this is the am__doit target >confinc.out
 .PHONY: am__doit
 END
-# If we don't find an include directive, just comment out the code.
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for style of include used by $am_make" >&5
-$as_echo_n "checking for style of include used by $am_make... " >&6; }
 am__include="#"
 am__quote=
-_am_result=none
-# First try GNU make style include.
-echo "include confinc" > confmf
-# Ignore all kinds of additional output from 'make'.
-case `$am_make -s -f confmf 2> /dev/null` in #(
-*the\ am__doit\ target*)
-  am__include=include
-  am__quote=
-  _am_result=GNU
-  ;;
-esac
-# Now try BSD make style include.
-if test "$am__include" = "#"; then
-   echo '.include "confinc"' > confmf
-   case `$am_make -s -f confmf 2> /dev/null` in #(
-   *the\ am__doit\ target*)
-     am__include=.include
-     am__quote="\""
-     _am_result=BSD
+# BSD make does it like this.
+echo '.include "confinc.mk" # ignored' > confmf.BSD
+# Other make implementations (GNU, Solaris 10, AIX) do it like this.
+echo 'include confinc.mk # ignored' > confmf.GNU
+_am_result=no
+for s in GNU BSD; do
+  { echo "$as_me:$LINENO: ${MAKE-make} -f confmf.$s && cat confinc.out" >&5
+   (${MAKE-make} -f confmf.$s && cat confinc.out) >&5 2>&5
+   ac_status=$?
+   echo "$as_me:$LINENO: \$? = $ac_status" >&5
+   (exit $ac_status); }
+  case $?:`cat confinc.out 2>/dev/null` in #(
+  '0:this is the am__doit target') :
+    case $s in #(
+  BSD) :
+    am__include='.include' am__quote='"' ;; #(
+  *) :
+    am__include='include' am__quote='' ;;
+esac ;; #(
+  *) :
      ;;
-   esac
-fi
-
-
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $_am_result" >&5
-$as_echo "$_am_result" >&6; }
-rm -f confinc confmf
+esac
+  if test "$am__include" != "#"; then
+    _am_result="yes ($s style)"
+    break
+  fi
+done
+rm -f confinc.* confmf.*
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: ${_am_result}" >&5
+$as_echo "${_am_result}" >&6; }
 
 # Check whether --enable-dependency-tracking was given.
 if test "${enable_dependency_tracking+set}" = set; then :
@@ -17759,7 +17759,7 @@ CC="$CC"
 CXX="$CXX"
 GFORTRAN="$GFORTRAN"
 GDC="$GDC"
-AMDEP_TRUE="$AMDEP_TRUE" ac_aux_dir="$ac_aux_dir"
+AMDEP_TRUE="$AMDEP_TRUE" MAKE="${MAKE-make}"
 
 
 # The HP-UX ksh and POSIX shell print the target directory to stdout
@@ -18751,29 +18751,35 @@ esac ;;
   # Older Autoconf quotes --file arguments for eval, but not when files
   # are listed without --file.  Let's play safe and only enable the eval
   # if we detect the quoting.
-  case $CONFIG_FILES in
-  *\'*) eval set x "$CONFIG_FILES" ;;
-  *)   set x $CONFIG_FILES ;;
-  esac
+  # TODO: see whether this extra hack can be removed once we start
+  # requiring Autoconf 2.70 or later.
+  case $CONFIG_FILES in #(
+  *\'*) :
+    eval set x "$CONFIG_FILES" ;; #(
+  *) :
+    set x $CONFIG_FILES ;; #(
+  *) :
+     ;;
+esac
   shift
-  for mf
+  # Used to flag and report bootstrapping failures.
+  am_rc=0
+  for am_mf
   do
     # Strip MF so we end up with the name of the file.
-    mf=`echo "$mf" | sed -e 's/:.*$//'`
-    # Check whether this is an Automake generated Makefile or not.
-    # We used to match only the files named 'Makefile.in', but
-    # some people rename them; so instead we look at the file content.
-    # Grep'ing the first line is not enough: some people post-process
-    # each Makefile.in and add a new line on top of each file to say so.
-    # Grep'ing the whole file is not good either: AIX grep has a line
+    am_mf=`$as_echo "$am_mf" | sed -e 's/:.*$//'`
+    # Check whether this is an Automake generated Makefile which includes
+    # dependency-tracking related rules and includes.
+    # Grep'ing the whole file directly is not great: AIX grep has a line
     # limit of 2048, but all sed's we know have understand at least 4000.
-    if sed -n 's,^#.*generated by automake.*,X,p' "$mf" | grep X >/dev/null 2>&1; then
-      dirpart=`$as_dirname -- "$mf" ||
-$as_expr X"$mf" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
-	 X"$mf" : 'X\(//\)[^/]' \| \
-	 X"$mf" : 'X\(//\)$' \| \
-	 X"$mf" : 'X\(/\)' \| . 2>/dev/null ||
-$as_echo X"$mf" |
+    sed -n 's,^am--depfiles:.*,X,p' "$am_mf" | grep X >/dev/null 2>&1 \
+      || continue
+    am_dirpart=`$as_dirname -- "$am_mf" ||
+$as_expr X"$am_mf" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
+	 X"$am_mf" : 'X\(//\)[^/]' \| \
+	 X"$am_mf" : 'X\(//\)$' \| \
+	 X"$am_mf" : 'X\(/\)' \| . 2>/dev/null ||
+$as_echo X"$am_mf" |
     sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
 	    s//\1/
 	    q
@@ -18791,53 +18797,48 @@ $as_echo X"$mf" |
 	    q
 	  }
 	  s/.*/./; q'`
-    else
-      continue
-    fi
-    # Extract the definition of DEPDIR, am__include, and am__quote
-    # from the Makefile without running 'make'.
-    DEPDIR=`sed -n 's/^DEPDIR = //p' < "$mf"`
-    test -z "$DEPDIR" && continue
-    am__include=`sed -n 's/^am__include = //p' < "$mf"`
-    test -z "$am__include" && continue
-    am__quote=`sed -n 's/^am__quote = //p' < "$mf"`
-    # Find all dependency output files, they are included files with
-    # $(DEPDIR) in their names.  We invoke sed twice because it is the
-    # simplest approach to changing $(DEPDIR) to its actual value in the
-    # expansion.
-    for file in `sed -n "
-      s/^$am__include $am__quote\(.*(DEPDIR).*\)$am__quote"'$/\1/p' <"$mf" | \
-	 sed -e 's/\$(DEPDIR)/'"$DEPDIR"'/g'`; do
-      # Make sure the directory exists.
-      test -f "$dirpart/$file" && continue
-      fdir=`$as_dirname -- "$file" ||
-$as_expr X"$file" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
-	 X"$file" : 'X\(//\)[^/]' \| \
-	 X"$file" : 'X\(//\)$' \| \
-	 X"$file" : 'X\(/\)' \| . 2>/dev/null ||
-$as_echo X"$file" |
-    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
-	    s//\1/
-	    q
-	  }
-	  /^X\(\/\/\)[^/].*/{
+    am_filepart=`$as_basename -- "$am_mf" ||
+$as_expr X/"$am_mf" : '.*/\([^/][^/]*\)/*$' \| \
+	 X"$am_mf" : 'X\(//\)$' \| \
+	 X"$am_mf" : 'X\(/\)' \| . 2>/dev/null ||
+$as_echo X/"$am_mf" |
+    sed '/^.*\/\([^/][^/]*\)\/*$/{
 	    s//\1/
 	    q
 	  }
-	  /^X\(\/\/\)$/{
+	  /^X\/\(\/\/\)$/{
 	    s//\1/
 	    q
 	  }
-	  /^X\(\/\).*/{
+	  /^X\/\(\/\).*/{
 	    s//\1/
 	    q
 	  }
 	  s/.*/./; q'`
-      as_dir=$dirpart/$fdir; as_fn_mkdir_p
-      # echo "creating $dirpart/$file"
-      echo '# dummy' > "$dirpart/$file"
-    done
+    { echo "$as_me:$LINENO: cd "$am_dirpart" \
+      && sed -e '/# am--include-marker/d' "$am_filepart" \
+        | $MAKE -f - am--depfiles" >&5
+   (cd "$am_dirpart" \
+      && sed -e '/# am--include-marker/d' "$am_filepart" \
+        | $MAKE -f - am--depfiles) >&5 2>&5
+   ac_status=$?
+   echo "$as_me:$LINENO: \$? = $ac_status" >&5
+   (exit $ac_status); } || am_rc=$?
   done
+  if test $am_rc -ne 0; then
+    { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
+$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
+as_fn_error $? "Something went wrong bootstrapping makefile fragments
+    for automatic dependency tracking.  Try re-running configure with the
+    '--disable-dependency-tracking' option to at least be able to build
+    the package (albeit without support for automatic dependency tracking).
+See \`config.log' for more details" "$LINENO" 5; }
+  fi
+  { am_dirpart=; unset am_dirpart;}
+  { am_filepart=; unset am_filepart;}
+  { am_mf=; unset am_mf;}
+  { am_rc=; unset am_rc;}
+  rm -f conftest-deps.mk
 }
  ;;
     "libtool":C)
diff --git a/libgomp/ompd-support.c b/libgomp/ompd-support.c
index 27c5ad148e0..e769e55fd0f 100644
--- a/libgomp/ompd-support.c
+++ b/libgomp/ompd-support.c
@@ -33,6 +33,8 @@ const unsigned short gompd_sizeof_gomp_thread_handle
   __attribute__ ((used)) OMPD_SECTION = 0;
 #endif
 
+unsigned long gompd_thread_initial_tls_bias __attribute__ ((used));
+
 /* Get offset of the member m in struct t.  */
 #define gompd_get_offset(t, m) \
   const unsigned short gompd_access_##t##_##m __attribute__ ((used)) \
@@ -67,6 +69,9 @@ gompd_load (void)
   gompd_state |= OMPD_ENABLED;
   ompd_dll_locations = &ompd_dll_locations_array[0];
   ompd_dll_locations_valid ();
+
+  gompd_thread_initial_tls_bias = (unsigned long) ((char *) &gomp_tls_data
+  						   - (char *) pthread_self ());
 }
 
 #ifndef __ELF__
diff --git a/libgomp/ompd-threads.c b/libgomp/ompd-threads.c
new file mode 100644
index 00000000000..436a7d617fe
--- /dev/null
+++ b/libgomp/ompd-threads.c
@@ -0,0 +1,216 @@
+/* Copyright (C) The GNU Toolchain Authors.
+   Contributed by Ahmed Sayed <ahmedsayedmousse@gmail.com>.
+   This file is part of the GNU Offloading and Multi Processing Library
+   (libgomp).
+
+   Libgomp is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
+   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+   more details.
+
+   Under Section 7 of GPL version 3, you are granted additional
+   permissions described in the GCC Runtime Library Exception, version
+   3.1, as published by the Free Software Foundation.
+
+   You should have received a copy of the GNU General Public License and
+   a copy of the GCC Runtime Library Exception along with this program;
+   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/* This file contains the implementation of functions defined in
+   Section 5.5 ThreadHandles. */
+
+
+#include "ompd-helper.h"
+
+ompd_rc_t
+ompd_get_thread_in_parallel (ompd_parallel_handle_t *parallel_handle,
+			     int thread_num,
+			     ompd_thread_handle_t **thread_handle)
+{
+
+  if (parallel_handle == NULL)
+    return ompd_rc_stale_handle;
+  CHECK (parallel_handle->ah);
+
+  ompd_address_space_context_t *context = parallel_handle->ah->context;
+  ompd_rc_t ret;
+
+  ompd_word_t team_size_var = 1;
+  if (parallel_handle->th.address)
+    gompd_get_team_size(parallel_handle, &team_size_var);
+
+  if (thread_num < 0 || thread_num >= team_size_var)
+    return ompd_rc_bad_input;
+
+  ompd_word_t temp_offset;
+  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
+  ompd_addr_t temp_addr;
+
+  ACCESS_VALUE (context, NULL, "gompd_access_gomp_thread_pool_threads",
+		temp_offset, 1, ret, symbol_addr, temp_symbol_addr, temp_addr);
+
+  symbol_addr.address += thread_num * target_sizes.sizeof_pointer;
+
+  DEREFERENCE (context, NULL, symbol_addr, target_sizes.sizeof_pointer, 1,
+	       temp_addr, ret, 1);
+
+  ret = callbacks->alloc_memory (sizeof (ompd_thread_handle_t),
+				 (void **) thread_handle);
+
+  CHECK_RET (ret);
+
+  if (symbol_addr.address == 0)
+    return ompd_rc_unsupported;
+
+  (*thread_handle)->th = symbol_addr;
+  (*thread_handle)->ah = parallel_handle->ah;
+  return ret;
+}
+
+/* The ompd_get_thread_handle function that maps a native thread to an
+   OMPD thread handle.  */
+
+ompd_rc_t
+ompd_get_thread_handle (ompd_address_space_handle_t *handle,
+			ompd_thread_id_t kind, ompd_size_t sizeof_thread_id,
+			const void *thread_id,
+			ompd_thread_handle_t **thread_handle)
+{
+  CHECK (handle);
+  if (kind != OMPD_THREAD_ID_PTHREAD)
+    return ompd_rc_unsupported;
+
+  ompd_address_space_context_t *context = handle->context;
+  ompd_thread_context_t *tcontext;
+  ompd_rc_t ret;
+
+  ret = callbacks->get_thread_context_for_thread_id (context, kind,
+						     sizeof_thread_id,
+						     thread_id, &tcontext);
+  CHECK_RET (ret);
+
+  ompd_size_t temp_symbol_size, symbol_size;
+  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
+
+  GET_VALUE (context, NULL, "gompd_sizeof_gomp_thread", symbol_size,
+	     temp_symbol_size, target_sizes.sizeof_short, 1, ret,
+	     temp_symbol_addr);
+
+  GET_VALUE (context, tcontext, "gomp_tls_data", symbol_addr.address,
+	     temp_symbol_addr.address, symbol_size, 1, ret, symbol_addr);
+
+  ret = callbacks->alloc_memory (sizeof (ompd_thread_handle_t),
+				 (void **) thread_handle);
+
+  CHECK_RET (ret);
+
+  (*thread_handle)->ah = handle;
+  (*thread_handle)->th = symbol_addr;
+  (*thread_handle)->thread_context = tcontext;
+  return ret;
+}
+
+
+ompd_rc_t
+ompd_rel_thread_handle (ompd_thread_handle_t *thread_handle)
+{
+  if (thread_handle == NULL)
+    return ompd_rc_stale_handle;
+
+  ompd_rc_t ret;
+  ret = callbacks->free_memory ((void *) thread_handle);
+  if (ret != ompd_rc_ok)
+  return ret;
+
+  return ompd_rc_ok;
+}
+
+
+/* return -1, 0 or 1 for thread_handle_1 <, == or > thread_handle_2.  */
+ompd_rc_t
+ompd_thread_handle_compare (ompd_thread_handle_t *thread_handle_1,
+			    ompd_thread_handle_t *thread_handle_2,
+			    int	*cmp_value )
+{
+
+  if (thread_handle_1 == NULL || thread_handle_2 == NULL)
+    return ompd_rc_stale_handle;
+  if (cmp_value == NULL)
+    return ompd_rc_bad_input;
+  if (thread_handle_1->ah->kind != thread_handle_2->ah->kind)
+    return ompd_rc_bad_input;
+
+  *cmp_value = thread_handle_1->th.address - thread_handle_2->th.address;
+  return ompd_rc_ok;
+}
+
+
+ompd_rc_t
+ompd_get_thread_id (ompd_thread_handle_t *thread_handle, ompd_thread_id_t kind,
+		    ompd_size_t sizeof_thread_id, void *thread_id)
+{
+  if (kind != OMPD_THREAD_ID_PTHREAD)
+    return ompd_rc_unsupported;
+  if (thread_id == NULL)
+    return ompd_rc_bad_input;
+  if (thread_handle == NULL)
+    return ompd_rc_stale_handle;
+
+  CHECK (thread_handle->ah);
+  ompd_address_space_context_t *context = thread_handle->ah->context;
+
+  ompd_rc_t ret;
+  ompd_address_t taddr = thread_handle->th;
+  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
+  ompd_size_t temp_symbol_size, symbol_size;
+  ompd_word_t temp_offset, offset;
+
+  GET_VALUE (context, NULL, "gompd_sizeof_gomp_thread_handle", symbol_size,
+	     temp_symbol_size, target_sizes.sizeof_short, 1, ret, symbol_addr);
+
+  if (symbol_size == 0)
+    goto use_tls_bias;
+
+  if (sizeof_thread_id != symbol_size)
+    return ompd_rc_bad_input;
+
+  GET_VALUE (context, NULL, "gompd_access_gomp_thread_handle", offset,
+	     temp_offset, target_sizes.sizeof_short, 1, ret, symbol_addr);
+  taddr.address += offset;
+
+  ret = callbacks->read_memory (context, NULL, &taddr, symbol_size, thread_id);
+  return ret;
+
+use_tls_bias:
+
+  GET_VALUE (context, NULL, "gompd_thread_initial_tls_bias", offset, temp_offset,
+	     target_sizes.sizeof_long, 1, ret, symbol_addr);
+
+  ret = callbacks->symbol_addr_lookup (context, NULL,"gomp_tls_data",
+				       &symbol_addr, NULL);
+  ret = callbacks->device_to_host (context, &temp_symbol_addr.address,
+				   target_sizes.sizeof_long_long, 1,
+				   &symbol_addr.address);
+  CHECK_RET (ret);
+
+  taddr.address = symbol_addr.address + offset;
+  ret = callbacks->read_memory (context, NULL, &taddr,
+				target_sizes.sizeof_long_long, thread_id);
+  return ret;
+}
+
+
+/* OMPD doesn't support GPUs for now.  */
+ompd_rc_t ompd_get_device_from_thread (ompd_thread_handle_t *thread_handle,
+				       ompd_address_space_handle_t **device)
+{
+  if (thread_handle == NULL)
+    return ompd_rc_stale_handle;
+  return ompd_rc_unsupported;
+}
diff --git a/libgomp/team.c b/libgomp/team.c
index d53246961b7..8e18fd6af63 100644
--- a/libgomp/team.c
+++ b/libgomp/team.c
@@ -77,6 +77,7 @@ gomp_thread_start (void *xdata)
   void *local_data;
 
   ompd_bp_thread_begin ();
+
 #if defined HAVE_TLS || defined USE_EMUTLS
   thr = &gomp_tls_data;
 #else
@@ -313,6 +314,9 @@ gomp_free_thread (void *arg __attribute__((unused)))
       gomp_end_task ();
       free (task);
     }
+
+  ompd_bp_thread_end ();
+
 }
 
 /* Launch a team.  */
diff --git a/libgomp/testsuite/Makefile.in b/libgomp/testsuite/Makefile.in
index 048844f0a40..76cd09b0faf 100644
--- a/libgomp/testsuite/Makefile.in
+++ b/libgomp/testsuite/Makefile.in
@@ -90,21 +90,8 @@ host_triplet = @host@
 target_triplet = @target@
 subdir = testsuite
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
-am__aclocal_m4_deps = $(top_srcdir)/../config/acx.m4 \
-	$(top_srcdir)/../config/ax_count_cpus.m4 \
-	$(top_srcdir)/../config/depstand.m4 \
-	$(top_srcdir)/../config/enable.m4 \
-	$(top_srcdir)/../config/futex.m4 \
-	$(top_srcdir)/../config/lead-dot.m4 \
-	$(top_srcdir)/../config/lthostflags.m4 \
-	$(top_srcdir)/../config/multi.m4 \
-	$(top_srcdir)/../config/override.m4 \
-	$(top_srcdir)/../config/tls.m4 \
-	$(top_srcdir)/../config/toolexeclibdir.m4 \
-	$(top_srcdir)/../ltoptions.m4 $(top_srcdir)/../ltsugar.m4 \
-	$(top_srcdir)/../ltversion.m4 $(top_srcdir)/../lt~obsolete.m4 \
-	$(top_srcdir)/acinclude.m4 $(top_srcdir)/../libtool.m4 \
-	$(top_srcdir)/../config/cet.m4 \
+am__aclocal_m4_deps = $(top_srcdir)/acinclude.m4 \
+	$(top_srcdir)/../libtool.m4 $(top_srcdir)/../config/cet.m4 \
 	$(top_srcdir)/plugin/configfrag.ac $(top_srcdir)/configure.ac
 am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
 	$(ACLOCAL_M4)
@@ -249,7 +236,6 @@ dvidir = @dvidir@
 enable_shared = @enable_shared@
 enable_static = @enable_static@
 exec_prefix = @exec_prefix@
-get_gcc_base_ver = @get_gcc_base_ver@
 host = @host@
 host_alias = @host_alias@
 host_cpu = @host_cpu@
@@ -265,10 +251,8 @@ libtool_VERSION = @libtool_VERSION@
 link_gomp = @link_gomp@
 localedir = @localedir@
 localstatedir = @localstatedir@
-lt_host_flags = @lt_host_flags@
 mandir = @mandir@
 mkdir_p = @mkdir_p@
-multi_basedir = @multi_basedir@
 offload_additional_lib_paths = @offload_additional_lib_paths@
 offload_additional_options = @offload_additional_options@
 offload_plugins = @offload_plugins@
@@ -278,6 +262,7 @@ pdfdir = @pdfdir@
 prefix = @prefix@
 program_transform_name = @program_transform_name@
 psdir = @psdir@
+runstatedir = @runstatedir@
 sbindir = @sbindir@
 sharedstatedir = @sharedstatedir@
 srcdir = @srcdir@

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [patch] libgompd: Add thread handles
  2022-06-06 22:21 Ahmed Sayed Mousse
@ 2022-06-07  9:56 ` Jakub Jelinek
  0 siblings, 0 replies; 9+ messages in thread
From: Jakub Jelinek @ 2022-06-07  9:56 UTC (permalink / raw)
  To: Ahmed Sayed Mousse; +Cc: gcc-patches

On Tue, Jun 07, 2022 at 12:21:25AM +0200, Ahmed Sayed Mousse via Gcc-patches wrote:
> This patch is the initial implementation of OpenMP-API specs book section
> 20.5.5 with title "Thread Handles"
> 
> libgomp/ChangeLog
> 
> 2022-05-06 Ahmed Sayed <ahmedsayedmousse@gmail.com>

Two spaces should separate the date and name and name and email.
> 
> * Makefile.am (libgompd_la_SOURCES): Add ompd-threads.c.
> 
> * Makefile.in: Regenerate.
> 

No empty lines in between (and all ChangeLog lines start with a tab (I
assume your mailer ate that).

> * ompd-support.h ( gompd_thread_initial_tls_bias ): New Variable.

No spaces after ( or before )

> * ompd-support.c ( gompd_thread_initial_tls_bias ): New Variable.
> 
> ( gompd_load ): ( gompd_thread_initial_tls_bias ): Initialized with
> &gomp_tls_data - pthread_self ().

It is just gompd_load you are changing, so it should be:
	(gompd_load): Initialize gompd_thread_initial_tls_bias.
or so.

> --- a/libgomp/ompd-support.c
> +++ b/libgomp/ompd-support.c
> @@ -36,6 +36,10 @@
>  const char **ompd_dll_locations = NULL;
>  __UINT64_TYPE__ gompd_state;
> 
> +#if (defined HAVE_TLS || defined USE_EMUTLS)
> +__UINT64_TYPE__ gompd_thread_initial_tls_bias;

In reality it isn't these conditions, but
#ifdef GOMP_NEEDS_THREAD_HANDLE that determines if there is
a TLS bias possible.
But the point of those gompd_sizeof* and gompd_access* vars
was to make libgompd slightly more independent from the exact
libgomp version, otherwise one could just use sizeof and offsetof
values directly in libgompd.
So, even using similar ifdefs on the libgompd side looks wrong,
the var should be there unconditionally and just use some special
value (e.g. -1 which isn't a possible TLS bias because the
struct has some alignment requirements) to say that the TLS bias
can't be used and one needs to use struct gomp_thread's handle
member instead.

Also, as I mentioned yesterday, using __UINT64_TYPE__ for everything
is very vasteful, use the right type for each information.
As for TLS bias, in reality it will be up to +- a few hundreds of bytes,
worst case kilobytes, but in theory it could be on 64-bit targets even
larger than 4GB, but on 32-bit arches it can't, so size_t would
be the right type.  Except I think the interfaces don't cover size_t size,
but long would be a usable replacement (not the same thing size-wise on
Windows, but Windows will always GOMP_NEEDS_THREAD_HANDLE).

> +#endif
> +
>  void
>  gompd_load (void)
>  {
> @@ -61,7 +65,11 @@ gompd_load (void)
>        = (__UINT64_TYPE__) & (((struct gomp_thread *) NULL)->handle);
>      __UINT64_TYPE__ gompd_sizeof_gomp_thread_handle
>        = sizeof (((struct gomp_thread *) NULL)->handle);

There is a preexisting bug above:
  #ifdef GOMP_NEEDS_THREAD_HANDLE
    __UINT64_TYPE__ gompd_access_gomp_thread_handle
      = (__UINT64_TYPE__) & (((struct gomp_thread *) NULL)->handle);
    __UINT64_TYPE__ gompd_sizeof_gomp_thread_handle
      = sizeof (((struct gomp_thread *) NULL)->handle);
just defines automatic variables in the function and sets them to
those values.  They need to be global vars, ideally const
initialized at file scope.  But, as the field is sometimes present
and sometimes it is not, I think best would be to initialize
it to offsetof/sizeof #ifdef GOMP_NEEDS_THREAD_HANDLE and
otherwise to 0 and 0.
Then we even don't need a magic value or when TLS bias can't be used
and instead always GET_VALUE of gompd_sizeof_gomp_thread_handle,
if it is 0, then use TLS bias, otherwise load the handle.

Again, comment more about the already committed patch now, besides
trying to shrink the values from __UINT64_TYPE__ to probably short int
and making them const and initialized at file scope initializers and
using offsetof, there is a big question when do we expect OMPD to work.
Seems the gompd_{sizeof,access}* symbols aren't exported from the
library, so they are present (say on ELF) just in .symtab/.strtab
sections and debug info.  Those sections can be stripped or stripped to
file, so that would mean OMPD would work only if the libgomp.so.1 library
is not stripped or has separate debug info installed.
Also, if one builds the library with LTO, I think the linker with the
compiler will happily remove all those symbols, as nothing uses them.
To fix this latter thing, one can just add __attribute__((used)) to
all those vars.
But if we want to make those work somehow even without debug info
and .symtab/.strtab sections around, I think we want to force the
symbols into .dynsym/.dynstr too (i.e. export in libgomp.map).
Exporting dozens of such symbols would be quite costly though.
So if we go that route, I think it would be best if we had just
1-2 of such variables with data for libgompd (probably 2 where
one is const and can be in .rodata and the other for vars that might need
changing).  As most if not all of the const data can be represented in
unsigned short, I think it should be an array of const unsigned short,
with macros that say what each element means and those macros we'd just keep
frozen as an unchangeable ABI between the libgomp and libgompd libraries.
Ideally, [0] element of the array would be some kind of version which
libgompd initialization can check and punt if the version is unexpected
(in theory in the future libgompd could support multiple versions etc.).

> +  #elif (defined HAVE_TLS || defined USE_EMUTLS)
> +    gompd_thread_initial_tls_bias = (__UINT64_TYPE__) \
> +    				     (&gomp_tls_data - pthread_self ());

I think you should add casts, (char *) &gomp_tls_data - (char *) pthread_self ()
But, sure, this difference is not constant and so needs to be in the other,
.data variable if the above notes are incorporated into a later patch,
for now just that the gompd_thread_initial_tls_bias var can't be const.

> +ompd_rc_t
> +ompd_get_thread_in_parallel (ompd_parallel_handle_t *parallel_handle, int
> +			     thread_num, ompd_thread_handle_t **thread_handle)
> +{
...
> +  ompd_word_t team_size_var;
> +  ret = ompd_get_icv_from_scope ((void *) parallel_handle, ompd_scope_parallel,
> +				 gompd_icv_team_size_var, &team_size_var);
> +  if (ret != ompd_rc_ok)
> +    return ret;
> +  if (thread_num < 0 || thread_num >= team_size_var)
> +    return ompd_rc_bad_input;

Does ompd_get_icv_from_scope with gompd_icv_team_size_var DTRT?
I.e. read the nthreads var from struct gomp_team if the parallel handle
represents a parallel with corresponding struct gomp_team, otherwise (if it
represents an implicit parallel, return 1)?  See how
omp_get_team_size (0) aka omp_get_num_threads () is implemented on the
library side.

	Jakub


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [patch] libgompd: Add thread handles
@ 2022-06-06 22:21 Ahmed Sayed Mousse
  2022-06-07  9:56 ` Jakub Jelinek
  0 siblings, 1 reply; 9+ messages in thread
From: Ahmed Sayed Mousse @ 2022-06-06 22:21 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 528 bytes --]

This patch is the initial implementation of OpenMP-API specs book section
20.5.5 with title "Thread Handles"

libgomp/ChangeLog

2022-05-06 Ahmed Sayed <ahmedsayedmousse@gmail.com>

* Makefile.am (libgompd_la_SOURCES): Add ompd-threads.c.

* Makefile.in: Regenerate.

* ompd-support.h ( gompd_thread_initial_tls_bias ): New Variable.

* ompd-support.c ( gompd_thread_initial_tls_bias ): New Variable.

( gompd_load ): ( gompd_thread_initial_tls_bias ): Initialized with
&gomp_tls_data - pthread_self ().

* ompd-threads.c: New.

[-- Attachment #2: diff.txt --]
[-- Type: text/plain, Size: 13937 bytes --]

diff --git a/libgomp/Makefile.am b/libgomp/Makefile.am
index 6d913a93e7f..23f5bede1bf 100644
--- a/libgomp/Makefile.am
+++ b/libgomp/Makefile.am
@@ -94,7 +94,7 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c error.c \
 	priority_queue.c affinity-fmt.c teams.c allocator.c oacc-profiling.c \
 	oacc-target.c ompd-support.c

-libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c
+libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c ompd-threads.c

 include $(top_srcdir)/plugin/Makefrag.am

diff --git a/libgomp/Makefile.in b/libgomp/Makefile.in
index 40f896b5f03..7acdcbf31d5 100644
--- a/libgomp/Makefile.in
+++ b/libgomp/Makefile.in
@@ -233,7 +233,8 @@ am_libgomp_la_OBJECTS = alloc.lo atomic.lo barrier.lo critical.lo \
 	affinity-fmt.lo teams.lo allocator.lo oacc-profiling.lo \
 	oacc-target.lo ompd-support.lo $(am__objects_1)
 libgomp_la_OBJECTS = $(am_libgomp_la_OBJECTS)
-am_libgompd_la_OBJECTS = ompd-init.lo ompd-helper.lo ompd-icv.lo
+am_libgompd_la_OBJECTS = ompd-init.lo ompd-helper.lo ompd-icv.lo \
+	ompd-threads.lo
 libgompd_la_OBJECTS = $(am_libgompd_la_OBJECTS)
 AM_V_P = $(am__v_P_@AM_V@)
 am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
@@ -583,7 +584,7 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c \
 	oacc-async.c oacc-plugin.c oacc-cuda.c priority_queue.c \
 	affinity-fmt.c teams.c allocator.c oacc-profiling.c \
 	oacc-target.c ompd-support.c $(am__append_7)
-libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c
+libgompd_la_SOURCES = ompd-init.c ompd-helper.c ompd-icv.c ompd-threads.c

 # Nvidia PTX OpenACC plugin.
 @PLUGIN_NVPTX_TRUE@libgomp_plugin_nvptx_version_info = -version-info $(libtool_VERSION)
@@ -801,6 +802,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-icv.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-init.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-support.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-threads.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ordered.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/parallel.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/priority_queue.Plo@am__quote@
diff --git a/libgomp/ompd-support.c b/libgomp/ompd-support.c
index d8a7174b2f3..c7da3ef98a6 100644
--- a/libgomp/ompd-support.c
+++ b/libgomp/ompd-support.c
@@ -36,6 +36,10 @@
 const char **ompd_dll_locations = NULL;
 __UINT64_TYPE__ gompd_state;

+#if (defined HAVE_TLS || defined USE_EMUTLS)
+__UINT64_TYPE__ gompd_thread_initial_tls_bias;
+#endif
+
 void
 gompd_load (void)
 {
@@ -61,7 +65,11 @@ gompd_load (void)
       = (__UINT64_TYPE__) & (((struct gomp_thread *) NULL)->handle);
     __UINT64_TYPE__ gompd_sizeof_gomp_thread_handle
       = sizeof (((struct gomp_thread *) NULL)->handle);
+  #elif (defined HAVE_TLS || defined USE_EMUTLS)
+    gompd_thread_initial_tls_bias = (__UINT64_TYPE__) \
+    				     (&gomp_tls_data - pthread_self ());
   #endif
+
   gomp_debug (2, "OMP OMPD active\n");
   static const char *ompd_dll_locations_array[2]
     = {"libgompd" SONAME_SUFFIX (1) , NULL};
diff --git a/libgomp/ompd-support.h b/libgomp/ompd-support.h
index 39d55161132..2dd88af2d73 100644
--- a/libgomp/ompd-support.h
+++ b/libgomp/ompd-support.h
@@ -69,6 +69,10 @@
 void gompd_load (void);
 extern __UINT64_TYPE__ gompd_state;

+#if (defined HAVE_TLS || defined USE_EMUTLS)
+extern __UINT64_TYPE__ gompd_thread_initial_tls_bias;
+#endif
+
 #define OMPD_ENABLED 0x1

 #define GOMPD_FOREACH_ACCESS(gompd_access) \
diff --git a/libgomp/ompd-support.h.gch b/libgomp/ompd-support.h.gch
new file mode 100644
index 00000000000..ee98f7091c0
Binary files /dev/null and b/libgomp/ompd-support.h.gch differ
diff --git a/libgomp/ompd-threads.c b/libgomp/ompd-threads.c
new file mode 100644
index 00000000000..bc370e1af9c
--- /dev/null
+++ b/libgomp/ompd-threads.c
@@ -0,0 +1,310 @@
+/* Copyright (C) The GNU Toolchain Authors.
+   Contributed by Ahmed Sayed <ahmedsayedmousse@gmail.com>.
+   This file is part of the GNU Offloading and Multi Processing Library
+   (libgomp).
+
+   Libgomp is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
+   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+   more details.
+
+   Under Section 7 of GPL version 3, you are granted additional
+   permissions described in the GCC Runtime Library Exception, version
+   3.1, as published by the Free Software Foundation.
+
+   You should have received a copy of the GNU General Public License and
+   a copy of the GCC Runtime Library Exception along with this program;
+   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/* This file contains the implementation of functions defined in
+   Section 5.5 ThreadHandles. */
+
+
+#include "ompd-helper.h"
+
+ompd_rc_t
+ompd_get_thread_in_parallel (ompd_parallel_handle_t *parallel_handle, int
+			     thread_num, ompd_thread_handle_t **thread_handle)
+{
+
+  if (parallel_handle == NULL)
+    return ompd_rc_stale_handle;
+  if (parallel_handle->ah == NULL)
+    return ompd_rc_stale_handle;
+
+  ompd_address_space_context_t *context = parallel_handle->ah->context;
+  ompd_rc_t ret;
+
+  if (!context)
+    return ompd_rc_stale_handle;
+
+  if (!callbacks)
+    return ompd_rc_callback_error;
+
+  ompd_word_t team_size_var;
+  ret = ompd_get_icv_from_scope ((void *) parallel_handle, ompd_scope_parallel,
+				 gompd_icv_team_size_var, &team_size_var);
+  if (ret != ompd_rc_ok)
+    return ret;
+  if (thread_num < 0 || thread_num >= team_size_var)
+    return ompd_rc_bad_input;
+
+  ompd_size_t temp_offset, field_offset;
+  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
+
+  const char *symbol_name = "gompd_access_gomp_thread_pool_threads";
+  ret = callbacks->symbol_addr_lookup (context, NULL, symbol_name, &symbol_addr,
+	  			       NULL);
+  ret = callbacks->read_memory (context, NULL, &symbol_addr,
+	  			target_sizes.sizeof_long_long, &temp_offset);
+
+  ret = callbacks->device_to_host (context, &temp_offset,
+				   target_sizes.sizeof_long_long, 1,
+				   &field_offset);
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  symbol_addr.address += field_offset;
+
+  ret = callbacks->read_memory (context, NULL, &symbol_addr,
+				target_sizes.sizeof_pointer,
+				&temp_symbol_addr.address);
+  ret = callbacks->device_to_host (context, &temp_symbol_addr.address,
+				   target_sizes.sizeof_pointer, 1,
+				   &symbol_addr.address);
+
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  symbol_addr.address += thread_num * target_sizes.sizeof_pointer;
+
+  ret = callbacks->read_memory (context, NULL, &symbol_addr,
+	  			target_sizes.sizeof_pointer,
+				&temp_symbol_addr.address);
+  ret = callbacks->device_to_host (context, &temp_symbol_addr.address,
+				   target_sizes.sizeof_pointer, 1,
+				   &symbol_addr.address);
+
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  ret = callbacks->alloc_memory (sizeof (ompd_thread_handle_t),
+  				 (void **) thread_handle);
+
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  if (symbol_addr.address == 0)
+    return ompd_rc_unsupported;
+
+  (*thread_handle)->th = symbol_addr;
+  (*thread_handle)->ah = parallel_handle->ah;
+  return ret;
+}
+
+/* The ompd_get_thread_handle function that maps a native thread to an
+   OMPD thread handle.  */
+
+ompd_rc_t
+ompd_get_thread_handle (ompd_address_space_handle_t *handle,
+			ompd_thread_id_t kind, ompd_size_t sizeof_thread_id,
+			const void *thread_id,
+			ompd_thread_handle_t **thread_handle)
+{
+  if (!handle)
+    return ompd_rc_stale_handle;
+
+  if (kind != OMPD_THREAD_ID_PTHREAD)
+    return ompd_rc_unsupported;
+
+  ompd_address_space_context_t *context = handle->context;
+  ompd_thread_context_t *tcontext;
+  ompd_rc_t ret;
+
+  if (!context)
+    return ompd_rc_stale_handle;
+
+  ret = callbacks->get_thread_context_for_thread_id (context, kind,
+	  					     sizeof_thread_id,
+						     thread_id, &tcontext);
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  ompd_size_t temp_symbol_size, symbol_size;
+  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
+
+  const char *symbol_name = "gompd_sizeof_gomp_thread";
+  ret = callbacks->symbol_addr_lookup(context, NULL, symbol_name, &symbol_addr,
+	  			      NULL);
+  ret = callbacks->read_memory(context, NULL, &symbol_addr,
+	  		       target_sizes.sizeof_long_long,
+			       &temp_symbol_size);
+  ret = callbacks->device_to_host(context, &temp_symbol_size,
+	  			  target_sizes.sizeof_long_long, 1,
+				  &symbol_size);
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  ret = callbacks->symbol_addr_lookup (context, tcontext,"gomp_tls_data",
+				       &symbol_addr, NULL);
+
+  /* if libgomp is offloading or using pthread_key_t then lookup can't
+     find the symbol "gomp_tls_data". */
+  if (ret == ompd_rc_error)
+    return ompd_rc_unsupported;
+
+  ret = callbacks->read_memory (context, tcontext, &symbol_addr, symbol_size,
+	  			&temp_symbol_addr.address);
+  ret = callbacks->device_to_host (context, &temp_symbol_addr.address,
+  				   symbol_size, 1, &symbol_addr.address);
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  ret = callbacks->alloc_memory (sizeof(ompd_thread_handle_t), (void **)
+  				 (thread_handle));
+
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  (*thread_handle)->ah = handle;
+  (*thread_handle)->th = symbol_addr;
+  (*thread_handle)->thread_context = tcontext;
+  return ret;
+}
+
+
+ompd_rc_t
+ompd_rel_thread_handle (ompd_thread_handle_t *thread_handle)
+{
+  if (thread_handle == NULL)
+    return ompd_rc_stale_handle;
+
+  ompd_rc_t ret;
+  ret = callbacks->free_memory ((void *) thread_handle);
+  if (ret != ompd_rc_ok)
+  return ret;
+
+  return ompd_rc_ok;
+}
+
+
+/* return -1, 0 or 1 for thread_handle_1 <, == or > thread_handle_2. */
+ompd_rc_t
+ompd_thread_handle_compare (ompd_thread_handle_t *thread_handle_1,
+			    ompd_thread_handle_t *thread_handle_2,
+			    int	*cmp_value )
+{
+
+  if (thread_handle_1 == NULL)
+    return ompd_rc_stale_handle;
+  if (thread_handle_2 == NULL)
+    return ompd_rc_stale_handle;
+  if (cmp_value == NULL)
+    return ompd_rc_bad_input;
+  if (thread_handle_1->ah->kind != thread_handle_2->ah->kind)
+    return ompd_rc_bad_input;
+  *cmp_value = thread_handle_1->th.address - thread_handle_2->th.address;
+
+  return ompd_rc_ok;
+}
+
+
+ompd_rc_t
+ompd_get_thread_id (ompd_thread_handle_t *thread_handle, ompd_thread_id_t kind,
+		    ompd_size_t sizeof_thread_id, void *thread_id)
+{
+  if (kind != OMPD_THREAD_ID_PTHREAD)
+    return ompd_rc_unsupported;
+  if (thread_id == NULL)
+    return ompd_rc_bad_input;
+  if (!thread_handle || !thread_handle->ah)
+    return ompd_rc_stale_handle;
+
+  ompd_address_space_context_t *context = thread_handle->ah->context;
+  if (context == NULL)
+    return ompd_rc_stale_handle;
+
+  ompd_rc_t ret;
+  ompd_address_t taddr = thread_handle->th;
+  ompd_address_t temp_symbol_addr, symbol_addr = {OMPD_SEGMENT_UNSPECIFIED, 0};
+  ompd_size_t temp_symbol_size, symbol_size;
+  ompd_size_t temp_offset, offset;
+
+
+  char *symbol_name = "gompd_sizeof_gomp_thread_handle";
+  ret = callbacks->symbol_addr_lookup (context, NULL, symbol_name, &symbol_addr,
+	  			       NULL);
+  /* If symbol isn't found we check for the optimized version. */
+  if (ret == ompd_rc_error)
+    goto try_bias;
+  ret = callbacks->read_memory (context, NULL, &symbol_addr,
+	  			target_sizes.sizeof_long_long,
+				&temp_symbol_size);
+  ret = callbacks->device_to_host (context, &temp_symbol_size,
+	  			   target_sizes.sizeof_long_long, 1,
+				   &symbol_size);
+  if (ret != ompd_rc_ok)
+    return ret;
+  if (sizeof_thread_id != symbol_size)
+    return ompd_rc_bad_input;
+
+  symbol_name = "gompd_access_gomp_thread_handle";
+  ret = callbacks->symbol_addr_lookup (context, NULL, symbol_name, &symbol_addr,
+	  			       NULL);
+  ret = callbacks->read_memory (context, NULL, &symbol_addr, symbol_size,
+	  			&temp_offset);
+  ret = callbacks->device_to_host (context, &temp_offset, symbol_size, 1,
+	  			   &offset);
+
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  taddr.address += offset;
+  ret = callbacks->read_memory (context, NULL, &taddr,
+				target_sizes.sizeof_long_long, thread_id);
+  return ret;
+
+try_bias:
+  symbol_name = "gompd_thread_initial_tls_bias";
+  ret = callbacks->symbol_addr_lookup (context, NULL, symbol_name, &symbol_addr,
+	  			       NULL);
+  if (ret == ompd_rc_error)
+    return ompd_rc_unsupported;
+
+  ret = callbacks->read_memory (context, NULL, &symbol_addr,
+				target_sizes.sizeof_long_long, &temp_offset);
+  ret = callbacks->device_to_host (context, &temp_offset,
+				   target_sizes.sizeof_long_long, 1, &offset);
+
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  ret = callbacks->symbol_addr_lookup (context, NULL,"gomp_tls_data",
+				       &symbol_addr, NULL);
+  ret = callbacks->device_to_host (context, &temp_symbol_addr.address,
+				   target_sizes.sizeof_long_long, 1,
+				   &symbol_addr.address);
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  taddr.address = symbol_addr.address + offset;
+  ret = callbacks->read_memory (context, NULL, &taddr,
+				target_sizes.sizeof_long_long, thread_id);
+  return ret;
+}
+
+
+/* OMPD doesn't support GPUs for now. */
+ompd_rc_t ompd_get_device_from_thread (ompd_thread_handle_t *thread_handle,
+				       ompd_address_space_handle_t **device)
+{
+  if (thread_handle == NULL)
+    return ompd_rc_stale_handle;
+  return ompd_rc_unsupported;
+}

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-09-27  6:42 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-27  1:12 [patch] libgompd: Add thread handles Ahmed Sayed Mousse
2022-09-27  1:20 ` Ahmed Sayed Mousse
2022-09-27  6:40   ` Bernhard Reutner-Fischer
  -- strict thread matches above, loose matches on Subject: below --
2022-07-04 20:34 Ahmed Sayed Mousse
2022-07-27 12:11 ` Mohamed Atef
2022-07-29 11:22 ` Jakub Jelinek
2022-07-01  0:00 Ahmed Sayed Mousse
2022-06-06 22:21 Ahmed Sayed Mousse
2022-06-07  9:56 ` Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).