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 #include <pthread.h>
7 
8 inline void print_image(int width, int height, int max, unsigned char img[width][height])
9 {
10  int i, j;
11  printf("P2\n%d %d\n%d\n", width, height, max);
12  for (j = 0; j < height; j++)
13  {
14  for (i = 0; i < width; i++)
15  {
16  // assume grayscale image
17  printf("%d ", img[i][j]);
18  }
19  printf("\n");
20  }
21 }
22 
23 //#define PRINT_IMG 1
24 
25 #define DECIMAL_PLACES 28
26 #define int2fixed(num) ((num) << DECIMAL_PLACES)
27 #define fixedmul(a, b) ((((long long)a) * ((long long)b)) >> DECIMAL_PLACES)
28 #define fixed2int(num) ((num) >> DECIMAL_PLACES)
29 
30 #define HEIGHT 128
31 #define WIDTH 128
32 //#define MAX_ITER 10
33 //#define MAX_ITER 2
34 #define MAX_ITER 50
35 #define NUM_ACCEL 4
36 #define OPS_PER_ACCEL HEIGHT/NUM_ACCEL
37 #define OMP_ACCEL 4
38 
39 #define LEGUP 1
40 
41 struct thread_data
42 {
43  int startidx;
44  int maxidx;
45 };
46 
47 volatile unsigned char img[WIDTH][HEIGHT];
48 
49 void *mandelbrot(void *threadarg)
50 {
51  int i, j, tid;
52  int count = 0;
53  struct thread_data* arg = (struct thread_data*) threadarg;
54  int startidx = arg->startidx;
55  int maxidx = arg->maxidx;
56 
57  for (j = startidx; j < maxidx; j++)
58  {
59  for (i = 0; i < WIDTH; i++)
60  {
61 
62  int x_0 = int2fixed(-2) + ((((3 << 20) * i / WIDTH) ) << 8);
63  int y_0 = int2fixed(-1) + ((((2 << 20) * j / HEIGHT) ) << 8);
64 
65  int x = 0;
66  int y = 0;
67  int xtmp;
68  unsigned char iter;
69  unsigned char fiter = 0;
70 
71  for (iter = 0; iter < MAX_ITER; iter++)
72  {
73  long long abs_squared = fixedmul(x, x) + fixedmul(y, y);
74 
75  xtmp = fixedmul(x, x) - fixedmul(y, y) + x_0;
76  y = fixedmul(int2fixed(2), fixedmul(x, y)) + y_0;
77  x = xtmp;
78 
79  fiter += abs_squared <= int2fixed(4);
80  }
81 
82  //get black or white
83  unsigned char colour = (fiter >= MAX_ITER) ? 0 : 1;
84  //accumulate colour
85  count += colour;
86  //update image
87  img[i][j] = colour;
88  }
89  }
90 
91  pthread_exit((void*)count);
92 }
93 
94 
95 int main()
96 {
97 #ifdef LEGUP
98  legup_start_counter(0);
99 #endif
100  int final_result = 0;
101  int i, j;
102  int count[NUM_ACCEL];
103  pthread_t threads[NUM_ACCEL];
104  struct thread_data data[NUM_ACCEL];
105 
106  //initialize structs to pass into accels
107  for (i = 0; i < NUM_ACCEL; i++)
108  {
109  data[i].startidx = i * OPS_PER_ACCEL;
110  if (i == NUM_ACCEL - 1)
111  {
112  data[i].maxidx = HEIGHT;
113  }
114  else
115  {
116  data[i].maxidx = (i + 1) * OPS_PER_ACCEL;
117  }
118  }
119 
120  //launch threads
121  //for (i=0; i<1; i++) {
122  for (i = 0; i < NUM_ACCEL; i++)
123  {
124  pthread_create(&threads[i], NULL, mandelbrot, (void *)&data[i]);
125  }
126 
127  //join the threads
128  //for (i=0; i<1; i++) {
129  for (i = 0; i < NUM_ACCEL; i++)
130  {
131  pthread_join(threads[i], (void**)&count[i]);
132  }
133 
134  //sum the results
135  for (i = 0; i < NUM_ACCEL; i++)
136  {
137  final_result += count[i];
138  }
139  //count = mandelbrot();
140  /*
141  //check results
142  for (j = 0; j < HEIGHT; j++) {
143  for (i = 0; i < WIDTH; i++) {
144  // printf("img[%d][%d] = %d\n", i, j, img[i][j]);
145  final_result += img[i][j];
146  }
147  }*/
148 #ifdef PRINT_IMG
149  unsigned char final[WIDTH][HEIGHT];
150  for (j = 0; j < HEIGHT; j++)
151  {
152  for (i = 0; i < WIDTH; i++)
153  {
154  final[i][j] = img[i][j];
155  /* final[i+1][j] = img1[i+1][j];
156  final[i+2][j] = img2[i+2][j];
157  final[i+3][j] = img3[i+3][j];*/
158  }
159  }
160  print_image(WIDTH, HEIGHT, 1, final);
161  //print_image(WIDTH, HEIGHT, 1, img);
162  //print_image(WIDTH, HEIGHT, MAX_ITER, img);
163 
164 #else
165 #ifdef LEGUP
166  int perf_counter = legup_stop_counter(0);
167  printf("perf_counter = %d\n", perf_counter);
168 #endif
169  printf("Count: %d\n", final_result);
170  if (final_result == 12013)
171  {
172  printf("PASS\n");
173  }
174  else
175  {
176  printf("FAIL\n");
177  }
178 #endif
179 
180 }
#define NULL
void print_image(int width, int height, int max, unsigned char img[width][height])
Definition: mandelbrot.c:7
volatile unsigned char img[WIDTH][HEIGHT]
Definition: mandelbrot.c:20
#define MAX_ITER
Definition: mandelbrot.c:34
#define int2fixed(num)
Definition: mandelbrot.c:26
#define WIDTH
Definition: mandelbrot.c:31
int maxidx
Definition: main.c:26
#define OPS_PER_ACCEL
Definition: mandelbrot.c:36
#define max
Definition: backprop.h:17
#define NUM_ACCEL
Definition: mandelbrot.c:35
int startidx
Definition: main.c:25
int main()
Definition: mandelbrot.c:57
list threads
Definition: test_panda.py:871
#define HEIGHT
Definition: mandelbrot.c:30
int mandelbrot()
Definition: mandelbrot.c:22
x
Return the smallest n such that 2^n >= _x.
#define fixedmul(a, b)
Definition: mandelbrot.c:27

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