##******************************************************************************
##                              INTEL CONFIDENTIAL
##  Copyright(C) 2001-2010 Intel Corporation. All Rights Reserved.
##  The source code contained  or  described herein and all documents related to
##  the source code ("Material") are owned by Intel Corporation or its suppliers
##  or licensors.  Title to the  Material remains with  Intel Corporation or its
##  suppliers and licensors. The Material contains trade secrets and proprietary
##  and  confidential  information of  Intel or its suppliers and licensors. The
##  Material  is  protected  by  worldwide  copyright  and trade secret laws and
##  treaty  provisions. No part of the Material may be used, copied, reproduced,
##  modified, published, uploaded, posted, transmitted, distributed or disclosed
##  in any way without Intel's prior express written permission.
##  No license  under any  patent, copyright, trade secret or other intellectual
##  property right is granted to or conferred upon you by disclosure or delivery
##  of the Materials,  either expressly, by implication, inducement, estoppel or
##  otherwise.  Any  license  under  such  intellectual property  rights must be
##  express and approved by Intel in writing.
##
##******************************************************************************
##  Content:
##      Intel(R) Math Kernel Library Fortran-style DFT examples creation and run
##******************************************************************************

help:
	@echo "Usage: make {lib32|so32|lib64|so64|libem64t|soem64t} [function=name]"
	@echo "[compiler=compiler_name] [interface=interface_name] [threading=threading_name]"
	@echo "[parallel=parallel_name] [omp=omp_name]"
	@echo
	@echo "name           - function name. Please see dftf.lst file"
	@echo
	@echo "compiler_name  - can be gnu, pgi or intel. Default value is intel."
	@echo "                 Intel(R) Fortran Compiler as default."
	@echo "                 If compiler=gnu then GNU gfortran compiler will be used."
	@echo "                 Note: GNU g77 compiler is not supported."
	@echo "                 If compiler=pgi then PGI Fortran 95 compiler will be used."
	@echo
	@echo "interface_name - can be lp64 or ilp64 for em64t and ia64. Default value is lp64."
	@echo
	@echo "threading_name - can be parallel or sequential. Default value is parallel."
	@echo "parallel_name  - can be intel, pgi (only if compiler=pgi) or gnu (only if compiler=gnu). Default value is intel."
	@echo "omp_name       - can be guide of iomp5 if parallel=intel or"
	@echo "                 can be iomp5 or gomp if parallel=gnu or"
	@echo "                 can be pgmp if parallel=pgi. Default value is iomp5."
##------------------------------------------------------------------------------
## examples of using:
##
## make lib32 function=complex_1d_double_ex1
##                        - build by Intel(R) Fortran Compiler
##                          (as default) and run COMPLEX_1D_DOUBLE_EX1
##                          example for 32-bit applications, static linking
##
## make so32              - build by Intel(R) Fortran Compiler (as default) and run
##                          all examples of MKL for 32-bit applications, dynamic
##                          linking
##
## make lib64             - build by Intel(R) Fortran Compiler (as default) and run
##                          all examples of MKL for Intel(R) Itanium(R) processor family
##                          applications, static linking
##
## make so64              - build by Intel(R) Fortran Compiler (as default) and run
##                          all examples of MKL for Intel(R) Itanium(R) processor family
##                          applications, dynamic linking
##------------------------------------------------------------------------------

include dftf.lst

ifndef function
   function = $(DFT)
endif

ifndef compiler
   compiler=intel
endif

ifndef interface
   interface=lp64
endif

ifndef threading
   threading=parallel
endif

ifndef $(gnu_path)
   gnu_path=/usr/lib
endif

RES = $(addsuffix .res ,$(function))

ifeq (,$(filter gnu pgi,$(parallel)))
   override parallel=intel
   ifneq ($(omp),guide)
      override omp=iomp5
   endif
else
   ifeq ($(parallel),gnu)
      ifneq ($(omp),gomp)
         override omp=iomp5
      endif
   else
      override omp=pgmp
   endif
endif

ifndef MKLROOT
   MKLROOT=../..
   MKL_PATH = "$(subst examples/dftf,lib/$(_IA),$(PWD))"
else
   MKL_PATH = "$(MKLROOT)/lib/$(_IA)"
endif

