-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseqTimes.cpp
242 lines (180 loc) · 8.01 KB
/
seqTimes.cpp
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
// g++ -std=c++11 seqTimes.cpp -o seqTimes -I ~/include -L ~/lib -lsdsl -ldivsufsort -ldivsufsort64 -O3 -DNDEBUG
#include <set>
#include <map>
#include <string>
#include "sys/times.h"
#include <chrono>
#include <vector>
#include <array>
#include <sdsl/int_vector.hpp>
#include <sdsl/bit_vectors.hpp>
#include <sdsl/util.hpp>
#include <sdsl/rank_support.hpp>
#include <sdsl/select_support.hpp>
#include <sdsl/suffix_arrays.hpp>
void readCompressed(const std::string path, sdsl::wm_int<sdsl::rrr_vector<63>> &x_wm,
sdsl::rrr_vector<63> &b1_rrr, sdsl::wt_hutu<sdsl::rrr_vector<63>> &b2_wt,
sdsl::wm_int<sdsl::rrr_vector<63>> &y_wm)
{
// Path to sequences
const std::string xPath = path + ".X.bin-wm_int.sdsl";
const std::string b1Path = path + ".B1-rrr-64.sdsl";
const std::string b2Path = path + ".B2.bin-wt_hutu.sdsl";
const std::string yPath = path + ".Y.bin-wm_int.sdsl";
// Read compressed files
load_from_file(x_wm, xPath.c_str());
load_from_file(b1_rrr, b1Path.c_str());
load_from_file(b2_wt, b2Path.c_str());
load_from_file(y_wm, yPath.c_str());
return;
}
void reconstructGraph(sdsl::wm_int<sdsl::rrr_vector<63>> &x_wm,
sdsl::rrr_vector<63> &b1_rrr, sdsl::wt_hutu<sdsl::rrr_vector<63>> &b2_wt,
sdsl::wm_int<sdsl::rrr_vector<63>> &y_wm, std::map<uint32_t, std::set<uint32_t>> &graph)
{
// SDSL variables for B1
sdsl::rrr_vector<63>::rank_1_type rrrB1_rank(&b1_rrr);
sdsl::rrr_vector<63>::select_1_type b1_select(&b1_rrr);
std::vector<uint32_t> xRAM(x_wm.size(), 0);
for(uint64_t i = 0; i < x_wm.size(); ++i)
{
xRAM[i] = x_wm[i];
}
std::vector<uint8_t> b2RAM(b2_wt.size(), 0);
for(uint64_t i = 0; i < b2_wt.size(); ++i)
{
b2RAM[i] = b2_wt[i];
}
std::vector<uint32_t> yRAM(y_wm.size(), 0);
for(uint64_t i = 0; i < y_wm.size(); ++i)
{
yRAM[i] = y_wm[i];
}
// std::cerr << "All to RAM" << std::endl;
// How many partitions for this graph
const uint32_t howManyPartitions = rrrB1_rank(b1_rrr.size()) - 1;
uint32_t currentY = 0, nextY = yRAM[0];
// For every partition, let's find neighbors
for (uint32_t partitionNumber = 0; partitionNumber < howManyPartitions; ++partitionNumber)
{
// std::cerr << "Partition " << partitionNumber << std::endl;
const uint64_t partitionIndex = b1_select(partitionNumber + 1);
const uint64_t nextPartitionIndex = b1_select(partitionNumber + 2);
const uint32_t howManyNodesInPartition = nextPartitionIndex - partitionIndex;
currentY = nextY;
nextY = yRAM[partitionNumber + 1];
const uint32_t bytesPerNode = (nextY - currentY)/howManyNodesInPartition;
if(0 == bytesPerNode)
{
// std::cerr << "bPN 0" << std::endl;
for(uint64_t xCurrentIndex = partitionIndex; xCurrentIndex < nextPartitionIndex; ++xCurrentIndex)
{
const uint32_t current_node = xRAM[xCurrentIndex];
for (uint64_t xAdjacentIndex = xCurrentIndex + 1; xAdjacentIndex < nextPartitionIndex; ++xAdjacentIndex)
{
const uint32_t adjacent_node = xRAM[xAdjacentIndex];
graph[current_node].insert(adjacent_node);
graph[adjacent_node].insert(current_node);
}
}
}
else
{
// std::cerr << "bPN " << bytesPerNode << std::endl;
// For each current x, search it's neighbors
for(uint64_t xCurrentIndex = partitionIndex; xCurrentIndex < nextPartitionIndex; ++xCurrentIndex)
{
const uint32_t current_node = xRAM[xCurrentIndex];
// Get index of first byte of current node
const uint64_t currentByteIndex = currentY + bytesPerNode * (xCurrentIndex - partitionIndex);
// std::cerr << "cBi " << currentByteIndex << " ";
// Create a bool vector to check if nodes are already neighbors
const uint32_t howManyPossibleNeighbors = (nextPartitionIndex - xCurrentIndex) - 1;
std::vector<bool> neighbors(howManyPossibleNeighbors, 0);
// Check all bytes of nodes
uint32_t bytesChecked = 0;
while(bytesChecked != bytesPerNode)
{
// Get byte of current node to check for neighbors
const uint8_t maskByteOfCurrent = b2RAM[currentByteIndex + bytesChecked];
// For every possible neighbor, get their idexes
for(uint64_t xNeighborIndex = xCurrentIndex + 1; xNeighborIndex < nextPartitionIndex; ++xNeighborIndex)
{
// std::cerr << "xNeighborIndex ";
// Get index in vector of bools, to check if already neighbors
const uint32_t xNeighborBoolIndex = (xNeighborIndex - xCurrentIndex) - 1;
// If not neighbors yet
if(!neighbors[xNeighborBoolIndex])
{
// std::cerr << xNeighborIndex << " ";
// Get index of possible neighbor's byte to check
const uint64_t neighborByteIndex = currentY + bytesPerNode * (xNeighborIndex - partitionIndex) + bytesChecked;
// std::cerr << " " << neighborByteIndex
// Get byte of possible neighbor to check
const uint8_t maskBytePossibleNeighbor = b2RAM[neighborByteIndex];
// If not zero, they are neighbors!
if(maskByteOfCurrent & maskBytePossibleNeighbor)
{
// Mark as neighbors in vector of bools
neighbors[xNeighborBoolIndex] = 1;
const uint32_t adjacent_node = xRAM[xNeighborIndex];
graph[current_node].insert(adjacent_node);
graph[adjacent_node].insert(current_node);
}
}
}
++bytesChecked;
}
}
}
}
return;
}
int main(int argc, char const *argv[])
{
if(2 > argc)
{
std::cerr << "Modo de uso: " << argv[0] << " RUTA_BASE" << std::endl;
return -1;
}
const std::string path(argv[1]);
//uint64_t totalNodes = atoi(argv[2]);
//uint8_t random = atoi(argv[3]);
const uint8_t iterations = argv[2] ? atoi(argv[2]) : 1;
// Variables to read compressed sequences
sdsl::wm_int<sdsl::rrr_vector<63>> x_wm;
sdsl::rrr_vector<63> b1_rrr;
sdsl::wt_hutu<sdsl::rrr_vector<63>> b2_wt;
sdsl::wm_int<sdsl::rrr_vector<63>> y_wm;
readCompressed(path, x_wm, b1_rrr, b2_wt, y_wm);
std::cerr << "Compressed read" << std::endl;
std::map<uint32_t, std::set<uint32_t>> graph;
for(uint8_t i = 1; i <= iterations; ++i)
{
graph.clear();
std::cerr << "Reconstructing " << i << std::endl;
std::chrono::high_resolution_clock::time_point start_time = std::chrono::high_resolution_clock::now();
reconstructGraph(x_wm, b1_rrr, b2_wt, y_wm, graph);
std::chrono::high_resolution_clock::time_point stop_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds> (stop_time - start_time).count();
std::cerr << "Time Reconstruction " << i << ": " << duration << " [ms]" << std::endl;
}
std::cout << graph.size() << std::endl;
uint64_t nodeIndex = 0;
for(const auto & pair : graph)
{
// std::cout << pair.first << ": ";
while(pair.first != nodeIndex)
{
std::cout << std::endl;
++nodeIndex;
}
for(const auto & node : pair.second)
{
std::cout << node << " ";
}
++nodeIndex;
std::cout << std::endl;
}
return 0;
}