Skip to content

Commit

Permalink
[C API] NP_GetDims implementation (#422)
Browse files Browse the repository at this point in the history
### Summary:
Continue with basic implementation of a C API for Neuropod. Issue #407

### Test Plan:
Added test that test returned values of number of dims and data in dims array.
  • Loading branch information
vkuzmin-uber authored Aug 28, 2020
1 parent 5a10bdd commit 9992207
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
9 changes: 9 additions & 0 deletions source/neuropod/bindings/c/np_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ limitations under the License.
#include "neuropod/bindings/c/np_tensor_internal.h"
#include "neuropod/internal/neuropod_tensor_raw_data_access.hh"

void NP_GetDims(const NP_NeuropodTensor *tensor, size_t *num_dims, const int64_t **dims)
{
// get_dims returnis reference to internal tensor's dims collection.
// Use pointer to its data that is valid as long as tensor is valid.
const auto &dims_collection = tensor->tensor->as_tensor()->get_dims();
*num_dims = dims_collection.size();
*dims = dims_collection.data();
}

NP_TensorType NP_GetType(const NP_NeuropodTensor *tensor)
{
return static_cast<NP_TensorType>(tensor->tensor->as_tensor()->get_tensor_type());
Expand Down
14 changes: 11 additions & 3 deletions source/neuropod/tests/test_c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ static void CheckFailed(const char *expression, const char *filename, int line_n
#define ASSERT_EQ(expected, actual) CHECK((expected) == (actual))
#define ASSERT_NE(expected, actual) CHECK((expected) != (actual))
#define ASSERT_STREQ(expected, actual) ASSERT_EQ(0, strcmp((expected), (actual)))
#define ASSERT_ARRAYEQ(expected, actual, size) ASSERT_EQ(0, memcmp((expected), (actual), size))

static void TestLoadAndInference(void)
{
Expand Down Expand Up @@ -248,9 +249,16 @@ static void TestTensorGetters(void)
NP_NeuropodTensor *y = NP_AllocateTensor(allocator, sizeof(dims) / sizeof(int64_t), dims, DOUBLE_TENSOR);
NP_NeuropodTensor *z = NP_AllocateTensor(allocator, sizeof(dims) / sizeof(int64_t), dims, STRING_TENSOR);

ASSERT_EQ(NP_GetType(x), FLOAT_TENSOR);
ASSERT_EQ(NP_GetType(y), DOUBLE_TENSOR);
ASSERT_EQ(NP_GetType(z), STRING_TENSOR);
ASSERT_EQ(FLOAT_TENSOR, NP_GetType(x));
ASSERT_EQ(DOUBLE_TENSOR, NP_GetType(y));
ASSERT_EQ(STRING_TENSOR, NP_GetType(z));

size_t test_num_dims;
const int64_t *test_dims;
NP_GetDims(x, &test_num_dims, &test_dims);

ASSERT_EQ(sizeof(dims) / sizeof(dims[0]), test_num_dims);
ASSERT_ARRAYEQ(&dims[0], test_dims, test_num_dims);

NP_FreeTensor(x);
NP_FreeTensor(y);
Expand Down
2 changes: 1 addition & 1 deletion tools/autofix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fi
pushd source

# Reformat with clang-format
find . -name "*.hh" -o -name "*.h" -o -name "*.cc" | xargs -L1 clang-format -style=file -i
find . -name "*.hh" -o -name "*.h" -o -name "*.cc" -o -name "*.c" | xargs -L1 clang-format -style=file -i

pushd python

Expand Down

0 comments on commit 9992207

Please sign in to comment.