21#ifndef _libint2_src_bin_libint_buildtest_h_
22#define _libint2_src_bin_libint_buildtest_h_
26#include <graph_registry.h>
28#include <integral_11_11.h>
41void generate_rr_code(std::ostream& os,
42 const std::shared_ptr<CompilationParameters>& cparams,
43 std::deque<std::string>& decl_filenames,
44 std::deque<std::string>& def_filenames);
47void GenerateCode(
const std::shared_ptr<DirectedGraph>& dg,
48 const std::shared_ptr<CodeContext>& context,
49 const std::shared_ptr<CompilationParameters>& cparams,
50 const std::shared_ptr<Strategy>& strat,
51 const std::shared_ptr<Tactic>& tactic,
52 const std::shared_ptr<MemoryManager>& memman,
53 std::deque<std::string>& decl_filenames,
54 std::deque<std::string>& def_filenames,
55 const std::string& prefix,
const std::string& label,
60template <
unsigned int N>
66 const std::vector<unsigned int>& am()
const {
return am_; }
67 unsigned int size_to_unroll()
const {
return size_to_unroll_; }
68 unsigned int veclen()
const {
return veclen_; }
69 bool vectorize_by_line()
const {
return vectorize_by_line_; }
70 bool do_cse()
const {
return do_cse_; }
73 static const unsigned int max_am = 10;
74 std::vector<unsigned int> am_;
75 unsigned int size_to_unroll_;
77 bool vectorize_by_line_;
86template <
class Integral,
bool GenAllCode>
87void BuildTest(
const std::vector<std::shared_ptr<Integral> >& targets,
88 unsigned int size_to_unroll,
unsigned int veclen,
89 bool vec_by_line,
bool do_cse,
90 const std::string& complabel =
"buildtest",
91 std::ostream& os = std::cout);
98template <
class Integral,
bool GenAllCode>
100 const std::vector<std::shared_ptr<Integral> >& targets,
101 const std::shared_ptr<CompilationParameters>& cparams,
102 unsigned int size_to_unroll, std::ostream& os = std::cout,
103 const std::shared_ptr<Tactic>& tactic =
105 const std::shared_ptr<MemoryManager>& memman =
107 const std::string& complabel =
"general_integral");
109template <
class Integral,
bool GenAllCode>
110void __BuildTest(
const std::vector<std::shared_ptr<Integral> >& targets,
111 const std::shared_ptr<CompilationParameters>& cparams,
112 unsigned int size_to_unroll, std::ostream& os,
113 const std::shared_ptr<Tactic>& tactic,
114 const std::shared_ptr<MemoryManager>& memman,
115 const std::string& complabel) {
116 const std::string prefix(
"");
117 const std::string label = cparams->api_prefix() + complabel;
118 std::shared_ptr<Strategy> strat(
new Strategy);
119 std::shared_ptr<CodeContext> context(
new CppCodeContext(cparams));
122 taskmgr.
add(complabel);
128 unsigned int max_am = 0;
129 for (
unsigned int t = 0; t < targets.size(); ++t) {
130 const std::shared_ptr<Integral>& target = targets[t];
131 const unsigned int np = target->bra().num_part();
133 for (
unsigned int p = 0; p < np; p++) {
134 const unsigned int nf = target->bra().num_members(p);
135 for (
unsigned int f = 0; f < nf; f++) {
137 const unsigned int am = target->bra(p, f).qn();
139 max_am = max(max_am, am);
143 for (
unsigned int p = 0; p < np; p++) {
144 const unsigned int nf = target->ket().num_members(p);
145 for (
unsigned int f = 0; f < nf; f++) {
147 const unsigned int am = target->ket(p, f).qn();
149 max_am = max(max_am, am);
153 const bool need_to_optimize = (max_am <= cparams->max_am_opt(complabel));
155 std::deque<std::string> decl_filenames;
156 std::deque<std::string> def_filenames;
158 os <<
"Building " << complabel << std::endl;
161 dg_xxxx->set_label(complabel);
164 dg_xxxx->registry()->do_cse(need_to_optimize);
165 dg_xxxx->registry()->condense_expr(
166 condense_expr(size_to_unroll, cparams->max_vector_length() > 1));
168 dg_xxxx->registry()->accumulate_targets(cparams->accumulate_targets());
169 dg_xxxx->registry()->unroll_threshold(size_to_unroll);
171 for (
unsigned int t = 0; t < targets.size(); ++t) {
172 const std::shared_ptr<Integral>& target = targets[t];
173 std::shared_ptr<DGVertex> target_ptr =
174 std::dynamic_pointer_cast<DGVertex, Integral>(target);
175 assert(target_ptr != 0);
176 dg_xxxx->append_target(target_ptr);
181 GenerateCode(dg_xxxx, context, cparams, strat, tactic, memman, decl_filenames,
182 def_filenames, prefix, label,
false);
185 taskmgr.
current().params()->max_stack_size(max_am, memman->max_memory_used());
186 taskmgr.
current().params()->max_ntarget(targets.size());
187 os <<
"Max memory used = " << memman->max_memory_used() << std::endl;
191 std::shared_ptr<CodeContext> icontext(
new CppCodeContext(cparams));
193 std::shared_ptr<Libint2Iface> iface(
new Libint2Iface(cparams, icontext));
196 std::ostringstream oss;
197 for (std::deque<std::string>::const_iterator i = decl_filenames.begin();
198 i != decl_filenames.end(); ++i) {
199 oss <<
"#include <" << *i <<
">" << std::endl;
201 iface->to_int_iface(oss.str());
205 iface->macro_define(
"CARTGAUSS_MAX_AM", LIBINT_CARTGAUSS_MAX_AM));
207 iface->macro_define(
"CGSHELL_ORDERING", LIBINT_CGSHELL_ORDERING));
208 iface->to_params(iface->macro_define(
"CGSHELL_ORDERING_STANDARD",
209 LIBINT_CGSHELL_ORDERING_STANDARD));
210 iface->to_params(iface->macro_define(
"CGSHELL_ORDERING_INTV3",
211 LIBINT_CGSHELL_ORDERING_INTV3));
212 iface->to_params(iface->macro_define(
"CGSHELL_ORDERING_GAMESS",
213 LIBINT_CGSHELL_ORDERING_GAMESS));
214 iface->to_params(iface->macro_define(
"CGSHELL_ORDERING_ORCA",
215 LIBINT_CGSHELL_ORDERING_ORCA));
216 iface->to_params(iface->macro_define(
"CGSHELL_ORDERING_BAGEL",
217 LIBINT_CGSHELL_ORDERING_BAGEL));
218 iface->to_params(iface->macro_define(
"SHELLQUARTET_SET", LIBINT_SHELL_SET));
219 iface->to_params(iface->macro_define(
"SHELLQUARTET_SET_STANDARD",
220 LIBINT_SHELL_SET_STANDARD));
222 iface->macro_define(
"SHELLQUARTET_SET_ORCA", LIBINT_SHELL_SET_ORCA));
225 generate_rr_code(os, cparams, decl_filenames, def_filenames);
228 std::cout <<
"Generated headers: ";
229 std::copy(decl_filenames.begin(), decl_filenames.end(),
230 std::ostream_iterator<std::string>(std::cout,
" "));
231 std::cout << std::endl <<
"Generated sources: ";
232 std::copy(def_filenames.begin(), def_filenames.end(),
233 std::ostream_iterator<std::string>(std::cout,
" "));
234 std::cout << std::endl
235 <<
"Top compute function: "
241 const std::shared_ptr<CodeContext>& context,
242 const std::shared_ptr<CompilationParameters>& cparams,
243 const std::shared_ptr<Strategy>& strat,
244 const std::shared_ptr<Tactic>& tactic,
245 const std::shared_ptr<MemoryManager>& memman,
246 std::deque<std::string>& decl_filenames,
247 std::deque<std::string>& def_filenames,
248 const std::string& prefix,
const std::string& label,
250 dg->apply(strat, tactic);
251#if PRINT_DAG_GRAPHVIZ
253 std::basic_ofstream<char> dotfile(dg->label() +
".strat.dot");
254 dg->print_to_dot(
false, dotfile);
257 dg->optimize_rr_out(context);
259 std::cout <<
"The number of vertices = " << dg->num_vertices() << std::endl;
264 if (dg->missing_prerequisites()) {
268 std::deque<std::shared_ptr<DGVertex> > prereq_list = pe.vertices;
273#if PRINT_DAG_GRAPHVIZ
275 std::basic_ofstream<char> dotfile(dg->label() +
".expr.dot");
276 dg->print_to_dot(
false, dotfile);
280 std::string decl_filename(prefix + context->label_to_name(label));
281 decl_filename +=
".h";
282 std::string def_filename(prefix + context->label_to_name(label));
283 def_filename +=
".cc";
284 std::basic_ofstream<char> declfile(decl_filename.c_str());
285 std::basic_ofstream<char> deffile(def_filename.c_str());
288 std::shared_ptr<CodeSymbols> args(
new CodeSymbols);
289 if (have_parent) args->append_symbol(
"parent_stack");
291 label, declfile, deffile);
298#if PRINT_DAG_GRAPHVIZ
300 std::basic_ofstream<char> dotfile(dg->label() +
".symb.dot");
301 dg->print_to_dot(
true, dotfile);
305 decl_filenames.push_back(decl_filename);
306 def_filenames.push_back(def_filename);
310 if (dg->missing_prerequisites()) {
313 dg_prereq->registry() =
314 std::shared_ptr<GraphRegistry>(dg->registry()->clone());
319 dg_prereq->registry()->uncontract(
true);
320 assert(cparams->contracted_targets());
321 dg_prereq->registry()->return_targets(
false);
322 dg_prereq->registry()->accumulate_targets(
true);
323 dg_prereq->registry()->stack_name(
"stack");
324 if (dg->registry()->current_timer() >= 0) {
325 dg_prereq->registry()->current_timer(dg->registry()->current_timer() + 1);
335 while (!prereq_list.empty()) {
336 dg_prereq->append_target(prereq_list.front());
337 prereq_list.pop_front();
340 const std::string label_prereq = label +
"_prereq";
341 GenerateCode(dg_prereq, context, cparams, strat, tactic, memman,
342 decl_filenames, def_filenames, prefix, label_prereq,
true);
348template <
class Integral,
bool GenAllCode>
349void BuildTest(
const std::vector<std::shared_ptr<Integral> >& targets,
350 unsigned int size_to_unroll,
unsigned int veclen,
351 bool vec_by_line,
bool do_cse,
const std::string& complabel,
353 const unsigned int max_am = 10;
354 os <<
"generating code to compute " << complabel << std::endl;
357 taskmgr.
add(complabel);
362 cparams->max_am(complabel, max_am);
363 cparams->num_bf(complabel, 4u);
364 cparams->max_vector_length(veclen);
365 cparams->vectorize_by_line(vec_by_line);
367 cparams->align_size(LIBINT_ALIGN_SIZE);
369 cparams->count_flops(
true);
371 cparams->accumulate_targets(
true);
373 cparams->accumulate_targets(
false);
375#ifdef LIBINT_API_PREFIX
377 const std::string api_prefix(LIBINT_API_PREFIX);
378 cparams->api_prefix(api_prefix);
381#if LIBINT_CONTRACTED_INTS
382 cparams->contracted_targets(
true);
384 cparams->contracted_targets(
false);
386#ifdef LIBINT_USER_DEFINED_REAL
388 const std::string realtype(LIBINT_USER_DEFINED_REAL);
389 cparams->realtype(realtype);
394 cparams->max_am_opt(complabel, max_am);
396 cparams->max_am_opt(complabel, 0);
398 cparams->default_task_name(complabel);
405 std::shared_ptr<Tactic> tactic;
408 typename Integral::OperatorType,
409 typename Integral::AuxIndexType>
411 std::shared_ptr<genint_11_11_t> cast_ptr =
412 std::dynamic_pointer_cast<genint_11_11_t>(targets.front());
414 const unsigned int la = cast_ptr->bra(0, 0).norm();
415 const unsigned int lb = cast_ptr->ket(0, 0).norm();
416 const unsigned int lc = cast_ptr->bra(1, 0).norm();
417 const unsigned int ld = cast_ptr->ket(1, 0).norm();
421 tactic = std::shared_ptr<Tactic>(
426 __BuildTest<Integral, true>(targets, cparams, size_to_unroll, os, tactic,
430template <
unsigned int N>
431TesterCmdLine<N>::TesterCmdLine(
int argc,
char* argv[]) {
433 throw ProgrammingError(
"TesterCmdLine<N>::TesterCmdLine but N is 0");
434 const int argc_min = N + 2;
435 const int argc_max = N + 5;
436 if (argc < argc_min || argc > argc_max) {
438 <<
"Usage: " << argv[0]
439 <<
" <am> size_to_unroll [vector_length] [vector_method] [do_cse]"
441 <<
" <am> -- angular momenta on each center, e.g. 4 nonnegative "
442 "integers for a 4-center ERI"
444 <<
" size_to_unroll -- size of the largest integral set to be "
447 <<
" vector_length -- (optional) max vector length. Defaults to "
450 <<
" vector_method -- (optional) vectorization method. Valid "
451 "choices are 0 (by-block) and 1 (by-line). Defaults to 0."
453 <<
" do_cse -- (optional) do Common Subexpression Elimination? "
454 "Valid choices are 0 (no) and 1 (yes). Defaults to 0."
458 "TesterCmdLine<N>::TesterCmdLine -- incorrect number of command-line "
461 for (
unsigned int i = 1; i < N + 1; ++i) {
462 const unsigned int am = atoi(argv[i]);
465 "TesterCmdLine<N>::TesterCmdLine -- angular momentum limit exceeded");
468 size_to_unroll_ = atoi(argv[N + 1]);
472 veclen_ = atoi(argv[N + 2]);
474 vectorize_by_line_ =
false;
476 vectorize_by_line_ = (1 == atoi(argv[N + 3]));
480 do_cse_ = (1 == atoi(argv[N + 4]));
Class CodeSymbols specifies a set of symbols used in a code.
Definition code.h:32
These are the parameters received by the compiler.
Definition default_params.h:39
CppCodeContext is an implementation of CodeContext for C++.
Definition context.h:210
DirectedGraph is an implementation of a directed graph composed of vertices represented by DGVertex o...
Definition dg.h:65
FirstChoiceTactic simply chooses the first RR.
Definition tactic.h:58
FourCenter_OS_Tactic decides graph build for (bra0 ket0| bra1 ket1) = <bra0 bra1|ket0 ket1>
Definition tactic.h:169
Generic integral over a two-body operator with one bfs for each particle in bra and ket.
Definition integral_11_11.h:36
static void set_default_dims(const std::shared_ptr< CompilationParameters > &cparams)
Sets default ImplicitDimension object.
Definition dims.cc:30
static std::shared_ptr< ImplicitDimensions > default_dims()
Default ImplicitDimension object.
Definition dims.cc:37
Libint2Iface is used to generate Libint2 interfaces.
Definition iface.h:42
Manages tasks. This is a Singleton.
Definition task.h:65
void current(const std::string &task_label)
Makes this task current (must have been added already)
Definition task.cc:63
void add(const std::string &task_label)
Adds a new task. Do nothing if the task exists already.
Definition task.cc:34
static LibraryTaskManager & Instance()
LibraryTaskManager is a Singleton.
Definition task.cc:32
The shift parameter is computed as follows: delta = floor(nrrs*scale*random()/RAND_MAX) where nrrs is...
Definition tactic.h:202
Strategy specifies how to apply recurrence relations.
Definition strategy.h:39
Command-line parser for the standard build tester – N is the number of centers, i....
Definition buildtest.h:61
WorstFitMemoryManager allocates memory by trying to find the largest-possible free block.
Definition src/bin/libint/memory.h:215
Defaults definitions for various parameters assumed by Libint.
Definition algebra.cc:24
void extract_symbols(const std::shared_ptr< DirectedGraph > &dg)
extracts external symbols and RRs from the graph
Definition dg.cc:2394
void GenerateCode(const std::shared_ptr< DirectedGraph > &dg, const std::shared_ptr< CodeContext > &context, const std::shared_ptr< CompilationParameters > &cparams, const std::shared_ptr< Strategy > &strat, const std::shared_ptr< Tactic > &tactic, const std::shared_ptr< MemoryManager > &memman, std::deque< std::string > &decl_filenames, std::deque< std::string > &def_filenames, const std::string &prefix, const std::string &label, bool have_parent)
defined below generates code for dg; dg and memman are reset at the end
Definition buildtest.h:240
std::string label_to_funcname(const std::string &label)
Converts a label, e.g.
Definition default_params.cc:201
bool condense_expr(unsigned int unroll_threshold, bool vectorize)
need to condense expressions? Makes sense if vectorizing the code or the compiler somehow prefers lon...
Definition default_params.cc:212
void __BuildTest(const std::vector< std::shared_ptr< Integral > > &targets, const std::shared_ptr< CompilationParameters > &cparams, unsigned int size_to_unroll, std::ostream &os=std::cout, const std::shared_ptr< Tactic > &tactic=std::shared_ptr< Tactic >(new FirstChoiceTactic< DummyRandomizePolicy >), const std::shared_ptr< MemoryManager > &memman=std::shared_ptr< MemoryManager >(new WorstFitMemoryManager), const std::string &complabel="general_integral")
This is a generic test of building an Integral using specified cparams, memman, size_to_unroll,...
Definition buildtest.h:110
void BuildTest(const std::vector< std::shared_ptr< Integral > > &targets, unsigned int size_to_unroll, unsigned int veclen, bool vec_by_line, bool do_cse, const std::string &complabel="buildtest", std::ostream &os=std::cout)
This is a user-friendly generic test of building an Integral using specified size_to_unroll,...
Definition buildtest.h:349