Browse Source

Added optional environment var to build yaflpy with 32(default) or 64 bit yaflFloat.

shkolnick-kun 2 years ago
parent
commit
972fe4a579
6 changed files with 73 additions and 36 deletions
  1. 1 1
      setup.cfg
  2. 19 3
      setup.py
  3. 0 1
      src/yafl.c
  4. 15 5
      src/yaflpy/yafl_config.h
  5. 34 24
      src/yaflpy/yaflpy.pyx
  6. 4 2
      tests/src/yaflpy_compile_unscented.py

+ 1 - 1
setup.cfg

@@ -8,7 +8,7 @@ long_description = file: README.md
 long_description_content_type = text/markdown
 
 license = Apache License, Version 2.0
-license_file = file: LICENSE
+license_file = LICENSE
 
 author = anonimous
 author_email = shkolnick-kun@gmail.com

+ 19 - 3
setup.py

@@ -50,6 +50,7 @@ from setuptools import Extension, setup
 
 try:
     from Cython.Build import cythonize
+    from Cython.Compiler.Main import default_options
 except ImportError:
     cythonize = None
 
@@ -74,10 +75,21 @@ SETUP_DIR = dirname(__file__)
 SRC_DIR   = join(SETUP_DIR, "src")
 EXT_DIR   = join(SRC_DIR, EXT_NAME)
 
+DEFINES = [("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")]
+CT_ENV  = {"YAFLPY_USE_64_BIT":False}
+
+if int(os.getenv("YAFL_64", 0)):
+    #Override defaults in 64 bit case
+    print('YAFL: The extensuion will be compiled in 64 bit variant!!!')
+    CT_ENV   = {"YAFLPY_USE_64_BIT":True}
+    DEFINES += [("YAFL_USE_64_BIT",1)]
+
 extensions = [
     Extension(EXT_NAME, [join(EXT_DIR, EXT_NAME + ".pyx")],
-        include_dirs=[numpy.get_include(), SRC_DIR, EXT_DIR],
-        define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")])
+              language='c',
+              include_dirs=[numpy.get_include(), SRC_DIR, EXT_DIR],
+              define_macros=DEFINES
+              )
     ]
 
 if not isfile(join(EXT_DIR, EXT_NAME + ".c")):
@@ -86,7 +98,11 @@ else:
     CYTHONIZE = bool(int(os.getenv("CYTHONIZE", 0))) and cythonize is not None
 
 if CYTHONIZE:
-    compiler_directives = {"language_level": 3, "embedsignature": True}
+    default_options["compile_time_env"] = CT_ENV
+    compiler_directives = {
+        "language_level": 3,
+        "embedsignature": True
+        }
     extensions = cythonize(extensions, compiler_directives=compiler_directives)
 else:
     extensions = no_cythonize(extensions)

+ 0 - 1
src/yafl.c

@@ -151,7 +151,6 @@ yaflStatusEn yafl_ekf_base_update(yaflKalmanBaseSt * self, yaflFloat * z, yaflKa
     }
 
     /* Decorrelate measurement noise */
-
     YAFL_TRY(status, yafl_math_ruv(_NZ,      _Y,  _UR));
     YAFL_TRY(status, yafl_math_rum(_NZ, _NX, _HY, _UR));
 

+ 15 - 5
src/yaflpy/yafl_config.h

@@ -24,13 +24,23 @@
 
 #define YAFL_LOG(...) fprintf(stderr, __VA_ARGS__)
 
-typedef double  yaflFloat;
 typedef int32_t yaflInt;
 
-#define YAFL_EPS  (1.0e-15)
-
-#define YAFL_SQRT sqrt
-#define YAFL_ABS  abs
+#ifndef YAFL_USE_64_BIT
+#   define YAFL_USE_64_BIT (0)
+#endif/*YAFL_USE_64_BIT*/
+
+#if YAFL_USE_64_BIT
+    typedef double  yaflFloat;
+#   define YAFL_EPS  (1.0e-15)
+#   define YAFL_SQRT sqrt
+#   define YAFL_ABS  fabs
+#else/*YAFL_USE_64_BIT*/
+    typedef float  yaflFloat;
+#   define YAFL_EPS  (1.0e-7)
+#   define YAFL_SQRT sqrtf
+#   define YAFL_ABS  fabsf
+#endif/*YAFL_USE_64_BIT*/
 
 #ifdef __GNUC__
 #   define YAFL_UNLIKELY(x) __builtin_expect((x), 0)

