Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
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
55namespace Tpetra {
56namespace Details {
57
81template<class ValueType, class DeviceType>
82bool
83reallocDualViewIfNeeded (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
103 execution_space().fence (); // keep this fence to respect needFenceBeforeRealloc
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 return true; // we did reallocate
109 }
110 else {
111 if (newSize == 0) { // special case: realloc to 0 means always do it
113 execution_space().fence (); // keep this fence to respect needFenceBeforeRealloc
114 }
115 // If current size is 0, the DualView's Views likely lack a label.
116 dv = dual_view_type (curSize == 0 ? newLabel : dv.d_view.label (), 0);
117 return true; // we did reallocate
118 }
119 // Instead of writing curSize >= tooBigFactor * newSize, express
120 // via division to avoid overflow (for very large right-hand side).
121 // We've already tested whether newSize == 0, so this is safe.
122 else if (curSize / newSize >= tooBigFactor) {
123 // The allocation is much too big, so free it and reallocate
124 // to the new, smaller size.
126 execution_space().fence (); // keep this fence to respect needFenceBeforeRealloc
127 }
128 dv = dual_view_type (); // free first, in order to save memory
129 // If current size is 0, the DualView's Views likely lack a label.
130 dv = dual_view_type (curSize == 0 ? newLabel : dv.d_view.label (), newSize);
131 return true; // we did reallocate
132 }
133 else {
134 dv.d_view = Kokkos::subview (dv.d_view, range_type (0, newSize));
135 dv.h_view = Kokkos::subview (dv.h_view, range_type (0, newSize));
136 return false; // we did not reallocate
137 }
138 }
139}
140
142template<class ValueType, class DeviceType>
143bool
144reallocDualViewIfNeeded (Kokkos::DualView<ValueType*, DeviceType>& exports,
145 const size_t newSize,
146 const std::string& newLabel,
147 const size_t tooBigFactor = 2,
148 const bool needFenceBeforeRealloc = true)
149{
151 newLabel.c_str (),
154}
155
156} // namespace Details
157} // namespace Tpetra
158
159#endif // TPETRA_DETAILS_REALLOCDUALVIEWIFNEEDED_HPP
Declaration of Tpetra::Details::Profiling, a scope guard for Kokkos Profiling.
Struct that holds views of the contents of a CrsMatrix.
Implementation details of Tpetra.
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.
Namespace Tpetra contains the class and methods constituting the Tpetra library.