From patchwork Tue Sep 23 18:18:35 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julian Brown X-Patchwork-Id: 392616 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 063F7140080 for ; Wed, 24 Sep 2014 04:18:57 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=YmqzTcTJUnw1+424Lmp/e/diOAn0uNzYPSln7yQtChtMm59NUUt0W KKFYNMLaShvCHfL9kfY8F9HtSvSmSf4yqB9T+0igJQOfDvDvKpCjy+fc4CqTntB+ KlYm63tDeWsi2a+8ufaz1DO4AyyYra0S2XR81Wh8uGSEDhXdTFfm5s= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=ccC/thyZySzbBKEG93fnc0CXOXA=; b=WB3gcfybEDFP1ITdj+RS DJJoWTp3liqNXZG5vV60ktXaJJVM3kntX+ISwgZJxvOv3xuX6LKiN1TESUrLqWMg pOzko4zoWiIh24A3U+ZvFVELHGILB68Rg6etKCUUTJAecqMs1jK1LFR4NAT/I91L v/t9RtwV1fg1kgiro4eRRNg= Received: (qmail 16276 invoked by alias); 23 Sep 2014 18:18:49 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 16263 invoked by uid 89); 23 Sep 2014 18:18:48 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00 autolearn=ham version=3.3.2 X-HELO: relay1.mentorg.com Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 23 Sep 2014 18:18:45 +0000 Received: from nat-ies.mentorg.com ([192.94.31.2] helo=SVR-IES-FEM-01.mgc.mentorg.com) by relay1.mentorg.com with esmtp id 1XWUfh-0002k0-Nv from Julian_Brown@mentor.com for gcc-patches@gcc.gnu.org; Tue, 23 Sep 2014 11:18:42 -0700 Received: from octopus (137.202.0.76) by SVR-IES-FEM-01.mgc.mentorg.com (137.202.0.104) with Microsoft SMTP Server id 14.3.181.6; Tue, 23 Sep 2014 19:18:40 +0100 Date: Tue, 23 Sep 2014 19:18:35 +0100 From: Julian Brown To: Subject: [PATCH 4/10] OpenACC 2.0 support for libgomp - host plugin Message-ID: <20140923191835.1783aa01@octopus> MIME-Version: 1.0 X-IsSubscribed: yes This patch was originally by Thomas Schwinge and was posted here: https://gcc.gnu.org/ml/gcc-patches/2014-02/msg01172.html It implements a plugin for host execution that can be used for testing non-shared-memory semantics on a "virtual" target device. It's merged with a minor follow-up patch, also by Thomas. Julian xxxx-xx-xx Thomas Schwinge James Norris * plugin-host.c: New file. * target.c (struct gomp_device_descr): Add device_alloc_func, device_free_func, device_dev2host_func, device_host2dev_func members. (gomp_load_plugin_for_device): Load these. (gomp_map_vars, gomp_unmap_tgt, gomp_unmap_vars, gomp_update): Use these. (resolve_device, gomp_find_available_plugins): Remove ID 257 hack. commit 1adb683c08079789d013713751a15803b26f11c2 Author: Julian Brown Date: Fri Sep 19 09:07:08 2014 -0700 Merge r207938. 2014-02-20 Thomas Schwinge James Norris * plugin-host.c: New file. * target.c (struct gomp_device_descr): Add device_alloc_func, device_free_func, device_dev2host_func, device_host2dev_func members. (gomp_load_plugin_for_device): Load these. (gomp_map_vars, gomp_unmap_tgt, gomp_unmap_vars, gomp_update): Use these. (resolve_device, gomp_find_available_plugins): Remove ID 257 hack. Merge r207940. 2014-02-20 Thomas Schwinge * target.c (gomp_load_plugin_for_device): Don't call dlcose if dlopen failed. diff --git a/libgomp/plugin-host.c b/libgomp/plugin-host.c new file mode 100644 index 0000000..5354ebe --- /dev/null +++ b/libgomp/plugin-host.c @@ -0,0 +1,84 @@ +/* Plugin for non-shared memory host execution. + + Copyright (C) 2014 Free Software Foundation, Inc. + + Contributed by Thomas Schwinge . + + This file is part of the GNU OpenMP 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 + . */ + +/* Simple implementation of a libgomp plugin for non-shared memory host + execution. */ + +#include +#include +#include +#include + +bool +device_available (void) +{ +#ifdef DEBUG + printf ("libgomp plugin: %s:%s\n", __FILE__, __FUNCTION__); +#endif + + return true; +} + +void * +device_alloc (size_t size) +{ + void *ptr = malloc (size); + +#ifdef DEBUG + printf ("libgomp plugin: %s:%s (%zd): %p\n", __FILE__, __FUNCTION__, size, ptr); +#endif + + return ptr; +} + +void +device_free (void *ptr) +{ +#ifdef DEBUG + printf ("libgomp plugin: %s:%s (%p)\n", __FILE__, __FUNCTION__, ptr); +#endif + + free (ptr); +} + +void *device_dev2host (void *dest, const void *src, size_t n) +{ +#ifdef DEBUG + printf ("libgomp plugin: %s:%s (%p, %p, %zd)\n", __FILE__, __FUNCTION__, dest, src, n); +#endif + + return memcpy (dest, src, n); +} + +void *device_host2dev (void *dest, const void *src, size_t n) +{ +#ifdef DEBUG + printf ("libgomp plugin: %s:%s (%p, %p, %zd)\n", __FILE__, __FUNCTION__, dest, src, n); +#endif + + return memcpy (dest, src, n); +} diff --git a/libgomp/target.c b/libgomp/target.c index f1e776b..d0db4c2 100644 --- a/libgomp/target.c +++ b/libgomp/target.c @@ -122,6 +122,10 @@ struct gomp_device_descr /* Function handlers. */ bool (*device_available_func) (void); + void *(*device_alloc_func) (size_t); + void (*device_free_func) (void *); + void *(*device_dev2host_func)(void *, const void *, size_t); + void *(*device_host2dev_func)(void *, const void *, size_t); /* Splay tree containing information about mapped memory regions. */ struct splay_tree_s dev_splay_tree; @@ -145,14 +149,10 @@ resolve_device (int device_id) struct gomp_task_icv *icv = gomp_icv (false); device_id = icv->default_device_var; } - if (device_id >= gomp_get_num_devices () - && device_id != 257) + if (device_id < 0 + || device_id >= gomp_get_num_devices ()) return NULL; - /* FIXME: Temporary hack for testing non-shared address spaces on host. */ - if (device_id == 257) - return &devices[0]; - return &devices[device_id]; } @@ -226,10 +226,10 @@ gomp_map_vars (struct gomp_device_descr *devicep, size_t mapnum, if (not_found_cnt || is_target) { - /* FIXME: This would be accelerator memory allocation, not - host, and should allocate tgt_align aligned tgt_size block - of memory. */ - tgt->to_free = gomp_malloc (tgt_size + tgt_align - 1); + /* Allocate tgt_align aligned tgt_size block of memory. */ + /* FIXME: Perhaps change interface to allocate properly aligned + memory. */ + tgt->to_free = devicep->device_alloc_func (tgt_size + tgt_align - 1); tgt->tgt_start = (uintptr_t) tgt->to_free; tgt->tgt_start = (tgt->tgt_start + tgt_align - 1) & ~(tgt_align - 1); tgt->tgt_end = tgt->tgt_start + tgt_size; @@ -282,13 +282,14 @@ gomp_map_vars (struct gomp_device_descr *devicep, size_t mapnum, break; case 1: /* TO */ case 3: /* TOFROM */ - /* FIXME: This is supposed to be copy from host to device - memory. Perhaps add some smarts, like if copying + /* Copy from host to device memory. */ + /* FIXME: Perhaps add some smarts, like if copying several adjacent fields from host to target, use some host buffer to avoid sending each var individually. */ - memcpy ((void *) (tgt->tgt_start + k->tgt_offset), - (void *) k->host_start, - k->host_end - k->host_start); + devicep->device_host2dev_func((void *) (tgt->tgt_start + + k->tgt_offset), + (void *) k->host_start, + k->host_end - k->host_start); break; case 4: /* POINTER */ cur_node.host_start @@ -322,10 +323,12 @@ gomp_map_vars (struct gomp_device_descr *devicep, size_t mapnum, array section. Now subtract bias to get what we want to initialize the pointer with. */ cur_node.tgt_offset -= sizes[i]; - /* FIXME: host to device copy, see above FIXME comment. */ - memcpy ((void *) (tgt->tgt_start + k->tgt_offset), - (void *) &cur_node.tgt_offset, - sizeof (void *)); + /* Copy from host to device memory. */ + /* FIXME: see above FIXME comment. */ + devicep->device_host2dev_func ((void *) (tgt->tgt_start + + k->tgt_offset), + (void *) &cur_node.tgt_offset, + sizeof (void *)); break; } array++; @@ -338,10 +341,12 @@ gomp_map_vars (struct gomp_device_descr *devicep, size_t mapnum, { cur_node.tgt_offset = tgt->list[i]->tgt->tgt_start + tgt->list[i]->tgt_offset; - /* FIXME: host to device copy, see above FIXME comment. */ - memcpy ((void *) (tgt->tgt_start + i * sizeof (void *)), - (void *) &cur_node.tgt_offset, - sizeof (void *)); + /* Copy from host to device memory. */ + /* FIXME: see above FIXME comment. */ + devicep->device_host2dev_func ((void *) (tgt->tgt_start + + i * sizeof (void *)), + (void *) &cur_node.tgt_offset, + sizeof (void *)); } } @@ -352,10 +357,9 @@ gomp_map_vars (struct gomp_device_descr *devicep, size_t mapnum, static void gomp_unmap_tgt (struct target_mem_desc *tgt) { - /* FIXME: Deallocate on target the tgt->tgt_start .. tgt->tgt_end - region. */ + /* Deallocate on target the tgt->tgt_start .. tgt->tgt_end region. */ if (tgt->tgt_end) - free (tgt->to_free); + tgt->device_descr->device_free_func(tgt->to_free); free (tgt->array); free (tgt); @@ -381,10 +385,11 @@ gomp_unmap_vars (struct target_mem_desc *tgt) { splay_tree_key k = tgt->list[i]; if (k->copy_from) - /* FIXME: device to host copy. */ - memcpy ((void *) k->host_start, - (void *) (k->tgt->tgt_start + k->tgt_offset), - k->host_end - k->host_start); + /* Copy from device to host memory. */ + devicep->device_dev2host_func ((void *) k->host_start, + (void *) (k->tgt->tgt_start + + k->tgt_offset), + k->host_end - k->host_start); splay_tree_remove (&devicep->dev_splay_tree, k); if (k->tgt->refcount > 1) k->tgt->refcount--; @@ -431,17 +436,23 @@ gomp_update (struct gomp_device_descr *devicep, size_t mapnum, (void *) n->host_start, (void *) n->host_end); if ((kinds[i] & 7) == 1) - /* FIXME: host to device copy. */ - memcpy ((void *) (n->tgt->tgt_start + n->tgt_offset - + cur_node.host_start - n->host_start), - (void *) cur_node.host_start, - cur_node.host_end - cur_node.host_start); + /* Copy from host to device memory. */ + devicep->device_host2dev_func ((void *) (n->tgt->tgt_start + + n->tgt_offset + + cur_node.host_start + - n->host_start), + (void *) cur_node.host_start, + cur_node.host_end + - cur_node.host_start); else if ((kinds[i] & 7) == 2) - /* FIXME: device to host copy. */ - memcpy ((void *) cur_node.host_start, - (void *) (n->tgt->tgt_start + n->tgt_offset - + cur_node.host_start - n->host_start), - cur_node.host_end - cur_node.host_start); + /* Copy from device to host memory. */ + devicep->device_dev2host_func ((void *) cur_node.host_start, + (void *) (n->tgt->tgt_start + + n->tgt_offset + + cur_node.host_start + - n->host_start), + cur_node.host_end + - cur_node.host_start); } else gomp_fatal ("Trying to update [%p..%p) object that is not mapped", @@ -584,28 +595,44 @@ static bool gomp_load_plugin_for_device (struct gomp_device_descr *device, const char *plugin_name) { - if (!device || !plugin_name) - return false; - - device->plugin_handle = dlopen (plugin_name, RTLD_LAZY); - if (!device->plugin_handle) - return false; + char *err = NULL; /* Clear any existing error. */ dlerror (); - /* Check if all required functions are available in the plugin and store - their handlers. - TODO: check for other routines as well. */ - device->device_available_func = dlsym (device->plugin_handle, - "device_available"); - if (dlerror () != NULL) + device->plugin_handle = dlopen (plugin_name, RTLD_LAZY); + if (!device->plugin_handle) { - dlclose (device->plugin_handle); - return false; + err = dlerror (); + goto out; } - return true; + /* Check if all required functions are available in the plugin and store + their handlers. */ +#define DLSYM(f) \ + do \ + { \ + device->f##_func = dlsym (device->plugin_handle, #f); \ + err = dlerror (); \ + if (err != NULL) \ + goto out; \ + } \ + while (0) + DLSYM (device_available); + DLSYM (device_alloc); + DLSYM (device_free); + DLSYM (device_dev2host); + DLSYM (device_host2dev); +#undef DLSYM + + out: + if (err != NULL) + { + gomp_error ("while loading %s: %s", plugin_name, err); + if (device->plugin_handle) + dlclose (device->plugin_handle); + } + return err == NULL; } /* This functions scans folder, specified in environment variable @@ -650,7 +677,6 @@ gomp_find_available_plugins (void) if (devices == NULL) { num_devices = 0; - closedir (dir); goto out; } @@ -660,26 +686,10 @@ gomp_find_available_plugins (void) gomp_mutex_init (&devices[num_devices].dev_env_lock); num_devices++; } - closedir (dir); out: - /* FIXME: Temporary hack for testing non-shared address spaces on host. - We create device 257 just to check memory mapping. */ - if (num_devices == 0) - { - num_devices = 1; - devices = malloc (sizeof (struct gomp_device_descr)); - if (devices == NULL) - { - num_devices = 0; - return; - } - devices[0].plugin_handle = NULL; - devices[0].device_available_func = NULL; - devices[0].dev_splay_tree.root = NULL; - gomp_mutex_init (&devices[0].dev_env_lock); - } - devices[0].id = 257; + if (dir) + closedir (dir); } /* This function initializes runtime needed for offloading.