Hello, I believe I misused the write_memory() function. This function is currently used in ompd_device_initialize() to write a block of memory that represents the id of the device, stored in the handle as a pointer. I would need to resubmit the patch but I have some questions regarding the issue.
Re-reading the documentation, it seems the callback function is used to write to the OpenMP program described in the context, which may be a different device than the host, like a SMX, which would have its own dedicated memory. They way it is currently written, I fear it might just corrupt some random memory block somewhere in target device instead of writing to the handle if the device_context somewhere other than the host. I am not sure how best to store that information. It is passed in as a void *id with the accompanying ompd_size_t sizeof_id variable. Would it be safe to assume all variable information will be safely stored in the unsigned long long, do some casting based on the sizeof_id, and store it in unsigned long long member variable? Cheers, Tony Sim On Fri, Jul 3, 2020 at 1:49 PM y2s1982 <y2s1...@gmail.com> wrote: > This patch adds OMPD process and device handling functions and related > data structures as described in the OMP 5.0 API documentation, section > 5.5.2. > > 2020-07-03 Tony Sim <y2s1...@gmail.com> > > libgomp/ChangeLog: > > * Makefile.am (libgompd_la_SOURCES): Add ompd-proc.c > * Makefile.in: Regenerate. > * libgompd.h (ompd_address_space_handle_t): Add definition. > * ompd-proc.c: New file. > * ompd-types.h: New file. > > --- > libgomp/Makefile.am | 2 +- > libgomp/Makefile.in | 9 +-- > libgomp/libgompd.h | 9 +++ > libgomp/ompd-proc.c | 132 +++++++++++++++++++++++++++++++++++++++++++ > libgomp/ompd-types.h | 90 +++++++++++++++++++++++++++++ > 5 files changed, 237 insertions(+), 5 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 3a428e1c1e4..495995e00d3 100644 > --- a/libgomp/libgompd.h > +++ b/libgomp/libgompd.h > @@ -38,4 +38,13 @@ > > extern ompd_callbacks_t gompd_callbacks; > > +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 *process_reference; > + ompd_size_t ref_count; > +} ompd_address_space_handle_t; > + > #endif /* LIBGOMPD_H */ > diff --git a/libgomp/ompd-proc.c b/libgomp/ompd-proc.c > new file mode 100644 > index 00000000000..39feba056f2 > --- /dev/null > +++ b/libgomp/ompd-proc.c > @@ -0,0 +1,132 @@ > +/* 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 = 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 = NULL; > + (*handle)->sizeof_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; > + p = NULL; > + > + ret = gompd_callbacks.alloc_memory (sizeof_id, p); > + if (ret != ompd_rc_ok) > + return ret; > + > + (*device_handle)->id = p; > + > + ret = gompd_callbacks.write_memory (device_context, NULL, id, sizeof_id, > + (*device_handle)->id); > + if (ret != ompd_rc_ok) > + return ret; > + (*device_handle)->sizeof_id = sizeof_id; > + (*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--; > + } > + > + if (handle->id) > + { > + ret = gompd_callbacks.free_memory (handle->id); > + if (ret != ompd_rc_ok) > + return ret; > + } > + > + 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 > >