Commit d16e53ac39a72d169d9b7c204be121caa81009aa

Authored by kmazouzi
1 parent ead55f0407
Exists in master

add program arguments

Showing 2 changed files with 107 additions and 90 deletions Side-by-side Diff

lab3/omp_heat2D.c View file @ d16e53a
... ... @@ -28,13 +28,13 @@
28 28 #include <math.h>
29 29 #include <omp.h>
30 30  
31   -#define N 500
32   -#define M 500
  31 +#define NN 50
  32 +#define MM 50
33 33  
34 34 #define ITER_PRINT 100
35 35 #define PRINT_DATA 1
36 36  
37   -#define EPSILON 1e-1
  37 +#define _EPSILON 0.001
38 38  
39 39  
40 40 void update(int nx,int ny, float *u, float *unew, float * diff);
41 41  
42 42  
43 43  
44 44  
45 45  
... ... @@ -48,32 +48,44 @@
48 48 {
49 49  
50 50 float diff=1.0;
  51 + float EPSILON=_EPSILON;
  52 + int N=NN,M=MM;
51 53  
  54 + if(argc !=3)
  55 + {
  56 + fprintf(stderr,"usage %s N EPSILON\n ", argv[0]);
  57 + fprintf(stderr,"\t\twhere N is GRID size, EPSILON is Tolerance\n");
  58 + fprintf(stderr,"\t\texample N = 100, EPSILON = 0.1\n");
  59 + return -1;
  60 + }
  61 +
  62 + N = M = atoi(argv[1]);
  63 + EPSILON = atof(argv[2]);
  64 +
52 65 float *u = (float *)malloc(N * M * sizeof(float));
53 66 float *unew = (float *)malloc(N * M * sizeof(float));
54   -
  67 +
55 68 if(u==0 || unew ==0)
56 69 {
57 70 perror("Can't allocated data\n");
58 71 return -1;
59 72 }
60 73  
61   - printf ( "\n" );
62   - printf ( "HEATED_PLATE\n" );
63   - printf ( " Parallel OpenMP version, using %d Threads\n",omp_get_max_threads() );
64   - printf ( " A program to solve for the steady state temperature distribution\n" );
65   - printf ( " over a rectangular plate.\n" );
66   - printf ( " Spatial grid of %d by %d points.\n\n", M, N );
  74 + printf ( "\n" );
  75 + printf ( "HEATED_PLATE\n" );
  76 + printf ( " Parallel OpenMP version, using %d Threads\n",omp_get_max_threads() );
  77 + printf ( " A program to solve for the steady state temperature distribution\n" );
  78 + printf ( " over a rectangular plate.\n" );
  79 + printf ( " Spatial grid of %d by %d points.\n\n", M, N );
67 80  
68 81  
69 82 /* Initialize grid and create input file */
70 83 printf("Initializing grid\n");
71   -
  84 +
72 85 inidat(N, M,u,unew);
73 86  
74 87 prtdat(N, M,u, "initial.dat");
75   -
76   -
  88 +
77 89 printf("Start computing\n");
78 90  
79 91 int iter=0;
80 92  
81 93  
82 94  
... ... @@ -82,19 +94,19 @@
82 94 * iterate until the new solution unew differs from the old solution u
83 95 * by no more than EPSILON.
84 96 * */
85   -
  97 +
86 98 while(diff> EPSILON) {
87 99  
88 100 update(N, M, u, unew,&diff);
89   -
  101 +
90 102 if(iter%ITER_PRINT==0)
91   - printf("Iteration %d, diff = %f\n ", iter,diff);
  103 + printf("Iteration %d, diff = %f\n ", iter,diff);
92 104  
93 105 iter++;
94 106 }
95 107  
96 108 prtdat(N, M, u, "final.dat");
97   -
  109 +
98 110 free(u);
99 111 free(unew);
100 112 }
101 113  
102 114  
103 115  
104 116  
105 117  
106 118  
107 119  
108 120  
... ... @@ -115,49 +127,45 @@
115 127 unew[ix*ny+iy] =
116 128 (u[(ix+1)*ny+iy] + u[(ix-1)*ny+iy] +
117 129 u[ix*ny+iy+1] + u[ix*ny+iy-1] )/4.0;
118   -
  130 +
119 131 }
120 132 }
121 133  
122   -//compute reduction
  134 + //compute reduction
123 135  
124 136  
125   - float mydiff;
126   -
  137 + float mydiff;
  138 +
127 139 #pragma omp parallel shared(nx,ny,u,unew, diff) private (ix,iy,mydiff)
128   - {
129   -mydiff=0.0;
  140 + {
  141 + mydiff=0.0;
130 142 #pragma omp for
131   - for (ix = 1; ix < nx-1; ix++) {
132   - for (iy = 1; iy < ny-1; iy++) {
133   - if (mydiff < fabs (unew[ix*ny+iy] - u[ix*ny+iy] ))
134   - {
135   - mydiff = fabs ( unew[ix*ny+iy] - u[ix*ny+iy] );
  143 + for (ix = 1; ix < nx-1; ix++) {
  144 + for (iy = 1; iy < ny-1; iy++) {
  145 + if (mydiff < fabs (unew[ix*ny+iy] - u[ix*ny+iy] ))
  146 + {
  147 + mydiff = fabs ( unew[ix*ny+iy] - u[ix*ny+iy] );
  148 + }
136 149 }
137 150 }
138   - }
139 151  
140 152  
141 153 # pragma omp critical
142   - {
143   - if (*diff < mydiff )
144   - {
145   - *diff = mydiff;
146   - }
147   - }
148   -
149   -
150   -#pragma omp for
151   - for (ix = 1; ix < nx-1; ix++) {
152   - for (iy = 1; iy < ny-1; iy++) {
153   - u[ix*ny+iy] = unew[ix*ny+iy];
  154 + {
  155 + if (*diff < mydiff )
  156 + {
  157 + *diff = mydiff;
  158 + }
154 159 }
155   - }
156 160  
157 161  
158   -
159   -
160   - }
  162 +#pragma omp for
  163 + for (ix = 1; ix < nx-1; ix++) {
  164 + for (iy = 1; iy < ny-1; iy++) {
  165 + u[ix*ny+iy] = unew[ix*ny+iy];
  166 + }
  167 + }
  168 + }
161 169 }
162 170  
163 171 /*****************************************************************************
164 172  
165 173  
... ... @@ -178,22 +186,22 @@
178 186 u[ix*ny+iy]=0.0;
179 187 }
180 188 else
181   - if(iy==0 && ix!=0)
182   - {
183   - u[ix*ny+iy]=100.0;
184   - }else
185   -
186   - if(ix==nx-1)
187   - {
188   - u[ix*ny+iy]=100.0;
189   - }else
  189 + if(iy==0 && ix!=0)
  190 + {
  191 + u[ix*ny+iy]=100.0;
  192 + }else
190 193  
191   - if(iy==ny-1 && ix!=0)
192   - {
193   - u[ix*ny+iy]=100.0;
194   - }else
  194 + if(ix==nx-1)
  195 + {
  196 + u[ix*ny+iy]=100.0;
  197 + }else
195 198  
196   - u[ix*ny+iy]=( float ) ( 2 * nx + 2 * ny - 4 );
  199 + if(iy==ny-1 && ix!=0)
  200 + {
  201 + u[ix*ny+iy]=100.0;
  202 + }else
  203 +
  204 + u[ix*ny+iy]=0.0;
197 205 }
198 206 }
199 207  
200 208  
... ... @@ -202,12 +210,12 @@
202 210 **************************************************************************/
203 211 void prtdat(int nx, int ny, float *u,const char *fnam)
204 212 {
205   -
  213 +
206 214 int ix, iy;
207 215 FILE *fp;
208 216  
209 217 if(ITER_PRINT==0)return;
210   -
  218 +
211 219 fp = fopen(fnam, "w");
212 220  
213 221 for (ix = 0 ; ix < nx; ix++) {
lab3/ser_heat2D.c View file @ d16e53a
... ... @@ -27,13 +27,13 @@
27 27 #include <stdlib.h>
28 28 #include <math.h>
29 29  
30   -#define N 200
31   -#define M 200
  30 +#define NN 50
  31 +#define MM 50
32 32  
33 33 #define ITER_PRINT 100
34 34 #define PRINT_DATA 1
35 35  
36   -#define EPSILON 1e-1
  36 +#define _EPSILON 0.01
37 37  
38 38  
39 39 void update(int nx,int ny, float *u, float *unew, float * diff);
40 40  
... ... @@ -45,7 +45,21 @@
45 45  
46 46 int main(int argc, char *argv[])
47 47 {
  48 + int N=NN,M=MM;
48 49  
  50 + float EPSILON=_EPSILON;
  51 +
  52 + if(argc !=3)
  53 + {
  54 + fprintf(stderr,"usage %s N EPSILON\n ", argv[0]);
  55 + fprintf(stderr,"\t\twhere N is GRID size, EPSILON is Tolerance\n");
  56 + fprintf(stderr,"\t\texample N = 100, EPSILON = 0.1\n");
  57 + return -1;
  58 + }
  59 +
  60 + N = M = atoi(argv[1]);
  61 + EPSILON = atof(argv[2]);
  62 +
49 63 float diff=1.0;
50 64  
51 65 float *u = (float *)malloc(N * M * sizeof(float));
52 66  
53 67  
54 68  
... ... @@ -63,18 +77,17 @@
63 77 printf ( " A program to solve for the steady state temperature distribution\n" );
64 78 printf ( " over a rectangular plate.\n" );
65 79 printf ( "\n" );
66   - printf ( " Spatial grid of %d by %d points.\n\n", M, N );
  80 + printf ( " Spatial grid of %d by %d points.\n", M, N );
  81 + printf ( " The iteration will end until tolerance <= %f\n\n",EPSILON);
67 82  
68   -
69 83 /* Initialize grid and create input file */
70 84 printf("Initializing grid\n");
71 85  
72 86 inidat(N, M,u,unew);
73 87  
74 88 prtdat(N, M,u, "initial.dat");
75   -
76 89  
77   - printf("Start computing\n");
  90 + printf("Start computing\n\n");
78 91  
79 92 int iter=0;
80 93  
... ... @@ -88,7 +101,8 @@
88 101 update(N, M, u, unew,&diff);
89 102  
90 103 if(iter%ITER_PRINT==0)
91   - printf("Iteration %d, diff = %f\n ", iter,diff);
  104 +
  105 + printf("Iteration %d, diff = %f\n ", iter,diff);
92 106  
93 107 iter++;
94 108 }
... ... @@ -111,10 +125,8 @@
111 125  
112 126 for (ix = 1; ix < nx-1; ix++) {
113 127 for (iy = 1; iy < ny-1; iy++) {
114   - unew[ix*ny+iy] =
115   - (u[(ix+1)*ny+iy] + u[(ix-1)*ny+iy] +
116   - u[ix*ny+iy+1] + u[ix*ny+iy-1] )/4.0;
117   -
  128 + unew[ix*ny+iy] = (u[(ix+1)*ny+iy] + u[(ix-1)*ny+iy] + u[ix*ny+iy+1] + u[ix*ny+iy-1] )/4.0
  129 + ;
118 130 if (*diff < fabs (unew[ix*ny+iy] - u[ix*ny+iy] ))
119 131 {
120 132 *diff = fabs ( unew[ix*ny+iy] - u[ix*ny+iy] );
121 133  
122 134  
123 135  
124 136  
... ... @@ -152,27 +164,27 @@
152 164 else
153 165 if(iy==0 && ix!=0)
154 166 {
155   - u[ix*ny+iy]=100.0;
  167 + u[ix*ny+iy]=100.0;
156 168 }else
157 169  
158 170 if(ix==nx-1)
159 171 {
160   - u[ix*ny+iy]=100.0;
  172 + u[ix*ny+iy]=100.0;
161 173 }else
162 174  
163 175 if(iy==ny-1 && ix!=0)
164 176 {
165   - u[ix*ny+iy]=100.0;
  177 + u[ix*ny+iy]=100.0;
166 178 }else
167 179  
168   - u[ix*ny+iy]=( float ) ( 2 * nx + 2 * ny - 4 );
  180 + u[ix*ny+iy]=0.0;
169 181 }
170 182 }
171 183  
172 184 /**************************************************************************
173 185 * Print Data to files
174 186 **************************************************************************/
175   -void prtdat(int nx, int ny, float *u,const char *fnam)
  187 +void prtdat(int nx, int ny, float *u,const char *fname)
176 188 {
177 189  
178 190 int ix, iy;
179 191  
180 192  
181 193  
182 194  
... ... @@ -180,23 +192,20 @@
180 192  
181 193 if(ITER_PRINT==0)return;
182 194  
183   - fp = fopen(fnam, "w");
  195 + fp = fopen(fname, "w");
184 196  
  197 + // fprintf ( fp, "%d\n", M );
  198 + // fprintf ( fp, "%d\n", N );
  199 +
185 200 for (ix = 0 ; ix < nx; ix++) {
186 201 for (iy =0; iy < ny; iy++) {
187 202  
188   - fprintf(fp, "%8.3f", u[ix*ny+iy]);
189   -
190   - if(iy!=ny-1)
191   - {
192   - fprintf(fp, " ");
193   - }else
194   - {
195   - fprintf(fp, "\n");
196   - }
  203 + fprintf(fp, "%6.2f ", u[ix*ny+iy]);
197 204 }
  205 + fputc ( '\n', fp);
198 206 }
199 207  
  208 + printf (" Data written to the output file %s\n", fname);
200 209 fclose(fp);
201 210 }