QNANO
Distribute_Block.h
1 #ifndef QNANO_NEW_DISTRIBUTE_BLOCK_DEFINED_H_
2 #define QNANO_NEW_DISTRIBUTE_BLOCK_DEFINED_H_
3 
4 #include <iostream>
5 #include <cstdlib>
6 
8 {
9 private:
10  size_t total_size;
11  size_t nr_blocks;
12  size_t nr_onemore;
13 
14 public:
15  size_t get_total_size() const{return total_size;}
16  size_t get_nr_blocks() const{return nr_blocks;}
17  size_t get_floor_blocksize() const
18  {
19  return get_total_size()/get_nr_blocks();
20  }
21  size_t get_blocksize(size_t bl) const
22  {
23  if(bl<0 || bl>=nr_blocks)
24  {
25  std::cerr<<"DistributeBlock out of bound: "<<bl<<"/"<<nr_blocks<<std::endl
26 ;
27  exit(1);
28  }
29  else if(bl<nr_onemore)
30  {
31  return get_total_size()/get_nr_blocks()+1;
32  }
33  else
34  {
35  return get_total_size()/get_nr_blocks();
36  }
37  }
38  size_t get_start(size_t bl) const
39  {
40  if(bl<0 || bl>=get_nr_blocks())
41  {
42  std::cerr<<"DistributeBlock out of bound: "<<bl<<"/"<<get_nr_blocks()<<std::endl;
43  exit(1);
44  }
45  if(bl<nr_onemore)
46  {
47  return bl*get_blocksize(0);
48  }
49  else
50  {
51  return bl*get_floor_blocksize()+nr_onemore;
52  }
53  }
54  size_t lookup_element(size_t el) const
55  {
56  size_t max_size_before_onemore = (get_floor_blocksize()+1)*nr_onemore;
57  if(el<max_size_before_onemore) return el/(get_floor_blocksize()+1);
58  return (el-max_size_before_onemore)/(get_floor_blocksize())+nr_onemore;
59  }
60  virtual void print() const
61  {
62  for(size_t i=0;i<get_nr_blocks();i++){
63  std::cout<<i<<": start: "<<get_start(i)<<" blocksize: "<<get_blocksize(i)<<std::endl;
64  }
65  }
66  void initialize(size_t total_size_, size_t nr_blocks_)
67  {
68  total_size=total_size_;
69  nr_blocks=nr_blocks_;
70  size_t div=total_size/nr_blocks;
71  nr_onemore=total_size-div*nr_blocks;
72  }
73  Distribute_Block(size_t total_size_=0, size_t nr_blocks_=1)
74  {
75  initialize(total_size_, nr_blocks_);
76  }
77 };
78 
79 
80 #endif
Definition: Distribute_Block.h:7