Skip to content

Commit

Permalink
Pre-process stencil code to create context code containing loops.
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckyount committed Dec 12, 2022
1 parent 34d4185 commit 9a95e87
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
12 changes: 6 additions & 6 deletions src/compiler/lib/YaskKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ namespace yask {
print_macros(os);
os << "\n#endif // DEFINE_MACROS\n";

// Stencil-context code.
os << "\n#if defined(DEFINE_CONTEXT) && !defined(CONTEXT_DONE)\n"
"#define CONTEXT_DONE\n"
// Stencil-solution code.
os << "\n#if defined(DEFINE_SOLUTION) && !defined(SOLUTION_DONE)\n"
"#define SOLUTION_DONE\n"
"namespace yask {" << endl;

// First, create a class to hold the data (vars).
Expand All @@ -94,11 +94,11 @@ namespace yask {
// A struct for each equation bundle.
print_eq_bundles(os);

// Finish the context.
// Finish the solution.
print_context(os);

os << "} // namespace yask.\n"
"#endif // DEFINE_CONTEXT\n"
"#endif // DEFINE_SOLUTION\n"
"\n//End of automatically-generated code." << endl;
}

Expand All @@ -109,7 +109,7 @@ namespace yask {
string sname = _stencil._get_name();
os << "// Stencil solution:\n"
"#define YASK_STENCIL_NAME \"" << sname << "\"\n"
"#define YASK_STENCIL_CONTEXT " << _context << endl;
"#define YASK_STENCIL_SOLUTION " << _context << endl;
os << "\n// Target:\n"
"#define YASK_TARGET \"" << _settings._target << "\"\n"
"#define REAL_BYTES " << _settings._elem_bytes << endl;
Expand Down
29 changes: 22 additions & 7 deletions src/kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -275,18 +275,22 @@ YK_EXT_SRC_NAMES := factory soln_apis context halo stencil_calc setup alloc \
YK_OBJS := $(addprefix $(YK_OBJ_DIR)/,$(addsuffix .o,$(YK_COMM_SRC_NAMES) $(COMM_SRC_NAMES)))
YK_EXT_OBJS := $(addprefix $(YK_EXT_OBJ_DIR)/,$(addsuffix .o,$(YK_EXT_SRC_NAMES)))
YK_CODE_FILE := $(YK_GEN_DIR)/yask_stencil_code.hpp
YK_GEN_HEADERS := $(addprefix $(YK_GEN_DIR)/, \
YK_SOLUTION_FILE := $(YK_GEN_DIR)/yask_solution_code.hpp
YK_LOOP_FILES := $(addprefix $(YK_GEN_DIR)/, \
yask_rank_loops.hpp \
yask_mega_block_loops.hpp \
yask_block_loops.hpp \
yask_micro_block_loops.hpp \
yask_nano_block_loops.hpp \
yask_pico_block_loops.hpp \
yask_misc_loops.hpp \
yask_misc_loops.hpp)
YK_GEN_HEADERS := $(addprefix $(YK_GEN_DIR)/, \
yask_layout_macros.hpp \
yask_layouts.hpp \
yask_var_code.hpp) \
$(YK_CODE_FILE)
$(YK_LOOP_FILES) \
$(YK_CODE_FILE) \
$(YK_SOLUTION_FILE)
YK_INC_DIRS := $(INC_DIRS) $(YK_LIB_SRC_DIR) $(COMM_DIR) $(COEFF_DIR)
YK_INC_GLOB := $(addsuffix /*.hpp,$(YK_INC_DIRS))
YK_API_TEST_EXEC := $(BIN_OUT_DIR)/$(YK_BASE)_api_test.exe
Expand All @@ -297,7 +301,7 @@ MAKE_REPORT_FILE := $(BUILD_OUT_DIR)/$(YK_EXT_BASE).make-report.txt
YC_REPORT_FILE := $(BUILD_OUT_DIR)/$(YK_EXT_BASE).yask_compiler-report.txt

# File-related macros.
MACROS += YK_CODE_FILE=$(YK_CODE_FILE)
MACROS += YK_CODE_FILE=$(YK_CODE_FILE) YK_SOLUTION_FILE=$(YK_SOLUTION_FILE)

# YASK loop compiler.
GEN_LOOPS := $(UTILS_BIN_DIR)/gen_loops.pl
Expand All @@ -316,7 +320,8 @@ YK_CXXOPT ?= -O3
YK_CXXDBG ?= -g
YK_CXXFLAGS := -std=c++17
YK_CXXWARN := -Wall
YK_CXXFLAGS += $(addprefix -I,$(YK_INC_DIRS) $(YK_GEN_DIR))
YK_CXX_INCFLAGS := $(addprefix -I,$(YK_INC_DIRS) $(YK_GEN_DIR))
YK_CXXFLAGS += $(YK_CXX_INCFLAGS)
SWIG_CXXFLAGS := $(DBL_EPSILON_CXXFLAG)
YK_SO_FLAGS := -shared -fPIC
OMPFLAG := -fopenmp
Expand Down Expand Up @@ -497,7 +502,8 @@ else ifeq ($(model_cache),2)
endif

# Add in final flags and user-added flags.
YK_CXXFLAGS += $(YK_CXXDBG) $(YK_CXXOPT) $(OMPFLAGS) $(addprefix -D,$(MACROS) $(subst $(comma),$(space),$(EXTRA_MACROS))) $(YK_CXXWARN) $(EXTRA_YK_CXXFLAGS)
YK_MACROS := $(addprefix -D,$(MACROS) $(subst $(comma),$(space),$(EXTRA_MACROS)))
YK_CXXFLAGS += $(YK_CXXDBG) $(YK_CXXOPT) $(OMPFLAGS) $(YK_MACROS) $(YK_CXXWARN) $(EXTRA_YK_CXXFLAGS)
YK_LIBS += $(EXTRA_YK_LIBS)

# Number of stencil/var dims extracted from YASK compiler output.
Expand Down Expand Up @@ -606,7 +612,7 @@ $(YK_EXT_OBJ_DIR)/%.o: %.cpp $(YK_GEN_HEADERS) $(YK_INC_GLOB)

# C++-preprocessor rule.
%.i: %.cpp $(YK_GEN_HEADERS) $(YK_INC_GLOB)
$(YK_CXX) $(YK_CXXFLAGS) -x c++ -EP $< > $@
g++ -E -CC $(YK_CXX_INCFLAGS) $< > $@
@ls -l $@

######## Primary targets.
Expand Down Expand Up @@ -687,6 +693,7 @@ $(YK_GEN_DIR)/yask_var_code.hpp: $(GEN_LAYOUTS) $(YK_CODE_FILE)
$(call MK_DIR,$(dir $@))
$(PERL) $< -v $(NVDIMS) > $@

# Run the YASK compiler to create the stencil code.
# NB: '$(BASH) -o pipefail' ensures failure of command before '| tee' is visible.
$(YK_CODE_FILE): $(YC_EXEC)
$(call MK_DIR,$(dir $@))
Expand All @@ -695,6 +702,14 @@ $(YK_CODE_FILE): $(YC_EXEC)
"$(RUN_PREFIX) $< $(YC_FLAGS) -p $@ $(EXTRA_YC_FLAGS) 2>&1 | tee $(YC_REPORT_FILE)"
@- $(INDENT) $@

# Pre-process the stencil code to create the context code.
# This adds the nano- and pico-loop code to make it easier for humans
# and performance tools to parse.
$(YK_SOLUTION_FILE): $(YK_CODE_FILE) $(YK_LOOP_FILES)
g++ -E -CC $(YK_CXX_INCFLAGS) $(YK_MACROS) -DDEFINE_SOLUTION $(YK_CODE_FILE) \
| $(PERL) -n -e '$$start=1 if /Automatic/; print if $$start && !/^#/' > $@
@- $(INDENT) $@

headers: $(YK_GEN_HEADERS)
@ echo 'Header files generated.'

Expand Down
8 changes: 3 additions & 5 deletions src/kernel/lib/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ IN THE SOFTWARE.
using namespace std;

// Auto-generated stencil code that extends base types.
#define DEFINE_CONTEXT
#include YSTR2(YK_CODE_FILE)
#undef DEFINE_CONTEXT
#include YSTR2(YK_SOLUTION_FILE)

namespace yask {

Expand Down Expand Up @@ -75,7 +73,7 @@ namespace yask {

auto ep = dynamic_pointer_cast<KernelEnv>(env);
assert(ep);
auto dp = YASK_STENCIL_CONTEXT::new_dims(); // create Dims.
auto dp = YASK_STENCIL_SOLUTION::new_dims(); // create Dims.
assert(dp);
auto req_opts = make_shared<KernelSettings>(dp, ep);
assert(req_opts);
Expand All @@ -96,7 +94,7 @@ namespace yask {

// Create problem-specific object defined by stencil compiler.
// TODO: allow more than one type of solution to be created.
auto sp = make_shared<YASK_STENCIL_CONTEXT>(ep, actl_opts, req_opts);
auto sp = make_shared<YASK_STENCIL_SOLUTION>(ep, actl_opts, req_opts);
assert(sp);

return sp;
Expand Down
24 changes: 12 additions & 12 deletions utils/bin/gen_loops.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1094,8 +1094,8 @@ ($)
"#endif // Part $partNum.\n";
$partNum++;
push @code,
"// Enable part $loopPart by defining the following macro.",
"#ifdef ${macroPrefix}$loopPart$partNum\n";
"#ifdef ${macroPrefix}${loopPart}$partNum",
"// Enable part $partNum by defining the above macro.";

# clear data for this loop so we'll be ready for a nested loop.
undef @loopDims;
Expand Down Expand Up @@ -1140,9 +1140,9 @@ ($)
# Front matter.
my @fcode;
push @fcode,
"// Enable part 0 by defining the following macro.",
"#ifdef ${macroPrefix}${loopPart}0\n",
"// These macros must be re-defined for each generated loop-nest.",
"// Enable part 0 by defining the above macro.",
"// The following macros must be re-defined for each generated loop-nest.",
macroDef('SIMD_PRAGMA', undef, pragma($OPT{simd})),
macroDef('INNER_LOOP_PREFIX', undef, pragma($OPT{inner})),
macroDef('OMP_PRAGMA', undef, pragma($OPT{omp})),
Expand Down Expand Up @@ -1184,19 +1184,19 @@ ($)

# header.
print OUT
"/*\n",
" * Var-scanning code.\n",
" * Generated automatically from the following pseudo-code:\n",
" *\n",
" * $codeString\n",
" *\n */";
" /*\n",
" * Var-scanning code.\n",
" * Generated automatically from the following pseudo-code:\n",
" *\n",
" * $codeString\n",
" *\n */";

# print out code.
for my $line (@code) {
print OUT "\n" if $line =~ m=^\s*//=; # blank line before comment.
print OUT " $line\n"; # add space at beginning of every line.
}
print OUT "// End of generated code.\n";
print OUT " // End of generated var-scanning code.\n";
close OUT;
system("$indent $OPT{output}") if -x $indent;

Expand Down Expand Up @@ -1299,7 +1299,7 @@ ()
}
}
close MF;
print "".(scalar keys %macros)." macro(s) read from '$OPT{macro_file}'\n";
print "info: ".(scalar keys %macros)." macro(s) read from '$OPT{macro_file}'\n";
}

my $codeString = join(' ', @ARGV); # just concat all non-options params together.
Expand Down

0 comments on commit 9a95e87

Please sign in to comment.