Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <time.h>
- #include <iostream>
- using namespace std;
- #define LAYERS 100
- #define NEURONS 100
- #define INPUTS 100
- unsigned long long getRealNanosecondsCount(timespec time)
- {
- return (unsigned long long)time.tv_sec * 1000000000 + time.tv_nsec;
- }
- float sigmoid(float x)
- {
- return x/100;
- }
- int main()
- {
- struct timespec current_time;
- struct timespec next_time;
- srand((unsigned)time(0));
- // array of inputs weights
- float ** weights = new float * [LAYERS];
- for(int i=0;i<LAYERS;i++)
- {
- weights[i] = new float[NEURONS*INPUTS];
- for(int j=0;j<NEURONS*INPUTS;j++)
- {
- weights[i][j] = (float)rand() / RAND_MAX;
- }
- }
- //--------------------------
- // array of inputs values
- float * inputs = new float[INPUTS];
- for(int i=0;i<INPUTS;i++)
- {
- inputs[i] = (float)rand() / RAND_MAX;
- }
- //---------------------------
- // array of outputs
- float * outputs = new float[NEURONS];
- //---------------------------
- float * temp = new float[INPUTS*NEURONS];
- cout << "Memory for weights: " << sizeof(float)*LAYERS*NEURONS*INPUTS << endl;
- cout << "Memory for inputs: " << sizeof(float)*INPUTS << endl;
- cout << "Memory for outputs: " << sizeof(float)*NEURONS << endl;
- cout << "Memory for temp: " << sizeof(float)*NEURONS*INPUTS << endl;
- clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ¤t_time);
- for(int i=0;i<LAYERS;i++)
- {
- #pragma omp parallel for
- for(int j=0;j<INPUTS*NEURONS;j++)
- {
- temp[j] = inputs[j%INPUTS] * weights[i][j];
- }
- #pragma omp parallel for
- for(int j=0;j<NEURONS;j++)
- {
- inputs[j] = 0;
- for(int l=0;l<INPUTS;l++)
- {
- inputs[j] += temp[j*INPUTS + l];
- }
- inputs[j] = sigmoid(inputs[j]);
- }
- }
- clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &next_time);
- for(int i=0;i<NEURONS;i++)
- {
- cout << inputs[i] << endl;
- }
- delete[] temp;
- delete[] outputs;
- delete[] inputs;
- for(int i=0;i<LAYERS;i++)
- {
- delete[] weights[i];
- }
- delete[] weights;
- cout << "Time: " << getRealNanosecondsCount(next_time) - getRealNanosecondsCount(current_time) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement