Skip to content

Commit

Permalink
tweak expected size variables; updated perf suite
Browse files Browse the repository at this point in the history
  • Loading branch information
jrenaud90 committed Oct 5, 2023
1 parent 6efb520 commit b3d800b
Show file tree
Hide file tree
Showing 19 changed files with 88 additions and 74 deletions.
71 changes: 41 additions & 30 deletions Benchmarks/CyRK - SciPy Comparison.ipynb

Large diffs are not rendered by default.

Binary file modified Benchmarks/CyRK_CySolver.pdf
Binary file not shown.
Binary file added Benchmarks/CyRK_SciPy_Compare_predprey_v0-8-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file modified Benchmarks/CyRK_cyrk_ode.pdf
Binary file not shown.
Binary file modified Benchmarks/CyRK_numba.pdf
Binary file not shown.
Binary file modified Benchmarks/SciPy.pdf
Binary file not shown.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 2023

#### v0.8.3 (TBD)
#### v0.8.3 (2023-10-05)

New Features:
- Added tests to check if memory access violations can occur when `CySolver` is resolved many times.
Expand All @@ -17,6 +17,7 @@ Performance:
Other Changes:
- Moved some common constants for both `CySolver` and `cyrk_ode` out of their files and into `cy.common`.
- Added more meaningful memory error messages to `CySolver`.
- Better future-proofed package structure (mainifests, gitignores, etc.).

#### v0.8.2 (2023-09-25)

Expand Down
3 changes: 2 additions & 1 deletion CyRK/cy/common.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ cdef Py_ssize_t MAX_SIZET_SIZE
cdef double CPU_CACHE_SIZE
cdef double EXPECTED_SIZE_DBL
cdef double EXPECTED_SIZE_DBLCMPLX
cdef double MAX_ARRAY_PREALLOCATE_SIZE_DBL
cdef double MAX_ARRAY_PREALLOCATE_SIZE_DBLCMPLX
cdef double MIN_ARRAY_PREALLOCATE_SIZE
cdef double MAX_ARRAY_PREALLOCATE_SIZE
cdef double ARRAY_PREALLOC_TABS_SCALE
cdef double ARRAY_PREALLOC_RTOL_SCALE

Expand Down
13 changes: 6 additions & 7 deletions CyRK/cy/common.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ cdef Py_ssize_t MAX_SIZET_SIZE = <Py_ssize_t>(0.95 * SIZE_MAX)
cdef Py_ssize_t MAX_INT_SIZE = <Py_ssize_t>(0.95 * INT32_MAX)

# # Memory management constants
# Assume that a cpu has a L1 of 300KB. Say that this progam will have access to 50% of that total.
cdef double CPU_CACHE_SIZE = 0.5 * 300_000.
# Assume that a cpu has a L1 of 300KB. Say that this progam will have access to 75% of that total.
cdef double CPU_CACHE_SIZE = 0.75 * 300_000.
# Number of entities we can fit into that size is based on the size of double (or double complex)
cdef double EXPECTED_SIZE_DBL = CPU_CACHE_SIZE / sizeof(double)
cdef double EXPECTED_SIZE_DBLCMPLX = CPU_CACHE_SIZE / sizeof(double complex)
cdef double MIN_ARRAY_PREALLOCATE_SIZE = 100.
cdef double MAX_ARRAY_PREALLOCATE_SIZE = 100_000.
cdef double MAX_ARRAY_PREALLOCATE_SIZE_DBL = 600_000.
cdef double MAX_ARRAY_PREALLOCATE_SIZE_DBLCMPLX = 300_000.
cdef double MIN_ARRAY_PREALLOCATE_SIZE = 10.
cdef double ARRAY_PREALLOC_TABS_SCALE = 1000. # A delta_t_abs higher than this value will start to grow array size.
cdef double ARRAY_PREALLOC_RTOL_SCALE = 1.0e-6 # A rtol lower than this value will start to grow array size.
cdef double ARRAY_PREALLOC_RTOL_SCALE = 1.0e-5 # A rtol lower than this value will start to grow array size.


