##########################################################################################################################################################
#
#
#
#  Makefile for compiling the Genetic algorithm package for MAGIX
#  Copyright (C) 2009 - 2016  Thomas Moeller
#
#  I. Physikalisches Institut, University of Cologne
#
#
#
#  Makefile options:    - "defaultsmp":         builds the default MAGIX version using SMP
#
#                                               Used files:     ../lib/general/Module_Variables.f90
#                                                               ../lib/general/Module_FunctionCalling.f90
#                                                               ../lib/default/Module_Default__SMP.f90
#                                                               ../lib/starter/SMP/SMP__Starter__Program.f90
#                                                               src/Genetic__Core.f90
#
#
#                       - "defaultmpi":         builds the default MAGIX version using MPI
#
#                                               Used files:     ../lib/general/Module_Variables.f90
#                                                               ../lib/general/Module_FunctionCalling.f90
#                                                               ../lib/default/Module_Default__MPI.f90
#                                                               ../lib/starter/MPI/MPI__Starter__Program.f90
#                                                               src/Genetic__Core.f90
#
#
#                       - "myxclasssmp":        builds the myXCLASS optimized MAGIX version using SMP
#
#                                               Used files:     ../lib/general/Module_Variables.f90
#                                                               ../lib/general/Module_FunctionCalling.f90
#                                                               ../lib/myXCLASS/Module_myXCLASS__Core.f90
#                                                               ../lib/myXCLASS/Module_myXCLASS__SMP.f90
#                                                               ../lib/starter/SMP/SMP__Starter__Program.f90
#                                                               src/Genetic__Core.f90
#
#
#                       - "myxclassmpi":        builds the myXCLASS optimized MAGIX version using MPI
#
#                                               Used files:     ../lib/general/Module_Variables.f90
#                                                               ../lib/general/Module_FunctionCalling.f90
#                                                               ../lib/myXCLASS/Module_myXCLASS__Core.f90
#                                                               ../lib/myXCLASS/Module_myXCLASS__MPI.f90
#                                                               ../lib/starter/MPI/MPI__Starter__Program.f90
#                                                               src/Genetic__Core.f90
#
#
#                       - "myxclassgpu":        builds the myXCLASS optimized MAGIX version using GPU (CUDA)
#
#                                               Used files:     ../lib/general/Module_Variables.f90
#                                                               ../lib/general/Module_FunctionCalling.f90
#                                                               ../lib/default/Module_myXCLASS__GPU.f90
#                                                               ../lib/starter/SMP/myXCLASS__SMP__Starter__Program.f90
#                                                               src/Genetic__Core.f90
#
#
#  Versions of the program:
#
#  Who           When        What
#
#  T. Moeller    07.09.2009  Initial version
#  T. Moeller    31.08.2014  modified for myXCLASS and GPU optimization
#  T. Moeller    05.09.2014  add MPI version
#
#
#
#  License:
#
#    GNU GENERAL PUBLIC LICENSE
#    Version 3, 29 June 2007
#    (Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>)
#
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##########################################################################################################################################################


#=========================================================================================================================================================
# The directories to find the various pieces


# standard MAGIX directories
bindir = bin


# settings for all MAGIX versions
coresrcdir = src
libdir = ../lib/general
starterlibdir = ../lib/starter


# settings for MAGIX default version
defaultsrcdir = src
defaultlibdir = ../lib/default


# settings for myXCLASS optimized versions of MAGIX
myxclasssrcdir = src
myxclasslibdir = ../lib/myXCLASS
myxclassdbdir = ../lib/myXCLASS/sqlite


#---------------------------------------------------------------------------------------------------------------------------------------------------------
# suffixes' list
.SUFFIXES:
.SUFFIXES: .o .f90


#=========================================================================================================================================================
# Various compilation programs and flags. You need to make sure these are correct for your system
SHELL = /bin/bash


#=========================================================================================================================================================
# specify name of algorithm
NameAlgorithm = Genetic


#=========================================================================================================================================================
# define source and objects files
#=========================================================================================================================================================


#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# general module files (for all MAGIX versions)
GeneralFSRC = $(libdir)/Module_Variables.f90 $(libdir)/Module_FunctionCalling.f90           ## general fortran module files
GeneralFOBJ = $(libdir)/Module_Variables.o $(libdir)/Module_FunctionCalling.o               ## general fortran object files
MFIL = *.mod                                                                                ## file name extension for fortran module files


#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
## general (core) algorithm file
CoreFSRC = $(coresrcdir)/$(NameAlgorithm)__Core.f90                                         ## core fortran source file
CoreFOBJ = $(coresrcdir)/$(NameAlgorithm)__Core.o                                           ## core fortran object file


# starter file for default MAGIX version (SMP version)
StarterSMPFSRC = $(starterlibdir)/SMP/SMP__Starter__Program.f90                             ## default fortran source file
StarterSMPFOBJ = $(starterlibdir)/SMP/SMP__Starter__Program.o                               ## default fortran object file


