English
File size: 1,862 Bytes
26225c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*==========================  omp_num_threads.hpp  ============================
 * include openmp and provide a function to compute a smart number of threads
 *
 * Hugo Raguet 2018
 *===========================================================================*/
#pragma once

#ifdef _OPENMP

    #include <omp.h>
    #include <cstdint>  // requires C++11, needed for uintmax_t

    /* rough minimum number of operations per thread */
    #ifndef MIN_OPS_PER_THREAD
        #define MIN_OPS_PER_THREAD 10000
    #endif

    /* num_ops is a rough estimation of the total number of operations 
     * max_threads is the maximum number of jobs performed in parallel */
    static inline int compute_num_threads(uintmax_t num_ops,
        uintmax_t max_threads)
    {
        uintmax_t num_threads = num_ops/MIN_OPS_PER_THREAD;
        if (num_threads > (unsigned) omp_get_max_threads()){
            num_threads = omp_get_max_threads();
        }
        if (num_threads > (unsigned) omp_get_num_procs()){
            num_threads = omp_get_num_procs();
        }
        if (num_threads > max_threads){ num_threads = max_threads; }
        return num_threads > 1 ? num_threads : 1;
    }

    /* overload for max_threads defaulting to num_ops */
    static inline int compute_num_threads(uintmax_t num_ops)
    { return compute_num_threads(num_ops, num_ops); }

    #define NUM_THREADS(...) \
        num_threads(compute_num_threads((uintmax_t) __VA_ARGS__))

#else /* provide default definitions for basic openmp queries */

    static inline int omp_get_num_procs(){ return 1; }
    static inline int omp_get_thread_num(){ return 0; }
    static inline int omp_get_max_threads(){ return 1; }
    static inline void omp_set_num_threads(int){ /* do nothing */ }
    static inline int compute_num_threads(int num_ops, int max_threads = 1)
        { return 1; }

#endif