diff --git a/src/TNL/File.h b/src/TNL/File.h index 178ce95858a50954ea05c34649084dd368fc9243..9412c23cc2e16ed9435823684848702c9184fe99 100644 --- a/src/TNL/File.h +++ b/src/TNL/File.h @@ -28,7 +28,8 @@ enum class IOMode write = 2 }; -/* When we need to transfer data between the GPU and the CPU we use +/** + * When we need to transfer data between the GPU and the CPU we use * 5 MB buffer. This size should ensure good performance -- see. * http://wiki.accelereyes.com/wiki/index.php/GPU_Memory_Transfer * Similar constant is defined in tnlLonegVectorCUDA @@ -36,127 +37,154 @@ enum class IOMode static constexpr std::streamsize FileGPUvsCPUTransferBufferSize = 5 * 2<<20; -///\brief Class file is aimed mainly for saving and loading binary data. -/// -/// \par Example -/// \include FileExample.cpp -// \par Output -// \include FileExample.out +/** + * \brief Class file is aimed mainly for saving and loading binary data. + * + * \par Example + * \include FileExample.cpp + * \par Output + * \include FileExample.out + */ class File { - std::fstream file; - String fileName; - -public: - /// \brief Basic constructor. - File() = default; - - ///// - /// \brief Attempts to open given file and returns \e true after the file is - /// successfully opened. Otherwise returns \e false. - /// - /// Opens file with given \e fileName and returns true/false based on the success in opening the file. - /// \param fileName String which indicates name of the file user wants to open. - /// \param mode Indicates what user needs to do with opened file. - bool open( const String& fileName, - const IOMode mode ); - - /// \brief Attempts to close given file and returns \e true when the file is - /// successfully closed. Otherwise returns \e false. - bool close(); - - /// \brief Returns name of given file. - const String& getFileName() const - { - return this->fileName; - } - - /// \brief Method that can write particular data type from given file into GPU. (Function that gets particular elements from given file.) - /// - /// Returns \e true when the elements are successfully read from given file. Otherwise returns \e false. - /// - /// Throws \ref std::ios_base::failure on failure. - /// - /// \tparam Type Type of data. - /// \tparam Device Place where data are stored after reading from file. For example \ref Devices::Host or \ref Devices::Cuda. - /// \tparam Index Type of index by which the elements are indexed. - /// \param buffer Pointer in memory where the elements are loaded and stored after reading. - /// \param elements Number of elements the user wants to get (read) from given file. - template< typename Type, typename Device = Devices::Host > - bool read( Type* buffer, std::streamsize elements ); - - // Toto je treba?? - template< typename Type, typename Device = Devices::Host > - bool read( Type* buffer ); - - /// \brief Method that can write particular data type from CPU into given file. (Function that writes particular elements into given file.) - /// - /// Returns \e true when the elements are successfully written into given file. Otherwise returns \e false. - /// - /// Throws \ref std::ios_base::failure on failure. - /// - /// \tparam Type Type of data. - /// \tparam Device Place from where the data are loaded before writing into file. For example \ref Devices::Host or \ref Devices::Cuda. - /// \tparam Index Type of index by which the elements are indexed. - /// \param buffer Pointer in memory where the elements are loaded from before writing into file. - /// \param elements Number of elements the user wants to write into the given file. - template< typename Type, typename Device = Devices::Host > - bool write( const Type* buffer, std::streamsize elements ); - - // Toto je treba? - template< typename Type, typename Device = Devices::Host > - bool write( const Type* buffer ); - -protected: - template< typename Type, - typename Device, - typename = typename std::enable_if< std::is_same< Device, Devices::Host >::value >::type > - bool read_impl( Type* buffer, std::streamsize elements ); - - template< typename Type, - typename Device, - typename = typename std::enable_if< std::is_same< Device, Devices::Cuda >::value >::type, - typename = void > - bool read_impl( Type* buffer, std::streamsize elements ); - - template< typename Type, - typename Device, - typename = typename std::enable_if< std::is_same< Device, Devices::MIC >::value >::type, - typename = void, - typename = void > - bool read_impl( Type* buffer, std::streamsize elements ); - - template< typename Type, - typename Device, - typename = typename std::enable_if< std::is_same< Device, Devices::Host >::value >::type > - bool write_impl( const Type* buffer, std::streamsize elements ); - - template< typename Type, - typename Device, - typename = typename std::enable_if< std::is_same< Device, Devices::Cuda >::value >::type, - typename = void > - bool write_impl( const Type* buffer, std::streamsize elements ); - - template< typename Type, - typename Device, - typename = typename std::enable_if< std::is_same< Device, Devices::MIC >::value >::type, - typename = void, - typename = void > - bool write_impl( const Type* buffer, std::streamsize elements ); + public: + + enum class Mode + { + In = 1, + Out = 2, + Append = 4 + }; + + /** + * \brief Basic constructor. + */ + File() = default; + + /** + * \brief Attempts to open given file and returns \e true after the file is + * successfully opened. Otherwise returns \e false. + * + * Opens file with given \e fileName and returns true/false based on the success in opening the file. + * \param fileName String which indicates name of the file user wants to open. + * \param mode Indicates what user needs to do with opened file. + */ + bool open( const String& fileName, + const IOMode mode ); + + /** + * \brief Attempts to close given file and returns \e true when the file is + * successfully closed. Otherwise returns \e false. + */ + bool close(); + + /** + * \brief Returns name of given file. + */ + const String& getFileName() const + { + return this->fileName; + } + + /** + * \brief Method that can write particular data type from given file into GPU. (Function that gets particular elements from given file.) + * + * Returns \e true when the elements are successfully read from given file. Otherwise returns \e false. + * + * Throws \ref std::ios_base::failure on failure. + * + * \tparam Type Type of data. + * \tparam Device Place where data are stored after reading from file. For example \ref Devices::Host or \ref Devices::Cuda. + * \tparam Index Type of index by which the elements are indexed. + * \param buffer Pointer in memory where the elements are loaded and stored after reading. + * \param elements Number of elements the user wants to get (read) from given file. + */ + template< typename Type, typename Device = Devices::Host > + bool read( Type* buffer, std::streamsize elements = 1 ); + + // Toto je treba?? + //template< typename Type, typename Device = Devices::Host > + //bool read( Type* buffer ); + + /** + * \brief Method that can write particular data type from CPU into given file. (Function that writes particular elements into given file.) + * + * Returns \e true when the elements are successfully written into given file. Otherwise returns \e false. + * + * Throws \ref std::ios_base::failure on failure. + * + * \tparam Type Type of data. + * \tparam Device Place from where the data are loaded before writing into file. For example \ref Devices::Host or \ref Devices::Cuda. + * \tparam Index Type of index by which the elements are indexed. + * \param buffer Pointer in memory where the elements are loaded from before writing into file. + * \param elements Number of elements the user wants to write into the given file. + */ + template< typename Type, typename Device = Devices::Host > + bool write( const Type* buffer, std::streamsize elements = 1 ); + + // Toto je treba? + //template< typename Type, typename Device = Devices::Host > + //bool write( const Type* buffer ); + + protected: + template< typename Type, + typename Device, + typename = typename std::enable_if< std::is_same< Device, Devices::Host >::value >::type > + bool read_impl( Type* buffer, std::streamsize elements ); + + template< typename Type, + typename Device, + typename = typename std::enable_if< std::is_same< Device, Devices::Cuda >::value >::type, + typename = void > + bool read_impl( Type* buffer, std::streamsize elements ); + + template< typename Type, + typename Device, + typename = typename std::enable_if< std::is_same< Device, Devices::MIC >::value >::type, + typename = void, + typename = void > + bool read_impl( Type* buffer, std::streamsize elements ); + + template< typename Type, + typename Device, + typename = typename std::enable_if< std::is_same< Device, Devices::Host >::value >::type > + bool write_impl( const Type* buffer, std::streamsize elements ); + + template< typename Type, + typename Device, + typename = typename std::enable_if< std::is_same< Device, Devices::Cuda >::value >::type, + typename = void > + bool write_impl( const Type* buffer, std::streamsize elements ); + + template< typename Type, + typename Device, + typename = typename std::enable_if< std::is_same< Device, Devices::MIC >::value >::type, + typename = void, + typename = void > + bool write_impl( const Type* buffer, std::streamsize elements ); + + std::fstream file; + String fileName; }; -/// Returns true if the file exists and false otherwise. -/// -/// Finds out if the file \e fileName exists. -/// \param fileName Name of the file that user wants to find in the PC. +/** + * \brief Returns true if the file exists and false otherwise. + * + * Finds out if the file \e fileName exists. + * \param fileName Name of the file to check. + */ bool fileExists( const String& fileName ); -// serialization of strings +/** + * \brief Serialization of strings + */ File& operator<<( File& file, const std::string& str ); -// deserialization of strings +/** + * \brief Deserialization of strings. + */ File& operator>>( File& file, std::string& str ); } // namespace TNL -#include <TNL/File_impl.h> +#include <TNL/File.hpp> diff --git a/src/TNL/File_impl.h b/src/TNL/File.hpp similarity index 98% rename from src/TNL/File_impl.h rename to src/TNL/File.hpp index 03c3f0d4a23cb8ba7a4972e09c0846f03bb39503..9c4defdbcec3f25a3fe816fb4b5be100481663c4 100644 --- a/src/TNL/File_impl.h +++ b/src/TNL/File.hpp @@ -56,17 +56,17 @@ inline bool File::close() return true; } -template< typename Type, typename Device > +/*template< typename Type, typename Device > bool File::read( Type* buffer ) { return read< Type, Device >( buffer, 1 ); -} +}*/ -template< typename Type, typename Device > +/*template< typename Type, typename Device > bool File::write( const Type* buffer ) { return write< Type, Device >( buffer, 1 ); -} +}*/ template< typename Type, typename Device > bool File::read( Type* buffer, std::streamsize elements )