From patchwork Tue Jun 13 09:35:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tobias Burnus X-Patchwork-Id: 107215 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp414461vqr; Tue, 13 Jun 2023 02:36:10 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5slD7dHPDT1RdOCfOkuu8cB1UjPGXyJT9maJQWspbXM2PZR0o+Qg1fJUncUy6aGpPMV+Fy X-Received: by 2002:a17:907:74a:b0:978:af67:c7f6 with SMTP id xc10-20020a170907074a00b00978af67c7f6mr11245698ejb.13.1686648970471; Tue, 13 Jun 2023 02:36:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686648970; cv=none; d=google.com; s=arc-20160816; b=d7F+ueBaAX0bZblvwwHsxTAWMSINJDHi4VxEu19CZ4BwMDv/VH+Dl4q8nhE2xLggSa qs4CNpRLEvesUV4KXTBGxRunney/CY0RUG7KI66pD2fZKD4bUR1mh7H3itVbkWu01AZL JZX2jzME35gS+Rnbyfy9Q0UIqsnVlUvVguWbN3vhp7dPKDA4exnhc2ogyILwH6nMLhd8 RTpu0rto47JWBRZER8mEiCp5G8k+nCPq4gmK6yiFIacjjTtKVSsmuebEmApQ+HC9OB79 0k4B3HYv/9S1z5ko/LkzhRWgSW37GbkCg5qouk2MTzW7ybRroAUU7XlLfXXZVTBjxGYV FUNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:from:to :content-language:user-agent:mime-version:date:message-id :ironport-sdr:dmarc-filter:delivered-to; bh=gfweo3Pf9+fWye9+pHwTsvX1pPsimBBc9jOS9XaJG1g=; b=G8oTGBxqe3zTYlFC/T81J/nG5s9xn3kYHHaXbG1Hh0d0bpNfb7EpznU/wgITI4CZBL 3uoGUvaKVa3dWyJthHJJ9zQr/br0pvupxSZJ3lJuNwM8ATPZ1bEkyBtKoB2oZBTozUaB JvoqEKEahzvu5IAiak4llZDSiY3iP3vZwe4INkCd6iTEKDtzqz8cu2jnc/ToKTMEsq5v 37tN+KIyOj43Zs2o4EvqV3Ywb2YEbzqbQvlA8WoTBsysMNAoN0SLQmYA4WD9K9CbH5uR wJED8AyReSWOgt2EpqBbuPFw3B4v0RgJHurmAb8ZL5wr2bUXNYR+r8t+l4tnWZdL9xZa iAfQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id bl18-20020a170906c25200b0095357b3921bsi6563438ejb.863.2023.06.13.02.36.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Jun 2023 02:36:10 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 48C253857C45 for ; Tue, 13 Jun 2023 09:35:52 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa3.mentor.iphmx.com (esa3.mentor.iphmx.com [68.232.137.180]) by sourceware.org (Postfix) with ESMTPS id 4B38E385843E for ; Tue, 13 Jun 2023 09:35:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4B38E385843E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com X-IronPort-AV: E=Sophos;i="6.00,239,1681200000"; d="diff'?scan'208";a="8625949" Received: from orw-gwy-01-in.mentorg.com ([192.94.38.165]) by esa3.mentor.iphmx.com with ESMTP; 13 Jun 2023 01:35:16 -0800 IronPort-SDR: ed5G9vZ2JdkYgQ5rdKgMux8rnDrejbxmprIgOUHubQHIwnhA2KxQtnLnBUSn7VB+K0qw12bk+n oBXtSxGbiO3dUyeNQjUXs7K64Nb0wEjFRSSQQOaPrpiSbZYARtKZ7/KyI8ubqrdtgdZOlaO3MT z5B9FNZ5uMNWVd36rsT2tsLNt9u0+XRTMafxjdN5G76Sn/MheRRx/BqpOmlVfeEwk26MAdnDlW JceBlOQsqIp4JhI5ZKmfXZz6cMS6nSJ5RTCaegqSUe5B6CP0jPPtAtiJAjl2S16XAAztuevDdK Jro= Message-ID: <13868b67-0a2c-771b-2a30-36e097e89519@codesourcery.com> Date: Tue, 13 Jun 2023 11:35:11 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Content-Language: en-US To: gcc-patches From: Tobias Burnus Subject: [committed] libgomp/testsuite: Add requires-unified-addr-1.{c,f90} [PR109837] X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-13.mgc.mentorg.com (139.181.222.13) To svr-ies-mbx-12.mgc.mentorg.com (139.181.222.12) X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768579631053101546?= X-GMAIL-MSGID: =?utf-8?q?1768579631053101546?= Add a testcase for "omp requires unified_address" as we hadn't one. The feature itself worked since the beginning (hardware + implementation wise); that the devices report 'omp requires unified_address' as supported is newer: for nvptx since r13-3460-g131d18e928a3ea and for GCN since r14-1584-gf1af7d65ff64fe (a week ago). The test assumes (→ dg-output) that all offload devices support unified_address; this implies: if an offloading device is available, it also remains available after adding the unified-address requirement. Goal: ensure that we don't end up with only host fallback. Unified address implies: Pointer size is the same such that no 'is_device_ptr' is required to convert an opaque pointer, it also permits to do device-pointer pointer arithmetic on the host. This testcase also assumes that 'int' / 'integer' has the same size on host and device. (If not: good luck with offloading in general!) The test also makes the sound assumption that derived-type component pointers are passed through with the derived-type itself such that the pointer address remains well defined. In terms of the standard, it would have an undefined association status. Committed as Rev. r14-1783-gd5c58ad1ebaff9 Tobias ----------------- Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955 commit d5c58ad1ebaff924c2546df074174cffb128feb8 Author: Tobias Burnus Date: Tue Jun 13 11:27:47 2023 +0200 libgomp/testsuite: Add requires-unified-addr-1.{c,f90} [PR109837] Add a testcase for 'omp requires unified_address' that is currently supported by all devices but was not tested for. libgomp/ PR libgomp/109837 * testsuite/libgomp.c-c++-common/requires-unified-addr-1.c: New test. * testsuite/libgomp.fortran/requires-unified-addr-1.f90: New test. --- .../libgomp.c-c++-common/requires-unified-addr-1.c | 74 ++++++++++++++ .../libgomp.fortran/requires-unified-addr-1.f90 | 111 +++++++++++++++++++++ 2 files changed, 185 insertions(+) diff --git a/libgomp/testsuite/libgomp.c-c++-common/requires-unified-addr-1.c b/libgomp/testsuite/libgomp.c-c++-common/requires-unified-addr-1.c new file mode 100644 index 00000000000..bff0a6b31ab --- /dev/null +++ b/libgomp/testsuite/libgomp.c-c++-common/requires-unified-addr-1.c @@ -0,0 +1,74 @@ +/* PR libgomp/109837 */ + +#include +#include +#include +#include + +#pragma omp requires unified_address + +#define N 15 + +void +test_device (int dev) +{ + struct st { + int *ptr; + int n; + }; + struct st s; + + s.n = 10; + s.ptr = (int *) omp_target_alloc (sizeof (int)*s.n, dev); + int *ptr1 = (int *) omp_target_alloc (sizeof (int)*N, dev); + assert (s.ptr != NULL); + assert (ptr1 != NULL); + + int q[4] = {1,2,3,4}; + int *qptr; + #pragma omp target enter data map(q) device(device_num: dev) + #pragma omp target data use_device_addr(q) device(device_num: dev) + qptr = q; + + #pragma omp target map(to:s) device(device_num: dev) + for (int i = 0; i < s.n; i++) + s.ptr[i] = 23*i; + + int *ptr2 = &s.ptr[3]; + + #pragma omp target firstprivate(qptr) map(tofrom:ptr2) device(device_num: dev) + for (int i = 0; i < 4; i++) + *(qptr++) = ptr2[i]; + + #pragma omp target exit data map(q) device(device_num: dev) + for (int i = 0; i < 4; i++) + q[i] = 23 * (i+3); + + #pragma omp target map(to: ptr1) device(device_num: dev) + for (int i = 0; i < N; i++) + ptr1[i] = 11*i; + + int *ptr3 = (int *) malloc (sizeof (int)*N); + assert (0 == omp_target_memcpy(ptr3, ptr1, N * sizeof(int), 0, 0, + omp_get_initial_device(), dev)); + for (int i = 0; i < N; i++) + assert (ptr3[i] == 11*i); + + free (ptr3); + omp_target_free (ptr1, dev); + omp_target_free (s.ptr, dev); +} + +int +main() +{ + int ntgts = omp_get_num_devices(); + if (ntgts) + fprintf (stderr, "Offloading devices exist\n"); /* { dg-output "Offloading devices exist(\n|\r\n|\r)" { target offload_device } } */ + else + fprintf (stderr, "Only host fallback\n"); /* { dg-output "Only host fallback(\n|\r\n|\r)" { target { ! offload_device } } } */ + + for (int i = 0; i <= ntgts; i++) + test_device (i); + return 0; +} diff --git a/libgomp/testsuite/libgomp.fortran/requires-unified-addr-1.f90 b/libgomp/testsuite/libgomp.fortran/requires-unified-addr-1.f90 new file mode 100644 index 00000000000..f5a5adf093b --- /dev/null +++ b/libgomp/testsuite/libgomp.fortran/requires-unified-addr-1.f90 @@ -0,0 +1,111 @@ +! PR libgomp/109837 + +program main + use iso_c_binding + use iso_fortran_env + use omp_lib + implicit none (external, type) + !$omp requires unified_address + + integer(c_intptr_t), parameter :: N = 15 + integer :: i, ntgts + + ntgts = omp_get_num_devices(); + if (ntgts > 0) then + write (ERROR_UNIT, '(a)') "Offloading devices exist" ! { dg-output "Offloading devices exist(\n|\r\n|\r)" { target offload_device } } + else + write (ERROR_UNIT, '(a)') "Only host fallback" ! { dg-output "Only host fallback(\n|\r\n|\r)" { target { ! offload_device } } } + endif + + do i = 0, ntgts + call test_device (i); + end do + +contains + + subroutine test_device (dev) + integer, value, intent(in) :: dev + + type t + integer(c_intptr_t) :: n, m + integer, pointer :: fptr(:) + type(c_ptr) :: cptr + end type t + type(t) :: s + type(c_ptr) :: cptr, qptr, cptr2, cptr2a + integer, target :: q(4) + integer, pointer :: fptr(:) + integer(c_intptr_t) :: i + + s%n = 10; + s%m = 23; + s%cptr = omp_target_alloc (s%n * NUMERIC_STORAGE_SIZE/CHARACTER_STORAGE_SIZE, dev); + cptr = omp_target_alloc (s%m * NUMERIC_STORAGE_SIZE/CHARACTER_STORAGE_SIZE, dev); + if (.not. c_associated(s%cptr)) stop 1 + if (.not. c_associated(cptr)) stop 2 + call c_f_pointer (cptr, s%fptr, [s%m]) + + cptr = omp_target_alloc (N * NUMERIC_STORAGE_SIZE/CHARACTER_STORAGE_SIZE, dev); + if (.not. c_associated(cptr)) stop 3 + + q = [1, 2, 3, 4] + !$omp target enter data map(q) device(device_num: dev) + !$omp target data use_device_addr(q) device(device_num: dev) + qptr = c_loc(q) + !$omp end target data + + !$omp target map(to:s) device(device_num: dev) + block + integer, pointer :: iptr(:) + call c_f_pointer(s%cptr, iptr, [s%n]) + do i = 1, s%n + iptr(i) = 23 * int(i) + end do + do i = 1, s%m + s%fptr(i) = 35 * int(i) + end do + end block + + cptr2 = c_loc(s%fptr(4)) + cptr2a = s%cptr + + !$omp target firstprivate(qptr) map(tofrom: cptr2) map(to :cptr2a) device(device_num: dev) + block + integer, pointer :: iptr(:), iptr2(:), qvar(:) + call c_f_pointer(cptr2, iptr, [4]) + call c_f_pointer(cptr2a, iptr2, [4]) + call c_f_pointer(qptr, qvar, [4]) + qvar = iptr + iptr2 + end block + + !$omp target exit data map(q) device(device_num: dev) + do i = 1, 4 + if (q(i) /= 23 * int(i) + 35 * (int(i) + 4 - 1)) stop 4 + end do + + !$omp target map(to: cptr) device(device_num: dev) + block + integer, pointer :: p(:) + call c_f_pointer(cptr, p, [N]) + do i = 1, N + p(i) = 11 * int(i) + end do + end block + + allocate(fptr(N)) + if (0 /= omp_target_memcpy (c_loc(fptr), cptr, & + N * NUMERIC_STORAGE_SIZE/CHARACTER_STORAGE_SIZE, & + 0_c_intptr_t, 0_c_intptr_t, & + omp_get_initial_device(), dev)) & + stop 5 + + do i = 1, N + if (fptr(i) /= 11 * int(i)) stop 6 + end do + + deallocate (fptr); + call omp_target_free (cptr, dev); + call omp_target_free (s%cptr, dev); + call omp_target_free (c_loc(s%fptr), dev); + end +end