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

Added and updated File examples.

parent 6f411470
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -21,7 +21,19 @@ add_subdirectory( flow-vl )
ADD_EXECUTABLE( ArrayExample ArrayExample.cpp )

ADD_EXECUTABLE( ConfigDescriptionExample ConfigDescriptionExample.cpp )

ADD_EXECUTABLE( FileExample FileExample.cpp )
ADD_CUSTOM_COMMAND( COMMAND FileExample > FileExample.out OUTPUT FileExample.out )

IF( BUILD_CUDA )
   CUDA_ADD_EXECUTABLE(FileExampleCuda FileExampleCuda.cu)
   ADD_CUSTOM_COMMAND( COMMAND FileExampleCuda > FileExampleCuda.out OUTPUT FileExampleCuda.out )
ENDIF()

ADD_EXECUTABLE( FileExampleSaveAndLoad FileExampleSaveAndLoad.cpp )
ADD_CUSTOM_COMMAND( COMMAND FileExampleSaveAndLoad > FileExampleSaveAndLoad.out OUTPUT FileExampleSaveAndLoad.out )


ADD_EXECUTABLE( ListExample ListExample.cpp )
ADD_EXECUTABLE( LoggerExample LoggerExample.cpp )
ADD_EXECUTABLE( MathExample MathExample.cpp )
@@ -61,6 +73,8 @@ ADD_CUSTOM_COMMAND( COMMAND TimerExampleLogger > TimerExampleLogger.out OUTPUT T
ADD_EXECUTABLE( VectorExample VectorExample.cpp )

ADD_CUSTOM_TARGET( run ALL DEPENDS
   FileExample.out
   FileExampleSaveAndLoad.out
   ObjectExample_getType.out
   ParseObjectTypeExample.out
   StringExample.out
@@ -70,3 +84,8 @@ ADD_CUSTOM_TARGET( run ALL DEPENDS
   StringExampleStrip.out
   TimerExample.out
   TimerExampleLogger.out )

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

using namespace TNL;
using namespace std;

int main()
{
   const int size = 3;
   double doubleArray[] = {  3.1415926535897932384626433,
                             2.7182818284590452353602874,
                             1.6180339887498948482045868 };

   /***
    * Save array to file.
    */
   File file;
   file.open( "test-file.tnl", File::Mode::Out | File::Mode::Truncate );
   file.save< double, double, Devices::Host >( doubleArray, size );
   file.close();

   /***
    * Allocate arrays on host and device
    */
   double *deviceArray, *hostArray;
   cudaMalloc( ( void** ) &deviceArray, size * sizeof( double ) );
   hostArray = new double[ 3 ];

   /***
    * Read array from the file to device
    */
   file.open( "test-file.tnl", File::Mode::In );
   file.load< double, double, Devices::Cuda >( deviceArray, size );
   file.close();

   /***
    * Copy array from device to host
    */
   cudaMemcpy( ( void* ) hostArray, ( const void* ) deviceArray, size * sizeof( double), cudaMemcpyDeviceToHost );

   /***
    * Print the array on host
    */
   std::cout.precision( 15 );
   for( int i = 0; i < size; i++ )
      std::cout << hostArray[ i ] << std::endl;

   /***
    * Free allocated memory
    */
   cudaFree( deviceArray );
   delete[] hostArray;
}


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

using namespace TNL;
using namespace std;

int main()
{
   const int size = 3;
   double doubleArray[] = {  3.1415926535897932384626433,
                             2.7182818284590452353602874,
                             1.6180339887498948482045868 };
   float floatArray[ 3 ];
   int intArray[ 3 ];

   /***
    * Save the array of doubles as floats.
    */
   File file;
   file.open( "test-file.tnl", File::Mode::Out | File::Mode::Truncate );
   file.save< double, float, Devices::Host >( doubleArray, size );
   file.close();

   /***
    * Load the array of floats from the file.
    */
   file.open( "test-file.tnl", File::Mode::In );
   file.load< float, float, Devices::Host >( floatArray, size );
   file.close();

   /***
    * Load the array of floats from the file and convert them to integers.
    */
   file.open( "test-file.tnl", File::Mode::In );
   file.load< int, float, Devices::Host >( intArray, size );
   file.close();

   /***
    * Print all arrays.
    */
   std::cout.precision( 15 );
   for( int i = 0; i < size; i++ )
      std::cout << doubleArray[ i ] << " -- "
                << floatArray[ i ] << " -- "
                << intArray[ i ] << std::endl;
}

+4 −4
Original line number Diff line number Diff line
@@ -46,12 +46,12 @@ int main( int argc, char* argv[] )
   if( string1 )
      cout << "string1 is not empty" << endl;

   /*File myFile;
   myFile.open( "string_save.out", File::out );
   File myFile;
   myFile.open( "string_save.out", File::Mode::Out );
   myFile << string1;
   myFile.close();

   myFile.open( "string_save.out", File::in );
   myFile.open( "string_save.out", File::Mode::In );
   myFile >> string3;
   cout << "string 3 after loading = " << string3 << endl;*/
   cout << "string 3 after loading = " << string3 << endl;
}
Loading