expr_trace.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2014 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Justus Calvin
19  * Department of Chemistry, Virginia Tech
20  *
21  * expr_trace.h
22  * Mar 16, 2014
23  *
24  */
25 
26 #ifndef TILEDARRAY_EXPR_TRACE_H__INCLUDED
27 #define TILEDARRAY_EXPR_TRACE_H__INCLUDED
28 
30 #include <iostream>
31 
32 namespace TiledArray {
33 namespace expressions {
34 
35 template <typename>
36 class Expr;
37 template <typename, bool>
38 class TsrExpr;
39 
41 class ExprOStream {
42  std::ostream& os_;
43  unsigned int tab_;
44 
45  public:
47 
49  ExprOStream(std::ostream& os) : os_(os), tab_(0u) {}
50 
52 
54  ExprOStream(const ExprOStream& other) : os_(other.os_), tab_(other.tab_) {}
55 
57 
60  template <typename T>
61  std::ostream& operator<<(const T& t) {
62  for (unsigned int i = 0u; i < tab_; ++i) {
63  os_ << " ";
64  }
65 
66  os_ << t;
67  return os_;
68  }
69 
71  void inc() { ++tab_; }
72 
74  void dec() { --tab_; }
75 
77  std::ostream& get_stream() const { return os_; }
78 
79 }; // class ExprOStream
80 
82 
85  std::ostream& os_;
86  BipartiteIndexList target_indices_;
87 
88  public:
90 
93  ExprTraceTarget(std::ostream& os, const std::string& target_annotation)
94  : os_(os), target_indices_(target_annotation) {}
95 
97 
100  : os_(other.os_), target_indices_(other.target_indices_) {}
101 
103 
107  template <typename D>
108  std::ostream& operator<<(const Expr<D>& expr) const {
109  if (TiledArray::get_default_world().rank() == 0) {
110  os_ << target_indices_ << " =\n";
111 
112  ExprOStream expr_stream(os_);
113  expr_stream.inc();
114  expr.derived().print(expr_stream, target_indices_);
115  }
116 
117  return os_;
118  }
119 
120 }; // class ExprTraceTarget
121 
123 
129 template <typename A, bool Alias>
130 inline ExprTraceTarget operator<<(std::ostream& os,
131  const TsrExpr<A, Alias>& tsr) {
132  return ExprTraceTarget(os, tsr.annotation());
133 }
134 
135 } // namespace expressions
136 } // namespace TiledArray
137 
138 #endif // TILEDARRAY_EXPR_TRACE_H__INCLUDED
void inc()
Increment the number of tabs.
Definition: expr_trace.h:71
ExprTraceTarget(std::ostream &os, const std::string &target_annotation)
Constructor.
Definition: expr_trace.h:93
ExprOStream(const ExprOStream &other)
Copy constructor.
Definition: expr_trace.h:54
Expression wrapper for array objects.
Definition: tsr_expr.h:83
Base class for expression evaluation.
Definition: expr.h:97
auto rank(const DistArray< Tile, Policy > &a)
Definition: dist_array.h:1617
World & get_default_world()
Definition: madness.h:90
Expression output stream.
Definition: expr_trace.h:41
void dec()
Decrement the number of tabs.
Definition: expr_trace.h:74
std::ostream & get_stream() const
Output stream accessor.
Definition: expr_trace.h:77
const std::string & annotation() const
Tensor annotation accessor.
Definition: tsr_expr.h:304
ExprTraceTarget(const ExprTraceTarget &other)
Copy constructor.
Definition: expr_trace.h:99
std::ostream & operator<<(const T &t)
Output operator.
Definition: expr_trace.h:61
std::ostream & operator<<(const Expr< D > &expr) const
Start the expression trace.
Definition: expr_trace.h:108
ExprOStream(std::ostream &os)
Constructor.
Definition: expr_trace.h:49
ExprTraceTarget operator<<(std::ostream &os, const TsrExpr< A, Alias > &tsr)
Expression trace factory function.
Definition: expr_trace.h:130
derived_type & derived()
Cast this object to its derived type.
Definition: expr.h:365