-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAi.cpp
373 lines (354 loc) · 12.5 KB
/
Ai.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <iostream>
#include <pthread.h>
#include <future>
#include <string.h>
#include <cstring>
#include <string>
#include <fstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <semaphore.h>
#include <string>
#include <cstring>
using namespace std;
//this is the mutex to lock the threads
pthread_mutex_t m;
//global string which writes data on to the named pipe
string Ag_str;
double* Array_g;
//this is the structure which is used to store weights or inputs
struct input
{
public:
double *inp;
int total = 0;
//functions
input();
input(int size);
void setsize(int s);
void setval(double val);
double getval(int index);
void display();
};
input::input()
{
inp = NULL;
total = 0;
}
input::input(int size)
{
inp = new double[size];
total = 0;
}
void input::setsize(int s)
{
inp = new double[s];
}
void input::setval(double val)
{
inp[total] = val;
total++;
}
double input::getval(int index)
{
if (index < total)
{
return this->inp[index];
}
double n=-999.9999;
return n;
}
void input::display()
{
for (int i = 0; i < total; i++)
cout << inp[i] << " ";
cout << endl;
}
// hidden neuron=hd
struct hn
{
public:
double *input;
double *weight;
int size;
int fin;
int file_descriptor;
};
int read(string s, input &in, int neurons)
{
ifstream file;
file.open(s);
string st;
int count = 1;
int a = 1, i = 0, move = 0;
getline(file, st);
cout << st << '\n';
while (st[i] != '\0')
{
if (st[i] == ',')
count++;
i++;
}
in.setsize(count);
// getline(file,st);
file.close();
cout << "total input " << count << endl;
i = 0;
char *d = new char[10];
cout << st << endl;
while (st[i] != '\0')
{
cout << st[i] << endl;
while (st[i] != '\0' && st[i] != ',')
{
if (st[i] != ',')
{
d[move++] = st[i];
i++;
}
}
d[move] = '\0';
// cout<<d<<endl;
// cout<<stod(d)<<endl;
double v = stod(d);
in.setval(v);
move = 0;
if (st[i] == ',')
i++;
if (st[i] == '\0')
break;
}
in.display();
return count;
}
void *calc_h1(void *arg)
{
pthread_mutex_lock(&m);
hn *obj = (hn *)arg;
double *sum = new double;
*sum = 0;
for (int i = 0; i < obj->fin; i++)
{
cout << *obj->weight << " " << obj->input[i] << endl;
*sum += (*obj->weight) * obj->input[i];
// cout<<"sum:"<<*sum<<'\n';
}
double wr = *sum;
double rw = wr;
string temps=to_string(rw);
Ag_str+=temps;
Ag_str+=',';
// cout<<"Total sum:"<<*sum<<" "<<wr<<" "<<rw<<'\n';
// ssize_t bytes_written = write(obj->file_descriptor, &rw, sizeof(rw));
// if (bytes_written == -1)
// {
// perror("write error");
// cout << "Error code: " << errno << ", Message: " << strerror(errno) << endl;
// }
// cout << "\nBytes written : " << bytes_written << '\n';
cout << "Total sum:" << *sum << " " << wr << " " << rw << '\n';
pthread_mutex_unlock(&m);
pthread_exit(NULL);
}
string read_from_pipe()
{
int fd = open("pipe", O_RDONLY);
if (fd < 0) {
perror("open");
exit(1);
}
const int BUF_SIZE = 1024;
char buf[BUF_SIZE];
ssize_t num_bytes_read;
const char* msg;
while ((num_bytes_read = read(fd, buf, BUF_SIZE)) > 0) {
msg = buf;
//std::cout << "Received message: " << msg << std::endl;
}
//cout<<"\n_________________messAge is : "<<msg<<'\n';
if (num_bytes_read == -1) {
perror("read");
exit(1);
}
close(fd);
string A_str=msg;
A_str+='\0';
return A_str;
}
int main(int argc,char*argv[])
{
// cout<<"\nThe File Running is : "<<argv[0]<<' '<<argv[1]<<'\n';
char* a=argv[1];
int input_neurons;
int hidden_layers = 3;
int h1_neurons=7;
int h2_neurons = 7;
int h3_neurons = 7;
int h4_neurons = 7;
int h5_neurons = 7;
if(*a=='1')//for forward_propagation
{
pthread_mutex_init(&m,NULL);
input i_obj;
// read from the file
input_neurons=read("input.txt", i_obj, input_neurons);
cout << "\ntotal:" << i_obj.total << '\n'; // can change the total here again
input h_obj;
h1_neurons=read("h1.txt", h_obj, h1_neurons);
hn *send = new hn;
send->input = new double[i_obj.total];
for (int i = 0; i < i_obj.total; i++)
{
send->input[i] = i_obj.inp[i];
}
send->size = h_obj.total;
send->fin = i_obj.total;
// make a named pipe here
mkfifo("pipe", 0666);
if (access("pipe", F_OK) == -1)
{
// named pipe doesn't exist
perror("access");
exit(EXIT_FAILURE);
}
if (access("pipe", R_OK) == -1)
{
// named pipe exists but isn't readable
perror("access");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid > 0)
{
// send->file_descriptor = file_descptr;
// now create threads for this first process and calculate the values;
// double* pipe_data=new double[h_obj.total];//data to be recived
pthread_t id[h_obj.total]; // number of threads = number of input neurons
Array_g=new double[h_obj.total];
for (int i = 0; i < h_obj.total; i++)
{
void *status;
send->weight = new double;
*send->weight = h_obj.getval(i);
cout << "he;; " << *send->weight << endl;
send->file_descriptor=i;
pthread_create(&id[i], NULL, calc_h1, (void *)send);
pthread_join(id[i], NULL);
// double* sum=(double*)status;
// cout<<"\nSum is :"<<*sum<<'\n';
}
Ag_str+='*';
string Atemp=to_string(hidden_layers);
Ag_str+=Atemp;
Ag_str+='*';
Ag_str+='\0';
const char *char_array = Ag_str.c_str();
cout<<"\nString is : "<<Ag_str<<'\n';
int file_descptr = open("pipe", O_WRONLY);
ssize_t bytes_written = write(file_descptr,char_array, strlen(char_array));
cout << "\nBytes written : " << bytes_written << '\n';
close(file_descptr);
wait(nullptr);
}
else if (pid == 0)
{
execlp("./h", "1","3", NULL);
}
}
else if(*a=='0')//for back_propagation to forward_propgation
{
int pid=fork();
if(pid>0)
{
string A_str=read_from_pipe();
//cout<<"\nMessage : "<<A_str<<'\n';
int ss=0;
double* Arr=new double[20];
int j=0;
int layers_on_pipe;
int count=0;
while(A_str[ss]!='*')
{
string Atemp;
while(A_str[ss]!=',')
{
Atemp+=A_str[ss];
++ss;
}
if(A_str[ss]==','){++count;}
double n=stod(Atemp);
Arr[j]=n;
++j;
++ss;
if(A_str[ss]=='*')
{
++ss;
string At;
// cout<<"come"<<'\n';
while(A_str[ss]!='*')
{
At+=A_str[ss];
++ss;
}
layers_on_pipe=stoi(At);
}
}
cout<<'\n';
cout<<"\nInput Layer\nNew Weights Are : ";
for(int i=0;i<count;i++)
{
cout<<Arr[i]<<' ';
}
cout<<'\n';
input i_obj;
// read from the file
read("output.txt", i_obj, count);
cout << "\ntotal:" << i_obj.total << '\n'; // can change the total here again
input h_obj;
read("h1.txt", h_obj, h1_neurons);
hn *send = new hn;
send->input = new double[i_obj.total];
for (int i = 0; i < i_obj.total; i++)
{
send->input[i] = i_obj.inp[i];
}
send->size = h_obj.total;
send->fin = i_obj.total;
pthread_t id[h_obj.total]; // number of threads = number of input neurons
for (int i = 0; i < h_obj.total; i++)
{
void *status;
send->weight = new double;
*send->weight = h_obj.getval(i);
cout << "he;; " << *send->weight << endl;
pthread_create(&id[i],NULL, calc_h1, (void *)send);
pthread_join(id[i],NULL);
// double* sum=(double*)status;
// cout<<"\nSum is :"<<*sum<<'\n';
}
Ag_str+='*';
string Atemp=to_string(hidden_layers);
Ag_str+=Atemp;
Ag_str+='*';
Ag_str+='\0';
const char *char_array = Ag_str.c_str();
cout<<"\nString is : "<<Ag_str<<'\n';
int file_descptr = open("pipe", O_WRONLY);
ssize_t bytes_written = write(file_descptr,char_array, strlen(char_array));
cout << "\nBytes written : " << bytes_written << '\n';
close(file_descptr);
wait(nullptr);
}
else if(pid==0)
{
cout<<"\nReached the destination, finally!";
execlp("./h", "1","e", NULL);
}
}
pthread_mutex_destroy(&m);
pthread_exit(NULL);
return 0;
}