Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Kokkos_Parallel_MP_Vector.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef KOKKOS_PARALLEL_MP_VECTOR_HPP
43 #define KOKKOS_PARALLEL_MP_VECTOR_HPP
44 
45 #include "Sacado_MP_Vector.hpp"
46 #include "Kokkos_Core.hpp"
47 
48 //----------------------------------------------------------------------------
49 // Kokkos execution policies useful for Sacado::MP::Vector scalar type
50 //----------------------------------------------------------------------------
51 
52 namespace Kokkos {
53 
57 template< class ExecSpace >
59 
61  typedef ExecSpace execution_space ;
62  typedef void work_tag ;
63 
64  size_t range;
65  size_t team;
66  size_t shared;
67 
68  MPVectorWorkConfig( const size_t range_,
69  const size_t team_,
70  const size_t shared_ = 0 ) :
71  range(range_), team(team_), shared(shared_) {}
72 
73  ExecSpace space() const { return ExecSpace(); }
74 };
75 
76 namespace Impl {
77 
78 #if defined( KOKKOS_ENABLE_THREADS )
79 // Specialization of ParallelFor<> for MPVectorWorkConfig and Threads
80 // The default implementation ignores the team size and uses the standard
81 // work-range implementation. In the future maybe we should try and use
82 // hyperthreads in a useful way. That would require:
83 // -- interpreting the team-size differently, rather as the sacado size
84 // -- determining the vector size of the architecture
85 // -- laying out the threads differently to use hyperthreads across the
86 // the sacado dimension
87 template< class FunctorType >
88 class ParallelFor< FunctorType , MPVectorWorkConfig< Threads > > :
89  public ParallelFor< FunctorType , Kokkos::RangePolicy< Threads > > {
90  typedef Kokkos::RangePolicy< Threads > Policy ;
91 public:
92  ParallelFor( const FunctorType & functor ,
93  const MPVectorWorkConfig< Threads > & work_config ) :
94  ParallelFor< FunctorType , Policy >( functor ,
95  Policy( 0, work_config.range ) ) {}
96 };
97 #endif
98 
99 #if defined( KOKKOS_ENABLE_OPENMP )
100 // Specialization of ParallelFor<> for MPVectorWorkConfig and OpenMP
101 // The default implementation ignores the team size and uses the standard
102 // work-range implementation. In the future maybe we should try and use
103 // hyperthreads in a useful way. That would require:
104 // -- interpreting the team-size differently, rather as the sacado size
105 // -- determining the vector size of the architecture
106 // -- laying out the threads differently to use hyperthreads across the
107 // the sacado dimension
108 template< class FunctorType >
109 class ParallelFor< FunctorType , MPVectorWorkConfig< OpenMP > > :
110  public ParallelFor< FunctorType , Kokkos::RangePolicy< OpenMP > > {
111  typedef Kokkos::RangePolicy< OpenMP > Policy ;
112 public:
113  ParallelFor( const FunctorType & functor ,
114  const MPVectorWorkConfig< OpenMP > & work_config ) :
115  ParallelFor< FunctorType , Policy >( functor ,
116  Policy( 0, work_config.range ) ) {}
117 };
118 #endif
119 
120 #if defined(KOKKOS_ENABLE_SERIAL)
121 // Specialization of ParallelFor<> for MPVectorWorkConfig and Serial
122 // The default implementation ignores the team size and uses the standard
123 // work-range implementation. In the future maybe we should try and use
124 // hyperthreads in a useful way. That would require:
125 // -- interpreting the team-size differently, rather as the sacado size
126 // -- determining the vector size of the architecture
127 // -- laying out the threads differently to use hyperthreads across the
128 // the sacado dimension
129 template< class FunctorType >
130 class ParallelFor< FunctorType , MPVectorWorkConfig< Serial > > :
131  public ParallelFor< FunctorType , Kokkos::RangePolicy< Serial > > {
132  typedef Kokkos::RangePolicy< Serial > Policy ;
133 public:
134  ParallelFor( const FunctorType & functor ,
135  const MPVectorWorkConfig< Serial > & work_config ) :
136  ParallelFor< FunctorType , Policy >( functor ,
137  Policy( 0, work_config.range ) ) {}
138 };
139 #endif // defined(KOKKOS_ENABLE_SERIAL)
140 
141 #if defined( KOKKOS_ENABLE_CUDA ) && defined( __CUDACC__ )
142 
143 // Specialization of ParallelFor<> for MPVectorWorkConfig on Cuda
144 // Here we use threadIdx.x for each entry in the specified team-size
145 template< class FunctorType >
146 class ParallelFor< FunctorType , MPVectorWorkConfig< Cuda > > {
147 public:
148 
149  typedef Kokkos::RangePolicy< Cuda > Policy;
150 
151  const FunctorType m_functor ;
152  const MPVectorWorkConfig< Cuda > m_config;
153  const Cuda::size_type m_work ;
154 
155  inline
156  __device__
157  void operator()(void) const
158  {
159  const Cuda::size_type work_stride = blockDim.y * gridDim.x ;
160 
161  for ( Cuda::size_type iwork = threadIdx.y + blockDim.y * blockIdx.x ;
162  iwork < m_work ;
163  iwork += work_stride ) {
164  m_functor( iwork , threadIdx.x );
165  }
166  }
167 
168  ParallelFor( const FunctorType & functor ,
169  const MPVectorWorkConfig< Cuda > & work_config )
170  : m_functor( functor ) ,
171  m_config( work_config ) ,
172  m_work( work_config.range )
173  {
174  }
175 
176  inline
177  void execute() const
178  {
179  // To do: query number of registers used by functor and adjust
180  // nwarp accordingly to get maximum occupancy
181 
182  Cuda::size_type nwarp = 0;
183  if (m_config.team > CudaTraits::WarpSize) {
184  const Cuda::size_type warps_per_team =
185  ( m_config.team + CudaTraits::WarpSize-1 ) / CudaTraits::WarpSize;
186  nwarp = cuda_internal_maximum_warp_count() / warps_per_team;
187  }
188  else {
189  const Cuda::size_type teams_per_warp =
190  CudaTraits::WarpSize / m_config.team ;
191  nwarp = cuda_internal_maximum_warp_count() * teams_per_warp;
192  }
193  const dim3 block( m_config.team , nwarp , 1 );
194 
195  Cuda::size_type nblock =
196  std::min( (m_work + block.y - 1 ) / block.y ,
197  cuda_internal_maximum_grid_count() );
198  const dim3 grid( nblock , 1 , 1 );
199 
200  const Cuda::size_type shared = m_config.shared;
201  CudaParallelLaunch< ParallelFor >( *this , grid , block , shared , Policy().space().impl_internal_space_instance(), false );
202  }
203 };
204 
205 #endif
206 
207 } // namespace Impl
208 
209 } // namespace Kokkos
210 
211 //----------------------------------------------------------------------------
212 //----------------------------------------------------------------------------
213 
214 #endif /* #ifndef KOKKOS_ATOMIC_MP_VECTOR_HPP */
MPVectorWorkConfig(const size_t range_, const size_t team_, const size_t shared_=0)
KOKKOS_INLINE_FUNCTION PCE< Storage > min(const typename PCE< Storage >::value_type &a, const PCE< Storage > &b)
Team-based parallel work configuration for Sacado::MP::Vector.