Grid 0.7.0
Threads.h
Go to the documentation of this file.
1/*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/Threads.h
6
7 Copyright (C) 2015
8
9Author: Peter Boyle <paboyle@ph.ed.ac.uk>
10Author: paboyle <paboyle@ph.ed.ac.uk>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26 See the full license in the file "LICENSE" in the top level distribution directory
27*************************************************************************************/
28/* END LEGAL */
29#pragma once
30
31#ifndef MAX
32#define MAX(x,y) ((x)>(y)?(x):(y))
33#define MIN(x,y) ((x)>(y)?(y):(x))
34#endif
35
36#define strong_inline __attribute__((always_inline)) inline
37#define UNROLL _Pragma("unroll")
38
40// New primitives; explicit host thread calls, and accelerator data parallel calls
42
43#ifdef _OPENMP
44#define GRID_OMP
45#include <omp.h>
46#endif
47
48#ifdef GRID_OMP
49#define DO_PRAGMA_(x) _Pragma (#x)
50#define DO_PRAGMA(x) DO_PRAGMA_(x)
51#define thread_num(a) omp_get_thread_num()
52#define thread_max(a) omp_get_max_threads()
53#else
54#define DO_PRAGMA_(x)
55#define DO_PRAGMA(x)
56#define thread_num(a) (0)
57#define thread_max(a) (1)
58#endif
59
60#define thread_for( i, num, ... ) DO_PRAGMA(omp parallel for schedule(static)) for ( uint64_t i=0;i<num;i++) { __VA_ARGS__ } ;
61#define thread_for2d( i1, n1,i2,n2, ... ) \
62 DO_PRAGMA(omp parallel for collapse(2)) \
63 for ( uint64_t i1=0;i1<n1;i1++) { \
64 for ( uint64_t i2=0;i2<n2;i2++) { \
65 { __VA_ARGS__ } ; \
66 }}
67#define thread_foreach( i, container, ... ) DO_PRAGMA(omp parallel for schedule(static)) for ( uint64_t i=container.begin();i<container.end();i++) { __VA_ARGS__ } ;
68#define thread_for_in_region( i, num, ... ) DO_PRAGMA(omp for schedule(static)) for ( uint64_t i=0;i<num;i++) { __VA_ARGS__ } ;
69#define thread_for_collapse2( i, num, ... ) DO_PRAGMA(omp parallel for collapse(2)) for ( uint64_t i=0;i<num;i++) { __VA_ARGS__ } ;
70#define thread_for_collapse( N , i, num, ... ) DO_PRAGMA(omp parallel for collapse ( N ) ) for ( uint64_t i=0;i<num;i++) { __VA_ARGS__ } ;
71#define thread_for_collapse_in_region( N , i, num, ... ) DO_PRAGMA(omp for collapse ( N )) for ( uint64_t i=0;i<num;i++) { __VA_ARGS__ } ;
72#define thread_region DO_PRAGMA(omp parallel)
73#define thread_critical DO_PRAGMA(omp critical)
74
75#ifdef GRID_OMP
76inline void thread_bcopy(const void *from, void *to,size_t bytes)
77{
78 const uint64_t *ufrom = (const uint64_t *)from;
79 uint64_t *uto = (uint64_t *)to;
80 assert(bytes%8==0);
81 uint64_t words=bytes/8;
82 thread_for(w,words,{
83 uto[w] = ufrom[w];
84 });
85}
86#else
87inline void thread_bcopy(const void *from, void *to,size_t bytes)
88{
89 bcopy(from,to,bytes);
90}
91#endif
void thread_bcopy(const void *from, void *to, size_t bytes)
Definition Threads.h:87
#define thread_for(i, num,...)
Definition Threads.h:60