Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit 95a57bc

Browse files
committed
Initial refactor
1 parent 9e4dce9 commit 95a57bc

15 files changed

+1840
-0
lines changed

.github/workflows/cmake.yml

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
name: Build and Test
2+
3+
on:
4+
pull_request:
5+
push:
6+
release:
7+
types:
8+
- created
9+
10+
env:
11+
BUILD_TYPE: Release
12+
13+
jobs:
14+
build:
15+
runs-on: ${{ matrix.os }}
16+
strategy:
17+
fail-fast: false
18+
matrix:
19+
build_type: [Release]
20+
os: [ubuntu-18.04, windows-latest]
21+
include:
22+
- os: ubuntu-18.04
23+
build_type: Debug
24+
- os: windows-latest
25+
build_type: Debug
26+
steps:
27+
- uses: actions/checkout@v2
28+
with:
29+
fetch-depth: 0
30+
- if: contains(matrix.os, 'ubuntu')
31+
name: Get Dependencies
32+
run: |
33+
sudo apt-get update
34+
sudo apt-get install p7zip-full build-essential zlib1g-dev freeglut3-dev liblapacke-dev libopenblas-dev libatlas-base-dev libpcap-dev python3 python3-pip apt-transport-https dotnet-sdk-3.1 libpcap-dev python-virtualenv ninja-build libeigen3-dev ${{ matrix.deps }}
35+
- uses: actions/setup-python@v2
36+
with:
37+
python-version: '3.x'
38+
- name: Setup windows flags
39+
if: contains(matrix.os, 'windows') && matrix.build_type == 'Release'
40+
id: flags
41+
run: echo "::set-output name=SETUP_PY_FLAGS::-G 'Visual Studio 16 2019'"
42+
- name: Get mac dependencies
43+
if: contains(matrix.os, 'macos')
44+
run: |
45+
brew install freeglut lapack openblas
46+
- name: Create Build Environment
47+
# Some projects don't allow in-source building, so create a separate build directory
48+
# We'll use this as our working directory for all subsequent commands
49+
run: cmake -E make_directory ${{runner.workspace}}/build
50+
51+
- name: Configure CMake
52+
shell: bash
53+
working-directory: ${{runner.workspace}}/build
54+
run: cmake $GITHUB_WORKSPACE -DUSE_EIGEN=On -DDOWNLOAD_EIGEN=On -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DENABLE_TESTS=ON -DCMAKE_INSTALL_PREFIX=install_root ${{matrix.config_flags}}
55+
56+
- name: Build
57+
working-directory: ${{runner.workspace}}/build
58+
shell: bash
59+
# Execute the build. You can specify a specific target with "--target <NAME>"
60+
run: cmake --build . --config $BUILD_TYPE -v
61+
62+
- name: Install
63+
working-directory: ${{runner.workspace}}/build
64+
shell: bash
65+
# Execute the build. You can specify a specific target with "--target <NAME>"
66+
run: cmake --build . --config $BUILD_TYPE --target install
67+
68+
- name: Set bundle name
69+
id: bundle
70+
run: echo "::set-output name=BUNDLE_FILE_NAME::cnmatrix-$(git describe --tags)-${{ matrix.os }}.zip"
71+
72+
- name: Bundle
73+
if: matrix.build_type == 'Release'
74+
working-directory: ${{runner.workspace}}/cnmatrix
75+
run: 7z a ${{runner.workspace}}/build/${{ steps.bundle.outputs.BUNDLE_FILE_NAME }} ${{runner.workspace}}/build/install_root/*
76+
77+
- uses: actions/upload-artifact@v2
78+
name: Upload
79+
if: matrix.build_type == 'Release'
80+
with:
81+
name: cnmatrix-${{ matrix.os }}-${{ matrix.build_type }}
82+
path: |
83+
${{runner.workspace}}/build/${{steps.bundle.outputs.BUNDLE_FILE_NAME}}
84+
- name: Get release
85+
id: get_release
86+
if: github.event_name == 'release'
87+
uses: bruceadams/[email protected]
88+
env:
89+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
90+
91+
- name: Upload Release Asset
92+
id: upload-release-asset
93+
continue-on-error: true
94+
if: github.event_name == 'release'
95+
uses: actions/upload-release-asset@v1
96+
env:
97+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
98+
with:
99+
upload_url: ${{ steps.get_release.outputs.upload_url }}
100+
asset_path: ${{runner.workspace}}/build/${{steps.bundle.outputs.BUNDLE_FILE_NAME}}
101+
asset_name: ${{steps.bundle.outputs.BUNDLE_FILE_NAME}}
102+
asset_content_type: application/zip
103+
104+
- name: Test
105+
working-directory: ${{runner.workspace}}/build
106+
shell: bash
107+
# Execute tests defined by the CMake configuration.
108+
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
109+
run: ctest -C $BUILD_TYPE --output-on-failure -j30

.gitignore

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Linux
2+
.idea
3+
*.o
4+
*.so
5+
lib
6+
cmake-build-*
7+
build*
8+
9+
# Temp files
10+
*~
11+
*/\#*\#
12+
\#*\#
13+
14+
15+
# Windows specific
16+
*.dll
17+
*.exe
18+
obj/
19+
bin/
20+
x64/
21+
packages
22+
.vs/
23+
Debug/
24+
Release/
25+
*.VC.db
26+
*.user
27+
*.ipch
28+
*.tlog
29+
*.d
30+
build
31+
32+
.options
33+
.zip
34+
.avi
35+
36+
*/__pycache__
37+
*.pyc
38+
39+
venv
40+
.eggs/
41+
dist/

