Skip to content

Instantly share code, notes, and snippets.

@alarrosa14
Last active November 12, 2015 01:07
Show Gist options
  • Save alarrosa14/47dcb65213457747f568 to your computer and use it in GitHub Desktop.
Save alarrosa14/47dcb65213457747f568 to your computer and use it in GitHub Desktop.
PVM mandelbrot algorithm.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pvm3.h>
int main() {
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;
long cols = (long) ceil(fabs(xMax - xMin) / dxy);
long rows = (long) ceil(fabs(yMax - yMin) / dxy);
int n_slaves = 40;
int res;
int myTID = pvm_mytid();
int slaves[n_slaves];
if ((res = pvm_spawn("mandelbrot_slave",NULL,PvmTaskDefault,"",n_slaves,slaves)) < 1){
printf("Master: pvm_spawn error\n");
pvm_exit();
exit(1);
}
long matrix_size = rows*cols;
unsigned char* result = (unsigned char*)malloc(matrix_size*sizeof(unsigned char));
memset(result, 0, matrix_size);
long i;
for (i = 0; i < n_slaves; i++) {
pvm_initsend(PvmDataDefault);
pvm_pklong(&i, 1, 1);
pvm_send(slaves[i], 1);
}
long row;
int slave_tid;
while (i < rows) {
pvm_recv(-1, -1);
pvm_upkint(&slave_tid, 1, 1);
pvm_upklong(&row, 1, 1);
pvm_upkbyte(result + row*cols, (int) cols, 1);
pvm_initsend(PvmDataDefault);
pvm_pklong(&i, 1, 1);
pvm_send(slave_tid, 1);
i++;
}
for (i = 0; i < n_slaves; i++) {
pvm_recv(-1, -1);
pvm_upkint(&slave_tid, 1, 1);
pvm_upklong(&row, 1, 1);
pvm_upkbyte(result + row*cols, cols, 1);
pvm_kill(slave_tid);
}
long written = 0;
long bytes;
while (written != matrix_size) {
bytes = write(1, (unsigned char*) (result + written), matrix_size);
written += bytes;
}
free(result);
pvm_exit();
}
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <pvm3.h>
int main(int argc, char** argv){
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;
long cols = (long) ceil(fabs(xMax - xMin) / dxy);
double cx;
double zx, zy, new_zx;
unsigned char n;
long nx;
int myTID = pvm_mytid();
unsigned char* result = (unsigned char*)malloc(cols*sizeof(unsigned char));
memset(result, 0, cols);
long row;
while(1) {
pvm_recv(-1, -1);
pvm_upklong(&row, 1, 1);
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 + row*dxy;
zx = new_zx;
n++;
}
*(result + nx) = n;
}
pvm_initsend(PvmDataDefault);
pvm_pkint(&myTID, 1,1);
pvm_pklong(&row, 1,1);
pvm_pkbyte(result, cols, 1);
pvm_send(pvm_parent(), 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment