##******************************************************************************
##                              INTEL CONFIDENTIAL
##  Copyright(C) 2006-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:
##      Wrapper library building.
##      This library allows to use MKL DFTI routines through FFTW interface.
##******************************************************************************

help:
	@echo "Usage: make {lib32|lib64|libem64t} [precision=MKL_DOUBLE|MKL_SINGLE]"
	@echo "[function=name] [compiler=compiler_name] [threading=threading_name]"
	@echo "[parallel=parallel_name] [omp=omp_name]"
	@echo
	@echo "name           - function name. Please see fftw2xf.lst file"
	@echo
	@echo "compiler_name  - can be gnu or intel, or even pgi. Default value is intel."
	@echo "                 Intel(R) Fortran Compiler as default,"
	@echo "                 Intel(R) C Compiler will be used to build wrappers library."
	@echo "                 If compiler=gnu then GNU gfortran compiler will be used."
	@echo "                 To use g77 add FC=g77."
	@echo "parallel_name  - can be intel, pgi (only if compiler=pgi) or gnu (if compiler=gnu). Default value is intel."
	@echo "omp_name       - can be guide or iomp5 if parallel=intel or"
	@echo "                 can be iomp5 or gomp if parallel=gnu or"
	@echo "                 can be pgmp if parallel=pgi."
	@echo "                 Default value is iomp5."
	@echo "threading_name - can be parallel or sequential. Default value is parallel."
	@echo
	@echo "precision=MKL_DOUBLE - for double precision data, default."
	@echo "precision=MKL_SINGLE - for single precision data."

##------------------------------------------------------------------------------
## examples of using:
##
## make lib64 compiler=gnu
##                    - compile by GNU Fortran compiler for double precision data and build library for
##                      Intel(R) Itanium(R)-based applications
##
## make lib64 precision=MKL_SINGLE compiler=gnu
##                    - compile by GNU Fortran compiler for single precision data and build library for
##                      Intel(R) Itanium(R)-based applications
##
## make libem64t
##                    - compile by Intel(R) Fortran Compiler and build library for Intel(R) EM64T processor
##                      family applications
##------------------------------------------------------------------------------

include fftw2xf.lst

ifneq ($(precision),MKL_SINGLE)
   override precision = MKL_DOUBLE
   T = _double
else
   T = _single
endif

ifndef function
   function = $(DFT_$(precision))
endif

ifeq (,$(filter gnu pgi,$(compiler)))
   override compiler=intel
   override parallel=intel
endif

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 threading
   threading = parallel
endif

COMMON = source/mkl_fftw_examples_support.f

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

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

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

INSTALL_DIR=$(PWD)

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 ($(FC),gfortran)
   FCOPTS = -w -D$(precision) -I"$(MKLROOT)"/include -I"$(MKLROOT)"/include/fftw -I./source -fsecond-underscore
else
   FCOPTS = -w -D$(precision) -I"$(MKLROOT)"/include -I"$(MKLROOT)"/include/fftw
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)
else
   IFACE_LIB=$(MKL_PATH)/libmkl_$(IFACE_COMP_PART)_lp64.$(EXT)
endif

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

ifeq ($(threading),sequential)
   OMP_LIB =
   GFORTRAN_LIB=
else
   ifeq ($(parallel),pgi)
      FOPTS += -mp
   endif
   ifeq ($(omp),gomp)
      OMP_LIB = -L$(gnu_path) -l$(omp)
   else
      ifeq ($(omp),pgmp)
        OMP_LIB =
      else
        OMP_LIB = -L$(MKL_PATH) -l$(omp)
      endif
   endif
endif

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

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

RES_DIR=_results/$(compiler)_$(threading)_$(_IA)_$(RES_EXT)$T$Z

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

vpath %.f source
vpath %.o $(RES_DIR)

lib32:
	$(MAKE) wrap_lib _IA=32
	$(MAKE) $(RES) _IA=32 EXT=a RES_EXT=lib
libem64t:
	$(MAKE) wrap_lib _IA=em64t
	$(MAKE) $(RES) _IA=em64t EXT=a RES_EXT=lib
lib64:
	$(MAKE) wrap_lib _IA=64
	$(MAKE) $(RES) _IA=64 EXT=a RES_EXT=lib

wrap_lib:
	cd "$(MKLROOT)"/interfaces/fftw2xf && $(MAKE) lib$(_IA) PRECISION=$(precision) compiler=$(compiler) INSTALL_DIR=$(INSTALL_DIR)/lib/$(_IA)

$(RES): %.res: %.f
	mkdir -p ./$(RES_DIR)
	$(FC) $(SPEC_OPT) $(FCOPTS) $(FOPTS) $< $(COMMON) -L$(INSTALL_DIR)/lib/$(_IA) -lfftw2xf_$(compiler) -L$(MKL_PATH) $(MKL_LIBS) -lpthread -lm -o $(RES_DIR)/$*.out
	export LD_LIBRARY_PATH=$(MKL_PATH):"$(LD_LIBRARY_PATH)"; $(RES_DIR)/$*.out >$(RES_DIR)/$@
#-------------------------------------------------------------------------------