CMakeLists.txt

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
3+
project(cnmatrix LANGUAGES C CXX)
4+
include(CTest)
5+
6+
option(ENABLE_TESTS "Enable tests" OFF)
7+
if (${ENABLE_TESTS})
8+
enable_testing()
9+
endif()
10+
11+
include(CheckIncludeFile)
12+
include (CheckSymbolExists)
13+
14+
option(DOWNLOAD_EIGEN "Download eigen if it isn't installed on the system" ON)
15+
option(USE_EIGEN "Use eigen for math operations" DOWNLOAD_EIGEN)
16+
option(USE_COLUMN_MAJOR_MATRICES "Use column major matrices for math operations" OFF)
17+
18+
find_package (Eigen3 3.3)
19+
if(NOT EIGEN3_FOUND)
20+
if(DOWNLOAD_EIGEN)
21+
find_program(GIT git)
22+
if(NOT EXISTS "${CMAKE_BINARY_DIR}/libeigen-src/Eigen")
23+
execute_process(COMMAND ${GIT} clone -b 3.4 https://gitlab.com/libeigen/eigen.git "${CMAKE_BINARY_DIR}/libeigen-src")
24+
endif()
25+
set(EIGEN3_INCLUDE_DIR "${CMAKE_BINARY_DIR}/libeigen-src/")
26+
set(USE_EIGEN ON)
27+
else()
28+
message(WARNING "Could not find eigen; falling back to other backend")
29+
set(USE_EIGEN OFF)
30+
endif()
31+
endif()
32+
33+
IF(USE_EIGEN)
34+
add_compile_definitions(USE_EIGEN)
35+
36+
if(USE_COLUMN_MAJOR_MATRICES)
37+
add_compile_definitions(CN_MATRIX_IS_COL_MAJOR)
38+
endif()
39+
ENDIF()
40+
41+
find_library(CBLAS_LIB NAMES cblas)
42+
set(USE_OPENBLAS_DEFAULT OFF)
43+
if(NOT CBLAS_LIB)
44+
set(USE_OPENBLAS_DEFAULT ON)
45+
endif()
46+
47+
option(USE_OPENBLAS "Use OpenBLAS" ${USE_OPENBLAS_DEFAULT})
48+
49+
50+
install(DIRECTORY include/cnmatrix DESTINATION include)
51+
include(GNUInstallDirs)
52+
configure_file(cnmatrix.pc.in cnmatrix.pc @ONLY)
53+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/cnmatrix.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
54+
55+
56+
57+
IF(UNIX)
58+
set(SHARED_FLAGS "-fPIC -Wall -Wno-unused-variable -Wno-switch -Wno-parentheses -Wno-missing-braces -Werror=return-type -fvisibility=hidden -Werror=vla -fno-math-errno -Werror=missing-field-initializers -Werror=pointer-arith")
59+
60+
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SHARED_FLAGS} -std=gnu99 -Werror=incompatible-pointer-types -Werror=implicit-function-declaration ")
61+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SHARED_FLAGS} -std=c++14")
62+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -rdynamic")
63+
64+
if(ENABLE_WARNINGS_AS_ERRORS)
65+
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
66+
endif()
67+
68+
ELSEIF(WIN32)
69+
add_compile_options(/wd4244 /wd4996 /wd4018 /wd4101 /wd4477 /wd4068 /wd4217)
70+
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
71+
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
72+
endif()
73+
74+
75+
add_subdirectory(src)
76+
77+
if(ENABLE_TESTS)
78+
add_subdirectory(tests)
79+
endif()
80+

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
This library provides a consistent C interface to a few matrix backends.
2+
3+
The interface itself is a little more sane than raw lapack / blas calls, and is meant to be reasonably performant for medium to large matrices. It should also be cross platform and work reasonably well on embedded low latency systems; as it consistently tries to avoid heap allocations.
4+
5+
As a caveat though; this library makes sense for C code bases, for C++ codebases it likely makes more sense to just use eigen directly.
6+
7+

