##******************************************************************************
##                              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 C-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 "[omp=omp_name] [parallel=parallel_name]"
	@echo
	@echo "name           - function name. Please see dftc.lst file"
	@echo
	@echo "compiler_name  - can be gnu, pgi or intel. Default value is intel."
	@echo "                 Intel(R) C Compiler as default"
	@echo
	@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
	@echo "interface_name - can be lp64 or ilp64 for em64t and ia64. Default value is lp64."
	@echo
	@echo "parallel_name  - can be intel (default value), pgi (only if compiler=pgi) or"
	@echo "                 gnu (if compiler=gnu)."
	@echo
	@echo "threading_name - can be parallel or sequential. Default value is parallel."

##------------------------------------------------------------------------------
## examples of using:
##
## make lib32 function=complex_1d_single_ex1
##                             - build by Intel(R) C Compiler (as default) and
##                               run COMPLEX_1D_SINGLE_EX1 example for 32-bit
##                               applications, static linking
##
## make so32 compiler=gnu      - build by GNU C compiler and run all examples
##                               of MKL for 32-bit applications, dynamic linking
##
## make lib64 compiler=gnu     - build by GNU C compiler and run all examples
##                               of MKL for Intel(R) Itanium(R) processor family
##                               applications, static linking
##
## make so64                   - build by Intel(R) C Compiler (as default) and run
##                               run all examples of MKL for Intel(R) Itanium(R)
##                               processor family applications, dynamic linking
##------------------------------------------------------------------------------

include dftc.lst

ifndef function
   function = $(DFT)
endif

ifndef compiler
   compiler=intel
endif

ifndef interface
   interface=lp64
endif

ifndef threading
   threading=parallel
endif

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

COMMON = source/dfti_example_support.c source/dfti_example_status_print.c

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

ifndef MKLROOT
   MKLROOT=../..
   MKL_PATH = "$(subst examples/dftc,lib/$(_IA),$(PWD))"
else
   MKL_PATH = "$(MKLROOT)/lib/$(_IA)"
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

ifeq ($(compiler),gnu)
   CC=gcc
   IFACE_COMP_PART=intel
else
   ifeq ($(compiler),pgi)
      CC=pgcc
   else
      override CC=icc
   endif
   IFACE_COMP_PART=intel
endif

ifeq ($(parallel),gnu)
   IFACE_THREADING_PART=gnu
else
   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)
   COPTS = -DMKL_ILP64
else
   IFACE_LIB=$(MKL_PATH)/libmkl_$(IFACE_COMP_PART)_lp64.$(EXT)
   COPTS =
endif

ifeq ($(_IA),32)
   ifeq ($(compiler),intel)
      SPEC_OPT=-xK -O0
#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)
   COPTS =
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) -l$(omp)
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)
   COPTS += -mp -pgf90libs
endif

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

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

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

vpath %.c source

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

$(RES): %.res: %.c
	mkdir -p ./$(RES_DIR)
	$(CC) $(SPEC_OPT) $(COPTS) -w -I$(MKLROOT)/include $< $(COMMON) -L$(MKL_PATH) $(MKL_LIBS) -lpthread -lm -o $(RES_DIR)/$*.out
	export LD_LIBRARY_PATH=$(MKL_PATH):$(LD_LIBRARY_PATH); $(RES_DIR)/$*.out data/$*.d >$(RES_DIR)/$@
#-------------------------------------------------------------------------------
