Last active
November 12, 2015 01:07
-
-
Save alarrosa14/6f3ed435b400ff9cb6fa to your computer and use it in GitHub Desktop.
C fork based multiprocess mandelbrot algorithm implementation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <limits.h> | |
#include <sys/mman.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <math.h> | |
typedef unsigned char bool; | |
#define true 0xff | |
#define false 0x00 | |
int process_qty = 8; | |
const double yMin = -1.0; | |
const double yMax = +1.0; | |
const double xMin = -2.0; | |
const double xMax = +0.5; | |
const double dxy = 0.00025; | |
void* childProcess(unsigned long process, unsigned char* result, bool* rows_processed, unsigned long cols, unsigned long rows) { | |
double cx; | |
double zx, zy, new_zx; | |
unsigned char n; | |
unsigned long nx; | |
int cy = (int) process; | |
while (cy < rows) { | |
if (rows_processed[cy] == false) { | |
rows_processed[cy] = true; | |
for (cx = xMin, nx = 0; cx < xMax; cx += dxy, nx++) { | |
zx = 0.0; | |
zy = 0.0; | |
n = 0; | |
while ((zx*zx + zy*zy < 8.0) && (n != UCHAR_MAX)) { | |
new_zx = zx*zx - zy*zy + cx; | |
zy = 2.0*zx*zy + yMin + cy*dxy; | |
zx = new_zx; | |
n++; | |
} | |
*(result + cy*cols + nx) = n; | |
} | |
} | |
cy++; | |
} | |
_exit(1); | |
} | |
int main(int argc, char *argv[]){ | |
// Calculo alto y ancho de la imagen resultante: | |
unsigned long cols = (unsigned long) ceil(fabs(xMax - xMin) / dxy); | |
unsigned long rows = (unsigned long) ceil(fabs(yMax - yMin) / dxy); | |
// Pido shared para array de filas procesadas: | |
int fd_rp; | |
fd_rp = shm_open("/rowsProcessed", O_RDWR | O_CREAT, 0777); | |
ftruncate(fd_rp, rows); | |
bool* rows_processed = (bool*) mmap(NULL, rows, PROT_READ | PROT_WRITE, MAP_SHARED, fd_rp, 0); | |
// Inicializo en 0 rows_processed | |
unsigned long i; | |
for (i = 0; i < rows; i++) { | |
rows_processed[i] = false; | |
} | |
// Pido shared memory para la imagen | |
int fd; | |
fd = shm_open("/result", O_RDWR | O_CREAT, 0777); | |
ftruncate(fd, cols*rows); | |
void* result = mmap(NULL, cols*rows, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
// Ejecuto los threads: | |
pid_t pid; | |
for (i = 0; i < process_qty; i++) { | |
if ((pid = fork()) == 0) { | |
childProcess(i, (unsigned char *) result, (bool*) rows_processed, cols, rows); | |
_exit(1); | |
} | |
} | |
// Espero por los porcesos hijos: | |
int status; | |
while (wait(&status) > 0); | |
unsigned long written = 0; | |
unsigned long bytes; | |
while (written != cols*rows) { | |
bytes = write(1, (unsigned char*) result + written, cols*rows); | |
written += bytes; | |
} | |
shm_unlink("/result"); | |
shm_unlink("/rowsProcessed"); | |
fprintf(stderr, "To process the image: convert -depth 8 -size %dx%d gray:output out.jpg\n", cols, rows); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment