This patch adds OMPD functions defined in 5.5.2 of the OpenMP 5.0 API
documentation. It adds per-process and per-device functions, defines
related handle data types, and adds a helper function for storing device
id.

2020-07-03  Tony Sim  <y2s1...@gmail.com>

libgomp/ChangeLog:

        * Makefile.am (libgompd_la_OBJECTS): Add ompd-proc.c and
                                             ompd-helper.c
        * Makefile.in: Regenerate.
        * libgompd.h (ompd_address_space_handle_t): Define.
        (_gompd_device_id): Define.
        * ompd-helper.c: New file.
        * ompd-helper.h: New file.
        * ompd-proc.c: New file.
        * ompd-types.h: New file.

---
 libgomp/Makefile.am   |   2 +-
 libgomp/Makefile.in   |  10 ++--
 libgomp/libgompd.h    |  10 ++++
 libgomp/ompd-helper.c |  58 +++++++++++++++++++++
 libgomp/ompd-helper.h |  37 +++++++++++++
 libgomp/ompd-proc.c   | 117 ++++++++++++++++++++++++++++++++++++++++++
 libgomp/ompd-types.h  |  90 ++++++++++++++++++++++++++++++++
 7 files changed, 319 insertions(+), 5 deletions(-)
 create mode 100644 libgomp/ompd-helper.c
 create mode 100644 libgomp/ompd-helper.h
 create mode 100644 libgomp/ompd-proc.c
 create mode 100644 libgomp/ompd-types.h

diff --git a/libgomp/Makefile.am b/libgomp/Makefile.am
index e15a838e55c..4306951046f 100644
--- a/libgomp/Makefile.am
+++ b/libgomp/Makefile.am
@@ -90,7 +90,7 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c 
env.c error.c \
        oacc-mem.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
 
-libgompd_la_SOURCES = ompd-lib.c
+libgompd_la_SOURCES = ompd-lib.c ompd-proc.c ompd-helper.c
 
 include $(top_srcdir)/plugin/Makefrag.am
 
diff --git a/libgomp/Makefile.in b/libgomp/Makefile.in
index af897d6c6ba..dc0f3b58ebe 100644
--- a/libgomp/Makefile.in
+++ b/libgomp/Makefile.in
@@ -235,7 +235,7 @@ am_libgomp_la_OBJECTS = alloc.lo atomic.lo barrier.lo 
critical.lo \
        $(am__objects_1)
 libgomp_la_OBJECTS = $(am_libgomp_la_OBJECTS)
 libgompd_la_LIBADD =
-am_libgompd_la_OBJECTS = ompd-lib.lo
+am_libgompd_la_OBJECTS = ompd-lib.lo ompd-proc.lo ompd-helper.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@)
@@ -574,10 +574,10 @@ nodist_toolexeclib_HEADERS = libgomp.spec
 libgomp_version_info = -version-info $(libtool_VERSION)
 libgompd_version_info = -version-info $(libtool_VERSION)
 libgomp_la_LDFLAGS = $(libgomp_version_info) $(libgomp_version_script) \
-        $(lt_host_flags)
+       $(lt_host_flags)
 
 libgompd_la_LDFLAGS = $(libgompd_version_info) $(libgompd_version_script) \
-        $(lt_host_flags)
+       $(lt_host_flags)
 
 libgomp_la_DEPENDENCIES = $(libgomp_version_dep)
 libgompd_la_DEPENDENCIES = $(libgompd_version_dep)
@@ -592,7 +592,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 $(am__append_4)
-libgompd_la_SOURCES = ompd-lib.c
+libgompd_la_SOURCES = ompd-lib.c ompd-proc.c ompd-helper.c
 
 # Nvidia PTX OpenACC plugin.
 @PLUGIN_NVPTX_TRUE@libgomp_plugin_nvptx_version_info = -version-info 
$(libtool_VERSION)
@@ -816,7 +816,9 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/oacc-plugin.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/oacc-profiling.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/oacc-target.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-helper.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-lib.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ompd-proc.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/libgompd.h b/libgomp/libgompd.h
index 3a428e1c1e4..f1338e7b3e4 100644
--- a/libgomp/libgompd.h
+++ b/libgomp/libgompd.h
@@ -38,4 +38,14 @@
 
 extern ompd_callbacks_t gompd_callbacks;
 
+typedef __UINT64_TYPE__ _gompd_device_id;
+
+typedef struct _ompd_aspace_handle {
+  ompd_address_space_context_t *context;
+  ompd_device_t kind;
+  _gompd_device_id id;
+  ompd_address_space_handle_t *process_reference;
+  ompd_size_t ref_count;
+} ompd_address_space_handle_t;
+
 #endif /* LIBGOMPD_H */
