1ROOTDIR = $(abspath $(dir $(firstword $(MAKEFILE_LIST))))
2DEPDIR = ../../..
3SRCDIR = .
4INCDIR = .
5BLDDIR = obj
6OUTDIR = .
7
8CXXFLAGS = $(NULL)
9CFLAGS = $(NULL)
10DFLAGS = $(NULL)
11
12BLAS = 2
13OMP = 1
14SYM = 1
15
16BLAS_STATIC = 0
17
18# include common Makefile artifacts
19include $(DEPDIR)/Makefile.inc
20
21# necessary include directories
22IFLAGS += -I$(call quote,$(INCDIR))
23IFLAGS += -I$(call quote,$(DEPDIR)/include)
24
25ifneq (,$(strip $(wildcard $(LIBNAME).$(SLIBEXT))))
26  DEPSTATIC = 1
27else
28  DEPSTATIC = 0
29endif
30
31XWRAP ?= 0
32ifneq (Darwin,$(UNAME))
33ifneq (0,$(DEPSTATIC))
34  XWRAP = $(GEMM)
35endif
36endif
37
38OUTNAME := $(shell basename "$(ROOTDIR)")
39HEADERS := $(wildcard $(INCDIR)/*.h) $(wildcard $(INCDIR)/*.hpp) $(wildcard $(INCDIR)/*.hxx) $(wildcard $(INCDIR)/*.hh) \
40           $(wildcard $(SRCDIR)/*.h) $(wildcard $(SRCDIR)/*.hpp) $(wildcard $(SRCDIR)/*.hxx) $(wildcard $(SRCDIR)/*.hh) \
41           $(DEPDIR)/include/libxsmm_source.h
42CPPSRCS := $(wildcard $(SRCDIR)/*.cpp)
43CXXSRCS := $(wildcard $(SRCDIR)/*.cxx)
44CCXSRCS := $(wildcard $(SRCDIR)/*.cc)
45CSOURCS := $(wildcard $(SRCDIR)/*.c)
46CPPOBJS := $(patsubst %,$(BLDDIR)/%,$(notdir $(CPPSRCS:.cpp=-cpp.o)))
47CXXOBJS := $(patsubst %,$(BLDDIR)/%,$(notdir $(CXXSRCS:.cxx=-cxx.o)))
48CCXOBJS := $(patsubst %,$(BLDDIR)/%,$(notdir $(CCXSRCS:.cc=-cc.o)))
49COBJCTS := $(patsubst %,$(BLDDIR)/%,$(notdir $(CSOURCS:.c=-c.o)))
50ifneq (,$(strip $(FC)))
51FXXSRCS := $(wildcard $(SRCDIR)/*.f)
52F77SRCS := $(wildcard $(SRCDIR)/*.F)
53F90SRCS := $(wildcard $(SRCDIR)/*.f90) $(wildcard $(SRCDIR)/*.F90)
54FXXOBJS := $(patsubst %,$(BLDDIR)/%,$(notdir $(FXXSRCS:.f=-f.o)))
55F77OBJS := $(patsubst %,$(BLDDIR)/%,$(notdir $(F77SRCS:.F=-f77.o)))
56F90OBJS := $(patsubst %,$(BLDDIR)/%,$(notdir $(F90SRCS:.f90=-f90.o)))
57F90OBJS := $(patsubst %,$(BLDDIR)/%,$(notdir $(F90OBJS:.F90=-f90.o)))
58endif
59SOURCES := $(CPPSRCS) $(CXXSRCS) $(CCXSRCS) $(CSOURCS)
60OBJECTS := $(CPPOBJS) $(CXXOBJS) $(CCXOBJS) $(COBJCTS)
61FTNSRCS := $(FXXSRCS) $(F77SRCS) $(F90SRCS)
62MODULES := $(addsuffix .mod,$(basename $(FTNSRCS))) $(addsuffix .modmic,$(basename $(FTNSRCS)))
63FTNOBJS := $(FXXOBJS) $(F77OBJS) $(F90OBJS)
64XFILES := $(OUTDIR)/autobatch \
65  $(OUTDIR)/dgemm-blas $(OUTDIR)/dgemm-wrap \
66  $(OUTDIR)/dgemv-blas $(OUTDIR)/dgemv-wrap
67
68.PHONY: all
69all: $(XFILES)
70
71.PHONY: compile
72compile: $(OBJECTS) $(FTNOBJS)
73
74ifneq (0,$(XWRAP))
75  ifneq (2,$(XWRAP))
76    WRAP_GEMM = -Wl,--wrap=dgemm_,--wrap=sgemm_
77    WRAP_GEMV = -Wl,--wrap=dgemv_,--wrap=sgemv_
78  else
79    WRAP_GEMM = -Wl,--wrap=dgemm_
80    WRAP_GEMV = -Wl,--wrap=dgemv_
81  endif
82  DFLAGS += -DWRAP
83endif
84
85$(OUTDIR)/autobatch: $(BLDDIR)/autobatch-c.o $(OUTDIR)/.make $(LIBDEP) $(EXTDEP)
86	$(LD) -o $@ $< $(EXTLIB) $(MAINLIB) $(SLDFLAGS) $(LDFLAGS) $(CLDFLAGS) $(WRAP_GEMM)
87
88ifeq (0,$(NOBLAS))
89$(OUTDIR)/dgemm-blas: $(BLDDIR)/dgemm-c.o $(OUTDIR)/.make
90	$(LD) -o $@ $< $(SLDFLAGS) $(LDFLAGS) $(CLDFLAGS)
91ifeq (Darwin,$(UNAME))
92ifneq (0,$(XWRAP))
93	$(info ================================================================================)
94	$(info The static link-time wrapper mechanism is not supported under OS X!)
95	$(info ================================================================================)
96endif
97endif
98else
99.PHONY: $(OUTDIR)/dgemm-blas
100endif
101
102ifeq (0,$(NOBLAS))
103$(OUTDIR)/dgemv-blas: $(BLDDIR)/dgemv-c.o $(OUTDIR)/.make
104	$(LD) -o $@ $< $(SLDFLAGS) $(LDFLAGS) $(CLDFLAGS)
105ifeq (Darwin,$(UNAME))
106ifneq (0,$(XWRAP))
107	$(info ================================================================================)
108	$(info The static link-time wrapper mechanism is not supported under OS X!)
109	$(info ================================================================================)
110endif
111endif
112else
113.PHONY: $(OUTDIR)/dgemv-blas
114endif
115
116ifneq (0,$(XWRAP))
117$(OUTDIR)/dgemm-wrap: $(BLDDIR)/dgemm-c.o $(OUTDIR)/.make $(LIBDEP) $(EXTDEP)
118ifneq (0,$(OMP))
119	$(LD) -o $@ $< $(EXTLIB) $(MAINLIB) $(SLDFLAGS) $(LDFLAGS) $(CLDFLAGS) $(WRAP_GEMM)
120else ifneq (,$(strip $(OMPLIB)))
121	$(LD) -o $@ $< $(EXTLIB) $(MAINLIB) $(SLDFLAGS) $(LDFLAGS) $(CLDFLAGS) $(WRAP_GEMM) \
122		$(XLIB_BEGIN) $(OMPLIB) $(XLIB_END)
123else # should not happen
124	$(LD) -o $@ $< $(EXTLIB) $(MAINLIB) $(SLDFLAGS) $(LDFLAGS) $(CLDFLAGS) $(WRAP_GEMM)
125endif
126else
127.PHONY: $(OUTDIR)/dgemm-wrap
128endif
129
130ifneq (0,$(XWRAP))
131$(OUTDIR)/dgemv-wrap: $(BLDDIR)/dgemv-c.o $(OUTDIR)/.make $(LIBDEP) $(EXTDEP)
132ifneq (0,$(OMP))
133	$(LD) -o $@ $< $(EXTLIB) $(MAINLIB) $(SLDFLAGS) $(LDFLAGS) $(CLDFLAGS) $(WRAP_GEMV)
134else ifneq (,$(strip $(OMPLIB)))
135	$(LD) -o $@ $< $(EXTLIB) $(MAINLIB) $(SLDFLAGS) $(LDFLAGS) $(CLDFLAGS) $(WRAP_GEMV) \
136		$(XLIB_BEGIN) $(OMPLIB) $(XLIB_END)
137else # should not happen
138	$(LD) -o $@ $< $(EXTLIB) $(MAINLIB) $(SLDFLAGS) $(LDFLAGS) $(CLDFLAGS) $(WRAP_GEMV)
139endif
140else
141.PHONY: $(OUTDIR)/dgemv-wrap
142endif
143
144.PHONY: test
145test: $(OUTDIR)/.make $(OUTDIR)/wrap-test.sh $(XFILES)
146	@bash $(OUTDIR)/wrap-test.sh dgemm $(shell echo $$(($(TESTSIZE) * 1000)))
147	@bash $(OUTDIR)/wrap-test.sh dgemm 350  16  20 350  35 350  1 0.0
148	@bash $(OUTDIR)/wrap-test.sh dgemm 200 200 200 256 256 256  1 0.0
149	@bash $(OUTDIR)/wrap-test.sh dgemm  24  23  21  32  32  32 -1 0.5
150	@bash $(OUTDIR)/wrap-test.sh dgemv $(shell echo $$(($(TESTSIZE) * 1000)))
151	@bash $(OUTDIR)/wrap-test.sh dgemv 350  20 350 1 1 1 0
152	@bash $(OUTDIR)/wrap-test.sh dgemv 200 200 256 1 1 1 0
153	@bash $(OUTDIR)/wrap-test.sh dgemv  24  21  32 2 2 1 1
154
155$(BLDDIR)/%-cpp.o: $(SRCDIR)/%.cpp .state $(BLDDIR)/.make $(HEADERS) Makefile $(DEPDIR)/Makefile.inc
156	$(CXX) $(DFLAGS) $(IFLAGS) $(CXXFLAGS) $(CTARGET) -c $< -o $@
157
158$(BLDDIR)/%-c.o: $(SRCDIR)/%.c .state $(BLDDIR)/.make $(HEADERS) Makefile $(DEPDIR)/Makefile.inc
159	$(CC) $(DFLAGS) $(IFLAGS) $(CFLAGS) $(CTARGET) -c $< -o $@
160
161$(BLDDIR)/%-f.o: $(SRCDIR)/%.f .state $(BLDDIR)/.make Makefile $(DEPDIR)/Makefile.inc
162	$(FC) $(FCMTFLAGS) $(DFLAGS) $(IFLAGS) $(FCFLAGS) $(FTARGET) -c $< -o $@
163
164$(BLDDIR)/%-f90.o: $(SRCDIR)/%.f90 .state $(BLDDIR)/.make Makefile $(DEPDIR)/Makefile.inc
165	$(FC) $(FCMTFLAGS) $(DFLAGS) $(IFLAGS) $(FCFLAGS) $(FTARGET) -c $< -o $@
166
167$(BLDDIR)/%-f90.o: $(SRCDIR)/%.F90 .state $(BLDDIR)/.make Makefile $(DEPDIR)/Makefile.inc
168	$(FC) $(FCMTFLAGS) $(DFLAGS) $(IFLAGS) $(FCFLAGS) $(FTARGET) -c $< -o $@
169
170$(BLDDIR)/%-f77.o: $(SRCDIR)/%.F .state $(BLDDIR)/.make Makefile $(DEPDIR)/Makefile.inc
171	$(FC) $(FCMTFLAGS) $(DFLAGS) $(IFLAGS) $(FCFLAGS) $(FTARGET) -c $< -o $@
172
173.PHONY: clean
174clean:
175ifneq ($(call qapath,$(BLDDIR)),$(ROOTDIR))
176ifneq ($(call qapath,$(BLDDIR)),$(call qapath,.))
177	@rm -rf $(BLDDIR)
178endif
179endif
180ifneq (,$(wildcard $(BLDDIR))) # still exists
181	@rm -f $(OBJECTS) $(OBJECTX) $(FTNOBJS) $(FTNOBJX) *__genmod.* fit.log *.dat
182	@rm -f $(BLDDIR)/*.gcno $(BLDDIR)/*.gcda $(BLDDIR)/*.gcov
183endif
184	@rm -f .make .state
185
186.PHONY: realclean
187realclean: clean
188ifneq ($(call qapath,$(OUTDIR)),$(ROOTDIR))
189ifneq ($(call qapath,$(OUTDIR)),$(call qapath,.))
190	@rm -rf $(OUTDIR)
191endif
192endif
193ifneq (,$(wildcard $(OUTDIR))) # still exists
194	@rm -f $(OUTDIR)/libxsmm.$(DLIBEXT) $(OUTDIR)/*.stackdump
195	@rm -f $(XFILES) $(MODULES)
196endif
197
198