cnmatrix.pc.in

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prefix=@CMAKE_INSTALL_PREFIX@
2+
exec_prefix=@CMAKE_INSTALL_PREFIX@
3+
libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@
4+
includedir=${prefix}/include/cnmatrix
5+
6+
Name: @CMAKE_PROJECT_NAME@
7+
Description: Cnmatrix
8+
Version: 0
9+
Libs: -L${libdir} -lcnmatrix @EXTRA_LIBS@
10+
Cflags: -I${includedir} -I${includedir}/redist

include/cnmatrix/cn_flt.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#ifdef CN_USE_FLOAT
4+
5+
#define FLT float
6+
#define FLT_SQRT sqrtf
7+
#define FLT_TAN tanf
8+
#define FLT_SIN sinf
9+
#define FLT_COS cosf
10+
#define FLT_ACOS acosf
11+
#define FLT_ASIN asinf
12+
#define FLT_ATAN2 atan2f
13+
#define FLT_FABS__ fabsf
14+
#define FLT_STRTO strtof
15+
16+
#define CN_FLT CN_32F
17+
#define FLT_POW powf
18+
19+
#else
20+
21+
#define CN_USE_DOUBLE 1
22+
#define FLT double
23+
#define FLT_SQRT sqrt
24+
#define FLT_TAN tan
25+
#define FLT_SIN sin
26+
#define FLT_COS cos
27+
#define FLT_ACOS acos
28+
#define FLT_ASIN asin
29+
#define FLT_ATAN2 atan2
30+
#define FLT_FABS__ fabs
31+
#define FLT_STRTO strtod
32+
33+
#endif

