YAKL
YAKL_intrinsics_sign.h
Go to the documentation of this file.
1 
2 #pragma once
3 // Included by YAKL_intrinsics.h
4 
6 namespace yakl {
7  namespace intrinsics {
8 
9  template <class T1, class T2>
10  YAKL_INLINE T1 sign(T1 a, T2 b) { return b >= 0 ? std::abs(a) : -std::abs(a); }
11 
12  template <class T1, class T2, int rank, int myStyle>
14  Array<T2,rank,memHost,myStyle> const & b ) {
15  #ifdef YAKL_DEBUG
16  using yakl::componentwise::operator==;
17  using yakl::componentwise::operator!;
18  if (!allocated(a)) yakl_throw("ERROR: Calling sign with unallocated a");
19  if (!allocated(b)) yakl_throw("ERROR: Calling sign with unallocated b");
20  if (any(!(shape(a) == shape(b)))) yakl_throw("ERROR: Calling sign with differently arrays");
21  #endif
22  auto ret = a.createHostObject();
23  for( int i=0; i < a.totElems(); i++) {
24  ret.data()[i] = b.data()[i] >= 0 ? std::abs(a.data()[i]) : -std::abs(a.data()[i]);
25  }
26  return ret;
27  }
28 
29  template <class T1, class T2, int rank, int myStyle>
31  Array<T2,rank,memDevice,myStyle> const & b , Stream stream = Stream() ) {
32  #ifdef YAKL_DEBUG
33  using yakl::componentwise::operator==;
34  using yakl::componentwise::operator!;
35  if (!allocated(a)) yakl_throw("ERROR: Calling sign with unallocated a");
36  if (!allocated(b)) yakl_throw("ERROR: Calling sign with unallocated b");
37  if (any(!(shape(a) == shape(b)))) yakl_throw("ERROR: Calling sign with differently arrays");
38  #endif
39  auto ret = a.createDeviceObject();
40  parallel_for( "YAKL_internal_sign" , a.totElems() , YAKL_LAMBDA (int i) {
41  ret.data()[i] = b.data()[i] >= 0 ? std::abs(a.data()[i]) : -std::abs(a.data()[i]);
42  }, DefaultLaunchConfig().set_stream(stream) );
43  ret.set_stream_dependency(stream);
44  return ret;
45  }
46 
47  template <class T1, class T2, int rank, unsigned D0, unsigned D1, unsigned D2, unsigned D3>
49  SArray<T2,rank,D0,D1,D2,D3> const & b ) {
51  for( int i=0; i < a.totElems(); i++) {
52  ret.data()[i] = b.data()[i] >= 0 ? std::abs(a.data()[i]) : -std::abs(a.data()[i]);
53  }
54  return ret;
55  }
56 
57  template <class T1, class T2, int rank, class B0, class B1, class B2, class B3>
59  FSArray<T2,rank,B0,B1,B2,B3> const & b ) {
61  for( int i=0; i < a.totElems(); i++) {
62  ret.data()[i] = b.data()[i] >= 0 ? std::abs(a.data()[i]) : -std::abs(a.data()[i]);
63  }
64  return ret;
65  }
66 
67  }
68 }
70 
yakl::FSArray::totElems
static constexpr unsigned totElems()
Get the total number of array elements.
Definition: YAKL_FSArray.h:179
yakl::Stream
Implements the functionality of a stream for parallel kernel execution. If the Stream::create() metho...
Definition: YAKL_streams_events.h:394
yakl::intrinsics::shape
YAKL_INLINE auto shape(T const &arr)
Definition: YAKL_intrinsics_shape.h:9
yakl::intrinsics::allocated
YAKL_INLINE bool allocated(T const &arr)
Definition: YAKL_intrinsics_allocated.h:9
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::intrinsics::sign
YAKL_INLINE T1 sign(T1 a, T2 b)
Definition: YAKL_intrinsics_sign.h:10
__YAKL_NAMESPACE_WRAPPER_END__
#define __YAKL_NAMESPACE_WRAPPER_END__
Definition: YAKL.h:20
__YAKL_NAMESPACE_WRAPPER_BEGIN__
#define __YAKL_NAMESPACE_WRAPPER_BEGIN__
Definition: YAKL.h:19
yakl::DefaultLaunchConfig
LaunchConfig<> DefaultLaunchConfig
This launch configuration sets vector length to the device default and B4B to false.
Definition: YAKL_LaunchConfig.h:77
YAKL_INLINE
#define YAKL_INLINE
Used to decorate functions called from kernels (parallel_for and parallel_outer) or from CPU function...
Definition: YAKL_defines.h:140
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::Array
This declares the yakl::Array class. Please see the yakl::styleC and yakl::styleFortran template spec...
Definition: YAKL_Array.h:40
yakl::CSArray
C-style array on the stack similar in nature to, e.g., float arr[ny][nx];
Definition: YAKL_CSArray.h:30
yakl
yakl::CSArray::totElems
static constexpr unsigned totElems()
Get the total number of array elements.
Definition: YAKL_CSArray.h:131
yakl::simd::abs
YAKL_INLINE Pack< T, N > abs(Pack< T, N > a)
Definition: YAKL_simd.h:266
yakl::CSArray::data
YAKL_INLINE T * data() const
Get the underlying raw data pointer.
Definition: YAKL_CSArray.h:123
yakl::FSArray::data
YAKL_INLINE T * data() const
Get the underlying raw data pointer.
Definition: YAKL_FSArray.h:171
YAKL_LAMBDA
#define YAKL_LAMBDA
Used to create C++ lambda expressions passed to parallel_for and parallel_outer
Definition: YAKL_defines.h:128
yakl::FSArray
Fortran-style array on the stack similar in nature to, e.g., float arr[ny][nx];
Definition: YAKL_FSArray.h:53
yakl::intrinsics::any
bool any(Array< T, rank, memHost, myStyle > arr)
Definition: YAKL_intrinsics_any.h:10