| 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 |
|
|---|
| 47 |
|
|---|
| 48 | /* NOTE by WenhaoWu (wuwenhao@udel.edu)
|
|---|
| 49 | * This example is modified so that the bound can be adjusted
|
|---|
| 50 | * by defining 'N' and if it isn't defined then the bound value
|
|---|
| 51 | * in the original example is used as the default value of 'N'
|
|---|
| 52 | */
|
|---|
| 53 | #ifndef N
|
|---|
| 54 | #define N 1000
|
|---|
| 55 | #endif
|
|---|
| 56 |
|
|---|
| 57 | /*
|
|---|
| 58 | A file-scope variable used within a function called by a parallel region.
|
|---|
| 59 | No threadprivate is used to avoid data races.
|
|---|
| 60 |
|
|---|
| 61 | Data race pairs sum0@71:3 vs. sum0@71:8
|
|---|
| 62 | sum0@71:3 vs. sum0@71:3
|
|---|
| 63 | */
|
|---|
| 64 | #include <stdio.h>
|
|---|
| 65 | #include <assert.h>
|
|---|
| 66 | int sum0=0, sum1=0;
|
|---|
| 67 | //#pragma omp threadprivate(sum0)
|
|---|
| 68 |
|
|---|
| 69 | void foo (int i)
|
|---|
| 70 | {
|
|---|
| 71 | sum0=sum0+i;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | int main()
|
|---|
| 75 | {
|
|---|
| 76 | int i, sum=0;
|
|---|
| 77 | #pragma omp parallel
|
|---|
| 78 | {
|
|---|
| 79 | #pragma omp for
|
|---|
| 80 | for (i=1;i<=1000;i++)
|
|---|
| 81 | {
|
|---|
| 82 | foo (i);
|
|---|
| 83 | }
|
|---|
| 84 | #pragma omp critical
|
|---|
| 85 | {
|
|---|
| 86 | sum= sum+sum0;
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 | /* reference calculation */
|
|---|
| 90 | for (i=1;i<=1000;i++)
|
|---|
| 91 | {
|
|---|
| 92 | sum1=sum1+i;
|
|---|
| 93 | }
|
|---|
| 94 | printf("sum=%d; sum1=%d\n",sum,sum1);
|
|---|
| 95 | // assert(sum==sum1);
|
|---|
| 96 | return 0;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|