Template Numerical Library: TNL::File Class Reference

Template Numerical Library version\ main:0b2c40fb1

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:
[legend]

## Public Member Functions
| File ()=default
| Basic constructor.
| File (const File &)=delete
| File (const std::string &fileName, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
| Constructor which opens given file.
| File (File &&) noexcept=default
void | close ()
| Closes the file.
const std::string & | getFileName () 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 *destination, std::streamsize elements=1)
| Method for loading data from the file.
void | open (const std::string &fileName, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
| Open given file.
File & | operator= (const File &)=delete
File & | operator= (File &&) noexcept=default
template<typename Type, typename TargetType = Type, typename Allocator = Allocators::Host< Type >>
void | save (const Type *source, 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< ! allocates_host_accessible_data_v< Allocator > >, typename = void>
void | load_impl (Type *destination, std::streamsize elements)
template<typename Type, typename SourceType, typename Allocator, typename = std::enable_if_t< allocates_host_accessible_data_v< Allocator > >>
void | load_impl (Type *destination, std::streamsize elements)
template<typename Type, typename TargetType, typename Allocator, typename = std::enable_if_t< ! allocates_host_accessible_data_v< Allocator > >, typename = void>
void | save_impl (const Type *source, std::streamsize elements)
template<typename Type, typename TargetType, typename Allocator, typename = std::enable_if_t< allocates_host_accessible_data_v< Allocator > >>
void | save_impl (const Type *source, std::streamsize elements)

## Protected Attributes
std::fstream | file
std::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 <string>
  #include <TNL/File.h>
  using namespace TNL;
  int
  main()
  {
  File file;
  file.open( std::string( "new-file.tnl" ), std::ios_base::out );
  std::string title( "'string to file'" );
  file << title;
  file.close();
  file.open( std::string( "new-file.tnl" ), std::ios_base::in );
  std::string restoredString;
  file >> restoredString;
  file.close();
  std::cout << "restored string = " << restoredString << '\n';
  }
  std::cout
  std::string
  TNL::File::File
  File()=default
  Basic constructor.
  TNL
  The main TNL namespace.
  Definition AtomicOperations.h:15

Output
  restored string = 'string to file'

## Constructor & Destructor Documentation

## ◆ File()

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

Constructor which opens given file.

All parameters are passed to the open method.

## Member Function Documentation

## ◆ close()

void TNL::File::close | ( |  | ) |

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

  SourceType | type of data stored on the file,

Parameters

  elements | number of elements to be read and ignored.

## ◆ load()

template<typename Type, typename SourceType = Type, typename Allocator = Allocators::Host< Type >>

void TNL::File::load | ( | Type * | destination,
|  | 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

  Type | type of data to be loaded to the destination.
  SourceType | type of data stored on the file,
  Allocator | type of the allocator which was used to allocate destination.

Parameters

  destination | Pointer in memory where the elements are loaded and stored after reading.
  elements | number 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 <TNL/Allocators/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;
  double* 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 ] << '\n';
  /***
  * Free allocated memory
  */
  cudaFree( deviceArray );
  delete[] hostArray;
  }
  TNL::File
  This class serves for binary IO. It allows to do IO even for data allocated on GPU together with on-t...
  Definition File.h:25
  TNL::File::load
  void load(Type *destination, std::streamsize elements=1)
  Method for loading data from the file.
  TNL::File::save
  void save(const Type *source, std::streamsize elements=1)
  Method for saving data to the file.
  TNL::File::close
  void close()
  Closes the file.
  TNL::File::open
  void open(const std::string &fileName, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
  Open given file.
  TNL::Allocators::Host
  std::allocator< T > Host
  Allocator for the host memory space – alias for std::allocator.
  Definition Host.h:21
  TNL::Allocators::Cuda
  Allocator for the CUDA device memory space.
  Definition Cuda.h:21

Output

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 ] << '\n';
  }

Output
  3.14159265358979 -- 3.14159274101257 -- 3
  2.71828182845905 -- 2.71828174591064 -- 2
  1.61803398874989 -- 1.6180340051651 -- 1

## ◆ open()

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

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

  fileName | String which indicates file name.
  mode | Indicates in what mode the file will be opened - see std::ios_base::openmode.

## ◆ save()

template<typename Type, typename TargetType = Type, typename Allocator = Allocators::Host< Type >>

void TNL::File::save | ( | const Type * | source,
|  | 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

  Type | type of data in the source.
  TargetType | tells as what type data the buffer shall be saved.
  Allocator | type of the allocator which was used to allocate source.
  Index | type of index by which the elements are indexed.

Parameters

  source | buffer that is going to be saved to the file.
  elements | number of elements saved to the file.

See File::load for examples.

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

- src/TNL/File.h