-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBarrierBasedStatic.h
140 lines (114 loc) · 3.87 KB
/
BarrierBasedStatic.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
//
// Created by fede on 21/01/19.
//
#ifndef LEVENSHTEIN_Worker3_H
#define LEVENSHTEIN_Worker3_H
#include <iostream>
#include <string>
#include <vector>
#include <string.h>
#include <random>
#include <omp.h>
#include <unistd.h>
#include <thread>
#include <atomic>
#include "BoostBarrier.h"
#include "ind.h"
#include "uint.h"
#include "ConcurrentQueue.h"
class BarrierBasedStatic {
public:
void operator()() {
const int DStart = -M_Tiles + 1;
const int DFinish = N_Tiles;
for (int d = DStart; d < DFinish; d++) {
const uint16 iMax = std::min(M_Tiles + d,
N_Tiles); //handling the case when we are filling the left up corner of the matrix
const uint16 iMin = std::max(d, 0); //handling the case when we are filling the right low corner of the matrix
uint16 diagLength = iMax - iMin;
int k = 0;
while(k*MAX_THREAD_COUNT + tid < diagLength){
uint16 i = k*MAX_THREAD_COUNT + tid + iMin;
uint16 j = M_Tiles - i + d - 1;
I = i * TILE_WIDTH + 1;
J = j * TILE_WIDTH + 1;
computeSubMatrix();
++k;
}
/*
lower=upper=-1;
if (diagLength > MAX_THREAD_COUNT) {
const uint16 tilePerThread = diagLength / MAX_THREAD_COUNT;
lower = tid * tilePerThread + iMin;
upper = lower + tilePerThread;
} else if (tid < diagLength) {
lower = tid + iMin;
upper = lower + 1;
}
uint16 remainder = diagLength % MAX_THREAD_COUNT;
for (int i = lower; i < upper; i++) {
uint16 j = M_Tiles - i + d - 1;
I = i * TILE_WIDTH + 1;
J = j * TILE_WIDTH + 1;
computeSubMatrix();
}
if(remainder && tid < remainder){
int i = iMax - tid - 1;
uint16 j = M_Tiles - i + d - 1;
I = i * TILE_WIDTH + 1;
J = j * TILE_WIDTH + 1;
computeSubMatrix();
}
*/
privateBarrier.count_down_and_wait();
}
thread_barrier->count_down_and_wait();
}
BarrierBasedStatic(const char *x, const char *y, uint16 *D, barrier *b) :
D(D), x(x), y(y), thread_barrier(b) {
M = strlen(x);
N = strlen(y);
M_Tiles = ceil((float) M / BarrierBasedStatic::TILE_WIDTH);
N_Tiles = ceil((float) N / BarrierBasedStatic::TILE_WIDTH);
tid = nThreads++;
lower = upper = 0;
}
void setLUd(uint lower, uint upper, uint d) {
this->lower = lower;
this->upper = upper;
}
static const int TILE_WIDTH = 512;
static const int MAX_THREAD_COUNT = 12;
static barrier privateBarrier;
static int nThreads;
private:
void computeSubMatrix() {
uint M_ = M + 1;
uint N_ = N + 1;
for (uint i = I; i < N_ && i < I + TILE_WIDTH; i++) {
for (uint j = J; j < M_ && j < J + TILE_WIDTH; j++) {
if (x[i - 1] != y[j - 1]) {
uint16 k = (uint16) minimum_(D[i * M_ + j - 1], //insertion
D[(i - 1) * M_ + j], //insertion
D[(i - 1) * M_ + j - 1]); //substitution
D[i * M_ + j] = k + 1;
} else {
D[i * M_ + j] = D[(i - 1) * M_ + j - 1];
}
}
}
}
int minimum_(const int a, const int b, const int c) {
return std::min(std::min(a, b), c);
}
uint lower, upper;
uint16 *D;
int tx, ty;
uint M, N;
uint I, J;
int M_Tiles, N_Tiles;
const char *x, *y;
barrier *thread_barrier;
int tid;
};
#endif //LEVENSHTEIN_WORKER_H