Debug swcsa
[SXSI/TextCollection.git] / incbwt / rlcsa_test.cpp
1 #include <cstdlib>
2 #include <ctime>
3 #include <iostream>
4 #include <vector>
5
6 #include "rlcsa.h"
7 #include "misc/utils.h"
8
9 #ifdef MULTITHREAD_SUPPORT
10 #include <omp.h>
11 #endif
12
13
14 using namespace CSA;
15
16
17 const int MAX_THREADS = 64;
18 const usint MAX_OCCURRENCES = 100000;
19
20
21 int main(int argc, char** argv)
22 {
23   std::cout << "RLCSA test" << std::endl;
24   if(argc < 2)
25   {
26     std::cout << "Usage: rlcsa_test basename [patterns [threads]]" << std::endl;
27     return 1;
28   }
29
30   std::cout << "Base name: " << argv[1] << std::endl;
31   if(argc > 2) { std::cout << "Patterns: " << argv[2] << std::endl; }
32   sint threads = 1;
33   if(argc > 3)
34   {
35     threads = std::min(MAX_THREADS, std::max(atoi(argv[3]), 1));
36   }
37   std::cout << "Threads: " << threads << std::endl; 
38   std::cout << std::endl;
39
40   const RLCSA rlcsa(argv[1], false);
41   rlcsa.printInfo();
42   rlcsa.reportSize(true);
43
44   if(argc < 3) { return 0; }
45   std::ifstream patterns(argv[2], std::ios_base::binary);
46   if(!patterns)
47   {
48     std::cerr << "Error opening pattern file!" << std::endl;
49     return 2;
50   }
51
52   std::vector<std::string> rows;
53   readRows(patterns, rows, true);
54
55   usint total = 0, total_size = 0, ignored = 0, found = 0;
56   sint i, n = rows.size();
57   pair_type result;
58   usint* matches;
59
60   double start = readTimer();
61   #ifdef MULTITHREAD_SUPPORT
62   usint occurrences;
63   omp_set_num_threads(threads);
64   #pragma omp parallel private(result, matches, occurrences)
65   {
66     #pragma omp for schedule(dynamic, 1)
67     for(i = 0; i < n; i++)
68     {
69       result = rlcsa.count(rows[i]);
70       occurrences = length(result);
71       #pragma omp critical
72       {
73         if(!isEmpty(result))
74         {
75           found++;
76           if(occurrences <= MAX_OCCURRENCES) { total += occurrences; }
77           else { ignored++; }
78         }
79         total_size += rows[i].length();
80       }
81       if(!isEmpty(result) && occurrences <= MAX_OCCURRENCES)
82       {
83         matches = rlcsa.locate(result);
84         delete[] matches;
85       }
86     }
87   }
88   #else
89   for(i = 0; i < n; i++)
90   {
91     result = rlcsa.count(rows[i]);
92     total_size += rows[i].length();
93     if(!isEmpty(result)) { found++; total += length(result); }
94     matches = rlcsa.locate(result);
95     if(matches) { delete[] matches; }
96   }
97   #endif
98
99   double time = readTimer() - start;
100   std::cout << "Patterns:    " << n << " (" << (n / time) << " / sec)" << std::endl;
101   std::cout << "Total size:  " << total_size << " bytes (" << (total_size / time) << " / sec)" << std::endl;
102   std::cout << "Matches:     " << total << " (" << (total / time) << " / sec)" << std::endl;
103   std::cout << "Found:       " << found << std::endl;
104   std::cout << "Time:        " << time << " seconds" << std::endl;
105   std::cout << std::endl;
106   std::cout << "Ignored " << ignored << " patterns with more than " << MAX_OCCURRENCES << " occurrences." << std::endl;
107   std::cout << std::endl;
108
109   return 0;
110 }