Debug swcsa
[SXSI/TextCollection.git] / incbwt / display_test.cpp
1 #include <algorithm>
2 #include <cstdlib>
3 #include <ctime>
4 #include <fstream>
5 #include <iostream>
6
7 #include "rlcsa.h"
8 #include "misc/utils.h"
9
10 #ifdef MULTITHREAD_SUPPORT
11 #include <omp.h>
12 const int MAX_THREADS = 64;
13 #endif
14
15
16 using namespace CSA;
17
18
19 int main(int argc, char** argv)
20 {
21   std::cout << "RLCSA display test" << std::endl;
22   if(argc < 4)
23   {
24     std::cout << "Usage: display_test basename sequences max_length [threads [random_seed]]" << std::endl;
25     return 1;
26   }
27
28   std::cout << "Base name: " << argv[1] << std::endl;
29
30   sint sequences = std::max(atoi(argv[2]), 1);
31   std::cout << "Sequences: " << sequences << std::endl;
32
33   usint max_length = std::max(atoi(argv[3]), 1);
34   std::cout << "Prefix length: " << max_length << std::endl;
35
36   sint threads = 1;
37 #ifdef MULTITHREAD_SUPPORT
38   if(argc > 4)
39   {
40     threads = std::min(MAX_THREADS, std::max(atoi(argv[4]), 1));
41   }
42 #endif
43   std::cout << "Threads: " << threads << std::endl; 
44
45   usint seed = 0xDEADBEEF;
46   if(argc > 5)
47   {
48     seed = atoi(argv[5]);
49   }
50   std::cout << "Random seed: " << seed << std::endl; 
51   std::cout << std::endl;
52
53   RLCSA rlcsa(argv[1]);
54   if(!rlcsa.supportsDisplay())
55   {
56     std::cerr << "Error: Display is not supported!" << std::endl;
57     return 2;
58   }
59   rlcsa.printInfo();
60   rlcsa.reportSize(true);
61
62   usint total = 0;
63   usint seq_num, seq_total = rlcsa.getNumberOfSequences();
64   sint i;
65
66   double start = readTimer();
67   srand(seed);
68   uchar* buffer = new uchar[max_length];
69   #ifdef MULTITHREAD_SUPPORT
70   usint length;
71   usint thread_id;
72   uchar* buffers[threads]; buffers[0] = buffer;
73   for(i = 1; i < threads; i++)
74   {
75     buffers[i] = new uchar[max_length];
76   }
77   omp_set_num_threads(threads);
78   #pragma omp parallel private(seq_num, length, thread_id)
79   {
80     #pragma omp for schedule(dynamic, 1)
81     for(i = 0; i < sequences; i++)
82     {
83       #pragma omp critical
84       {
85         seq_num = rand() % seq_total;
86       }
87       thread_id = omp_get_thread_num();
88       length = rlcsa.displayPrefix(seq_num, max_length, buffers[thread_id]);
89       #pragma omp critical
90       {
91         total += length;
92       }
93     }
94   }
95   for(i = 1; i < threads; i++) { delete[] buffers[i]; }
96   #else
97   for(i = 0; i < sequences; i++)
98   {
99     seq_num = rand() % seq_total;
100     total += rlcsa.displayPrefix(seq_num, max_length, buffer);
101   }
102   #endif
103   delete[] buffer;
104
105   double time = readTimer() - start;
106   double megabytes = total / (double)MEGABYTE;
107   std::cout << megabytes << " megabytes in " << time << " seconds (" << (megabytes / time) << " MB/s)" << std::endl;
108   std::cout << std::endl;
109
110   return 0;
111 }