-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetManager.cpp
48 lines (44 loc) · 1.45 KB
/
NetManager.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
#include <thread>
#include <mutex>
#include <iostream>
#include "NetManager.h"
NetManager::NetManager(unsigned int num_of_layers, unsigned int num_of_neurons, unsigned int num_of_inputs,
unsigned int num_of_outputs, unsigned int num_of_creatures)
{
//networks.reserve(num_of_creatures);
for (int i=0; i < num_of_creatures; i++) {
networks.push_back(NNet(num_of_layers, num_of_neurons, num_of_inputs, num_of_outputs));
outputs.push_back(std::vector<float>(num_of_outputs));
}
}
std::vector<std::vector<float>> NetManager::calculate(std::vector<std::vector<float>> &inputs)
{
this->inputs = inputs;
std::vector<std::thread> threads;
int num_of_threads = std::thread::hardware_concurrency();
threads.resize(num_of_threads);
counter = networks.size() - 1;
for (int i=0; i < num_of_threads; i++){
threads[i] = std::thread(&worker_func, this);
}
for (int i=0; i < num_of_threads; i++){
threads[i].join();
}
return this->outputs;
}
void NetManager::worker_func(NetManager* netManager)
{
while(true)
{
netManager->mutex.lock();
if (netManager->counter <= 0)
{
netManager->mutex.unlock();
return;
}
int index = netManager->counter;
netManager->counter--;
netManager->mutex.unlock();
netManager->outputs[index] = netManager->networks[index].feed_forward(netManager->inputs[index]);
}
}