#
#Copyright (2011) Intel Corporation All Rights Reserved.
#
#The source code, information and material ("Material") contained
#herein is owned by Intel Corporation or its suppliers or licensors,
#and title to such Material remains with Intel Corporation or its
#suppliers or licensors. The Material contains proprietary information
#of Intel or its suppliers and licensors. The Material is protected by
#worldwide copyright 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 or other intellectual property rights in the
#Material is granted to or conferred upon you, either expressly, by
#implication, inducement, estoppel or otherwise. Any license under such
#intellectual property rights must be express and approved by Intel in
#writing.
#
#Unless otherwise agreed by Intel in writing, you may not remove or alter
#this notice or any other notice embedded in Materials by Intel or Intel's
#suppliers or licensors in any way.
#

ICC ?= icc
MPIICC ?= mpiicc -cc=$(ICC)
TARGET_ICC ?= $(ICC)
INSTALL ?= install
VERSION ?= 0.0.0
prefix ?= /usr
exec_prefix ?= $(prefix)
libexecdir ?= $(exec_prefix)/libexec
datarootdir ?= $(prefix)/share
docdir ?= $(datarootdir)/doc/micperf
srcinstalldir ?= $(prefix)/src/micperf

EXTRA_CFLAGS = -O3 -qopenmp -DSTREAM_ARRAY_SIZE=64000000 \
-qopt-streaming-stores always -restrict -fstack-protector -D_FORTIFY_SOURCE=2

EXTRA_LDFLAGS = -qopenmp -Wl,-z,relro,-z,now

all: knc

knc: stream_mic

stream: stream.c stream_mpi.c
stream_mpi: stream_mpi.c

knlsb: stream stream_mpi
knllb: stream_mic

# Optimize KNL binaries to use AVX512 and KNC card binaries for the k1om architecture
knc: MIC_CFLAGS = -mmic
knlsb: CPU_CFLAGS = -axMIC-AVX512
knllb: MIC_CFLAGS = -axMIC-AVX512

install: install-knc

install-knc: install-k1om install-license

install-knlsb: install-license knlsb
	# on a system where the micperf rpm is installed, sources will cause $(INSTALL) to fail, error can be suppressed.
	$(INSTALL) -d $(DESTDIR)$(libexecdir)/micperf/x86_64_AVX512
	$(INSTALL) stream $(DESTDIR)$(libexecdir)/micperf/x86_64_AVX512
	$(INSTALL) stream_mpi $(DESTDIR)$(libexecdir)/micperf/x86_64_AVX512

install-knllb: install-x86_64-avx512 install-license

stream_mic: stream.c
	$(TARGET_ICC) $(MIC_CFLAGS) $(CPPFLAGS) $(CFLAGS) $(EXTRA_CFLAGS) $< \
		$(LDFLAGS) $(EXTRA_LDFLAGS) -o $@

stream:
	$(ICC) $(CPU_CFLAGS) $(CPPFLAGS) $(CFLAGS) $(EXTRA_CFLAGS) $< \
		$(LDFLAGS) $(EXTRA_LDFLAGS) -o $@
stream_mpi:
	$(MPIICC) $(CPU_CFLAGS) $(CPPFLAGS) $(CFLAGS) $(EXTRA_CFLAGS) $< \
                $(LDFLAGS) $(EXTRA_LDFLAGS) -o $@

install-k1om:
	$(INSTALL) -d $(DESTDIR)$(libexecdir)/micperf/k1om
	$(INSTALL) stream_mic $(DESTDIR)$(libexecdir)/micperf/k1om

install-x86_64-avx512:
	$(INSTALL) -d $(DESTDIR)$(libexecdir)/micperf/x86_64_AVX512
	$(INSTALL) stream_mic $(DESTDIR)$(libexecdir)/micperf/x86_64_AVX512

install-license:
	$(INSTALL) -d $(DESTDIR)$(docdir)
	$(INSTALL) -m 644 license_stream.txt $(DESTDIR)$(docdir)

clean:
	-rm -rf stream_mic
	-rm -rf stream stream_mpi

.PHONY: all knc knlsb knllb install install-knc install-knlsb install-knllb clean install-k1om install-license
