| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#ifndef DMLC_INPUT_SPLIT_SHUFFLE_H_ |
| 8 |
#define DMLC_INPUT_SPLIT_SHUFFLE_H_ |
| 9 |
|
| 10 |
#include <algorithm> |
| 11 |
#include <cstdio> |
| 12 |
#include <cstring> |
| 13 |
#include <memory> |
| 14 |
#include <string> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
#include <dmlc/io.h> |
| 18 |
|
| 19 |
namespace dmlc { |
| 20 |
|
| 21 |
class InputSplitShuffle : public InputSplit { |
| 22 |
public: |
| 23 |
|
| 24 |
virtual ~InputSplitShuffle(void) { |
| 25 |
source_.reset(); |
| 26 |
} |
| 27 |
|
| 28 |
virtual void BeforeFirst(void) { |
| 29 |
if (num_shuffle_parts_ > 1) { |
| 30 |
std::shuffle(shuffle_indexes_.begin(), shuffle_indexes_.end(), trnd_); |
| 31 |
int idx = shuffle_indexes_[0] + part_index_ * num_shuffle_parts_; |
| 32 |
source_->ResetPartition(idx, num_parts_ * num_shuffle_parts_); |
| 33 |
cur_shuffle_idx_ = 0; |
| 34 |
} else { |
| 35 |
source_->BeforeFirst(); |
| 36 |
} |
| 37 |
} |
| 38 |
virtual void HintChunkSize(size_t chunk_size) { |
| 39 |
source_->HintChunkSize(chunk_size); |
| 40 |
} |
| 41 |
virtual size_t GetTotalSize(void) { |
| 42 |
return source_->GetTotalSize(); |
| 43 |
} |
| 44 |
|
| 45 |
virtual bool NextRecord(Blob *out_rec) { |
| 46 |
if (num_shuffle_parts_ > 1) { |
| 47 |
if (!source_->NextRecord(out_rec)) { |
| 48 |
if (cur_shuffle_idx_ == num_shuffle_parts_ - 1) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
++cur_shuffle_idx_; |
| 52 |
int idx = shuffle_indexes_[cur_shuffle_idx_] + part_index_ * num_shuffle_parts_; |
| 53 |
source_->ResetPartition(idx, num_parts_ * num_shuffle_parts_); |
| 54 |
return NextRecord(out_rec); |
| 55 |
} else { |
| 56 |
return true; |
| 57 |
} |
| 58 |
} else { |
| 59 |
return source_->NextRecord(out_rec); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
virtual bool NextChunk(Blob *out_chunk) { |
| 64 |
if (num_shuffle_parts_ > 1) { |
| 65 |
if (!source_->NextChunk(out_chunk)) { |
| 66 |
if (cur_shuffle_idx_ == num_shuffle_parts_ - 1) { |
| 67 |
return false; |
| 68 |
} |
| 69 |
++cur_shuffle_idx_; |
| 70 |
int idx = shuffle_indexes_[cur_shuffle_idx_] + part_index_ * num_shuffle_parts_; |
| 71 |
source_->ResetPartition(idx, num_parts_ * num_shuffle_parts_); |
| 72 |
return NextChunk(out_chunk); |
| 73 |
} else { |
| 74 |
return true; |
| 75 |
} |
| 76 |
} else { |
| 77 |
return source_->NextChunk(out_chunk); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
virtual void ResetPartition(unsigned rank, unsigned nsplit) { |
| 82 |
CHECK(nsplit == num_parts_) << "num_parts is not consistent!"; |
| 83 |
int idx = shuffle_indexes_[0] + rank * num_shuffle_parts_; |
| 84 |
source_->ResetPartition(idx, nsplit * num_shuffle_parts_); |
| 85 |
cur_shuffle_idx_ = 0; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
InputSplitShuffle(const char *uri, unsigned part_index, unsigned num_parts, const char *type, |
| 103 |
unsigned num_shuffle_parts, int shuffle_seed) |
| 104 |
: part_index_(part_index), |
| 105 |
num_parts_(num_parts), |
| 106 |
num_shuffle_parts_(num_shuffle_parts), |
| 107 |
cur_shuffle_idx_(0) { |
| 108 |
for (unsigned i = 0; i < num_shuffle_parts_; i++) { |
| 109 |
shuffle_indexes_.push_back(i); |
| 110 |
} |
| 111 |
trnd_.seed(kRandMagic_ + part_index_ + num_parts_ + num_shuffle_parts_ + shuffle_seed); |
| 112 |
std::shuffle(shuffle_indexes_.begin(), shuffle_indexes_.end(), trnd_); |
| 113 |
int idx = shuffle_indexes_[cur_shuffle_idx_] + part_index_ * num_shuffle_parts_; |
| 114 |
source_.reset(InputSplit::Create(uri, idx, num_parts_ * num_shuffle_parts_, type)); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
static InputSplit *Create(const char *uri, unsigned part_index, unsigned num_parts, |
| 135 |
const char *type, unsigned num_shuffle_parts, int shuffle_seed) { |
| 136 |
CHECK(num_shuffle_parts > 0) << "number of shuffle parts should be greater than zero!"; |
| 137 |
return new InputSplitShuffle(uri, part_index, num_parts, type, num_shuffle_parts, shuffle_seed); |
| 138 |
} |
| 139 |
|
| 140 |
private: |
| 141 |
|
| 142 |
static const int kRandMagic_ = 666; |
| 143 |
|
| 144 |
std::mt19937 trnd_; |
| 145 |
|
| 146 |
std::unique_ptr<InputSplit> source_; |
| 147 |
|
| 148 |
unsigned part_index_; |
| 149 |
|
| 150 |
unsigned num_parts_; |
| 151 |
|
| 152 |
unsigned num_shuffle_parts_; |
| 153 |
|
| 154 |
unsigned cur_shuffle_idx_; |
| 155 |
|
| 156 |
std::vector<int> shuffle_indexes_; |
| 157 |
}; |
| 158 |
} |
| 159 |
#endif |