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



##########
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:
       ThreadLocalStore has some problems 
(https://github.com/dmlc/dmlc-core/issues/571), let's just use 
https://en.cppreference.com/w/cpp/keyword/thread_local ?

##########
File path: src/io/dataloader.cc
##########
@@ -0,0 +1,188 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ *  Copyright (c) 2020 by Contributors
+ * \file dataloader.cc
+ * \brief Pure c++ backed dataloader implementation
+ */
+#include <dmlc/parameter.h>
+#include <dmlc/omp.h>
+#include <mxnet/io.h>
+
+#include "./inst_vector.h"
+#include "./iter_prefetcher.h"
+#include "../profiler/custom_op_profiler.h"
+
+namespace mxnet {
+namespace io {
+struct ThreadedDataLoaderParam : public 
dmlc::Parameter<ThreadedDataLoaderParam> {
+  /*! \brief Multithread worker number. */
+  int num_workers;
+  /*! \brief dataset pointer.*/
+  std::intptr_t dataset;
+  /*! \brief sampler pointer.*/
+  std::intptr_t sampler;
+  /*! \brief batchify function pointer.*/
+  std::intptr_t batchify_fn;
+  /*! \brief pin memory to device id.*/
+  int pin_device_id;
+  // declare parameters
+  DMLC_DECLARE_PARAMETER(ThreadedDataLoaderParam) {
+      DMLC_DECLARE_FIELD(num_workers).set_default(0)
+          .describe("Number of thread workers.");
+      DMLC_DECLARE_FIELD(dataset)
+          .describe("Number of thread workers.");

Review comment:
       describe text is wrong

##########
File path: src/io/dataloader.cc
##########
@@ -170,8 +164,7 @@ class ThreadedDataLoader : public IIterator<TBlobBatch> {
   /*! \brief batched buffer */
   std::vector<NDArray> batched_buffer_;
   /*! \brief pointer to dataset */
-  // std::shared_ptr<Dataset> dataset_;
-  std::vector<std::shared_ptr<Dataset>> datasets_;
+  std::shared_ptr<Dataset> dataset_;

Review comment:
       Would we have some double free now? Because this shared_ptr would clean 
up the Dataset at the end of it's lifetime, and we also have a MXDatasetFree C 
API

##########
File path: src/io/iter_sampler.cc
##########
@@ -126,7 +126,9 @@ class RandomSampler : public IIterator<DataInst> {
     param_.InitAllowUnknown(kwargs);
     indices_.resize(param_.length);
     std::iota(std::begin(indices_), std::end(indices_), 0);  // fill like 
arange
-    rng_.reset(new common::RANDOM_ENGINE(kRandMagic + param_.seed));
+    mshadow::Random<cpu> *ctx_rng = ResourceManager::Get()->Request(
+      Context::CPU(), ResourceRequest::kRandom).get_random<cpu, 
real_t>(nullptr);
+    rng_.reset(new common::RANDOM_ENGINE(ctx_rng->GetSeed() + param_.seed));

Review comment:
       Delete the user-facing `seed` parameter now that you're using the MXNet 
seed?




----------------------------------------------------------------
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