Skip to content

Instantly share code, notes, and snippets.

@hnrck
Created January 4, 2019 12:24
Show Gist options
  • Save hnrck/1d0358cc62a44252fb7025ff300a9bdd to your computer and use it in GitHub Desktop.
Save hnrck/1d0358cc62a44252fb7025ff300a9bdd to your computer and use it in GitHub Desktop.
Open MPI installation test file with generic Makefile
#include <iostream>
#include <mpi.h>
struct Info {
int world_size;
int world_rank;
struct Processor {
char name[MPI_MAX_PROCESSOR_NAME];
int name_len;
} processor;
};
int main() {
auto info = Info();
MPI_Init(nullptr, nullptr);
MPI_Comm_size(MPI_COMM_WORLD, &info.world_size);
MPI_Comm_rank(MPI_COMM_WORLD, &info.world_rank);
MPI_Get_processor_name(info.processor.name, &info.processor.name_len);
std::cout << "Hello, world! From processor " << info.processor.name << ", rank " << info.world_rank + 1 << "/"
<< info.world_size << std::endl;
MPI_Finalize();
return 0;
}
BIN?=main
OMPI_CC=clang++ mpic++
MPICXX?=mpic++
MPIRUN?=mpirun
EXT?=cpp
LINKS?=
SRCS=$(shell find . -name "*."$(EXT))
DIRS=$(shell find ./* -type d)
OBJS=$(patsubst %.$(EXT),%.o,$(SRCS))
INCS=$(addprefix -I, $(DIRS))
CPPFLAGS+=-std=c++17 -Wall $(INCS)
LDFLAGS+=$(addprefix -l, $(LINKS))
.PHONY: all run clean
all: $(BIN)
$(EXT).o: $(SRCS)
$(CXX) $(CPPFLAGS) -c $<
$(BIN): $(OBJS)
$(MPICXX) $(CPPFLAGS) -o $(BIN) $(notdir $(OBJS)) $(LDFLAGS)
run: all
-$(MPIRUN) ./$(BIN)
clean:
-rm -f $(notdir $(OBJS)) $(BIN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment