-
Notifications
You must be signed in to change notification settings - Fork 0
/
fakeprocess2.h
74 lines (64 loc) · 1.91 KB
/
fakeprocess2.h
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
#ifndef FAKE_PROCESS_H
#define FAKE_PROCESS_H
#include <iostream>
#include <stdlib.h>
#include <mpi.h>
#include "globals.h"
class FakeProcess {
public:
FakeProcess(int rank, bool token, double rate = 0.2):
_rank{rank},
_token{token},
_rate{rate}
{
srand(0);
// Wait confirmation that Server is on
if (_rank != 0) {
std::cout << "P" << _rank << ": waiting for server startup." << std::endl;
MPI_Recv(&_token, 1, MPI_UNSIGNED, 0, TAG_SEND, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
std::cout << "P" << _rank << ": working." << std::endl;
}
}
bool useResource() {
if (_token) {
std::cout << "P" << _rank << ": used the resource." << std::endl;
MPI_Send(&_token, 1, MPI_UNSIGNED, 0, TAG_GIVEBACK, MPI_COMM_WORLD);
_token = 0;
} else {
std::cout << "P" << _rank << ": waiting for token..." << std::endl;
unsigned ask = 1;
MPI_Send(&ask, 1, MPI_UNSIGNED, 0, TAG_ASK, MPI_COMM_WORLD);
MPI_Recv(&_token, 1, MPI_UNSIGNED, 0, TAG_SEND, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
std::cout << "P" << _rank << ": received token." << std::endl;
}
return true;
}
bool doOtherStuff() {
//std::cout << "P" << _rank << ": doing other stuff." << std::endl;
double t1 = MPI_Wtime();
double t2 = MPI_Wtime();
while (t2 < t1+0.02) {
t2 = MPI_Wtime();
}
return false;
}
bool update() {
double chance = (double)rand()/(double)(RAND_MAX);
if (chance < _rate) { // Wants to access resource
return useResource();
} else {
return doOtherStuff();
}
}
bool token() {
return _token;
}
void token(bool t) {
_token = t;
}
private:
int _rank;
bool _token;
double _rate;
};
#endif