|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <pthread.h> |
| 4 | + |
| 5 | +#ifndef WIDTH |
| 6 | +#define WIDTH 3840 |
| 7 | +#endif |
| 8 | + |
| 9 | +#ifndef HEIGHT |
| 10 | +#define HEIGHT 2160 |
| 11 | +#endif |
| 12 | + |
| 13 | +#ifndef MAX_ITER |
| 14 | +#define MAX_ITER 2000 |
| 15 | +#endif |
| 16 | + |
| 17 | +#ifndef NUM_THREADS |
| 18 | +#define NUM_THREADS 12 |
| 19 | +#endif |
| 20 | + |
| 21 | +#if NUM_THREADS > HEIGHT |
| 22 | +#error "Can't have more threads than pixel rows!" |
| 23 | +#endif |
| 24 | + |
| 25 | +void set_pixel(unsigned char *img, int x, int y, unsigned char *color); |
| 26 | +unsigned char *create_image(int w, int h); |
| 27 | +void write_ppm(char *name, int w, int h, unsigned char *img); |
| 28 | + |
| 29 | +unsigned char **palette; |
| 30 | + |
| 31 | +unsigned char cga_palette[16][3] = { // CGA 16-color palette, as RGB |
| 32 | + {0x00,0x00,0x00}, |
| 33 | + {0x00,0x00,0xAA}, |
| 34 | + {0x00,0xAA,0x00}, |
| 35 | + {0x00,0xAA,0xAA}, |
| 36 | + {0xAA,0x00,0x00}, |
| 37 | + {0xAA,0x00,0xAA}, |
| 38 | + {0xAA,0x55,0x00}, |
| 39 | + {0xAA,0xAA,0xAA}, |
| 40 | + {0x55,0x55,0x55}, |
| 41 | + {0x55,0x55,0xFF}, |
| 42 | + {0x55,0xFF,0x55}, |
| 43 | + {0x55,0xFF,0xFF}, |
| 44 | + {0xFF,0x55,0x55}, |
| 45 | + {0xFF,0x55,0xFF}, |
| 46 | + {0xFF,0xFF,0x55}, |
| 47 | + {0xFF,0xFF,0xFF} |
| 48 | +}; |
| 49 | + |
| 50 | +int next_row=0; |
| 51 | +pthread_mutex_t lock; |
| 52 | +unsigned char *img; |
| 53 | + |
| 54 | +void dorow(int py) { |
| 55 | + int px,i; |
| 56 | + double xz,yz,x,y,xt; |
| 57 | + |
| 58 | + for (px=0; px<WIDTH; px++) { |
| 59 | + xz = (double)px*3.5/WIDTH-2.5; |
| 60 | + yz = (double)py*2.0/HEIGHT-1.0; |
| 61 | + x = 0.0; |
| 62 | + y = 0.0; |
| 63 | + for (i=0; i<MAX_ITER; i++) { |
| 64 | + if (x*x+y*y > 4) { |
| 65 | + break; |
| 66 | + } |
| 67 | + xt = x*x - y*y + xz; |
| 68 | + y = 2*x*y + yz; |
| 69 | + x = xt; |
| 70 | + } |
| 71 | + if (i >= MAX_ITER) { |
| 72 | + i = 0; |
| 73 | + } |
| 74 | + set_pixel(img, px, py, palette[i]); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +void *rowthread(void *v) { |
| 79 | + int row; |
| 80 | + |
| 81 | + for(;;) { |
| 82 | + pthread_mutex_lock(&lock); |
| 83 | + row=next_row++; |
| 84 | + pthread_mutex_unlock(&lock); |
| 85 | + if(row>=HEIGHT) break; |
| 86 | + dorow(row); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +int main() { |
| 91 | + double start, end; |
| 92 | + struct timespec tspec; |
| 93 | + int py,i; |
| 94 | + pthread_t rowthreads[NUM_THREADS-1]; |
| 95 | + |
| 96 | + img = create_image(WIDTH, HEIGHT); |
| 97 | + palette=malloc(MAX_ITER*sizeof(unsigned char *)); |
| 98 | + for (int j=0; j<MAX_ITER; j++) palette[j]=malloc(3*sizeof(unsigned char)); |
| 99 | + clock_gettime(CLOCK_REALTIME, &tspec); |
| 100 | + start = tspec.tv_sec+1e-9*tspec.tv_nsec; |
| 101 | + |
| 102 | + for (i=0; i<MAX_ITER; i++) { |
| 103 | + if (i<16) { |
| 104 | + palette[i][0] = cga_palette[i][0]; |
| 105 | + palette[i][1] = cga_palette[i][1]; |
| 106 | + palette[i][2] = cga_palette[i][2]; |
| 107 | + } else if (i % 16 == 0) { |
| 108 | + palette[i][0] = i & 0x10 ? 255 : 0; |
| 109 | + palette[i][1] = i & 0x20 ? 255 : 0; |
| 110 | + palette[i][2] = i & 0x40 ? 255 : 0; |
| 111 | + if ((i & 0x70) == 0) { |
| 112 | + palette[i][0] = 255; |
| 113 | + palette[i][1] = 128; |
| 114 | + } |
| 115 | + } else { |
| 116 | + palette[i][0] = palette[i-1][0] > 16 ? palette[i-1][0] - 16 : 0; |
| 117 | + palette[i][1] = palette[i-1][1] > 16 ? palette[i-1][1] - 16 : 0; |
| 118 | + palette[i][2] = palette[i-1][2] > 16 ? palette[i-1][2] - 16 : 0; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + for (i=0; i<NUM_THREADS-1; i++) |
| 123 | + pthread_create(rowthreads+i, NULL, rowthread, NULL); |
| 124 | + rowthread(NULL); |
| 125 | + for(i=0; i<NUM_THREADS-1; i++) pthread_join(rowthreads[i], NULL); |
| 126 | + |
| 127 | + |
| 128 | + write_ppm("mandelbrot.ppm", WIDTH, HEIGHT, img); |
| 129 | + |
| 130 | + clock_gettime(CLOCK_REALTIME, &tspec); |
| 131 | + end = tspec.tv_sec+1e-9*tspec.tv_nsec; |
| 132 | + |
| 133 | + printf("Elapsed Time: %f s\n", end-start); |
| 134 | + |
| 135 | + return 0; |
| 136 | +} |
| 137 | + |
| 138 | +unsigned char *create_image(int w, int h) { |
| 139 | + unsigned char *rv; |
| 140 | + |
| 141 | + return rv = malloc(w*h*3); |
| 142 | +} |
| 143 | + |
| 144 | +void set_pixel(unsigned char *img, int x, int y, unsigned char *color) { |
| 145 | + img[3*(x+y*WIDTH)+0] = color[0]; // Red |
| 146 | + img[3*(x+y*WIDTH)+1] = color[1]; // Green |
| 147 | + img[3*(x+y*WIDTH)+2] = color[2]; // Blue |
| 148 | +} |
| 149 | + |
| 150 | +void write_ppm(char *name, int w, int h, unsigned char *img) { |
| 151 | + FILE *out; |
| 152 | + |
| 153 | + if (!(out=fopen(name, "w"))) { |
| 154 | + fprintf(stderr, "Unable to open %s for writing\n", name); |
| 155 | + return; |
| 156 | + } |
| 157 | + fprintf(out, "P6\n%d %d\n255\n", WIDTH, HEIGHT); |
| 158 | + fwrite(img, 3, w*h, out); |
| 159 | + fclose(out); |
| 160 | +} |
| 161 | + |
0 commit comments