forked from SNUCSE-CTA/FastAPSP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuxData.cpp
More file actions
260 lines (234 loc) · 8.26 KB
/
AuxData.cpp
File metadata and controls
260 lines (234 loc) · 8.26 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "AuxData.h"
using namespace std;
AuxData::AuxData(){
}
AuxData::~AuxData() {
}
void AuxData::Preprocessing(uchar *text,uint k, ulong startp[], int min, int _m) {
m = _m;
Alphabet = 4;
rank['A'] = 0; rank['C'] = 1; rank['G'] = 2; rank['T'] = 3;
B = (int)(log(2*m*k) / log(Alphabet) );
if(m < B) B = m;
ShiftBits = (unsigned int)ceil(log((double)Alphabet)/ log(2.0));
cerr << "B : " << B << endl;
cerr << "m : " << m << endl;
BMask = 1;
for(int i = 0; i < B*ShiftBits; i++) BMask <<= 1;
BMask--;
Size = (ulong) pow((double)Alphabet, B);
cerr << "2km : " << Size << endl;
qrm = new unsigned int[Size];
if( min < m ){
qList = new LinkedList [Size];
}
if( min < B ){
Bprefixp = new int [B];
Bprefixp[0] = 0; int p = 1;
for(int i = 1; i < B; i++){
p *= Alphabet;
Bprefixp[i] = Bprefixp[i-1] + p;
}
Bprefix = new struct tree_node * [Size];
for(int j = 0; j < Size; j++){
Bprefix[j] = NULL;
}
}
for (size_t i = 0; i < Size; ++i) {
qrm[i] = B - 1;
}
uint* hash = new uint[k];
unsigned int* T = new unsigned int[Size/32]; // We use bit vector $T$
for(int i = 0; i < Size/32; i++) T[i] = 0;
for(int i = 0; i < k; i++){
if( startp[i+1] - startp[i] >= B ){
uint h = 0;
for(int r = B; r >= 1; --r){
h <<= ShiftBits;
h += rank[decode(text, startp[i+1] - r)];
}
T[h/32] |= (1<<h%32);
}
}
m2; if( m >= min ) m2 = m; else m2 = min; // m2 = m' in our algorithm
B2; if( B >= min ) B2 = B; else B2 = min; // B2 = B'
for (int q = m; q >= B; --q) {
for (int j = 0; j < k; ++j) {
int len = startp[j+1] - startp[j];
if( len >= m2 ){
// computing hash for only case1 strings
if( q == m ){
hash[j] = 0;
for (int r = B-1; r >= 0; --r) {
hash[j] <<= ShiftBits;
hash[j] += rank[decode(text, startp[j]+q-r-1)];
}
}
else{
hash[j] = hash[j] >> ShiftBits;
unsigned int temp = rank[decode(text, startp[j]+q-B)];
temp <<= ((B-1)*ShiftBits);
hash[j] += temp;
}
int h = hash[j];
if( qrm[h] < q ){
qrm[h] = q;
}
}
if( B2 <= len ){
if( B2 <= len && len < m ){
if( q == len ){
hash[j] = 0;
for(int r = B-1; r >= 0; --r){
hash[j] <<= ShiftBits;
hash[j] += rank[decode(text, startp[j]+q-r-1)];
}
}
else if( q < len ){
hash[j] = hash[j] >> ShiftBits;
unsigned int temp = rank[decode(text, startp[j]+q-B)];
temp <<= ((B-1)*ShiftBits);
hash[j] += temp;
}
else continue;
}
int h = hash[j];
if( B2 <= q && q < m && (T[h/32] & (1<<(h%32)))){
if( qList[h].empty() ){
qList[h].push_front( (unsigned int) q );
}
if( !qList[h].empty() && qList[h].front() != q ){
qList[h].push_front( (unsigned int) q );
}
}
}
}
}
/*double aver_q = 0.0;
for(int i = 0; i < Size; i++){
aver_q += qrm[i];
}
cerr << "Average of qrm : " << aver_q / Size << endl;*/
delete [] hash;
delete [] T;
}
#define NUMLOCK 1024
void AuxData::Preprocessing_par(uchar *text, uint k, ulong startp[], int min, int threads, int _m) {
m = _m;
Alphabet = 4;
rank['A'] = 0; rank['C'] = 1; rank['G'] = 2; rank['T'] = 3;
B = log(2*m*k) / log(Alphabet);
if(m < B) B = m;
ShiftBits = (unsigned int)ceil(log((double)Alphabet) / log(2.0));
cerr << "B : " << B << endl;
cerr << "m : " << m << endl;
BMask = 1;
for (int i = 0; i < B*ShiftBits; i++) BMask <<= 1;
BMask--;
Size = (ulong) pow(pow(2.0, ShiftBits), B);
qrm = (unsigned int*) memalign(64, Size*sizeof(unsigned int));
unsigned int* T;
if( min < m ){
qList = new LinkedList [Size];
T = (unsigned int*)( memalign(64, Size*sizeof(unsigned int)));
}
if( B > min ){
Bprefixp = new int[B];
Bprefixp[0] = 0; int p = 1;
for(int i = 1; i < B; i++){
p *= Alphabet;
Bprefixp[i] = Bprefixp[i-1] + p;
}
Bprefix = new struct tree_node *[Size];
for (int j = 0; j < Size; j++){
Bprefix[j] = NULL;
}
}
#pragma omp parallel for
for (size_t i = 0; i < Size; ++i) {
qrm[i] = B - 1;
}
omp_lock_t lock[NUMLOCK];
for(int i = 0; i < NUMLOCK; i++){
omp_init_lock(&lock[i]);
}
uint* hash = (unsigned int*) memalign(64, k*sizeof(unsigned int));
if(min < m){
#pragma omp parallel for
for(int i = 0; i < Size; i++){
T[i] = 0;
}
#pragma imp parallel for
for(int i = 0; i < k; i++){
int h = 0;
for(int r = B; r >= 1; r--){
h <<= ShiftBits;
h += rank[decode(text, startp[i+1]-r)];
}
omp_set_lock(&lock[h%NUMLOCK]);
T[h] = 1;
omp_unset_lock(&lock[h%NUMLOCK]);
}
}
m2; if(m >= min) m2 = m; else m2 = min;
B2; if(B >= min) B2 = B; else B2 = min;
for (int q = m; q >= B; --q) {
#pragma omp parallel for
for (int j = 0; j < k; ++j) { // loop through patterns
int len = startp[j+1] - startp[j];
if( len >= m2 ){
if (q == m){
hash[j] = 0;
for (int r = B - 1; r >= 0; --r) {
hash[j] <<= ShiftBits;
hash[j] += rank[decode(text, startp[j] + q - r - 1)];
}
}
else{
hash[j] = hash[j] >> ShiftBits;
unsigned int temp = rank[decode(text, startp[j] + q - B)];
temp <<= ((B - 1)*ShiftBits);
hash[j] += temp;
}
int h = hash[j];
omp_set_lock(&lock[h%NUMLOCK]);
if (qrm[h] < q){
qrm[h] = q;
}
omp_unset_lock(&lock[h%NUMLOCK]);
}
if( B2 <= len ){
if( B2 <= len && len < m ){
if( q == len ){
hash[j] = 0;
for(int r = B-1; r >= 0; r--){
hash[j] <<= ShiftBits;
hash[j] += rank[decode(text, startp[j]+q-r-1)];
}
}
else if( q < len ){
hash[j] = hash[j] >> ShiftBits;
unsigned int temp = rank[decode(text, startp[j]+q-B)];
temp <<= ((B-1)*ShiftBits);
hash[j] += temp;
}
else continue;
}
int h = hash[j];
if( B2 <= q && q < m && T[h] == 1 ){
omp_set_lock(&lock[h%NUMLOCK]);
if ( qList[h].empty() ){
qList[h].push_front((unsigned int)q);
}
if ( !qList[h].empty() && qList[h].front() != q){
qList[h].push_front((unsigned int)q);
}
omp_unset_lock(&lock[h%NUMLOCK]);
}
}
}
}
for(int i = 0; i < NUMLOCK; i++){
omp_destroy_lock(&lock[i]);
}
}