[arrayfire] 363/408: FEAT: Adding support for linear indexing in C API

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Sep 21 19:12:31 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/sid
in repository arrayfire.

commit 4b5725b6930df0d3e96a769bf83163b07cd69fc1
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date:   Wed Aug 26 21:12:42 2015 -0400

    FEAT: Adding support for linear indexing in C API
---
 src/api/c/index.cpp |  23 ++++++++++-
 test/index.cpp      | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 129 insertions(+), 2 deletions(-)

diff --git a/src/api/c/index.cpp b/src/api/c/index.cpp
index 0e0d449..a9f276d 100644
--- a/src/api/c/index.cpp
+++ b/src/api/c/index.cpp
@@ -13,6 +13,7 @@
 #include <af/array.h>
 #include <af/index.h>
 #include <af/arith.h>
+#include <af/data.h>
 #include <ArrayInfo.hpp>
 #include <err_common.hpp>
 #include <handle.hpp>
@@ -39,7 +40,17 @@ af_err af_index(af_array *result, const af_array in, const unsigned ndims, const
 {
     af_array out;
     try {
-        af_dtype in_type = getInfo(in).getType();
+
+        ArrayInfo iInfo = getInfo(in);
+        if (ndims == 1 && ndims != (dim_t)iInfo.ndims()) {
+            af_array tmp_in;
+            AF_CHECK(af_flat(&tmp_in, in));
+            AF_CHECK(af_index(result, tmp_in, ndims, index));
+            AF_CHECK(af_release_array(tmp_in));
+            return AF_SUCCESS;
+        }
+
+        af_dtype in_type = iInfo.getType();
 
         switch(in_type) {
         case f32:    indexArray<float>   (out, in, ndims, index);  break;
@@ -145,6 +156,15 @@ af_err af_index_gen(af_array *out, const af_array in, const dim_t ndims, const a
         ARG_ASSERT(2, (ndims>0));
         ARG_ASSERT(3, (indexs!=NULL));
 
+        ArrayInfo iInfo = getInfo(in);
+        if (ndims == 1 && ndims != (dim_t)iInfo.ndims()) {
+            af_array tmp_in;
+            AF_CHECK(af_flat(&tmp_in, in));
+            AF_CHECK(af_index_gen(out, tmp_in, ndims, indexs));
+            AF_CHECK(af_release_array(tmp_in));
+            return AF_SUCCESS;
+        }
+
         int track = 0;
         af_seq seqs[] = {af_span, af_span, af_span, af_span};
         for (dim_t i = 0; i < ndims; i++) {
@@ -183,7 +203,6 @@ af_err af_index_gen(af_array *out, const af_array in, const dim_t ndims, const a
             }
         }
 
-        ArrayInfo iInfo = getInfo(in);
         dim4 iDims = iInfo.dims();
 
         ARG_ASSERT(1, (iDims.ndims()>0));
diff --git a/test/index.cpp b/test/index.cpp
index 28b5745..6a798aa 100644
--- a/test/index.cpp
+++ b/test/index.cpp
@@ -1261,3 +1261,111 @@ TEST(Indexing, SNIPPET_indexing_copy)
   // but the underlying array should only be
   // freed once.
 }
+
+TEST(Asssign, LinearIndexSeq)
+{
+    using af::array;
+    const int nx = 5;
+    const int ny = 4;
+
+    const int st = nx - 2;
+    const int en = nx * (ny - 1);
+    const int num = (en - st + 1);
+
+    array a = af::randu(nx, ny);
+    af::index idx = af::seq(st, en);
+
+    af_array in_arr = a.get();
+    af_index_t ii = idx.get();
+    af_array out_arr;
+
+    ASSERT_EQ(AF_SUCCESS,
+              af_index(&out_arr, in_arr, 1, &ii.idx.seq));
+
+    af::array out(out_arr);
+
+    ASSERT_EQ(out.dims(0), num);
+    ASSERT_EQ(out.elements(), num);
+
+    std::vector<float> hout(nx * ny);
+    std::vector<float> ha(nx * ny);
+
+    a.host(&ha[0]);
+    out.host(&hout[0]);
+
+    for (int i = 0; i < num; i++) {
+        ASSERT_EQ(ha[i + st], hout[i]);
+    }
+}
+
+TEST(Asssign, LinearIndexGenSeq)
+{
+    using af::array;
+    const int nx = 5;
+    const int ny = 4;
+
+    const int st = nx - 2;
+    const int en = nx * (ny - 1);
+    const int num = (en - st + 1);
+
+    array a = af::randu(nx, ny);
+    af::index idx = af::seq(st, en);
+
+    af_array in_arr = a.get();
+    af_index_t ii = idx.get();
+    af_array out_arr;
+
+    ASSERT_EQ(AF_SUCCESS,
+              af_index_gen(&out_arr, in_arr, 1, &ii));
+
+    af::array out(out_arr);
+
+    ASSERT_EQ(out.dims(0), num);
+    ASSERT_EQ(out.elements(), num);
+
+    std::vector<float> hout(nx * ny);
+    std::vector<float> ha(nx * ny);
+
+    a.host(&ha[0]);
+    out.host(&hout[0]);
+
+    for (int i = 0; i < num; i++) {
+        ASSERT_EQ(ha[i + st], hout[i]);
+    }
+}
+
+TEST(Asssign, LinearIndexGenArr)
+{
+    using af::array;
+    const int nx = 5;
+    const int ny = 4;
+
+    const int st = nx - 2;
+    const int en = nx * (ny - 1);
+    const int num = (en - st + 1);
+
+    array a = af::randu(nx, ny);
+    af::index idx = af::array(af::seq(st, en));
+
+    af_array in_arr = a.get();
+    af_index_t ii = idx.get();
+    af_array out_arr;
+
+    ASSERT_EQ(AF_SUCCESS,
+              af_index_gen(&out_arr, in_arr, 1, &ii));
+
+    af::array out(out_arr);
+
+    ASSERT_EQ(out.dims(0), num);
+    ASSERT_EQ(out.elements(), num);
+
+    std::vector<float> hout(nx * ny);
+    std::vector<float> ha(nx * ny);
+
+    a.host(&ha[0]);
+    out.host(&hout[0]);
+
+    for (int i = 0; i < num; i++) {
+        ASSERT_EQ(ha[i + st], hout[i]);
+    }
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/arrayfire.git



More information about the debian-science-commits mailing list