### Global, generic variables ###
SHELL     = /bin/bash
ARCH      = $(shell uname -m)
GCC       = gcc-4.3.1
#GCCFLAGS  = -Wall -march=native -mfpmath=sse,387 -O3 -fomit-frame-pointer -fkeep-inline-functions -funsafe-loop-optimizations -freorder-blocks-and-partition -fno-math-errno -ffinite-math-only -fno-trapping-math -fno-signaling-nans -fwhole-program --param l1-cache-line-size=1 --param l1-cache-size=64 --param l2-cache-size=4096
GCCFLAGS  = -Wall -march=native -O3 

# For ICC 
# on Opteron: -xW
#ICCFLAGS    = -xW
# on Core2 Duo -xT
ICCFLAGS    = -xT 

LIBS      = -lm -lblas -lcblas
LDFLAGS   = $(LIBS)

### TAU specific variables ###
TAU_MAKEFILE = ~/TAU/TAU/$(ARCH)/lib/Makefile.tau-pdt
TAU_CXX      = tau_cxx.sh
TAU_CC       = tau_cc.sh
TAU_OPTS     = -optNoRevert -optLinking="$(LIBS)" -optTauCC="$(CC)" -optCPPOpts="$(GCCFLAGS)" -tau_makefile=$(TAU_MAKEFILE)

PROGNAM    = EuclideanDist
PROGRAM    = $(PROGNAM)               # nom de l'executable
PROGOUT    = $(PROGNAM)_$(ARCH)
TAU_PROG   = $(PROGOUT)_TAU
SRCS       = $(PROGNAM).c             # les fichiers source
OBJS       = $(PROGNAM).o             # fichiers objets

MKL_LIBS = -liomp5 -lpthread -I/opt/intel/mkl/10.0.3.020/include/ -L/opt/intel/mkl/10.0.3.020/lib/em64t/

.SUFFIXES: .c .o
.cpp.o:
	$(CXX) -c $(GCCFLAGS) $<
.c.o:
	$(CC) -c $(GCCFLAGS) $<

# Targets
default: all
# all: $(PROGRAM) icc gcc
all: $(PROGRAM) icc gcc tests plots
$(PROGRAM):
	$(GCC) $(SRCS) -o $(PROGOUT) $(LDFLAGS)

tau:
	$(TAU_CC) $(TAU_OPTS) $(SRCS) -o $(TAU_PROG)

set_mkl_blas:
	sudo eselect blas set mkl-gfortran

set_goto_blas:
	sudo eselect blas set goto

icc:
	icc $(MKL_LIBS) $(LIBS) $(ICCFLAGS) $(SRCS) -o $(PROGOUT)_ICC

gcc:
	$(GCC) $(LIBS) $(GCCFLAGS) $(SRCS) -o $(PROGOUT)_GCC
	
clean:
	/bin/rm -f $(OBJS) $(PROGRAM) $(TAU_PROG) *.dat

tests: icctest gcctest

icctest:
	./$(PROGOUT)_ICC     > icc.dat

gcctest:
	./$(PROGOUT)_GCC     > gcc.dat

plots:
	gnuplot Plot.gp