ifeq ($(compiler),gnu)
   FC=gfortran
   ifeq ($(FC),gfortran)
      IFACE_COMP_PART=gf
   else
      IFACE_COMP_PART=intel
   endif
else
   ifeq ($(compiler),pgi)
      FC=pgf95
   else
      override FC=ifort
   endif
   IFACE_COMP_PART=intel
endif

ifeq ($(parallel),gnu)
   IFACE_THREADING_PART=gnu
   GFORTRAN_LIB=-L$(gnu_path) -lgfortran
else
   GFORTRAN_LIB=
   ifeq ($(parallel),pgi)
      IFACE_THREADING_PART=pgi
   else
      IFACE_THREADING_PART=intel
   endif
endif


ifeq ($(interface),ilp64)
   IFACE_LIB=$(MKL_PATH)/libmkl_$(IFACE_COMP_PART)_ilp64.$(EXT)
   FOPTS = -i8
   ifeq ($(compiler),gnu)
      override FC=gfortran
      FOPTS=-fdefault-integer-8
   endif
else
   IFACE_LIB=$(MKL_PATH)/libmkl_$(IFACE_COMP_PART)_lp64.$(EXT)
   FOPTS =
endif

ifeq ($(_IA),32)
   ifeq ($(compiler),intel)
      SPEC_OPT=-xK
#This option is required by Intel(R) 11.0 compiler to produce workable binaries for Pentium(R) III.
#If you don't need it, you can remove this option.
   endif
   IFACE_LIB=$(MKL_PATH)/libmkl_$(IFACE_COMP_PART).$(EXT)
   FOPTS =
endif

ifeq ($(threading),sequential)
   THREADING_LIB=$(MKL_PATH)/libmkl_sequential.$(EXT)
   OMP_LIB =
else
   THREADING_LIB=$(MKL_PATH)/libmkl_$(IFACE_THREADING_PART)_thread.$(EXT)
   OMP_LIB = -L$(MKL_PATH) -liomp5
endif

CORE_LIB=$(MKL_PATH)/libmkl_core.$(EXT)

MKL_LIBS=$(IFACE_LIB) -Wl,--start-group $(THREADING_LIB) $(CORE_LIB) -Wl,--end-group $(OMP_LIB)

ifeq ($(_IA),32)
   RES_DIR=_results/$(compiler)_$(threading)_$(_IA)_$(RES_EXT)$Z
else
   RES_DIR=_results/$(compiler)_$(interface)_$(threading)_$(_IA)_$(RES_EXT)$Z
endif

ifeq ($(compiler)-$(IFACE_THREADING_PART),pgi-pgi)
   FOPTS += -mp
endif

ifeq ($(compiler),pgi)
   FOPTS += -Mnokeepobj
endif

ifeq ($(compiler),intel)
   FOPTS += -vec-report0
endif

COMMON = $(SUPPORT:%=$(RES_DIR)/%.o)

#-------------------------------------------------------------------------------

vpath %.f90 $(MKLROOT)/include
vpath %.f90 source

lib32:
	$(MAKE) lib _IA=32 EXT=a RES_EXT=lib
so32:
	$(MAKE) lib _IA=32 EXT=so RES_EXT=so
libem64t:
	$(MAKE) lib _IA=em64t EXT=a RES_EXT=lib
soem64t:
	$(MAKE) lib _IA=em64t EXT=so RES_EXT=so
lib64:
	$(MAKE) lib _IA=64 EXT=a RES_EXT=lib
so64:
	$(MAKE) lib _IA=64 EXT=so RES_EXT=so

lib: del_obj $(COMMON) $(RES)

$(RES_DIR)/%.o: %.f90
	$(FC) $(SPEC_OPT) $(FOPTS) -w -c $< -o $@

$(RES): %.res: %.f90
	$(FC) $(SPEC_OPT) $(FOPTS) -w $(COMMON) $< -L$(MKL_PATH) $(MKL_LIBS) -lpthread -o $(RES_DIR)/$*.out
	export LD_LIBRARY_PATH=$(MKL_PATH):$(LD_LIBRARY_PATH); $(RES_DIR)/$*.out <data/$*.d >$(RES_DIR)/$@

del_obj:
	-rm -fr $(RES_DIR)/*.o $(RES_DIR)/*.mod
	mkdir -p ./$(RES_DIR)
#-------------------------------------------------------------------------------
