logutils.hpp
Go to the documentation of this file.
1 //==============================================================================
11 //==============================================================================
12 #ifndef LOGUTILS_HPP_
13 #define LOGUTILS_HPP_
14 
15 #include <iostream>
16 #ifdef HAVE_OPENMP
17 #include <omp.h>
18 #endif
19 
21 class LoggerHelper {
22  public:
27  LoggerHelper(int max_, int chunks, int minsize)
28  : per_chunk(max_/chunks), max(max_)
29  {
30  std::vector<int> sizes;
31  for (int i=0;i<chunks;++i)
32  sizes.push_back(max/chunks);
33  for(int i=chunks-max%chunks;i<chunks;++i)
34  sizes[i]++;
35  groups.resize(chunks);
36  groups[0] = 0;
37  for (int i=1;i<chunks;++i)
38  groups[i] = groups[i-1]+sizes[i-1];
39  groups.push_back(max);
40  if (max < minsize)
41  per_chunk=-1;
42  }
43 
46  std::pair<int, int> group(int group)
47  {
48  return std::make_pair(groups[group],groups[group+1]);
49  }
50 
54  void log(int it, const std::string& prefix)
55  {
56  if(per_chunk == -1)
57  return;
58 #ifdef HAVE_OPENMP
59  if (omp_get_num_threads() == 1)
60 #endif
61  std::cout << prefix << it << '/' << max << std::endl;
62  }
63  protected:
64  std::vector<int> groups;
65  int per_chunk;
66  int max;
67 };
68 
69 #endif
std::vector< int > groups
Group start/end offsets.
Definition: logutils.hpp:64
int per_chunk
Will log for each per_chunk processed.
Definition: logutils.hpp:65
std::pair< int, int > group(int group)
Returns the start and end offsets of a chunk group.
Definition: logutils.hpp:46
void log(int it, const std::string &prefix)
Log to the terminal.
Definition: logutils.hpp:54
int max
Total number of its.
Definition: logutils.hpp:66
LoggerHelper(int max_, int chunks, int minsize)
Default constructor.
Definition: logutils.hpp:27
Helper class for progress logging during time consuming processes.
Definition: logutils.hpp:21