Repository navigation
Expand file tree
/
Copy pathThreadManagerBase.cpp
More file actions
163 lines (134 loc) · 4.07 KB
/
Copy pathThreadManagerBase.cpp
File metadata and controls
163 lines (134 loc) · 4.07 KB
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
#include <utility>
#include <string>
#include <cstdlib>
#include <mpi.h>
#include <numeric>
#include <iomanip>
#include <zconf.h>
#include "ThreadManagerBase.h"
#include "easylogging++.h"
#include "MessageType.h"
#include "Logger.h"
using namespace std;
int ThreadManagerBase::getSize() {
return size;
}
vector<QueueElement> &ThreadManagerBase::getQueue() {
return queue;
}
vector<int> &ThreadManagerBase::getTabAcks() {
return tabAcks;
}
void ThreadManagerBase::initTabAcks() {
for (int i = 0; i < size; i++) {
tabAcks.push_back(0);
}
}
ThreadManagerBase::ThreadManagerBase(int rank, int size, char *processorName) : rank(rank), size(size), processorName(processorName) {
this->initTabAcks();
this->myWeight = 80 + rand() % 40;
this->clock = 0;
this->tabAcks[rank] = 1;
this->clockMutex = PTHREAD_MUTEX_INITIALIZER;
this->conditionalMutex = PTHREAD_COND_INITIALIZER;
LOG(INFO) << "Start thread: " << rank << " with weight: " << myWeight;
}
void ThreadManagerBase::increaseClock() {
this->clock += 1;
}
int *ThreadManagerBase::constructMessage() {
static int message[ThreadManagerBase::MSG_SIZE];
message[0] = this->clock;
message[1] = this->myWeight;
return message;
}
void ThreadManagerBase::sendMessageForEverybody(int *msg, MessageType type) {
LOG(INFO) << MAIN_MESS << this->toString() << "Send message for everybody";
for (int i = 0; i < this->size; i++) {
if (i == rank) continue;
MPI_Send(msg, MSG_SIZE, MPI_INT, i, type, MPI_COMM_WORLD);
}
}
void ThreadManagerBase::addOwnRequestToQueue() {
QueueElement newElement = QueueElement(this->rank, this->clock, this->myWeight);
this->addRequestToQueue(newElement);
}
void ThreadManagerBase::addRequestToQueue(QueueElement element) {
this->queue.push_back(element);
this->sortQueue();
}
// TODO sort it correctly
void ThreadManagerBase::sortQueue() {
sort(this->queue.begin(), this->queue.end());
}
int ThreadManagerBase::getSumOfACks() {
int sum = 0;
for (int &ack:this->tabAcks) {
sum += ack;
}
return sum;
}
bool ThreadManagerBase::isEveryAck() {
return this->getSumOfACks() == this->size;
}
bool ThreadManagerBase::isEnoughPlaceOnLift() {
int sum = 0;
for (QueueElement &queueElement: this->queue) {
sum += queueElement.getWeight();
if (queueElement.getId() == this->rank) break;
}
return sum <= ThreadManagerBase::CAPACITY;
}
void ThreadManagerBase::clearAcks() {
for (int i = 0; i < this->tabAcks.size(); i++) {
this->tabAcks[i] = i == this->rank ? 1 : 0;
}
}
void ThreadManagerBase::removeFromQueueById(int id) {
queue.erase(
remove_if(queue.begin(), queue.end(),
[id](const QueueElement &o) { return o.getId() == id; }),
queue.end()
);
this->sortQueue();
}
void ThreadManagerBase::removeYourselfFromQueue() {
LOG(INFO) << MAIN_MESS << this->toString() << "Remove yourself from queue";
this->removeFromQueueById(this->rank);
// logger.queueUpdate(*this);
}
void ThreadManagerBase::updateClock(int receivedClock) {
if (receivedClock > this->clock)
this->clock = receivedClock;
this->increaseClock();
}
ThreadManagerBase::~ThreadManagerBase() {
pthread_mutex_destroy(&this->clockMutex);
pthread_cond_destroy(&this->conditionalMutex);
}
void ThreadManagerBase::lock() {
pthread_mutex_lock(&this->clockMutex);
}
void ThreadManagerBase::unlock() {
pthread_mutex_unlock(&this->clockMutex);
}
void ThreadManagerBase::wait() {
pthread_cond_wait(&this->conditionalMutex, &this->clockMutex);
}
void ThreadManagerBase::signal() {
pthread_cond_signal(&this->conditionalMutex);
}
void ThreadManagerBase::sleepRandomTime() {
sleep(1 + rand() % 5);
}
string ThreadManagerBase::toString() {
std::stringstream ss;
ss << left;
ss << "[";
ss << "ID:" << setw(2) << this->rank;
ss << "WEIGHT:" << setw(4) << this->myWeight;
ss << "CLOCK:" << setw(2) << this->clock;
ss << "COMP:" << setw(11) << this->processorName;
ss << "] ";
return ss.str();
}