/usr/include/trilinos/Kokkos_NodeHelpers.hpp is in libtrilinos-tpetra-dev 12.4.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | //@HEADER
// ************************************************************************
//
// Kokkos: Node API and Parallel Node Kernels
// Copyright (2008) Sandia Corporation
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ************************************************************************
//@HEADER
#ifndef KOKKOS_NODE_HELPERS_HPP_
#define KOKKOS_NODE_HELPERS_HPP_
#include <Kokkos_NodeAPIConfigDefs.hpp>
#include "Teuchos_Array.hpp"
#include "Teuchos_ArrayRCP.hpp"
namespace KokkosClassic {
/// \class ReadyBufferHelper
/// \brief A class to assist in readying buffers via the Node::readyBuffers() method.
/// \ingroup kokkos_node_api
///
/// \tparam Node The Kokkos Node type.
///
/// Kokkos asks that you call the Node's readyBuffers() method on
/// all compute buffers that you plan to pass into a kernel. In a
/// debug build, this checks to make sure that the ArrayRCP objects
/// are really compute buffers. ReadyBufferHelper provides a
/// transaction-like interface to help you with this. The
/// ReadyBufferHelper also keeps the ArrayRCP objects for you during
/// its lifetime. This prevents them from falling out of scope and
/// getting deleted, thus ensuring that when you extract the raw
/// pointer to give to the Kokkos kernel, the pointer will be valid
/// throughout the lifetime of the kernel.
///
/// Here is an example of how to use ReadyBufferHelper with a simple
/// Kokkos kernel. Suppose you have a \c parallel_for kernel Op
/// that takes a const array and a nonconst array:
/// \code
/// template<class T>
/// class Op {
/// public:
/// Op (const T* x, T* y, size_t n) :
/// x_ (x), y_ (y), n_ (n) {}
///
/// inline KERNEL_PREFIX void execute (size_t i) {
/// y_[i] += 2.0 * x_[i];
/// }
/// private:
/// const T* x_;
/// T* y_;
/// size_t n_;
/// };
/// \endcode
/// Here's how you would use ReadyBufferHelper when invoking the
/// kernel in parallel:
/// \code
/// using Teuchos::ArrayRCP;
/// using Teuchos::RCP;
///
/// // Suppose node_type is the Kokkos Node type.
/// RCP<node_type> node = ...;
///
/// // x and y are compute buffers that you got elsewhere.
/// ArrayRCP<const T> x = ...;
/// ArrayRCP<T> y = ...;
/// const size_t n = x.size ();
///
/// ReadyBufferHelper<node_type> rbh (node);
/// rbh.begin (); // Start adding compute buffers
///
/// // Create the Kokkos kernel "work-data pair" (Kokkos' phrase for
/// // what other programming languages call "closure"). When adding
/// // a const buffer (ArrayRCP<const T>), omit the "const" when naming
/// // the type in the template method call.
/// //
/// // If T is a template parameter in this scope, you need the "template"
/// // keyword when invoking ReadyBufferHelper template methods. You don't
/// // need it if T is a concrete type in this scope.
/// Op wdp (rbh.template addConstBuffer<T> (x),
/// rbh.template addNonConstBuffer<T> (y),
/// n);
/// rbh.end (); // Done adding compute buffers
///
/// // Invoke the kernel in parallel over the range 0, ..., n-1.
/// // The ReadyBufferHelper will protect x and y from falling out
/// // of scope.
/// //
/// // If T is a template parameter in this scope, you need the "template"
/// // keyword. You don't need it if T is a concrete type in this scope.
/// node->template parallel_for<Op<T> > (0, x.size (), wdp);
/// \endcode
template <class Node>
class ReadyBufferHelper {
public:
/*! The node via which buffers are being readied. */
ReadyBufferHelper(RCP<Node> node);
/*! Destructor. */
virtual ~ReadyBufferHelper();
//! Tell this object that you are ready to start adding buffers.
void begin();
//! Add a const buffer, and return its raw pointer.
template <class T>
const T* addConstBuffer(ArrayRCP<const T> buff);
//! Add a non-const buffer, and return its raw pointer.
template <class T>
T* addNonConstBuffer(ArrayRCP<T> buff);
//! Tell this object that you are done adding buffers.
void end();
protected:
//! The Kokkos Node instance for which to add buffers.
RCP<Node> node_;
//! List of compute buffers that were added with addConstBuffer().
Array<ArrayRCP<const char> > cbufs_;
//! List of compute buffers that were added with addNonConstBuffer().
Array<ArrayRCP< char> > ncbufs_;
};
template <class Node>
ReadyBufferHelper<Node>::ReadyBufferHelper (RCP<Node> node)
: node_(node)
{}
template <class Node>
ReadyBufferHelper<Node>::~ReadyBufferHelper()
{}
template <class Node>
void ReadyBufferHelper<Node>::begin() {
cbufs_.clear ();
ncbufs_.clear ();
}
template <class Node>
template <class T>
const T* ReadyBufferHelper<Node>::addConstBuffer (ArrayRCP<const T> buff) {
cbufs_.push_back (arcp_reinterpret_cast<const char> (buff));
return buff.get ();
}
template <class Node>
template <class T>
T* ReadyBufferHelper<Node>::addNonConstBuffer (ArrayRCP<T> buff) {
cbufs_.push_back (arcp_reinterpret_cast<char> (buff));
return buff.get ();
}
template <class Node>
void ReadyBufferHelper<Node>::end () {
node_->readyBuffers (cbufs_ (), ncbufs_ ());
}
/// \class ArrayOfViewsHelper
/// \brief Helper class for getting an array of views.
/// \ingroup kokkos_node_api
///
/// \tparam Node The Kokkos Node type.
///
/// The class method getArrayOfNonConstView takes an array of
/// (device) buffers, and returns an array of (host) views of those
/// buffers. The host views may be either write-only (meaning that
/// their contents on the host before being written are undefined),
/// or read-and-write (meaning that they have valid contents on the
/// host).
///
/// All methods of ArrayOfViewsHelper are class (i.e., static)
/// methods. It doesn't make sense to make an instance of
/// ArrayOfViewsHelper. We forbid this syntactically by declaring
/// the (unimplemented) constructor private.
template <class Node>
class ArrayOfViewsHelper {
public:
/// \brief Invoke the Node's viewBufferNonConst() method to get an array of views.
///
/// \param node [in/out] The Kokkos Node instance.
///
/// \param rw [in] Whether to create read-and-write views, or
/// write-only views. Read-and-write views may be safely read
/// before they are written. Write-only views have undefined
/// contents before they are written. In both cases, views are
/// to be read and written on the host. If the device has a
/// separate memory space, then each view is copied back to the
/// device (namely, to their corresponding buffers in
/// arrayOfBuffers) once its reference count falls to zero.
///
/// \param arrayOfBuffers [in/out] The array of buffers for which
/// to create views.
template <class T>
static ArrayRCP<ArrayRCP<T> >
getArrayOfNonConstViews (const RCP<Node> &node,
ReadWriteOption rw,
const ArrayRCP<ArrayRCP<T> > &arrayOfBuffers);
private:
//! Constructor is private and undeclared, so you can't call it.
ArrayOfViewsHelper();
};
template <class Node>
template <class T>
ArrayRCP<ArrayRCP<T> >
ArrayOfViewsHelper<Node>::getArrayOfNonConstViews(const RCP<Node> &node, ReadWriteOption rw, const ArrayRCP<ArrayRCP<T> > &arrayOfBuffers) {
ArrayRCP< ArrayRCP<T> > arrayofviews;
const size_t numBufs = arrayOfBuffers.size();
if (numBufs > 0) {
arrayofviews = arcp< ArrayRCP<T> >(numBufs);
for (size_t i=0; i < numBufs; ++i) {
if (arrayOfBuffers[i].size() > 0) {
arrayofviews[i] = node->template viewBufferNonConst<T>(rw,arrayOfBuffers[i].size(),arrayOfBuffers[i]);
}
}
}
return arrayofviews;
}
//! A trivial implementation of ArrayOfViewsHelper, for CPU-only nodes.
template <class Node>
class ArrayOfViewsHelperTrivialImpl {
public:
template <class T>
static ArrayRCP<ArrayRCP<T> > getArrayOfNonConstViews(const RCP<Node> &node, ReadWriteOption rw, const ArrayRCP<ArrayRCP<T> > &arrayOfBuffers);
private:
/*! Cannot allocate object; all static */
ArrayOfViewsHelperTrivialImpl();
~ArrayOfViewsHelperTrivialImpl();
};
template <class Node>
template <class T>
ArrayRCP<ArrayRCP<T> >
ArrayOfViewsHelperTrivialImpl<Node>::getArrayOfNonConstViews(const RCP<Node> &node, ReadWriteOption rw, const ArrayRCP<ArrayRCP<T> > &arrayOfBuffers) {
(void)node;
(void)rw;
return arrayOfBuffers;
}
} // end of namespace KokkosClassic
#endif
|