Commit c1780697 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Moved implementations of scan and distributed scan into the detail namespace

The algorithms are supposed to be used via overloaded plain functions in
the Algorithms namespace: for now, there are only inplaceInclusiveScan
and inplaceExclusiveScan (and their distributed variant).

The scan and segmentedScan methods were removed from data structures
(Vector, VectorView, DistributedVector, DistributedVectorView). They
were inflexible (only std::plus was actually used for reduction),
incomplete (some overloads just threw NotImplementedError), and they
were violating the open-closed principle:
https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle
parent 624e709f
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -12,8 +12,8 @@ set( COMMON_EXAMPLES
     MapReduceExample-3
     ReductionWithArgument
     ReductionWithArgumentWithFunctional
     ScanExample
     ExclusiveScanExample
     inplaceInclusiveScanExample
     inplaceExclusiveScanExample
     SegmentedScanExample
)

+0 −48
Original line number Diff line number Diff line
#include <iostream>
#include <cstdlib>
#include <TNL/Containers/Vector.h>

using namespace TNL;
using namespace TNL::Containers;
using namespace TNL::Algorithms;

template< typename Device >
void scan( Vector< double, Device >& v )
{
   /***
    * Reduction is sum of two numbers.
    */
   auto reduce = [] __cuda_callable__ ( const double& a, const double& b ) { return a + b; };

   /***
    * As parameters, we pass vector on which the scan is to be performed, interval
    * where the scan is performed, lambda function which is used by the scan and
    * zero element (idempotent) of the 'sum' operation.
    */
   Scan< Device, ScanType::Exclusive >::perform( v, 0, v.getSize(), reduce, 0.0 );
}

int main( int argc, char* argv[] )
{
   /***
    * Firstly, test the exclusive prefix sum with vectors allocated on CPU.
    */
   Vector< double, Devices::Host > host_v( 10 );
   host_v = 1.0;
   std::cout << "host_v = " << host_v << std::endl;
   scan( host_v );
   std::cout << "The exclusive prefix sum of the host vector is " << host_v << "." << std::endl;

   /***
    * And then also on GPU.
    */
#ifdef HAVE_CUDA
   Vector< double, Devices::Cuda > cuda_v( 10 );
   cuda_v = 1.0;
   std::cout << "cuda_v = " << cuda_v << std::endl;
   scan( cuda_v );
   std::cout << "The exclusive prefix sum of the CUDA vector is " << cuda_v << "." << std::endl;
#endif
   return EXIT_SUCCESS;
}
+0 −1
Original line number Diff line number Diff line
ExclusiveScanExample.cpp
 No newline at end of file
+0 −47
Original line number Diff line number Diff line
#include <iostream>
#include <cstdlib>
#include <TNL/Containers/Vector.h>

using namespace TNL;
using namespace TNL::Containers;
using namespace TNL::Algorithms;

template< typename Device >
void scan( Vector< double, Device >& v )
{
   /***
    * Reduction is sum of two numbers.
    */
   auto reduction = [] __cuda_callable__ ( const double& a, const double& b ) { return a + b; };

   /***
    * As parameters, we pass vector on which the scan is to be performed, interval
    * where the scan is performed, lambda function which is used by the scan and
    * zero element (idempotent) of the 'sum' operation.
    */
   Scan< Device >::perform( v, 0, v.getSize(), reduction, 0.0 );
}

int main( int argc, char* argv[] )
{
   /***
    * Firstly, test the prefix sum with vectors allocated on CPU.
    */
   Vector< double, Devices::Host > host_v( 10 );
   host_v = 1.0;
   std::cout << "host_v = " << host_v << std::endl;
   scan( host_v );
   std::cout << "The prefix sum of the host vector is " << host_v << "." << std::endl;

   /***
    * And then also on GPU.
    */
#ifdef HAVE_CUDA
   Vector< double, Devices::Cuda > cuda_v( 10 );
   cuda_v = 1.0;
   std::cout << "cuda_v = " << cuda_v << std::endl;
   scan( cuda_v );
   std::cout << "The prefix sum of the CUDA vector is " << cuda_v << "." << std::endl;
#endif
   return EXIT_SUCCESS;
}
 No newline at end of file
+0 −1
Original line number Diff line number Diff line
ScanExample.cpp
 No newline at end of file
Loading