-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpx-net-init.cpp
164 lines (152 loc) · 4.38 KB
/
hpx-net-init.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* Copyright (c) 2013 Michael LeSane
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
//XOR tests
float tests[][2] =
{
{0.0,0.0},
{0.0,1.0},
{1.0,0.0},
{1.0,1.0}
};
//XOR targets
float targets[][1] =
{
{0.0},
{1.0},
{1.0},
{0.0}
};
//parallelism threshold values for forward passes and backpropagation.
//after # of neuron rows iterated during the respective processes exceed these values, execution is purely serial.
int FORWARD_THRESHOLD,
BACKPROP_THRESHOLD;
//generates a random float between 0 and 1.
double rnd()
{
return ( (double)rand() * ( 1 - 0 ) ) / (double)RAND_MAX + 0;
}
//sigmoid function -- hyperbolic tangent
float f(float x)
{
return tanh(x);
}
//derivative of sigmoid function
float df(float x)
{
return 1.0-pow(x,2.0);
}
//initialization -- seeds random number generator
void init()
{
unsigned int seed;
FILE* urandom = fopen("/dev/urandom", "r");
fread(&seed, sizeof(int), 1, urandom);
fclose(urandom);
srand(seed);
}
//purely serial, calculates dot product of activation-value and weight vectors.
float productsum(std::vector<float> roots, std::vector<float> weights)
{
float out = 0;
for(int i = 0; i < (int)roots.size(); i++)
out += roots[i]*weights[i];
return out;
}
//returns a vector of futures with dataflow such that the final value of each will be plugged into the sigmoid function.
std::vector<hpx::lcos::future<float>> extract_future_roots(std::vector<neuron> contents)
{
std::vector<hpx::lcos::future<float>> out;
for(int i = 0; i < (int)contents.size(); i++)
out.push_back(contents[i].get_f_future());
return out;
}
//HPX_PLAIN_ACTION(extract_future_roots,extract_future_roots_action);
//purely parallel equivalent of productsum().
float future_productsum(std::vector<neuron> prev, std::vector<float> weights)
{
// return 0;
// extract_future_roots_action efr;
// hpx::lcos::future<std::vector<hpx::lcos::future<float>>> future_roots = hpx::async(efr,hpx::find_here(),prev);
// hpx::lcos::future<std::vector<hpx::lcos::future<float>>> future_roots = hpx::async(&extract_future_roots,prev);
return productsum(future_get_roots(prev),weights);
/* hpx::lcos::future<float> out = hpx::lcos::local::dataflow
(
hpx::util::unwrapped
( [] (std::vector<hpx::lcos::future<float>> roots,
std::vector<float> weights)
{
hpx::lcos::future<float> out = hpx::lcos::make_ready_future((float)0.0);
for(int i = 0; i < (int)roots.size(); i++)
{
hpx::lcos::future<float> add = hpx::lcos::local::dataflow
(
hpx::util::unwrapped
( [] (float a, float b)
{
return a*b;
}
),roots[i],hpx::lcos::make_ready_future((float)weights[i])
);
out = hpx::lcos::local::dataflow
(
hpx::util::unwrapped
( [] (float a, float b)
{
return a+b;
}
),out,add
);
}
return out;
}
),
hpx::lcos::make_ready_future(extract_future_roots(prev)),
//future_roots, //too parallel?
hpx::lcos::make_ready_future(weights)
);
return out.get();
/**/
}
//extracts activations from a vector of neurons, returns as a vector
std::vector<float> extract_roots(std::vector<neuron> contents)
{
std::vector<float> result;
for(int i = 0; i < (int)contents.size(); i++)
result.push_back(contents[i].value);
return result;
}
//HPX_PLAIN_ACTION(extract_roots,extract_roots_action);
//waits on futures and extracts activations from a vector of neurons, returns as a vector
std::vector<float> future_get_roots(std::vector<neuron> contents)
{
std::vector<float> result;
for(int i = 0; i < (int)contents.size(); i++)
result.push_back(contents[i].get_value());
return result;
}
float calc_hidden_error(neuron_row next,int j)
{
float error = 0;
for (int k = 0; k < (int)next.size(); k++)
if (next.contents[k].bias) continue;
else error += next.contents[k].get_delta() * next.contents[k].weights[j];
return error;
}
//HPX_PLAIN_ACTION(calc_hidden_error,calc_hidden_error_action);
hpx::lcos::future<float> future_hidden_error(neuron_row next,int j)
{
//calc_hidden_error_action che;
//hpx::lcos::future<float> result = hpx::async(che,hpx::find_here(),next,j);
hpx::lcos::future<float> result = hpx::async(&calc_hidden_error,next,j);
return result;
}
//converts a float array to a float vector.
std::vector<float> to_vector(float x[],int s)
{
std::vector<float> out;
for(int i = 0; i < s; i++) out.push_back(x[i]);
return out;
}