PandA-2024.02
mandelbrot.c
Go to the documentation of this file.
1 // Mandelbrot kernel
2 // Author: Andrew Canis
3 // Date: July 1, 2012
4 
5 #include <stdio.h>
6 
7 inline void print_image(int width, int height, int max, unsigned char img[width][height])
8 {
9  int i, j;
10  printf("P2\n%d %d\n%d\n", width, height, max);
11  for (j = 0; j < height; j++)
12  {
13  for (i = 0; i < width; i++)
14  {
15  // assume grayscale image
16  printf("%d ", img[i][j]);
17  }
18  printf("\n");
19  }
20 }
21 
22 //#define PRINT_IMG 1
23 
24 #define DECIMAL_PLACES 28
25 #define int2fixed(num) ((num) << DECIMAL_PLACES)
26 #define fixedmul(a, b) ((((long long)a) * ((long long)b)) >> DECIMAL_PLACES)
27 #define fixed2int(num) ((num) >> DECIMAL_PLACES)
28 
29 #define HEIGHT 128
30 #define WIDTH 128
31 //#define MAX_ITER 10
32 //#define MAX_ITER 2
33 #define MAX_ITER 50
34 #define NUM_ACCEL 4
35 #define OPS_PER_ACCEL HEIGHT/NUM_ACCEL
36 #define OMP_ACCEL 4
37 
38 
39 struct thread_data
40 {
41  int startidx;
42  int maxidx;
43 };
44 
45 volatile unsigned char img[WIDTH][HEIGHT];
46 
48 {
49  int i, j, tid;
50  int count = 0;
51 
52  for (j = 0; j < OPS_PER_ACCEL; j++)
53  {
54  for (i = 0; i < WIDTH; i++)
55  {
56 
57  int x_0 = int2fixed(-2) + ((((3 << 20) * i / WIDTH) ) << 8);
58  int y_0 = int2fixed(-1) + ((((2 << 20) * (j + startidx) / HEIGHT) ) << 8);
59 
60  int x = 0;
61  int y = 0;
62  int xtmp;
63  unsigned char iter;
64  unsigned char fiter = 0;
65 
66  for (iter = 0; iter < MAX_ITER; iter++)
67  {
68  long long abs_squared = fixedmul(x, x) + fixedmul(y, y);
69 
70  xtmp = fixedmul(x, x) - fixedmul(y, y) + x_0;
71  y = fixedmul(int2fixed(2), fixedmul(x, y)) + y_0;
72  x = xtmp;
73 
74  fiter += abs_squared <= int2fixed(4);
75  }
76 
77  //get black or white
78  unsigned char colour = (fiter >= MAX_ITER) ? 0 : 1;
79  //accumulate colour
80  count += colour;
81  //update image
82  img[i][j + startidx] = colour;
83  }
84  }
85  return count;
86 }
87 
88 
89 int main()
90 {
91  int final_result = 0;
92  int i, j;
93  int count[NUM_ACCEL];
94 
95 #pragma omp simd
96  //launch threads
97  //for (i=0; i<1; i++) {
98  for (i = 0; i < NUM_ACCEL; i++)
99  {
100  count[i] = mandelbrot(i * OPS_PER_ACCEL);
101  }
102 
103  //sum the results
104  for (i = 0; i < NUM_ACCEL; i++)
105  {
106  final_result += count[i];
107  }
108  //count = mandelbrot();
109  /*
110  //check results
111  for (j = 0; j < HEIGHT; j++) {
112  for (i = 0; i < WIDTH; i++) {
113  // printf("img[%d][%d] = %d\n", i, j, img[i][j]);
114  final_result += img[i][j];
115  }
116  }*/
117 #ifdef PRINT_IMG
118  unsigned char final[WIDTH][HEIGHT];
119  for (j = 0; j < HEIGHT; j++)
120  {
121  for (i = 0; i < WIDTH; i++)
122  {
123  final[i][j] = img[i][j];
124  /* final[i+1][j] = img1[i+1][j];
125  final[i+2][j] = img2[i+2][j];
126  final[i+3][j] = img3[i+3][j];*/
127  }
128  }
129  print_image(WIDTH, HEIGHT, 1, final);
130  //print_image(WIDTH, HEIGHT, 1, img);
131  //print_image(WIDTH, HEIGHT, MAX_ITER, img);
132 
133 #else
134  printf("Count: %d\n", final_result);
135  if (final_result == 12013)
136  {
137  printf("PASS\n");
138  return 0;
139  }
140  else
141  {
142  printf("FAIL\n");
143  return 1;
144  }
145 #endif
146 
147 }
void print_image(int width, int height, int max, unsigned char img[width][height])
Definition: mandelbrot.c:7
#define OPS_PER_ACCEL
Definition: mandelbrot.c:35
volatile unsigned char img[WIDTH][HEIGHT]
Definition: mandelbrot.c:20
#define HEIGHT
Definition: mandelbrot.c:29
#define fixedmul(a, b)
Definition: mandelbrot.c:26
int maxidx
Definition: main.c:26
#define max
Definition: backprop.h:17
#define MAX_ITER
Definition: mandelbrot.c:33
#define int2fixed(num)
Definition: mandelbrot.c:25
int startidx
Definition: main.c:25
int main()
Definition: mandelbrot.c:57
int mandelbrot()
Definition: mandelbrot.c:22
x
Return the smallest n such that 2^n >= _x.
#define WIDTH
Definition: mandelbrot.c:30
#define NUM_ACCEL
Definition: mandelbrot.c:34

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13