zhreshold commented on a change in pull request #17841:
URL: https://github.com/apache/incubator-mxnet/pull/17841#discussion_r420413000



##########
File path: src/io/dataset.cc
##########
@@ -78,53 +74,44 @@ class RecordFileDataset final : public Dataset {
     delete idx_stream;
   }
 
-  RecordFileDataset* Clone(void) const {
-    auto other = new RecordFileDataset(std::vector<std::pair<std::string, 
std::string> >());
-    other->param_ = param_;
-    other->idx_ = idx_;
-    // do not share the pointer since it's not threadsafe to seek 
simultaneously
-    if (reader_ && stream_) {
-      dmlc::Stream *stream = dmlc::Stream::Create(param_.rec_file.c_str(), 
"r");
-      other->reader_ = std::make_shared<dmlc::RecordIOReader>(stream);
-      other->stream_.reset(stream);
-    }
-    return other;
-  }
-
   uint64_t GetLen() const {
     return idx_.size();
   }
 
   bool GetItem(uint64_t idx, std::vector<NDArray>* ret) {
     ret->resize(1);
     auto& out = (*ret)[0];
+    auto& reader = RecordIOPair::Get()->second;
+    if (!reader) {
+      auto s = dmlc::Stream::Create(param_.rec_file.c_str(), "r");
+      auto& stream = RecordIOPair::Get()->first;
+      stream.reset(s);
+      reader = std::make_unique<dmlc::RecordIOReader>(s);
+    }
     size_t pos = idx_[static_cast<size_t>(idx)];
-    {
-      std::lock_guard<std::mutex> lck(mutex_);
-      reader_->Seek(pos);
-      if (reader_->NextRecord(&read_buff_)) {
-        const char *buf = read_buff_.c_str();
-        const size_t size = read_buff_.size();
-        out = NDArray(TShape({static_cast<dim_t>(size)}), Context::CPU(), 
false, mshadow::kInt8);
-        TBlob dst = out.data();
-        RunContext rctx{Context::CPU(), nullptr, nullptr, false};
-        mxnet::ndarray::Copy<cpu, cpu>(
-          TBlob(const_cast<void*>(reinterpret_cast<const void*>(buf)),
-            out.shape(), cpu::kDevMask, out.dtype(), 0),
-            &dst, Context::CPU(), Context::CPU(), rctx);
-      }
+    auto read_buff = ReadBuff::Get();
+    reader->Seek(pos);
+    if (reader->NextRecord(read_buff)) {
+      const char *buf = read_buff->c_str();
+      const size_t size = read_buff->size();
+      out = NDArray(TShape({static_cast<dim_t>(size)}), Context::CPU(), false, 
mshadow::kInt8);
+      TBlob dst = out.data();
+      RunContext rctx{Context::CPU(), nullptr, nullptr, false};
+      mxnet::ndarray::Copy<cpu, cpu>(
+        TBlob(const_cast<void*>(reinterpret_cast<const void*>(buf)),
+          out.shape(), cpu::kDevMask, out.dtype(), 0),
+          &dst, Context::CPU(), Context::CPU(), rctx);
     }
     return true;
   }
 
  private:
+  using ReaderPtr = std::unique_ptr<dmlc::RecordIOReader>;
+  using StreamPtr = std::unique_ptr<dmlc::Stream>;
+  using RecordIOPair = dmlc::ThreadLocalStore<std::pair<StreamPtr, ReaderPtr> 
>;

Review comment:
       Swapped with `thread_local`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to