Skip to content
Snippets Groups Projects
Commit ab81368e authored by Tomáš Oberhuber's avatar Tomáš Oberhuber Committed by Tomáš Oberhuber
Browse files

Added and updated File examples.

parent 6f411470
No related branches found
No related tags found
1 merge request!29Revision
......@@ -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
FileExampleCuda.cu
\ No newline at end of file
#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;
}
#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;
}
......@@ -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;
}
......@@ -94,6 +94,19 @@ class File
* \tparam Device device where the data are stored after reading. For example \ref Devices::Host or \ref Devices::Cuda.
* \param buffer Pointer in memory where the elements are loaded and stored after reading.
* \param elements number of elements to be loaded from the file.
*
* The following example shows how to load data directly to GPU.
*
* \par Example
* \include FileExampleCuda.cpp
* \par Output
* \include FileExampleCuda.out
* The following example shows how to do on-the-fly data conversion.
*
* \par Example
* \include FileExampleSaveAndLoad.cpp
* \par Output
* \include FileExampleSaveAndLoad.out
*/
template< typename Type, typename SourceType = Type, typename Device = Devices::Host >
void load( Type* buffer, std::streamsize elements = 1 );
......@@ -115,6 +128,8 @@ class File
* \tparam Index type of index by which the elements are indexed.
* \param buffer buffer that is going to be saved to the file.
* \param elements number of elements saved to the file.
*
* See \ref File::load for examples.
*/
template< typename Type, typename TargetType = Type, typename Device = Devices::Host >
void save( const Type* buffer, std::streamsize elements = 1 );
......@@ -179,6 +194,7 @@ class File
*
* Finds out if the file \e fileName exists.
* \param fileName Name of the file to check.
* \return returns true if the file exists and false othervise
*/
bool fileExists( const String& fileName );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment