LevelS C++ support library  3.83
share_utils.h
Go to the documentation of this file.
1 /*
2  * This file is part of libcxxsupport.
3  *
4  * libcxxsupport is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * libcxxsupport is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with libcxxsupport; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
21  * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
22  * (DLR).
23  */
24 
25 /*! \file share_utils.h
26  * Various convenience functions for subdividing tasks into chunks
27  *
28  * Copyright (C) 2002-2016 Max-Planck-Society
29  * \author Martin Reinecke
30  */
31 
32 #ifndef PLANCK_SHARE_UTILS_H
33 #define PLANCK_SHARE_UTILS_H
34 
35 #include <algorithm>
36 #include "datatypes.h"
37 
38 /*! Divides the index range [\a glo; \a ghi) into \a nshares approximately
39  equal parts, and returns the sub-range [\a lo; \a hi) of the
40  part with the number \a myshare (first part has index 0). */
41 inline void calcShareGeneral (int64 glo, int64 ghi, int64 nshares,
42  int64 myshare, int64 &lo, int64 &hi)
43  {
44  int64 nwork = ghi-glo;
45  int64 nbase = nwork/nshares;
46  int64 additional = nwork%nshares;
47  lo = glo+myshare*nbase + ((myshare<additional) ? myshare : additional);
48  hi = lo+nbase+(myshare<additional);
49  }
50 
51 inline int64 shareSize (int64 glo, int64 ghi, int64 nshares, int64 myshare)
52  {
53  int64 nwork = ghi-glo;
54  int64 nbase = nwork/nshares;
55  int64 additional = nwork%nshares;
56  return (myshare<additional) ? nbase+1 : nbase;
57  }
58 
59 /*! Helper class for dividing a range of work items into chunks of specified
60  size. */
62  {
63  private:
64  uint64 s_full, s_chunk, offset;
65 
66  public:
67  /*! Creates an object that produces chunk information for \a s_full_
68  work items and a desired chunk size of \a s_chunk_.
69  \note Both \a s_chunk_ and \a s_full_ must be larger than 0. */
70  chunkMaker (uint64 s_full_, uint64 s_chunk_)
71  : s_full(s_full_), s_chunk(s_chunk_), offset(0) {}
72 
73  /*! Returns the total number of chunks. */
74  uint64 nchunks() const
75  { return 1 + (s_full-1)/s_chunk; }
76 
77  /*! Returns the start index of the next chunk in \a start, and its size
78  in \a size. If all chunks have been processed already, the return
79  value is \a false, else \a true. */
80  bool getNext (uint64 &start, uint64 &size)
81  {
82  using namespace std;
83  if (offset>=s_full) return false;
84  start=offset;
85  size=min(s_chunk,s_full-offset);
86  offset+=s_chunk;
87  return true;
88  }
89  };
90 
91 #endif
void calcShareGeneral(int64 glo, int64 ghi, int64 nshares, int64 myshare, int64 &lo, int64 &hi)
Definition: share_utils.h:41
uint64 nchunks() const
Definition: share_utils.h:74
bool getNext(uint64 &start, uint64 &size)
Definition: share_utils.h:80
chunkMaker(uint64 s_full_, uint64 s_chunk_)
Definition: share_utils.h:70

Generated on Wed Nov 13 2024 12:18:16 for LevelS C++ support library