| 1 | /*
|
|---|
| 2 | Copyright (c) 2017, Lawrence Livermore National Security, LLC.
|
|---|
| 3 | Produced at the Lawrence Livermore National Laboratory
|
|---|
| 4 | Written by Chunhua Liao, Pei-Hung Lin, Joshua Asplund,
|
|---|
| 5 | Markus Schordan, and Ian Karlin
|
|---|
| 6 | (email: liao6@llnl.gov, lin32@llnl.gov, asplund1@llnl.gov,
|
|---|
| 7 | schordan1@llnl.gov, karlin1@llnl.gov)
|
|---|
| 8 | LLNL-CODE-732144
|
|---|
| 9 | All rights reserved.
|
|---|
| 10 |
|
|---|
| 11 | This file is part of DataRaceBench. For details, see
|
|---|
| 12 | https://github.com/LLNL/dataracebench. Please also see the LICENSE file
|
|---|
| 13 | for our additional BSD notice.
|
|---|
| 14 |
|
|---|
| 15 | Redistribution and use in source and binary forms, with
|
|---|
| 16 | or without modification, are permitted provided that the following
|
|---|
| 17 | conditions are met:
|
|---|
| 18 |
|
|---|
| 19 | * Redistributions of source code must retain the above copyright
|
|---|
| 20 | notice, this list of conditions and the disclaimer below.
|
|---|
| 21 |
|
|---|
| 22 | * Redistributions in binary form must reproduce the above copyright
|
|---|
| 23 | notice, this list of conditions and the disclaimer (as noted below)
|
|---|
| 24 | in the documentation and/or other materials provided with the
|
|---|
| 25 | distribution.
|
|---|
| 26 |
|
|---|
| 27 | * Neither the name of the LLNS/LLNL nor the names of its contributors
|
|---|
| 28 | may be used to endorse or promote products derived from this
|
|---|
| 29 | software without specific prior written permission.
|
|---|
| 30 |
|
|---|
| 31 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|---|
| 32 | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|---|
| 33 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|---|
| 34 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|---|
| 35 | DISCLAIMED. IN NO EVENT SHALL LAWRENCE LIVERMORE NATIONAL
|
|---|
| 36 | SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE
|
|---|
| 37 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|---|
| 38 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|---|
| 39 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 40 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|---|
| 41 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|---|
| 42 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|---|
| 43 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|---|
| 44 | THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 45 | */
|
|---|
| 46 | #include <stdio.h>
|
|---|
| 47 | #define min(x, y) (((x) < (y)) ? (x) : (y))
|
|---|
| 48 |
|
|---|
| 49 | /*
|
|---|
| 50 | use of omp target + teams + distribute + parallel for
|
|---|
| 51 | */
|
|---|
| 52 | int main(int argc, char* argv[])
|
|---|
| 53 | {
|
|---|
| 54 | int i, i2;
|
|---|
| 55 | int len = 2560;
|
|---|
| 56 | double sum =0.0, sum2=0.0;
|
|---|
| 57 | double a[len], b[len];
|
|---|
| 58 | /*Initialize with some values*/
|
|---|
| 59 | for (i=0; i<len; i++)
|
|---|
| 60 | {
|
|---|
| 61 | a[i]= ((double)i)/2.0;
|
|---|
| 62 | b[i]= ((double)i)/3.0;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | #pragma omp target map(to: a[0:len], b[0:len]) map(tofrom: sum)
|
|---|
| 66 | #pragma omp teams num_teams(10) thread_limit(256) reduction (+:sum)
|
|---|
| 67 | #pragma omp distribute
|
|---|
| 68 | for (i2=0; i2< len; i2+=256)
|
|---|
| 69 | #pragma omp parallel for reduction (+:sum)
|
|---|
| 70 | for (i=i2;i< min(i2+256, len); i++)
|
|---|
| 71 | sum += a[i]*b[i];
|
|---|
| 72 |
|
|---|
| 73 | /* CPU reference computation */
|
|---|
| 74 | #pragma omp parallel for reduction (+:sum2)
|
|---|
| 75 | for (i=0;i< len; i++)
|
|---|
| 76 | sum2 += a[i]*b[i];
|
|---|
| 77 | printf ("sum=%f sum2=%f\n", sum, sum2);
|
|---|
| 78 | return 0;
|
|---|
| 79 | }
|
|---|