diff --git a/exercise2/Makefile b/exercise2/Makefile index a809fa5..ff8427a 100644 --- a/exercise2/Makefile +++ b/exercise2/Makefile @@ -10,17 +10,16 @@ NPROC := 3 .PHONY: all clean run -all: clean run +all: parallel $(TARGET): $(SRC) - mkdir -p $(OUTDIR) $(CC) $(CFLAGS) -o $(OUT) $< run: $(TARGET) cd $(OUTDIR) && ./$< $(ARGS) clean: - rm -rf $(OUTDIR) + rm -rf $(OUTDIR)/* time: $(TARGET) python3 bench.py './$(OUT) 0' $(NBENCH) diff --git a/exercise2/mandel_mpi.c b/exercise2/mandel_mpi.c index c3ce163..597ad3e 100644 --- a/exercise2/mandel_mpi.c +++ b/exercise2/mandel_mpi.c @@ -2,6 +2,7 @@ #include #include #include +#include #define XSIZE 2560 #define YSIZE 2048 @@ -22,6 +23,8 @@ typedef struct { double real, imag; } complex_t; +int rank, size; + void calculate() { for (int i = 0; i < XSIZE; i++) { for (int j = 0; j < YSIZE; j++) { @@ -83,36 +86,53 @@ void fancycolour(uchar *p, int iter) { int main(int argc, char **argv) { MPI_Init(&argc, &argv); - int rank, size; MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); - printf("hello from rank %d/%d", rank, size); - if (argc == 1) { - puts("Usage: MANDEL n"); - puts("n decides whether image should be written to disk (1=yes, 0=no)"); - return 0; - } - /* Calculate the range in the y-axis such that we preserve the - aspect ratio */ + step = (xright - xleft) / XSIZE; - yupper = ycenter + (step * YSIZE) / 2; - ylower = ycenter - (step * YSIZE) / 2; + double old_yupper = ycenter + (step * YSIZE) / 2; + double old_ylower = ycenter - (step * YSIZE) / 2; + double delta = (old_yupper - old_ylower) / size; + + ylower = old_ylower + rank * delta; + yupper = old_ylower + (rank + 1) * delta; calculate(); - if (strtol(argv[1], NULL, 10) != 0) { - /* create nice image from iteration counts. take care to create it upside - down (bmp format) */ - unsigned char *buffer = calloc(XSIZE * YSIZE * 3, 1); - for (int i = 0; i < XSIZE; i++) { - for (int j = 0; j < YSIZE; j++) { - int p = ((YSIZE - j - 1) * XSIZE + i) * 3; - fancycolour(buffer + p, pixel[PIXEL(i, j)]); - } + printf("after calculate %d\n", rank); + + const int bufsize = XSIZE * YSIZE * 3; + unsigned char *buffer = calloc(bufsize, 1); + for (int i = 0; i < XSIZE; i++) { + for (int j = 0; j < YSIZE; j++) { + int p = ((YSIZE - j - 1) * XSIZE + i) * 3; + fancycolour(buffer + p, pixel[PIXEL(i, j)]); } - /* write image to disk */ - savebmp("mandel.bmp", buffer, XSIZE, YSIZE); } + + printf("after buffer %d\n", rank); + + if (rank != 0) + MPI_Send(buffer, bufsize, MPI_INT8_T, 0, 0, MPI_COMM_WORLD); + + printf("hello again from %d\n", rank); + + // use process 0 as central process: gather calulations and output image + if (rank == 0) { + unsigned char *bufferest = calloc(bufsize * size, 1); + memcpy(bufferest, buffer, bufsize); + printf("after memcpy\n"); + + for (int i = 1; i < size; i++) { + MPI_Recv(buffer, bufsize, MPI_INT8_T, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + memcpy(bufferest + bufsize * i, buffer, bufsize); + } + printf("after loop\n"); + + savebmp("mandel.bmp", buffer, XSIZE, YSIZE * size); + printf("after save\n"); + } + MPI_Finalize(); return 0; }