From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 120484 invoked by alias); 20 Apr 2015 14:16:19 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 120471 invoked by uid 89); 20 Apr 2015 14:16:17 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-yk0-f170.google.com Received: from mail-yk0-f170.google.com (HELO mail-yk0-f170.google.com) (209.85.160.170) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Mon, 20 Apr 2015 14:16:16 +0000 Received: by ykft189 with SMTP id t189so18045731ykf.1 for ; Mon, 20 Apr 2015 07:16:14 -0700 (PDT) X-Received: by 10.170.81.87 with SMTP id x84mr14908298ykx.113.1429539374601; Mon, 20 Apr 2015 07:16:14 -0700 (PDT) Received: from msticlxl57.ims.intel.com ([192.55.55.41]) by mx.google.com with ESMTPSA id e94sm15531318yhq.44.2015.04.20.07.16.11 (version=TLSv1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 20 Apr 2015 07:16:13 -0700 (PDT) Date: Mon, 20 Apr 2015 14:16:00 -0000 From: Ilya Verbin To: Jakub Jelinek , Thomas Schwinge Cc: gcc-patches@gcc.gnu.org, Kirill Yukhin , Julian Brown Subject: [PATCH][OpenMP] Fix resolve_device with -foffload=disable Message-ID: <20150420141603.GA46200@msticlxl57.ims.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-IsSubscribed: yes X-SW-Source: 2015-04/txt/msg01033.txt.bz2 Hi! Currently if a compiler is configured with enabled offloading, the 'devices' array in libgomp is filled properly with a number of available devices. However, if a program is compiled with -foffload=disable, the resolve_device function returns a pointer to the device, and host-fallback is not happening. The patch below fixes this issue. make check-target-libgomp passed. OK for trunk? libgomp/ * libgomp.h (struct gomp_device_descr): Add num_images. * target.c (resolve_device): Call gomp_init_device. Return NULL if there is no image loaded to the device. (gomp_offload_image_to_device): Increase num_images. (GOMP_offload_unregister): Decrease num_images. (GOMP_target): Don't call gomp_init_device. (GOMP_target_data): Ditto. (GOMP_target_update): Ditto. (gomp_target_init): Set num_images to 0. * testsuite/libgomp.c/target-1-disable.c: New test. diff --git a/libgomp/libgomp.h b/libgomp/libgomp.h index 5272f01..47a064a 100644 --- a/libgomp/libgomp.h +++ b/libgomp/libgomp.h @@ -762,6 +762,9 @@ struct gomp_device_descr /* Set to true when device is initialized. */ bool is_initialized; + /* Number of images offloaded to the device. */ + int num_images; + /* OpenACC-specific data and functions. */ /* This is mutable because of its mutable data_environ and target_data members. */ diff --git a/libgomp/target.c b/libgomp/target.c index d8da783..f5126b9 100644 --- a/libgomp/target.c +++ b/libgomp/target.c @@ -132,6 +132,14 @@ resolve_device (int device_id) if (device_id < 0 || device_id >= gomp_get_num_devices ()) return NULL; + gomp_mutex_lock (&devices[device_id].lock); + if (!devices[device_id].is_initialized) + gomp_init_device (&devices[device_id]); + gomp_mutex_unlock (&devices[device_id].lock); + + if (devices[device_id].num_images <= 0) + return NULL; + return &devices[device_id]; } @@ -697,6 +705,7 @@ gomp_offload_image_to_device (struct gomp_device_descr *devicep, struct addr_pair *target_table = NULL; int i, num_target_entries = devicep->load_image_func (devicep->target_id, target_data, &target_table); + devicep->num_images++; if (num_target_entries != num_funcs + num_vars) { @@ -831,6 +840,7 @@ GOMP_offload_unregister (void *host_table, enum offload_target_type target_type, } devicep->unload_image_func (devicep->target_id, target_data); + devicep->num_images--; /* Remove mapping from splay tree. */ struct splay_tree_key_s k; @@ -966,11 +976,6 @@ GOMP_target (int device, void (*fn) (void *), const void *unused, return; } - gomp_mutex_lock (&devicep->lock); - if (!devicep->is_initialized) - gomp_init_device (devicep); - gomp_mutex_unlock (&devicep->lock); - void *fn_addr; if (devicep->capabilities & GOMP_OFFLOAD_CAP_NATIVE_EXEC) @@ -1034,11 +1039,6 @@ GOMP_target_data (int device, const void *unused, size_t mapnum, return; } - gomp_mutex_lock (&devicep->lock); - if (!devicep->is_initialized) - gomp_init_device (devicep); - gomp_mutex_unlock (&devicep->lock); - struct target_mem_desc *tgt = gomp_map_vars (devicep, mapnum, hostaddrs, NULL, sizes, kinds, false, false); @@ -1069,11 +1069,6 @@ GOMP_target_update (int device, const void *unused, size_t mapnum, || !(devicep->capabilities & GOMP_OFFLOAD_CAP_OPENMP_400)) return; - gomp_mutex_lock (&devicep->lock); - if (!devicep->is_initialized) - gomp_init_device (devicep); - gomp_mutex_unlock (&devicep->lock); - gomp_update (devicep, mapnum, hostaddrs, sizes, kinds, false); } @@ -1265,6 +1260,7 @@ gomp_target_init (void) current_device.type = current_device.get_type_func (); current_device.mem_map.root = NULL; current_device.is_initialized = false; + current_device.num_images = 0; current_device.openacc.data_environ = NULL; for (i = 0; i < new_num_devices; i++) { diff --git a/libgomp/testsuite/libgomp.c/target-1-disable.c b/libgomp/testsuite/libgomp.c/target-1-disable.c new file mode 100644 index 0000000..00ea143 --- /dev/null +++ b/libgomp/testsuite/libgomp.c/target-1-disable.c @@ -0,0 +1,4 @@ +/* { dg-options "-foffload=disable" } */ +/* { dg-require-effective-target offload_device } */ + +#include "target-1.c" -- Ilya