Blame view

lab3/omp_heat2D.c 5.59 KB
1e6ef8e72   kmazouzi   Steady state heat
1
2
  /****************************************************************************
   * DESCRIPTION:  
a67a1dd2b   kmazouzi   rewrite error wit...
3
   *   OpenMP HEAT2D Example - C Version
1e6ef8e72   kmazouzi   Steady state heat
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
   *   This example is based on a simplified 
   *   two-dimensional heat equation domain decomposition.  The initial 
   *   temperature is computed to be high in the middle of the domain and 
   *   zero at the boundaries.  The boundaries are held at zero throughout 
   *   the simulation.  During the time-stepping, an array containing two 
   *   domains is used; these domains alternate between old data and new data.
   *
   *  The physical region, and the boundary conditions, are suggested
      by this diagram;
  
                     u = 0
               +------------------+
               |                  |
      u = 100  |                  | u = 100
               |                  |
               +------------------+
                     u = 100
  
  Interrior point :
    u[Central] = (1/4) * ( u[North] + u[South] + u[East] + u[West] )
  
   ****************************************************************************/
a67a1dd2b   kmazouzi   rewrite error wit...
26
27
  
  #include <stdio.h> 
1e6ef8e72   kmazouzi   Steady state heat
28
29
  #include <stdlib.h>
  #include <math.h>
1e6ef8e72   kmazouzi   Steady state heat
30

d16e53ac3   kmazouzi   add program argum...
31
32
  #define NN 50
  #define MM 50  
1e6ef8e72   kmazouzi   Steady state heat
33
34
  
  #define ITER_PRINT 100
a67a1dd2b   kmazouzi   rewrite error wit...
35
  #define _MAX_ITER 400 
1e6ef8e72   kmazouzi   Steady state heat
36
  #define PRINT_DATA 1
a67a1dd2b   kmazouzi   rewrite error wit...
37
  #define _EPSILON 0.01
1e6ef8e72   kmazouzi   Steady state heat
38

3604dfae0   kmazouzi   MPI version
39
  float update(int nx,int ny, float *u, float *unew);