# MPI starter program for default MAGIX version (MPI version)
StarterMPIFSRC = $(starterlibdir)/MPI/MPI__Starter__Program.f90
StarterMPIFOBJ = $(starterlibdir)/MPI/MPI__Starter__Program.o


# GPU starter program for myXCLASS optimized version of MAGIX (GPU version)
# (use SMP starter because starter is the same for GPU version)
StarterGPUFSRC = $(starterlibdir)/SMP/SMP__Starter__Program.f90
StarterGPUFOBJ = $(starterlibdir)/SMP/SMP__Starter__Program.o


#=========================================================================================================================================================
#=========================================================================================================================================================
# DEFAULT MAGIX version
#=========================================================================================================================================================
#=========================================================================================================================================================


#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# default SMP MAGIX version
DEFAULTEXECSMP = $(NameAlgorithm)__SMP-Starter.exe                                          ## final binary file (default SMP version)


# default module model file (SMP version)
DefaultModelSMPFSRC = $(defaultlibdir)/Module_Default__SMP.f90
DefaultModelSMPFOBJ = $(defaultlibdir)/Module_Default__SMP.o


#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# default MPI MAGIX version
DEFAULTEXECMPI = $(NameAlgorithm)__MPI-Starter.exe                                          ## final binary file (default MPI version)


# default module model file (MPI version)
DefaultModelMPIFSRC = $(defaultlibdir)/Module_Default__MPI.f90
DefaultModelMPIFOBJ = $(defaultlibdir)/Module_Default__MPI.o


#=========================================================================================================================================================
#=========================================================================================================================================================
# myXCLASS optimized MAGIX version
#=========================================================================================================================================================
#=========================================================================================================================================================


# myXCLASS sqite files
myXCLASSCSRC = $(myxclassdbdir)/sqlite3.c $(myxclassdbdir)/sqlite3_NumberEntries.c $(myxclassdbdir)/sqlite3_PartitionFunction.c \
               $(myxclassdbdir)/sqlite3_RadTrans.c
myXCLASSCOBJ = $(myxclassdbdir)/sqlite3.o $(myxclassdbdir)/sqlite3_NumberEntries.o $(myxclassdbdir)/sqlite3_PartitionFunction.o \
               $(myxclassdbdir)/sqlite3_RadTrans.o


#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# myXCLASS MAGIX version (SMP)
myXCLASSEXECSMP = $(NameAlgorithm)__SMP-Starter__myXCLASS.exe                               ## final binary file (myXCLASS SMP version)


# myXCLASS module file (SMP)
myXCLASSModelSMPFSRC = $(myxclasslibdir)/Module_myXCLASS__Core.f90 $(myxclasslibdir)/Module_myXCLASS__SMP.f90
myXCLASSModelSMPFOBJ = $(myxclasslibdir)/Module_myXCLASS__Core.o $(myxclasslibdir)/Module_myXCLASS__SMP.o


#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# myXCLASS MAGIX version (MPI)
myXCLASSEXECMPI = $(NameAlgorithm)__MPI-Starter__myXCLASS.exe                               ## final binary file (myXCLASS MPI version)


# myXCLASS module file (MPI)
myXCLASSModelMPIFSRC = $(myxclasslibdir)/Module_myXCLASS__Core.f90 $(myxclasslibdir)/Module_myXCLASS__MPI.f90
myXCLASSModelMPIFOBJ = $(myxclasslibdir)/Module_myXCLASS__Core.o $(myxclasslibdir)/Module_myXCLASS__MPI.o


#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# myXCLASS MAGIX version (GPU)
myXCLASSEXECGPU = $(NameAlgorithm)__SMP-Starter__myXCLASS.exe                               ## final binary file (myXCLASS GPU version)


# myXCLASS module file (GPU)
myXCLASSModelGPUFSRC = $(myxclasslibdir)/Module_myXCLASS__GPU.f90
myXCLASSModelGPUFOBJ = $(myxclasslibdir)/Module_myXCLASS__GPU.o
#---------------------------------------------------------------------------------------------------------------------------------------------------------



#---------------------------------------------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------------------------------------------
# define compiler and correpsonding flags
LIBFORTRAN = -ldl -lpthread -lgomp
INCLUDE = -I$(myxclassdbdir)


# set GNU Compiler and flags
FC = gfortran
#FFLAGSGNU = -g -O0 -fbacktrace -fopenmp -ffree-form -ffree-line-length-none -fbounds-check -frange-check  -Wall -Wextra
FFLAGSGNU = -O3 -fopenmp -ffree-form -ffree-line-length-none -fbounds-check -frange-check
FFLAGSLIBGNU = $(FFLAGSGNU) $(LIBFORTRAN)


# set mpi compiler
FCMPI = mpif90
FFLAGSMPI = $(FFLAGSGNU)


