| [a1acb0c5] | 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 |
|
|---|
| [86ee0b6] | 47 | /*
|
|---|
| 48 | Two parallel for loops within one single parallel region,
|
|---|
| 49 | combined with private() and reduction().
|
|---|
| 50 | */
|
|---|
| [a1acb0c5] | 51 | #include <stdio.h>
|
|---|
| 52 | #include <math.h>
|
|---|
| 53 |
|
|---|
| 54 | #define MSIZE 200
|
|---|
| 55 | int n=MSIZE, m=MSIZE, mits=1000;
|
|---|
| 56 | double tol=0.0000000001, relax = 1.0, alpha = 0.0543;
|
|---|
| 57 | double u[MSIZE][MSIZE], f[MSIZE][MSIZE], uold[MSIZE][MSIZE];
|
|---|
| 58 | double dx, dy;
|
|---|
| 59 |
|
|---|
| 60 | void
|
|---|
| 61 | initialize ()
|
|---|
| 62 | {
|
|---|
| 63 | int i, j, xx, yy;
|
|---|
| 64 |
|
|---|
| [86ee0b6] | 65 | dx = 2.0 / (n - 1);
|
|---|
| 66 | dy = 2.0 / (m - 1);
|
|---|
| [a1acb0c5] | 67 |
|
|---|
| [86ee0b6] | 68 | /* Initialize initial condition and RHS */
|
|---|
| [a1acb0c5] | 69 | //#pragma omp parallel for private(i,j,xx,yy)
|
|---|
| 70 | for (i = 0; i < n; i++)
|
|---|
| 71 | for (j = 0; j < m; j++)
|
|---|
| 72 | {
|
|---|
| 73 | xx = (int) (-1.0 + dx * (i - 1)); /* -1 < x < 1 */
|
|---|
| 74 | yy = (int) (-1.0 + dy * (j - 1)); /* -1 < y < 1 */
|
|---|
| 75 | u[i][j] = 0.0;
|
|---|
| 76 | f[i][j] = -1.0 * alpha * (1.0 - xx * xx) * (1.0 - yy * yy)
|
|---|
| 77 | - 2.0 * (1.0 - xx * xx) - 2.0 * (1.0 - yy * yy);
|
|---|
| 78 |
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void
|
|---|
| 83 | jacobi ()
|
|---|
| 84 | {
|
|---|
| 85 | double omega;
|
|---|
| 86 | int i, j, k;
|
|---|
| 87 | double error, resid, ax, ay, b;
|
|---|
| 88 |
|
|---|
| 89 | omega = relax;
|
|---|
| [86ee0b6] | 90 | /* Initialize coefficients */
|
|---|
| [a1acb0c5] | 91 |
|
|---|
| [86ee0b6] | 92 | dx = 2.0 / (n - 1);
|
|---|
| 93 | dy = 2.0 / (m - 1);
|
|---|
| [a1acb0c5] | 94 |
|
|---|
| 95 | ax = 1.0 / (dx * dx); /* X-direction coef */
|
|---|
| 96 | ay = 1.0 / (dy * dy); /* Y-direction coef */
|
|---|
| 97 | b = -2.0 / (dx * dx) - 2.0 / (dy * dy) - alpha; /* Central coeff */
|
|---|
| 98 |
|
|---|
| 99 | error = 10.0 * tol;
|
|---|
| 100 | k = 1;
|
|---|
| 101 |
|
|---|
| 102 | while (k <= mits)
|
|---|
| 103 | {
|
|---|
| 104 | error = 0.0;
|
|---|
| 105 |
|
|---|
| 106 | /* Copy new solution into old */
|
|---|
| 107 | #pragma omp parallel
|
|---|
| 108 | {
|
|---|
| 109 | #pragma omp for private(i,j)
|
|---|
| 110 | for (i = 0; i < n; i++)
|
|---|
| 111 | for (j = 0; j < m; j++)
|
|---|
| 112 | uold[i][j] = u[i][j];
|
|---|
| 113 | #pragma omp for private(i,j,resid) reduction(+:error) nowait
|
|---|
| 114 | for (i = 1; i < (n - 1); i++)
|
|---|
| 115 | for (j = 1; j < (m - 1); j++)
|
|---|
| 116 | {
|
|---|
| 117 | resid = (ax * (uold[i - 1][j] + uold[i + 1][j])
|
|---|
| 118 | + ay * (uold[i][j - 1] + uold[i][j + 1]) +
|
|---|
| 119 | b * uold[i][j] - f[i][j]) / b;
|
|---|
| 120 |
|
|---|
| 121 | u[i][j] = uold[i][j] - omega * resid;
|
|---|
| 122 | error = error + resid * resid;
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | /* omp end parallel */
|
|---|
| 126 |
|
|---|
| 127 | /* Error check */
|
|---|
| 128 |
|
|---|
| 129 | k = k + 1;
|
|---|
| 130 | error = sqrt (error) / (n * m);
|
|---|
| 131 | } /* End iteration loop */
|
|---|
| 132 |
|
|---|
| 133 | printf ("Total Number of Iterations:%d\n", k);
|
|---|
| 134 | printf ("Residual:%E\n", error);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | int main()
|
|---|
| 138 | {
|
|---|
| 139 | initialize();
|
|---|
| 140 | jacobi();
|
|---|
| 141 | return 0;
|
|---|
| 142 | }
|
|---|