OpenMP.h 1.79 KB
Newer Older
1
2
3
4
5
6
7
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
8
 * Authors:
9
10
11
12
13
14
15
16
17
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
18
 *
19
 ******************************************************************************/
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33



/** \file OpenMP.h */

#ifndef AMDIS_OPENMP_H
#define AMDIS_OPENMP_H

#ifdef _OPENMP
#include <omp.h>
#include <vector>
#endif

namespace AMDiS {
34

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifdef _OPENMP

  template<typename T>
  class ThreadPrivate {
  public:
    ThreadPrivate()
      : data(omp_get_max_threads())
    {}

    ThreadPrivate(T val)
      : data(omp_get_max_threads(), val)
    {}

    inline T& get()
    {
50
#ifndef NDEBUG
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
51
      if (omp_get_thread_num() >= data.size()) {
52
	std::cout << "Error in ThreadPrivate::get()!\n";
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
53
54
55
56
57
58
59
60
	exit(0);
      }
#endif
      return data[omp_get_thread_num()];
    }

    inline void set(T& val)
    {
61
#ifndef NDEBUG
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
62
      if (omp_get_thread_num() >= data.size()) {
63
	std::cout << "Error in ThreadPrivate::set()!\n";
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
64
65
66
67
68
69
70
	exit(0);
      }
#endif
      data[omp_get_thread_num()] = val;
    }

  private:
71
    std::vector<T> data;
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  };

#else

  template<typename T>
  class ThreadPrivate {
  public:
    ThreadPrivate() {}

    ThreadPrivate(T val)
      : data(val)
    {}

    inline T& get()
    {
      return data;
    }

    inline void set(T& val)
    {
      data = val;
    }

  private:
    T data;
  };

#endif

}

#endif