Aquila  2.0 prealpha
Cognitive Robotics Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
som.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>, <Martin Peniak - www.martinpeniak.com> //
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 SELF_ORGANISING_MAP_H
22 #define SELF_ORGANISING_MAP_H
23 #define MAX_GPU_DEVICES 4
24 
25 #include <vector>
26 #include <cuda.h>
27 #include <cuda_runtime.h>
28 #include "interface.h"
29 
30 using namespace std;
31 
32 class SOM : public yarp::os::Thread
33 {
34 public:
35  SOM(Interface *pInterface);
36 
37 private:
38  Interface *intrfc;
39 
40  cudaDeviceProp deviceInfo[MAX_GPU_DEVICES];
41  string trainingFileName;
42  string mapFileName;
43  size_t usedMemory;
44 
45  //host memory pointers
46  int *winner_h;
47  float *input_h;
48  float *weight_h;
49  float *output_h;
50  float *average_h;
51  float *distance_h;
52 
53  //device memory pointers
54  int *winner_d;
55  float *input_d;
56  float *weight_d;
57  float *output_d;
58  float *average_d;
59  float *distance_d;
60 
61  int debug;
62  int gpuDevice;
63  int seed;
64  int numThreads;
65  int numBlocks;
66  int maxThreads;
67  int numInputs;
68  int numSamples;
69  int numOutputs;
70  int numSubiterations;
71  int iterationPause;
72  int numIterations;
73  int neighbourhoodSize;
74  int currentNeighbourhoodSize;
75  int randomSequenceId;
76  float trainingTime;
77  float highestDataPoint;
78  float lowestDataPoint;
79  float initLearningRate;
80  float sigma;
81  bool GPU;
82  bool visualiseProgress;
83 
84  void run();
85  void setNumBlocks();
86  void allocateMemory();
87  void freeMemory();
88  void updateProgress();
89  void findBestMatch(float *outputs, int *winner, int numOutputs);
90  void propogateInput(float *inputs, float *weights, float *outputs, int numInputs, int sequenceId, int numOutputs);
91  void updateWeights(float *inputs, float *weights, int *winner, float sigma, int numInputs, int sequenceId, int numOutputs, int neighbourhoodSize, float initLearningRate,int numIterations, int currentIteration);
92  float scaleRange(float in, float oldMin, float oldMax, float newMin, float newMax);
93 
94 public:
95  vector<string> queryGPU();
96  void loadTrainingSet(Bottle portData = 0);
97  void randomiseWeights();
98  void saveMap();
99  void printOptions();
100  void printGPUProperties(int deviceID);
101 
102  void setGPUMode(bool gpuMode);
103  void setDebugMessages(int level);
104  void setDevice(int device);
105  void setSeed(int value);
106  void setMaxThreads(int threads);
107  void setNumInputs(int inputs);
108  void setNumSamples(int samples);
109  void setNumOutputs(int outputs);
110  void setNumSubiterations(int subIterations);
111  void setInitLearningRate(float learningRate);
112  void setSigma(float sigma);
113  void setVisualiseProgress(bool visualise);
114  void setIterationPause(int value);
115  void setNeighbourhoodSize(int value);
116  void setNumIterations(int value);
117  void setTrainingFile(string fileName);
118  void setMapFile(string fileName);
119 
120  void getWeightsAsBottle(Bottle *bottle);
121  int getDevice();
122  int getDebugMessages();
123  int getSeed();
124  int getMaxThreads();
125  int getNumInputs();
126  int getNumSamples();
127  int getNumOutputs();
128  int getNeighbourhoodSize();
129  int getNumSubiterations();
130  int getIterationPause();
131  float getInitLearningRate();
132  float getSigma();
133  float getTrainingTime();
134  float getLowestDataPoint();
135  float getHighestDataPoint();
136  bool getVisualiseProgress();
137  string getTrainingFile();
138  string getMapFile();
139 };
140 
141 //GPU kernel wrappers
142 void findBestMatchOnDevice(dim3 grid, dim3 block, float *outputs, int *winner, int numOutputs);
143 void propogateInputOnDevice(dim3 grid, dim3 block, float *inputs, float *weights, float *outputs, int numInputs, int sequenceId, int numOutputs);
144 void updateWeightsOnDevice(dim3 grid, dim3 block, float *inputs, float *weights, int *winner, float sigma, int numInputs, int sequenceId, int numOutputs, int neighbourhoodSize, float initLearningRate,int numIterations, int currentIteration);
145 
146 #endif//SELF_ORGANISING_MAP_H