Grid 0.7.0
ThreadReduction.h
Go to the documentation of this file.
1/*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/ThreadReduction.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// Introduce a class to gain deterministic bit reproducible reduction.
32// make static; perhaps just a namespace is required.
34
36public:
37 static int _threads;
38 static int _hyperthreads;
39 static int _cores;
40
41 static void SetCores(int cr) {
42#ifdef GRID_OMP
43 _cores = cr;
44#else
45 _cores = 1;
46#endif
47 }
48 static void SetThreads(int thr) {
49#ifdef GRID_OMP
50 _threads = MIN(thr,omp_get_max_threads()) ;
51 omp_set_num_threads(_threads);
52#else
53 _threads = 1;
54#endif
55 };
56 static void SetMaxThreads(void) {
57#ifdef GRID_OMP
58 _threads = omp_get_max_threads();
59 omp_set_num_threads(_threads);
60#else
61 _threads = 1;
62#endif
63 };
64 static int GetHyperThreads(void) { assert(_threads%_cores ==0); return _threads/_cores; };
65 static int GetCores(void) { return _cores; };
66 static int GetThreads(void) { return _threads; };
67 static int SumArraySize(void) {return _threads;};
68
69 static void GetWork(int nwork, int me, int & mywork, int & myoff){
70 GetWork(nwork,me,mywork,myoff,_threads);
71 }
72 static void GetWork(int nwork, int me, int & mywork, int & myoff,int units){
73 int basework = nwork/units;
74 int backfill = units-(nwork%units);
75 if ( me >= units ) {
76 mywork = myoff = 0;
77 } else {
78 mywork = (nwork+me)/units;
79 myoff = basework * me;
80 if ( me > backfill )
81 myoff+= (me-backfill);
82 }
83 return;
84 };
85
86 static void GetWorkBarrier(int nwork, int &me, int & mywork, int & myoff){
87 me = ThreadBarrier();
88 GetWork(nwork,me,mywork,myoff);
89 };
90
91 static int ThreadBarrier(void) {
92#ifdef GRID_OMP
93#pragma omp barrier
94 return omp_get_thread_num();
95#else
96 return 0;
97#endif
98 };
99
100 template<class obj> static void ThreadSum( std::vector<obj> &sum_array,obj &val,int me){
101 sum_array[me] = val;
102 val=Zero();
104 for(int i=0;i<_threads;i++) val+= sum_array[i];
106 }
107
108 static void bcopy(const void *src, void *dst, size_t len) {
109#ifdef GRID_OMP
110#pragma omp parallel
111 {
112 const char *c_src =(char *) src;
113 char *c_dest=(char *) dst;
114 int me,mywork,myoff;
115 GridThread::GetWorkBarrier(len,me, mywork,myoff);
116 bcopy(&c_src[myoff],&c_dest[myoff],mywork);
117 }
118#else
119 bcopy(src,dst,len);
120#endif
121 }
122
123
124};
125
127
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
#define MIN(a, b)
Definition Zolotarev.cc:23
static void SetMaxThreads(void)
static int GetThreads(void)
static int _cores
static void bcopy(const void *src, void *dst, size_t len)
static int ThreadBarrier(void)
static void GetWork(int nwork, int me, int &mywork, int &myoff, int units)
static void ThreadSum(std::vector< obj > &sum_array, obj &val, int me)
static int _hyperthreads
static void GetWorkBarrier(int nwork, int &me, int &mywork, int &myoff)
static int SumArraySize(void)
static int GetHyperThreads(void)
static int GetCores(void)
static void GetWork(int nwork, int me, int &mywork, int &myoff)
static int _threads
static void SetCores(int cr)
static void SetThreads(int thr)
Definition Simd.h:194