+ 34 - 24
src/yaflpy/yaflpy.pyx

@@ -23,9 +23,14 @@
 from libc cimport stdint
 
 #------------------------------------------------------------------------------
-cdef extern from "yafl_config.h":
-    ctypedef double         yaflFloat
-    ctypedef stdint.int32_t yaflInt
+IF YAFLPY_USE_64_BIT:
+    cdef extern from "yafl_config.h":
+        ctypedef double         yaflFloat
+        ctypedef stdint.int32_t yaflInt
+ELSE:
+    cdef extern from "yafl_config.h":
+        ctypedef float          yaflFloat
+        ctypedef stdint.int32_t yaflInt
 
 #------------------------------------------------------------------------------
 cdef extern from "yafl_math.c":
@@ -314,6 +319,11 @@ import scipy.stats as st
 import traceback as tb
 
 #==============================================================================
+IF YAFLPY_USE_64_BIT:
+    NP_DTYPE = np.float64
+ELSE:
+    NP_DTYPE = np.float32
+
 #Status masks
 ST_MSK_REGULARIZED  = YAFL_ST_MSK_REGULARIZED
 ST_MSK_GLITCH_SMALL = YAFL_ST_MSK_GLITCH_SMALL
@@ -454,38 +464,38 @@ cdef class yaflKalmanBase:
             self._residual_z = None
 
         # Allocate memories and setup the rest of c_self
-        self._z  = np.zeros((dim_z,), dtype=np.float64)
+        self._z  = np.zeros((dim_z,), dtype=NP_DTYPE)
         self.v_z = self._z
 
-        self._x  = np.zeros((dim_x,), dtype=np.float64)
+        self._x  = np.zeros((dim_x,), dtype=NP_DTYPE)
         self.v_x = self._x
         self.c_self.base.base.x = &self.v_x[0]
 
-        self._y  = np.zeros((dim_z,), dtype=np.float64)
+        self._y  = np.zeros((dim_z,), dtype=NP_DTYPE)
         self.v_y = self._y
         self.c_self.base.base.y = &self.v_y[0]
 
-        self._Up  = np.zeros((_U_sz(dim_x),), dtype=np.float64)
+        self._Up  = np.zeros((_U_sz(dim_x),), dtype=NP_DTYPE)
         self.v_Up = self._Up
         self.c_self.base.base.Up = &self.v_Up[0]
 
-        self._Dp  = np.ones((dim_x,), dtype=np.float64)
+        self._Dp  = np.ones((dim_x,), dtype=NP_DTYPE)
         self.v_Dp = self._Dp
         self.c_self.base.base.Dp = &self.v_Dp[0]
 
-        self._Uq  = np.zeros((_U_sz(dim_x),), dtype=np.float64)
+        self._Uq  = np.zeros((_U_sz(dim_x),), dtype=NP_DTYPE)
         self.v_Uq = self._Uq
         self.c_self.base.base.Uq = &self.v_Uq[0]
 
-        self._Dq  = np.ones((dim_x,), dtype=np.float64)
+        self._Dq  = np.ones((dim_x,), dtype=NP_DTYPE)
         self.v_Dq = self._Dq
         self.c_self.base.base.Dq = &self.v_Dq[0]
 
-        self._Ur  = np.zeros((_U_sz(dim_z),), dtype=np.float64)
+        self._Ur  = np.zeros((_U_sz(dim_z),), dtype=NP_DTYPE)
         self.v_Ur = self._Ur
         self.c_self.base.base.Ur = &self.v_Ur[0]
 
-        self._Dr  = np.ones((dim_z,), dtype=np.float64)
+        self._Dr  = np.ones((dim_z,), dtype=NP_DTYPE)
         self.v_Dr = self._Dr
         self.c_self.base.base.Dr = &self.v_Dr[0]
 
@@ -729,15 +739,15 @@ cdef class yaflExtendedBase(yaflKalmanBase):
 
 
         # Allocate memories and setup the rest of c_self
-        self._H  = np.zeros((dim_z, dim_x), dtype=np.float64)
+        self._H  = np.zeros((dim_z, dim_x), dtype=NP_DTYPE)
         self.v_H = self._H
         self.c_self.base.ekf.H = &self.v_H[0, 0]
 
