##******************************************************************************
##                              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 fftw2xc.lst file."
	@echo
	@echo "compiler_name  - can be gnu, pgi or intel. Default value is intel."
	@echo "                 Intel (R) C Compiler as default."
	@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 "threading_name - can be parallel or sequential. Default value is parallel."
	@echo
	@echo "parallel_name  - can be intel or pgi (only if compiler=pgi). Default value is intel. "
	@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 C 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 C compiler for single precision data and build library for
##                     Intel(R) Itanium(R)-based applications
##
## make libem64t
##                   - compile by Intel(R) C Compiler and build library for Intel(R) EM64T processor
##                     family applications
##------------------------------------------------------------------------------

include fftw2xc.lst

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

ifndef function
   function = $(DFT)
endif

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

ifndef threading
   threading = parallel
endif

COMMON = source/dfti_example_support.c

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

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

INSTALL_DIR=$(PWD)

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

ifeq (,$(filter 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 ($(parallel),pgi)
   IFACE_THREADING_PART=pgi
else
   IFACE_THREADING_PART=intel
endif

IFACE_COMP_PART=intel

COPTS = -w -D$(precision) -I"$(MKLROOT)"/include -I"$(MKLROOT)"/include/fftw

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 ($(compiler)-$(IFACE_THREADING_PART),pgi-pgi)
   COPTS += -mp
endif

ifeq ($(compiler),pgi)
   COPTS += -Mnokeepobj
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 %.c 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/fftw2xc && $(MAKE) lib$(_IA) PRECISION=$(precision) compiler=$(compiler) INSTALL_DIR=$(INSTALL_DIR)/lib/$(_IA)

$(RES): %.res: %.c
	mkdir -p ./$(RES_DIR)
	$(CC) $(SPEC_OPT) $< $(COPTS) $(COMMON) -L"$(INSTALL_DIR)/lib/$(_IA)" -lfftw2xc_$(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)/$@
#-------------------------------------------------------------------------------
