Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tpetra_Details_reallocDualViewIfNeeded.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
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 Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #ifndef TPETRA_DETAILS_REALLOCDUALVIEWIFNEEDED_HPP
43 #define TPETRA_DETAILS_REALLOCDUALVIEWIFNEEDED_HPP
44 
51 
53 #include "Kokkos_DualView.hpp"
54 
55 namespace Tpetra {
56 namespace Details {
57 
81 template<class ValueType, class DeviceType>
82 bool
83 reallocDualViewIfNeeded (Kokkos::DualView<ValueType*, DeviceType>& dv,
84  const size_t newSize,
85  const char newLabel[],
86  const size_t tooBigFactor = 2,
87  const bool needFenceBeforeRealloc = true)
88 {
89  typedef typename DeviceType::execution_space execution_space;
90  typedef Kokkos::DualView<ValueType*, DeviceType> dual_view_type;
91  typedef Kokkos::pair<size_t, size_t> range_type;
92 
93  // Profiling this matters, because GPU allocations can be expensive.
95  ProfilingRegion region ("Tpetra::Details::reallocDualViewIfNeeded");
96 
97  const size_t curSize = static_cast<size_t> (dv.extent (0));
98  if (curSize == newSize) {
99  return false; // did not reallocate
100  }
101  else if (curSize < newSize) { // too small; need to reallocate
102  if (needFenceBeforeRealloc) {
103  execution_space::fence ();
104  }
105  dv = dual_view_type (); // free first, in order to save memory
106  // If current size is 0, the DualView's Views likely lack a label.
107  dv = dual_view_type (curSize == 0 ? newLabel : dv.d_view.label (), newSize);
108  execution_space::fence ();
109  return true; // we did reallocate
110  }
111  else {
112  if (newSize == 0) { // special case: realloc to 0 means always do it
113  if (needFenceBeforeRealloc) {
114  execution_space::fence ();
115  }
116  // If current size is 0, the DualView's Views likely lack a label.
117  dv = dual_view_type (curSize == 0 ? newLabel : dv.d_view.label (), 0);
118  execution_space::fence ();
119  return true; // we did reallocate
120  }
121  // Instead of writing curSize >= tooBigFactor * newSize, express
122  // via division to avoid overflow (for very large right-hand side).
123  // We've already tested whether newSize == 0, so this is safe.
124  else if (curSize / newSize >= tooBigFactor) {
125  // The allocation is much too big, so free it and reallocate
126  // to the new, smaller size.
127  if (needFenceBeforeRealloc) {
128  execution_space::fence ();
129  }
130  dv = dual_view_type (); // free first, in order to save memory
131  // If current size is 0, the DualView's Views likely lack a label.
132  dv = dual_view_type (curSize == 0 ? newLabel : dv.d_view.label (), newSize);
133  execution_space::fence ();
134  return true; // we did reallocate
135  }
136  else {
137  dv.d_view = Kokkos::subview (dv.d_view, range_type (0, newSize));
138  dv.h_view = Kokkos::subview (dv.h_view, range_type (0, newSize));
139  return false; // we did not reallocate
140  }
141  }
142 }
143 
145 template<class ValueType, class DeviceType>
146 bool
147 reallocDualViewIfNeeded (Kokkos::DualView<ValueType*, DeviceType>& exports,
148  const size_t newSize,
149  const std::string& newLabel,
150  const size_t tooBigFactor = 2,
151  const bool needFenceBeforeRealloc = true)
152 {
153  return reallocDualViewIfNeeded<ValueType, DeviceType> (exports, newSize,
154  newLabel.c_str (),
155  tooBigFactor,
156  needFenceBeforeRealloc);
157 }
158 
159 } // namespace Details
160 } // namespace Tpetra
161 
162 #endif // TPETRA_DETAILS_REALLOCDUALVIEWIFNEEDED_HPP
Declaration of Tpetra::Details::Profiling, a scope guard for Kokkos Profiling.
bool reallocDualViewIfNeeded(Kokkos::DualView< ValueType *, DeviceType > &dv, const size_t newSize, const char newLabel[], const size_t tooBigFactor=2, const bool needFenceBeforeRealloc=true)
Reallocate the DualView in/out argument, if needed.