SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LinAlgInterpreter.h
Go to the documentation of this file.
1 //
2 // For more information, please see: http://software.sci.utah.edu
3 //
4 // The MIT License
5 //
6 // Copyright (c) 2009 Scientific Computing and Imaging Institute,
7 // University of Utah.
8 //
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 // DEALINGS IN THE SOFTWARE.
27 //
28 
29 #ifndef CORE_PARSER_LINALGINTERPRETER_H
30 #define CORE_PARSER_LINALGINTERPRETER_H 1
31 
32 // For the pointers to the datatypes
33 #include <Core/Datatypes/Matrix.h>
34 #include <Core/Thread/Thread.h>
35 #include <Core/Thread/Barrier.h>
36 
37 #include <Core/Containers/Handle.h>
39 
40 #include <Core/Parser/Parser.h>
41 
42 // Include files needed for Windows
43 #include <Core/Parser/share.h>
44 
45 namespace SCIRun {
46 
47 // Define class as they are not used in order
48 
49 class LinAlgIntepreter;
50 class LinAlgFunction;
51 
52 class LinAlgProgram;
53 class LinAlgProgramCode;
55 
56 // Handles for a few of the classes
57 // As Program is stored in a large array we do not need a handle for that
58 // one. These are helper classes that are located elsewhere in memory
59 
60 typedef Handle<LinAlgProgramVariable> LinAlgProgramVariableHandle;
61 typedef Handle<LinAlgProgram> LinAlgProgramHandle;
62 
63 //-----------------------------------------------------------------------------
64 // Functions for databasing the function calls that make up the program
65 
66 // This class is used to describe each function in the system
67 // It describes the name + input arguments, the return type,
68 // additional flags and a pointer the the actual function that
69 // needs to be called
70 
71 
73  public:
74  // Build a new function
76  bool (*function)(LinAlgProgramCode& pc, std::string& err),
77  std::string function_id,
78  std::string function_type,
79  int function_flags
80  );
81 
82  // Virtual destructor so I can do dynamic casts on this class
83  virtual ~LinAlgFunction() {}
84 
85  // Get the pointer to the function
86  bool (*get_function())(LinAlgProgramCode& pc, std::string& err)
87  { return (function_); }
88 
89  private:
90  // The function to call that needs to be called on the data
91  bool (*function_)(LinAlgProgramCode& pc, std::string& err);
92 
93 };
94 
95 
96 //-----------------------------------------------------------------------------
97 // Code segment class, all the function calls are based on this class
98 // providing the program with input and output variables all located in one
99 // piece of memory. Although this class points to auxilary memory block,
100 // an effort is made to store them all in the same array, to minimize
101 // page swapping
102 
104  public:
105 
106  // Constructor
107  LinAlgProgramCode(bool (*function)(LinAlgProgramCode& pc,std::string& err)) :
108  function_(function) {}
109 
111  function_(0) {}
112 
113  // Set the function pointer
114  inline void set_function(bool (*function)(LinAlgProgramCode& pc,std::string& err))
115  {
116  function_ = function;
117  }
118 
119  // Get the function pointer
120  inline bool (*get_function())(LinAlgProgramCode& pc,std::string& err)
121  {
122  return (function_);
123  }
124 
125  // Tell the porgam where to temporary space has been allocated
126  // for this part of the program
127  inline void set_handle(size_t j, MatrixHandle* variable)
128  {
129  if (j >= variables_.size()) variables_.resize(j+1);
130  variables_[j] = variable;
131  }
132 
133  // These functions are called by the actual code segments
134  // For Matrix buffers
135  inline MatrixHandle* get_handle(size_t j)
136  { return (variables_[j]); }
137 
138  inline MatrixHandle& handle(size_t j)
139  { return (*(variables_[j])); }
140 
141  // Run this code segment
142  // Run time errors are reported by returning a false,
143  // After which we can look in the parser script to see
144  // which function failed
145  inline bool run(std::string& err)
146  { return (function_(*this, err)); }
147 
148  void print();
149 
150  private:
151  // This is the minimal information needed to run the parsed program
152  // In order improve performance, all the buffers and instructions are
153  // grouped together so they fit in a few pages of the memory manager
154 
155  // Function call to evaluate this piece of the code
156  bool (*function_)(LinAlgProgramCode& pc,std::string& err);
157 
158  // Location of where the data is stored, these are Matrix handle pointers
159  // This way one can allocate new matrices and remove them when the handle
160  // is no loner used. The handles are stored in a different array
162 };
163 
164 
165 
167 
168  public:
169  // For handles
170  int ref_cnt;
171 
172  // Constructor of the variable
174  ref_cnt(0), name_(name), handle_(0)
175  {
176  }
177 
178 
179  LinAlgProgramVariable(std::string name, MatrixHandle handle) :
180  ref_cnt(0), name_(name), handle_(handle) {}
181 
182  // Retrieve the data pointer from the central temporal
183  // storage
184  MatrixHandle* get_handle() { return (&(handle_)); }
185  MatrixHandle& handle() { return (handle_); }
186 
187  private:
188  // Name of variable
189  std::string name_;
190 
191  // Where the data needs to be store
192  MatrixHandle handle_;
193 };
194 
195 
197  public:
198  MatrixHandlePointer() : ref_cnt(0), matrix(0) {}
199 
200  int ref_cnt;
202 };
203 
205  public:
207  matrix_(new MatrixHandlePointer()) {}
208 
209  void set_matrix(MatrixHandle matrix) { matrix_->matrix= matrix; }
210  MatrixHandle get_matrix() { return (matrix_->matrix); }
211  MatrixHandle* get_handle() { return (&(matrix_->matrix)); }
212  bool is_matrix() { return (true); }
213 
214  private:
215  Handle<MatrixHandlePointer> matrix_;
216 };
217 
218 
220 
221  public:
222  int ref_cnt;
223 
224  // Default constructor
225  LinAlgProgram() : ref_cnt(0)
226  {}
227 
228  // Get the optimization parameters, these can only be set when creating the
229  // object as it depends on allocated buffer sizes and those are hard to change
230  // when allocated
231 
232  bool add_source(std::string& name, MatrixHandle& matrix);
233  bool add_sink(std::string& name);
234 
235  void resize_const_variables(size_t sz)
236  { const_variables_.resize(sz); }
237  void resize_single_variables(size_t sz)
238  { single_variables_.resize(sz); }
239 
240  void resize_const_functions(size_t sz)
241  { const_functions_.resize(sz); }
242  void resize_single_functions(size_t sz)
243  { single_functions_.resize(sz); }
244 
245  // Set variables which we use as temporal information structures
246  /// @todo: need to remove them at some point
248  { const_variables_[j] = handle; }
250  { single_variables_[j] = handle; }
251 
253  { return (const_variables_[j]); }
255  { return (single_variables_[j]); }
256 
257  // Set program code
259  { const_functions_[j] = pc; }
261  { single_functions_[j] = pc; }
262 
263  // Code to find the pointers that are given for sources and sinks
264  bool find_source(std::string& name, LinAlgProgramSource& ps);
265  bool find_sink(std::string& name, LinAlgProgramSource& ps);
266 
267  bool run_const(size_t& error_line, std::string& err);
268  bool run_single(size_t& error_line, std::string& err);
269 
270  void set_parser_program(ParserProgramHandle handle) { pprogram_ = handle; }
271  ParserProgramHandle get_parser_program() { return (pprogram_); }
272 
273  private:
274 
275  // Source and Sink information
276  std::map<std::string,LinAlgProgramSource> input_sources_;
277  std::map<std::string,LinAlgProgramSource> output_sinks_;
278 
279  // Variable lists
280  std::vector<LinAlgProgramVariableHandle> const_variables_;
281  std::vector<LinAlgProgramVariableHandle> single_variables_;
282 
283  // Program code
284  std::vector<LinAlgProgramCode> const_functions_;
285  std::vector<LinAlgProgramCode> single_functions_;
286 
287  ParserProgramHandle pprogram_;
288 
289 };
290 
292 
293  public:
294  // The interpreter Creates executable code from the parsed code
295  // The first step is setting the data sources and sinks
296 
297 
298  //------------------------------------------------------------------------
299  // Step 0 : create program variable
300  bool create_program(LinAlgProgramHandle& mprogram,std::string& error);
301 
302 
303  //------------------------------------------------------------------------
304  // Step 1: add sources and sinks
305 
306  // Matrix sources
307  bool add_matrix_source(LinAlgProgramHandle& pprogram,
308  std::string& name,
309  MatrixHandle& matrix,
310  std::string& error);
311 
312  // Matrix sinks
313  bool add_matrix_sink(LinAlgProgramHandle& pprogram,
314  std::string& name,
315  std::string& error);
316 
317  //------------------------------------------------------------------------
318  // Step 2: translate code and generate executable code
319 
320  // Main function for transscribing the parser output into a program that
321  // can actually be executed
322  bool translate(ParserProgramHandle& pprogram,
323  LinAlgProgramHandle& mprogram,
324  std::string& error);
325 
326  //------------------------------------------------------------------------
327  // Step 3: Run the code
328 
329  bool run(LinAlgProgramHandle& mprogram,std::string& error);
330 };
331 
332 }
333 
334 #endif
LinAlgProgramCode(bool(*function)(LinAlgProgramCode &pc, std::string &err))
Definition: LinAlgInterpreter.h:107
LinAlgProgramVariable(std::string name, MatrixHandle handle)
Definition: LinAlgInterpreter.h:179
LockingHandle< Matrix< double > > MatrixHandle
Definition: MatrixFwd.h:55
Handle< LinAlgProgram > LinAlgProgramHandle
Definition: LinAlgInterpreter.h:61
virtual ~LinAlgFunction()
Definition: LinAlgInterpreter.h:83
Definition: LinAlgInterpreter.h:103
LinAlgProgramVariableHandle get_single_variable(size_t j)
Definition: LinAlgInterpreter.h:254
MatrixHandle * get_handle()
Definition: LinAlgInterpreter.h:211
MatrixHandle get_matrix()
Definition: LinAlgInterpreter.h:210
MatrixHandle & handle()
Definition: LinAlgInterpreter.h:185
void set_const_variable(size_t j, LinAlgProgramVariableHandle &handle)
Definition: LinAlgInterpreter.h:247
#define SCISHARE
Definition: share.h:39
void set_matrix(MatrixHandle matrix)
Definition: LinAlgInterpreter.h:209
LinAlgProgramCode()
Definition: LinAlgInterpreter.h:110
void resize_const_functions(size_t sz)
Definition: LinAlgInterpreter.h:240
LinAlgProgramVariableHandle get_const_variable(size_t j)
Definition: LinAlgInterpreter.h:252
ParserProgramHandle get_parser_program()
Definition: LinAlgInterpreter.h:271
void set_const_program_code(size_t j, LinAlgProgramCode &pc)
Definition: LinAlgInterpreter.h:258
bool run(std::string &err)
Definition: LinAlgInterpreter.h:145
MatrixHandle * get_handle()
Definition: LinAlgInterpreter.h:184
const char * name[]
Definition: BoostGraphExampleTests.cc:87
MatrixHandle * get_handle(size_t j)
Definition: LinAlgInterpreter.h:135
Definition: LinAlgInterpreter.h:196
bool is_matrix()
Definition: LinAlgInterpreter.h:212
void set_single_program_code(size_t j, LinAlgProgramCode &pc)
Definition: LinAlgInterpreter.h:260
LinAlgProgram()
Definition: LinAlgInterpreter.h:225
int ref_cnt
Definition: LinAlgInterpreter.h:222
LinAlgProgramSource()
Definition: LinAlgInterpreter.h:206
boost::shared_ptr< ParserProgram > ParserProgramHandle
Definition: Parser.h:70
Definition: LinAlgInterpreter.h:204
bool(*)(LinAlgProgramCode &pc, std::string &err) get_function()
Definition: LinAlgInterpreter.h:120
MatrixHandle matrix
Definition: LinAlgInterpreter.h:201
void set_parser_program(ParserProgramHandle handle)
Definition: LinAlgInterpreter.h:270
Definition: LinAlgInterpreter.h:166
void resize_const_variables(size_t sz)
Definition: LinAlgInterpreter.h:235
void resize_single_functions(size_t sz)
Definition: LinAlgInterpreter.h:242
Handle< LinAlgProgramVariable > LinAlgProgramVariableHandle
Definition: LinAlgInterpreter.h:54
Definition: LinAlgInterpreter.h:291
int ref_cnt
Definition: LinAlgInterpreter.h:170
Definition: LinAlgInterpreter.h:219
void set_handle(size_t j, MatrixHandle *variable)
Definition: LinAlgInterpreter.h:127
void set_single_variable(size_t j, LinAlgProgramVariableHandle &handle)
Definition: LinAlgInterpreter.h:249
LinAlgProgramVariable(std::string name)
Definition: LinAlgInterpreter.h:173
int ref_cnt
Definition: LinAlgInterpreter.h:200
bool(*)(LinAlgProgramCode &pc, std::string &err) get_function()
Definition: LinAlgInterpreter.h:86
void set_function(bool(*function)(LinAlgProgramCode &pc, std::string &err))
Definition: LinAlgInterpreter.h:114
MatrixHandle & handle(size_t j)
Definition: LinAlgInterpreter.h:138
void resize_single_variables(size_t sz)
Definition: LinAlgInterpreter.h:237
MatrixHandlePointer()
Definition: LinAlgInterpreter.h:198
Definition: Parser.h:419
Definition: LinAlgInterpreter.h:72