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

« back to all changes in this revision

Viewing changes to lib/kokkos/core/src/Cuda/Kokkos_Cuda_Internal.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: Manycore Performance-Portable Multidimensional Arrays
 
6
//              Copyright (2012) Sandia Corporation
 
7
//
 
8
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
 
9
// the U.S. Government retains certain rights in this software.
 
10
//
 
11
// Redistribution and use in source and binary forms, with or without
 
12
// modification, are permitted provided that the following conditions are
 
13
// met:
 
14
//
 
15
// 1. Redistributions of source code must retain the above copyright
 
16
// notice, this list of conditions and the following disclaimer.
 
17
//
 
18
// 2. Redistributions in binary form must reproduce the above copyright
 
19
// notice, this list of conditions and the following disclaimer in the
 
20
// documentation and/or other materials provided with the distribution.
 
21
//
 
22
// 3. Neither the name of the Corporation nor the names of the
 
23
// contributors may be used to endorse or promote products derived from
 
24
// this software without specific prior written permission.
 
25
//
 
26
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
 
27
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
28
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
29
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
 
30
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
31
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
32
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
33
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
34
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
35
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
36
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
37
//
 
38
// Questions? Contact  H. Carter Edwards (hcedwar@sandia.gov)
 
39
//
 
40
// ************************************************************************
 
41
//@HEADER
 
42
*/
 
43
 
 
44
#ifndef KOKKOS_CUDA_INTERNAL_HPP
 
45
#define KOKKOS_CUDA_INTERNAL_HPP
 
46
 
 
47
namespace Kokkos {
 
48
namespace Impl {
 
49
 
 
50
void cuda_internal_error_throw( cudaError e , const char * name, const char * file = NULL, const int line = 0 );
 
51
 
 
52
void cuda_device_synchronize();
 
53
 
 
54
inline
 
55
void cuda_internal_safe_call( cudaError e , const char * name, const char * file = NULL, const int line = 0)
 
56
{
 
57
  if ( cudaSuccess != e ) { cuda_internal_error_throw( e , name, file, line ); }
 
58
}
 
59
 
 
60
template<class DriverType>
 
61
int cuda_get_max_block_size(const typename DriverType::functor_type & f) {
 
62
#if ( CUDA_VERSION < 6050 )
 
63
  return 256;
 
64
#else
 
65
  bool Large = ( CudaTraits::ConstantMemoryUseThreshold < sizeof(DriverType) );
 
66
 
 
67
  int numBlocks;
 
68
  if(Large) {
 
69
    int blockSize=32;
 
70
    int sharedmem = FunctorTeamShmemSize< typename DriverType::functor_type  >::value( f , blockSize );
 
71
    cudaOccupancyMaxActiveBlocksPerMultiprocessor(
 
72
        &numBlocks,
 
73
        cuda_parallel_launch_constant_memory<DriverType>,
 
74
        blockSize,
 
75
        sharedmem);
 
76
 
 
77
    while (blockSize<1024 && numBlocks>0) {
 
78
      blockSize*=2;
 
79
      sharedmem = FunctorTeamShmemSize< typename DriverType::functor_type  >::value( f , blockSize );
 
80
 
 
81
      cudaOccupancyMaxActiveBlocksPerMultiprocessor(
 
82
          &numBlocks,
 
83
          cuda_parallel_launch_constant_memory<DriverType>,
 
84
          blockSize,
 
85
          sharedmem);
 
86
    }
 
87
    if(numBlocks>0) return blockSize;
 
88
    else return blockSize/2;
 
89
  } else {
 
90
    int blockSize=32;
 
91
    int sharedmem = FunctorTeamShmemSize< typename DriverType::functor_type  >::value( f , blockSize );
 
92
    cudaOccupancyMaxActiveBlocksPerMultiprocessor(
 
93
        &numBlocks,
 
94
        cuda_parallel_launch_local_memory<DriverType>,
 
95
        blockSize,
 
96
        sharedmem);
 
97
 
 
98
    while (blockSize<1024 && numBlocks>0) {
 
99
      blockSize*=2;
 
100
      sharedmem = FunctorTeamShmemSize< typename DriverType::functor_type  >::value( f , blockSize );
 
101
 
 
102
      cudaOccupancyMaxActiveBlocksPerMultiprocessor(
 
103
          &numBlocks,
 
104
          cuda_parallel_launch_local_memory<DriverType>,
 
105
          blockSize,
 
106
          sharedmem);
 
107
    }
 
108
    if(numBlocks>0) return blockSize;
 
109
    else return blockSize/2;
 
110
  }
 
111
#endif
 
112
}
 
113
 
 
114
template<class DriverType>
 
115
int cuda_get_opt_block_size(const typename DriverType::functor_type & f) {
 
116
#if ( CUDA_VERSION < 6050 )
 
117
  return 256;
 
118
#else
 
119
  bool Large = ( CudaTraits::ConstantMemoryUseThreshold < sizeof(DriverType) );
 
120
 
 
121
  int blockSize=16;
 
122
  int numBlocks;
 
123
  int sharedmem;
 
124
  int maxOccupancy=0;
 
125
  int bestBlockSize=0;
 
126
 
 
127
  if(Large) {
 
128
    while(blockSize<1024) {
 
129
      blockSize*=2;
 
130
 
 
131
      //calculate the occupancy with that optBlockSize and check whether its larger than the largest one found so far
 
132
      sharedmem = FunctorTeamShmemSize< typename DriverType::functor_type  >::value( f , blockSize );
 
133
      cudaOccupancyMaxActiveBlocksPerMultiprocessor(
 
134
              &numBlocks,
 
135
              cuda_parallel_launch_constant_memory<DriverType>,
 
136
              blockSize,
 
137
              sharedmem);
 
138
      if(maxOccupancy < numBlocks*blockSize) {
 
139
        maxOccupancy = numBlocks*blockSize;
 
140
        bestBlockSize = blockSize;
 
141
      }
 
142
    }
 
143
  } else {
 
144
    while(blockSize<1024) {
 
145
      blockSize*=2;
 
146
      sharedmem = FunctorTeamShmemSize< typename DriverType::functor_type  >::value( f , blockSize );
 
147
 
 
148
      cudaOccupancyMaxActiveBlocksPerMultiprocessor(
 
149
              &numBlocks,
 
150
              cuda_parallel_launch_local_memory<DriverType>,
 
151
              blockSize,
 
152
              sharedmem);
 
153
 
 
154
      if(maxOccupancy < numBlocks*blockSize) {
 
155
        maxOccupancy = numBlocks*blockSize;
 
156
        bestBlockSize = blockSize;
 
157
      }
 
158
    }
 
159
  }
 
160
  return bestBlockSize;
 
161
#endif
 
162
}
 
163
 
 
164
}
 
165
}
 
166
 
 
167
#define CUDA_SAFE_CALL( call )  \
 
168
        Kokkos::Impl::cuda_internal_safe_call( call , #call, __FILE__, __LINE__ )
 
169
 
 
170
#endif /* #ifndef KOKKOS_CUDA_INTERNAL_HPP */
 
171