YAKL
YAKL_intrinsics_maxloc.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 T, int myStyle>
10  inline int maxloc( Array<T,1,memHost,myStyle> const &arr ) {
11  #ifdef YAKL_DEBUG
12  if (! allocated(arr)) yakl_throw("ERROR: calling maxloc on an unallocated array");
13  #endif
14  T mv = maxval(arr);
15  if constexpr (myStyle == styleC) {
16  for (int i=0; i < arr.totElems(); i++) { if (arr(i) == mv) return i; }
17  } else {
18  for (int i=lbound(arr,1); i <= ubound(arr,1); i++) { if (arr(i) == mv) return i; }
19  }
20  return -1;
21  }
22 
23  template <class T>
24  inline int maxloc( Array<T,1,memDevice,styleC> const &arr , Stream stream = Stream() ) {
25  #ifdef YAKL_DEBUG
26  if (! allocated(arr)) yakl_throw("ERROR: calling maxloc on an unallocated array");
27  #endif
28  T mv = maxval(arr,stream);
29  #ifdef YAKL_B4B
30  for (int i=0; i < arr.totElems(); i++) { if (arr(i) == mv) return i; }
31  return -1;
32  #else
33  ScalarLiveOut<int> ind(0,stream);
34  c::parallel_for( "YAKL_internal_maxloc" , arr.totElems() , YAKL_LAMBDA (int i) { if (arr(i) == mv) ind = i; },
35  DefaultLaunchConfig().set_stream(stream) );
36  return ind.hostRead(stream);
37  #endif
38  }
39 
40  template <class T>
41  inline int maxloc( Array<T,1,memDevice,styleFortran> const &arr , Stream stream = Stream() ) {
42  #ifdef YAKL_DEBUG
43  if (! allocated(arr)) yakl_throw("ERROR: calling maxloc on an unallocated array");
44  #endif
45  T mv = maxval(arr,stream);
46  #ifdef YAKL_B4B
47  for (int i=lbound(arr,1); i <= ubound(arr,1); i++) { if (arr(i) == mv) return i; }
48  return -1;
49  #else
50  ScalarLiveOut<int> ind(lbound(arr,1),stream);
51  fortran::parallel_for( "YAKL_internal_maxloc" , {lbound(arr,1),ubound(arr,1)} , YAKL_LAMBDA (int i) { if (arr(i) == mv) ind = i; },
52  DefaultLaunchConfig().set_stream(stream) );
53  return ind.hostRead(stream);
54  #endif
55  }
56 
57  template <class T, class D0>
58  YAKL_INLINE int maxloc( FSArray<T,1,D0> const &arr ) {
59  T m = arr.data()[0];
60  int loc = lbound(arr,1);
61  for (int i=lbound(arr,1); i<=ubound(arr,1); i++) {
62  if (arr(i) > m) {
63  m = arr(i);
64  loc = i;
65  }
66  }
67  return loc;
68  }
69 
70  template <class T, unsigned D0>
71  YAKL_INLINE int maxloc( SArray<T,1,D0> const &arr ) {
72  T m = arr.data()[0];
73  int loc = 0;
74  for (int i=1; i<arr.get_dimensions()(0); i++) {
75  if (arr(i) > m) {
76  m = arr(i);
77  loc = i;
78  }
79  }
80  return loc;
81  }
82 
83  }
84 }
86 
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::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::ScalarLiveOut::hostRead
T hostRead(Stream stream=Stream()) const
Returns a host copy of the data. This is blocking.
Definition: YAKL_ScalarLiveOut.h:83
__YAKL_NAMESPACE_WRAPPER_END__
#define __YAKL_NAMESPACE_WRAPPER_END__
Definition: YAKL.h:20
yakl::fortran::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_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::intrinsics::ubound
YAKL_INLINE int ubound(T const &arr, int dim)
Definition: YAKL_intrinsics_ubound.h:9
yakl::ScalarLiveOut
Class to handle scalars that exist before kernels, are written to by kernels, and read after the kern...
Definition: YAKL_ScalarLiveOut.h:39
yakl::intrinsics::lbound
YAKL_INLINE int lbound(T const &arr, int dim)
Definition: YAKL_intrinsics_lbound.h:9
yakl::intrinsics::maxloc
int maxloc(Array< T, 1, memHost, myStyle > const &arr)
Definition: YAKL_intrinsics_maxloc.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::styleC
constexpr int styleC
Template parameter for yakl::Array that specifies it should follow C-style behavior.
Definition: YAKL_Array.h:20
yakl::intrinsics::maxval
T maxval(Array< T, rank, memHost, myStyle > const &arr)
Definition: YAKL_intrinsics_maxval.h:10
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::get_dimensions
YAKL_INLINE CSArray< uint, 1, rank > get_dimensions() const
Returns the dimensions of this array as a yakl::SArray object.
Definition: YAKL_CSArray.h:157
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