Medial Code Documentation
Loading...
Searching...
No Matches
pipeline_reader.h
1#ifndef LIGHTGBM_UTILS_PIPELINE_READER_H_
2#define LIGHTGBM_UTILS_PIPELINE_READER_H_
3
4#include <LightGBM/utils/log.h>
5
6#include <cstdio>
7
8#include <functional>
9#include <thread>
10#include <memory>
11#include <algorithm>
12#include <vector>
13#include "file_io.h"
14
15namespace LightGBM {
16
21public:
27 static size_t Read(const char* filename, int skip_bytes, const std::function<size_t(const char*, size_t)>& process_fun) {
28 auto reader = VirtualFileReader::Make(filename);
29 if (!reader->Init()) {
30 return 0;
31 }
32 size_t cnt = 0;
33 const size_t buffer_size = 16 * 1024 * 1024;
34 // buffer used for the process_fun
35 auto buffer_process = std::vector<char>(buffer_size);
36 // buffer used for the file reading
37 auto buffer_read = std::vector<char>(buffer_size);
38 size_t read_cnt = 0;
39 if (skip_bytes > 0) {
40 // skip first k bytes
41 read_cnt = reader->Read(buffer_process.data(), skip_bytes);
42 }
43 // read first block
44 read_cnt = reader->Read(buffer_process.data(), buffer_size);
45
46 size_t last_read_cnt = 0;
47 while (read_cnt > 0) {
48 // start read thread
49 std::thread read_worker = std::thread(
50 [&] {
51 last_read_cnt = reader->Read(buffer_read.data(), buffer_size);
52 });
53 // start process
54 cnt += process_fun(buffer_process.data(), read_cnt);
55 // wait for read thread
56 read_worker.join();
57 // exchange the buffer
58 std::swap(buffer_process, buffer_read);
59 read_cnt = last_read_cnt;
60 }
61 return cnt;
62 }
63};
64
65} // namespace LightGBM
66
67#endif // LightGBM_UTILS_PIPELINE_READER_H_
A pipeline file reader, use 2 threads, one read block from file, the other process the block.
Definition pipeline_reader.h:20
static size_t Read(const char *filename, int skip_bytes, const std::function< size_t(const char *, size_t)> &process_fun)
Read data from a file, use pipeline methods.
Definition pipeline_reader.h:27
desc and descl2 fields must be written in reStructuredText format
Definition application.h:10
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.hpp:24418
static std::unique_ptr< VirtualFileReader > Make(const std::string &filename)
Create appropriate reader for filename.
Definition file_io.cpp:153