~ubuntu-branches/debian/sid/lammps/sid

« back to all changes in this revision

Viewing changes to lib/kokkos/core/src/Kokkos_Cuda.hpp

  • Committer: Package Import Robot
  • Author(s): Anton Gladky
  • Date: 2015-04-29 23:44:49 UTC
  • mfrom: (5.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20150429234449-mbhy9utku6hp6oq8
Tags: 0~20150313.gitfa668e1-1
Upload into unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
//@HEADER
 
3
// ************************************************************************
 
4
//
 
5
//                             Kokkos
 
6
//         Manycore Performance-Portable Multidimensional Arrays
 
7
//
 
8
//              Copyright (2012) Sandia Corporation
 
9
//
 
10
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
 
11
// the U.S. Government retains certain rights in this software.
 
12
//
 
13
// Redistribution and use in source and binary forms, with or without
 
14
// modification, are permitted provided that the following conditions are
 
15
// met:
 
16
//
 
17
// 1. Redistributions of source code must retain the above copyright
 
18
// notice, this list of conditions and the following disclaimer.
 
19
//
 
20
// 2. Redistributions in binary form must reproduce the above copyright
 
21
// notice, this list of conditions and the following disclaimer in the
 
22
// documentation and/or other materials provided with the distribution.
 
23
//
 
24
// 3. Neither the name of the Corporation nor the names of the
 
25
// contributors may be used to endorse or promote products derived from
 
26
// this software without specific prior written permission.
 
27
//
 
28
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
 
29
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
30
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
31
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
 
32
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
33
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
34
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
35
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
36
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
37
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
38
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
39
//
 
40
// Questions?  Contact  H. Carter Edwards (hcedwar@sandia.gov)
 
41
//
 
42
// ************************************************************************
 
43
//@HEADER
 
44
*/
 
45
 
 
46
#ifndef KOKKOS_CUDA_HPP
 
47
#define KOKKOS_CUDA_HPP
 
48
 
 
49
#include <Kokkos_Core_fwd.hpp>
 
50
 
 
51
// If CUDA execution space is enabled then use this header file.
 
52
 
 
53
#if defined( KOKKOS_HAVE_CUDA )
 
54
 
 
55
#include <iosfwd>
 
56
#include <vector>
 
57
 
 
58
#include <Kokkos_CudaSpace.hpp>
 
59
 
 
60
#include <Kokkos_Parallel.hpp>
 
61
#include <Kokkos_Layout.hpp>
 
62
#include <Kokkos_ScratchSpace.hpp>
 
63
#include <Kokkos_MemoryTraits.hpp>
 
64
#include <impl/Kokkos_Tags.hpp>
 
65
 
 
66
/*--------------------------------------------------------------------------*/
 
67
 
 
68
namespace Kokkos {
 
69
namespace Impl {
 
70
class CudaExec ;
 
71
} // namespace Impl
 
72
} // namespace Kokkos
 
73
 
 
74
/*--------------------------------------------------------------------------*/
 
75
 
 
76
namespace Kokkos {
 
77
 
 
78
/// \class Cuda
 
79
/// \brief Kokkos Execution Space that uses CUDA to run on GPUs.
 
80
///
 
81
/// An "execution space" represents a parallel execution model.  It tells Kokkos
 
82
/// how to parallelize the execution of kernels in a parallel_for or
 
83
/// parallel_reduce.  For example, the Threads execution space uses Pthreads or
 
84
/// C++11 threads on a CPU, the OpenMP execution space uses the OpenMP language
 
85
/// extensions, and the Serial execution space executes "parallel" kernels
 
86
/// sequentially.  The Cuda execution space uses NVIDIA's CUDA programming
 
87
/// model to execute kernels in parallel on GPUs.
 
88
class Cuda {
 
89
public:
 
90
  //! \name Type declarations that all Kokkos execution spaces must provide.
 
91
  //@{
 
92
 
 
93
  //! Tag this class as a kokkos execution space
 
94
  typedef Cuda                  execution_space ;
 
95
 
 
96
#if defined( KOKKOS_USE_CUDA_UVM )
 
97
  //! This execution space's preferred memory space.
 
98
  typedef CudaUVMSpace          memory_space ;
 
99
#else
 
100
  //! This execution space's preferred memory space.
 
101
  typedef CudaSpace             memory_space ;
 
102
#endif
 
103
 
 
104
  //! The size_type best suited for this execution space.
 
105
  typedef memory_space::size_type  size_type ;
 
106
 
 
107
  //! This execution space's preferred array layout.
 
108
  typedef LayoutLeft            array_layout ;
 
109
 
 
110
  //! For backward compatibility
 
111
  typedef Cuda                  device_type ;
 
112
  //! 
 
113
  typedef ScratchMemorySpace< Cuda >  scratch_memory_space ;
 
114
 
 
115
  //@}
 
116
  //--------------------------------------------------
 
117
  //! \name Functions that all Kokkos devices must implement.
 
118
  //@{
 
119
 
 
120
  /// \brief True if and only if this method is being called in a
 
121
  ///   thread-parallel function.
 
122
  KOKKOS_INLINE_FUNCTION static int in_parallel() {
 
123
#if defined( __CUDA_ARCH__ )
 
124
    return true;
 
125
#else
 
126
    return false;
 
127
#endif
 
128
  }
 
129
 
 
130
  /** \brief  Set the device in a "sleep" state.
 
131
   *
 
132
   * This function sets the device in a "sleep" state in which it is
 
133
   * not ready for work.  This may consume less resources than if the
 
134
   * device were in an "awake" state, but it may also take time to
 
135
   * bring the device from a sleep state to be ready for work.
 
136
   *
 
137
   * \return True if the device is in the "sleep" state, else false if
 
138
   *   the device is actively working and could not enter the "sleep"
 
139
   *   state.
 
140
   */
 
141
  static bool sleep();
 
142
 
 
143
  /// \brief Wake the device from the 'sleep' state so it is ready for work.
 
144
  ///
 
145
  /// \return True if the device is in the "ready" state, else "false"
 
146
  ///  if the device is actively working (which also means that it's
 
147
  ///  awake).
 
148
  static bool wake();
 
149
 
 
150
  /// \brief Wait until all dispatched functors complete.
 
151
  ///
 
152
  /// The parallel_for or parallel_reduce dispatch of a functor may
 
153
  /// return asynchronously, before the functor completes.  This
 
154
  /// method does not return until all dispatched functors on this
 
155
  /// device have completed.
 
156
  static void fence();
 
157
 
 
158
  //! Free any resources being consumed by the device.
 
159
  static void finalize();
 
160
 
 
161
  //! Has been initialized
 
162
  static int is_initialized();
 
163
 
 
164
  //! Print configuration information to the given output stream.
 
165
  static void print_configuration( std::ostream & , const bool detail = false );
 
166
 
 
167
  //@}
 
168
  //--------------------------------------------------
 
169
  //! \name  Cuda space instances
 
170
 
 
171
  ~Cuda() {}
 
172
  Cuda();
 
173
  explicit Cuda( const int instance_id );
 
174
 
 
175
#if defined( KOKKOS_HAVE_CXX11 )
 
176
  Cuda & operator = ( const Cuda & ) = delete ;
 
177
#else
 
178
private:
 
179
  Cuda & operator = ( const Cuda & );
 
180
public:
 
181
#endif
 
182
 
 
183
  //--------------------------------------------------------------------------
 
184
  //! \name Device-specific functions
 
185
  //@{
 
186
 
 
187
  struct SelectDevice {
 
188
    int cuda_device_id ;
 
189
    SelectDevice() : cuda_device_id(0) {}
 
190
    explicit SelectDevice( int id ) : cuda_device_id( id ) {}
 
191
  };
 
192
 
 
193
  //! Initialize, telling the CUDA run-time library which device to use.
 
194
  static void initialize( const SelectDevice = SelectDevice()
 
195
                        , const size_t num_instances = 1 );
 
196
 
 
197
  /// \brief Cuda device architecture of the selected device.
 
198
  ///
 
199
  /// This matches the __CUDA_ARCH__ specification.
 
200
  static size_type device_arch();
 
201
 
 
202
  //! Query device count.
 
203
  static size_type detect_device_count();
 
204
 
 
205
  /** \brief  Detect the available devices and their architecture
 
206
   *          as defined by the __CUDA_ARCH__ specification.
 
207
   */
 
208
  static std::vector<unsigned> detect_device_arch();
 
209
 
 
210
  //@}
 
211
  //--------------------------------------------------------------------------
 
212
 
 
213
  const cudaStream_t m_stream ;
 
214
  const int          m_device ;
 
215
};
 
216
 
 
217
} // namespace Kokkos
 
218
 
 
219
/*--------------------------------------------------------------------------*/
 
220
/*--------------------------------------------------------------------------*/
 
221
 
 
222
namespace Kokkos {
 
223
namespace Impl {
 
224
 
 
225
template<>
 
226
struct VerifyExecutionCanAccessMemorySpace
 
227
  < Kokkos::CudaSpace
 
228
  , Kokkos::Cuda::scratch_memory_space
 
229
  >
 
230
{
 
231
  enum { value = true };
 
232
  KOKKOS_INLINE_FUNCTION static void verify( void ) { }
 
233
  KOKKOS_INLINE_FUNCTION static void verify( const void * ) { }
 
234
};
 
235
 
 
236
template<>
 
237
struct VerifyExecutionCanAccessMemorySpace
 
238
  < Kokkos::HostSpace
 
239
  , Kokkos::Cuda::scratch_memory_space
 
240
  >
 
241
{
 
242
  enum { value = false };
 
243
  inline static void verify( void ) { CudaSpace::access_error(); }
 
244
  inline static void verify( const void * p ) { CudaSpace::access_error(p); }
 
245
};
 
246
 
 
247
} // namespace Impl
 
248
} // namespace Kokkos
 
249
 
 
250
/*--------------------------------------------------------------------------*/
 
251
/*--------------------------------------------------------------------------*/
 
252
 
 
253
#include <Cuda/Kokkos_CudaExec.hpp>
 
254
#include <Cuda/Kokkos_Cuda_View.hpp>
 
255
#include <Cuda/Kokkos_Cuda_Parallel.hpp>
 
256
 
 
257
//----------------------------------------------------------------------------
 
258
 
 
259
#endif /* #if defined( KOKKOS_HAVE_CUDA ) */
 
260
#endif /* #ifndef KOKKOS_CUDA_HPP */
 
261
 
 
262
 
 
263