include/cnmatrix/cn_matrix.blas.h

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#ifdef __cplusplus
2+
extern "C" {
3+
#endif
4+
5+
#include "assert.h"
6+
#include "stdint.h"
7+
#include "stdlib.h"
8+
9+
#define SV_Error(code, msg) assert(0 && msg); // cv::error( code, msg, SV_Func, __FILE__, __LINE__ )
10+
11+
#define SV_32FC1 SV_32F
12+
#define SV_64FC1 SV_64F
13+
14+
#define SV_MAGIC_MASK 0xFFFF0000
15+
#define SV_MAT_MAGIC_VAL 0x42420000
16+
17+
#define SV_CN_MAX 512
18+
#define SV_CN_SHIFT 3
19+
#define SV_DEPTH_MAX (1 << SV_CN_SHIFT)
20+
21+
#define SV_MAT_DEPTH_MASK (SV_DEPTH_MAX - 1)
22+
#define SV_MAT_DEPTH(flags) ((flags)&SV_MAT_DEPTH_MASK)
23+
24+
#define SV_MAKETYPE(depth, cn) (SV_MAT_DEPTH(depth) + (((cn)-1) << SV_CN_SHIFT))
25+
#define SV_MAKE_TYPE SV_MAKETYPE
26+
27+
#define SV_MAT_CN_MASK ((SV_CN_MAX - 1) << SV_CN_SHIFT)
28+
#define SV_MAT_CN(flags) ((((flags)&SV_MAT_CN_MASK) >> SV_CN_SHIFT) + 1)
29+
#define SV_MAT_TYPE_MASK (SV_DEPTH_MAX * SV_CN_MAX - 1)
30+
#define SV_MAT_TYPE(flags) ((flags)&SV_MAT_TYPE_MASK)
31+
#define SV_MAT_CONT_FLAG_SHIFT 14
32+
#define SV_MAT_CONT_FLAG (1 << SV_MAT_CONT_FLAG_SHIFT)
33+
#define SV_IS_MAT_CONT(flags) ((flags)&SV_MAT_CONT_FLAG)
34+
#define SV_IS_CONT_MAT SV_IS_MAT_CONT
35+
#define SV_SUBMAT_FLAG_SHIFT 15
36+
#define SV_SUBMAT_FLAG (1 << SV_SUBMAT_FLAG_SHIFT)
37+
#define SV_IS_SUBMAT(flags) ((flags)&SV_MAT_SUBMAT_FLAG)
38+
39+
#define SV_IS_MATND_HDR(mat) ((mat) != NULL && (((const SvMat *)(mat))->type & SV_MAGIC_MASK) == SV_MATND_MAGIC_VAL)
40+
41+
#define SV_IS_MATND(mat) (SV_IS_MATND_HDR(mat) && ((const SvMat *)(mat))->data.ptr != NULL)
42+
#define SV_MATND_MAGIC_VAL 0x42430000
43+
44+
/** 0x3a50 = 11 10 10 01 01 00 00 ~ array of log2(sizeof(arr_type_elem)) */
45+
#define SV_ELEM_SIZE(type) \
46+
(SV_MAT_CN(type) << ((((sizeof(size_t) / 4 + 1) * 16384 | 0x3a50) >> SV_MAT_DEPTH(type) * 2) & 3))
47+
48+
#ifndef MIN
49+
#define MIN(a, b) ((a) > (b) ? (b) : (a))
50+
#endif
51+
52+
#ifndef MAX
53+
#define MAX(a, b) ((a) < (b) ? (b) : (a))
54+
#endif
55+
56+
/** 0x3a50 = 11 10 10 01 01 00 00 ~ array of log2(sizeof(arr_type_elem)) */
57+
#define SV_ELEM_SIZE(type) \
58+
(SV_MAT_CN(type) << ((((sizeof(size_t) / 4 + 1) * 16384 | 0x3a50) >> SV_MAT_DEPTH(type) * 2) & 3))
59+
60+
//#include "shim_types_c.h"
61+
62+
#ifdef __cplusplus
63+
}
64+
#endif

include/cnmatrix/cn_matrix.eigen.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "sv_matrix.h"
2+
#include <assert.h>
3+
#include <stdint.h>
4+
#include <stdlib.h>
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
//#define SV_MATRIX_IS_COL_MAJOR 1
11+
12+
#ifdef __cplusplus
13+
}
14+
#endif

0 commit comments

Comments
 (0)