source: CIVL/examples/omp/c_jacobi03.c@ 1aaefd4

main test-branch
Last change on this file since 1aaefd4 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 100644
File size: 9.7 KB
Line 
1/* ***********************************************************************
2 This program is part of the
3 OpenMP Source Code Repository
4
5 http://www.pcg.ull.es/ompscr/
6 e-mail: ompscr@etsii.ull.es
7
8 Copyright (c) 2004, OmpSCR Group
9 All rights reserved.
10
11 Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright notice,
14 this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18 * Neither the name of the University of La Laguna nor the names of its contributors
19 may be used to endorse or promote products derived from this software without
20 specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
28 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 OF SUCH DAMAGE.
32
33 FILE: c_jacobi03.c
34 VERSION: 1.0
35 DATE: Oct 2004
36 AUTHORS: Author: Joseph Robicheaux, Kuck and Associates, Inc. (KAI), 1998
37 Modified: Sanjiv Shah, Kuck and Associates, Inc. (KAI), 1998
38 This version: Dieter an Mey, Aachen University (RWTH), 1999 - 2003
39 anmey@rz.rwth-aachen.de
40 http://www.rwth-aachen.de/People/D.an.Mey.html
41 COMMENTS TO: ompscr@etsii.ull.es
42 DESCRIPTION: program to solve a finite difference discretization of Helmholtz equation :
43 (d2/dx2)u + (d2/dy2)u - alpha u = f using Jacobi iterative method.
44 COMMENTS: OpenMP version 3: 1 PR outside the iteration loop, 4 Barriers
45 Directives are used in this code to achieve paralleism.
46 All do loops are parallized with default 'static' scheduling.
47 REFERENCES: http://www.rz.rwth-aachen.de/computing/hpc/prog/par/openmp/jacobi.html
48 BASIC PRAGMAS: parallel for
49 USAGE: ./c_jacobi03.par 5000 5000 0.8 1.0 1000
50 INPUT: n - grid dimension in x direction
51 m - grid dimension in y direction
52 alpha - Helmholtz constant (always greater than 0.0)
53 tol - error tolerance for iterative solver
54 relax - Successice over relaxation parameter
55 mits - Maximum iterations for iterative solver
56 OUTPUT: Residual and error
57 u(n,m) - Dependent variable (solutions)
58 f(n,m) - Right hand side function
59 FILE FORMATS: -
60 RESTRICTIONS: -
61 REVISION HISTORY:
62**************************************************************************/
63#include <omp.h>
64#include <stdio.h>
65#include <math.h>
66#include <stdlib.h>
67//#include "OmpSCR.h"
68
69#define U(i,j) u[(i)*n+(j)]
70#define F(i,j) f[(i)*n+(j)]
71#define NUM_ARGS 6
72#define NUM_TIMERS 1
73
74#define NIN 4
75#define MIN 4
76#define ALPHA 0.1
77#define TOL 0.1
78#define RELAX 2
79#define MITS 2
80
81int n, m, mits;
82double tol, relax, alpha;
83
84void jacobi (int n, int m, double dx, double dy,
85 double alpha, double omega,
86 double *u, double *f,
87 double tol, int maxit );
88
89
90
91/******************************************************
92* Initializes data
93* Assumes exact solution is u(x,y) = (1-x^2)*(1-y^2)
94*
95******************************************************/
96void initialize(
97 int n,
98 int m,
99 double alpha,
100 double *dx,
101 double *dy,
102 double *u,
103 double *f)
104{
105 int i,j,xx,yy;
106
107 *dx = 2.0 / (n-1);
108 *dy = 2.0 / (m-1);
109
110 /* Initilize initial condition and RHS */
111 for (j=0; j<m; j++){
112 for (i=0; i<n; i++){
113 xx = -1.0 + *dx * (i-1);
114 yy = -1.0 + *dy * (j-1);
115 U(j,i) = 0.0;
116 F(j,i) = -alpha * (1.0 - xx*xx) * (1.0 - yy*yy)
117 - 2.0 * (1.0 - xx*xx) - 2.0 * (1.0 - yy*yy);
118 }
119 }
120
121}
122
123
124/************************************************************
125* Checks error between numerical and exact solution
126*
127************************************************************/
128void error_check(
129 int n,
130 int m,
131 double alpha,
132 double dx,
133 double dy,
134 double *u,
135 double *f)
136{
137 int i,j;
138 double xx, yy, temp, error;
139
140 dx = 2.0 / (n-1);
141 dy = 2.0 / (n-2);
142 error = 0.0;
143
144 for (j=0; j<m; j++){
145 for (i=0; i<n; i++){
146 xx = -1.0 + dx * (i-1);
147 yy = -1.0 + dy * (j-1);
148 temp = U(j,i) - (1.0 - xx*xx) * (1.0 - yy*yy);
149 error += temp*temp;
150 }
151 }
152
153 error = sqrt(error)/(n*m);
154
155 printf("Solution Error : %g\n", error);
156
157}
158
159
160
161
162int main(int argc, char **argv){
163 double *u, *f, dx, dy;
164 double dt, mflops;
165 int NUMTHREADS;
166// char *PARAM_NAMES[NUM_ARGS] = {"Grid dimension: X dir =", "Grid dimension: Y dir =", "Helmhotlz constant =",
167// "Successive over-relaxation parameter =",
168// "error tolerance for iterative solver =", "Maximum iterations for solver ="};
169// char *TIMERS_NAMES[NUM_TIMERS] = {"Total_time"};
170// char *DEFAULT_VALUES[NUM_ARGS] = {"5000", "5000", "0.8", "1.0", "1e-7", "1000"};
171
172
173
174 NUMTHREADS = 1; //omp_get_num_threads();
175 //OSCR_init (NUMTHREADS, "Jacobi Solver v1", "Use 'jacoib03' <n> <m> <alpha> <relax> <tol> <mits>", NUM_ARGS,
176 // PARAM_NAMES, DEFAULT_VALUES , NUM_TIMERS, NUM_TIMERS, TIMERS_NAMES,
177 // argc, argv);
178
179 n = NIN; // OSCR_getarg_int(1);
180 m = MIN; // OSCR_getarg_int(2);
181 alpha = ALPHA; // OSCR_getarg_double(3);
182 relax = RELAX; // OSCR_getarg_double(4);
183 tol = TOL; // OSCR_getarg_double(5);
184 mits = MITS; // OSCR_getarg_int(6);
185
186 printf("-> %d, %d, %g, %g, %g, %d\n",
187 n, m, alpha, relax, tol, mits);
188
189 u = (double *) malloc(n*m*sizeof(double));
190 f = (double *) malloc(n*m*sizeof(double));
191
192
193 /* arrays are allocated and initialzed */
194 initialize(n, m, alpha, &dx, &dy, u, f);
195
196
197 /* Solve Helmholtz eqiation */
198 //OSCR_timer_start(0);
199 jacobi(n, m, dx, dy, alpha, relax, u,f, tol, mits);
200
201 //OSCR_timer_stop(0);
202 dt = 1; //OSCR_timer_read(0);
203
204 printf(" elapsed time : %12.6f\n", dt);
205 mflops = (0.000001*mits*(m-2)*(n-2)*13) / dt;
206 printf(" MFlops : %12.6g (%d, %d, %d, %g)\n",mflops, mits, m, n, dt);
207
208 error_check(n, m, alpha, dx, dy, u, f);
209
210 //OSCR_report(1, TIMERS_NAMES);
211
212 return 0;
213}
214
215
216
217/*
218 subroutine jacobi (n,m,dx,dy,alpha,omega,u,f,tol,maxit)
219******************************************************************
220* Subroutine HelmholtzJ
221* Solves poisson equation on rectangular grid assuming :
222* (1) Uniform discretization in each direction, and
223* (2) Dirichlect boundary conditions
224*
225* Jacobi method is used in this routine
226*
227* Input : n,m Number of grid points in the X/Y directions
228* dx,dy Grid spacing in the X/Y directions
229* alpha Helmholtz eqn. coefficient
230* omega Relaxation factor
231* f(n,m) Right hand side function
232* u(n,m) Dependent variable/Solution
233* tol Tolerance for iterative solver
234* maxit Maximum number of iterations
235*
236* Output : u(n,m) - Solution
237*****************************************************************
238*/
239void jacobi ( const int n, const int m, double dx, double dy, double alpha,
240 double omega, double *u, double *f, double tol, int maxit )
241{
242 int i,j,k;
243 double error, resid, ax, ay, b;
244
245
246 double *uold;
247
248 /* wegen Array-Kompatibilitaet, werden die Zeilen und Spalten (im Kopf)
249 getauscht, zB uold[spalten_num][zeilen_num]; bzw. wir tuen so, als ob wir das
250 gespiegelte Problem loesen wollen */
251
252 uold = (double *)malloc(sizeof(double) * n *m);
253
254
255
256 ax = 1.0/(dx * dx); /* X-direction coef */
257 ay = 1.0/(dy*dy); /* Y_direction coef */
258 b = -2.0/(dx*dx)-2.0/(dy*dy) - alpha; /* Central coeff */
259
260 error = 10.0 * tol;
261 k = 1;
262
263#pragma omp parallel private(resid, i)
264 {
265
266 while (k <= maxit && error > tol) {
267
268 /* copy new solution into old */
269#pragma omp for
270 for (j=0; j<m; j++)
271 for (i=0; i<n; i++)
272 uold[i + m*j] = u[i + m*j];
273
274 /* compute stencil, residual and update */
275#pragma omp for reduction(+:error)
276 for (i=1; i<n-1; i++){
277 resid =(
278 ax * (uold[i-1 + m*j] + uold[i+1 + m*j])
279 + ay * (uold[i + m*(j-1)] + uold[i + m*(j+1)])
280 + b * uold[i + m*j] - f[i + m*j]
281 ) / b;
282
283 /* update solution */
284 u[i + m*j] = uold[i + m*j] - omega * resid;
285
286 /* accumulate residual error */
287 error =error + resid*resid;
288
289
290 } /* end for */
291
292 /* error check */
293#pragma omp master
294 {
295 k++;
296 error = sqrt(error) /(n*m);
297 }
298
299 } /* while */
300
301
302 } /* end parallel */
303
304 printf("Total Number of Iteratuons %d\n", k);
305 printf("Residual %.15f\n", error);
306
307 free(uold);
308}
309
Note: See TracBrowser for help on using the repository browser.