# CS380P Lab 5: MPI Barnes-Hut N-Body Simulation
# Makefile for compiling nbody program

# Compiler and flags
MPICC = mpicc
CFLAGS = -O3 -Wall -std=c99 -g
LDFLAGS = -lm

# Target executable
TARGET = nbody

# Source files
SOURCES = nbody.c
OBJECTS = $(SOURCES:.c=.o)

# Default target (without visualization)
all: $(TARGET)

# Build target
$(TARGET): $(SOURCES)
	$(MPICC) $(CFLAGS) -o $(TARGET) $(SOURCES) $(LDFLAGS)

# Clean build artifacts
clean:
	rm -f $(TARGET) $(OBJECTS) *.o core output*.txt

# Run test with small input (10 bodies)
test: $(TARGET)
	mpirun -n 2 ./$(TARGET) -i ../input/nb-10.txt -o output-10.txt -s 100 -t 0.5 -d 0.005

# Run test with medium input (100 bodies)
test-medium: $(TARGET)
	mpirun -n 4 ./$(TARGET) -i ../input/nb-100.txt -o output-100.txt -s 100 -t 0.5 -d 0.005

# Run test with large input (100000 bodies) - limited iterations
test-large: $(TARGET)
	mpirun -n 8 ./$(TARGET) -i ../input/nb-100000.txt -o output-100000.txt -s 10 -t 0.5 -d 0.005

# Run all tests
test-all: test test-medium test-large

# Performance benchmarking
benchmark: $(TARGET)
	@echo "=== Sequential (1 process) ==="
	@mpirun -n 1 ./$(TARGET) -i ../input/nb-100.txt -o output-seq.txt -s 100 -t 0.5 -d 0.005
	@echo ""
	@echo "=== 2 processes ==="
	@mpirun -n 2 ./$(TARGET) -i ../input/nb-100.txt -o output-2p.txt -s 100 -t 0.5 -d 0.005
	@echo ""
	@echo "=== 4 processes ==="
	@mpirun -n 4 ./$(TARGET) -i ../input/nb-100.txt -o output-4p.txt -s 100 -t 0.5 -d 0.005
	@echo ""
	@echo "=== 8 processes ==="
	@mpirun -n 8 ./$(TARGET) -i ../input/nb-100.txt -o output-8p.txt -s 100 -t 0.5 -d 0.005

.PHONY: all clean test test-medium test-large test-all benchmark
