CARLsim  6.1.0
CARLsim: a GPU-accelerated SNN simulator
PTI.cpp
Go to the documentation of this file.
1 #include "PTI.h"
2 #include "ParameterInstances.h"
3 
4 #include <cassert>
5 #include <cstdlib>
6 #include <cstring>
7 #include <fstream>
8 #include <iostream>
9 #include <memory>
10 #include <stdexcept>
11 #include <string>
12 #include <typeinfo>
13 
14 using namespace std;
15 
16 struct PTI::PTIImpl {
17  ostream &outputStream;
18  const unique_ptr<ParameterInstances> instances;
19 
20  PTIImpl(const char* const fileName, istream &defaultInputStream, ostream &outputStream):
21  outputStream(outputStream),
22  instances(loadParameterInstances(fileName, defaultInputStream)) {
23  }
24 
25  static const char* getStringArgument(const char* const option, const int argc, const char* const argv[]) {
26  assert(option != NULL);
27  for (int i = 0; i < argc - 1; i++) {
28  if (0 == strcmp(option, argv[i]))
29  return argv[i+1];
30  }
31  return NULL;
32  }
33 
34  static int getIntegerArgument(const char* const option, const int argc, const char* const argv[]) {
35  assert(option != NULL);
36  if (argc < 0)
37  throw invalid_argument(string("PTI::PTIImpl: argc is negative."));
38  if (argv == NULL)
39  throw invalid_argument(string("PTI::PTIImpl: argv is NULL."));
40  for (int i = 0; i < argc - 1; i++) {
41  if (0 == strcmp(option, argv[i]))
42  return atoi(argv[i+1]);
43  }
44  return -1;
45  }
46 
49  static ParameterInstances* loadParameterInstances(const char* const fileName, istream &defaultInputStream) {
50  // I asked the following SO question while deciding how to write this method: http://stackoverflow.com/questions/23049166/initialize-polymorphic-variable-on-stack
51  istream * const input(fileName ? new ifstream(fileName, ifstream::in) : &defaultInputStream);
52 
53  if (fileName) {
54  if (!dynamic_cast<ifstream*>(input)->is_open())
55  throw invalid_argument(string("PTI::PTIImpl: Failed to open file") + string(fileName) + string("."));
56  }
57 
58  ParameterInstances* const result = new ParameterInstances(*input);
59 
60  if (fileName) {
61  dynamic_cast<ifstream*>(input)->close();
62  delete(input);
63  }
64  return result;
65  }
66 
67  bool repOK() const {
68  return instances.get() != NULL;
69  }
70 };
71 
72 PTI::PTI(const int argc, const char* const argv[], ostream &outputStream):
73  impl(new PTIImpl(PTIImpl::getStringArgument("-f", argc, argv), cin, outputStream)) {
74 
75  assert(repOK());
76 }
77 
78 PTI::PTI(const int argc, const char* const argv[], ostream &outputStream, istream &defaultInputStream):
79  impl(new PTIImpl(PTIImpl::getStringArgument("-f", argc, argv), defaultInputStream, outputStream)) {
80 
81  assert(repOK());
82 }
83 
84 // Empty destructor allows us to get away with defining the pimpl in an auto_ptr. See http://stackoverflow.com/questions/311166/stdauto-ptr-or-boostshared-ptr-for-pimpl-idiom
85 PTI::~PTI() { };
86 
87 string PTI::usage() const {
88  return string("\nThis program should be called with 3 arguments:\n\n") +
89  string("./a.out [filename]\n\n") +
90  string("<filename> is the name of the csv parameter file to be read.\n\n") +
91  string("Format of csv file: Each row represents a single \nindividual, while \
92  each csv represents a min or max value for a parameter. \nEach csv is a float.\
93  If there are 4 individuals with 4 parameters, \nthen there will be four rows, \
94  each with 8 csv (2 for each parameter).\n\n");
95 }
96 
97 void PTI::runExperiment(const Experiment& experiment) const {
98  experiment.run(*(impl.get()->instances.get()), impl.get()->outputStream);
99  assert(repOK());
100 }
101 
102 bool PTI::repOK() const {
103  return impl.get()->repOK();
104 }
string usage() const
Definition: PTI.cpp:87
PTI(const int argc, const char *const argv[], ostream &outputStream)
Definition: PTI.cpp:72
bool repOK() const
Definition: PTI.cpp:102
~PTI()
Definition: PTI.cpp:85
PTIImpl(const char *const fileName, istream &defaultInputStream, ostream &outputStream)
Definition: PTI.cpp:20
static const char * getStringArgument(const char *const option, const int argc, const char *const argv[])
Definition: PTI.cpp:25
ostream & outputStream
Definition: PTI.cpp:17
static int getIntegerArgument(const char *const option, const int argc, const char *const argv[])
Definition: PTI.cpp:34
bool repOK() const
Definition: PTI.cpp:67
static ParameterInstances * loadParameterInstances(const char *const fileName, istream &defaultInputStream)
Definition: PTI.cpp:49
void runExperiment(const Experiment &experiment) const
Definition: PTI.cpp:97
const unique_ptr< ParameterInstances > instances
Definition: PTI.cpp:18
virtual void run(const ParameterInstances &parameters, std::ostream &outputStream) const =0