-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.c
83 lines (74 loc) · 2.03 KB
/
worker.c
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
/*
* Authors: LOPES Marco, ISELI Cyril and RINGOT Gaëtan
* Purpose: Management of worker
* Language: C
* Date : november 2016
*/
#include "worker.h"
/**
* Calc number of neighbours
*
* @param oldState: array of cases
* @param line: case line
* @param column: case column
* @return number of neighbours
*/
int neighbour(bool **oldState, uint line, uint column) {
int nbALive = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i || j) {
if (oldState[line + i][column + j]) {
nbALive++;
}
}
}
}
return nbALive;
}
/**
* Calc the next state with the rules of "Games of Life"
*
* @param oldState: array of cases
* @param line: case line
* @param column: case column
* @return new state
*/
bool nextState(bool **oldState, uint line, uint column) {
int nbNeighbour = neighbour(oldState, line, column);
if (oldState[line][column]) {
if (nbNeighbour < 2) {
return false;
} else if (nbNeighbour == 2 || nbNeighbour == 3) {
return true;
} else {
return false;
}
} else {
return (nbNeighbour == 3);
}
}
/**
* Task of a single thread
*
* @param paramsWorker: struct which contain all information to perform the task
* @return NULL
*/
void *worker(void *paramsWorker) {
paramsWorkerSt *params = (paramsWorkerSt *) paramsWorker;
uint jump = params->idThread;
uint size = (params->height - 2) * (params->width - 2);
uint line, column;
while (!*params->quit) {
line = jump / (params->width - 2) + 1;
column = jump % (params->width - 2) + 1;
(*params->actualState)[line][column] = nextState(*params->oldState, line, column);
jump += params->numberThreads;
if (jump >= size) {
jump = params->idThread;
pthread_barrier_wait(params->workersDisplayBarrier);
pthread_barrier_wait(params->workersDisplayBarrier);
}
}
return NULL;
}