-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalContext.h
71 lines (63 loc) · 2.11 KB
/
GlobalContext.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
#ifndef _GLOBALCONTEXT_H_
#define _GLOBALCONTEXT_H_
#include <cstdio>
#include <pthread.h>
#include <vector>
#include <algorithm>
#include <atomic>
#include <iostream>
#include <map>
#include "MapReduceClient.h"
#include "MapReduceFramework.h"
#include "Barrier.h"
typedef std::vector<std::vector<IntermediatePair> *> MidVectors;
class ThreadContext;
class GlobalContext {
private:
std::atomic<uint32_t> next_pair_index{0};
std::atomic<uint64_t> general_atomic{0};
int pairs_number;
public:
const MapReduceClient &client;
const InputVec &input_vec;
OutputVec &output_vec;
pthread_mutex_t output_vec_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t wait_for_job_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t get_job_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t map_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t reduce_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
MidVectors shuffled_vectors;
std::map<K2*, std::vector<IntermediatePair>*> shuffle_map;
Barrier *threads_barrier;
bool is_job_ended = false;
int intermediary_elements;
GlobalContext (const MapReduceClient &client, const InputVec &input_vec,
OutputVec &output_vec, int multiThreadLevel);
// Getters
float get_map_progress_percentage ()
{
return (float) this->get_first_counter_value ()
/ (float) this->pairs_number * 100.0;
}
float get_shuffle_progress_percentage ()
{
return (float) this->get_first_counter_value ()
/ (float) intermediary_elements * 100.0;
}
int get_pairs_number () const
{ return this->pairs_number; }
// Atmoic variables
uint32_t increment_next_pair_index ()
{ return (this->next_pair_index)++; }
void set_stage_and_reset_general_atomic (stage_t stage);
void increment_first_counter_general_atomic (uint64_t inc = 1);
void increment_second_counter_general_atomic (uint64_t inc = 1);
stage_t get_stage ();
uint64_t get_first_counter_value ();
uint64_t get_second_counter_value ();
void reset_next_pair_index ()
{ next_pair_index.exchange (0); }
~GlobalContext ();
};
#endif //_GLOBALCONTEXT_H_