From efd1fa4922f772bceb914082cb75bc6819852634 Mon Sep 17 00:00:00 2001 From: fredrikr79 Date: Wed, 10 Sep 2025 17:51:06 +0200 Subject: [PATCH] ex2: make parallel job init --- exercise2/Makefile | 12 +++++++++--- exercise2/mandel_mpi.c | 7 +++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/exercise2/Makefile b/exercise2/Makefile index 40ce328..a809fa5 100644 --- a/exercise2/Makefile +++ b/exercise2/Makefile @@ -2,9 +2,11 @@ CC := gcc CFLAGS := -Wall -Wextra -std=c17 SRC := mandel_mpi.c TARGET := $(SRC:.c=) -ARGS := 1 OUTDIR := out +OUT := $(OUTDIR)/$(TARGET) +ARGS := 1 NBENCH := 1 +NPROC := 3 .PHONY: all clean run @@ -12,7 +14,7 @@ all: clean run $(TARGET): $(SRC) mkdir -p $(OUTDIR) - $(CC) $(CFLAGS) -o $(OUTDIR)/$@ $< + $(CC) $(CFLAGS) -o $(OUT) $< run: $(TARGET) cd $(OUTDIR) && ./$< $(ARGS) @@ -21,4 +23,8 @@ clean: rm -rf $(OUTDIR) time: $(TARGET) - python3 bench.py '$(OUTDIR)/$(TARGET) 0' $(NBENCH) + python3 bench.py './$(OUT) 0' $(NBENCH) + +parallel: $(SRC) + mpicc -o $(OUT) $(SRC) + mpirun -np $(NPROC) $(OUT) diff --git a/exercise2/mandel_mpi.c b/exercise2/mandel_mpi.c index 7127d7d..c3ce163 100644 --- a/exercise2/mandel_mpi.c +++ b/exercise2/mandel_mpi.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -81,6 +82,11 @@ 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)"); @@ -107,5 +113,6 @@ int main(int argc, char **argv) { /* write image to disk */ savebmp("mandel.bmp", buffer, XSIZE, YSIZE); } + MPI_Finalize(); return 0; }