mandelbrot.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "mandelbrot.h"
  2. double map(int value, int min, int max, int map_min, int map_max)
  3. {
  4. double R = (double) (map_max - map_min) / (double) (max - min);
  5. double y = map_min + (value * R) + R;
  6. return y;
  7. }
  8. int mandelbrot_pix(double x, double y, int bail_out, int max_iter)
  9. {
  10. int k;
  11. double real, imaginary, x2, y2;
  12. x2 = x;
  13. y2 = y;
  14. // iterate a^2 - b^2 + 2ab
  15. for (k = 0; k < max_iter; k++) {
  16. // a^2 - b^2
  17. real = x * x - y * y;
  18. // 2ab
  19. imaginary = 2 * x * y;
  20. // x = real + c
  21. x = real + x2;
  22. // y = imaginary + c
  23. y = imaginary +y2;
  24. if ((x * x + y * y) > bail_out) {
  25. return k;
  26. }
  27. }
  28. return max_iter;
  29. }
  30. void mandelbrot(int *field, int w, int h, int bail_out, int max_iter,
  31. int thread_count)
  32. {
  33. for (int i = 0; i < w; i++) {
  34. for (int j = 0; j < h; j++) {
  35. double x = map(i, 0, w, -1, 1);
  36. double y = map(j, 0, h, -1, 1);
  37. int k = mandelbrot_pix(x, y, BAIL_OUT, MAX_ITER);
  38. fprintf(stdout, "\nComputing %d %d = %d", i, j, k);
  39. field[i + j * h] = k;
  40. }
  41. }
  42. }
  43. void *produce(void *b)
  44. {
  45. mandel_buf_t *buf;
  46. mandel_t *m;
  47. double x;
  48. double y;
  49. buf = (mandel_buf_t *) b;
  50. for (int i = 0; i < buf->prop.width; i++) {
  51. for (int j = 0; j < buf->prop.height; j++) {
  52. m = malloc(sizeof(mandel_t));
  53. m->x = i;
  54. m->y = j;
  55. enqueue(buf->q, m, buf->lock);
  56. }
  57. }
  58. return NULL;
  59. }
  60. void *consume(void *b)
  61. {
  62. mandel_buf_t *buf;
  63. mandel_t *m;
  64. double x, y;
  65. int k, i;
  66. buf = (mandel_buf_t *) b;
  67. /* This will not work on a multi-consumer program */
  68. while (i < buf->prop.width * buf->prop.height) {
  69. if (!is_empty(buf->q)) {
  70. m = dequeue(buf->q, buf->lock);
  71. x = map(m->x, 0, buf->prop.width, -1, 1);
  72. y = map(m->y, 0, buf->prop.height, -1, 1);
  73. //fprintf(stdout, "\nSending %d %d", m->x, m->y, k);
  74. k = mandelbrot_pix(x, y, buf->prop.bail_out,
  75. buf->prop.max_iter);
  76. buf->result_field[m->y + m->x * buf->prop.height] = k;
  77. free(m);
  78. i++;
  79. }
  80. }
  81. return NULL;
  82. }
  83. int main(int argc, char **argv)
  84. {
  85. pthread_t consumer;
  86. pthread_t producer;
  87. pthread_attr_t attr;
  88. pthread_mutex_t lock;
  89. printf
  90. ("WIDTH: %d\nHEIGHT %d\nBAIL OUT %d\nMAX ITER %d\nTHREAD COUNT %d",
  91. WIDTH, HEIGHT, BAIL_OUT, MAX_ITER, THREAD_COUNT);
  92. pthread_attr_init(&attr);
  93. /* Define current mandelbrot set properties */
  94. mandel_prop_t prop;
  95. prop.bail_out = BAIL_OUT;
  96. prop.max_iter = MAX_ITER;
  97. prop.height = HEIGHT;
  98. prop.width = WIDTH;
  99. /* Initialize the buffer for threads to work with */
  100. mandel_buf_t buf;
  101. buf.prop = prop;
  102. queue_t *queue;
  103. queue = initialize_queue();
  104. pthread_mutex_init(&lock, NULL);
  105. buf.lock = &lock;
  106. buf.result_field = malloc(sizeof(int) * WIDTH * HEIGHT);
  107. buf.q = queue;
  108. /* Create and start threads */
  109. pthread_create(&producer, &attr, produce, &buf);
  110. pthread_create(&consumer, &attr, consume, &buf);
  111. pthread_join(producer, NULL);
  112. printf("\nFinished producer!");
  113. pthread_join(consumer, NULL);
  114. printf("\nFinished consumer!");
  115. /* Create the png image with the result buffer */
  116. pix_row rows[WIDTH];
  117. pix p;
  118. char filename[30];
  119. image img;
  120. p.r = 200;
  121. p.g = 200;
  122. p.b = 200;
  123. for (int i = 0; i < WIDTH; i++) {
  124. rows[i].p = malloc(HEIGHT * sizeof(pix));
  125. for (int j = 0; j < HEIGHT; j++) {
  126. p.r =
  127. map(buf.result_field[i * HEIGHT + j], 0,
  128. MAX_ITER, 0, 255);
  129. p.g =
  130. map(buf.result_field[i * HEIGHT + j], 0,
  131. MAX_ITER, 0, 255);
  132. p.b =
  133. map(buf.result_field[i * HEIGHT + j], 0,
  134. MAX_ITER, 0, 255);
  135. rows[i].p[j] = p;
  136. }
  137. }
  138. sprintf(filename, "mandel.png");
  139. img = initialize_png("mandelbrot", filename, WIDTH, HEIGHT);
  140. write_image(&img, rows);
  141. finish_image(&img);
  142. for (int i = 0; i < WIDTH; i++) {
  143. free(rows[i].p);
  144. }
  145. free(buf.result_field);
  146. return (0);
  147. }