-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot_omp.c
executable file
·278 lines (219 loc) · 5.6 KB
/
mandelbrot_omp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# include <stdlib.h>
# include <stdio.h>
# include <math.h>
# include <time.h>
# include <omp.h>
int main ( void );
int i4_min ( int i1, int i2 );
void timestamp ( void );
/******************************************************************************/
int main ( void )
/******************************************************************************/
/*
Purpose
MAIN is the main program for MANDELBROT_OPENMP.
Discussion:
MANDELBROT_OPENMP computes an image of the Mandelbrot set.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
03 September 2012
Author:
John Burkardt
Local Parameters:
Local, int COUNT_MAX, the maximum number of iterations taken
for a particular pixel.
*/
{
int m = 500;
int n = 500;
int b[m][n];
int c;
int c_max;
int count[m][n];
int count_max = 2000;
int g[m][n];
int i;
int ierror;
int j;
int jhi;
int jlo;
int k;
char *output_filename = "mandelbrot.ppm";
FILE *output_unit;
int r[m][n];
double wtime;
double wtime_total;
double x_max = 1.25;
double x_min = - 2.25;
double x;
double x1;
double x2;
double y_max = 1.75;
double y_min = - 1.75;
double y;
double y1;
double y2;
timestamp ( );
printf ( "\n" );
printf ( "MANDELBROT_OPENMP\n" );
printf ( " C/OpenMP version\n" );
printf ( "\n" );
printf ( " Create an ASCII PPM image of the Mandelbrot set.\n" );
printf ( "\n" );
printf ( " For each point C = X + i*Y\n" );
printf ( " with X range [%g,%g]\n", x_min, x_max );
printf ( " and Y range [%g,%g]\n", y_min, y_max );
printf ( " carry out %d iterations of the map\n", count_max );
printf ( " Z(n+1) = Z(n)^2 + C.\n" );
printf ( " If the iterates stay bounded (norm less than 2)\n" );
printf ( " then C is taken to be a member of the set.\n" );
printf ( "\n" );
printf ( " An ASCII PPM image of the set is created using\n" );
printf ( " M = %d pixels in the X direction and\n", m );
printf ( " N = %d pixels in the Y direction.\n", n );
wtime = omp_get_wtime ( );
/*
Carry out the iteration for each pixel, determining COUNT.
*/
# pragma omp parallel \
shared ( b, count, count_max, g, r, x_max, x_min, y_max, y_min ) \
private ( i, j, k, x, x1, x2, y, y1, y2 )
{
# pragma omp
// #pragma omp parallel for schedule(static,8)
// #pragma omp parallel for schedule(dynamic,8)
// #pragma omp parallel for schedule(guided,8)
for ( i = 0; i < m; i++ )
{
for ( j = 0; j < n; j++ )
{
x = ( ( double ) ( j - 1 ) * x_max
+ ( double ) ( m - j ) * x_min )
/ ( double ) ( m - 1 );
y = ( ( double ) ( i - 1 ) * y_max
+ ( double ) ( n - i ) * y_min )
/ ( double ) ( n - 1 );
count[i][j] = 0;
x1 = x;
y1 = y;
for ( k = 1; k <= count_max; k++ )
{
x2 = x1 * x1 - y1 * y1 + x;
y2 = 2 * x1 * y1 + y;
if ( x2 < -2.0 || 2.0 < x2 || y2 < -2.0 || 2.0 < y2 )
{
count[i][j] = k;
break;
}
x1 = x2;
y1 = y2;
}
if ( ( count[i][j] % 2 ) == 1 )
{
r[i][j] = 255;
g[i][j] = 255;
b[i][j] = 255;
}
else
{
c = ( int ) ( 255.0 * sqrt ( sqrt ( sqrt (
( ( double ) ( count[i][j] ) / ( double ) ( count_max ) ) ) ) ) );
r[i][j] = 3 * c / 5;
g[i][j] = 3 * c / 5;
b[i][j] = c;
}
}
}
}
wtime = omp_get_wtime ( ) - wtime;
printf ( "\n" );
printf ( " Time = %g seconds.\n", wtime );
/*
Write data to an ASCII PPM file.
*/
output_unit = fopen ( output_filename, "wt" );
fprintf ( output_unit, "P3\n" );
fprintf ( output_unit, "%d %d\n", n, m );
fprintf ( output_unit, "%d\n", 255 );
for ( i = 0; i < m; i++ )
{
for ( jlo = 0; jlo < n; jlo = jlo + 4 )
{
jhi = i4_min ( jlo + 4, n );
for ( j = jlo; j < jhi; j++ )
{
fprintf ( output_unit, " %d %d %d", r[i][j], g[i][j], b[i][j] );
}
fprintf ( output_unit, "\n" );
}
}
fclose ( output_unit );
printf ( "\n" );
printf ( " Graphics data written to \"%s\".\n", output_filename );
/*
Terminate.
*/
printf ( "\n" );
printf ( "MANDELBROT_OPENMP\n" );
printf ( " Normal end of execution.\n" );
printf ( "\n" );
timestamp ( );
return 0;
}
/******************************************************************************/
int i4_min ( int i1, int i2 )
/******************************************************************************/
/*
Purpose:
I4_MIN returns the smaller of two I4's.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
29 August 2006
Author:
John Burkardt
Parameters:
Input, int I1, I2, two integers to be compared.
Output, int I4_MIN, the smaller of I1 and I2.
*/
{
int value;
if ( i1 < i2 )
{
value = i1;
}
else
{
value = i2;
}
return value;
}
/******************************************************************************/
void timestamp ( void )
/******************************************************************************/
/*
Purpose:
TIMESTAMP prints the current YMDHMS date as a time stamp.
Example:
31 May 2001 09:45:54 AM
Modified:
24 September 2003
Author:
John Burkardt
Parameters:
None
*/
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct tm *tm;
size_t len;
time_t now;
now = time ( NULL );
tm = localtime ( &now );
len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
printf ( "%s\n", time_buffer );
return;
# undef TIME_SIZE
}