-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral.h
194 lines (169 loc) · 4.64 KB
/
general.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <unordered_map>
#include <vector>
#include <iostream>
#include<omp.h>
#include<cstring>
#include <emmintrin.h>
#include <smmintrin.h>
#include <x86intrin.h>
#include <stdlib.h>
using namespace std;
extern int threads;
#ifndef NEXT_POW_2
/**
* compute the next number, greater than or equal to 32-bit unsigned v.
* taken from "bit twiddling hacks":
* http://graphics.stanford.edu/~seander/bithacks.html
*/
#define NEXT_POW_2(V) \
do { \
V--; \
V |= V >> 1; \
V |= V >> 2; \
V |= V >> 4; \
V |= V >> 8; \
V |= V >> 16; \
V++; \
} while(0)
#endif
class custom_container {
public:
int * container;
int * head;
custom_container * mainC;
unordered_map<int, vector<int> > * p;
int bufSize;
int partition;
custom_container(){
this->mainC = NULL;
this->container = NULL;
this->head = NULL;
}
custom_container(int partition, int bufSize, custom_container * c){
this->bufSize = bufSize;
if (c == NULL)
NEXT_POW_2(this->bufSize);
this->partition = partition;
if(posix_memalign((void **)&this->container, 64, partition * this->bufSize * sizeof(int)) != 0)
throw std::bad_alloc();
this->head = new int[partition];
for (int i = 0;i < partition; i++){
this->head[i] = 0;
}
this->mainC = c;
}
void push_back(int partition, int el){
//int j = __sync_fetch_and_add(this->head + partition, 1);
this->container[partition * this->bufSize + this->head[partition]] = el;
this->head[partition] += 1;
}
void insertData(int partition, int * els, int n){
int * p = this->getPartition(partition);
//int& h = this->head[partition];
int j = __atomic_add_fetch(this->head + partition, n, __ATOMIC_ACQUIRE);
int h = j - n;
p = p + h;
int i = 0;
for(;i < n; i += 1){
*p = els[i];
p += 1;
}
}
void flush(int p){
int * pAddr = this->container + p * this->bufSize;
if (this->head[p] > 0)
this->mainC->insertData(p, pAddr, this->head[p]);
this->head[p] = 0;
}
void manageWriteBuff(int partition){
if (this->head[partition] == this->bufSize){
this->flush(partition);
}
}
void flushAll(){
for(int i = 0;i < this->partition; i++){
this->flush(i);
}
}
int* getPartition(int partition){
return this->container + partition * this->bufSize;
}
int getPartitionSize(int partition){
return this->head[partition];
}
void reset(){
memset(this->head, 0, sizeof(int) * this->partition);
}
~custom_container(){
free(this->container);
free(this->head);
}
};
#define HASH_BIT_MODULO(K, MASK, NBITS) (((K) & MASK) >> NBITS)
inline
uint32_t
get_hist_size(uint32_t relSize)
{
NEXT_POW_2(relSize);
relSize >>= 2;
if(relSize < 4) relSize = 4;
return relSize;
}
class CustomHashTable{
public:
int * container;
int * next;
int size;
uint32_t nSize;
uint32_t mask;
CustomHashTable(int n){
this->size = n;
this->nSize = get_hist_size(n);
this->container = new int[nSize];
this->next = new int[n];
this->mask = (nSize-1) << 18;
}
void Data(int el){
uint32_t idx = HASH_BIT_MODULO(el, this->mask, 18);
if (this->container[idx] != 0){
while (this->container[idx] != 0){
idx++;
}
}
this->container[idx] = el;
}
int find(int el){
int idx = HASH_BIT_MODULO(el, this->mask, 18);
if (this->container[idx] == 0){
return -1;
}
while (this->container[idx] != el and idx < this->size){
idx++;
}
if (idx == this->size){
return -1;
} else {
return idx;
}
}
int end(){
return -1;
}
void flush(){
for (int i = 0; i < this->size; i++){
this->container[i] = 0;
}
}
~CustomHashTable(){
free(this->container);
}
};
inline bool processEquality(int a, int b){
return a == b;
}
inline bool processGreaterThan(int a, int b){
return a > b;
}
inline bool processLessThan(int a, int b){
return a < b;
}