From 679bdadb6b51b14013a00588cec2452d6ee1d1ac Mon Sep 17 00:00:00 2001 From: TatWai Chong Date: Mon, 31 Jul 2023 15:15:12 -0700 Subject: Simplify overloaded writeToNpyFiles and readFromNpyFiles templatize these functions instead to reduce redundant code. Signed-off-by: TatWai Chong Change-Id: Ie8b6f7d2b489c3508fea72481ce38f0db6d0c490 --- src/numpy_utils.cpp | 259 ++++++---------------------------------------------- 1 file changed, 30 insertions(+), 229 deletions(-) (limited to 'src') diff --git a/src/numpy_utils.cpp b/src/numpy_utils.cpp index 0002fd9..64460bd 100644 --- a/src/numpy_utils.cpp +++ b/src/numpy_utils.cpp @@ -16,6 +16,7 @@ #include "numpy_utils.h" #include "half.hpp" #include + // Magic NUMPY header static const char NUMPY_HEADER_STR[] = "\x93NUMPY\x1\x0\x76\x0{"; static const int NUMPY_HEADER_SZ = 128; @@ -24,20 +25,10 @@ static const int NUMPY_MAX_DIMS_SUPPORTED = 10; // Offset for NUMPY header desc dictionary string static const int NUMPY_HEADER_DESC_OFFSET = 8; -NumpyUtilities::NPError NumpyUtilities::readFromNpyFile(const char* filename, const uint32_t elems, bool* databuf) -{ - const char dtype_str[] = "'|b1'"; - return readFromNpyFileCommon(filename, dtype_str, 1, elems, databuf, true); -} - +// This is an entry function for reading 8-/16-/32-bit npy file. +template <> NumpyUtilities::NPError NumpyUtilities::readFromNpyFile(const char* filename, const uint32_t elems, int32_t* databuf) { - const char dtype_str_uint8[] = "'|u1'"; - const char dtype_str_int8[] = "'|i1'"; - const char dtype_str_uint16[] = "'(filename, elems, tmp_buf); + copyBufferByElement(databuf, tmp_buf, elems); + free(tmp_buf); } else { - // uint8 - uint8_t* ui8databuf = nullptr; - ui8databuf = (uint8_t*)calloc(sizeof(ui8databuf), elems); - - rc = readFromNpyFileCommon(filename, dtype_str_uint8, sizeof(uint8_t), elems, ui8databuf, false); - - for (unsigned i = 0; i < elems; ++i) - { - databuf[i] = (int32_t)ui8databuf[i]; - } - free(ui8databuf); + uint8_t* tmp_buf = new uint8_t[elems]; + rc = readFromNpyFile(filename, elems, tmp_buf); + copyBufferByElement(databuf, tmp_buf, elems); + free(tmp_buf); } break; - case 2: // 16-bit + case 2: if (is_signed) { - // int16 - int16_t* i16databuf = nullptr; - i16databuf = (int16_t*)calloc(sizeof(i16databuf), elems); - - rc = readFromNpyFileCommon(filename, dtype_str_int16, sizeof(int16_t), elems, i16databuf, false); - - for (unsigned i = 0; i < elems; ++i) - { - databuf[i] = (int32_t)i16databuf[i]; - } - free(i16databuf); - - return rc; + int16_t* tmp_buf = new int16_t[elems]; + rc = readFromNpyFile(filename, elems, tmp_buf); + copyBufferByElement(databuf, tmp_buf, elems); + free(tmp_buf); } else { - // uint16 - uint16_t* ui16databuf = nullptr; - ui16databuf = (uint16_t*)calloc(sizeof(ui16databuf), elems); - - rc = readFromNpyFileCommon(filename, dtype_str_uint16, sizeof(uint16_t), elems, ui16databuf, false); - - for (unsigned i = 0; i < elems; ++i) - { - databuf[i] = (int32_t)ui16databuf[i]; - } - free(ui16databuf); - - return rc; + uint16_t* tmp_buf = new uint16_t[elems]; + rc = readFromNpyFile(filename, elems, tmp_buf); + copyBufferByElement(databuf, tmp_buf, elems); + free(tmp_buf); } break; - case 4: // 32-bit + case 4: if (is_signed) { - // int32 - return readFromNpyFileCommon(filename, dtype_str_int32, sizeof(int32_t), elems, databuf, false); + bool is_bool; + const char* dtype_str = getDTypeString(is_bool); + rc = readFromNpyFileCommon(filename, dtype_str, sizeof(int32_t), elems, databuf, is_bool); } else { // uint32, not supported - return DATA_TYPE_NOT_SUPPORTED; + rc = DATA_TYPE_NOT_SUPPORTED; } break; default: @@ -144,31 +102,6 @@ NumpyUtilities::NPError NumpyUtilities::readFromNpyFile(const char* filename, co return rc; } -NumpyUtilities::NPError NumpyUtilities::readFromNpyFile(const char* filename, const uint32_t elems, int64_t* databuf) -{ - const char dtype_str[] = "' shape = { (int32_t)elems }; - return writeToNpyFile(filename, shape, databuf); -} - -NumpyUtilities::NPError - NumpyUtilities::writeToNpyFile(const char* filename, const std::vector& shape, const bool* databuf) -{ - const char dtype_str[] = "'|b1'"; - return writeToNpyFileCommon(filename, dtype_str, 1, shape, databuf, true); // bools written as size 1 -} - -NumpyUtilities::NPError - NumpyUtilities::writeToNpyFile(const char* filename, const uint32_t elems, const uint8_t* databuf) -{ - std::vector shape = { (int32_t)elems }; - return writeToNpyFile(filename, shape, databuf); -} - -NumpyUtilities::NPError - NumpyUtilities::writeToNpyFile(const char* filename, const std::vector& shape, const uint8_t* databuf) -{ - const char dtype_str[] = "'|u1'"; - return writeToNpyFileCommon(filename, dtype_str, sizeof(uint8_t), shape, databuf, false); -} - -NumpyUtilities::NPError - NumpyUtilities::writeToNpyFile(const char* filename, const uint32_t elems, const int8_t* databuf) -{ - std::vector shape = { (int32_t)elems }; - return writeToNpyFile(filename, shape, databuf); -} - -NumpyUtilities::NPError - NumpyUtilities::writeToNpyFile(const char* filename, const std::vector& shape, const int8_t* databuf) -{ - const char dtype_str[] = "'|i1'"; - return writeToNpyFileCommon(filename, dtype_str, sizeof(int8_t), shape, databuf, false); -} - -NumpyUtilities::NPError - NumpyUtilities::writeToNpyFile(const char* filename, const uint32_t elems, const uint16_t* databuf) -{ - std::vector shape = { (int32_t)elems }; - return writeToNpyFile(filename, shape, databuf); -} - -NumpyUtilities::NPError - NumpyUtilities::writeToNpyFile(const char* filename, const std::vector& shape, const uint16_t* databuf) -{ - const char dtype_str[] = "' shape = { (int32_t)elems }; - return writeToNpyFile(filename, shape, databuf); -} - -NumpyUtilities::NPError - NumpyUtilities::writeToNpyFile(const char* filename, const std::vector& shape, const int16_t* databuf) -{ - const char dtype_str[] = "' shape = { (int32_t)elems }; - return writeToNpyFile(filename, shape, databuf); -} - -NumpyUtilities::NPError - NumpyUtilities::writeToNpyFile(const char* filename, const std::vector& shape, const int32_t* databuf) -{ - const char dtype_str[] = "' shape = { (int32_t)elems }; - return writeToNpyFile(filename, shape, databuf); -} - -NumpyUtilities::NPError - NumpyUtilities::writeToNpyFile(const char* filename, const std::vector& shape, const int64_t* databuf) -{ - const char dtype_str[] = "' shape = { (int32_t)elems }; - return writeToNpyFile(filename, shape, databuf); -} - -NumpyUtilities::NPError - NumpyUtilities::writeToNpyFile(const char* filename, const std::vector& shape, const float* databuf) -{ - const char dtype_str[] = "' shape = { (int32_t)elems }; - return writeToNpyFile(filename, shape, databuf); -} - -NumpyUtilities::NPError - NumpyUtilities::writeToNpyFile(const char* filename, const std::vector& shape, const double* databuf) -{ - const char dtype_str[] = "'& shape, - const half_float::half* databuf) -{ - const char dtype_str[] = "'