This is an automated email from the ASF dual-hosted git repository.

jxie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/master by this push:
     new b37b3f5  Replace std::swap_ranges with memcpy (#10351)
b37b3f5 is described below

commit b37b3f5014abc25ce70bb4b89ed96d1fc9ac7fb3
Author: Deokjae Lee <36436141+asitsta...@users.noreply.github.com>
AuthorDate: Tue Apr 3 03:33:05 2018 +0900

    Replace std::swap_ranges with memcpy (#10351)
---
 src/operator/random/shuffle_op.cc | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/operator/random/shuffle_op.cc 
b/src/operator/random/shuffle_op.cc
index d2a3e2d..983f879 100644
--- a/src/operator/random/shuffle_op.cc
+++ b/src/operator/random/shuffle_op.cc
@@ -30,6 +30,7 @@
 #include <algorithm>
 #include <random>
 #include <vector>
+#include <cstring>
 #ifdef USE_GNU_PARALLEL_SHUFFLE
   #include <parallel/algorithm>
 #endif
@@ -55,18 +56,24 @@ void Shuffle1D(DType* const out, const index_t size, Rand* 
const prnd) {
 
 template<typename DType, typename Rand>
 void ShuffleND(DType* const out, const index_t size, const index_t 
first_axis_len,
-                Rand* const prnd) {
+                Rand* const prnd, const OpContext& ctx) {
   // Fisher-Yates shuffling
+  using namespace mxnet_op;
   const index_t stride = size / first_axis_len;
   auto rand_n = [prnd](index_t n) {
     std::uniform_int_distribution<index_t> dist(0, n - 1);
     return dist(*prnd);
   };
   CHECK_GT(first_axis_len, 0U);
+  const size_t stride_bytes = sizeof(DType) * stride;
+  Tensor<cpu, 1, char> buf =
+    ctx.requested[1].get_space_typed<cpu, 1, char>(Shape1(stride_bytes), 
ctx.get_stream<cpu>());
   for (index_t i = first_axis_len - 1; i > 0; --i) {
     const index_t j = rand_n(i + 1);
     if (i != j) {
-      std::swap_ranges(out + stride * i, out + stride * (i + 1), out + stride 
* j);
+      std::memcpy(buf.dptr_, out + stride * i, stride_bytes);
+      std::memcpy(out + stride * i, out + stride * j, stride_bytes);
+      std::memcpy(out + stride * j, buf.dptr_, stride_bytes);
     }
   }
 }
@@ -97,7 +104,7 @@ void ShuffleForwardCPU(const nnvm::NodeAttrs& attrs,
     if (input_shape.ndim() == 1) {
       Shuffle1D(out.dptr_, size, &prnd);
     } else {
-      ShuffleND(out.dptr_, size, first_axis_len, &prnd);
+      ShuffleND(out.dptr_, size, first_axis_len, &prnd, ctx);
     }
   });
 }

-- 
To stop receiving notification emails like this one, please contact
j...@apache.org.

Reply via email to