Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. /*
  2. c program:
  3. --------------------------------
  4. 1. draws Mandelbrot set for Fc(z)=z*z +c
  5. using Mandelbrot algorithm ( boolean escape time )
  6. -------------------------------
  7. 2. technique of creating ppm file is based on the code of Claudio Rocchini
  8. http://en.wikipedia.org/wiki/Image:Color_complex_plot.jpg
  9. create 24 bit color graphic file , portable pixmap file = PPM
  10. see http://en.wikipedia.org/wiki/Portable_pixmap
  11. to see the file use external application ( graphic viewer)
  12. */
  13. #include <stdio.h>
  14. #include <math.h>
  15. #include <omp.h>
  16. #include <time.h>
  17.  
  18. int iX, iY;
  19. #define iXmax 4000
  20. #define iYmax 4000
  21. #define ThreadsNumber 8
  22.  
  23. int buffor[iXmax][iYmax][3];
  24. int main()
  25. {
  26. double s1, s2;
  27. /* screen ( integer) coordinate */
  28.  
  29. /* world ( double) coordinate = parameter plane*/
  30. double Cx, Cy;
  31. const double CxMin = -2.5;
  32. const double CxMax = 1.5;
  33. const double CyMin = -2.0;
  34. const double CyMax = 2.0;
  35. /* */
  36. double PixelWidth = (CxMax - CxMin) / iXmax;
  37. double PixelHeight = (CyMax - CyMin) / iYmax;
  38. /* color component ( R or G or B) is coded from 0 to 255 */
  39. /* it is 24 bit color RGB file */
  40. const int MaxColorComponentValue = 255;
  41. FILE *fp;
  42. char *filename = "new1.ppm";
  43. char *comment = "# "; /* comment should start with # */
  44. static unsigned char color[3];
  45. /* Z=Zx+Zy*i ; Z0 = 0 */
  46. double Zx, Zy;
  47. double Zx2, Zy2; /* Zx2=Zx*Zx; Zy2=Zy*Zy */
  48. /* */
  49. int Iteration;
  50. const int IterationMax = 200;
  51. /* bail-out value , radius of circle ; */
  52. const double EscapeRadius = 2;
  53. double ER2 = EscapeRadius * EscapeRadius;
  54. /*create new file,give it a name and open it in binary mode */
  55. fp = fopen(filename, "wb"); /* b - binary mode */
  56. /*write ASCII header to the file*/
  57. fprintf(fp, "P6\n %s\n %d\n %d\n %d\n", comment, iXmax, iYmax, MaxColorComponentValue);
  58. /* compute and write image data bytes to the file*/
  59.  
  60.  
  61.  
  62. // --------------------------------------- SEQUENCE EXECUTION
  63. clock_t start, stop;
  64. double time;
  65. start = clock();
  66. for (iY = 0; iY < iYmax; iY++)
  67. {
  68. Cy = CyMin + iY * PixelHeight;
  69. if (fabs(Cy) < PixelHeight / 2)
  70. Cy = 0.0; /* Main antenna */
  71. for (iX = 0; iX < iXmax; iX++)
  72. {
  73. Cx = CxMin + iX * PixelWidth;
  74. /* initial value of orbit = critical point Z= 0 */
  75. Zx = 0.0;
  76. Zy = 0.0;
  77. Zx2 = Zx * Zx;
  78. Zy2 = Zy * Zy;
  79. /* */
  80. for (Iteration = 0; Iteration < IterationMax && ((Zx2 + Zy2) < ER2); Iteration++)
  81. {
  82. Zy = 2 * Zx * Zy + Cy;
  83. Zx = Zx2 - Zy2 + Cx;
  84. Zx2 = Zx * Zx;
  85. Zy2 = Zy * Zy;
  86. };
  87. /* compute pixel color (24 bit = 3 bytes) */
  88. if (Iteration == IterationMax)
  89. { /* interior of Mandelbrot set = black */
  90. color[0] = 0;
  91. color[1] = 0;
  92. color[2] = 0;
  93. }
  94. else
  95. { /* exterior of Mandelbrot set = white */
  96. color[0] = 255; /* Red*/
  97. color[1] = 255; /* Green */
  98. color[2] = 255; /* Blue */
  99. };
  100. /*write color to the file*/
  101. buffor[iX][iY][0] = color[0];
  102. buffor[iX][iY][1] = color[1];
  103. buffor[iX][iY][2] = color[2];
  104. }
  105. }
  106. stop = clock();
  107. time = (double)(((stop - start) / (double)CLOCKS_PER_SEC) * 1000);
  108. printf("timedif_0, = %.4f ms\n", time);
  109. // --------------------------------------- SEQUENCE EXECUTION
  110.  
  111. // --------------------------------------- PARALLEL EXECUTION
  112. int id, sum[ThreadsNumber];
  113. for(int i =0; i < ThreadsNumber; i++)
  114. {
  115. sum[i] = 0;
  116. }
  117. s1 = omp_get_wtime();
  118. #pragma omp parallel private(id) num_threads(ThreadsNumber)
  119. {
  120. id = omp_get_thread_num();
  121. #pragma omp for private(color, iY, iX, Iteration, Cy, Cx, Zx, Zy, Zx2, Zy2) schedule(static, 10)
  122. for (iY = 0; iY < iYmax; iY++)
  123. {
  124. Cy = CyMin + iY * PixelHeight;
  125. if (fabs(Cy) < PixelHeight / 2)
  126. Cy = 0.0; /* Main antenna */
  127. for (iX = 0; iX < iXmax; iX++)
  128. {
  129. Cx = CxMin + iX * PixelWidth;
  130. /* initial value of orbit = critical point Z= 0 */
  131. Zx = 0.0;
  132. Zy = 0.0;
  133. Zx2 = Zx * Zx;
  134. Zy2 = Zy * Zy;
  135. /* */
  136. for (Iteration = 0; Iteration < IterationMax && ((Zx2 + Zy2) < ER2); Iteration++)
  137. {
  138. Zy = 2 * Zx * Zy + Cy;
  139. Zx = Zx2 - Zy2 + Cx;
  140. Zx2 = Zx * Zx;
  141. Zy2 = Zy * Zy;
  142. };
  143. sum[id] += Iteration;
  144. /* compute pixel color (24 bit = 3 bytes) */
  145. if (Iteration == IterationMax)
  146. { /* interior of Mandelbrot set = black */
  147. color[0] = 0;
  148. color[1] = 0;
  149. color[2] = 0;
  150. }
  151. else
  152. { /* exterior of Mandelbrot set = white */
  153. color[0] = (id+3 * 30) % 255; /* Red*/
  154. color[1] = (id+3 * 30) % 255; /* Green */
  155. color[2] = (id+3 * 30) % 255; /* Blue */
  156. };
  157. /*write color to the file*/
  158. buffor[iX][iY][0] = color[0];
  159. buffor[iX][iY][1] = color[1];
  160. buffor[iX][iY][2] = color[2];
  161. }
  162. sum[id] += Iteration;
  163. }
  164.  
  165. }
  166. s2 = omp_get_wtime();
  167.  
  168.  
  169. for (int i = 0; i < ThreadsNumber; i++)
  170. printf("Thread %d : %d \n", i, sum[i]);
  171.  
  172. printf("timedif_1, = %.4f \n", (s2 - s1) * 1000);
  173.  
  174.  
  175. // --------------------------------------- PARALLEL EXECUTION
  176.  
  177. for (int i = 0; i < iYmax; i++)
  178. {
  179. for (int j = 0; j < iYmax; j++)
  180. {
  181. fwrite(&buffor[j][i][0], 1, 1, fp);
  182. fwrite(&buffor[j][i][1], 1, 1, fp);
  183. fwrite(&buffor[j][i][2], 1, 1, fp);
  184. }
  185. }
  186. fclose(fp);
  187. return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement