# Makefile for timing and testing of matrix computations: matrix multiplication, 
# LU factorization (Gaussian elimination), Cholesky factorization, symmetric
# indefinite factorization and modified Cholesky algorithms.
# Sets compiler and linker parameters.
# Compiles source code and links objects to generate executable files:
# mmultime, mmultest, mfactime and mfactest.
# Cleans object and executable files in current directory.

# Hardware specifications
PROC = Intel Xeon 5345
CORES = 4 x dual-core
CLKSPEED = 2.33 GHz
CACHE = 4096 KB per dual-core

# Compiler options
CC = icc
LANGUAGE = -x c++
OPTM = -O3

CFLAGS = -Wall $(LANGUAGE) $(OPTM)
CPPFLAGS = -I$(includedir) -DCHOLFACT -DLDLTFACT -DMODCHOL
CPPFLAGS +=	"-DCOMPILER=\"$(CC)\"" "-DLANGUAGE=\"$(LANGUAGE)\"" \
	"-DOPTM=\"$(OPTM)\"" "-DPROC=\"$(PROC)\"" "-DCORES=\"$(CORES)\"" \
	"-DCLKSPEED=\"$(CLKSPEED)\"" "-DCACHE=\"$(CACHE)\"" \
	"-DDATADIR=\"$(DATADIR)\""

LDFLAGS = -shared-intel 
LIBS =  -L/share/apps/intel/Compiler/11.1/046/mkl/lib/em64t \
        -lmkl_intel_lp64 -lmkl_core -lmkl_intel_thread \
		-L/share/apps/intel/Compiler/11.1/046/lib/intel64 \
		-liomp5 -lpthread -lrt -lstdc++

prefix = /scratch/st1185
projectdir = $(prefix)
sourcedir = $(projectdir)/source
includedir = $(projectdir)/include
DATADIR = $(projectdir)/data
VPATH = $(sourcedir) $(includedir) $(datadir)

objects1 = mmultime.o matmult.o matcom.o timing.o
objects2 = mmultest.o matmult.o matcom.o
objects3 = mfactime.o lufact.o cholfact.o modchol.o ldltfact.o matcom.o timing.o
objects4 = mfactest.o lufact.o cholfact.o modchol.o ldltfact.o matcom.o timing.o
objects = $(objects1) $(objects2) $(objects3) $(objects4)
sources = $(objects:.o=.c)

.PHONY: all
all: mmultime mmultest mfactime mfactest

mmultime: $(objects1)
mmultime.o: matmult.h matcom.h timing.h
matmult.o: matmult.h lapack.h matcom.h
matcom.o: matcom.h
timing.o: timing.h

mmultest: $(objects2)
mmultest.o: matmult.h matcom.h
matmult.o: matmult.h lapack.h matcom.h 
matcom.o: matcom.h

mfactime: $(objects3)
mfactime.o: lufact.h cholfact.h modchol.h ldltfact.h matcom.h timing.h
lufact.o: lufact.h lapack.h matcom.h 
cholfact.o: cholfact.h lapack.h matcom.h timing.h
modchol.o: modchol.h ldltfact.h lapack.h matcom.h timing.h
ldltfact.o: ldltfact.h lapack.h matcom.h timing.h
matcom.o: matcom.h
timing.o: timing.h

mfactest: $(objects4)
mfactest.o: lufact.h cholfact.h modchol.h ldltfact.h matcom.h
lufact.o: lufact.h lapack.h matcom.h 
cholfact.o: cholfact.h lapack.h matcom.h timing.h
modchol.o: modchol.h ldltfact.h lapack.h matcom.h timing.h
ldltfact.o: ldltfact.h lapack.h matcom.h timing.h
matcom.o: matcom.h
timing.o: timing.h

# Pattern rules
%: %.o
	$(CC) $^ $(LDFLAGS) $(LIBS) -o $@
%.o: %.c
	$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@

.PHONY:	cleanall cleanobj
cleanall: cleanobj
	rm -f mmultime mmultest mfactime mfactest
cleanobj: 
	rm -f *.o
clean1: 
	rm -f mmultime $(objects1)
clean2: 
	rm -f mmultime $(objects2)
clean3: 
	rm -f mfactime $(objects3)
clean4: 
	rm -f mfactest $(objects4)
