2016-01-02 14:51:32 +00:00
|
|
|
/*************************************************************************************
|
|
|
|
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
|
|
|
Source file: ./lib/Threads.h
|
|
|
|
|
|
|
|
Copyright (C) 2015
|
|
|
|
|
|
|
|
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
|
|
|
|
Author: paboyle <paboyle@ph.ed.ac.uk>
|
|
|
|
|
|
|
|
This program 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 program 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 program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
See the full license in the file "LICENSE" in the top level distribution directory
|
|
|
|
*************************************************************************************/
|
|
|
|
/* END LEGAL */
|
2015-05-11 18:59:03 +01:00
|
|
|
#ifndef GRID_THREADS_H
|
|
|
|
#define GRID_THREADS_H
|
|
|
|
|
2015-05-13 00:31:00 +01:00
|
|
|
#ifdef _OPENMP
|
|
|
|
#define GRID_OMP
|
|
|
|
#endif
|
|
|
|
|
2015-05-21 06:34:33 +01:00
|
|
|
#define UNROLL _Pragma("unroll")
|
|
|
|
|
2015-05-13 00:31:00 +01:00
|
|
|
#ifdef GRID_OMP
|
2015-05-11 18:59:03 +01:00
|
|
|
#include <omp.h>
|
2015-05-31 15:09:02 +01:00
|
|
|
#define PARALLEL_FOR_LOOP _Pragma("omp parallel for ")
|
2015-05-13 00:31:00 +01:00
|
|
|
#define PARALLEL_NESTED_LOOP2 _Pragma("omp parallel for collapse(2)")
|
2015-05-11 18:59:03 +01:00
|
|
|
#else
|
|
|
|
#define PARALLEL_FOR_LOOP
|
2015-05-13 00:31:00 +01:00
|
|
|
#define PARALLEL_NESTED_LOOP2
|
2015-05-11 18:59:03 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Grid {
|
|
|
|
|
|
|
|
// Introduce a class to gain deterministic bit reproducible reduction.
|
|
|
|
// make static; perhaps just a namespace is required.
|
|
|
|
|
|
|
|
class GridThread {
|
|
|
|
public:
|
|
|
|
static int _threads;
|
2015-11-06 11:46:41 +00:00
|
|
|
static int _hyperthreads;
|
|
|
|
static int _cores;
|
2015-05-11 18:59:03 +01:00
|
|
|
|
2015-11-06 11:46:41 +00:00
|
|
|
static void SetCores(int cr) {
|
|
|
|
#ifdef GRID_OMP
|
|
|
|
_cores = cr;
|
|
|
|
#else
|
|
|
|
_cores = 1;
|
|
|
|
#endif
|
|
|
|
}
|
2015-05-11 18:59:03 +01:00
|
|
|
static void SetThreads(int thr) {
|
2015-05-13 00:31:00 +01:00
|
|
|
#ifdef GRID_OMP
|
2015-05-11 18:59:03 +01:00
|
|
|
_threads = MIN(thr,omp_get_max_threads()) ;
|
|
|
|
omp_set_num_threads(_threads);
|
|
|
|
#else
|
|
|
|
_threads = 1;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
static void SetMaxThreads(void) {
|
2015-05-13 00:31:00 +01:00
|
|
|
#ifdef GRID_OMP
|
2015-11-29 00:52:44 +00:00
|
|
|
// setenv("KMP_AFFINITY","balanced",1);
|
2015-05-11 18:59:03 +01:00
|
|
|
_threads = omp_get_max_threads();
|
|
|
|
omp_set_num_threads(_threads);
|
|
|
|
#else
|
|
|
|
_threads = 1;
|
|
|
|
#endif
|
|
|
|
};
|
2015-11-06 11:46:41 +00:00
|
|
|
static int GetHyperThreads(void) { assert(_threads%_cores ==0); return _threads/_cores; };
|
|
|
|
static int GetCores(void) { return _cores; };
|
2015-05-11 18:59:03 +01:00
|
|
|
static int GetThreads(void) { return _threads; };
|
|
|
|
static int SumArraySize(void) {return _threads;};
|
|
|
|
|
|
|
|
static void GetWork(int nwork, int me, int & mywork, int & myoff){
|
2015-11-04 11:22:14 +00:00
|
|
|
GetWork(nwork,me,mywork,myoff,_threads);
|
|
|
|
}
|
|
|
|
static void GetWork(int nwork, int me, int & mywork, int & myoff,int units){
|
|
|
|
int basework = nwork/units;
|
|
|
|
int backfill = units-(nwork%units);
|
|
|
|
if ( me >= units ) {
|
2015-05-11 18:59:03 +01:00
|
|
|
mywork = myoff = 0;
|
|
|
|
} else {
|
2015-11-04 11:22:14 +00:00
|
|
|
mywork = (nwork+me)/units;
|
2015-05-11 18:59:03 +01:00
|
|
|
myoff = basework * me;
|
|
|
|
if ( me > backfill )
|
|
|
|
myoff+= (me-backfill);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void GetWorkBarrier(int nwork, int &me, int & mywork, int & myoff){
|
|
|
|
me = ThreadBarrier();
|
|
|
|
GetWork(nwork,me,mywork,myoff);
|
|
|
|
};
|
|
|
|
|
|
|
|
static int ThreadBarrier(void) {
|
2015-05-13 00:31:00 +01:00
|
|
|
#ifdef GRID_OMP
|
2015-05-11 18:59:03 +01:00
|
|
|
#pragma omp barrier
|
|
|
|
return omp_get_thread_num();
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class obj> static void ThreadSum( std::vector<obj> &sum_array,obj &val,int me){
|
|
|
|
sum_array[me] = val;
|
|
|
|
val=zero;
|
|
|
|
ThreadBarrier();
|
|
|
|
for(int i=0;i<_threads;i++) val+= sum_array[i];
|
|
|
|
ThreadBarrier();
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|