1e6ef8e72   kmazouzi   Steady state heat
40
41
42
43
44
45
46
47
  void inidat(int nx, int ny, float *u, float *unew); 
  void prtdat(int nx, int ny, float *u,const char *fnam);
  
  
  
  
  int main(int argc, char *argv[])
  {
d16e53ac3   kmazouzi   add program argum...
48
      int N=NN,M=MM;
a67a1dd2b   kmazouzi   rewrite error wit...
49
50
      float EPSILON=_EPSILON;
      int MAX_ITER= _MAX_ITER;
d16e53ac3   kmazouzi   add program argum...
51

a67a1dd2b   kmazouzi   rewrite error wit...
52
      if(argc !=4)
d16e53ac3   kmazouzi   add program argum...
53
      {
a67a1dd2b   kmazouzi   rewrite error wit...
54
55
56
57
58
59
          fprintf(stderr,"usage %s N EPSILON MAX_ITER
   ", argv[0]);
          fprintf(stderr,"\t\twhere N is GRID size, EPSILON is  Tolerance, MAX_ITER is max iteration
  "); 
          fprintf(stderr,"\t\texample N = 100, EPSILON = 0.1, MAX_ITER=1000
  "); 
d16e53ac3   kmazouzi   add program argum...
60
61
62
63
64
          return -1;
      } 
  
      N = M = atoi(argv[1]);
      EPSILON = atof(argv[2]);
a67a1dd2b   kmazouzi   rewrite error wit...
65
66
67
      MAX_ITER = atoi (argv[3]);
  
      float diff=1.0;
1e6ef8e72   kmazouzi   Steady state heat
68
69
70
  
      float *u     = (float *)malloc(N * M * sizeof(float));
      float *unew  = (float *)malloc(N * M * sizeof(float));
d16e53ac3   kmazouzi   add program argum...
71

1e6ef8e72   kmazouzi   Steady state heat
72
73
74
75
76
77
      if(u==0 || unew ==0)
      {
          perror("Can't allocated data
  ");
          return -1;
      }
d16e53ac3   kmazouzi   add program argum...
78
79
80
81
      printf ( "
  " );
      printf ( "HEATED_PLATE
  " );
a67a1dd2b   kmazouzi   rewrite error wit...
82
83
      printf ( "  OpenMP version
  " );
d16e53ac3   kmazouzi   add program argum...
84
85
86
87
      printf ( "  A program to solve for the steady state temperature distribution
  " );
      printf ( "  over a rectangular plate.
  " );
a67a1dd2b   kmazouzi   rewrite error wit...
88
89
90
91
92
93
94
      printf ( "
  " );
      printf ( "  Spatial grid of %d by %d points.
  ", M, N );
      printf ( "  The iteration will end until tolerance <= %f
  
  ",EPSILON);
1e6ef8e72   kmazouzi   Steady state heat
95
96
97
98
  
      /* Initialize grid and create input file */
      printf("Initializing grid
  ");
d16e53ac3   kmazouzi   add program argum...
99

1e6ef8e72   kmazouzi   Steady state heat
100
101
102
      inidat(N, M,u,unew);
  
      prtdat(N, M,u, "initial.dat");
d16e53ac3   kmazouzi   add program argum...
103

a67a1dd2b   kmazouzi   rewrite error wit...
104
105
106
      printf("Start computing
  
  ");
1e6ef8e72   kmazouzi   Steady state heat
107
108
109
110
111
112
113
  
      int iter=0;
  
      /* 
       *   iterate until the  new solution unew differs from the old solution u
       *     by no more than EPSILON.
       *     */
d16e53ac3   kmazouzi   add program argum...
114

a67a1dd2b   kmazouzi   rewrite error wit...
115
      while(diff> EPSILON && iter <MAX_ITER) {
1e6ef8e72   kmazouzi   Steady state heat
116

a67a1dd2b   kmazouzi   rewrite error wit...
117
          diff= update(N, M, u, unew);
d16e53ac3   kmazouzi   add program argum...
118

1e6ef8e72   kmazouzi   Steady state heat
119
          if(iter%ITER_PRINT==0)
a67a1dd2b   kmazouzi   rewrite error wit...
120
121
122
  
              printf("Iteration %d, diff = %e
   ", iter,diff);
1e6ef8e72   kmazouzi   Steady state heat
123
124
125
  
          iter++;
      }
a67a1dd2b   kmazouzi   rewrite error wit...
126
127
128
      if(diff>EPSILON)
         printf("***Not converged MAX_ITERATION %d reached
  ***", MAX_ITER);
1e6ef8e72   kmazouzi   Steady state heat
129
      prtdat(N, M, u, "final.dat");
d16e53ac3   kmazouzi   add program argum...
130

1e6ef8e72   kmazouzi   Steady state heat
131
132
133
134
135
136
137
138
139
      free(u);
      free(unew);
  }
  
  
  
  /****************************************************************************
   *  subroutine update
   ****************************************************************************/
3604dfae0   kmazouzi   MPI version
140
  float update(int nx,int ny, float *u, float *unew)
1e6ef8e72   kmazouzi   Steady state heat
141
142
  {
      int ix, iy;
3604dfae0   kmazouzi   MPI version
143
      float diff=0.0;
1e6ef8e72   kmazouzi   Steady state heat
144

a67a1dd2b   kmazouzi   rewrite error wit...
145
  #pragma omp parallel for shared(nx,ny,u,unew) private (ix,iy) reduction(max:diff)
1e6ef8e72   kmazouzi   Steady state heat
146
147
148
149
      for (ix = 1; ix < nx-1; ix++) {
          for (iy = 1; iy < ny-1; iy++) {
              unew[ix*ny+iy] =  
                  (u[(ix+1)*ny+iy] +  u[(ix-1)*ny+iy] +
a67a1dd2b   kmazouzi   rewrite error wit...
150
151
                   u[ix*ny+iy+1] +  u[ix*ny+iy-1] )*0.25;
   diff = fmax( diff, fabs(unew[ix*ny+iy] - u[ix*ny+iy]));
1e6ef8e72   kmazouzi   Steady state heat
152
153
          }
      }
a67a1dd2b   kmazouzi   rewrite error wit...
154
     
1e6ef8e72   kmazouzi   Steady state heat
155

3604dfae0   kmazouzi   MPI version
156
157
158
  /*
   * COPY OLD DATA
   */
a67a1dd2b   kmazouzi   rewrite error wit...
159
  #pragma omp parallel for
d16e53ac3   kmazouzi   add program argum...
160
161
162
163
164
          for (ix = 1; ix < nx-1; ix++) {
              for (iy = 1; iy < ny-1; iy++) {
                  u[ix*ny+iy] = unew[ix*ny+iy]; 
              }
          }   
a67a1dd2b   kmazouzi   rewrite error wit...
165
      
3604dfae0   kmazouzi   MPI version
166
167
  
      return diff;
1e6ef8e72   kmazouzi   Steady state heat
168
169
170
171
172
  }
  
  /*****************************************************************************
   *  Initialize Data
   *****************************************************************************/
3604dfae0   kmazouzi   MPI version
173

1e6ef8e72   kmazouzi   Steady state heat
174
175
176
177
178
179
180
  void inidat(int nx, int ny, float *u, float *unew) 
  {
      int ix, iy;
  
      /*
       *Set boundary data and interrior values
       * */
1e6ef8e72   kmazouzi   Steady state heat
181

3604dfae0   kmazouzi   MPI version
182
183
184
185
186
187
  #pragma omp parallel private (ix,iy)
      {
      // interior points
      #pragma omp for    
      for (ix = 1; ix < nx-1; ix++) 
          for (iy = 1; iy < ny-1; iy++) { 
a67a1dd2b   kmazouzi   rewrite error wit...
188
              u[ix*ny+iy]=0.0; 
3604dfae0   kmazouzi   MPI version
189
190
191
192
193
194
          }
  
     //boundary left
      #pragma omp for    
      for (ix = 1; ix < nx-1; ix++){ 
          u[ix*ny]=100.0; 
d16e53ac3   kmazouzi   add program argum...
195

3604dfae0   kmazouzi   MPI version
196
      }
d16e53ac3   kmazouzi   add program argum...
197

3604dfae0   kmazouzi   MPI version
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
      //boundary right
      #pragma omp for    
      for (ix = 1; ix < nx-1; ix++){ 
          u[ix*ny+ (ny-1)]=100.0; 
  
      }
  
      //boundary down
      #pragma omp for    
      for (iy = 0; iy < ny; iy++){ 
          u[(nx-1)*(ny)+iy]=100.0; 
  
      }
  
      //boundary top
      #pragma omp for 
      for (iy = 0; iy < ny; iy++){ 
a67a1dd2b   kmazouzi   rewrite error wit...
215
          u[iy]=100.0; 
3604dfae0   kmazouzi   MPI version
216
217
218
219
  
      }
     
      }
d16e53ac3   kmazouzi   add program argum...
220

1e6ef8e72   kmazouzi   Steady state heat
221
  }
3604dfae0   kmazouzi   MPI version
222

1e6ef8e72   kmazouzi   Steady state heat
223
224
225
226
227
  /**************************************************************************
   * Print Data to files
   **************************************************************************/
  void prtdat(int nx, int ny, float *u,const char *fnam)
  {
d16e53ac3   kmazouzi   add program argum...
228

1e6ef8e72   kmazouzi   Steady state heat
229
230
231
232
      int ix, iy;
      FILE *fp;
  
      if(ITER_PRINT==0)return;
d16e53ac3   kmazouzi   add program argum...
233

1e6ef8e72   kmazouzi   Steady state heat
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
      fp = fopen(fnam, "w");
  
      for (ix = 0 ; ix < nx; ix++) {
          for (iy =0; iy < ny; iy++) {
  
              fprintf(fp, "%8.3f", u[ix*ny+iy]);
  
              if(iy!=ny-1)
              {
                  fprintf(fp, " ");
              }else
              {
                  fprintf(fp, "
  ");
              }
          }
      }
  
      fclose(fp);
  }