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

Array revision - assignement operator modified to accept arrays and non-array types.

parent 402faa22
Loading
Loading
Loading
Loading

src/Examples/ArrayExample.cpp

deleted100644 → 0
+0 −27
Original line number Diff line number Diff line
#include <iostream>
#include <TNL/Containers/Array.h>

using namespace TNL;
using namespace std;

int main()
{
    Containers::Array<int> array1;
    array1.setSize(5);
    array1.setValue(0);
    cout << "Does array contain 1?" << array1.containsValue(1) << endl;
    cout << "Does array contain only zeros?" << array1.containsOnlyValue(0) << endl;

    Containers::Array<int> array2(3);
    array2.setValue(1);
    array2.swap(array1);
    array2.setElement(2,4);

    cout << "First array:" << array1.getData() << endl;
    cout << "Second array:" << array2.getData() << endl;

    array2.reset();
    cout << "Second array after reset:" << array2.getData() << endl;

    // bind
}
+4 −11
Original line number Diff line number Diff line
ADD_SUBDIRECTORY( Containers )

add_subdirectory( simple-examples )
add_subdirectory( heat-equation )
add_subdirectory( transport-equation )
@@ -10,15 +12,6 @@ add_subdirectory( flow )
add_subdirectory( flow-sw )
add_subdirectory( flow-vl )

#add_subdirectory( mean-curvature-flow )
#add_subdirectory( hamilton-jacobi )
#add_subdirectory( hamilton-jacobi-parallel )
#add_subdirectory( fast-sweeping )
#add_subdirectory( hamilton-jacobi-parallel-map )
#add_subdirectory( fast-sweeping-map )
#add_subdirectory( narrow-band )

ADD_EXECUTABLE( ArrayExample ArrayExample.cpp )

ADD_EXECUTABLE( ConfigDescriptionExample ConfigDescriptionExample.cpp )

@@ -81,7 +74,7 @@ ADD_CUSTOM_COMMAND( COMMAND TimerExampleLogger > TimerExampleLogger.out OUTPUT T

ADD_EXECUTABLE( VectorExample VectorExample.cpp )

ADD_CUSTOM_TARGET( run ALL DEPENDS
ADD_CUSTOM_TARGET( RunExamples ALL DEPENDS
   FileExample.out
   FileExampleSaveAndLoad.out
   FileNameExample.out
@@ -98,6 +91,6 @@ ADD_CUSTOM_TARGET( run ALL DEPENDS
   TimerExampleLogger.out )

if( BUILD_CUDA )
   ADD_CUSTOM_TARGET( run-cuda ALL DEPENDS
   ADD_CUSTOM_TARGET( RunExamples-cuda ALL DEPENDS
      FileExampleCuda.out )
ENDIF()
 No newline at end of file
+75 −0
Original line number Diff line number Diff line
#include <iostream>
#include <TNL/Containers/Array.h>

using namespace TNL;
using namespace std;

/***
 * The following works for any device (CPU, GPU ...).
 */
template< typename Device >
void arrayExample()
{
   const int size = 10;
   using ArrayType = Containers::Array< int, Device >;
   using IndexType = typename ArrayType::IndexType;
   ArrayType a1( size ), a2( size );

   /***
    * You may initiate the array using setElement
    */
   for( int i = 0; i< size; i++ )
      a1.setElement( i, i );

   /***
    * You may also assign value to all array elements
    */
   a2 = 0;

   /***
    * Simple array values checks can be done as follows ...
    */
   if( a1.containsValue( 1 ) )
      std::cout << "a1 contains value 1." << std::endl;
   if( a1.containsValue( size ) )
      std::cout << "a1 contains value " << size << "." << std::endl;
   if( a1.containsOnlyValue( 0 ) )
      std::cout << "a2 contains only value 0." << std::endl;

   /***
    * More efficient way of array elements manipulation is with the lambda functions
    */
   ArrayType a3( size );
   auto f1 = [] __cuda_callable__ ( IndexType i ) -> int { return 2 * i;};
   a3.evaluate( f1 );

   for( int i = 0; i < size; i++ )
      if( a3.getElement( i ) != 2 * i )
         std::cerr << "Something is wrong!!!" << std::endl;

   /***
    * You may swap array data with the swap method.
    */
   a1.swap( a3 );

   /***
    * Of course, you may save it to file and load again
    */
   a1.save( "a1.tnl" );
   a2.load( "a1.tnl" );

   if( a2 != a1 )
      std::cerr << "Something is wrong!!!" << std::endl;

   std::cout << "a2 = " << a2 << std::endl;
}

int main()
{
   std::cout << "The first test runs on CPU ..." << std::endl;
   arrayExample< Devices::Host >();
#ifdef HAVE_CUDA
   std::cout << "The second test runs on GPU ..." << std::endl;
   arrayExample< Devices::Cuda >();
#endif
}
+1 −0
Original line number Diff line number Diff line
ArrayExample.cpp
 No newline at end of file
+15 −0
Original line number Diff line number Diff line
IF( BUILD_CUDA )
   CUDA_ADD_EXECUTABLE( ArrayExampleCuda ArrayExample.cu )
   ADD_CUSTOM_COMMAND( COMMAND ArrayExampleCuda > ArrayExampleCuda.out OUTPUT ArrayExampleCuda.out )
ELSE()
   ADD_EXECUTABLE( ArrayExample ArrayExample.cpp )
   ADD_CUSTOM_COMMAND( COMMAND ArrayExample > ArrayExample.out OUTPUT ArrayExample.out )
ENDIF()

IF( BUILD_CUDA )
ADD_CUSTOM_TARGET( RunContainersExamples-cuda ALL DEPENDS
   ArrayExampleCuda.out )
ELSE()
ADD_CUSTOM_TARGET( RunContainersExamples ALL DEPENDS
   ArrayExample.out )
ENDIF()
Loading