/*! * Copyright (c) 2014-2019 by Contributors * \file sparse_page_writer.h * \author Tianqi Chen */ #ifndef XGBOOST_DATA_SPARSE_PAGE_WRITER_H_ #define XGBOOST_DATA_SPARSE_PAGE_WRITER_H_ #include #include #include #include #include #include #include #include #include #if DMLC_ENABLE_STD_THREAD #include #include #endif // DMLC_ENABLE_STD_THREAD namespace xgboost { namespace data { template struct SparsePageFormatReg; /*! * \brief Format specification of SparsePage. */ template class SparsePageFormat { public: /*! \brief virtual destructor */ virtual ~SparsePageFormat() = default; /*! * \brief Load all the segments into page, advance fi to end of the block. * \param page The data to read page into. * \param fi the input stream of the file * \return true of the loading as successful, false if end of file was reached */ virtual bool Read(T* page, dmlc::SeekStream* fi) = 0; /*! * \brief save the data to fo, when a page was written. * \param fo output stream */ virtual size_t Write(const T& page, dmlc::Stream* fo) = 0; }; /*! * \brief Create sparse page of format. * \return The created format functors. */ template inline SparsePageFormat* CreatePageFormat(const std::string& name) { auto *e = ::dmlc::Registry>::Get()->Find(name); if (e == nullptr) { LOG(FATAL) << "Unknown format type " << name; return nullptr; } return (e->body)(); } /*! * \brief Registry entry for sparse page format. */ template struct SparsePageFormatReg : public dmlc::FunctionRegEntryBase, std::function* ()>> { }; /*! * \brief Macro to register sparse page format. * * \code * // example of registering a objective * XGBOOST_REGISTER_SPARSE_PAGE_FORMAT(raw) * .describe("Raw binary data format.") * .set_body([]() { * return new RawFormat(); * }); * \endcode */ #define SparsePageFmt SparsePageFormat #define XGBOOST_REGISTER_SPARSE_PAGE_FORMAT(Name) \ DMLC_REGISTRY_REGISTER(SparsePageFormatReg, SparsePageFmt, Name) #define CSCPageFmt SparsePageFormat #define XGBOOST_REGISTER_CSC_PAGE_FORMAT(Name) \ DMLC_REGISTRY_REGISTER(SparsePageFormatReg, CSCPageFmt, Name) #define SortedCSCPageFmt SparsePageFormat #define XGBOOST_REGISTER_SORTED_CSC_PAGE_FORMAT(Name) \ DMLC_REGISTRY_REGISTER(SparsePageFormatReg, SortedCSCPageFmt, Name) #define EllpackPageFmt SparsePageFormat #define XGBOOST_REGISTER_ELLPACK_PAGE_FORMAT(Name) \ DMLC_REGISTRY_REGISTER(SparsePageFormatReg, EllpackPageFmt, Name) #define GHistIndexPageFmt SparsePageFormat #define XGBOOST_REGISTER_GHIST_INDEX_PAGE_FORMAT(Name) \ DMLC_REGISTRY_REGISTER(SparsePageFormatReg, \ GHistIndexPageFmt, Name) } // namespace data } // namespace xgboost #endif // XGBOOST_DATA_SPARSE_PAGE_WRITER_H_