Advertisement
Guest User

Untitled

a guest
Mar 24th, 2011
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <time.h>
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. #define LAYERS 100
  8. #define NEURONS 100
  9. #define INPUTS 100
  10.  
  11. unsigned long long getRealNanosecondsCount(timespec time)
  12. {
  13.     return (unsigned long long)time.tv_sec * 1000000000 + time.tv_nsec;
  14. }
  15.  
  16. float sigmoid(float x)
  17. {
  18.   return x/100;
  19. }
  20.  
  21. int main()
  22. {
  23.   struct timespec current_time;
  24.   struct timespec next_time;
  25.  
  26.   srand((unsigned)time(0));
  27.  
  28.   // array of inputs weights
  29.   float ** weights = new float * [LAYERS];
  30.   for(int i=0;i<LAYERS;i++)
  31.   {
  32.       weights[i] = new float[NEURONS*INPUTS];
  33.  
  34.       for(int j=0;j<NEURONS*INPUTS;j++)
  35.       {
  36.         weights[i][j] = (float)rand() / RAND_MAX;
  37.       }
  38.   }
  39.   //--------------------------
  40.   // array of inputs values
  41.   float * inputs = new float[INPUTS];
  42.   for(int i=0;i<INPUTS;i++)
  43.   {
  44.     inputs[i] = (float)rand() / RAND_MAX;
  45.   }
  46.   //---------------------------
  47.   // array of outputs
  48.   float * outputs = new float[NEURONS];
  49.   //---------------------------
  50.   float * temp = new float[INPUTS*NEURONS];
  51.  
  52.   cout << "Memory for weights: " << sizeof(float)*LAYERS*NEURONS*INPUTS << endl;
  53.   cout << "Memory for inputs: " << sizeof(float)*INPUTS << endl;
  54.   cout << "Memory for outputs: " << sizeof(float)*NEURONS << endl;
  55.   cout << "Memory for temp: " << sizeof(float)*NEURONS*INPUTS << endl;
  56.  
  57.   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &current_time);
  58.  
  59.   for(int i=0;i<LAYERS;i++)
  60.   {
  61.     #pragma omp parallel for
  62.     for(int j=0;j<INPUTS*NEURONS;j++)
  63.     {
  64.       temp[j] = inputs[j%INPUTS] * weights[i][j];
  65.     }
  66.    
  67.     #pragma omp parallel for
  68.     for(int j=0;j<NEURONS;j++)
  69.     {
  70.       inputs[j] = 0;
  71.      
  72.       for(int l=0;l<INPUTS;l++)
  73.       {
  74.         inputs[j] += temp[j*INPUTS + l];
  75.       }
  76.      
  77.       inputs[j] = sigmoid(inputs[j]);
  78.     }
  79.   }
  80.  
  81.   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &next_time);
  82.  
  83.   for(int i=0;i<NEURONS;i++)
  84.   {
  85.     cout << inputs[i] << endl;
  86.   }
  87.  
  88.   delete[] temp;
  89.   delete[] outputs;
  90.   delete[] inputs;
  91.  
  92.   for(int i=0;i<LAYERS;i++)
  93.   {
  94.       delete[] weights[i];
  95.   }
  96.   delete[] weights;
  97.  
  98.   cout << "Time: " << getRealNanosecondsCount(next_time) - getRealNanosecondsCount(current_time) << endl;
  99.  
  100.   return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement