source: CIVL/examples/omp/dataracebench-1.3.2/micro-benchmarks/DRB005-indirectaccess1-orig-yes.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100755
File size: 4.8 KB
Line 
1/*
2Copyright (c) 2017, Lawrence Livermore National Security, LLC.
3Produced at the Lawrence Livermore National Laboratory
4Written by Chunhua Liao, Pei-Hung Lin, Joshua Asplund,
5Markus Schordan, and Ian Karlin
6(email: liao6@llnl.gov, lin32@llnl.gov, asplund1@llnl.gov,
7schordan1@llnl.gov, karlin1@llnl.gov)
8LLNL-CODE-732144
9All rights reserved.
10
11This file is part of DataRaceBench. For details, see
12https://github.com/LLNL/dataracebench. Please also see the LICENSE file
13for our additional BSD notice.
14
15Redistribution and use in source and binary forms, with
16or without modification, are permitted provided that the following
17conditions 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
31THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35DISCLAIMED. IN NO EVENT SHALL LAWRENCE LIVERMORE NATIONAL
36SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE
37LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
38OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
44THE POSSIBILITY OF SUCH DAMAGE.
45*/
46/*
47This program is extracted from a real application at LLNL.
48Two pointers (xa1 and xa2) have a pair of values with a distance of 12.
49They are used as start base addresses for two 1-D arrays.
50Their index set has two indices with distance of 12: 999 +12 = 1011.
51So there is loop carried dependence.
52
53However, having loop carried dependence does not mean data races will always happen.
54The iterations with loop carried dependence must be scheduled to
55different threads in order for data races to happen.
56
57In this example, we use schedule(static,1) to increase the chance that
58the dependent loop iterations will be scheduled to different threads.
59Data race pair: xa1[idx]@136:5 vs. xa2[idx]@137:5
60*/
61#include <assert.h>
62#include <stdio.h>
63#include <stdlib.h>
64
65/* NOTE by WenhaoWu (wuwenhao@udel.edu)
66 * This example is modified so that the bound can be adjusted
67 * by defining 'N' and if it isn't defined then the bound value
68 * in the original example is used as the default value of 'N'
69 */
70#ifndef N
71#define N 180
72#endif
73
74int indexSet[180] = {
75521, 523, 525, 527, 529, 531,
76547, 549, 551, 553, 555, 557,
77573, 575, 577, 579, 581, 583,
78599, 601, 603, 605, 607, 609,
79625, 627, 629, 631, 633, 635,
80
81651, 653, 655, 657, 659, 661,
82859, 861, 863, 865, 867, 869,
83885, 887, 889, 891, 893, 895,
84911, 913, 915, 917, 919, 923, // change original 921 to 923 = 911+12
85937, 939, 941, 943, 945, 947,
86
87963, 965, 967, 969, 971, 973,
88989, 991, 993, 995, 997, 999,
891197, 1199, 1201, 1203, 1205, 1207,
901223, 1225, 1227, 1229, 1231, 1233,
911249, 1251, 1253, 1255, 1257, 1259,
92
931275, 1277, 1279, 1281, 1283, 1285,
941301, 1303, 1305, 1307, 1309, 1311,
951327, 1329, 1331, 1333, 1335, 1337,
961535, 1537, 1539, 1541, 1543, 1545,
971561, 1563, 1565, 1567, 1569, 1571,
98
991587, 1589, 1591, 1593, 1595, 1597,
1001613, 1615, 1617, 1619, 1621, 1623,
1011639, 1641, 1643, 1645, 1647, 1649,
1021665, 1667, 1669, 1671, 1673, 1675,
1031873, 1875, 1877, 1879, 1881, 1883,
104
1051899, 1901, 1903, 1905, 1907, 1909,
1061925, 1927, 1929, 1931, 1933, 1935,
1071951, 1953, 1955, 1957, 1959, 1961,
1081977, 1979, 1981, 1983, 1985, 1987,
1092003, 2005, 2007, 2009, 2011, 2013};
110
111int main (int argc, char* argv[])
112{
113 // max index value is 2013. +12 to obtain a valid xa2[idx] after xa1+12.
114 // +1 to ensure a reference like base[2015] is within the bound.
115 double * base = (double*) malloc(sizeof(double)* (2013+12+1));
116 if (base == 0)
117 {
118 printf ("Error in malloc(). Aborting ...\n");
119 return 1;
120 }
121
122 double * xa1 = base;
123 double * xa2 = xa1 + 12;
124 int i;
125
126 // initialize segments touched by indexSet
127 for (i =521; i<= 2025; ++i)
128 {
129 base[i]=0.5*i;
130 }
131// default static even scheduling may not trigger data race, using static,1 instead.
132#pragma omp parallel for schedule(static,1)
133 for (i =0; i< N; ++i)
134 {
135 int idx = indexSet[i];
136 xa1[idx]+= 1.0 + i;
137 xa2[idx]+= 3.0 + i;
138 }
139
140 printf("x1[999]=%f xa2[1285]=%f\n", xa1[999], xa2[1285]);
141 free (base);
142 return 0;
143}
144
Note: See TracBrowser for help on using the repository browser.