| 1 | // Two parallel for loops within one single parallel region,
|
|---|
| 2 | // combined with private() and reduction().
|
|---|
| 3 | #include <stdio.h>
|
|---|
| 4 | #include <math.h>
|
|---|
| 5 |
|
|---|
| 6 | #define MSIZE 200
|
|---|
| 7 | int n=MSIZE, m=MSIZE, mits=1000;
|
|---|
| 8 | double tol=0.0000000001, relax = 1.0, alpha = 0.0543;
|
|---|
| 9 | double u[MSIZE][MSIZE], f[MSIZE][MSIZE], uold[MSIZE][MSIZE];
|
|---|
| 10 | double dx, dy;
|
|---|
| 11 |
|
|---|
| 12 | void
|
|---|
| 13 | initialize ()
|
|---|
| 14 | {
|
|---|
| 15 | int i, j, xx, yy;
|
|---|
| 16 |
|
|---|
| 17 | dx = 2.0 / (n - 1); // -->dx@112:2
|
|---|
| 18 | dy = 2.0 / (m - 1); //-->dy@113:2
|
|---|
| 19 |
|
|---|
| 20 | /* Initialize initial condition and RHS */
|
|---|
| 21 | //#pragma omp parallel for private(i,j,xx,yy)
|
|---|
| 22 | for (i = 0; i < n; i++)
|
|---|
| 23 | for (j = 0; j < m; j++)
|
|---|
| 24 | {
|
|---|
| 25 | xx = (int) (-1.0 + dx * (i - 1)); /* -1 < x < 1 */
|
|---|
| 26 | yy = (int) (-1.0 + dy * (j - 1)); /* -1 < y < 1 */
|
|---|
| 27 | u[i][j] = 0.0;
|
|---|
| 28 | f[i][j] = -1.0 * alpha * (1.0 - xx * xx) * (1.0 - yy * yy)
|
|---|
| 29 | - 2.0 * (1.0 - xx * xx) - 2.0 * (1.0 - yy * yy);
|
|---|
| 30 |
|
|---|
| 31 | }
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | void
|
|---|
| 35 | jacobi ()
|
|---|
| 36 | {
|
|---|
| 37 | double omega;
|
|---|
| 38 | int i, j, k;
|
|---|
| 39 | double error, resid, ax, ay, b;
|
|---|
| 40 |
|
|---|
| 41 | omega = relax;
|
|---|
| 42 | /*
|
|---|
| 43 | * Initialize coefficients */
|
|---|
| 44 |
|
|---|
| 45 | dx = 2.0 / (n - 1); // -->dx@112:2
|
|---|
| 46 | dy = 2.0 / (m - 1); //-->dy@113:2
|
|---|
| 47 |
|
|---|
| 48 | ax = 1.0 / (dx * dx); /* X-direction coef */
|
|---|
| 49 | ay = 1.0 / (dy * dy); /* Y-direction coef */
|
|---|
| 50 | b = -2.0 / (dx * dx) - 2.0 / (dy * dy) - alpha; /* Central coeff */
|
|---|
| 51 |
|
|---|
| 52 | error = 10.0 * tol;
|
|---|
| 53 | k = 1;
|
|---|
| 54 |
|
|---|
| 55 | while (k <= mits)
|
|---|
| 56 | {
|
|---|
| 57 | error = 0.0;
|
|---|
| 58 |
|
|---|
| 59 | /* Copy new solution into old */
|
|---|
| 60 | #pragma omp parallel
|
|---|
| 61 | {
|
|---|
| 62 | #pragma omp for private(i,j)
|
|---|
| 63 | for (i = 0; i < n; i++)
|
|---|
| 64 | for (j = 0; j < m; j++)
|
|---|
| 65 | uold[i][j] = u[i][j];
|
|---|
| 66 | #pragma omp for private(i,j,resid) reduction(+:error) nowait
|
|---|
| 67 | for (i = 1; i < (n - 1); i++)
|
|---|
| 68 | for (j = 1; j < (m - 1); j++)
|
|---|
| 69 | {
|
|---|
| 70 | resid = (ax * (uold[i - 1][j] + uold[i + 1][j])
|
|---|
| 71 | + ay * (uold[i][j - 1] + uold[i][j + 1]) +
|
|---|
| 72 | b * uold[i][j] - f[i][j]) / b;
|
|---|
| 73 |
|
|---|
| 74 | u[i][j] = uold[i][j] - omega * resid;
|
|---|
| 75 | error = error + resid * resid;
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 | /* omp end parallel */
|
|---|
| 79 |
|
|---|
| 80 | /* Error check */
|
|---|
| 81 |
|
|---|
| 82 | k = k + 1;
|
|---|
| 83 | error = sqrt (error) / (n * m);
|
|---|
| 84 | } /* End iteration loop */
|
|---|
| 85 |
|
|---|
| 86 | printf ("Total Number of Iterations:%d\n", k);
|
|---|
| 87 | printf ("Residual:%E\n", error);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | int main()
|
|---|
| 91 | {
|
|---|
| 92 | initialize();
|
|---|
| 93 | jacobi();
|
|---|
| 94 | return 0;
|
|---|
| 95 | }
|
|---|