cdef void interpolate(
Expand Down
31 changes: 17 additions & 14 deletions CyRK/cy/cyrk.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ from libc.math cimport sqrt, fabs, nextafter, fmax, fmin, NAN
from CyRK.rk.rk cimport find_rk_properties
from CyRK.cy.common cimport double_numeric, interpolate, SAFETY, MIN_FACTOR, MAX_FACTOR, MAX_STEP, INF, EPS, \
EPS_10, EPS_100, \
MAX_INT_SIZE, EXPECTED_SIZE_DBL, EXPECTED_SIZE_DBLCMPLX, MIN_ARRAY_PREALLOCATE_SIZE, MAX_ARRAY_PREALLOCATE_SIZE, \
MAX_INT_SIZE, MIN_ARRAY_PREALLOCATE_SIZE, MAX_ARRAY_PREALLOCATE_SIZE_DBL, MAX_ARRAY_PREALLOCATE_SIZE_DBLCMPLX, \
ARRAY_PREALLOC_TABS_SCALE, ARRAY_PREALLOC_RTOL_SCALE


Expand Down Expand Up @@ -284,30 +284,33 @@ def cyrk_ode(
# Expected size of output arrays.
cdef Py_ssize_t expected_size_to_use, num_concats, current_size
cdef double temp_expected_size
cdef double max_expected
if expected_size == 0:
# CySolver will attempt to guess on a best size for the arrays.
# Pick starting value that makes sense with the anticipated CPU cache and the data type's size.
if y_is_complex:
temp_expected_size = EXPECTED_SIZE_DBLCMPLX
else:
temp_expected_size = EXPECTED_SIZE_DBL
# Then there are going to be (y + num_extra) number of doubles
if capture_extra:
temp_expected_size = temp_expected_size / (y_size_dbl + num_extra)
else:
temp_expected_size = temp_expected_size / y_size_dbl
# Pick starting value that works with most problems
temp_expected_size = 500.0
# If t_delta_abs is very large or rtol is very small, then we may need more.
temp_expected_size = \
fmax(
temp_expected_size,
fmax(
fmax(1., t_delta_abs / ARRAY_PREALLOC_TABS_SCALE),
fmax(1., (ARRAY_PREALLOC_RTOL_SCALE / rtol_min))
t_delta_abs / ARRAY_PREALLOC_TABS_SCALE,
ARRAY_PREALLOC_RTOL_SCALE / rtol_min
)
)
# Fix values that are very small/large
temp_expected_size = fmax(temp_expected_size, MIN_ARRAY_PREALLOCATE_SIZE)
temp_expected_size = fmin(temp_expected_size, MAX_ARRAY_PREALLOCATE_SIZE)

if double_numeric is cython.double:
max_expected = MAX_ARRAY_PREALLOCATE_SIZE_DBL
else:
max_expected = MAX_ARRAY_PREALLOCATE_SIZE_DBLCMPLX
if capture_extra:
max_expected /= (y_size + num_extra)
else:
max_expected /= y_size

temp_expected_size = fmin(temp_expected_size, max_expected)
# Store result as int
expected_size_to_use = <Py_ssize_t>temp_expected_size
else:
Expand Down
22 changes: 11 additions & 11 deletions CyRK/cy/cysolver.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ np.import_array()

from CyRK.rk.rk cimport find_rk_properties
from CyRK.cy.common cimport interpolate, SAFETY, MIN_FACTOR, MAX_FACTOR, MAX_STEP, INF, EPS, EPS_10, EPS_100, \
MAX_INT_SIZE, EXPECTED_SIZE_DBL, MIN_ARRAY_PREALLOCATE_SIZE, MAX_ARRAY_PREALLOCATE_SIZE, \
MAX_INT_SIZE, MIN_ARRAY_PREALLOCATE_SIZE, MAX_ARRAY_PREALLOCATE_SIZE_DBL, \
ARRAY_PREALLOC_TABS_SCALE, ARRAY_PREALLOC_RTOL_SCALE

cdef (double, double) EMPTY_T_SPAN = (NAN, NAN)
Expand Down Expand Up @@ -414,27 +414,27 @@ cdef class CySolver:

# Expected size of output arrays.
cdef double temp_expected_size
cdef double max_expected
if expected_size == 0:
# CySolver will attempt to guess on a best size for the arrays.
# Pick starting value that makes sense with the anticipated CPU cache and the data type's size.
temp_expected_size = EXPECTED_SIZE_DBL
# Then there are going to be (y + num_extra) number of doubles
if self.capture_extra:
temp_expected_size = temp_expected_size / (self.y_size_dbl + self.num_extra)
else:
temp_expected_size = temp_expected_size / self.y_size_dbl
# Pick starting value that works with most problems
temp_expected_size = 500.0
# If t_delta_abs is very large or rtol is very small, then we may need more.
temp_expected_size = \
fmax(
temp_expected_size,
fmax(
fmax(1., self.t_delta_abs / ARRAY_PREALLOC_TABS_SCALE),
fmax(1., (ARRAY_PREALLOC_RTOL_SCALE / rtol_min))
self.t_delta_abs / ARRAY_PREALLOC_TABS_SCALE,
ARRAY_PREALLOC_RTOL_SCALE / rtol_min
)
)
# Fix values that are very small/large
temp_expected_size = fmax(temp_expected_size, MIN_ARRAY_PREALLOCATE_SIZE)
temp_expected_size = fmin(temp_expected_size, MAX_ARRAY_PREALLOCATE_SIZE)
if capture_extra:
max_expected = MAX_ARRAY_PREALLOCATE_SIZE_DBL / (self.y_size + self.num_extra)
else:
max_expected = MAX_ARRAY_PREALLOCATE_SIZE_DBL / self.y_size
temp_expected_size = fmin(temp_expected_size, max_expected)
# Store result as int
self.expected_size = <Py_ssize_t>temp_expected_size
else:
Expand Down
8 changes: 2 additions & 6 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
include CyRK/array/*.pyx
include CyRK/array/*.pxd
include CyRK/rk/*.pyx
include CyRK/rk/*.pxd
include CyRK/cy/*.pyx
include CyRK/cy/*.pxd
global-include *.pxd
global-include *.pyx
1 change: 1 addition & 0 deletions Performance/cyrk_performance-DOP853.csv
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ CyRK Version, Run-on Date, cython (avg), cython (std),CySolver (avg),CySolver (s
0.7.0a6, 28/08/2023 15:54:30, 1.0249, 0.0023, 0.1009, 0.0001, 0.1745, 0.0003, 10.0953, 0.0187, 0.5086, 0.0004, 1.6063, 0.0157, 0.4882, 0.0005, 0.0831, 0.0001, 0.1019, 0.0039, 4.6144, 0.0984, 0.3383, 0.0006, 0.7983, 0.0067, 1.4988, 0.0178, 0.1394, 0.0006, 0.2681, 0.0004, 20.1680, 0.0990, 1.2133, 0.0049, 3.4577, 0.0201, 1.7582, 0.0131, 0.1490, 0.0031, 0.5073, 0.0059, 24.8074, 0.4213, 1.2755, 0.0208, 6.5607, 0.0177
0.7.1, 30/08/2023 13:06:08, 1.0108, 0.0108, 0.0930, 0.0004, 0.1709, 0.0004, 10.0508, 0.0336, 0.4942, 0.0005, 1.5619, 0.0081, 0.4779, 0.0045, 0.0741, 0.0001, 0.0993, 0.0009, 4.5178, 0.0105, 0.3211, 0.0018, 0.7910, 0.0070, 1.4697, 0.0163, 0.1282, 0.0002, 0.2644, 0.0022, 20.0282, 0.2074, 1.1535, 0.0006, 3.3897, 0.0012, 1.7311, 0.0088, 0.1364, 0.0027, 0.4774, 0.0019, 23.5503, 0.0176, 1.1954, 0.0037, 6.4767, 0.0802
0.8.0, 06/09/2023 15:03:15, 0.9615, 0.0011, 0.0561, 0.0001, 0.1602, 0.0004, 9.8846, 0.1049, 0.4796, 0.0010, 1.4545, 0.0154, 0.4413, 0.0005, 0.0376, 0.0001, 0.0953, 0.0001, 4.3959, 0.0170, 0.2890, 0.0007, 0.7862, 0.0168, 1.4213, 0.0046, 0.0937, 0.0005, 0.2464, 0.0012, 19.5317, 0.0414, 1.1684, 0.0054, 3.1531, 0.0143, 1.6563, 0.0055, 0.0989, 0.0018, 0.4797, 0.0064, 22.9818, 0.1217, 1.2122, 0.0250, 6.4178, 0.0221
0.8.3, 05/10/2023 16:59:21, 0.9504, 0.0037, 0.0551, 0.0002, 0.1624, 0.0001, 9.9624, 0.2841, 0.4721, 0.0009, 1.4638, 0.0075, 0.4286, 0.0025, 0.0381, 0.0012, 0.0953, 0.0002, 4.2439, 0.0103, 0.2896, 0.0004, 0.7662, 0.0078, 1.3670, 0.0023, 0.0912, 0.0015, 0.2437, 0.0042, 19.0453, 0.0910, 1.1357, 0.0015, 3.0867, 0.0039, 1.6214, 0.0179, 0.0931, 0.0003, 0.4685, 0.0014, 22.4041, 0.1208, 1.1700, 0.0008, 6.2697, 0.0249
1 change: 1 addition & 0 deletions Performance/cyrk_performance-RK23.csv
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ CyRK Version, Run-on Date, cython (avg), cython (std),CySolver (avg),CySolver (s
0.7.0a6, 28/08/2023 15:52:13, 4.5831, 0.0535, 0.3040, 0.0014, 0.8701, 0.0066, 44.4575, 0.1312, 2.5780, 0.2150, 10.0452, 0.5722, 2.8942, 0.0355, 0.2581, 0.0066, 0.6178, 0.0021, 28.1092, 0.4533, 2.0150, 0.0244, 6.2654, 0.0942, 4.8829, 0.0073, 0.3678, 0.0006, 1.0106, 0.0144, 83.3562, 0.5923, 5.4591, 0.0302, 22.3938, 0.7174, 5.8152, 0.0915, 0.3946, 0.0008, 1267.8939, 2192.9856, 102.3280, 1.0445, 7.0396, 0.2830, 36.1755, 0.3725
0.7.1, 30/08/2023 13:03:46, 4.5783, 0.0691, 0.2913, 0.0003, 0.8521, 0.0037, 44.6829, 0.8883, 2.4223, 0.0259, 9.0549, 0.2553, 2.8797, 0.0312, 0.2429, 0.0002, 0.6060, 0.0057, 27.9517, 0.2481, 1.9483, 0.0099, 5.9059, 0.0472, 4.7863, 0.0076, 0.3516, 0.0008, 0.9815, 0.0151, 81.9632, 0.6779, 5.3389, 0.0114, 20.6745, 0.4038, 5.6843, 0.0111, 0.3861, 0.0015, 1229.8100, 2127.0902, 99.4341, 0.2647, 6.5017, 0.2603, 35.6680, 0.7413
0.8.0, 06/09/2023 15:00:58, 4.4794, 0.0786, 0.2262, 0.0003, 0.8199, 0.0021, 43.1476, 0.0854, 2.1466, 0.0018, 10.1946, 0.1933, 2.7552, 0.0105, 0.1866, 0.0004, 0.5903, 0.0024, 26.9694, 0.1524, 1.7797, 0.0257, 5.9678, 0.0812, 4.7003, 0.0324, 0.2855, 0.0005, 0.9508, 0.0055, 80.1011, 0.3480, 4.8504, 0.0219, 20.8530, 0.2461, 5.5239, 0.0051, 0.3178, 0.0014, 1270.6912, 2197.9259, 95.6354, 0.1723, 5.4006, 0.0797, 35.0990, 0.8415
0.8.3, 05/10/2023 16:57:04, 4.4364, 0.0974, 0.2495, 0.0005, 0.8070, 0.0028, 42.8127, 0.0596, 2.4683, 0.1174, 9.0078, 0.0796, 2.6867, 0.0297, 0.1936, 0.0001, 0.5844, 0.0005, 27.1970, 0.8808, 1.8609, 0.0142, 6.0141, 0.1670, 4.6906, 0.1724, 0.3072, 0.0008, 0.9267, 0.0016, 78.5589, 0.3792, 5.1886, 0.0043, 20.6397, 0.8234, 5.4013, 0.0234, 0.3169, 0.0029, 1258.9088, 2177.5555, 94.7052, 0.3245, 5.3609, 0.0110, 34.2569, 0.6742
1 change: 1 addition & 0 deletions Performance/cyrk_performance-RK45.csv
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ CyRK Version, Run-on Date, cython (avg), cython (std),CySolver (avg),CySolver (s
0.7.0a6, 28/08/2023 15:53:37, 1.2422, 0.0017, 0.1071, 0.0001, 0.2161, 0.0008, 12.0481, 0.0702, 0.5705, 0.0113, 2.0538, 0.0396, 0.8788, 0.0042, 0.1046, 0.0002, 0.1716, 0.0003, 8.3498, 0.0205, 0.5435, 0.0008, 1.4974, 0.0019, 1.6924, 0.0167, 0.1459, 0.0001, 0.3222, 0.0085, 24.6399, 0.0578, 1.3955, 0.0234, 4.2863, 0.0472, 1.9800, 0.0062, 0.1529, 0.0005, 0.5698, 0.0008, 29.2582, 0.4323, 1.4362, 0.0020, 8.3727, 0.1087
0.7.1, 30/08/2023 13:05:11, 1.2473, 0.0174, 0.0985, 0.0005, 0.2110, 0.0020, 12.1557, 0.1808, 0.5338, 0.0012, 1.9231, 0.0144, 0.8757, 0.0034, 0.0959, 0.0000, 0.1693, 0.0015, 8.4225, 0.0678, 0.5282, 0.0034, 1.4849, 0.0082, 1.6579, 0.0112, 0.1361, 0.0009, 0.3019, 0.0024, 24.1373, 0.0868, 1.3549, 0.0143, 4.1917, 0.0442, 1.9731, 0.0101, 0.1435, 0.0012, 0.5592, 0.0011, 28.7442, 0.0958, 1.3859, 0.0005, 8.1105, 0.0455
0.8.0, 06/09/2023 15:02:20, 1.2080, 0.0171, 0.0582, 0.0001, 0.2023, 0.0008, 11.8032, 0.0256, 0.4807, 0.0012, 1.8475, 0.0119, 0.8318, 0.0034, 0.0569, 0.0002, 0.1659, 0.0003, 8.1177, 0.0578, 0.4684, 0.0018, 1.4517, 0.0058, 1.6145, 0.0023, 0.0921, 0.0005, 0.2928, 0.0011, 23.6490, 0.0748, 1.2090, 0.0118, 4.0150, 0.0179, 1.8902, 0.0034, 0.0969, 0.0009, 0.5533, 0.0009, 27.6976, 0.0946, 1.2947, 0.0416, 7.9394, 0.0140
0.8.3, 05/10/2023 16:58:27, 1.1821, 0.0195, 0.0597, 0.0001, 0.2044, 0.0013, 11.6865, 0.0342, 0.5119, 0.0012, 1.8802, 0.0457, 0.8004, 0.0018, 0.0574, 0.0001, 0.1661, 0.0008, 7.8608, 0.0145, 0.4825, 0.0004, 1.4451, 0.0078, 1.5689, 0.0075, 0.0986, 0.0009, 0.2891, 0.0002, 23.1983, 0.0209, 1.3082, 0.0034, 4.0478, 0.0406, 1.8522, 0.0163, 0.1005, 0.0002, 0.5495, 0.0016, 27.1767, 0.0465, 1.3709, 0.0245, 7.9300, 0.1021
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

---

<a href="https://github.com/jrenaud90/CyRK/releases"><img src="https://img.shields.io/badge/CyRK-0.8.1 Alpha-orange" alt="CyRK Version 0.8.1 Alpha" /></a>
<a href="https://github.com/jrenaud90/CyRK/releases"><img src="https://img.shields.io/badge/CyRK-0.8.3 Alpha-orange" alt="CyRK Version 0.8.1 Alpha" /></a>


**Runge-Kutta ODE Integrator Implemented in Cython and Numba**

CyRK provides fast integration tools to solve systems of ODEs using an adaptive time stepping scheme. CyRK can accept differential equations that are written in pure Python, njited numba, or cython-based cdef classes. These kinds of functions are generally easier to implement than pure c functions. Using CyRK can speed up development time while not making a huge sacrifice when it comes to performance.
CyRK provides fast integration tools to solve systems of ODEs using an adaptive time stepping scheme. CyRK can accept differential equations that are written in pure Python, njited numba, or cython-based cdef class methods. These kinds of functions are generally easier to implement than pure c functions and can be used in existing Python software. Using CyRK can speed up development time while avoiding the slow performance that comes with using pure Python-based solvers like SciPy.

The purpose of this package is to provide some
functionality of [scipy's solve_ivp](https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html) with greatly improved performance.
Expand All @@ -28,7 +28,7 @@ The [cython](https://cython.org/) `CySolver` class that works with cython-based
An additional benefit of the two cython implementations is that they are pre-compiled. This avoids most of the start-up performance hit experienced by just-in-time compilers like numba.


<img style="text-align: center" src="https://github.com/jrenaud90/CyRK/blob/main/Benchmarks/CyRK_SciPy_Compare_predprey_v0-8-0.png" alt="CyRK Performance Graphic" />
<img style="text-align: center" src="https://github.com/jrenaud90/CyRK/blob/main/Benchmarks/CyRK_SciPy_Compare_predprey_v0-8-3.png" alt="CyRK Performance Graphic" />

## Installation

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name='CyRK'
version = '0.8.3a0.dev6'
version = '0.8.3'
description='Runge-Kutta ODE Integrator Implemented in Cython and Numba.'
authors= [
{name = 'Joe P. Renaud', email = '[email protected]'}
Expand Down

0 comments on commit b3d800b

Please sign in to comment.