# ICC compiler
#CC =icc
#CFLAGS=-restrict -unroll4 -ipo -falign-loops=256 -O3 -march=native $(shell pkg-config --cflags starpu-1.3)
# GCC compiler
CC=gcc
NVCC=nvcc
ENABLE_CUDA=no
LD=$(CC)

ifeq ($(ENABLE_CUDA),yes)
        LD := ${NVCC}
endif

CFLAGS = -O3 -g $(shell pkg-config --cflags starpu-1.3)
CPU_CFLAGS = ${CFLAGS} -Wall -mavx -fomit-frame-pointer -march=native -ffast-math
CUDA_CFLAGS = ${CFLAGS}
LDFLAGS +=$(shell pkg-config --libs starpu-1.3)

EXTERNLIB=extern_tasks.so
GENERATEDLIB=generated_tasks.so

C_OBJECTS=$(patsubst %.c,%.o,$(wildcard gen*.c))
CUDA_OBJECTS=$(patsubst %.cu,%.o,$(wildcard gen*.cu))
ifneq ($(ENABLE_CUDA),yes)
	CUDA_OBJECTS:=
endif

all: ${EXTERNLIB}

mult: mult.c cpu_mult.o #gpu_mult.o
	$(CC) $(CPU_CFLAGS) $^ -o $@ $(LDFLAGS)

%.o: %.c
	$(CC) -c -fPIC $(CPU_CFLAGS) $^ -o $@

%.o: %.cu
	$(NVCC) -dc $(CUDA_CFLAGS) $^ --shared --compiler-options '-fPIC' -o $@ $(LDFLAGS)

${EXTERNLIB}: cpu_mult.c
	$(CC) $(CFLAGS) -shared -fPIC $(LDFLAGS) $^ -o $@

${GENERATEDLIB}: $(C_OBJECTS) $(CUDA_OBJECTS)
	$(LD) -shared $(LDFLAGS) $^ -o $@

.PHONY: clean

clean:
	rm -f mult *.so *.o genc_*.c gencuda_*.cu *.dat

tjulia: julia_generatedc.dat
# Performance Tests
STRIDE=72
cstarpu.dat: mult
	STARPU_NOPENCL=0 STARPU_SCHED=dmda STARPU_CALIBRATE=1 ./mult $(STRIDE) > $@
julia_generatedc.dat: mult.jl
	STARPU_NOPENCL=0 STARPU_SCHED=dmda STARPU_CALIBRATE=1 julia mult.jl $(STRIDE) $@
julia_native.dat: mult_native.jl
	STARPU_NOPENCL=0 STARPU_SCHED=dmda STARPU_CALIBRATE=1 julia mult_native.jl $(STRIDE) $@
julia_calllib.dat: ${EXTERNLIB} mult.jl
	JULIA_TASK_LIB="${EXTERNLIB}" STARPU_NOPENCL=0 STARPU_SCHED=dmda STARPU_CALIBRATE=1 julia mult.jl $(STRIDE) julia_calllib.dat

test: cstarpu.dat julia_generatedc.dat julia_native.dat julia_calllib.dat
