tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
jobs.h
1 // -*- mode: c++; fill-column: 80; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  Copyright (c) 2010 Juan Jose Garcia Ripoll
4 
5  Tensor is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published
7  by the Free Software Foundation; either version 2 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 Library General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 
21 #ifndef TENSOR_JOBS_H
22 #define TENSOR_JOBS_H
23 
24 #include <string>
25 #include <vector>
26 #include <tensor/refcount.h>
27 #include <tensor/sdf.h>
28 
29 namespace jobs {
30 
31  class Job {
32 
33  public:
34  Job(int argc, const char **argv);
35 
36  tensor::index number_of_jobs() const { return number_of_jobs_; };
37  tensor::index current_job() const { return this_job_; };
38  void operator++();
39  bool to_do();
40 
41  double get_value_with_default(const std::string &variable, double def) const;
42  double get_value(const std::string &variable) const;
43  void dump_variables(sdf::OutDataFile &file) const;
44 
45  typedef tensor::shared_ptr<sdf::OutDataFile> dataset;
46  dataset open_dataset(const std::string &filename) const;
47  bool dataset_record_exists(const std::string &filename) const;
48 
49  private:
50  class Variable {
51  public:
52  Variable(const std::string name, double min, double max, tensor::index n = 10) :
53  name_(name), values_(tensor::linspace(min, max, n)), which_(0)
54  {}
55  Variable() :
56  name_(""), values_(), which_(0)
57  {}
58 
59  const std::string &name() const { return name_; }
60  const tensor::RTensor &values() const { return values_; }
61  tensor::index size() const { return values_.size(); }
62  void select(tensor::index i) { which_ = i; }
63  double value() const { return values_[which_]; }
64 
65  private:
66  std::string name_;
67  tensor::RTensor values_;
68  tensor::index which_;
69  };
70 
71  typedef std::vector<Variable> var_list;
72 
73  static const Variable no_variable;
74  std::string filename_;
75  var_list variables_;
76  tensor::index number_of_jobs_;
77  tensor::index this_job_, first_job_, last_job_;
78 
79  const Variable *find_variable(const std::string &name) const;
80  static const Variable parse_line(const std::string &s);
81  static int parse_file(std::istream &s, std::vector<Variable> &data);
82  tensor::index compute_number_of_jobs() const;
83  void select_job(tensor::index which);
84  };
85 
86 } // namespace jobs
87 
88 #endif // ! TENSOR_JOBS_H
const RTensor linspace(double min, double max, index n=100)
Vector of 'n' equally spaced numbers in the interval [min, max].
Definition: linspace_d.cc:27
Real Tensor with elements of type "double".
Definition: tensor.h:349
index size() const
Returns total number of elements in Tensor.
Definition: tensor.h:114