# set PGI Compiler and flags
FCPGI = pgf90
FFLAGSGPU = -O4 -mp -Mbounds -Mcuda=fastmath,nofma -r4 -Mconcur=allcores
#FFLAGSGPU = -g -O3 -mp -Mbounds -Mcuda=emu
#FFLAGSGPU = -fast -mp -Mbounds -Mcuda=fastmath
FFLAGSGPU = -fast -Mipa=fast,inline -Mconcur -mp -Mprefetch -Mcuda=rdc
FFLAGSLIBGPU = $(FFLAGSGPU) $(LIBFORTRAN)


# set Intel compiler and flags (NOT tested !!!)
# FC = ifort
# FFLAGSINTEL = -O3 -openmp -shared-intel -r8 -traceback -fstack-security-check -fp-stack-check -W1 -override-limits
# FFLAGSLIBINTEL = $(FFLAGSINTEL) -liomp5 $(LIBFORTRAN)


# C compiler. Here: GCC.
CC = gcc
CFLAGS = -O2 $(INCLUDE)
LDLIBSDB = -ldl -lpthread -lgomp


##--------------------------------------------------------------------------------------------------------------------------------------------------------
# make the binaries and clean up the directory
both: cleandefault buildgnusmpdefault cleanmyxclass sqlite buildmyxclasssmp cleanpast move
bothgpu: cleandefault buildgnusmpdefault cleanmyxclass sqlite buildmyxclassgpu cleanpast move


# options for default MAGIX version
defaultsmp: cleandefault buildgnusmpdefault cleanpast move
defaultmpi: cleandefault buildgnumpidefault cleanpast move


# options for myXCLASS
myxclasssmp: cleanmyxclass sqlite buildmyxclasssmp cleanpast move
myxclassmpi: cleanmyxclass sqlite buildmyxclassmpi cleanpast move
myxclassgpu: cleanmyxclass sqlite buildmyxclassgpu cleanpast move


#=========================================================================================================================================================
# build up source code including Module files


#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# build default version (SMP version)
buildgnusmpdefault:
	$(FC) $(FFLAGSLIBGNU) $(GeneralFSRC) $(DefaultModelSMPFSRC) $(CoreFSRC) $(StarterSMPFSRC) -o $(DEFAULTEXECSMP)
#buildinteldefault:
#	$(FC) $(FFLAGSLIBINTEL) $(GeneralFSRC) $(DefaultModelSMPFSRC) $(CoreFSRC) $(StarterSMPFSRC) -o $(DEFAULTEXECSMP)


#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# build default version (MPI version)
buildgnumpidefault:
	$(FCMPI) $(FFLAGSMPI) $(GeneralFSRC) $(DefaultModelMPIFSRC) $(CoreFSRC) $(StarterMPIFSRC) -o $(DEFAULTEXECMPI)


#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# general options for myXCLASS optimized MAGIX version
CDIR = $(myxclassdbdir)
CSRC = sqlite3.c sqlite3_NumberEntries.c sqlite3_PartitionFunction.c sqlite3_RadTrans.c
COBJ = $(CSRC:.c=.o)
.c.o: $(CDIR)/$(CSRC) Makefile
	$(CC) -c $(CFLAGS) $<
sqlite:
	$(CC) -O2 -c -I$(myxclassdbdir) $(myXCLASSCSRC)


# build myXCLASS optimized MAGIX version (SMP version)
buildmyxclasssmp:
	$(FC) $(FFLAGSMPI) $(INCLUDE) $(COBJ) $(GeneralFSRC) $(myXCLASSModelSMPFSRC) $(CoreFSRC) $(StarterSMPFSRC) -o $(myXCLASSEXECSMP) $(LIBFORTRAN)


# build myXCLASS optimized MAGIX version (MPI version)
buildmyxclassmpi:
	$(FCMPI) $(FFLAGSMPI) $(INCLUDE) $(COBJ) $(GeneralFSRC) $(myXCLASSModelMPIFSRC) $(CoreFSRC) $(StarterMPIFSRC) -o $(myXCLASSEXECMPI) $(LIBFORTRAN)


# build myXCLASS optimized MAGIX version (GPU version)
buildmyxclassgpu:
	$(FCPGI) $(FFLAGSLIBGPU) $(INCLUDE) $(COBJ) $(GeneralFSRC) $(myXCLASSModelGPUFSRC) $(CoreFSRC) $(StarterGPUFSRC) -o $(myXCLASSEXECGPU) $(LIBFORTRAN)



##--------------------------------------------------------------------------------------------------------------------------------------------------------
# clean directories and previous compilations
cleandefault:
	rm -f $(MFIL) *.o $(bindir)/$(DEFAULTEXECSMP) $(bindir)/$(DEFAULTEXECMPI)
cleanmyxclass:
	rm -f $(MFIL) *.o $(bindir)/$(myXCLASSEXECSMP) $(bindir)/$(myXCLASSEXECMPI) $(bindir)/$(myXCLASSEXECGPU)
cleanpast:
	rm -f $(MFIL) *.o *.oo


##--------------------------------------------------------------------------------------------------------------------------------------------------------
# move module file to bin directory
move:
	mv *.exe $(bindir)/