-        self._W  = np.zeros((dim_x, 2 * dim_x), dtype=np.float64)
+        self._W  = np.zeros((dim_x, 2 * dim_x), dtype=NP_DTYPE)
         self.v_W = self._W
         self.c_self.base.ekf.W = &self.v_W[0,0]
 
-        self._D  = np.ones((2 * dim_x,), dtype=np.float64)
+        self._D  = np.ones((2 * dim_x,), dtype=NP_DTYPE)
         self.v_D = self._D
         self.c_self.base.ekf.D = &self.v_D[0]
 
@@ -1140,32 +1150,32 @@ cdef class yaflUnscentedBase(yaflKalmanBase):
         # Sigma points and weights
         pnum = _points.pnum
 
-        self._wm  = np.zeros((pnum,), dtype=np.float64)
+        self._wm  = np.zeros((pnum,), dtype=NP_DTYPE)
         self.v_wm = self._wm
         self.c_self.base.ukf.wm = &self.v_wm[0]
 
-        self._wc  = np.zeros((pnum,), dtype=np.float64)
+        self._wc  = np.zeros((pnum,), dtype=NP_DTYPE)
         self.v_wc = self._wc
         self.c_self.base.ukf.wc = &self.v_wc[0]
 
-        self._sigmas_x  = np.zeros((pnum, dim_x), dtype=np.float64)
+        self._sigmas_x  = np.zeros((pnum, dim_x), dtype=NP_DTYPE)
         self.v_sigmas_x = self._sigmas_x
         self.c_self.base.ukf.sigmas_x = &self.v_sigmas_x[0, 0]
 
-        self._sigmas_z  = np.zeros((pnum, dim_z), dtype=np.float64)
+        self._sigmas_z  = np.zeros((pnum, dim_z), dtype=NP_DTYPE)
         self.v_sigmas_z = self._sigmas_z
         self.c_self.base.ukf.sigmas_z = &self.v_sigmas_z[0, 0]
 
         #Rest of rhe UKF
-        self._zp  = np.zeros((dim_z,), dtype=np.float64)
+        self._zp  = np.zeros((dim_z,), dtype=NP_DTYPE)
         self.v_zp = self._zp
         self.c_self.base.ukf.zp = &self.v_zp[0]
 
-        self._Pzx  = np.zeros((dim_z, dim_x), dtype=np.float64)
+        self._Pzx  = np.zeros((dim_z, dim_x), dtype=NP_DTYPE)
         self.v_Pzx = self._Pzx
         self.c_self.base.ukf.Pzx = &self.v_Pzx[0, 0]
 
-        self._Sx  = np.zeros((dim_x,), dtype=np.float64)
+        self._Sx  = np.zeros((dim_x,), dtype=NP_DTYPE)
         self.v_Sx = self._Sx
         self.c_self.base.ukf.Sx = &self.v_Sx[0]
 
@@ -1524,11 +1534,11 @@ cdef class Unscented(yaflUnscentedBase):
 
         super().__init__(dim_x, dim_z, dt, hx, fx, points, **kwargs)
 
-        self._Us  = np.zeros((_U_sz(dim_z),), dtype=np.float64)
+        self._Us  = np.zeros((_U_sz(dim_z),), dtype=NP_DTYPE)
         self.v_Us = self._Us
         self.c_self.base.ukf_full.Us = &self.v_Us[0]
 
-        self._Ds  = np.ones((dim_z,), dtype=np.float64)
+        self._Ds  = np.ones((dim_z,), dtype=NP_DTYPE)
         self.v_Ds = self._Ds
         self.c_self.base.ukf_full.Ds = &self.v_Ds[0]
 

+ 4 - 2
tests/src/yaflpy_compile_unscented.py

@@ -22,6 +22,7 @@ import scipy.stats
 import sys
 import time
 
+"""
 sys.path.insert(0,'../../src/yaflpy')
 
 pyximport.install(
@@ -30,10 +31,11 @@ pyximport.install(
     reload_support=True,
     language_level=3,
     setup_args={
-        'include_dirs': [np.get_include(), '../../src', '../../src/yaflpy'],
+        'include_dirs' : [np.get_include(), '../../src', '../../src/yaflpy'],
+        'define_macros':[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")]
         }
     )
-
+"""
 from yaflpy import MerweSigmaPoints as SP
 #from yaflpy import Unscented as KF
 #from yaflpy import UnscentedAdaptive as KF