diff --git a/libgomp/ompd-helper.c b/libgomp/ompd-helper.c
new file mode 100644
index 00000000000..949b9cf771e
--- /dev/null
+++ b/libgomp/ompd-helper.c
@@ -0,0 +1,58 @@
+/* Copyright (C) 2020 Free Software Foundation, Inc.
+   Contributed by Yoosuk Sim <y2s1...@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 definitions for OMPD helper functions */
+
+#include "ompd-helper.h"
+
+ompd_rc_t
+gompd_get_gompd_device_id (void *input_id, ompd_size_t id_size,
+                          _gompd_device_id *output_id)
+{
+  if (!input_id || !output_id || id_size == 0)
+    return ompd_rc_bad_input;
+  if (sizeof (_gompd_device_id) < id_size)
+    return ompd_rc_incompatible;
+
+  switch (id_size)
+    {
+    case 1:
+      *output_id = (_gompd_device_id) *((__UINT8_TYPE__ *) input_id);
+      break;
+    case 2:
+      *output_id = (_gompd_device_id) *((__UINT16_TYPE__ *) input_id);
+      break;
+    case 4:
+      *output_id = (_gompd_device_id) *((__UINT32_TYPE__ *) input_id);
+      break;
+    case 8:
+      *output_id = (_gompd_device_id) *((__UINT64_TYPE__ *) input_id);
+      break;
+    default:
+      return ompd_rc_incompatible;
+    }
+
+  return ompd_rc_ok;
+}
diff --git a/libgomp/ompd-helper.h b/libgomp/ompd-helper.h
new file mode 100644
index 00000000000..60a54376a53
--- /dev/null
+++ b/libgomp/ompd-helper.h
@@ -0,0 +1,37 @@
+/* Copyright (C) 2020 Free Software Foundation, Inc.
+   Contributed by Yoosuk Sim <y2s1...@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 header declares helper functions of OMPD library.  */
+
+#ifndef OMPD_HELPER_H
+#define OMPD_HELPER_H 1
+
+#include "omp-tools.h"
+#include "libgompd.h"
+
+ompd_rc_t gompd_get_gompd_device_id (void *, ompd_size_t, _gompd_device_id *)
+                                               __GOMPD_NOTHROW;
+
+#endif /* OMPD_HELPER_H */
diff --git a/libgomp/ompd-proc.c b/libgomp/ompd-proc.c
new file mode 100644
index 00000000000..c9a37a7a8b6
--- /dev/null
+++ b/libgomp/ompd-proc.c
@@ -0,0 +1,117 @@
+/* Copyright (C) 2020 Free Software Foundation, Inc.
+   Contributed by Yoosuk Sim <y2s1...@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 function definitions for OMPD's per-process functions
+   defined in the OpenMP 5.0 API Documentation, 5.5.2.  */
+
+#include <stdlib.h>
+#include "omp-tools.h"
+#include "libgompd.h"
+#include "ompd-types.h"
+#include "ompd-helper.h"
+
+ompd_rc_t
+ompd_process_initialize (ompd_address_space_context_t *context,
+                        ompd_address_space_handle_t **handle)
+{
+  ompd_rc_t ret = handle ? ompd_rc_ok : ompd_rc_stale_handle;
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  ret = context ? ompd_rc_ok : ompd_rc_bad_input;
+
+  void *p = NULL;
+  ret = gompd_callbacks.alloc_memory (sizeof (ompd_address_space_handle_t), p);
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  *handle = p;
+  (*handle)->context = context;
+  (*handle)->kind = OMPD_DEVICE_KIND_HOST;
+  (*handle)->id = 0;
+  (*handle)->process_reference = NULL;
+  (*handle)->ref_count = 0;
+
+  return ret;
+}
+
+ompd_rc_t
+ompd_device_initialize (ompd_address_space_handle_t *process_handle,
+                       ompd_address_space_context_t *device_context,
+                       ompd_device_t kind, ompd_size_t sizeof_id, void *id,
+                       ompd_address_space_handle_t **device_handle)
+{
+  ompd_rc_t ret = process_handle && device_handle
+                 ? ompd_rc_ok : ompd_rc_stale_handle;
+
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  ret = device_context && id ? ompd_rc_ok : ompd_rc_bad_input;
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  void *p = NULL;
+  ret = gompd_callbacks.alloc_memory (sizeof (ompd_address_space_handle_t), p);
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  *device_handle = p;
+
+  ret = gompd_get_gompd_device_id (id, sizeof_id, &(*device_handle)->id);
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  (*device_handle)->context = device_context;
+  (*device_handle)->kind = kind;
+  (*device_handle)->ref_count = 0;
+  (*device_handle)->process_reference = process_handle;
+  process_handle->ref_count++;
+
+  return ret;
+}
+
+ompd_rc_t
+ompd_rel_address_space_handle (ompd_address_space_handle_t *handle)
+{
+  ompd_rc_t ret = handle && handle->context
+                 ? ompd_rc_ok : ompd_rc_stale_handle;
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  ret = handle->ref_count == 0 ? ompd_rc_ok : ompd_rc_unavailable;
+  if (ret != ompd_rc_ok)
+    return ret;
+
+  if (handle->process_reference)
+    {
+      if (handle->process_reference->ref_count == 0)
+       return ompd_rc_error;
+      handle->process_reference->ref_count--;
+    }
+
+  ret = gompd_callbacks.free_memory (handle);
+  return ret;
+}
diff --git a/libgomp/ompd-types.h b/libgomp/ompd-types.h
new file mode 100644
index 00000000000..4a9aec18ebc
--- /dev/null
+++ b/libgomp/ompd-types.h
@@ -0,0 +1,90 @@
+/* Copyright (C) 2020 Free Software Foundation, Inc.
+   Contributed by Yoosuk Sim <y2s1...@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 various library-defined data types for OMPD library.  */
+
+#ifndef OMPD_TYPES_H
+#define OMPD_TYPES_H 1
+
+#include "omp-tools.h"
+
+#define OMPD_TYPES_VERSION   20180906 /* YYYYMMDD Format */
+
+/* Kinds of device threads  */
+#define OMPD_THREAD_ID_PTHREAD      ((ompd_thread_id_t) 0)
+#define OMPD_THREAD_ID_LWP         ((ompd_thread_id_t) 1)
+#define OMPD_THREAD_ID_WINTHREAD    ((ompd_thread_id_t) 2)
+#define OMPD_THREAD_ID_CUDALOGICAL  ((ompd_thread_id_t) 3)
+/* The range of non-standard implementation defined values */
+#define OMPD_THREAD_ID_LO       ((ompd_thread_id_t) 1000000)
+#define OMPD_THREAD_ID_HI       ((ompd_thread_id_t) 1100000)
+
+/* Target Cuda device-specific thread identification */
+typedef struct ompd_dim3_t {
+  ompd_addr_t x;
+  ompd_addr_t y;
+  ompd_addr_t z;
+} ompd_dim3_t;
+
+typedef struct ompd_cudathread_coord_t {
+  ompd_addr_t cudaDevId;
+  ompd_addr_t cudaContext;
+  ompd_addr_t warpSize;
+  ompd_addr_t gridId;
+  ompd_dim3_t gridDim;
+  ompd_dim3_t blockDim;
+  ompd_dim3_t blockIdx;
+  ompd_dim3_t threadIdx;
+} ompd_cudathread_coord_t;
+
+/* Memory Access Segment definitions for Host and Target Devices */
+#define OMPD_SEGMENT_UNSPECIFIED            ((ompd_seg_t) 0)
+
+/* Cuda-specific values consistent with those defined in cudadebugger.h */
+#define OMPD_SEGMENT_CUDA_PTX_UNSPECIFIED    ((ompd_seg_t) 0)
+#define OMPD_SEGMENT_CUDA_PTX_CODE          ((ompd_seg_t) 1)
+#define OMPD_SEGMENT_CUDA_PTX_REG           ((ompd_seg_t) 2)
+#define OMPD_SEGMENT_CUDA_PTX_SREG          ((ompd_seg_t) 3)
+#define OMPD_SEGMENT_CUDA_PTX_CONST         ((ompd_seg_t) 4)
+#define OMPD_SEGMENT_CUDA_PTX_GLOBAL        ((ompd_seg_t) 5)
+#define OMPD_SEGMENT_CUDA_PTX_LOCAL         ((ompd_seg_t) 6)
+#define OMPD_SEGMENT_CUDA_PTX_PARAM         ((ompd_seg_t) 7)
+#define OMPD_SEGMENT_CUDA_PTX_SHARED        ((ompd_seg_t) 8)
+#define OMPD_SEGMENT_CUDA_PTX_SURF          ((ompd_seg_t) 9)
+#define OMPD_SEGMENT_CUDA_PTX_TEX           ((ompd_seg_t) 10)
+#define OMPD_SEGMENT_CUDA_PTX_TEXSAMPLER     ((ompd_seg_t) 11)
+#define OMPD_SEGMENT_CUDA_PTX_GENERIC       ((ompd_seg_t) 12)
+#define OMPD_SEGMENT_CUDA_PTX_IPARAM        ((ompd_seg_t) 13)
+#define OMPD_SEGMENT_CUDA_PTX_OPARAM        ((ompd_seg_t) 14)
+#define OMPD_SEGMENT_CUDA_PTX_FRAME         ((ompd_seg_t) 15)
+
+/* Kinds of device device address spaces */
+#define OMPD_DEVICE_KIND_HOST     ((ompd_device_t) 1)
+#define OMPD_DEVICE_KIND_CUDA     ((ompd_device_t) 2)
+/* The range of non-standard implementation defined values */
+#define OMPD_DEVICE_IMPL_LO       ((ompd_device_t) 1000000)
+#define OMPD_DEVICE_IMPL_HI       ((ompd_device_t) 1100000)
+
+#endif /* OMPD_TYPES_H */
-- 
2.27.0

Reply via email to