-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstract_method.cpp
More file actions
88 lines (73 loc) · 2.51 KB
/
abstract_method.cpp
File metadata and controls
88 lines (73 loc) · 2.51 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
#include "abstract_method.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <thread>
#include "barrier.hpp"
//may return 0 when not able to detect
using namespace std;
AbstractMethod::AbstractMethod(ProgramArgs &args) {
this->HASH_BITS = args.hash_bits;
this->NUM_THREADS = args.num_threads;
this->DATA_SIZE = args.data_size;
this->VERBOSE = args.verbose;
this->args = &args;
this->processor_count = std::thread::hardware_concurrency();
this->barrier = new Barrier(NUM_THREADS+1); // +1 for waiting for main thread
}
int AbstractMethod::get_num_partitions() const {
return 1 << HASH_BITS;
}
int AbstractMethod::hash_function(uint64_t key) const {
return key % (1 << HASH_BITS);
}
void AbstractMethod::print_hash_values(const vector<tuple<uint64_t, uint64_t>>& data) const {
for (const tuple<uint64_t, uint64_t>& entry : data) {
cerr << hash_function(get<0>(entry)) << " " << get<1>(entry) << endl;
}
}
bool AbstractMethod::read_affinity_file() {
if(args->affinity_name == "no_affinity" || args->affinity_file.empty() || args->affinity_file == "none"){
if(args->verbose == 2){
cerr << "No affinity file specified" << endl;
}
return false;
}
if(args->verbose == 2){
cerr << processor_count << " processors detected" << endl;
}
// Read line from file
ifstream file;
file.open(args->affinity_file);
string line;
getline(file, line);
file.close();
// Convert to vector<int>
stringstream ss(line);
string word;
while(ss >> word) {
try {
int core = stoi(word);
if(core >= processor_count) {
throw out_of_range("Core number out of range");
}
affinity.push_back(core);
} catch (const invalid_argument& e) {
cerr << "Invalid core number: " << word << endl;
return false;
} catch (const out_of_range& e) {
cerr << "Core number out of range: " << word << endl;
return false;
}
}
// Print That we are populating affinity vector
if(affinity.size() < NUM_THREADS && args->verbose == 2) {
cerr << "Affinity size:" << to_string(affinity.size()) << " < threads: " << to_string(args->num_threads) << endl;
cerr << "Populating affinity vector:" << endl;
}
int current_index = 0;
while (affinity.size() < NUM_THREADS) {
affinity.push_back(affinity[current_index % NUM_THREADS]);
}
return true;
}