Template Numerical Library version\ main:b780a9f
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
TNL::File Class Reference

This class serves for binary IO. It allows to do IO even for data allocated on GPU together with on-the-fly data type conversion. More...

#include <TNL/File.h>

Collaboration diagram for TNL::File:
Collaboration graph
[legend]

Public Member Functions

 File ()=default
 Basic constructor.
 
 File (const File &)=delete
 
 File (const String &fileName, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
 Constructor which opens given file.
 
 File (File &&)=default
 
void close ()
 Closes the file.
 
const StringgetFileName () const
 Returns name of the file.
 
template<typename SourceType >
void ignore (std::streamsize elements=1)
 Extracts and discards characters from the file.
 
template<typename Type , typename SourceType = Type, typename Allocator = Allocators::Host< Type >>
void load (Type *buffer, std::streamsize elements=1)
 Method for loading data from the file.
 
void open (const String &fileName, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
 Open given file.
 
Fileoperator= (const File &)=delete
 
Fileoperator= (File &&)=default
 
template<typename Type , typename TargetType = Type, typename Allocator = Allocators::Host< Type >>
void save (const Type *buffer, std::streamsize elements=1)
 Method for saving data to the file.
 

Protected Member Functions

template<typename Type , typename SourceType , typename Allocator , typename = std::enable_if_t< ! std::is_same< Allocator, Allocators::Cuda< Type > >::value >>
void load_impl (Type *buffer, std::streamsize elements)
 
template<typename Type , typename SourceType , typename Allocator , typename = std::enable_if_t< std::is_same< Allocator, Allocators::Cuda< Type > >::value >, typename = void>
void load_impl (Type *buffer, std::streamsize elements)
 
template<typename Type , typename TargetType , typename Allocator , typename = std::enable_if_t< ! std::is_same< Allocator, Allocators::Cuda< Type > >::value >>
void save_impl (const Type *buffer, std::streamsize elements)
 
template<typename Type , typename TargetType , typename Allocator , typename = std::enable_if_t< std::is_same< Allocator, Allocators::Cuda< Type > >::value >, typename = void>
void save_impl (const Type *buffer, std::streamsize elements)
 

Protected Attributes

std::fstream file
 
String fileName
 

Detailed Description

This class serves for binary IO. It allows to do IO even for data allocated on GPU together with on-the-fly data type conversion.

Example
#include <iostream>
#include <TNL/File.h>
#include <TNL/String.h>
using namespace TNL;
int main()
{
File file;
file.open( String("new-file.tnl"), std::ios_base::out );
String title("'string to file'");
file << title;
file.close();
file.open( String("new-file.tnl"), std::ios_base::in );
String restoredString;
file >> restoredString;
file.close();
std::cout << "restored string = " << restoredString << std::endl;
}
This class serves for binary IO. It allows to do IO even for data allocated on GPU together with on-t...
Definition: File.h:28
void open(const String &fileName, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
Open given file.
Definition: File.hpp:31
void close()
Closes the file.
Definition: File.hpp:57
Class for managing strings.
Definition: String.h:33
T endl(T... args)
The main TNL namespace.
Definition: AtomicOperations.h:13
Output
restored string = 'string to file'

Constructor & Destructor Documentation

◆ File()

TNL::File::File ( const String fileName,
std::ios_base::openmode  mode = std::ios_base::in | std::ios_base::out 
)
inline

Constructor which opens given file.

All parameters are passed to the open method.

Member Function Documentation

◆ close()

void TNL::File::close ( )
inline

Closes the file.

Throws std::ios_base::failure on failure.

◆ ignore()

template<typename SourceType >
void TNL::File::ignore ( std::streamsize  elements = 1)

Extracts and discards characters from the file.

Throws std::ios_base::failure on failure.

Template Parameters
SourceTypetype of data stored on the file,
Parameters
elementsnumber of elements to be read and ignored.

◆ load()

template<typename Type , typename SourceType , typename Allocator >
void TNL::File::load ( Type *  buffer,
std::streamsize  elements = 1 
)

Method for loading data from the file.

The data will be stored in buffer which was allocated using the allocator of type Allocator. The data type of the buffer is given by the template parameter Type. The second template parameter SourceType defines the type of data in the source file. If both types are different, on-the-fly conversion takes place during the data loading.

Throws std::ios_base::failure on failure.

Template Parameters
Typetype of data to be loaded to the buffer.
SourceTypetype of data stored on the file,
Allocatortype of the allocator which was used to allocate buffer.
Parameters
bufferPointer in memory where the elements are loaded and stored after reading.
elementsnumber of elements to be loaded from the file.

The following example shows how to load data directly to GPU.

Example
#include <iostream>
#include <TNL/File.h>
#include <cuda.h>
using namespace TNL;
int main()
{
const int size = 3;
double doubleArray[] = { 3.1415926535897932384626433,
2.7182818284590452353602874,
1.6180339887498948482045868 };
/***
* Save array to file.
*/
File file;
file.open( "file-example-cuda-test-file.tnl", std::ios_base::out | std::ios_base::trunc );
file.save< double, double, Allocators::Host< double > >( 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( "file-example-cuda-test-file.tnl", std::ios_base::in );
file.load< double, double, Allocators::Cuda< double > >( 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;
}
void save(const Type *buffer, std::streamsize elements=1)
Method for saving data to the file.
Definition: File.hpp:156
void load(Type *buffer, std::streamsize elements=1)
Method for loading data from the file.
Definition: File.hpp:76
Allocator for the CUDA device memory space.
Definition: Cuda.h:25
Output
3.14159265358979
2.71828182845905
1.61803398874989
The following example shows how to do on-the-fly data conversion.
Example
#include <iostream>
#include <TNL/File.h>
using namespace TNL;
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", std::ios_base::out | std::ios_base::trunc );
file.save< double, float >( doubleArray, size );
file.close();
/***
* Load the array of floats from the file.
*/
file.open( "test-file.tnl", std::ios_base::in );
file.load< float, float >( floatArray, size );
file.close();
/***
* Load the array of floats from the file and convert them to integers.
*/
file.open( "test-file.tnl", std::ios_base::in );
file.load< int, float >( 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;
}
Output
3.14159265358979 -- 3.14159274101257 -- 3
2.71828182845905 -- 2.71828174591064 -- 2
1.61803398874989 -- 1.6180340051651 -- 1

◆ open()

void TNL::File::open ( const String fileName,
std::ios_base::openmode  mode = std::ios_base::in | std::ios_base::out 
)
inline

Open given file.

Opens file with given fileName in some mode from std::ios_base::openmode. Note that the file is always opened in binary mode, i.e. std::ios_base::binary is always added to mode.

Throws std::ios_base::failure on failure.

Parameters
fileNameString which indicates file name.
modeIndicates in what mode the file will be opened - see std::ios_base::openmode.

◆ save()

template<typename Type , typename TargetType , typename Allocator >
void TNL::File::save ( const Type *  buffer,
std::streamsize  elements = 1 
)

Method for saving data to the file.

The data from the buffer (with type Type) which was allocated using an allocator of type Allocator. TargetType defines as what data type the buffer shall be saved. If the type is different from the data type, on-the-fly data type conversion takes place during the data saving.

Throws std::ios_base::failure on failure.

Template Parameters
Typetype of data in the buffer.
TargetTypetells as what type data the buffer shall be saved.
Allocatortype of the allocator which was used to allocate buffer.
Indextype of index by which the elements are indexed.
Parameters
bufferbuffer that is going to be saved to the file.
elementsnumber of elements saved to the file.

See File::load for examples.


The documentation for this class was generated from the following files: