YAKL
Public Member Functions | List of all members
yakl::ScalarLiveOut< T > Class Template Reference

Class to handle scalars that exist before kernels, are written to by kernels, and read after the kernel terminates. More...

#include <YAKL_ScalarLiveOut.h>

Public Member Functions

YAKL_INLINE ScalarLiveOut ()
 Default constructor allocates room on the device for one scalar of type T More...
 
YAKL_INLINE ScalarLiveOut (ScalarLiveOut &&rhs)
 Copies and moves are shallow, not deep copy. More...
 
YAKL_INLINE ScalarLiveOut (ScalarLiveOut const &rhs)
 Copies and moves are shallow, not deep copy. More...
 
 ScalarLiveOut (T val, Stream stream=Stream())
 [ASYNCHRONOUS] This constructor allocates room on the device for one scalar of type T and initializes it on device with the provided value. More...
 
YAKL_INLINE ~ScalarLiveOut ()
 Deallocates the scalar value on the device. More...
 
YAKL_INLINEget () const
 Returns a modifiable reference to the underlying data on the device. More...
 
hostRead (Stream stream=Stream()) const
 Returns a host copy of the data. This is blocking. More...
 
void hostWrite (T val, Stream stream=Stream())
 [ASYNCHRONOUS] Writes a value to the device-resident underlying data More...
 
YAKL_INLINE T & operator() () const
 Returns a modifiable reference to the underlying data on the device. More...
 
YAKL_INLINE ScalarLiveOutoperator= (ScalarLiveOut &&rhs)
 Copies and moves are shallow, not deep copy. More...
 
YAKL_INLINE ScalarLiveOutoperator= (ScalarLiveOut const &rhs)
 Copies and moves are shallow, not deep copy. More...
 
template<class TLOC , typename std::enable_if< std::is_arithmetic< TLOC >::value, int >::type = 0>
YAKL_INLINE T & operator= (TLOC rhs) const
 Assign a value to the ScalarLiveOut object on the device. More...
 

Detailed Description

template<class T>
class yakl::ScalarLiveOut< T >

Class to handle scalars that exist before kernels, are written to by kernels, and read after the kernel terminates.

Handles the case where a scalar value is written to in a kernel and must be read on the host after the kernel completes. Passing a value to the constructor will initialize the device pointer with that data from the host. Using the = operator will assign to the value inside a kernel. This is the most common operation. To access the value as a reference that is writable, use the operator() (e.g., in an atomic operation perhaps). To read on the host afterward, using hostRead() To write after construction, use hostWrite()

ScalarLiveOut<float> sum(0.);
// You'd use a reduction for this in reality. This is just for demonstration.
parallel_for( Bounds<2>(ny,nx) , YAKL_LAMBDA (int j, int i) {
// sum() obtains a modifiable reference to the data contained by the ScalarLiveOut object
atomicAdd( sum() , 1. );
});
if (sum.hostRead() != ny*nx) yakl::yakl_throw("ERROR: Wrong sum");
ScalarLiveOut<bool> data_is_bad(false);
parallel_for( Bounds<2>(ny,nx) , YAKL_LAMBDA (int j, int i) {
// operator= always assigns on the device
if (density(j,i) < 0) data_is_bad = true;
});
if (data_is_bad.hostRead()) yakl::yakl_throw("ERROR: Data is bad");
Parameters
TThe type of the scalar value

Constructor & Destructor Documentation

◆ ScalarLiveOut() [1/4]

template<class T >
YAKL_INLINE yakl::ScalarLiveOut< T >::ScalarLiveOut ( )
inline

Default constructor allocates room on the device for one scalar of type T

◆ ScalarLiveOut() [2/4]

template<class T >
yakl::ScalarLiveOut< T >::ScalarLiveOut ( val,
Stream  stream = Stream() 
)
inlineexplicit

[ASYNCHRONOUS] This constructor allocates room on the device for one scalar of type T and initializes it on device with the provided value.

◆ ~ScalarLiveOut()

template<class T >
YAKL_INLINE yakl::ScalarLiveOut< T >::~ScalarLiveOut ( )
inline

Deallocates the scalar value on the device.

◆ ScalarLiveOut() [3/4]

template<class T >
YAKL_INLINE yakl::ScalarLiveOut< T >::ScalarLiveOut ( ScalarLiveOut< T > const &  rhs)
inline

Copies and moves are shallow, not deep copy.

◆ ScalarLiveOut() [4/4]

template<class T >
YAKL_INLINE yakl::ScalarLiveOut< T >::ScalarLiveOut ( ScalarLiveOut< T > &&  rhs)
inline

Copies and moves are shallow, not deep copy.

Member Function Documentation

◆ get()

template<class T >
YAKL_INLINE T yakl::ScalarLiveOut< T >::get ( ) const
inline

Returns a modifiable reference to the underlying data on the device.

◆ hostRead()

template<class T >
T yakl::ScalarLiveOut< T >::hostRead ( Stream  stream = Stream()) const
inline

Returns a host copy of the data. This is blocking.

◆ hostWrite()

template<class T >
void yakl::ScalarLiveOut< T >::hostWrite ( val,
Stream  stream = Stream() 
)
inline

[ASYNCHRONOUS] Writes a value to the device-resident underlying data

◆ operator()()

template<class T >
YAKL_INLINE T& yakl::ScalarLiveOut< T >::operator() ( ) const
inline

Returns a modifiable reference to the underlying data on the device.

◆ operator=() [1/3]

template<class T >
YAKL_INLINE ScalarLiveOut& yakl::ScalarLiveOut< T >::operator= ( ScalarLiveOut< T > &&  rhs)
inline

Copies and moves are shallow, not deep copy.

◆ operator=() [2/3]

template<class T >
YAKL_INLINE ScalarLiveOut& yakl::ScalarLiveOut< T >::operator= ( ScalarLiveOut< T > const &  rhs)
inline

Copies and moves are shallow, not deep copy.

◆ operator=() [3/3]

template<class T >
template<class TLOC , typename std::enable_if< std::is_arithmetic< TLOC >::value, int >::type = 0>
YAKL_INLINE T& yakl::ScalarLiveOut< T >::operator= ( TLOC  rhs) const
inline

Assign a value to the ScalarLiveOut object on the device.


The documentation for this class was generated from the following file:
yakl::c::parallel_for
void parallel_for(char const *str, Bounds< N, simple > const &bounds, F const &f, LaunchConfig< VecLen, B4B > config=LaunchConfig<>())
[ASYNCHRONOUS] Launch the passed functor in parallel.
yakl::atomicAdd
YAKL_INLINE void atomicAdd(T &update, T value)
[NON_B4B] yakl::atomicAdd(update,value) atomically performs update += value)
Definition: YAKL_atomics.h:321
yakl::intrinsics::sum
T sum(Array< T, rank, memHost, myStyle > const &arr)
Definition: YAKL_intrinsics_sum.h:10
yakl::yakl_throw
YAKL_INLINE void yakl_throw(const char *msg)
Throw an error message. Works from the host or device.
Definition: YAKL_error.h:17
YAKL_LAMBDA
#define YAKL_LAMBDA
Used to create C++ lambda expressions passed to parallel_for and parallel_outer
Definition: YAKL_defines.h:128