Aquila  2.0 prealpha
Cognitive Robotics Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
esn.h
Go to the documentation of this file.
1 //##############################################################################################################################################################################################################//
2 //Aquila - An Open-Source GPU-Accelerated Toolkit for Cognitive and Neuro-Robotics Research //
3 // //
4 //Copyright (c) <2012>, Anthony Morse //
5 //All rights reserved. //
6 // //
7 //Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: //
8 // //
9 // - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. //
10 // - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. //
11 // //
12 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR //
13 //A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT //
14 //LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR //
15 //TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. //
16 // //
17 //The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted //
18 //as representing official policies,either expressed or implied, of the FreeBSD Project. //
19 //##############################################################################################################################################################################################################//
20 
21 #ifndef ESN_H
22 #define ESN_H
23 
24 #define MAX_GPU_DEVICES 4
25 
26 //Tony: I would make these variables and initialse them from the configuration file, then you can dynamically change your ESNs, Martin
27 #define ESN_INPUT_SIZE 14 //the number of inputs to the ESN - 14 sensors for the emotive epoch headset
28 #define ESN_OUTPUT_SIZE 2 //the number of outputs to the ESN - ?
29 #define ESN_SPARCE 20 //the probability (%) of each connection
30 #define ESN_STRENGTH 0.2 //the range +- of each connection (actual strength is random within this range)
31 #define FEEDBACK 0 //switch feedback on (1) or off (0)
32 #define LEARNING_RATE 0.01 //the learning rate for percetron training
33 
34 #include <vector>
35 #include <string>
36 #include <cuda.h>
37 #include <cuda_runtime.h>
38 #include "interface.h"
39 
40 using namespace std;
41 
42 class ESN : public yarp::os::Thread
43 {
44 public:
45  ESN(Interface *pInterface);
46  int esnSize;
47 
48 private:
49  Interface *intrfc;
50 
51  cudaDeviceProp deviceInfo[MAX_GPU_DEVICES];
52  size_t inputActivitySize;
53  size_t activitySize;
54  size_t outputActivitySize;
55  size_t inputWeightsSize;
56  size_t weightsSize;
57  size_t outputWeightsSize;
58 
59  int P2P;
60  int debug;
61  int availableGPUs;
62  int requestedGPUs;
63  int inputSource;
64  int stepCounter;
65  int gpuDevice[MAX_GPU_DEVICES];
66  float *weights;
67  float *activity;
68  float *newActivity;
69  float *inputWeights;
70  float *inputActivity;
71  float *newInputActivity;
72  float *outputWeights;
73  float *outputActivity;
74  float *newOutputActivity;
75  float *PERCoutputWeights;
76  float *PERCinputWeights;
77  float *PERCoutputTarget;
78  float *PERCinputTarget;
79  bool GPU;
80 
81  void run();
82  bool threadInit();
83  void step();
84  void calculateActivity(float *activity, int N, float *weights, float *newActivity, int noElements);
85  void PERCweightsUpdate(float *activity, float *PERCactivity, float *PERCweights, float *target, float learningRate, int noPercUnits, int size);
86  void finishStep (float *newActivity, float *activity, int noElements, float *newOutActivity, float *outActivity, int noOutElements, float *newInputActivity, float *inputActivity, int noInputElements, int inputClamp, int outputClamp);
87 
88 public:
90 
91  void printGPUProperties(int deviceID);
92  void printOptions();
93  void printConfiguration();
94 
95  void setGPUMode(bool gpuMode);
96  void setDevice(int deviceID);
97  void setDevices(int deviceID[MAX_GPU_DEVICES]);
98  void setDebuggingLevel(int level);
99  void setInputSource(int source);
100 
101  void getActivityAsBottle(Bottle *bottle);
102  int getDevice();
103  int getNumDevices();
104  int getDebuggingLevel();
105  vector<string> queryGPU();
106 };
107 
108 //GPU kernel wrappers
109 
110 #endif//ESN_H