From f0f9474689b1dfa46302453c47187bf99c00ab7a Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Tue, 12 Apr 2016 13:32:10 -0600 Subject: [PATCH] Added MultipleTransfromations example to HowToLCIR --- HowToLoopChainIR/Makefile | 12 +- HowToLoopChainIR/bin/.gitkeep | 0 .../src/MultipleTransformations.cpp | 114 ++++++++++++++++++ 3 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 HowToLoopChainIR/bin/.gitkeep create mode 100644 HowToLoopChainIR/src/MultipleTransformations.cpp diff --git a/HowToLoopChainIR/Makefile b/HowToLoopChainIR/Makefile index 5f5ac9e..d365ee4 100644 --- a/HowToLoopChainIR/Makefile +++ b/HowToLoopChainIR/Makefile @@ -1,6 +1,7 @@ PROJECT_DIR=.. SRC=$(PROJECT_DIR)/src BIN=$(PROJECT_DIR)/bin +LIB=$(PROJECT_DIR)/lib THIRD_PARTY=$(PROJECT_DIR)/third-party THIRD_PARTY_SRC=$(THIRD_PARTY)/source @@ -11,8 +12,8 @@ INITED_FILE=$(THIRD_PARTY)/initialized DOC_PATH = $(PROJECT_DIR)/doxygen -LIB=$(THIRD_PARTY_INSTALL)/lib -INC=$(THIRD_PARTY_INSTALL)/include +T_LIB=$(THIRD_PARTY_INSTALL)/lib +T_INC=$(THIRD_PARTY_INSTALL)/include EX_SRC=./src EX_BIN=./bin @@ -22,18 +23,19 @@ EXAMPLES = RectangularDomainHowTo \ LoopChainHowTo \ ScheduleHowTo \ FusionTransformationHowTo \ - ShiftTransformationHowTo + ShiftTransformationHowTo \ + MultipleTransformations LONG_EXAMPLES=$(addprefix $(EX_BIN)/,$(EXAMPLES)) -LCIR_LIB=$(BIN)/LoopChainIR.a +LCIR_LIB=$(LIB)/libloopchainIR.a all: $(LONG_EXAMPLES) $(EXAMPLES): % : $(EX_BIN)/% $(LONG_EXAMPLES): $(EX_BIN)/% : $(EX_SRC)/%.cpp $(LCIR_LIB) - $(CXX) $(CPPFLAGS) $(CXXFLAGS) -I$(SRC) -Wl,-rpath -Wl,$(LIB) $^ -L$(LIB) -lisl -o $@ + $(CXX) $(CPPFLAGS) $(CXXFLAGS) -I$(SRC) -Wl,-rpath -Wl,$(T_LIB) $^ -L$(T_LIB) -lisl -o $@ clean: - rm $(EX_BIN)/* diff --git a/HowToLoopChainIR/bin/.gitkeep b/HowToLoopChainIR/bin/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/HowToLoopChainIR/src/MultipleTransformations.cpp b/HowToLoopChainIR/src/MultipleTransformations.cpp new file mode 100644 index 0000000..692ec74 --- /dev/null +++ b/HowToLoopChainIR/src/MultipleTransformations.cpp @@ -0,0 +1,114 @@ +/****************************************************************************** +How To Schedule +Recommended Reading: ++ ScheduleHowTo.cpp +******************************************************************************/ +#include "ShiftTransformation.hpp" +#include "FusionTransformation.hpp" +#include +#include +#include + +using namespace std; + +int main(){ + { + cout << "Shift-Fuse example on 2N_1D example" << endl; + LoopChain chain; + { + string lower[1] = { "1" }; + string upper[1] = { "N" }; + string symbols[1] = { "N" }; + chain.append( LoopNest( RectangularDomain( lower, upper, 1, symbols, 1 ) ) ); + } + { + // This loop is a shifted version of the previous one. + // How handy! + string lower[1] = { "1+9" }; + string upper[1] = { "N+9" }; + string symbols[1] = { "N" }; + chain.append( LoopNest( RectangularDomain( lower, upper, 1, symbols, 1 ) ) ); + } + // Create Schedule object from chain + Schedule sched( chain ); + + // Print everything + cout << "Before Transformation:\n" + << "Schedule state:\n" << sched + << "\nDefault scheduled code:\n" << sched.codegen() + << "-------------------------------------" << endl; + /* + Before Transformation: + Schedule state: + Domains: + [N] -> {statement_0[idx_0] : 1 <= idx_0 <= N} + [N] -> {statement_1[idx_0] : 1+9 <= idx_0 <= N+9} + + Transformations: + {statement_0[idx_0] -> [0,idx_0,0]; statement_1[idx_0] -> [1,idx_0,0]; } + + Default scheduled code: + { + for (int c1 = 1; c1 <= N; c1 += 1) + statement_0(c1); + for (int c1 = 10; c1 <= N + 9; c1 += 1) + statement_1(c1); + } + */ + + // Let's shift and fuse our loops. + vector transformations; + // Create our shift transformation + { + // We are shifting the second loop backwards by 9 + // could also shift the first loop forwards by 9 + ShiftTransformation* transformation = new ShiftTransformation(1, "-9"); + transformations.push_back( transformation ); + } + { + // Fuse our two loops + vector fuse_these; + fuse_these.push_back( (LoopChain::size_type) 0 ); + fuse_these.push_back( (LoopChain::size_type) 1 ); + FusionTransformation* transformation = new FusionTransformation( fuse_these ); + transformations.push_back( transformation ); + } + + // Apply the transformations + sched.apply( transformations ); + + // Print everything + cout << "After Shift Transformation:\n" + << "Schedule state:\n" << sched + << "\nTransformed code:\n" << sched.codegen() + << endl; + + /* + After Shift Transformation: + Schedule state: + Domains: + [N] -> {statement_0[idx_0] : 1 <= idx_0 <= N} + [N] -> {statement_1[idx_0] : 1+9 <= idx_0 <= N+9} + + Transformations: + {statement_0[idx_0] -> [0,idx_0,0]; statement_1[idx_0] -> [1,idx_0,0]; } + { + [t,i0,i1]->[t,i0+(-9),i1] : (t = 1); + [t,i0,i1]->[t,i0,i1] : (t != 1); + } + { + [f,i1,i2] -> [t,i1,f] : ( t = 0 ) and (f = 0 or f = 1); + [f,i1,i2] -> [f,i1,i2] : !(f = 0 or f = 1) + }; + + Transformed code: + for (int c1 = 1; c1 <= N; c1 += 1) { + statement_0(c1); + statement_1(c1 + 9); + } + + You can see that our two loop statements are now within a single loop over + the domain {1..N}; + */ + } +}