Skip to content

Commit

Permalink
add cnpy
Browse files Browse the repository at this point in the history
commit 2f56f4c23ea840f9d15c43801368cf4a089efa84
Author: sophgo-forum-service <[email protected]>
Date:   Sun May 19 23:03:28 2024 +0800

    [feat] cnpy opensource for cv18xx soc.

    - e9d84e
  • Loading branch information
carbonfix committed May 31, 2024
1 parent 83dc491 commit 3e2ce54
Show file tree
Hide file tree
Showing 10 changed files with 1,162 additions and 0 deletions.
1 change: 1 addition & 0 deletions .version/2024-05-31.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
| cvikernel | cvikernel | https://github.com/sophgo/cvikernel.git | sg200x-dev | 9f1f57a |
| cviruntime | cviruntime | https://github.com/sophgo/cviruntime.git | sg200x-dev | 3f49386 |
| cvimath | cvimath | https://github.com/sophgo/cvimath.git | sg200x-dev | ce8705f |
| cnpy | cnpy | https://github.com/sophgo/cnpy.git | sg200x-dev | 2f56f4c |
65 changes: 65 additions & 0 deletions cnpy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.0 FATAL_ERROR)
if(COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)

if (NOT DEFINED LLVM_MAIN_SRC_DIR)
project(CNPY)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

message(STATUS "CMAKE_SYSROOT ${CMAKE_SYSROOT}")
include_directories(${CMAKE_SYSROOT}/include)

include_directories(${CMAKE_INSTALL_PREFIX}/include)
link_directories(${CMAKE_INSTALL_PREFIX}/lib)

set(ENV{PKG_CONFIG_DIR} "")
set(ENV{PKG_CONFIG_LIBDIR} "${CMAKE_SYSROOT}/usr/lib/pkgconfig:${CMAKE_SYSROOT}/usr/share/pkgconfig")
set(ENV{PKG_CONFIG_SYSROOT_DIR} ${CMAKE_SYSROOT})
find_package(ZLIB)

option(ENABLE_STATIC "Build static (.a) library" ON)
add_library(cnpy SHARED "cnpy.cpp")
target_link_libraries(cnpy z)
install(TARGETS "cnpy" LIBRARY DESTINATION lib PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)

if(ENABLE_STATIC)
add_library(cnpy-static STATIC "cnpy.cpp")
set_target_properties(cnpy-static PROPERTIES OUTPUT_NAME "cnpy")
install(TARGETS "cnpy-static" ARCHIVE DESTINATION lib)
endif(ENABLE_STATIC)

install(FILES "cnpy.h" DESTINATION include)
install(FILES "mat2npz" "npy2mat" "npz2mat" DESTINATION bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)

add_executable(example1 example1.cpp)
target_link_libraries(example1 cnpy)

else()
message(STATUS "CNPY LLVM Tree Build")

add_custom_target(CNPY)
set_target_properties(CNPY PROPERTIES FOLDER Third_party)
add_dependencies(CNPY
cnpy
cnpy_example1
)

set(LLVM_OPTIONAL_SOURCES
cnpy.cpp
example1.cpp
)

find_package(ZLIB REQUIRED)

add_llvm_library(cnpy SHARED
cnpy.cpp)
target_link_libraries(cnpy ${ZLIB_LIBRARIES})

install(FILES "cnpy.h" DESTINATION include)
add_llvm_executable(cnpy_example1
example1.cpp)
target_link_libraries(cnpy_example1 PRIVATE
cnpy)
endif()
21 changes: 21 additions & 0 deletions cnpy/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) Carl Rogers, 2011

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
55 changes: 55 additions & 0 deletions cnpy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Purpose:

NumPy offers the `save` method for easy saving of arrays into .npy and `savez` for zipping multiple .npy arrays together into a .npz file.

`cnpy` lets you read and write to these formats in C++.

The motivation comes from scientific programming where large amounts of data are generated in C++ and analyzed in Python.

Writing to .npy has the advantage of using low-level C++ I/O (fread and fwrite) for speed and binary format for size.
The .npy file header takes care of specifying the size, shape, and data type of the array, so specifying the format of the data is unnecessary.

Loading data written in numpy formats into C++ is equally simple, but requires you to type-cast the loaded data to the type of your choice.

# Installation:

Default installation directory is /usr/local.
To specify a different directory, add `-DCMAKE_INSTALL_PREFIX=/path/to/install/dir` to the cmake invocation in step 4.

1. get [cmake](www.cmake.org)
2. create a build directory, say $HOME/build
3. cd $HOME/build
4. cmake /path/to/cnpy
5. make
6. make install

# Using:

To use, `#include"cnpy.h"` in your source code. Compile the source code mycode.cpp as

```bash
g++ -o mycode mycode.cpp -L/path/to/install/dir -lcnpy -lz --std=c++11
```

# Description:

There are two functions for writing data: `npy_save` and `npz_save`.

There are 3 functions for reading:
- `npy_load` will load a .npy file.
- `npz_load(fname)` will load a .npz and return a dictionary of NpyArray structues.
- `npz_load(fname,varname)` will load and return the NpyArray for data varname from the specified .npz file.

The data structure for loaded data is below.
Data is accessed via the `data<T>()`-method, which returns a pointer of the specified type (which must match the underlying datatype of the data).
The array shape and word size are read from the npy header.

```c++
struct NpyArray {
std::vector<size_t> shape;
size_t word_size;
template<typename T> T* data();
};
```
See [example1.cpp](example1.cpp) for examples of how to use the library. example1 will also be build during cmake installation.
Loading

0 comments on commit 3e2ce54

Please sign in to comment.