threading.h 2.37 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 *  This file is part of the MR utility library.
 *
 *  This code is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This code is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this code; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

19
/* Copyright (C) 2019-2020 Peter Bell, Max-Planck-Society
Martin Reinecke's avatar
Martin Reinecke committed
20
   Authors: Peter Bell, Martin Reinecke */
Martin Reinecke's avatar
Martin Reinecke committed
21

Martin Reinecke's avatar
Martin Reinecke committed
22
23
#ifndef DUCC0_THREADING_H
#define DUCC0_THREADING_H
Martin Reinecke's avatar
Martin Reinecke committed
24
25

#include <functional>
Martin Reinecke's avatar
Martin Reinecke committed
26

Martin Reinecke's avatar
Martin Reinecke committed
27
namespace ducc0 {
Martin Reinecke's avatar
Martin Reinecke committed
28

Martin Reinecke's avatar
Martin Reinecke committed
29
namespace detail_threading {
Martin Reinecke's avatar
Martin Reinecke committed
30

Martin Reinecke's avatar
Martin Reinecke committed
31
using std::size_t;
Martin Reinecke's avatar
Martin Reinecke committed
32

Martin Reinecke's avatar
rework    
Martin Reinecke committed
33
struct Range
Martin Reinecke's avatar
Martin Reinecke committed
34
  {
Martin Reinecke's avatar
rework    
Martin Reinecke committed
35
36
37
38
39
  size_t lo, hi;
  Range() : lo(0), hi(0) {}
  Range(size_t lo_, size_t hi_) : lo(lo_), hi(hi_) {}
  operator bool() const { return hi>lo; }
  };
Martin Reinecke's avatar
Martin Reinecke committed
40

Martin Reinecke's avatar
rework    
Martin Reinecke committed
41
class Scheduler
Martin Reinecke's avatar
Martin Reinecke committed
42
43
  {
  public:
44
    virtual ~Scheduler() {}
Martin Reinecke's avatar
Martin Reinecke committed
45
46
47
    virtual size_t num_threads() const = 0;
    virtual size_t thread_num() const = 0;
    virtual Range getNext() = 0;
Martin Reinecke's avatar
Martin Reinecke committed
48
49
  };

Martin Reinecke's avatar
Martin Reinecke committed
50
51
void set_default_nthreads(size_t new_default_nthreads);
size_t get_default_nthreads();
Martin Reinecke's avatar
Martin Reinecke committed
52

53
54
void set_pool_size(size_t new_pool_size);

Martin Reinecke's avatar
Martin Reinecke committed
55
56
57
58
59
60
61
62
63
void execSingle(size_t nwork,
  std::function<void(Scheduler &)> func);
void execStatic(size_t nwork, size_t nthreads, size_t chunksize,
  std::function<void(Scheduler &)> func);
void execDynamic(size_t nwork, size_t nthreads, size_t chunksize_min,
  std::function<void(Scheduler &)> func);
void execGuided(size_t nwork, size_t nthreads, size_t chunksize_min,
  double fact_max, std::function<void(Scheduler &)> func);
void execParallel(size_t nthreads, std::function<void(Scheduler &)> func);
Martin Reinecke's avatar
rework    
Martin Reinecke committed
64

Martin Reinecke's avatar
Martin Reinecke committed
65
} // end of namespace detail_threading
Martin Reinecke's avatar
Martin Reinecke committed
66

67
using detail_threading::set_pool_size;
68
69
using detail_threading::get_default_nthreads;
using detail_threading::set_default_nthreads;
Martin Reinecke's avatar
Martin Reinecke committed
70
71
72
73
74
using detail_threading::Scheduler;
using detail_threading::execSingle;
using detail_threading::execStatic;
using detail_threading::execDynamic;
using detail_threading::execGuided;
Martin Reinecke's avatar
Martin Reinecke committed
75
using detail_threading::execParallel;
Martin Reinecke's avatar
Martin Reinecke committed
76

Martin Reinecke's avatar
Martin Reinecke committed
77
} // end of namespace ducc0
Martin Reinecke's avatar
Martin Reinecke committed
78
79

#endif