1 /*  Copyright (C) 2009-2012  CSC - IT Center for Science Ltd.
2  *  Please see the accompanying LICENSE file for further information.
3 
4  * Helper macro for threading */
5 
6 
7 
8 #ifndef __THREADED_H
9 #define __THREADED_H
10 
11 /*
12  * Partitions a range of indices among threads.
13  *
14  * n    the number of elements in the range
15  * tn   the number of threads
16  * tid  thread id
17  * s    start index
18  * e    end index
19  */
20 #define SHARE_WORK(n, tn, tid, s, e) do {               \
21     int q = (n) / (tn);                                 \
22     int r = (n) % (tn);  /* 0 <= r < q */               \
23     *(s) = q * (tid);                                   \
24     *(e) = q * ((tid) + 1);                             \
25     if (r > 0) {                                        \
26         if (r > (tid)) {                                \
27             /* Assing this thread one element more. */  \
28             *(s) += (tid);                              \
29             *(e) += (tid) + 1;                          \
30         } else {                                        \
31            *(s) += r;                                  \
32 	   *(e) += r;					\
33         }                                               \
34     }                                                   \
35 } while (0)
36 
37 #endif  /* ! __THREADED_H */
38