Commit bf7b251a authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Added reduceElements and reduceEachElement to Array and ArrayView.

parent cb4c047e
Loading
Loading
Loading
Loading
+44 −0
Original line number Diff line number Diff line
#include <iostream>
#include <TNL/Containers/Array.h>
#include <TNL/Containers/ArrayView.h>

using namespace TNL;

template< typename Device >
void forElementsExample()
{
   /****
    * Create new arrays
    */
   const int size = 10;
   Containers::Array< float, Device > a( size ), b( size );
   b = 0;

   /****
    * Initiate the elements of array `a`
    */
   a.forEachElement( [] __cuda_callable__ ( int i, float& value ) { value = i; } );

   /****
    * Initiate elements of array `b` with indexes 0-4 using `a_view`
    */
   auto a_view = a.getView();
   b.forElements( 0, 5, [=] __cuda_callable__ ( int i, float& value ) { value = a_view[ i ] + 4.0; } );

   /****
    * Print the results
    */
   std::cout << " a = " << a << std::endl;
   std::cout << " b = " << b << std::endl;
}

int main( int argc, char* argv[] )
{
   std::cout << "Running example on the host system: " << std::endl;
   forElementsExample< Devices::Host >();

#ifdef HAVE_CUDA
   std::cout << "Running example on the CUDA device: " << std::endl;
   forElementsExample< Devices::Cuda >();
#endif
}
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
ArrayExample_forElements.cpp
 No newline at end of file
+44 −0
Original line number Diff line number Diff line
#include <iostream>
#include <functional>
#include <TNL/Containers/Array.h>
#include <TNL/Containers/ArrayView.h>

using namespace TNL;

template< typename Device >
void reduceElementsExample()
{
   /****
    * Create new arrays
    */
   const int size = 10;
   Containers::Array< float, Device > a( size );

   /****
    * Initiate the elements of array `a`
    */
   a.forEachElement( [] __cuda_callable__ ( int i, float& value ) { value = i; } );

   /****
    * Sum all elements of array `a`
    */
   auto fetch = [=] __cuda_callable__ ( int i, float& value ) { return value; };
   auto sum = a.reduceEachElement( fetch, std::plus<>{}, 0.0 );

   /****
    * Print the results
    */
   std::cout << " a = " << a << std::endl;
   std::cout << " sum = " << sum << std::endl;
}

int main( int argc, char* argv[] )
{
   std::cout << "Running example on the host system: " << std::endl;
   reduceElementsExample< Devices::Host >();

#ifdef HAVE_CUDA
   std::cout << "Running example on the CUDA device: " << std::endl;
   reduceElementsExample< Devices::Cuda >();
#endif
}
+1 −0
Original line number Diff line number Diff line
ArrayExample_reduceElements.cpp
 No newline at end of file
+44 −0
Original line number Diff line number Diff line
#include <iostream>
#include <TNL/Containers/Array.h>
#include <TNL/Containers/ArrayView.h>

using namespace TNL;

template< typename Device >
void forElementsExample()
{
   /****
    * Create new arrays
    */
   const int size = 10;
   Containers::Array< float, Device > a( size ), b( size );
   b = 0;

   /****
    * Create an ArrayView and use it for initiation of elements of array `a`
    */
   auto a_view = a.getView();
   a_view.forEachElement( [] __cuda_callable__ ( int i, float& value ) { value = i; } );

   /****
    * Initiate elements of array `b` with indexes 0-4 using `a_view`
    */
   b.getView().forElements( 0, 5, [=] __cuda_callable__ ( int i, float& value ) { value = a_view[ i ] + 4.0; } );

   /****
    * Print the results
    */
   std::cout << " a = " << a << std::endl;
   std::cout << " b = " << b << std::endl;
}

int main( int argc, char* argv[] )
{
   std::cout << "Running example on the host system: " << std::endl;
   forElementsExample< Devices::Host >();

#ifdef HAVE_CUDA
   std::cout << "Running example on the CUDA device: " << std::endl;
   forElementsExample< Devices::Cuda >();
#endif
}
 No newline at end of file
Loading