Hello,

In addition to the per-library process, I am trying to implement OMPD's
per-process functions defined in 5.5.2.
I have some questions on defining or at least using some of the data types.

In the attached patch, I took a liberty of tentatively defining the
_ompd_aspace_handle, which is the ompd_address_space_handle_t. This was not
explicitly defined by the standard. I took notes from the LLVM project to
come up with one, although I chose to retain some of the member variable
data types. Are they sufficient? I am not sure about the id variable. It
seems to be mainly added in the ompd_device_initialize function, but the
argument passed in is void *id. It is accompanied by sizeof_id that denotes
the size. I chose to keep the void pointer and stored the sizeof_id in the
struct, but is there a better way?

Another one that is used in the 5.5.2 section that isn't explicitly defined
is the _ompd_aspace_cont, which is the ompd_address_space_context_t. It
seems I need to read data from this, but I do not know how it should be
defined. Any suggestions?

As always, please feel free to let me know of any mistakes or other
suggestions regarding the patch.

Cheers,

Tony Sim
From 72ec5149a1296ef9f773529b486b9100ef0b8891 Mon Sep 17 00:00:00 2001
From: y2s1982 <y2s1...@gmail.com>
Date: Wed, 1 Jul 2020 22:47:25 -0400
Subject: [PATCH] libgomp: Add OMPD library and process functions

This patch adds OMPD library initialize and finalize functions, and OMPD
process initialize and resource release functions. It also imports
ompd-types.h that defines additional macros and data types.

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

libgomp/ChangeLog:

	* Makefile.am (libgompd_la_SOURCES): Add ompd-proc.c.
	* Makefile.in: Regenerate.
	* libgompd.h: Include omp-tools.h.
	(gompd_callbacks): Add new extern declaration.
	* ompd-lib.c (ompd_initialize): Simplified copy logic.
	(ompd_initialized): Moved to global scope.
	(ompd_finalize): Add new function that resets flag.
	* ompd-proc.c: New file.
	* ompd-types.h: New file.

---
 libgomp/Makefile.am  |  2 +-
 libgomp/Makefile.in  |  9 ++--
 libgomp/libgompd.h   |  4 ++
 libgomp/ompd-lib.c   | 16 +++++++-
 libgomp/ompd-proc.c  | 95 +++++++++++++++++++++++++++++++++++++++++++
 libgomp/ompd-types.h | 97 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 216 insertions(+), 7 deletions(-)
 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..e5556ef61e5 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
 
 include $(top_srcdir)/plugin/Makefrag.am
 
diff --git a/libgomp/Makefile.in b/libgomp/Makefile.in
index af897d6c6ba..f0f91761a0d 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
 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
 
 # Nvidia PTX OpenACC plugin.
 @PLUGIN_NVPTX_TRUE@libgomp_plugin_nvptx_version_info = -version-info $(libtool_VERSION)
@@ -817,6 +817,7 @@ distclean-compile:
 @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-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 9782828bff5..3a428e1c1e4 100644
--- a/libgomp/libgompd.h
+++ b/libgomp/libgompd.h
@@ -29,9 +29,13 @@
 #ifndef LIBGOMPD_H
 #define LIBGOMPD_H 1
 
+#include "omp-tools.h"
+
 #define ompd_stringify(x) ompd_str2(x)
 #define ompd_str2(x) #x
 
 #define OMPD_VERSION 201811
 
+extern ompd_callbacks_t gompd_callbacks;
+
 #endif /* LIBGOMPD_H */
diff --git a/libgomp/ompd-lib.c b/libgomp/ompd-lib.c
index f0ae9e85a7e..d5350e1045c 100644
--- a/libgomp/ompd-lib.c
+++ b/libgomp/ompd-lib.c
@@ -29,6 +29,9 @@
 #include "omp-tools.h"
 #include "libgompd.h"
 
+ompd_callbacks_t gompd_callbacks;
+static int ompd_initialized = 0;
+
 ompd_rc_t
 ompd_get_api_version (ompd_word_t *version)
 {
@@ -47,15 +50,24 @@ ompd_get_version_string (const char **string)
 ompd_rc_t
 ompd_initialize (ompd_word_t api_version, const ompd_callbacks_t *callbacks)
 {
-  static int ompd_initialized = 0;
+  if (!callbacks)
+    return ompd_rc_bad_input;
 
   if (ompd_initialized)
     return ompd_rc_error;
 
+  gompd_callbacks = *callbacks;
+
   (void) api_version;
-  (void) callbacks;
 
   ompd_initialized = 1;
 
   return ompd_rc_ok;
 }
+
+ompd_rc_t
+ompd_finalize (void)
+{
+  ompd_initialized = 0;
+  return ompd_rc_ok;
+}
diff --git a/libgomp/ompd-proc.c b/libgomp/ompd-proc.c
new file mode 100644
index 00000000000..6a9c827e327
--- /dev/null
+++ b/libgomp/ompd-proc.c
@@ -0,0 +1,95 @@
+/* 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"
+
+ompd_rc_t
+ompd_process_initialize (ompd_address_space_context_t *context,
+			ompd_address_space_handle_t **handle)
+{
+  ompd_rc_t ret = (context) ? ompd_rc_ok : ompd_rc_bad_input;
+  if (ret == ompd_rc_ok)
+  {
+    ret = gompd_callbacks.alloc_memory (sizeof (ompd_address_space_handle_t),
+				  (void **)(handle));
+  }
+
+  if (ret == ompd_rc_ok)
+  {
+    (*handle)->context = context;
+    (*handle)->id = NULL;
+    (*handle)->kind = OMPD_DEVICE_KIND_HOST;
+  }
+
+  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_context && id) ? ompd_rc_ok :
+							    ompd_rc_bad_input;
+  if (ret == ompd_rc_ok)
+  {
+    ret = gompd_callbacks.alloc_memory (sizeof (ompd_address_space_handle_t),
+					(void **)device_handle);
+  }
+  if (ret == ompd_rc_ok)
+  {
+    ret = gompd_callbacks.alloc_memory (sizeof_id,
+					(void *)(*device_handle)->id);
+  }
+  if (ret == ompd_rc_ok)
+  {
+    (*device_handle)->context = device_context;
+    (*device_handle)->sizeof_id = sizeof_id;
+    (*device_handle)->kind = kind;
+    ret = gompd_callbacks.write_memory (device_context, NULL, id, sizeof_id,
+					(*device_handle)->id);
+  }
+  return ret;
+}
+
+ompd_rc_t
+ompd_rel_address_space_handle (ompd_address_space_handle_t *handle)
+{
+  ompd_rc_t ret = (handle) ? ompd_rc_ok : ompd_rc_bad_input;
+  if (ret == ompd_rc_ok && (*handle).context)
+    ret = gompd_callbacks.free_memory ((*handle).context);
+  if (ret == ompd_rc_ok && (*handle).id)
+    ret = gompd_callbacks.free_memory ((*handle).id);
+  if (ret == ompd_rc_ok)
+    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..416889881f2
--- /dev/null
+++ b/libgomp/ompd-types.h
@@ -0,0 +1,97 @@
+/* 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)
+
+typedef struct _ompd_aspace_handle {
+  ompd_address_space_context_t *context;
+  ompd_device_t kind;
+  ompd_size_t sizeof_id;
+  void *id;
+} ompd_address_space_handle_t;
+
+#endif /* OMPD_TYPES_H */
-- 
2.27.0

Reply via email to