From d06f8d5b37c553423be816614cabd008a3eedc1b Mon Sep 17 00:00:00 2001
From: Josef Ilg <josef.ilg@fau.de>
Date: Wed, 6 Jun 2018 16:45:42 +0200
Subject: [PATCH] add standard functions of MPI + std main file

---
 Makefile            |  8 ++++++--
 src/Makefile        | 15 +++++++++++++++
 src/main.c          |  7 +++++++
 src/mpi/Makefile    |  1 -
 src/mpi/mpi-layer.c | 25 +++++++++++++++++++++----
 src/mpi/mpi-layer.h | 11 +++++++++++
 6 files changed, 60 insertions(+), 7 deletions(-)
 create mode 100644 src/Makefile
 create mode 100644 src/main.c
 create mode 100644 src/mpi/mpi-layer.h

diff --git a/Makefile b/Makefile
index 920c525..44945b2 100644
--- a/Makefile
+++ b/Makefile
@@ -1,13 +1,17 @@
 .PHONY: build clean
 
-build: mpi cracker
+build: main mpi cracker
+
+main:
+	$(MAKE) -C src
 
 mpi:
 	$(MAKE) -C src/mpi
-    
+
 cracker:
 	$(MAKE) -C src/cracker
 
 clean:
+	$(MAKE) -C src clean
 	$(MAKE) -C src/mpi clean
 	$(MAKE) -C src/cracker clean
diff --git a/src/Makefile b/src/Makefile
new file mode 100644
index 0000000..64a45da
--- /dev/null
+++ b/src/Makefile
@@ -0,0 +1,15 @@
+CFLAGS += -std=c99 -pedantic -Wall -Werror -Wextra -D_FORTIFY_SOURCE=2 -O3 -fPIC -fstack-protector-all -pie -s
+LDFLAGS += -lmpi
+CC = mpicc
+.PHONY: all clean
+
+all: main
+
+main: mpi/mpi-layer.o main.o
+	$(CC) -o $@ $^ $(LDFLAGS)
+
+%.o: %.c %.h
+	$(CC) $(CPPFLAGS) -c $(CFLAGS) $(LDFLAGS) -o $@ $<
+
+clean:
+	rm -f *.o
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..d869f4c
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,7 @@
+#include<stdio.h>
+#include"mpi/mpi-layer.h"
+
+int main(int argc, char* argv[]) {
+	mpi_start(argc, argv);	
+	return 0;
+}
diff --git a/src/mpi/Makefile b/src/mpi/Makefile
index 9e9f5c3..12de88e 100644
--- a/src/mpi/Makefile
+++ b/src/mpi/Makefile
@@ -1,5 +1,4 @@
 CFLAGS += -std=c99 -pedantic -Wall -Werror -Wextra -D_FORTIFY_SOURCE=2 -O3 -fPIC -fstack-protector-all -pie -s
-LDFLAGS += -lmpi
 CC = mpicc
 
 .PHONY: all clean
diff --git a/src/mpi/mpi-layer.c b/src/mpi/mpi-layer.c
index d06636e..e50e9cd 100644
--- a/src/mpi/mpi-layer.c
+++ b/src/mpi/mpi-layer.c
@@ -1,6 +1,23 @@
-#include <stdlib.h>
-#include <mpi.h>
+#include "mpi-layer.h"
 
-void init (void) {
-    return;
+int mpi_start (int argc, char* argv[]) {
+	int rank, size;
+
+  	MPI_Init(&argc, &argv);      /* starts MPI */
+	MPI_Comm_rank(MPI_COMM_WORLD, &rank);        /* get current process id */
+	MPI_Comm_size(MPI_COMM_WORLD, &size);        /* get number of processes */
+
+	printf("Process %d of %d: Hello!\n", rank, size);
+	node_logic(rank);
+
+	//TODO: Signal handler for soft abort
+	MPI_Abort(MPI_COMM_WORLD, 0);
+
+	MPI_Finalize();
+  	return 0;
+}
+
+void node_logic (int rank) {
+	
+	return;
 }
diff --git a/src/mpi/mpi-layer.h b/src/mpi/mpi-layer.h
new file mode 100644
index 0000000..cca3754
--- /dev/null
+++ b/src/mpi/mpi-layer.h
@@ -0,0 +1,11 @@
+#ifndef MPI_INIT_H_
+#define MPI_INIT_H_
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <mpi.h>
+#include <sys/wait.h>
+
+int mpi_start(int, char*[]);
+void node_logic(int);
+#endif
-- 
GitLab