From 0536e0ac69e99c9a5376844b2a68a846fe3a67bd Mon Sep 17 00:00:00 2001 From: Aleksei Nechaev <49936521+SkibidiProduction@users.noreply.github.com> Date: Mon, 24 Mar 2025 00:02:46 +0400 Subject: [PATCH 1/7] truncatedNormal method added (#1) * truncatedNormal method added * fix naming --- composer.json | 2 +- numpower.c | 160 +++++++++++------- php_numpower.h | 2 +- src/initializers.c | 29 ++++ src/initializers.h | 1 + stubs/numpower.stubs.php | 66 +++++--- tests/logic/002-ndarray-allclose.phpt | 8 +- tests/logic/005-ndarray-greater_equal.phpt | 8 +- tests/logic/007-ndarray-less_equal.phpt | 8 +- tests/logic/008-ndarray-not_equal.phpt | 8 +- tests/random/001-ndarray-standard_normal.phpt | 24 +-- 11 files changed, 197 insertions(+), 119 deletions(-) diff --git a/composer.json b/composer.json index 0dc289d..87f0c9a 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "numpower/extension", + "name": "rubixnumpower/extension", "require-dev": { "phpbench/phpbench": "^1.0" }, diff --git a/numpower.c b/numpower.c index fcc4989..c4a5c65 100644 --- a/numpower.c +++ b/numpower.c @@ -796,7 +796,7 @@ PHP_METHOD(NDArray, greater) { } /** - * NDArray::greater_equal + * NDArray::greaterEqual * * @param execute_data * @param return_value @@ -805,7 +805,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_greaterequal, 2) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, greater_equal) { +PHP_METHOD(NDArray, greaterEqual) { NDArray *nda, *ndb, *rtn = NULL; zval *a, *b; ZEND_PARSE_PARAMETERS_START(2, 2) @@ -860,7 +860,7 @@ PHP_METHOD(NDArray, less) { } /** - * NDArray::less_equal + * NDArray::lessEqual * * @param execute_data * @param return_value @@ -869,7 +869,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_lessequal, 2) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, less_equal) { +PHP_METHOD(NDArray, lessEqual) { NDArray *nda, *ndb, *rtn = NULL; zval *a, *b; ZEND_PARSE_PARAMETERS_START(2, 2) @@ -892,7 +892,7 @@ PHP_METHOD(NDArray, less_equal) { } /** - * NDArray::less_equal + * NDArray::notEqual * * @param execute_data * @param return_value @@ -901,7 +901,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_notequal, 2) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, not_equal) { +PHP_METHOD(NDArray, notEqual) { NDArray *nda, *ndb, *rtn = NULL; zval *a, *b; ZEND_PARSE_PARAMETERS_START(2, 2) @@ -977,7 +977,40 @@ PHP_METHOD(NDArray, normal) { } /** - * NDArray::random_binomial + * NDArray::truncatedNormal + * + * @param execute_data + * @param return_value + */ +ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_normal, 0, 0, 1) +ZEND_ARG_INFO(0, size) +ZEND_ARG_INFO(0, loc) +ZEND_ARG_INFO(0, scale) +ZEND_END_ARG_INFO() +PHP_METHOD(NDArray, truncatedNormal) { + NDArray *rtn = NULL; + int *shape; + zval* size; + double loc = 0.0, scale = 1.0; + ZEND_PARSE_PARAMETERS_START(1, 3) + Z_PARAM_ZVAL(size) + Z_PARAM_OPTIONAL + Z_PARAM_DOUBLE(loc) + Z_PARAM_DOUBLE(scale) + ZEND_PARSE_PARAMETERS_END(); + NDArray *nda = ZVAL_TO_NDARRAY(size); + if (nda == NULL) return; + shape = emalloc(sizeof(int) * NDArray_NUMELEMENTS(nda)); + for (int i = 0; i < NDArray_NUMELEMENTS(nda); i++) { + shape[i] = (int) NDArray_FDATA(nda)[i]; + } + rtn = NDArray_TruncatedNormal(loc, scale, shape, NDArray_NUMELEMENTS(nda)); + NDArray_FREE(nda); + RETURN_NDARRAY(rtn, return_value); +} + +/** + * NDArray::randomBinomial * * @param execute_data * @param return_value @@ -987,7 +1020,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_binomial, 0, 0, 3) ZEND_ARG_INFO(0, p) ZEND_ARG_INFO(0, n) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, random_binomial) { +PHP_METHOD(NDArray, randomBinomial) { NDArray *rtn = NULL; int *ishape; zval* shape; @@ -1009,7 +1042,7 @@ PHP_METHOD(NDArray, random_binomial) { } /** - * NDArray::standard_normal + * NDArray::standardNormal * * @param shape */ @@ -1017,7 +1050,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_standard_normal, 1) ZEND_ARG_ARRAY_INFO(0, shape, 0) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, standard_normal) { +PHP_METHOD(NDArray, standardNormal) { NDArray *rtn = NULL; zval* shape; HashTable *shape_ht; @@ -1344,7 +1377,7 @@ PHP_METHOD(NDArray, all) { } /** - * NDArray::allclose + * NDArray::allClose * * @param execute_data * @param return_value @@ -1355,7 +1388,7 @@ ZEND_ARG_INFO(0, b) ZEND_ARG_INFO(0, rtol) ZEND_ARG_INFO(0, atol) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, allclose) { +PHP_METHOD(NDArray, allClose) { zval *a, *b; double rtol = 1e-05, atol = 1e-08; int rtn; @@ -1476,7 +1509,7 @@ PHP_METHOD(NDArray, copy) { } /** - * NDArray::atleast_1d + * NDArray::atleast1d * * @param execute_data * @param return_value @@ -1484,7 +1517,7 @@ PHP_METHOD(NDArray, copy) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_atleast_1d, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, atleast_1d) { +PHP_METHOD(NDArray, atleast1d) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1501,7 +1534,7 @@ PHP_METHOD(NDArray, atleast_1d) { } /** - * NDArray::atleast_2d + * NDArray::atleast2d * * @param execute_data * @param return_value @@ -1509,7 +1542,7 @@ PHP_METHOD(NDArray, atleast_1d) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_atleast_2d, 0, 0, 1) ZEND_ARG_INFO(0, a) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, atleast_2d) { +PHP_METHOD(NDArray, atleast2d) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1526,7 +1559,7 @@ PHP_METHOD(NDArray, atleast_2d) { } /** - * NDArray::atleast_3d + * NDArray::atleast3d * * @param execute_data * @param return_value @@ -1535,7 +1568,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_atleast_3d, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, atleast_3d) { +PHP_METHOD(NDArray, atleast3d) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -3554,13 +3587,13 @@ PHP_METHOD(NDArray, add) { } /** -* NDArray::expand_dims +* NDArray::expandDims */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_expand_dims, 0) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, expand_dims) { +PHP_METHOD(NDArray, expandDims) { NDArray *rtn = NULL; zval *a; zval *axis; @@ -3661,14 +3694,14 @@ PHP_METHOD(NDArray, flip) { } /** -* NDArray::swapaxes +* NDArray::swapAxes */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_swapaxes, 0, 0, 3) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, axis1) ZEND_ARG_INFO(0, axis2) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, swapaxes) { +PHP_METHOD(NDArray, swapAxes) { NDArray *rtn = NULL; zval *a; long axis1, axis2; @@ -3693,14 +3726,14 @@ PHP_METHOD(NDArray, swapaxes) { } /** -* NDArray::rollaxis +* NDArray::rollAxis */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_rollaxis, 0, 0, 2) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, axis) ZEND_ARG_INFO(0, start) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, rollaxis) { +PHP_METHOD(NDArray, rollAxis) { NDArray *rtn = NULL; zval *a; long axis, start = 0; @@ -3726,14 +3759,14 @@ PHP_METHOD(NDArray, rollaxis) { } /** -* NDArray::moveaxis +* NDArray::moveAxis */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_moveaxis, 0, 0, 3) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, source) ZEND_ARG_INFO(0, destination) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, moveaxis) { +PHP_METHOD(NDArray, moveAxis) { NDArray *rtn = NULL; zval *a; zval *source, *destination; @@ -3765,12 +3798,12 @@ PHP_METHOD(NDArray, moveaxis) { } /** -* NDArray::vstack +* NDArray::verticalStack */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_vstack, 0, 0, 1) ZEND_ARG_INFO(0, arrays) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, vstack) { +PHP_METHOD(NDArray, verticalStack) { NDArray *rtn = NULL; zval *arrays; int num_args; @@ -3789,12 +3822,12 @@ PHP_METHOD(NDArray, vstack) { } /** -* NDArray::hstack +* NDArray::horizontalstack */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_hstack, 0, 0, 1) ZEND_ARG_INFO(0, arrays) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, hstack) { +PHP_METHOD(NDArray, horizontalstack) { NDArray *rtn = NULL; zval *arrays; int num_args; @@ -3813,12 +3846,12 @@ PHP_METHOD(NDArray, hstack) { } /** -* NDArray::dstack +* NDArray::depthStack */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_dstack, 0, 0, 1) ZEND_ARG_INFO(0, arrays) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, dstack) { +PHP_METHOD(NDArray, depthStack) { NDArray *rtn = NULL; zval *arrays; int num_args; @@ -3837,12 +3870,12 @@ PHP_METHOD(NDArray, dstack) { } /** -* NDArray::column_stack +* NDArray::columnStack */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_column_stack, 0, 0, 1) ZEND_ARG_INFO(0, arrays) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, column_stack) { +PHP_METHOD(NDArray, columnStack) { NDArray *rtn = NULL; zval *arrays; int num_args; @@ -4251,12 +4284,12 @@ PHP_METHOD(NDArray, lu) { } /** - * NDArray::matrix_rank + * NDArray::matrixRank */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_matrix_rank, 0) ZEND_ARG_INFO(0, a) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, matrix_rank) { +PHP_METHOD(NDArray, matrixRank) { NDArray *rtn; zval *a, *b; long axis; @@ -4284,13 +4317,13 @@ PHP_METHOD(NDArray, matrix_rank) { /** - * NDArray::dnn_conv2d_forward + * NDArray::dnnConv2dForward */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_dnn_conv2d_forward, 4, 0, 1) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, dnn_conv2d_forward) { +PHP_METHOD(NDArray, dnnConv2dForward) { NDArray *rtn; NDArray *ndb = NULL; zval *input, *filters; @@ -4311,13 +4344,13 @@ PHP_METHOD(NDArray, dnn_conv2d_forward) { } /** - * NDArray::dnn_conv1d_forward + * NDArray::dnnConv1dForward */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_dnn_conv1d_forward, 4, 0, 1) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, dnn_conv1d_forward) { +PHP_METHOD(NDArray, dnnConv1dForward) { NDArray *rtn; NDArray *ndb = NULL; zval *input, *filters; @@ -4338,14 +4371,14 @@ PHP_METHOD(NDArray, dnn_conv1d_forward) { } /** - * NDArray::dnn_conv2d_backward + * NDArray::dnnConv2dBackward */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_dnn_conv2d_backward, 3, 0, 1) ZEND_ARG_INFO(0, x) ZEND_ARG_INFO(0, y) ZEND_ARG_INFO(0, filters) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, dnn_conv2d_backward) { +PHP_METHOD(NDArray, dnnConv2dBackward) { NDArray **rtn; zval *x, *y, *filters; ZEND_PARSE_PARAMETERS_START(3, 3) @@ -5057,22 +5090,22 @@ static const zend_function_entry class_NDArray_methods[] = { ZEND_ME(NDArray, copy, arginfo_ndarray_copy, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, shape, arginfo_ndarray_shape, ZEND_ACC_PUBLIC) ZEND_ME(NDArray, flatten, arginfo_ndarray_flat, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, atleast_1d, arginfo_ndarray_atleast_1d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, atleast_2d, arginfo_ndarray_atleast_2d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, atleast_3d, arginfo_ndarray_atleast_3d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, atleast1d, arginfo_ndarray_atleast_1d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, atleast2d, arginfo_ndarray_atleast_2d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, atleast3d, arginfo_ndarray_atleast_3d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, transpose, arginfo_ndarray_transpose, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, slice, arginfo_slice, ZEND_ACC_PUBLIC) ZEND_ME(NDArray, append, arginfo_ndarray_append, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, expand_dims, arginfo_ndarray_expand_dims, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, squeeze, arginfo_ndarray_squeeze, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, flip, arginfo_ndarray_flip, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, swapaxes, arginfo_ndarray_swapaxes, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, rollaxis, arginfo_ndarray_rollaxis, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, moveaxis, arginfo_ndarray_moveaxis, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, vstack, arginfo_ndarray_vstack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, hstack, arginfo_ndarray_hstack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, dstack, arginfo_ndarray_dstack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, column_stack, arginfo_ndarray_column_stack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, swapAxes, arginfo_ndarray_swapaxes, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, rollAxis, arginfo_ndarray_rollaxis, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, moveAxis, arginfo_ndarray_moveaxis, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, verticalStack, arginfo_ndarray_vstack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, horizontalStack, arginfo_ndarray_hstack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, depthStack, arginfo_ndarray_dstack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, columnStack, arginfo_ndarray_column_stack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, concatenate, arginfo_ndarray_concatenate, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // INDEXING @@ -5091,10 +5124,11 @@ static const zend_function_entry class_NDArray_methods[] = { // RANDOM ZEND_ME(NDArray, normal, arginfo_ndarray_normal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, standard_normal, arginfo_ndarray_standard_normal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, truncatedNormal, arginfo_ndarray_normal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, standardNormal, arginfo_ndarray_standard_normal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, poisson, arginfo_ndarray_poisson, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, uniform, arginfo_ndarray_uniform, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, random_binomial, arginfo_ndarray_binomial, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, randomBinomial, arginfo_ndarray_binomial, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // LINALG ZEND_ME(NDArray, matmul, arginfo_ndarray_matmul, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) @@ -5113,24 +5147,24 @@ static const zend_function_entry class_NDArray_methods[] = { ZEND_ME(NDArray, inv, arginfo_ndarray_inv, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, lstsq, arginfo_ndarray_lstsq, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, lu, arginfo_ndarray_lu, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, matrix_rank, arginfo_ndarray_matrix_rank, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, matrixRank, arginfo_ndarray_matrix_rank, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, convolve2d, arginfo_ndarray_convolve2d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, correlate2d, arginfo_ndarray_correlate2d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // DNN - ZEND_ME(NDArray, dnn_conv2d_forward, arginfo_ndarray_dnn_conv2d_forward, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, dnn_conv2d_backward, arginfo_ndarray_dnn_conv2d_backward, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, dnn_conv1d_forward, arginfo_ndarray_dnn_conv1d_forward, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, dnnConv2dForward, arginfo_ndarray_dnn_conv2d_forward, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, dnnConv2dBackward, arginfo_ndarray_dnn_conv2d_backward, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, dnnConv1dForward, arginfo_ndarray_dnn_conv1d_forward, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // LOGIC ZEND_ME(NDArray, all, arginfo_ndarray_all, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, allclose, arginfo_ndarray_allclose, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, allClose, arginfo_ndarray_allclose, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, equal, arginfo_ndarray_equal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, greater, arginfo_ndarray_greater, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, greater_equal, arginfo_ndarray_greaterequal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, greaterEqual, arginfo_ndarray_greaterequal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, less, arginfo_ndarray_less, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, less_equal, arginfo_ndarray_lessequal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, not_equal, arginfo_ndarray_notequal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, lessEqual, arginfo_ndarray_lessequal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, notEqual, arginfo_ndarray_notequal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // MATH ZEND_ME(NDArray, abs, arginfo_ndarray_abs, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) @@ -5280,7 +5314,7 @@ PHP_RSHUTDOWN_FUNCTION(ndarray) { zend_module_entry ndarray_module_entry = { STANDARD_MODULE_HEADER, - "NumPower", /* Extension name */ + "RubixNumPower", /* Extension name */ ext_functions, /* zend_function_entry */ PHP_MINIT(ndarray), /* PHP_MINIT - Module initialization */ PHP_MSHUTDOWN(ndarray), /* PHP_MSHUTDOWN - Module shutdown */ diff --git a/php_numpower.h b/php_numpower.h index c5056d5..4898251 100644 --- a/php_numpower.h +++ b/php_numpower.h @@ -64,7 +64,7 @@ ZEND_FUNCTION(print_r_); PHPAPI zend_class_entry *phpsci_ce_NDArray; PHPAPI zend_class_entry *phpsci_ce_ArithmeticOperand; -# define PHP_NDARRAY_VERSION "0.6.0" +# define PHP_NDARRAY_VERSION "0.7.0" # if defined(ZTS) && defined(COMPILE_DL_NDARRAY) ZEND_TSRMLS_CACHE_EXTERN() diff --git a/src/initializers.c b/src/initializers.c index 6a600ea..d41045b 100644 --- a/src/initializers.c +++ b/src/initializers.c @@ -507,6 +507,35 @@ NDArray_Identity(int size) { return rtn; } +/** + * Random samples from a truncated Gaussian distribution. + * + * The values generated are similar to values from a Normal distribution, + * except that values more than two standard deviations from the mean are + * discarded and re-drawn. + * + * @param size + * @return + */ +NDArray* +NDArray_TruncatedNormal(double loc, double scale, int* shape, int ndim) { + NDArray *rtn; + rtn = NDArray_Zeros(shape, ndim, NDARRAY_TYPE_FLOAT32, NDARRAY_DEVICE_CPU); + + for (int i = 0; i < NDArray_NUMELEMENTS(rtn); i++) { + float z; + do { + float u1 = (float)rand() / (float)RAND_MAX; + float u2 = (float)rand() / (float)RAND_MAX; + z = sqrtf(-2.0f * logf(u1)) * cosf(2.0f * (float)M_PI * u2); + z = (float)loc + (float)scale * z; + NDArray_FDATA(rtn)[i] = z; + } while (z < (loc - 2.0 * scale) || z > (loc + 2.0 * scale)); + } + + return rtn; +} + /** * Random samples from a Gaussian distribution. * diff --git a/src/initializers.h b/src/initializers.h index fb45bd9..945ca3e 100644 --- a/src/initializers.h +++ b/src/initializers.h @@ -11,6 +11,7 @@ NDArray* NDArray_Zeros(int *shape, int ndim, const char *type, int device); NDArray* NDArray_Ones(int *shape, int ndim, const char *type); NDArray* NDArray_Identity(int size); NDArray* NDArray_Normal(double loc, double scale, int* shape, int ndim); +NDArray* NDArray_TruncatedNormal(double loc, double scale, int* shape, int ndim); NDArray* NDArray_StandardNormal(int* shape, int ndim); NDArray* NDArray_Poisson(double lam, int* shape, int ndim); NDArray* NDArray_Uniform(double low, double high, int* shape, int ndim); diff --git a/stubs/numpower.stubs.php b/stubs/numpower.stubs.php index a7b9b9c..33e2fdb 100644 --- a/stubs/numpower.stubs.php +++ b/stubs/numpower.stubs.php @@ -106,24 +106,24 @@ public static function positive(NDArray|array|float|int $a): NDArray|float|int { public static function reciprocal(NDArray|array|float|int $a): NDArray|float|int {} /** - * Raises each element of an array $a to a specified power $b and returns a new array containing the result. - * - * Same as $a ** $b; + * Subtract two arrays element-wise * * @param NDArray|array|float|int $a Input array * @param NDArray|array|float|int $b Input array - * @return NDArray|float|int $a ** $b + * @return NDArray|float|int $a - $b */ - public static function pow(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray|float|int {}; + public static function subtract(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray|float|int {} /** - * Subtract two arrays element-wise + * Raises each element of an array $a to a specified power $b and returns a new array containing the result. + * + * Same as $a ** $b; * * @param NDArray|array|float|int $a Input array * @param NDArray|array|float|int $b Input array - * @return NDArray|float|int $a - $b + * @return NDArray|float|int $a ** $b */ - public static function subtract(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray|float|int {} + public static function pow(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray|float|int {} /** * Computes the element-wise exponential function of an array, returning @@ -474,6 +474,20 @@ public static function sin(NDArray|array|float|int $array): NDArray|float|int {} */ public static function normal(array $size, float $loc = 0.0, float $scale = 1.0): NDArray {} + /** + * Generates an array of random numbers from a truncated normal distribution. + * + * The values generated are similar to values from a Normal distribution, + * except that values more than two standard deviations from the mean are + * discarded and re-drawn. + * + * @param array $size + * @param float $loc + * @param float $scale + * @return NDArray + */ + public static function truncatedNormal(array $size, float $loc = 0.0, float $scale = 1.0): NDArray {} + /** * Generates an array of random integers from a Poisson distribution. * The Poisson distribution models the number of events occurring in fixed intervals of time @@ -493,7 +507,7 @@ public static function poisson(array $size, float $lam = 1.0): NDArray {} * @param array $size * @return NDArray */ - public static function standard_normal(array $size): NDArray {} + public static function standardNormal(array $size): NDArray {} /** * Generates an array of random numbers from a uniform distribution. The uniform distribution provides an equal @@ -601,7 +615,7 @@ public static function variance(NDArray|array|float|int $array): float|int {} * @param NDArray|array|float|int $array * @return NDArray */ - public static function atleast_1d(NDArray|array|float|int $array): NDArray {} + public static function atleast1d(NDArray|array|float|int $array): NDArray {} /** * Convert inputs to arrays with at least two dimensions. @@ -609,7 +623,7 @@ public static function atleast_1d(NDArray|array|float|int $array): NDArray {} * @param NDArray|array|float|int $array * @return NDArray */ - public static function atleast_2d(NDArray|array|float|int $array): NDArray {} + public static function atleast2d(NDArray|array|float|int $array): NDArray {} /** * Convert inputs to arrays with at least three dimensions. @@ -617,7 +631,7 @@ public static function atleast_2d(NDArray|array|float|int $array): NDArray {} * @param NDArray|array|float|int $array * @return NDArray */ - public static function atleast_3d(NDArray|array|float|int $array): NDArray {} + public static function atleast3d(NDArray|array|float|int $array): NDArray {} /** * Create a copy of array `$a`. @@ -641,7 +655,7 @@ public static function copy(NDArray|array|float|int $a, int $device = NULL): NDA * @param int|int[]|null $axis This parameter specifies the position where the new axis (or axes) will be inserted within the expanded array. * @return NDArray */ - public static function expand_dims(NDArray|array|float|int $target, int|array $axis = NULL): NDArray {} + public static function expandDims(NDArray|array|float|int $target, int|array $axis = NULL): NDArray {} /** * Return a **copy** of the array `$a` into one dimension. @@ -698,7 +712,7 @@ public static function transpose(NDArray|array|float|int $a, ?array $axes): NDAr * @param int $axis2 Second axis * @return NDArray */ - public static function swapaxes(NDArray|array|float|int $a, int $axis1, int $axis2): NDArray {} + public static function swapAxes(NDArray|array|float|int $a, int $axis1, int $axis2): NDArray {} /** * Roll the specified axis backwards, until it lies in a given position. @@ -708,7 +722,7 @@ public static function swapaxes(NDArray|array|float|int $a, int $axis1, int $axi * @param int $start * @return NDArray */ - public static function rollaxis(NDArray|array|float|int $a, int $axis, int $start = 0): NDArray {} + public static function rollAxis(NDArray|array|float|int $a, int $axis, int $start = 0): NDArray {} /** * Move axes of an array to new positions. @@ -718,7 +732,7 @@ public static function rollaxis(NDArray|array|float|int $a, int $axis, int $star * @param int|array $destination * @return NDArray */ - public static function moveaxis(NDArray|array|float|int $a, int|array $source, int|array $destination): NDArray {} + public static function moveAxis(NDArray|array|float|int $a, int|array $source, int|array $destination): NDArray {} /** * Stack arrays in sequence vertically (row wise). @@ -726,7 +740,7 @@ public static function moveaxis(NDArray|array|float|int $a, int|array $source, i * @param NDArray[] $arrays * @return NDArray */ - public static function vstack(array $arrays): NDArray {} + public static function verticalStack(array $arrays): NDArray {} /** * Stack arrays in sequence horizontally (column wise). @@ -734,7 +748,7 @@ public static function vstack(array $arrays): NDArray {} * @param NDArray[] $arrays * @return NDArray */ - public static function hstack(array $arrays): NDArray {} + public static function horizontalStack(array $arrays): NDArray {} /** * Stack arrays in sequence depth wise (along third axis). @@ -742,7 +756,7 @@ public static function hstack(array $arrays): NDArray {} * @param NDArray[] $arrays * @return NDArray */ - public static function dstack(array $arrays): NDArray {} + public static function depthStack(array $arrays): NDArray {} /** * Join a sequence of arrays along an existing axis. @@ -769,7 +783,7 @@ public static function append(NDArray|array $array, NDArray|array $values, ?int * @param NDArray[] $arrays * @return NDArray */ - public static function column_stack(array $arrays): NDArray {} + public static function columnStack(array $arrays): NDArray {} /** * Creates a new NDArray from a PHP array. @@ -836,7 +850,7 @@ public static function all(NDArray|array|float|int $a): bool {} * @param float $atol * @return NDArray */ - public static function allclose(NDArray|array|float|int $a, NDArray|array|float|int $b, float $rtol = 1e-05, float $atol = 1e-08): NDArray {} + public static function allClose(NDArray|array|float|int $a, NDArray|array|float|int $b, float $rtol = 1e-05, float $atol = 1e-08): NDArray {} /** * Performs an element-wise equality comparison between two arrays and returns a @@ -869,7 +883,7 @@ public static function greater(NDArray|array|float|int $a, NDArray|array|float|i * @param NDArray|array|float|int $b * @return NDArray */ - public static function greater_equal(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray {} + public static function greaterEqual(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray {} /** * Performs an element-wise less-than comparison between two arrays and returns a new array of the same shape. @@ -891,7 +905,7 @@ public static function less(NDArray|array|float|int $a, NDArray|array|float|int * @param NDArray|array|float|int $b * @return NDArray */ - public static function less_equal(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray {} + public static function lessEqual(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray {} /** * Performs an element-wise inequality comparison between two arrays and returns a new array of the same shape. @@ -901,7 +915,7 @@ public static function less_equal(NDArray|array|float|int $a, NDArray|array|floa * @param NDArray|array|float|int $b * @return NDArray */ - public static function not_equal(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray {} + public static function notEqual(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray {} /** * Computes the sum of the diagonal elements of a square array, also known as the trace of the array. @@ -971,7 +985,7 @@ public static function norm(NDArray|array $a, int $order = 2): float {} * @param float $tol * @return NDArray */ - public static function matrix_rank(NDArray|array $a, float $tol = 1e-6): NDArray {} + public static function matrixRank(NDArray|array $a, float $tol = 1e-6): NDArray {} /** * Performs matrix multiplication between two arrays and returns the result as a new array. @@ -1162,5 +1176,5 @@ public static function argmax(NDArray|array $a, ?int $axis, bool $keepdims = fal * @param array ...$indices * @return NDArray|float */ - public function slice(...$indices): NDArray|float {}; + public function slice(...$indices): NDArray|float {} } \ No newline at end of file diff --git a/tests/logic/002-ndarray-allclose.phpt b/tests/logic/002-ndarray-allclose.phpt index a1fcea8..fa3308f 100644 --- a/tests/logic/002-ndarray-allclose.phpt +++ b/tests/logic/002-ndarray-allclose.phpt @@ -1,5 +1,5 @@ --TEST-- -NDArray::allclose +NDArray::allClose --FILE-- --EXPECT-- bool(false) diff --git a/tests/logic/005-ndarray-greater_equal.phpt b/tests/logic/005-ndarray-greater_equal.phpt index bdf6cf4..77c5c3d 100644 --- a/tests/logic/005-ndarray-greater_equal.phpt +++ b/tests/logic/005-ndarray-greater_equal.phpt @@ -1,13 +1,13 @@ --TEST-- -NDArray::greater_equal +NDArray::greaterEqual --FILE-- toArray()); -print_r(\NDArray::greater_equal($a, $a)->toArray()); -print_r(\NDArray::greater_equal($c, $c)->toArray()); +print_r(\NDArray::greaterEqual($a, $b)->toArray()); +print_r(\NDArray::greaterEqual($a, $a)->toArray()); +print_r(\NDArray::greaterEqual($c, $c)->toArray()); ?> --EXPECT-- Array diff --git a/tests/logic/007-ndarray-less_equal.phpt b/tests/logic/007-ndarray-less_equal.phpt index a86ddfd..4046a48 100644 --- a/tests/logic/007-ndarray-less_equal.phpt +++ b/tests/logic/007-ndarray-less_equal.phpt @@ -1,13 +1,13 @@ --TEST-- -NDArray::less_equal +NDArray::lessEqual --FILE-- toArray()); -print_r(\NDArray::less_equal($a, $a)->toArray()); -print_r(\NDArray::less_equal($c, $c)->toArray()); +print_r(\NDArray::lessEqual($a, $b)->toArray()); +print_r(\NDArray::lessEqual($a, $a)->toArray()); +print_r(\NDArray::lessEqual($c, $c)->toArray()); ?> --EXPECT-- Array diff --git a/tests/logic/008-ndarray-not_equal.phpt b/tests/logic/008-ndarray-not_equal.phpt index 6df4cd3..212ca7c 100644 --- a/tests/logic/008-ndarray-not_equal.phpt +++ b/tests/logic/008-ndarray-not_equal.phpt @@ -1,13 +1,13 @@ --TEST-- -NDArray::not_equal +NDArray::notEqual --FILE-- toArray()); -print_r(\NDArray::not_equal($a, $a)->toArray()); -print_r(\NDArray::not_equal($c, $c)->toArray()); +print_r(\NDArray::notEqual($a, $b)->toArray()); +print_r(\NDArray::notEqual($a, $a)->toArray()); +print_r(\NDArray::notEqual($c, $c)->toArray()); ?> --EXPECT-- Array diff --git a/tests/random/001-ndarray-standard_normal.phpt b/tests/random/001-ndarray-standard_normal.phpt index 2c4dd8a..5d6df8f 100644 --- a/tests/random/001-ndarray-standard_normal.phpt +++ b/tests/random/001-ndarray-standard_normal.phpt @@ -1,5 +1,5 @@ --TEST-- -NDArray::standard_normal +NDArray::standardNormal --FILE-- $shape) { - nd::standard_normal($shape); + nd::standardNormal($shape); echo "successful creation with $sk" . PHP_EOL; } echo PHP_EOL; @@ -47,7 +47,7 @@ new class $dataset = self::case2DataProvider(); foreach ($dataset as $condition => $data) { try { - nd::standard_normal($data); + nd::standardNormal($data); } catch (\Throwable $t) { echo "Error when passed " . $condition. ": " . $t->getMessage() . PHP_EOL; } @@ -75,7 +75,7 @@ new class $dataset = self::case3DataProvider(); foreach ($dataset as $condition => $data) { try { - nd::standard_normal([$data]); + nd::standardNormal([$data]); } catch (\Throwable $t) { echo "Error when shape " . $condition . ": " . $t->getMessage() . PHP_EOL; } @@ -101,7 +101,7 @@ new class { echo '--- CASE 4 ---' . PHP_EOL; try { - nd::standard_normal([]); + nd::standardNormal([]); } catch (\Throwable $t) { echo $t->getMessage() . PHP_EOL; } @@ -115,7 +115,7 @@ new class private function testCase5(): void { echo '--- CASE 5 ---' . PHP_EOL; - $a = nd::standard_normal([4]); + $a = nd::standardNormal([4]); echo "Shape is: " . count($a->toArray()) . PHP_EOL; @@ -125,7 +125,7 @@ new class } } - $a = nd::standard_normal([4, 4]); + $a = nd::standardNormal([4, 4]); foreach ($a->toArray() as $k => $el) { echo "Shape level of element " . $k + 1 . " is: " . count($el) . PHP_EOL; @@ -148,11 +148,11 @@ successful creation with 2-dim successful creation with 3-dim --- CASE 2 --- -Error when passed shape is integer: NDArray::standard_normal(): Argument #1 ($shape) must be of type array, int given -Error when passed shape is double: NDArray::standard_normal(): Argument #1 ($shape) must be of type array, float given -Error when passed shape is string: NDArray::standard_normal(): Argument #1 ($shape) must be of type array, string given -Error when passed shape is null: NDArray::standard_normal(): Argument #1 ($shape) must be of type array, null given -Error when passed shape is object: NDArray::standard_normal(): Argument #1 ($shape) must be of type array, stdClass given +Error when passed shape is integer: NDArray::standardNormal(): Argument #1 ($shape) must be of type array, int given +Error when passed shape is double: NDArray::standardNormal(): Argument #1 ($shape) must be of type array, float given +Error when passed shape is string: NDArray::standardNormal(): Argument #1 ($shape) must be of type array, string given +Error when passed shape is null: NDArray::standardNormal(): Argument #1 ($shape) must be of type array, null given +Error when passed shape is object: NDArray::standardNormal(): Argument #1 ($shape) must be of type array, stdClass given --- CASE 3 --- Error when shape value is array: Invalid parameter: Shape elements must be integers. From 8a134274392f31834082d7ee5d024f73697d5cc9 Mon Sep 17 00:00:00 2001 From: Aleksei Nechaev Date: Mon, 24 Mar 2025 03:13:56 +0700 Subject: [PATCH 2/7] fix naming --- numpower.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/numpower.c b/numpower.c index c4a5c65..a4fe32e 100644 --- a/numpower.c +++ b/numpower.c @@ -982,7 +982,7 @@ PHP_METHOD(NDArray, normal) { * @param execute_data * @param return_value */ -ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_normal, 0, 0, 1) +ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_truncated_normal, 0, 0, 1) ZEND_ARG_INFO(0, size) ZEND_ARG_INFO(0, loc) ZEND_ARG_INFO(0, scale) @@ -3822,12 +3822,12 @@ PHP_METHOD(NDArray, verticalStack) { } /** -* NDArray::horizontalstack +* NDArray::horizontalStack */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_hstack, 0, 0, 1) ZEND_ARG_INFO(0, arrays) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, horizontalstack) { +PHP_METHOD(NDArray, horizontalStack) { NDArray *rtn = NULL; zval *arrays; int num_args; @@ -5096,7 +5096,7 @@ static const zend_function_entry class_NDArray_methods[] = { ZEND_ME(NDArray, transpose, arginfo_ndarray_transpose, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, slice, arginfo_slice, ZEND_ACC_PUBLIC) ZEND_ME(NDArray, append, arginfo_ndarray_append, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, expand_dims, arginfo_ndarray_expand_dims, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, expandDims, arginfo_ndarray_expand_dims, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, squeeze, arginfo_ndarray_squeeze, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, flip, arginfo_ndarray_flip, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, swapAxes, arginfo_ndarray_swapaxes, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) @@ -5124,7 +5124,7 @@ static const zend_function_entry class_NDArray_methods[] = { // RANDOM ZEND_ME(NDArray, normal, arginfo_ndarray_normal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, truncatedNormal, arginfo_ndarray_normal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NDArray, truncatedNormal, arginfo_ndarray_truncated_normal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, standardNormal, arginfo_ndarray_standard_normal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, poisson, arginfo_ndarray_poisson, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NDArray, uniform, arginfo_ndarray_uniform, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) From 78db6567ed947e49b08bcf503f2e33c8fa0abbfe Mon Sep 17 00:00:00 2001 From: Aleksei Nechaev Date: Mon, 24 Mar 2025 20:39:13 +0700 Subject: [PATCH 3/7] GPU Acceleration for normal distribution generation added --- numpower.c | 36 +- src/initializers.c | 73 +- src/initializers.h | 2 +- src/ndmath/cuda/cuda_math.cu | 26 +- src/ndmath/cuda/cuda_math.h | 2 +- stubs/numpower.stubs.php | 697 +++++++++--------- .../manipulation/005-ndarray-expanddims.phpt | 34 +- 7 files changed, 478 insertions(+), 392 deletions(-) diff --git a/numpower.c b/numpower.c index a4fe32e..cb23eea 100644 --- a/numpower.c +++ b/numpower.c @@ -100,7 +100,7 @@ NDArray* ZVAL_TO_NDARRAY(zval* obj) { zend_class_entry* ce = NULL; ce = Z_OBJCE_P(obj); zend_string* class_name = Z_OBJ_P(obj)->ce->name; - if (strcmp(ZSTR_VAL(class_name), "NDArray") == 0) { + if (strcmp(ZSTR_VAL(class_name), "NumPower") == 0) { if (ce == phpsci_ce_NDArray) { return buffer_get(get_object_uuid(obj)); } @@ -112,7 +112,7 @@ NDArray* ZVAL_TO_NDARRAY(zval* obj) { } #endif } - zend_throw_error(NULL, "argument must be an array, long, double, gdimage or ndarray."); + zend_throw_error(NULL, "argument must be an array, long, double, gdimage or NumPower."); return NULL; } @@ -950,29 +950,41 @@ PHP_METHOD(NDArray, identity) { * @param return_value */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_normal, 0, 0, 1) -ZEND_ARG_INFO(0, size) -ZEND_ARG_INFO(0, loc) -ZEND_ARG_INFO(0, scale) + ZEND_ARG_INFO(0, size) + ZEND_ARG_INFO(0, loc) + ZEND_ARG_INFO(0, scale) + ZEND_ARG_INFO(0, accelerator) ZEND_END_ARG_INFO() PHP_METHOD(NDArray, normal) { NDArray *rtn = NULL; int *shape; zval* size; + long accelerator = 0; double loc = 0.0, scale = 1.0; - ZEND_PARSE_PARAMETERS_START(1, 3) - Z_PARAM_ZVAL(size) + + ZEND_PARSE_PARAMETERS_START(1, 4) + Z_PARAM_ZVAL(size) Z_PARAM_OPTIONAL - Z_PARAM_DOUBLE(loc) - Z_PARAM_DOUBLE(scale) + Z_PARAM_DOUBLE(loc) + Z_PARAM_DOUBLE(scale) + Z_PARAM_LONG(accelerator) ZEND_PARSE_PARAMETERS_END(); + + int accelerator_i = (int) accelerator; NDArray *nda = ZVAL_TO_NDARRAY(size); + if (nda == NULL) return; + shape = emalloc(sizeof(int) * NDArray_NUMELEMENTS(nda)); + for (int i = 0; i < NDArray_NUMELEMENTS(nda); i++) { shape[i] = (int) NDArray_FDATA(nda)[i]; + } - rtn = NDArray_Normal(loc, scale, shape, NDArray_NUMELEMENTS(nda)); + + rtn = NDArray_Normal(loc, scale, shape, NDArray_NUMELEMENTS(nda), accelerator_i); NDArray_FREE(nda); + RETURN_NDARRAY(rtn, return_value); } @@ -5244,7 +5256,7 @@ static const zend_function_entry class_NDArray_methods[] = { static zend_class_entry *register_class_NDArray(zend_class_entry *class_entry_Iterator, zend_class_entry *class_entry_Countable, zend_class_entry *class_entry_ArrayAccess) { zend_class_entry ce, *class_entry; - INIT_CLASS_ENTRY(ce, "NDArray", class_NDArray_methods); + INIT_CLASS_ENTRY(ce, "NumPower", class_NDArray_methods); ndarray_objects_init(&ce); ce.create_object = ndarray_create_object; class_entry = zend_register_internal_class(&ce); @@ -5274,6 +5286,8 @@ static zend_class_entry *register_class_ArithmeticOperand(zend_class_entry *clas PHP_MINIT_FUNCTION(ndarray) { phpsci_ce_NDArray = register_class_NDArray(zend_ce_iterator, zend_ce_countable, zend_ce_arrayaccess); phpsci_ce_ArithmeticOperand = register_class_ArithmeticOperand(zend_ce_iterator, zend_ce_countable, zend_ce_arrayaccess); + REGISTER_LONG_CONSTANT("NUMPOWER_CPU", 0, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("NUMPOWER_CUDA", 1, CONST_CS | CONST_PERSISTENT); return SUCCESS; } diff --git a/src/initializers.c b/src/initializers.c index d41045b..34a0074 100644 --- a/src/initializers.c +++ b/src/initializers.c @@ -13,6 +13,8 @@ #ifdef HAVE_CUBLAS #include +#include "ndmath/cuda/cuda_math.h" +#include #include #include "ndmath/cuda/cuda_math.h" #include "gpu_alloc.h" @@ -520,9 +522,10 @@ NDArray_Identity(int size) { NDArray* NDArray_TruncatedNormal(double loc, double scale, int* shape, int ndim) { NDArray *rtn; - rtn = NDArray_Zeros(shape, ndim, NDARRAY_TYPE_FLOAT32, NDARRAY_DEVICE_CPU); + rtn = NDArray_Zeros(shape, ndim, NDARRAY_TYPE_FLOAT32, NDARRAY_DEVICE_GPU); + scale = scale / 0.88; - for (int i = 0; i < NDArray_NUMELEMENTS(rtn); i++) { + /*for (int i = 0; i < NDArray_NUMELEMENTS(rtn); i++) { float z; do { float u1 = (float)rand() / (float)RAND_MAX; @@ -533,6 +536,9 @@ NDArray_TruncatedNormal(double loc, double scale, int* shape, int ndim) { } while (z < (loc - 2.0 * scale) || z > (loc + 2.0 * scale)); } + return rtn;*/ + int size = NDArray_NUMELEMENTS(rtn); + cuda_truncated_normal(NDArray_FDATA(rtn), size, loc, scale); return rtn; } @@ -543,21 +549,60 @@ NDArray_TruncatedNormal(double loc, double scale, int* shape, int ndim) { * @return */ NDArray* -NDArray_Normal(double loc, double scale, int* shape, int ndim) { +NDArray_Normal(double loc, double scale, int* shape, int ndim, int accelerator) { NDArray *rtn; - rtn = NDArray_Zeros(shape, ndim, NDARRAY_TYPE_FLOAT32, NDARRAY_DEVICE_CPU); + if (accelerator == 1) { +#ifdef HAVE_CUBLAS + rtn = NDArray_Zeros(shape, ndim, NDARRAY_TYPE_FLOAT32, NDARRAY_DEVICE_GPU); + if (rtn == NULL) { + return rtn; + } - // Generate random samples from the normal distribution - for (int i = 0; i < NDArray_NUMELEMENTS(rtn); i++) { - // Box-Muller transform to generate standard normal samples - float u1 = (float)rand() / (float)RAND_MAX; - float u2 = (float)rand() / (float)RAND_MAX; - float z = sqrtf(-2.0f * logf(u1)) * cosf(2.0f * (float)M_PI * u2); + int size = NDArray_NUMELEMENTS(rtn); + float* d_matrix = NULL; + + vmalloc((void**)&d_matrix, size * sizeof(float)); + + curandGenerator_t gen; + curandCreateGenerator(&gen, CURAND_RNG_PSEUDO_DEFAULT); + curandSetPseudoRandomGeneratorSeed(gen, 1234ULL); + curandGenerateNormal(gen, d_matrix, size, (float)loc, (float)scale); + + if (rtn->data != NULL) { + vfree(rtn->data); + } + + rtn->data = (void*)d_matrix; + curandDestroyGenerator(gen); +#endif + } else { + rtn = NDArray_Zeros(shape, ndim, NDARRAY_TYPE_FLOAT32, NDARRAY_DEVICE_CPU); + if (rtn == NULL) { + return rtn; + } - // Scale and shift the standard normal sample - NDArray_FDATA(rtn)[i] = (float)loc + (float)scale * z; - } + int size = NDArray_NUMELEMENTS(rtn); + + // Generate random samples from the normal distribution + for (int i = 0; i < size; i += 2) { + float s, u, v; + do { + u = 2.0f * ((float)rand() / (float)RAND_MAX) - 1.0; + v = 2.0f * ((float)rand() / (float)RAND_MAX) - 1.0; + s = u * u + v * v; + } while (s >= 1.0f || s ==0); + float factor = sqrt(-2.0f * log(s) / s); + float z1 = u * factor; + + NDArray_FDATA(rtn)[i] = (float)loc + (float)scale * z1; + if (i + 1 <= size) { + float z2 = v * factor; + NDArray_FDATA(rtn)[i+1] = (float)loc + (float)scale * z2; + } + } + } + return rtn; } @@ -569,7 +614,7 @@ NDArray_Normal(double loc, double scale, int* shape, int ndim) { */ NDArray* NDArray_StandardNormal(int* shape, int ndim) { - return NDArray_Normal(0.0, 1.0, shape, ndim); + return NDArray_Normal(0.0, 1.0, shape, ndim, 0); } /** diff --git a/src/initializers.h b/src/initializers.h index 945ca3e..6aa24ba 100644 --- a/src/initializers.h +++ b/src/initializers.h @@ -10,7 +10,7 @@ NDArray* NDArray_FromNDArray(NDArray *target, int buffer_offset, int* shape, int NDArray* NDArray_Zeros(int *shape, int ndim, const char *type, int device); NDArray* NDArray_Ones(int *shape, int ndim, const char *type); NDArray* NDArray_Identity(int size); -NDArray* NDArray_Normal(double loc, double scale, int* shape, int ndim); +NDArray* NDArray_Normal(double loc, double scale, int* shape, int ndim, int accelerator); NDArray* NDArray_TruncatedNormal(double loc, double scale, int* shape, int ndim); NDArray* NDArray_StandardNormal(int* shape, int ndim); NDArray* NDArray_Poisson(double lam, int* shape, int ndim); diff --git a/src/ndmath/cuda/cuda_math.cu b/src/ndmath/cuda/cuda_math.cu index c9fd57f..99634f1 100644 --- a/src/ndmath/cuda/cuda_math.cu +++ b/src/ndmath/cuda/cuda_math.cu @@ -7,7 +7,8 @@ #include #include #include - +#include +#include #define CHECK_CUDA(func) do { \ cudaError_t status = (func); \ @@ -27,6 +28,29 @@ } \ } while (0) +__global__ void truncatedNormalKernel(float* d_data, int size, double loc, double scale, unsigned long long seed) { + int idx = blockIdx.x * blockDim.x + threadIdx.x; + curandState_t state; + + if (idx < size) { + curand_init(seed, idx, 0, &state); + float z; + do { + z = curand_normal(&state) * scale + loc; + } while (z < (loc - 2.0 * scale) || z > (loc + 2.0 * scale)); + d_data[idx] = z; + } +} + +void cuda_truncated_normal(float* h_data, int size, double loc, double scale) { + // Определение параметров сетки и блоков + int threadsPerBlock = 256; + int blocksPerGrid = (size + threadsPerBlock - 1) / threadsPerBlock; + + // Вызов ядра CUDA + truncatedNormalKernel<<>>(h_data, size, loc, scale, 1234ULL); + cudaDeviceSynchronize(); +} // CUDA kernel to calculate the median of a float* array __global__ void findMedianKernelFloat(float* input, int size, float* median) { diff --git a/src/ndmath/cuda/cuda_math.h b/src/ndmath/cuda/cuda_math.h index 3891ad3..e7b3cb8 100644 --- a/src/ndmath/cuda/cuda_math.h +++ b/src/ndmath/cuda/cuda_math.h @@ -77,7 +77,7 @@ NDArray* NDArrayMathGPU_ElementWise1F(NDArray* ndarray, ElementWiseFloatGPUOpera void cuda_float_transpose(int tiledim, int blockrows, const float *d_in, float *d_out, int width, int height); void cuda_float_positive(int nblocks, float *d_array); void cuda_float_reciprocal(int nblocks, float *d_array); - +void cuda_truncated_normal(float* d_data, int size, double loc, double scale); #ifdef __cplusplus } #endif diff --git a/stubs/numpower.stubs.php b/stubs/numpower.stubs.php index 33e2fdb..090c4dc 100644 --- a/stubs/numpower.stubs.php +++ b/stubs/numpower.stubs.php @@ -1,22 +1,25 @@ size()` * - * @param NDArray|array|float|int $a + * @param NumPower|array|float|int $a * @return float|int */ - public static function mean(NDArray|array|float|int $a): float|int {} + public static function mean(NumPower|array|float|int $a): float|int {} /** * The median of the elements in the array. It sorts the array, and if the number of elements is odd, * it returns the middle value; if the number of elements is even, it returns the average of the two middle values * - * @param NDArray|array|float|int $a + * @param NumPower|array|float|int $a * @return float|int */ - public static function median(NDArray|array|float|int $a): float|int {} + public static function median(NumPower|array|float|int $a): float|int {} /** * Computes the specified quantile of the elements in the array. A quantile represents a * particular value below which a given percentage of data falls. For example, * the median is the 50th quantile. * - * @param NDArray|array|float|int $a + * @param NumPower|array|float|int $a * @param float|int $q * @return float|int */ - public static function quantile(NDArray|array|float|int $a, float|int $q): float|int {} + public static function quantile(NumPower|array|float|int $a, float|int $q): float|int {} /** * Calculates the standard deviation of the elements in the array. It is the * square root of the variance and provides a measure of the amount of variation * or dispersion in the data. * - * @param NDArray|array|float|int $a + * @param NumPower|array|float|int $a * @return float|int */ - public static function std(NDArray|array|float|int $a): float|int {} + public static function std(NumPower|array|float|int $a): float|int {} /** * Calculates the variance of the elements in the array. It measures the average of the * squared differences between each element and the mean. * - * @param NDArray|array|float|int $array + * @param NumPower|array|float|int $array * @return float|int */ - public static function variance(NDArray|array|float|int $array): float|int {} + public static function variance(NumPower|array|float|int $array): float|int {} /** * Convert inputs to arrays with at least one dimension. * * Scalar inputs are converted to 1-dimensional arrays, whilst higher-dimensional inputs are preserved. * - * @param NDArray|array|float|int $array - * @return NDArray + * @param NumPower|array|float|int $array + * @return NumPower */ - public static function atleast1d(NDArray|array|float|int $array): NDArray {} + public static function atleast1d(NumPower|array|float|int $array): NumPower {} /** * Convert inputs to arrays with at least two dimensions. * - * @param NDArray|array|float|int $array - * @return NDArray + * @param NumPower|array|float|int $array + * @return NumPower */ - public static function atleast2d(NDArray|array|float|int $array): NDArray {} + public static function atleast2d(NumPower|array|float|int $array): NumPower {} /** * Convert inputs to arrays with at least three dimensions. * - * @param NDArray|array|float|int $array - * @return NDArray + * @param NumPower|array|float|int $array + * @return NumPower */ - public static function atleast3d(NDArray|array|float|int $array): NDArray {} + public static function atleast3d(NumPower|array|float|int $array): NumPower {} /** * Create a copy of array `$a`. @@ -642,158 +645,158 @@ public static function atleast3d(NDArray|array|float|int $array): NDArray {} * - **0** - CPU copy (same as `$a->cpu()`); * - **1** - GPU copy (same as `$a->gpu()`); * - * @param NDArray|array|float|int $a + * @param NumPower|array|float|int $a * @param int|null $device NULL = Same device, 0 - copy to CPU, 1 - copy to GPU - * @return NDArray copy of $a + * @return NumPower copy of $a */ - public static function copy(NDArray|array|float|int $a, int $device = NULL): NDArray {} + public static function copy(NumPower|array|float|int $a, int $device = NULL): NumPower {} /** * Adds a new axis to the array at the specified position, thereby expanding its shape. * - * @param NDArray|array|float|int $target Target array. + * @param NumPower|array|float|int $target Target array. * @param int|int[]|null $axis This parameter specifies the position where the new axis (or axes) will be inserted within the expanded array. - * @return NDArray + * @return NumPower */ - public static function expandDims(NDArray|array|float|int $target, int|array $axis = NULL): NDArray {} + public static function expandDims(NumPower|array|float|int $target, int|array $axis = NULL): NumPower {} /** * Return a **copy** of the array `$a` into one dimension. * - * @param NDArray|array|float|int $a Target array - * @return NDArray A copy of `$a`, with dimensions collapsed to 1-d, in the same device. + * @param NumPower|array|float|int $a Target array + * @return NumPower A copy of `$a`, with dimensions collapsed to 1-d, in the same device. */ - public static function flatten(NDArray|array|float|int $a): NDArray {} + public static function flatten(NumPower|array|float|int $a): NumPower {} /** - * Changes the shape of the NDArray. + * Changes the shape of the NumPower. * - * @param NDArray|array|float|int $target Target array + * @param NumPower|array|float|int $target Target array * @param int[] $shape Target shape - * @return NDArray|float|int A copy of `$a`, with dimensions collapsed to 1-d, in the same device. + * @return NumPower|float|int A copy of `$a`, with dimensions collapsed to 1-d, in the same device. */ - public static function reshape(NDArray|array|float|int $target, array $shape): NDArray|float|int {} + public static function reshape(NumPower|array|float|int $target, array $shape): NumPower|float|int {} /** - * Return a PHP array representing the shape of the NDArray + * Return a PHP array representing the shape of the NumPower * - * @return int[] Shape of the NDArray + * @return int[] Shape of the NumPower */ public function shape(): array {} /** - * Return the total number of elements in the NDArray. + * Return the total number of elements in the NumPower. * - * @return int Total number of elements in the NDArray + * @return int Total number of elements in the NumPower */ public function size(): int {} /** - * Return a PHP array with the same shape and a **copy** of values of the NDArray. + * Return a PHP array with the same shape and a **copy** of values of the NumPower. * - * @return array PHP array with same shape and a copy of the elements of the NDArray + * @return array PHP array with same shape and a copy of the elements of the NumPower */ public function toArray(): array {} /** * Return the transpose of matrix `$a` * - * @param NDArray|array|float|int $a Target array + * @param NumPower|array|float|int $a Target array * @param array|null $axes For an n-D array, if $axes are given, their order indicates how the axes are permuted - * @return NDArray $a transposed + * @return NumPower $a transposed */ - public static function transpose(NDArray|array|float|int $a, ?array $axes): NDArray {} + public static function transpose(NumPower|array|float|int $a, ?array $axes): NumPower {} /** * Interchange two axes of an array. * - * @param NDArray|array|float|int $a Target array + * @param NumPower|array|float|int $a Target array * @param int $axis1 First axis * @param int $axis2 Second axis - * @return NDArray + * @return NumPower */ - public static function swapAxes(NDArray|array|float|int $a, int $axis1, int $axis2): NDArray {} + public static function swapAxes(NumPower|array|float|int $a, int $axis1, int $axis2): NumPower {} /** * Roll the specified axis backwards, until it lies in a given position. * - * @param NDArray|array|float|int $a Target array + * @param NumPower|array|float|int $a Target array * @param int $axis * @param int $start - * @return NDArray + * @return NumPower */ - public static function rollAxis(NDArray|array|float|int $a, int $axis, int $start = 0): NDArray {} + public static function rollAxis(NumPower|array|float|int $a, int $axis, int $start = 0): NumPower {} /** * Move axes of an array to new positions. * - * @param NDArray|array|float|int $a Target array + * @param NumPower|array|float|int $a Target array * @param int|array $source * @param int|array $destination - * @return NDArray + * @return NumPower */ - public static function moveAxis(NDArray|array|float|int $a, int|array $source, int|array $destination): NDArray {} + public static function moveAxis(NumPower|array|float|int $a, int|array $source, int|array $destination): NumPower {} /** * Stack arrays in sequence vertically (row wise). * - * @param NDArray[] $arrays - * @return NDArray + * @param NumPower[] $arrays + * @return NumPower */ - public static function verticalStack(array $arrays): NDArray {} + public static function verticalStack(array $arrays): NumPower {} /** * Stack arrays in sequence horizontally (column wise). * - * @param NDArray[] $arrays - * @return NDArray + * @param NumPower[] $arrays + * @return NumPower */ - public static function horizontalStack(array $arrays): NDArray {} + public static function horizontalStack(array $arrays): NumPower {} /** * Stack arrays in sequence depth wise (along third axis). * - * @param NDArray[] $arrays - * @return NDArray + * @param NumPower[] $arrays + * @return NumPower */ - public static function depthStack(array $arrays): NDArray {} + public static function depthStack(array $arrays): NumPower {} /** * Join a sequence of arrays along an existing axis. * - * @param NDArray[] $arrays + * @param NumPower[] $arrays * @param int|null $axis - * @return NDArray + * @return NumPower */ - public static function concatenate(array $arrays, ?int $axis = 0): NDArray {} + public static function concatenate(array $arrays, ?int $axis = 0): NumPower {} /** * Append values to the end of an array. * - * @param NDArray|array $array - * @param NDArray|array $values + * @param NumPower|array $array + * @param NumPower|array $values * @param int|null $axis - * @return NDArray + * @return NumPower */ - public static function append(NDArray|array $array, NDArray|array $values, ?int $axis): NDArray {} + public static function append(NumPower|array $array, NumPower|array $values, ?int $axis): NumPower {} /** * Stack 1-D arrays as columns into a 2-D array. * - * @param NDArray[] $arrays - * @return NDArray + * @param NumPower[] $arrays + * @return NumPower */ - public static function columnStack(array $arrays): NDArray {} + public static function columnStack(array $arrays): NumPower {} /** - * Creates a new NDArray from a PHP array. + * Creates a new NumPower from a PHP array. * - * It is the equivalent of `new NDArray($array);` + * It is the equivalent of `new NumPower($array);` * * @param array|float|int $array - * @return NDArray + * @return NumPower */ - public static function array(array|float|int $array): NDArray {} + public static function array(array|float|int $array): NumPower {} /** * This function returns a square array, where the main diagonal consists of ones and all other @@ -801,28 +804,28 @@ public static function array(array|float|int $array): NDArray {} * in the output array. * * @param int $size Number of rows and columns of the new square array of size `($size, $size)` - * @return NDArray Return a new square array of size `($size, $size)` + * @return NumPower Return a new square array of size `($size, $size)` */ - public static function identity(int $size): NDArray {} + public static function identity(int $size): NumPower {} /** - * The function creates a new NDArray with the specified shape, filled with ones. + * The function creates a new NumPower with the specified shape, filled with ones. * * @param int[] $shape - * @return NDArray + * @return NumPower */ - public static function ones(array $shape): NDArray {} + public static function ones(array $shape): NumPower {} /** - * The function creates a new NDArray with the specified shape, filled with zeros. + * The function creates a new NumPower with the specified shape, filled with zeros. * * @param int[] $shape - * @return NDArray + * @return NumPower */ - public static function zeros(array $shape): NDArray {} + public static function zeros(array $shape): NumPower {} /** - * Dumps the internal information of the NDArray. + * Dumps the internal information of the NumPower. * * @return void */ @@ -836,131 +839,131 @@ public function dump() : void {} public static function dumpDevices(): void {} /** - * @param NDArray|array|float|int $a + * @param NumPower|array|float|int $a * @return bool */ - public static function all(NDArray|array|float|int $a): bool {} + public static function all(NumPower|array|float|int $a): bool {} /** * Checks if all elements in two arrays are approximately equal within a specified tolerance element-wise. * - * @param NDArray|array|float|int $a - * @param NDArray|array|float|int $b + * @param NumPower|array|float|int $a + * @param NumPower|array|float|int $b * @param float $rtol * @param float $atol - * @return NDArray + * @return NumPower */ - public static function allClose(NDArray|array|float|int $a, NDArray|array|float|int $b, float $rtol = 1e-05, float $atol = 1e-08): NDArray {} + public static function allClose(NumPower|array|float|int $a, NumPower|array|float|int $b, float $rtol = 1e-05, float $atol = 1e-08): NumPower {} /** * Performs an element-wise equality comparison between two arrays and returns a * new array of the same shape. The result will be 1 where the * elements are equal and 0 where they are not. * - * @param NDArray|array|float|int $a - * @param NDArray|array|float|int $b - * @return NDArray + * @param NumPower|array|float|int $a + * @param NumPower|array|float|int $b + * @return NumPower */ - public static function equal(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray {} + public static function equal(NumPower|array|float|int $a, NumPower|array|float|int $b): NumPower {} /** * Performs an element-wise greater-than comparison between two arrays and returns a new array of the same shape. * The result will be 1 where the elements in the first array are greater than the corresponding elements in the * second array, and 0 otherwise. * - * @param NDArray|array|float|int $a - * @param NDArray|array|float|int $b - * @return NDArray + * @param NumPower|array|float|int $a + * @param NumPower|array|float|int $b + * @return NumPower */ - public static function greater(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray {} + public static function greater(NumPower|array|float|int $a, NumPower|array|float|int $b): NumPower {} /** * Performs an element-wise greater-than-or-equal-to comparison between two arrays and returns a new * array of the same shape. The result will be 1 where the elements in the first array are greater than * or equal to the corresponding elements in the second array, and 0 otherwise. * - * @param NDArray|array|float|int $a - * @param NDArray|array|float|int $b - * @return NDArray + * @param NumPower|array|float|int $a + * @param NumPower|array|float|int $b + * @return NumPower */ - public static function greaterEqual(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray {} + public static function greaterEqual(NumPower|array|float|int $a, NumPower|array|float|int $b): NumPower {} /** * Performs an element-wise less-than comparison between two arrays and returns a new array of the same shape. * The result will be 1 where the elements in the first array are less than the corresponding elements in the second * array, and 0 otherwise. * - * @param NDArray|array|float|int $a - * @param NDArray|array|float|int $b - * @return NDArray + * @param NumPower|array|float|int $a + * @param NumPower|array|float|int $b + * @return NumPower */ - public static function less(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray {} + public static function less(NumPower|array|float|int $a, NumPower|array|float|int $b): NumPower {} /** * Performs an element-wise less-than-or-equal-to comparison between two arrays and returns a * new array of the same shape. The result will be 1 where the elements in the first array * are less than or equal to the corresponding elements in the second array, and 0 otherwise. * - * @param NDArray|array|float|int $a - * @param NDArray|array|float|int $b - * @return NDArray + * @param NumPower|array|float|int $a + * @param NumPower|array|float|int $b + * @return NumPower */ - public static function lessEqual(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray {} + public static function lessEqual(NumPower|array|float|int $a, NumPower|array|float|int $b): NumPower {} /** * Performs an element-wise inequality comparison between two arrays and returns a new array of the same shape. * The result will be 1 where the elements are not equal and 0 where they are equal. * - * @param NDArray|array|float|int $a - * @param NDArray|array|float|int $b - * @return NDArray + * @param NumPower|array|float|int $a + * @param NumPower|array|float|int $b + * @return NumPower */ - public static function notEqual(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray {} + public static function notEqual(NumPower|array|float|int $a, NumPower|array|float|int $b): NumPower {} /** * Computes the sum of the diagonal elements of a square array, also known as the trace of the array. * - * @param NDArray|array $a + * @param NumPower|array $a * @return float */ - public static function trace(NDArray|array $a): float {} + public static function trace(NumPower|array $a): float {} /** * Calculates the Singular Value Decomposition (SVD) of an array, which decomposes the array * into three separate arrays: U, Sigma, and V^T. * - * @param NDArray|array $a + * @param NumPower|array $a * @return array PHP array containing the Unitary Arrays (U) `[0]`, the vector(s) with the singular values (S) `[1]` and the unitary arrays (Vh) `[2]` */ - public static function svd(NDArray|array $a): array {} + public static function svd(NumPower|array $a): array {} /** * Solves a linear system of equations for `x`, where `Ax = b`, and `A` and `b` are given arrays. * - * @param NDArray|array $a - * @param NDArray|array $b - * @return NDArray + * @param NumPower|array $a + * @param NumPower|array $b + * @return NumPower */ - public static function solve(NDArray|array $a, NDArray|array $b): NDArray {} + public static function solve(NumPower|array $a, NumPower|array $b): NumPower {} /** * Calculates the QR decomposition of an array, which expresses it as the product of an orthogonal matrix (Q) * and an upper triangular matrix (R). * - * @param NDArray|array $a + * @param NumPower|array $a * @return array */ - public static function qr(NDArray|array $a): array {} + public static function qr(NumPower|array $a): array {} /** * Computes the outer product of two vectors, which results in a higher-dimensional array with dimensions * calculated from the input arrays' dimensions. * - * @param NDArray|array $a - * @param NDArray|array $b - * @return NDArray + * @param NumPower|array $a + * @param NumPower|array $b + * @return NumPower */ - public static function outer(NDArray|array $a, NDArray|array $b): NDArray {} + public static function outer(NumPower|array $a, NumPower|array $b): NumPower {} /** * Calculates different norms (e.g., L1 norm, L2 norm) of an array, @@ -971,56 +974,56 @@ public static function outer(NDArray|array $a, NDArray|array $b): NDArray {} * - **1** - L1-Norm * - **2** - L2-Norm * - * @param NDArray|array $a + * @param NumPower|array $a * @param int $order (1) L1-Norm, (2) L2-Norm * @return float */ - public static function norm(NDArray|array $a, int $order = 2): float {} + public static function norm(NumPower|array $a, int $order = 2): float {} /** * Calculates the numerical rank of a matrix, number of singular * values of the array that are greater than tol. * - * @param NDArray|array $a + * @param NumPower|array $a * @param float $tol - * @return NDArray + * @return NumPower */ - public static function matrixRank(NDArray|array $a, float $tol = 1e-6): NDArray {} + public static function matrixRank(NumPower|array $a, float $tol = 1e-6): NumPower {} /** * Performs matrix multiplication between two arrays and returns the result as a new array. * - * @param NDArray|array $a Input array - * @param NDArray|array $b Input array - * @return NDArray The matrix product of the inputs. This is a scalar only when both x1, x2 are 1-d vectors. + * @param NumPower|array $a Input array + * @param NumPower|array $b Input array + * @return NumPower The matrix product of the inputs. This is a scalar only when both x1, x2 are 1-d vectors. */ - public static function matmul(NDArray|array $a, NDArray|array $b): NDArray {} + public static function matmul(NumPower|array $a, NumPower|array $b): NumPower {} /** * Computes the LU factorization of a matrix * - * @param NDArray|array $a - * @return NDArray + * @param NumPower|array $a + * @return NumPower */ - public static function lu(NDArray|array $a): NDArray {} + public static function lu(NumPower|array $a): NumPower {} /** * Performs the least-squares solution to a linear matrix equation `Ax = b`, * where `$a` is a given array and `$b` is the target array. * - * @param NDArray|array $a - * @param NDArray|array $b - * @return NDArray + * @param NumPower|array $a + * @param NumPower|array $b + * @return NumPower */ - public static function lstsq(NDArray|array $a, NDArray|array $b): NDArray {} + public static function lstsq(NumPower|array $a, NumPower|array $b): NumPower {} /** * Compute the inverse of a matrix, such that `$a * nd::inv($a) = np::identity($a->shape())`. * - * @param NDArray|array $a - * @return NDArray + * @param NumPower|array $a + * @return NumPower */ - public static function inv(NDArray|array $a): NDArray {} + public static function inv(NumPower|array $a): NumPower {} /** * Calculates the inner product of two arrays. This operation involves multiplying corresponding @@ -1028,20 +1031,20 @@ public static function inv(NDArray|array $a): NDArray {} * * - When dealing with N-D arrays, the inner product is computed by taking a sum product over the last axes of the arrays. * - * @param NDArray|array|float|int $a Input array - * @param NDArray|array|float|int $b Input array - * @return NDArray|float If both `$a` and `$b` are scalars or 1-D arrays, the function will return a scalar value. + * @param NumPower|array|float|int $a Input array + * @param NumPower|array|float|int $b Input array + * @return NumPower|float If both `$a` and `$b` are scalars or 1-D arrays, the function will return a scalar value. * Otherwise, if the input arrays have more than one dimension, an array will be returned. */ - public static function inner(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray|float {} + public static function inner(NumPower|array|float|int $a, NumPower|array|float|int $b): NumPower|float {} /** * Computes the eigenvalues and eigenvectors of a square array. * - * @param NDArray|array $a + * @param NumPower|array $a * @return array */ - public static function eig(NDArray|array $a): array {} + public static function eig(NumPower|array $a): array {} /** * The `dot` function performs the dot product of two arrays. The behaviour of the function @@ -1049,43 +1052,43 @@ public static function eig(NDArray|array $a): array {} * * - If both `$a` and `$b` are 1-D arrays, the dot product is computed as the inner product of the vectors. * - If both `$a` and `$b` are 2-D arrays, the dot product is computed as the matrix multiplication - * (See NDArray::matmul). + * (See NumPower::matmul). * - If either `$a` or `$b` is a 0-D (scalar) array, the dot product is equivalent to element-wise multiplication. - * (See NDArray::multiply). + * (See NumPower::multiply). * - If `$a` is an N-D array and `$b` is a 1-D array, the dot product is computed as the * sum product over the last axis of `$a` and `$b`. * - * @param NDArray|array|float|int $a Input array - * @param NDArray|array|float|int $b Input array - * @return NDArray + * @param NumPower|array|float|int $a Input array + * @param NumPower|array|float|int $b Input array + * @return NumPower */ - public static function dot(NDArray|array|float|int $a, NDArray|array|float|int $b): NDArray {} + public static function dot(NumPower|array|float|int $a, NumPower|array|float|int $b): NumPower {} /** * Computes the determinant of a square array, which represents the scaling factor of the volume * of the array transformation. * - * @param NDArray|array $a + * @param NumPower|array $a * @return float */ - public static function det(NDArray|array $a): float {} + public static function det(NumPower|array $a): float {} /** * Computes the condition number of an array. * - * @param NDArray|array $a + * @param NumPower|array $a * @return float */ - public static function cond(NDArray|array $a): float {} + public static function cond(NumPower|array $a): float {} /** * Calculates the Cholesky decomposition of a positive-definite array, decomposing * it into a lower triangular matrix and its conjugate transpose. * - * @param NDArray|array $a - * @return NDArray + * @param NumPower|array $a + * @return NumPower */ - public static function cholesky(NDArray|array $a): NDArray {} + public static function cholesky(NumPower|array $a): NumPower {} /** * Return evenly spaced values within a given interval. @@ -1093,66 +1096,66 @@ public static function cholesky(NDArray|array $a): NDArray {} * @param float|int $stop * @param float|int $start * @param float|int $step - * @return NDArray + * @return NumPower */ - public static function arange(float|int $stop, float|int $start = 0, float|int $step = 1): NDArray {} + public static function arange(float|int $stop, float|int $start = 0, float|int $step = 1): NumPower {} /** * Remove axes of length one from $a. * - * @param NDArray|array $a Input array + * @param NumPower|array $a Input array * @param int|int[] $axis Selects a subset of the entries of length one in the shape. * If an axis is selected with shape entry greater than one, an error is raised. - * @return NDArray|float The input array, but with all or a subset of the dimensions of length 1 removed. + * @return NumPower|float The input array, but with all or a subset of the dimensions of length 1 removed. * This is always $a itself or $a view into $a. Note that if all axes are squeezed, * the result is a 0d array and not a scalar. */ - public static function squeeze(NDArray|array $a, int|array $axis): NDArray|float {} + public static function squeeze(NumPower|array $a, int|array $axis): NumPower|float {} /** * Extract a diagonal or construct a diagonal array. * - * @param NDArray|array $a - * @return NDArray + * @param NumPower|array $a + * @return NumPower */ - public static function diag(NDArray|array $a): NDArray {} + public static function diag(NumPower|array $a): NumPower {} /** * Return a new array of given shape and type, filled with $fill_value. * * @param int[] $shape Shape of the new array * @param float|int $fill_value Fill value - * @return NDArray + * @return NumPower */ - public static function full(array $shape, float|int $fill_value): NDArray {} + public static function full(array $shape, float|int $fill_value): NumPower {} /** * Fill the array with a scalar value. * * @param float|int $fill_value Fill value - * @return NDArray + * @return NumPower */ - public function fill(float|int $fill_value): NDArray {} + public function fill(float|int $fill_value): NumPower {} /** * Returns the indices of the minimum values along an axis. * - * @param NDArray|array $a Target array + * @param NumPower|array $a Target array * @param int|null $axis If NULL, the index is into the flattened array, otherwise along the specified axis. * @param bool $keepdims - * @return NDArray Array of indices into the array. It has the same shape as $a with the dimension along $axis removed. + * @return NumPower Array of indices into the array. It has the same shape as $a with the dimension along $axis removed. */ - public static function argmin(NDArray|array $a, ?int $axis, bool $keepdims = false): NDArray {} + public static function argmin(NumPower|array $a, ?int $axis, bool $keepdims = false): NumPower {} /** * Returns the indices of the maximum values along an axis. * - * @param NDArray|array $a Target array + * @param NumPower|array $a Target array * @param int|null $axis If NULL, the index is into the flattened array, otherwise along the specified axis. * @param bool $keepdims - * @return NDArray Array of indices into the array. It has the same shape as $a with the dimension along axis removed. + * @return NumPower Array of indices into the array. It has the same shape as $a with the dimension along axis removed. */ - public static function argmax(NDArray|array $a, ?int $axis, bool $keepdims = false): NDArray {} + public static function argmax(NumPower|array $a, ?int $axis, bool $keepdims = false): NumPower {} /** * Array slicing, each argument represents a slice of a dimension. @@ -1174,7 +1177,7 @@ public static function argmax(NDArray|array $a, ?int $axis, bool $keepdims = fal * $array->slice(0, [0,2]); * * @param array ...$indices - * @return NDArray|float + * @return NumPower|float */ - public function slice(...$indices): NDArray|float {} + public function slice(...$indices): NumPower|float {} } \ No newline at end of file diff --git a/tests/manipulation/005-ndarray-expanddims.phpt b/tests/manipulation/005-ndarray-expanddims.phpt index e7e7bdb..09f01b8 100644 --- a/tests/manipulation/005-ndarray-expanddims.phpt +++ b/tests/manipulation/005-ndarray-expanddims.phpt @@ -1,5 +1,5 @@ --TEST-- -NDArray::expand_dims +NDArray::expandDims --FILE-- toArray()); +print_r(nd::expandDims(1, 0)->toArray()); -print_r(nd::expand_dims([[1, 2, 3, 4]], 0)->toArray()); -print_r(nd::expand_dims($a, -1)->toArray()); -print_r(nd::expand_dims($a, -2)->toArray()); -print_r(nd::expand_dims($a, 1)->toArray()); +print_r(nd::expandDims([[1, 2, 3, 4]], 0)->toArray()); +print_r(nd::expandDims($a, -1)->toArray()); +print_r(nd::expandDims($a, -2)->toArray()); +print_r(nd::expandDims($a, 1)->toArray()); -print_r(nd::expand_dims($b, 0)->toArray()); -print_r(nd::expand_dims($b, -1)->toArray()); -print_r(nd::expand_dims($b, -2)->toArray()); -print_r(nd::expand_dims($b, 1)->toArray()); +print_r(nd::expandDims($b, 0)->toArray()); +print_r(nd::expandDims($b, -1)->toArray()); +print_r(nd::expandDims($b, -2)->toArray()); +print_r(nd::expandDims($b, 1)->toArray()); -print_r(nd::expand_dims($c, 0)->toArray()); -print_r(nd::expand_dims($c, -1)->toArray()); -print_r(nd::expand_dims($c, -2)->toArray()); -print_r(nd::expand_dims($c, 1)->toArray()); +print_r(nd::expandDims($c, 0)->toArray()); +print_r(nd::expandDims($c, -1)->toArray()); +print_r(nd::expandDims($c, -2)->toArray()); +print_r(nd::expandDims($c, 1)->toArray()); -print_r(nd::expand_dims($c, [0, -1, 1])->toArray()); -print_r(nd::expand_dims($c, [0, -1])->toArray()); -print_r(nd::expand_dims($c, [2, 1, 0])->toArray()); +print_r(nd::expandDims($c, [0, -1, 1])->toArray()); +print_r(nd::expandDims($c, [0, -1])->toArray()); +print_r(nd::expandDims($c, [2, 1, 0])->toArray()); ?> --EXPECT-- Array From 971eb213aa5099216e34646a18c258962e404867 Mon Sep 17 00:00:00 2001 From: Aleksei Nechaev Date: Tue, 25 Mar 2025 21:11:55 +0700 Subject: [PATCH 4/7] update initializers --- numpower.c | 39 +++++++++++++++++++++++++-------------- src/initializers.c | 40 ++++++++++++++++++++++------------------ src/initializers.h | 2 +- src/ndarray.h | 3 ++- 4 files changed, 50 insertions(+), 34 deletions(-) diff --git a/numpower.c b/numpower.c index cb23eea..4f85c0a 100644 --- a/numpower.c +++ b/numpower.c @@ -288,7 +288,6 @@ int arithmetic_do_operation(zend_uchar opcode, zval *result, zval *op1, zval *op return retval; } - static void ndarray_destructor(zend_object* object) { zval *obj_uuid = OBJ_PROP_NUM(object, 0); if (GC_REFCOUNT(object) <= 1 && Z_TYPE_P(obj_uuid) != IS_UNDEF) { @@ -959,7 +958,7 @@ PHP_METHOD(NDArray, normal) { NDArray *rtn = NULL; int *shape; zval* size; - long accelerator = 0; + long accelerator = NDARRAY_DEVICE_CPU; double loc = 0.0, scale = 1.0; ZEND_PARSE_PARAMETERS_START(1, 4) @@ -978,8 +977,7 @@ PHP_METHOD(NDArray, normal) { shape = emalloc(sizeof(int) * NDArray_NUMELEMENTS(nda)); for (int i = 0; i < NDArray_NUMELEMENTS(nda); i++) { - shape[i] = (int) NDArray_FDATA(nda)[i]; - + shape[i] = (int) NDArray_FDATA(nda)[i]; } rtn = NDArray_Normal(loc, scale, shape, NDArray_NUMELEMENTS(nda), accelerator_i); @@ -995,29 +993,42 @@ PHP_METHOD(NDArray, normal) { * @param return_value */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_truncated_normal, 0, 0, 1) -ZEND_ARG_INFO(0, size) -ZEND_ARG_INFO(0, loc) -ZEND_ARG_INFO(0, scale) + ZEND_ARG_INFO(0, size) + ZEND_ARG_INFO(0, loc) + ZEND_ARG_INFO(0, scale) + ZEND_ARG_INFO(0, accelerator) ZEND_END_ARG_INFO() PHP_METHOD(NDArray, truncatedNormal) { NDArray *rtn = NULL; int *shape; zval* size; + long accelerator = NDARRAY_DEVICE_CPU; double loc = 0.0, scale = 1.0; - ZEND_PARSE_PARAMETERS_START(1, 3) - Z_PARAM_ZVAL(size) + + ZEND_PARSE_PARAMETERS_START(1, 4) + Z_PARAM_ZVAL(size) Z_PARAM_OPTIONAL - Z_PARAM_DOUBLE(loc) - Z_PARAM_DOUBLE(scale) + Z_PARAM_DOUBLE(loc) + Z_PARAM_DOUBLE(scale) + Z_PARAM_LONG(accelerator) ZEND_PARSE_PARAMETERS_END(); + + int accelerator_i = (int) accelerator; NDArray *nda = ZVAL_TO_NDARRAY(size); - if (nda == NULL) return; + + if (nda == NULL) { + return; + } + shape = emalloc(sizeof(int) * NDArray_NUMELEMENTS(nda)); + for (int i = 0; i < NDArray_NUMELEMENTS(nda); i++) { - shape[i] = (int) NDArray_FDATA(nda)[i]; + shape[i] = (int) NDArray_FDATA(nda)[i]; } - rtn = NDArray_TruncatedNormal(loc, scale, shape, NDArray_NUMELEMENTS(nda)); + + rtn = NDArray_TruncatedNormal(loc, scale, shape, NDArray_NUMELEMENTS(nda), accelerator_i); NDArray_FREE(nda); + RETURN_NDARRAY(rtn, return_value); } diff --git a/src/initializers.c b/src/initializers.c index 34a0074..62eeae0 100644 --- a/src/initializers.c +++ b/src/initializers.c @@ -520,25 +520,29 @@ NDArray_Identity(int size) { * @return */ NDArray* -NDArray_TruncatedNormal(double loc, double scale, int* shape, int ndim) { +NDArray_TruncatedNormal(double loc, double scale, int* shape, int ndim, int accelerator) { NDArray *rtn; - rtn = NDArray_Zeros(shape, ndim, NDARRAY_TYPE_FLOAT32, NDARRAY_DEVICE_GPU); scale = scale / 0.88; - /*for (int i = 0; i < NDArray_NUMELEMENTS(rtn); i++) { - float z; - do { - float u1 = (float)rand() / (float)RAND_MAX; - float u2 = (float)rand() / (float)RAND_MAX; - z = sqrtf(-2.0f * logf(u1)) * cosf(2.0f * (float)M_PI * u2); - z = (float)loc + (float)scale * z; - NDArray_FDATA(rtn)[i] = z; - } while (z < (loc - 2.0 * scale) || z > (loc + 2.0 * scale)); + if (accelerator == NDARRAY_DEVICE_GPU) { +#ifdef HAVE_CUBLAS + rtn = NDArray_Zeros(shape, ndim, NDARRAY_TYPE_FLOAT32, NDARRAY_DEVICE_GPU); + int size = NDArray_NUMELEMENTS(rtn); + cuda_truncated_normal(NDArray_FDATA(rtn), size, loc, scale); +#endif + } else { + rtn = NDArray_Zeros(shape, ndim, NDARRAY_TYPE_FLOAT32, NDARRAY_DEVICE_CPU); + for (int i = 0; i < NDArray_NUMELEMENTS(rtn); i++) { + float z; + do { + float u1 = (float)rand() / (float)RAND_MAX; + float u2 = (float)rand() / (float)RAND_MAX; + z = sqrtf(-2.0f * logf(u1)) * cosf(2.0f * (float)M_PI * u2); + z = (float)loc + (float)scale * z; + NDArray_FDATA(rtn)[i] = z; + } while (z < (loc - 2.0 * scale) || z > (loc + 2.0 * scale)); + } } - - return rtn;*/ - int size = NDArray_NUMELEMENTS(rtn); - cuda_truncated_normal(NDArray_FDATA(rtn), size, loc, scale); return rtn; } @@ -555,7 +559,7 @@ NDArray_Normal(double loc, double scale, int* shape, int ndim, int accelerator) #ifdef HAVE_CUBLAS rtn = NDArray_Zeros(shape, ndim, NDARRAY_TYPE_FLOAT32, NDARRAY_DEVICE_GPU); if (rtn == NULL) { - return rtn; + return NULL; } int size = NDArray_NUMELEMENTS(rtn); @@ -578,7 +582,7 @@ NDArray_Normal(double loc, double scale, int* shape, int ndim, int accelerator) } else { rtn = NDArray_Zeros(shape, ndim, NDARRAY_TYPE_FLOAT32, NDARRAY_DEVICE_CPU); if (rtn == NULL) { - return rtn; + return NULL; } int size = NDArray_NUMELEMENTS(rtn); @@ -596,7 +600,7 @@ NDArray_Normal(double loc, double scale, int* shape, int ndim, int accelerator) float z1 = u * factor; NDArray_FDATA(rtn)[i] = (float)loc + (float)scale * z1; - if (i + 1 <= size) { + if (i + 1 < size) { float z2 = v * factor; NDArray_FDATA(rtn)[i+1] = (float)loc + (float)scale * z2; } diff --git a/src/initializers.h b/src/initializers.h index 6aa24ba..32e96eb 100644 --- a/src/initializers.h +++ b/src/initializers.h @@ -11,7 +11,7 @@ NDArray* NDArray_Zeros(int *shape, int ndim, const char *type, int device); NDArray* NDArray_Ones(int *shape, int ndim, const char *type); NDArray* NDArray_Identity(int size); NDArray* NDArray_Normal(double loc, double scale, int* shape, int ndim, int accelerator); -NDArray* NDArray_TruncatedNormal(double loc, double scale, int* shape, int ndim); +NDArray* NDArray_TruncatedNormal(double loc, double scale, int* shape, int ndim, int accelerator); NDArray* NDArray_StandardNormal(int* shape, int ndim); NDArray* NDArray_Poisson(double lam, int* shape, int ndim); NDArray* NDArray_Uniform(double low, double high, int* shape, int ndim); diff --git a/src/ndarray.h b/src/ndarray.h index 014e029..f1d1e4f 100644 --- a/src/ndarray.h +++ b/src/ndarray.h @@ -137,8 +137,9 @@ typedef float (*ElementWiseDoubleOperation)(float); typedef float (*ElementWiseFloatOperation2F)(float, float, float); typedef float (*ElementWiseFloatOperation1F)(float, float); NDArray* NDArray_Map(NDArray *array, ElementWiseDoubleOperation op); +NDArray* NDArray_Map_Zval(NDArray *array, zval *callback); NDArray* NDArray_Map2F(NDArray *array, ElementWiseFloatOperation2F op, float val1, float val2); NDArray* NDArray_Map1F(NDArray *array, ElementWiseFloatOperation1F op, float val1); -NDArray * NDArray_Map1ND(NDArray *array, ElementWiseFloatOperation1F op, NDArray *val1); +NDArray* NDArray_Map1ND(NDArray *array, ElementWiseFloatOperation1F op, NDArray *val1); #endif //PHPSCI_NDARRAY_NDARRAY_H From 781b47838cdfc3816c84553f8c66853b97e38779 Mon Sep 17 00:00:00 2001 From: Aleksei Nechaev Date: Wed, 26 Mar 2025 08:19:57 +0700 Subject: [PATCH 5/7] Separation of classes --- numpower.c | 845 ++++++++++++++++++++++++++----------------------- php_numpower.h | 1 + src/debug.c | 5 +- 3 files changed, 450 insertions(+), 401 deletions(-) diff --git a/numpower.c b/numpower.c index 4f85c0a..475aac6 100644 --- a/numpower.c +++ b/numpower.c @@ -46,6 +46,7 @@ #endif static zend_object_handlers ndarray_object_handlers; +static zend_object_handlers numpower_object_handlers; static zend_object_handlers arithmetic_object_handlers; int * @@ -100,7 +101,7 @@ NDArray* ZVAL_TO_NDARRAY(zval* obj) { zend_class_entry* ce = NULL; ce = Z_OBJCE_P(obj); zend_string* class_name = Z_OBJ_P(obj)->ce->name; - if (strcmp(ZSTR_VAL(class_name), "NumPower") == 0) { + if (strcmp(ZSTR_VAL(class_name), "NDArray") == 0) { if (ce == phpsci_ce_NDArray) { return buffer_get(get_object_uuid(obj)); } @@ -112,7 +113,7 @@ NDArray* ZVAL_TO_NDARRAY(zval* obj) { } #endif } - zend_throw_error(NULL, "argument must be an array, long, double, gdimage or NumPower."); + zend_throw_error(NULL, "argument must be an array, long, double, gdimage or NDArray."); return NULL; } @@ -190,6 +191,11 @@ typedef struct { int value; } NDArrayObject; +typedef struct { + zend_object std; + int value; +} NumPowerObject; + static int ndarray_do_operation_ex(zend_uchar opcode, zval *result, zval *op1, zval *op2) { /* {{{ */ NDArray *nda = ZVAL_TO_NDARRAY(op1); NDArray *ndb = ZVAL_TO_NDARRAY(op2); @@ -303,6 +309,10 @@ static void ndarray_objects_init(zend_class_entry *class_type) { ndarray_object_handlers.free_obj = ndarray_destructor; } +static void numpower_objects_init(zend_class_entry *class_type) { + memcpy(&numpower_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));\ +} + static void arithmetic_objects_init(zend_class_entry *class_type) { memcpy(&arithmetic_object_handlers, &std_object_handlers, sizeof(zend_object_handlers)); arithmetic_object_handlers.do_operation = arithmetic_do_operation; @@ -318,6 +328,14 @@ static zend_object *ndarray_create_object(zend_class_entry *class_type) { return &intern->std; } +static zend_object *numpower_create_object(zend_class_entry *class_type) { + NumPowerObject *intern = zend_object_alloc(sizeof(NumPowerObject), class_type); + zend_object_std_init(&intern->std, class_type); + object_properties_init(&intern->std, class_type); + intern->std.handlers = &numpower_object_handlers; + return &intern->std; +} + static zend_object *arithmetic_create_object(zend_class_entry *class_type) { NDArrayObject *intern = zend_object_alloc(sizeof(NDArrayObject), class_type); zend_object_std_init(&intern->std, class_type); @@ -414,6 +432,13 @@ PHP_METHOD(ArithmeticOperand, __construct) { ZEND_PARSE_PARAMETERS_END(); } +ZEND_BEGIN_ARG_INFO(arginfo_NumPower_construct, 0) +ZEND_END_ARG_INFO(); +PHP_METHOD(NumPower, __construct) { + zend_object *obj = Z_OBJ_P(ZEND_THIS); + ZEND_PARSE_PARAMETERS_START(0, 0) + ZEND_PARSE_PARAMETERS_END(); +} /* }}}*/ ZEND_BEGIN_ARG_INFO(arginfo_construct, 1) @@ -573,17 +598,29 @@ PHP_METHOD(NDArray, dump) { ZEND_BEGIN_ARG_INFO(arginfo_dump_devices, 0) ZEND_END_ARG_INFO(); -PHP_METHOD(NDArray, dumpDevices) { +PHP_METHOD(NumPower, dumpDevices) { ZEND_PARSE_PARAMETERS_START(0, 0) ZEND_PARSE_PARAMETERS_END(); NDArray_DumpDevices(); } +ZEND_BEGIN_ARG_INFO(arginfo_load, 0) + ZEND_ARG_INFO(0, name) +ZEND_END_ARG_INFO(); +PHP_METHOD(NumPower, load) { + zend_string *name; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_STR(name) + ZEND_PARSE_PARAMETERS_END(); + NDArray *rtn = NDArray_Load(name->val); + RETURN_NDARRAY(rtn, return_value); +} + ZEND_BEGIN_ARG_INFO(arginfo_save, 0) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, name) ZEND_END_ARG_INFO(); -PHP_METHOD(NDArray, save) { +PHP_METHOD(NumPower, save) { zval *a; zend_string *name; ZEND_PARSE_PARAMETERS_START(2, 2) @@ -596,22 +633,10 @@ PHP_METHOD(NDArray, save) { RETURN_NULL(); } -ZEND_BEGIN_ARG_INFO(arginfo_load, 0) - ZEND_ARG_INFO(0, a) -ZEND_END_ARG_INFO(); -PHP_METHOD(NDArray, load) { - zend_string *name; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_STR(name) - ZEND_PARSE_PARAMETERS_END(); - NDArray *rtn = NDArray_Load(name->val); - RETURN_NDARRAY(rtn, return_value); -} - ZEND_BEGIN_ARG_INFO(arginfo_setdevice, 0) ZEND_ARG_INFO(0, deviceId) ZEND_END_ARG_INFO(); -PHP_METHOD(NDArray, setDevice) { +PHP_METHOD(NumPower, setDevice) { int numDevices; long deviceId; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -702,7 +727,7 @@ PHP_FUNCTION(print_r_) { } /** - * NDArray::zeros + * NumPower::zeros * * @param execute_data * @param return_value @@ -710,7 +735,7 @@ PHP_FUNCTION(print_r_) { ZEND_BEGIN_ARG_INFO(arginfo_ndarray_zeros, 1) ZEND_ARG_INFO(0, shape_zval) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, zeros) { +PHP_METHOD(NumPower, zeros) { NDArray *rtn = NULL; int *shape; zval *shape_zval; @@ -731,7 +756,7 @@ PHP_METHOD(NDArray, zeros) { } /** - * NDArray::equal + * NumPower::equal * * @param execute_data * @param return_value @@ -740,7 +765,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_equal, 2) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, equal) { +PHP_METHOD(NumPower, equal) { NDArray *nda, *ndb, *rtn = NULL; zval *a, *b; ZEND_PARSE_PARAMETERS_START(2, 2) @@ -763,7 +788,7 @@ PHP_METHOD(NDArray, equal) { } /** - * NDArray::greater + * NumPower::greater * * @param execute_data * @param return_value @@ -772,7 +797,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_greater, 2) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, greater) { +PHP_METHOD(NumPower, greater) { NDArray *nda, *ndb, *rtn = NULL; zval *a, *b; ZEND_PARSE_PARAMETERS_START(2, 2) @@ -795,7 +820,7 @@ PHP_METHOD(NDArray, greater) { } /** - * NDArray::greaterEqual + * NumPower::greaterEqual * * @param execute_data * @param return_value @@ -804,7 +829,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_greaterequal, 2) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, greaterEqual) { +PHP_METHOD(NumPower, greaterEqual) { NDArray *nda, *ndb, *rtn = NULL; zval *a, *b; ZEND_PARSE_PARAMETERS_START(2, 2) @@ -827,7 +852,7 @@ PHP_METHOD(NDArray, greaterEqual) { } /** - * NDArray::less + * NumPower::less * * @param execute_data * @param return_value @@ -836,7 +861,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_less, 2) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, less) { +PHP_METHOD(NumPower, less) { NDArray *nda, *ndb, *rtn = NULL; zval *a, *b; ZEND_PARSE_PARAMETERS_START(2, 2) @@ -859,7 +884,7 @@ PHP_METHOD(NDArray, less) { } /** - * NDArray::lessEqual + * NumPower::lessEqual * * @param execute_data * @param return_value @@ -868,7 +893,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_lessequal, 2) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, lessEqual) { +PHP_METHOD(NumPower, lessEqual) { NDArray *nda, *ndb, *rtn = NULL; zval *a, *b; ZEND_PARSE_PARAMETERS_START(2, 2) @@ -891,7 +916,7 @@ PHP_METHOD(NDArray, lessEqual) { } /** - * NDArray::notEqual + * NumPower::notEqual * * @param execute_data * @param return_value @@ -900,7 +925,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_notequal, 2) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, notEqual) { +PHP_METHOD(NumPower, notEqual) { NDArray *nda, *ndb, *rtn = NULL; zval *a, *b; ZEND_PARSE_PARAMETERS_START(2, 2) @@ -923,7 +948,7 @@ PHP_METHOD(NDArray, notEqual) { } /** - * NDArray::identity + * NumPower::identity * * @param execute_data * @param return_value @@ -931,7 +956,7 @@ PHP_METHOD(NDArray, notEqual) { ZEND_BEGIN_ARG_INFO(arginfo_ndarray_identity, 1) ZEND_ARG_INFO(0, size) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, identity) { +PHP_METHOD(NumPower, identity) { NDArray *rtn = NULL; int *shape; long size; @@ -943,7 +968,7 @@ PHP_METHOD(NDArray, identity) { } /** - * NDArray::normal + * NumPower::normal * * @param execute_data * @param return_value @@ -954,7 +979,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_normal, 0, 0, 1) ZEND_ARG_INFO(0, scale) ZEND_ARG_INFO(0, accelerator) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, normal) { +PHP_METHOD(NumPower, normal) { NDArray *rtn = NULL; int *shape; zval* size; @@ -987,7 +1012,7 @@ PHP_METHOD(NDArray, normal) { } /** - * NDArray::truncatedNormal + * NumPower::truncatedNormal * * @param execute_data * @param return_value @@ -998,7 +1023,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_truncated_normal, 0, 0, 1) ZEND_ARG_INFO(0, scale) ZEND_ARG_INFO(0, accelerator) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, truncatedNormal) { +PHP_METHOD(NumPower, truncatedNormal) { NDArray *rtn = NULL; int *shape; zval* size; @@ -1033,7 +1058,7 @@ PHP_METHOD(NDArray, truncatedNormal) { } /** - * NDArray::randomBinomial + * NumPower::randomBinomial * * @param execute_data * @param return_value @@ -1043,7 +1068,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_binomial, 0, 0, 3) ZEND_ARG_INFO(0, p) ZEND_ARG_INFO(0, n) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, randomBinomial) { +PHP_METHOD(NumPower, randomBinomial) { NDArray *rtn = NULL; int *ishape; zval* shape; @@ -1065,7 +1090,7 @@ PHP_METHOD(NDArray, randomBinomial) { } /** - * NDArray::standardNormal + * NumPower::standardNormal * * @param shape */ @@ -1073,7 +1098,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_standard_normal, 1) ZEND_ARG_ARRAY_INFO(0, shape, 0) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, standardNormal) { +PHP_METHOD(NumPower, standardNormal) { NDArray *rtn = NULL; zval* shape; HashTable *shape_ht; @@ -1113,7 +1138,7 @@ PHP_METHOD(NDArray, standardNormal) { } /** - * NDArray::poisson + * NumPower::poisson * * @param execute_data * @param return_value @@ -1122,7 +1147,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_poisson, 0, 0, 1) ZEND_ARG_ARRAY_INFO(0, shape, 0) ZEND_ARG_TYPE_INFO(0, lam, IS_DOUBLE, 0) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, poisson) { +PHP_METHOD(NumPower, poisson) { NDArray *rtn = NULL; zval* shape; HashTable *shape_ht; @@ -1165,7 +1190,7 @@ PHP_METHOD(NDArray, poisson) { } /** - * NDArray::poisson + * NumPower::uniform * * @param execute_data * @param return_value @@ -1175,7 +1200,7 @@ ZEND_ARG_INFO(0, size) ZEND_ARG_INFO(0, low) ZEND_ARG_INFO(0, high) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, uniform) { +PHP_METHOD(NumPower, uniform) { NDArray *rtn = NULL; int *shape; zval* size; @@ -1200,7 +1225,7 @@ PHP_METHOD(NDArray, uniform) { } /** - * NDArray::diag + * NumPower::diag * * @param execute_data * @param return_value @@ -1208,7 +1233,7 @@ PHP_METHOD(NDArray, uniform) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_diag, 0, 0, 1) ZEND_ARG_INFO(0, target) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, diag) { +PHP_METHOD(NumPower, diag) { NDArray *rtn = NULL; zval* target; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1224,7 +1249,7 @@ PHP_METHOD(NDArray, diag) { } /** - * NDArray::diag + * NumPower::diagonal * * @param execute_data * @param return_value @@ -1232,7 +1257,7 @@ PHP_METHOD(NDArray, diag) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_diagonal, 0, 0, 1) ZEND_ARG_INFO(0, target) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, diagonal) { +PHP_METHOD(NumPower, diagonal) { NDArray *rtn = NULL; zval* target; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1248,7 +1273,7 @@ PHP_METHOD(NDArray, diagonal) { } /** - * NDArray::full + * NumPower::full * * @param execute_data * @param return_value @@ -1257,7 +1282,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_full, 0, 0, 2) ZEND_ARG_INFO(0, shape) ZEND_ARG_INFO(0, fill_value) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, full) { +PHP_METHOD(NumPower, full) { NDArray *rtn = NULL; zval* shape; HashTable *shape_ht; @@ -1300,7 +1325,7 @@ PHP_METHOD(NDArray, full) { } /** - * NDArray::ones + * NumPower::ones * * @param execute_data * @param return_value @@ -1308,7 +1333,7 @@ PHP_METHOD(NDArray, full) { ZEND_BEGIN_ARG_INFO(arginfo_ndarray_ones, 1) ZEND_ARG_INFO(0, shape_zval) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, ones) { +PHP_METHOD(NumPower, ones) { double *ptr; NDArray *rtn = NULL; int *shape; @@ -1327,7 +1352,7 @@ PHP_METHOD(NDArray, ones) { } /** - * NDArray::arange + * NumPower::arange * * @param execute_data * @param return_value @@ -1337,7 +1362,7 @@ ZEND_ARG_INFO(0, stop) ZEND_ARG_INFO(0, start) ZEND_ARG_INFO(0, step) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, arange) { +PHP_METHOD(NumPower, arange) { NDArray *rtn = NULL; double start, stop, step; ZEND_PARSE_PARAMETERS_START(1, 3) @@ -1358,7 +1383,7 @@ PHP_METHOD(NDArray, arange) { } /** - * NDArray::all + * NumPower::all * * @param execute_data * @param return_value @@ -1367,7 +1392,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_all, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, all) { +PHP_METHOD(NumPower, all) { NDArray *rtn = NULL; zval *array; long axis; @@ -1400,7 +1425,7 @@ PHP_METHOD(NDArray, all) { } /** - * NDArray::allClose + * NumPower::allClose * * @param execute_data * @param return_value @@ -1411,7 +1436,7 @@ ZEND_ARG_INFO(0, b) ZEND_ARG_INFO(0, rtol) ZEND_ARG_INFO(0, atol) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, allClose) { +PHP_METHOD(NumPower, allClose) { zval *a, *b; double rtol = 1e-05, atol = 1e-08; int rtn; @@ -1447,7 +1472,7 @@ PHP_METHOD(NDArray, allClose) { } /** - * NDArray::transpose + * NumPower::transpose * * @param execute_data * @param return_value @@ -1456,7 +1481,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_transpose, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_ARG_INFO(0, axes) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, transpose) { +PHP_METHOD(NumPower, transpose) { NDArray *rtn = NULL; zval *array, *axes; HashTable *axes_ht; @@ -1496,7 +1521,7 @@ PHP_METHOD(NDArray, transpose) { } /** - * NDArray::copy + * NumPower::copy * * @param execute_data * @param return_value @@ -1505,7 +1530,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_copy, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_ARG_INFO(0, device) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, copy) { +PHP_METHOD(NumPower, copy) { NDArray *rtn = NULL; zval *array; long device = -1; @@ -1532,7 +1557,7 @@ PHP_METHOD(NDArray, copy) { } /** - * NDArray::atleast1d + * NumPower::atleast1d * * @param execute_data * @param return_value @@ -1540,7 +1565,7 @@ PHP_METHOD(NDArray, copy) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_atleast_1d, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, atleast1d) { +PHP_METHOD(NumPower, atleast1d) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1557,7 +1582,7 @@ PHP_METHOD(NDArray, atleast1d) { } /** - * NDArray::atleast2d + * NumPower::atleast2d * * @param execute_data * @param return_value @@ -1565,7 +1590,7 @@ PHP_METHOD(NDArray, atleast1d) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_atleast_2d, 0, 0, 1) ZEND_ARG_INFO(0, a) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, atleast2d) { +PHP_METHOD(NumPower, atleast2d) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1582,7 +1607,7 @@ PHP_METHOD(NDArray, atleast2d) { } /** - * NDArray::atleast3d + * NumPower::atleast3d * * @param execute_data * @param return_value @@ -1591,7 +1616,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_atleast_3d, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, atleast3d) { +PHP_METHOD(NumPower, atleast3d) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1629,7 +1654,7 @@ PHP_METHOD(NDArray, shape) { } /** - * NDArray::flatten + * NumPower::flatten * * @param execute_data * @param return_value @@ -1637,7 +1662,7 @@ PHP_METHOD(NDArray, shape) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_flat, 0, 0, 0) ZEND_ARG_INFO(0, a) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, flatten) { +PHP_METHOD(NumPower, flatten) { NDArray *rtn = NULL; zval *a; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1653,7 +1678,7 @@ PHP_METHOD(NDArray, flatten) { } /** - * NDArray::abs + * NumPower::abs * * @param execute_data * @param return_value @@ -1661,7 +1686,7 @@ PHP_METHOD(NDArray, flatten) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_abs, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, abs) { +PHP_METHOD(NumPower, abs) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1681,7 +1706,7 @@ PHP_METHOD(NDArray, abs) { } /** - * NDArray::sin + * NumPower::sin * * @param execute_data * @param return_value @@ -1689,7 +1714,7 @@ PHP_METHOD(NDArray, abs) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_sin, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, sin) { +PHP_METHOD(NumPower, sin) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1716,7 +1741,7 @@ PHP_METHOD(NDArray, sin) { } /** - * NDArray::cos + * NumPower::cos * * @param execute_data * @param return_value @@ -1724,7 +1749,7 @@ PHP_METHOD(NDArray, sin) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_cos, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, cos) { +PHP_METHOD(NumPower, cos) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1751,7 +1776,7 @@ PHP_METHOD(NDArray, cos) { } /** - * NDArray::sin + * NumPower::sin * * @param execute_data * @param return_value @@ -1759,7 +1784,7 @@ PHP_METHOD(NDArray, cos) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_tan, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, tan) { +PHP_METHOD(NumPower, tan) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1786,7 +1811,7 @@ PHP_METHOD(NDArray, tan) { } /** - * NDArray::arcsin + * NumPower::arcsin * * @param execute_data * @param return_value @@ -1794,7 +1819,7 @@ PHP_METHOD(NDArray, tan) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_arcsin, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, arcsin) { +PHP_METHOD(NumPower, arcsin) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1821,7 +1846,7 @@ PHP_METHOD(NDArray, arcsin) { } /** - * NDArray::rsqrt + * NumPower::rsqrt * * @param execute_data * @param return_value @@ -1829,7 +1854,7 @@ PHP_METHOD(NDArray, arcsin) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_rsqrt, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, rsqrt) { +PHP_METHOD(NumPower, rsqrt) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1856,7 +1881,7 @@ PHP_METHOD(NDArray, rsqrt) { } /** - * NDArray::arccos + * NumPower::arccos * * @param execute_data * @param return_value @@ -1864,7 +1889,7 @@ PHP_METHOD(NDArray, rsqrt) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_arccos, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, arccos) { +PHP_METHOD(NumPower, arccos) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1891,7 +1916,7 @@ PHP_METHOD(NDArray, arccos) { } /** - * NDArray::arccos + * NumPower::arccos * * @param execute_data * @param return_value @@ -1899,7 +1924,7 @@ PHP_METHOD(NDArray, arccos) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_arctan, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, arctan) { +PHP_METHOD(NumPower, arctan) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1926,7 +1951,7 @@ PHP_METHOD(NDArray, arctan) { } /** - * NDArray::arctan2 + * NumPower::arctan2 * * @param execute_data * @param return_value @@ -1935,7 +1960,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_arctan2, 0, 0, 2) ZEND_ARG_INFO(0, x) ZEND_ARG_INFO(0, y) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, arctan2) { +PHP_METHOD(NumPower, arctan2) { NDArray *rtn = NULL; zval *x, *y; ZEND_PARSE_PARAMETERS_START(2, 2) @@ -1963,7 +1988,7 @@ PHP_METHOD(NDArray, arctan2) { } /** - * NDArray::degrees + * NumPower::degrees * * @param execute_data * @param return_value @@ -1971,7 +1996,7 @@ PHP_METHOD(NDArray, arctan2) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_degrees, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, degrees) { +PHP_METHOD(NumPower, degrees) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -1998,7 +2023,7 @@ PHP_METHOD(NDArray, degrees) { } /** - * NDArray::sinh + * NumPower::sinh * * @param execute_data * @param return_value @@ -2006,7 +2031,7 @@ PHP_METHOD(NDArray, degrees) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_sinh, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, sinh) { +PHP_METHOD(NumPower, sinh) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -2033,7 +2058,7 @@ PHP_METHOD(NDArray, sinh) { } /** - * NDArray::cosh + * NumPower::cosh * * @param execute_data * @param return_value @@ -2041,7 +2066,7 @@ PHP_METHOD(NDArray, sinh) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_cosh, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, cosh) { +PHP_METHOD(NumPower, cosh) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -2068,7 +2093,7 @@ PHP_METHOD(NDArray, cosh) { } /** - * NDArray::tanh + * NumPower::tanh * * @param execute_data * @param return_value @@ -2076,7 +2101,7 @@ PHP_METHOD(NDArray, cosh) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_tanh, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, tanh) { +PHP_METHOD(NumPower, tanh) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -2103,7 +2128,7 @@ PHP_METHOD(NDArray, tanh) { } /** - * NDArray::arcsinh + * NumPower::arcsinh * * @param execute_data * @param return_value @@ -2111,7 +2136,7 @@ PHP_METHOD(NDArray, tanh) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_arcsinh, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, arcsinh) { +PHP_METHOD(NumPower, arcsinh) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -2138,7 +2163,7 @@ PHP_METHOD(NDArray, arcsinh) { } /** - * NDArray::arccosh + * NumPower::arccosh * * @param execute_data * @param return_value @@ -2146,7 +2171,7 @@ PHP_METHOD(NDArray, arcsinh) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_arccosh, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, arccosh) { +PHP_METHOD(NumPower, arccosh) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -2173,7 +2198,7 @@ PHP_METHOD(NDArray, arccosh) { } /** - * NDArray::fromImage + * NumPower::fromImage * * @param execute_data * @param return_value @@ -2182,7 +2207,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_fromimage, 0, 0, 1) ZEND_ARG_INFO(0, image) ZEND_ARG_INFO(0, channelLast) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, fromImage) { +PHP_METHOD(NumPower, fromImage) { NDArray *rtn = NULL; zval *image; bool channelLast = true; @@ -2196,7 +2221,7 @@ PHP_METHOD(NDArray, fromImage) { } /** - * NDArray::arctanh + * NumPower::arctanh * * @param execute_data * @param return_value @@ -2204,7 +2229,7 @@ PHP_METHOD(NDArray, fromImage) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_arctanh, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, arctanh) { +PHP_METHOD(NumPower, arctanh) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -2231,7 +2256,7 @@ PHP_METHOD(NDArray, arctanh) { } /** - * NDArray::rint + * NumPower::rint * * @param execute_data * @param return_value @@ -2239,7 +2264,7 @@ PHP_METHOD(NDArray, arctanh) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_rint, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, rint) { +PHP_METHOD(NumPower, rint) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -2266,7 +2291,7 @@ PHP_METHOD(NDArray, rint) { } /** - * NDArray::fix + * NumPower::fix * * @param execute_data * @param return_value @@ -2274,7 +2299,7 @@ PHP_METHOD(NDArray, rint) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_fix, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, fix) { +PHP_METHOD(NumPower, fix) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -2301,7 +2326,7 @@ PHP_METHOD(NDArray, fix) { } /** - * NDArray::trunc + * NumPower::trunc * * @param execute_data * @param return_value @@ -2309,7 +2334,7 @@ PHP_METHOD(NDArray, fix) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_trunc, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, trunc) { +PHP_METHOD(NumPower, trunc) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -2336,7 +2361,7 @@ PHP_METHOD(NDArray, trunc) { } /** - * NDArray::sinc + * NumPower::sinc * * @param execute_data * @param return_value @@ -2344,7 +2369,7 @@ PHP_METHOD(NDArray, trunc) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_sinc, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, sinc) { +PHP_METHOD(NumPower, sinc) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -2371,7 +2396,7 @@ PHP_METHOD(NDArray, sinc) { } /** - * NDArray::negative + * NumPower::negative * * @param execute_data * @param return_value @@ -2379,7 +2404,7 @@ PHP_METHOD(NDArray, sinc) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_negative, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, negative) { +PHP_METHOD(NumPower, negative) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -2406,7 +2431,7 @@ PHP_METHOD(NDArray, negative) { } /** - * NDArray::positive + * NumPower::positive * * @param execute_data * @param return_value @@ -2414,7 +2439,7 @@ PHP_METHOD(NDArray, negative) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_positive, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, positive) { +PHP_METHOD(NumPower, positive) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -2441,7 +2466,7 @@ PHP_METHOD(NDArray, positive) { } /** - * NDArray::reciprocal + * NumPower::reciprocal * * @param execute_data * @param return_value @@ -2449,7 +2474,7 @@ PHP_METHOD(NDArray, positive) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_reciprocal, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, reciprocal) { +PHP_METHOD(NumPower, reciprocal) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -2477,7 +2502,7 @@ PHP_METHOD(NDArray, reciprocal) { /** - * NDArray::sign + * NumPower::sign * * @param execute_data * @param return_value @@ -2485,7 +2510,7 @@ PHP_METHOD(NDArray, reciprocal) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_sign, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, sign) { +PHP_METHOD(NumPower, sign) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -2512,7 +2537,7 @@ PHP_METHOD(NDArray, sign) { } /** - * NDArray::clip + * NumPower::clip * * @param execute_data * @param return_value @@ -2522,7 +2547,7 @@ ZEND_ARG_INFO(0, array) ZEND_ARG_INFO(0, min) ZEND_ARG_INFO(0, max) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, clip) { +PHP_METHOD(NumPower, clip) { NDArray *rtn = NULL; zval *array; double min, max; @@ -2555,7 +2580,7 @@ PHP_METHOD(NDArray, clip) { } /** - * NDArray::maximum + * NumPower::maximum * * @param execute_data * @param return_value @@ -2564,7 +2589,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_maximum, 0, 0, 1) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, maximum) { +PHP_METHOD(NumPower, maximum) { NDArray *rtn = NULL; zval *a, *b; ZEND_PARSE_PARAMETERS_START(2, 2) @@ -2585,7 +2610,7 @@ PHP_METHOD(NDArray, maximum) { } /** - * NDArray::minimum + * NumPower::minimum * * @param execute_data * @param return_value @@ -2594,7 +2619,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_minimum, 0, 0, 1) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, minimum) { +PHP_METHOD(NumPower, minimum) { NDArray *rtn = NULL; zval *a, *b; ZEND_PARSE_PARAMETERS_START(2, 2) @@ -2616,7 +2641,7 @@ PHP_METHOD(NDArray, minimum) { /** - * NDArray::argmax + * NumPower::argmax * * @param execute_data * @param return_value @@ -2626,7 +2651,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_argmax, 0, 0, 1) ZEND_ARG_INFO(0, axis) ZEND_ARG_INFO(0, keepdims) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, argmax) { +PHP_METHOD(NumPower, argmax) { NDArray *rtn = NULL; zval *a; long axis; @@ -2651,7 +2676,7 @@ PHP_METHOD(NDArray, argmax) { } /** - * NDArray::argmin + * NumPower::argmin * * @param execute_data * @param return_value @@ -2661,7 +2686,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_argmin, 0, 0, 1) ZEND_ARG_INFO(0, axis) ZEND_ARG_INFO(0, keepdims) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, argmin) { +PHP_METHOD(NumPower, argmin) { NDArray *rtn = NULL; zval *a; long axis; @@ -2686,7 +2711,7 @@ PHP_METHOD(NDArray, argmin) { } /** - * NDArray::mean + * NumPower::mean * * @param execute_data * @param return_value @@ -2695,7 +2720,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_mean, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, mean) { +PHP_METHOD(NumPower, mean) { NDArray *rtn = NULL; zval *array; long axis; @@ -2744,7 +2769,7 @@ PHP_METHOD(NDArray, mean) { } /** - * NDArray::median + * NumPower::median * * @param execute_data * @param return_value @@ -2753,7 +2778,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_median, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, median) { +PHP_METHOD(NumPower, median) { NDArray *rtn = NULL; zval *array; long axis; @@ -2787,7 +2812,7 @@ PHP_METHOD(NDArray, median) { } /** - * NDArray::std + * NumPower::std * * @param execute_data * @param return_value @@ -2796,7 +2821,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_std, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, std) { +PHP_METHOD(NumPower, std) { NDArray *rtn = NULL; zval *array; long axis; @@ -2833,7 +2858,7 @@ PHP_METHOD(NDArray, std) { } /** - * NDArray::quantile + * NumPower::quantile * * @param execute_data * @param return_value @@ -2841,7 +2866,7 @@ PHP_METHOD(NDArray, std) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_quantile, 0, 0, 1) ZEND_ARG_INFO(0, target) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, quantile) { +PHP_METHOD(NumPower, quantile) { NDArray *rtn = NULL; zval *a, *q; long axis; @@ -2868,7 +2893,7 @@ PHP_METHOD(NDArray, quantile) { } /** - * NDArray::std + * NumPower::std * * @param execute_data * @param return_value @@ -2877,7 +2902,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_average, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_ARG_INFO(0, weights) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, average) { +PHP_METHOD(NumPower, average) { NDArray *rtn = NULL; zval *array, *weights = NULL; long axis; @@ -2905,7 +2930,7 @@ PHP_METHOD(NDArray, average) { } /** - * NDArray::variance + * NumPower::variance * * @param execute_data * @param return_value @@ -2914,7 +2939,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_variance, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, variance) { +PHP_METHOD(NumPower, variance) { NDArray *rtn = NULL; zval *array; long axis; @@ -2951,7 +2976,7 @@ PHP_METHOD(NDArray, variance) { } /** - * NDArray::ceil + * NumPower::ceil * * @param execute_data * @param return_value @@ -2959,7 +2984,7 @@ PHP_METHOD(NDArray, variance) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_ceil, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, ceil) { +PHP_METHOD(NumPower, ceil) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -2986,7 +3011,7 @@ PHP_METHOD(NDArray, ceil) { } /** - * NDArray::round + * NumPower::round * * @param execute_data * @param return_value @@ -2995,7 +3020,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_round, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_ARG_INFO(0, precision) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, round) { +PHP_METHOD(NumPower, round) { NDArray *rtn = NULL; zval *array; long precision; @@ -3024,7 +3049,7 @@ PHP_METHOD(NDArray, round) { } /** - * NDArray::floor + * NumPower::floor * * @param execute_data * @param return_value @@ -3032,7 +3057,7 @@ PHP_METHOD(NDArray, round) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_floor, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, floor) { +PHP_METHOD(NumPower, floor) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -3059,7 +3084,7 @@ PHP_METHOD(NDArray, floor) { } /** - * NDArray::arccos + * NumPower::arccos * * @param execute_data * @param return_value @@ -3067,7 +3092,7 @@ PHP_METHOD(NDArray, floor) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_radians, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, radians) { +PHP_METHOD(NumPower, radians) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -3094,7 +3119,7 @@ PHP_METHOD(NDArray, radians) { } /** - * NDArray::sqrt + * NumPower::sqrt * * @param execute_data * @param return_value @@ -3102,7 +3127,7 @@ PHP_METHOD(NDArray, radians) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_sqrt, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, sqrt) { +PHP_METHOD(NumPower, sqrt) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -3128,7 +3153,7 @@ PHP_METHOD(NDArray, sqrt) { } /** - * NDArray::sqrt + * NumPower::sqrt * * @param execute_data * @param return_value @@ -3136,7 +3161,7 @@ PHP_METHOD(NDArray, sqrt) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_square, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, square) { +PHP_METHOD(NumPower, square) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -3154,7 +3179,7 @@ PHP_METHOD(NDArray, square) { } /** - * NDArray::exp + * NumPower::exp * * @param execute_data * @param return_value @@ -3162,7 +3187,7 @@ PHP_METHOD(NDArray, square) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_exp, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, exp) { +PHP_METHOD(NumPower, exp) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -3188,7 +3213,7 @@ PHP_METHOD(NDArray, exp) { } /** - * NDArray::exp2 + * NumPower::exp2 * * @param execute_data * @param return_value @@ -3196,7 +3221,7 @@ PHP_METHOD(NDArray, exp) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_exp2, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, exp2) { +PHP_METHOD(NumPower, exp2) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -3214,7 +3239,7 @@ PHP_METHOD(NDArray, exp2) { } /** - * NDArray::exp2 + * NumPower::exp2 * * @param execute_data * @param return_value @@ -3222,7 +3247,7 @@ PHP_METHOD(NDArray, exp2) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_expm1, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, expm1) { +PHP_METHOD(NumPower, expm1) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -3249,7 +3274,7 @@ PHP_METHOD(NDArray, expm1) { } /** - * NDArray::log + * NumPower::log * * @param execute_data * @param return_value @@ -3257,7 +3282,7 @@ PHP_METHOD(NDArray, expm1) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_log, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, log) { +PHP_METHOD(NumPower, log) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -3283,7 +3308,7 @@ PHP_METHOD(NDArray, log) { } /** - * NDArray::logb + * NumPower::logb * * @param execute_data * @param return_value @@ -3291,7 +3316,7 @@ PHP_METHOD(NDArray, log) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_logb, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, logb) { +PHP_METHOD(NumPower, logb) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -3315,7 +3340,7 @@ PHP_METHOD(NDArray, logb) { } /** - * NDArray::log10 + * NumPower::log10 * * @param execute_data * @param return_value @@ -3323,7 +3348,7 @@ PHP_METHOD(NDArray, logb) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_log10, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, log10) { +PHP_METHOD(NumPower, log10) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -3347,7 +3372,7 @@ PHP_METHOD(NDArray, log10) { } /** - * NDArray::log1p + * NumPower::log1p * * @param execute_data * @param return_value @@ -3355,7 +3380,7 @@ PHP_METHOD(NDArray, log10) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_log1p, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, log1p) { +PHP_METHOD(NumPower, log1p) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -3379,7 +3404,7 @@ PHP_METHOD(NDArray, log1p) { } /** - * NDArray::log1p + * NumPower::log1p * * @param execute_data * @param return_value @@ -3387,7 +3412,7 @@ PHP_METHOD(NDArray, log1p) { ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_log2, 0, 0, 1) ZEND_ARG_INFO(0, array) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, log2) { +PHP_METHOD(NumPower, log2) { NDArray *rtn = NULL; zval *array; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -3411,13 +3436,13 @@ PHP_METHOD(NDArray, log2) { } /** - * NDArray::subtract + * NumPower::subtract */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_subtract, 0) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, subtract) { +PHP_METHOD(NumPower, subtract) { NDArray *rtn = NULL; zval *a, *b; long axis; @@ -3444,13 +3469,13 @@ PHP_METHOD(NDArray, subtract) { } /** - * NDArray::mod + * NumPower::mod */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_mod, 0) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, mod) { +PHP_METHOD(NumPower, mod) { NDArray *rtn = NULL; zval *a, *b; long axis; @@ -3477,13 +3502,13 @@ PHP_METHOD(NDArray, mod) { } /** - * NDArray::pow + * NumPower::pow */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_pow, 0) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, pow) { +PHP_METHOD(NumPower, pow) { NDArray *rtn = NULL; zval *a, *b; long axis; @@ -3511,13 +3536,13 @@ PHP_METHOD(NDArray, pow) { } /** - * NDArray::multiply + * NumPower::multiply */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_multiply, 0) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, multiply) { +PHP_METHOD(NumPower, multiply) { NDArray *rtn = NULL; zval *a, *b; long axis; @@ -3545,13 +3570,13 @@ PHP_METHOD(NDArray, multiply) { } /** - * NDArray::divide + * NumPower::divide */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_divide, 0) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, divide) { +PHP_METHOD(NumPower, divide) { NDArray *rtn = NULL; zval *a, *b; long axis; @@ -3577,13 +3602,13 @@ PHP_METHOD(NDArray, divide) { } /** - * NDArray::add + * NumPower::add */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_add, 0) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, add) { +PHP_METHOD(NumPower, add) { NDArray *rtn = NULL; zval *a, *b; long axis; @@ -3610,13 +3635,13 @@ PHP_METHOD(NDArray, add) { } /** -* NDArray::expandDims +* NumPower::expandDims */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_expand_dims, 0) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, expandDims) { +PHP_METHOD(NumPower, expandDims) { NDArray *rtn = NULL; zval *a; zval *axis; @@ -3645,13 +3670,13 @@ PHP_METHOD(NDArray, expandDims) { } /** -* NDArray::squeeze +* NumPower::squeeze */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_squeeze, 0, 0, 1) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, squeeze) { +PHP_METHOD(NumPower, squeeze) { NDArray *rtn = NULL, *ndaxis = NULL; zval *a; zval *axis; @@ -3682,13 +3707,13 @@ PHP_METHOD(NDArray, squeeze) { } /** -* NDArray::flip +* NumPower::flip */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_flip, 0, 0, 1) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, flip) { +PHP_METHOD(NumPower, flip) { NDArray *rtn = NULL; zval *a; zval *axis = NULL; @@ -3717,14 +3742,14 @@ PHP_METHOD(NDArray, flip) { } /** -* NDArray::swapAxes +* NumPower::swapAxes */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_swapaxes, 0, 0, 3) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, axis1) ZEND_ARG_INFO(0, axis2) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, swapAxes) { +PHP_METHOD(NumPower, swapAxes) { NDArray *rtn = NULL; zval *a; long axis1, axis2; @@ -3749,14 +3774,14 @@ PHP_METHOD(NDArray, swapAxes) { } /** -* NDArray::rollAxis +* NumPower::rollAxis */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_rollaxis, 0, 0, 2) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, axis) ZEND_ARG_INFO(0, start) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, rollAxis) { +PHP_METHOD(NumPower, rollAxis) { NDArray *rtn = NULL; zval *a; long axis, start = 0; @@ -3782,14 +3807,14 @@ PHP_METHOD(NDArray, rollAxis) { } /** -* NDArray::moveAxis +* NumPower::moveAxis */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_moveaxis, 0, 0, 3) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, source) ZEND_ARG_INFO(0, destination) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, moveAxis) { +PHP_METHOD(NumPower, moveAxis) { NDArray *rtn = NULL; zval *a; zval *source, *destination; @@ -3821,12 +3846,12 @@ PHP_METHOD(NDArray, moveAxis) { } /** -* NDArray::verticalStack +* NumPower::verticalStack */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_vstack, 0, 0, 1) ZEND_ARG_INFO(0, arrays) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, verticalStack) { +PHP_METHOD(NumPower, verticalStack) { NDArray *rtn = NULL; zval *arrays; int num_args; @@ -3845,12 +3870,12 @@ PHP_METHOD(NDArray, verticalStack) { } /** -* NDArray::horizontalStack +* NumPower::horizontalStack */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_hstack, 0, 0, 1) ZEND_ARG_INFO(0, arrays) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, horizontalStack) { +PHP_METHOD(NumPower, horizontalStack) { NDArray *rtn = NULL; zval *arrays; int num_args; @@ -3869,12 +3894,12 @@ PHP_METHOD(NDArray, horizontalStack) { } /** -* NDArray::depthStack +* NumPower::depthStack */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_dstack, 0, 0, 1) ZEND_ARG_INFO(0, arrays) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, depthStack) { +PHP_METHOD(NumPower, depthStack) { NDArray *rtn = NULL; zval *arrays; int num_args; @@ -3893,12 +3918,12 @@ PHP_METHOD(NDArray, depthStack) { } /** -* NDArray::columnStack +* NumPower::columnStack */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_column_stack, 0, 0, 1) ZEND_ARG_INFO(0, arrays) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, columnStack) { +PHP_METHOD(NumPower, columnStack) { NDArray *rtn = NULL; zval *arrays; int num_args; @@ -3917,13 +3942,13 @@ PHP_METHOD(NDArray, columnStack) { } /** -* NDArray::concatenate +* NumPower::concatenate */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_concatenate, 0, 0, 1) ZEND_ARG_INFO(0, arrays) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, concatenate) { +PHP_METHOD(NumPower, concatenate) { NDArray *rtn = NULL; zval *arrays; int num_args; @@ -3954,14 +3979,14 @@ PHP_METHOD(NDArray, concatenate) { } /** - * NDArray::append + * NumPower::append */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_append, 0, 0, 2) ZEND_ARG_INFO(0, array) ZEND_ARG_INFO(0, values) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() - PHP_METHOD(NDArray, append) { + PHP_METHOD(NumPower, append) { NDArray *rtn = NULL; int num_args; zval *axis = NULL, *array, *values; @@ -3990,13 +4015,13 @@ ZEND_END_ARG_INFO() } /** - * NDArray::matmul + * NumPower::matmul */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_matmul, 0) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, matmul) { +PHP_METHOD(NumPower, matmul) { NDArray *rtn = NULL; zval *a, *b; long axis; @@ -4023,13 +4048,13 @@ PHP_METHOD(NDArray, matmul) { } /** - * NDArray::inner + * NumPower::inner */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_inner, 0) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, inner) { +PHP_METHOD(NumPower, inner) { NDArray *rtn = NULL; zval *a, *b; long axis; @@ -4054,13 +4079,13 @@ PHP_METHOD(NDArray, inner) { } /** - * NDArray::outer + * NumPower::outer */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_outer, 0) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, outer) { +PHP_METHOD(NumPower, outer) { NDArray *rtn = NULL; zval *a, *b; long axis; @@ -4085,13 +4110,13 @@ PHP_METHOD(NDArray, outer) { } /** - * NDArray::dot + * NumPower::dot */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_dot, 0) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, dot) { +PHP_METHOD(NumPower, dot) { NDArray *rtn = NULL; zval *a, *b; long axis; @@ -4115,12 +4140,12 @@ PHP_METHOD(NDArray, dot) { } /** - * NDArray::trace + * NumPower::trace */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_trace, 0) ZEND_ARG_INFO(0, a) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, trace) { +PHP_METHOD(NumPower, trace) { NDArray *rtn = NULL; zval *a, *b; long axis; @@ -4140,12 +4165,12 @@ PHP_METHOD(NDArray, trace) { } /** - * NDArray::eig + * NumPower::eig */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_eig, 0) ZEND_ARG_INFO(0, a) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, eig) { +PHP_METHOD(NumPower, eig) { NDArray **rtn = NULL; zval *a, *b; long axis; @@ -4167,12 +4192,12 @@ PHP_METHOD(NDArray, eig) { } /** - * NDArray::cholesky + * NumPower::cholesky */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_cholesky, 0) ZEND_ARG_INFO(0, a) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, cholesky) { +PHP_METHOD(NumPower, cholesky) { NDArray *rtn = NULL; zval *a, *b; long axis; @@ -4192,13 +4217,13 @@ PHP_METHOD(NDArray, cholesky) { } /** - * NDArray::solve + * NumPower::solve */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_solve, 2) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, solve) { +PHP_METHOD(NumPower, solve) { NDArray *rtn = NULL; zval *a, *b; long axis; @@ -4224,13 +4249,13 @@ PHP_METHOD(NDArray, solve) { } /** - * NDArray::lstsq + * NumPower::lstsq */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_lstsq, 1) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, lstsq) { +PHP_METHOD(NumPower, lstsq) { NDArray *rtn = NULL; zval *a, *b; long axis; @@ -4255,12 +4280,12 @@ PHP_METHOD(NDArray, lstsq) { } /** - * NDArray::qr + * NumPower::qr */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_qr, 0) ZEND_ARG_INFO(0, a) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, qr) { +PHP_METHOD(NumPower, qr) { NDArray **rtns; zval *a, *b; long axis; @@ -4279,12 +4304,12 @@ PHP_METHOD(NDArray, qr) { } /** - * NDArray::lu + * NumPower::lu */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_lu, 0) ZEND_ARG_INFO(0, a) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, lu) { +PHP_METHOD(NumPower, lu) { NDArray **rtns; zval *a, *b; long axis; @@ -4307,12 +4332,12 @@ PHP_METHOD(NDArray, lu) { } /** - * NDArray::matrixRank + * NumPower::matrixRank */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_matrix_rank, 0) ZEND_ARG_INFO(0, a) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, matrixRank) { +PHP_METHOD(NumPower, matrixRank) { NDArray *rtn; zval *a, *b; long axis; @@ -4340,13 +4365,13 @@ PHP_METHOD(NDArray, matrixRank) { /** - * NDArray::dnnConv2dForward + * NumPower::dnnConv2dForward */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_dnn_conv2d_forward, 4, 0, 1) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, dnnConv2dForward) { +PHP_METHOD(NumPower, dnnConv2dForward) { NDArray *rtn; NDArray *ndb = NULL; zval *input, *filters; @@ -4367,13 +4392,13 @@ PHP_METHOD(NDArray, dnnConv2dForward) { } /** - * NDArray::dnnConv1dForward + * NumPower::dnnConv1dForward */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_dnn_conv1d_forward, 4, 0, 1) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, b) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, dnnConv1dForward) { +PHP_METHOD(NumPower, dnnConv1dForward) { NDArray *rtn; NDArray *ndb = NULL; zval *input, *filters; @@ -4394,14 +4419,14 @@ PHP_METHOD(NDArray, dnnConv1dForward) { } /** - * NDArray::dnnConv2dBackward + * NumPower::dnnConv2dBackward */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_dnn_conv2d_backward, 3, 0, 1) ZEND_ARG_INFO(0, x) ZEND_ARG_INFO(0, y) ZEND_ARG_INFO(0, filters) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, dnnConv2dBackward) { +PHP_METHOD(NumPower, dnnConv2dBackward) { NDArray **rtn; zval *x, *y, *filters; ZEND_PARSE_PARAMETERS_START(3, 3) @@ -4423,7 +4448,7 @@ PHP_METHOD(NDArray, dnnConv2dBackward) { } /** - * NDArray::convolve2d + * NumPower::convolve2d */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_convolve2d, 4, 0, 1) ZEND_ARG_INFO(0, a) @@ -4432,7 +4457,7 @@ ZEND_ARG_INFO(0, mode) ZEND_ARG_INFO(0, boundary) ZEND_ARG_INFO(0, fill_value) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, convolve2d) { +PHP_METHOD(NumPower, convolve2d) { NDArray *rtn; zval *a, *b; char *mode, *boundary; @@ -4484,7 +4509,7 @@ PHP_METHOD(NDArray, convolve2d) { } /** - * NDArray::correlate2d + * NumPower::correlate2d */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_correlate2d, 4, 0, 1) ZEND_ARG_INFO(0, a) @@ -4493,7 +4518,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_correlate2d, 4, 0, 1) ZEND_ARG_INFO(0, boundary) ZEND_ARG_INFO(0, fill_value) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, correlate2d) { +PHP_METHOD(NumPower, correlate2d) { NDArray *rtn; zval *a, *b; char *mode, *boundary; @@ -4545,13 +4570,13 @@ PHP_METHOD(NDArray, correlate2d) { } /** - * NDArray::norm + * NumPower::norm */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_norm, 2, 0, 1) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, order) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, norm) { +PHP_METHOD(NumPower, norm) { NDArray *rtn; zval *a; long order = 2; @@ -4572,12 +4597,12 @@ PHP_METHOD(NDArray, norm) { } /** - * NDArray::cond + * NumPower::cond */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_cond, 0) ZEND_ARG_INFO(0, a) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, cond) { +PHP_METHOD(NumPower, cond) { NDArray *rtn; zval *a; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -4596,12 +4621,12 @@ PHP_METHOD(NDArray, cond) { } /** - * NDArray::inv + * NumPower::inv */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_inv, 0) ZEND_ARG_INFO(0, a) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, inv) { +PHP_METHOD(NumPower, inv) { NDArray *rtn; zval *a, *b; long axis; @@ -4620,12 +4645,12 @@ PHP_METHOD(NDArray, inv) { } /** - * NDArray::svd + * NumPower::svd */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_svd, 0) ZEND_ARG_INFO(0, a) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, svd) { +PHP_METHOD(NumPower, svd) { NDArray **rtns; zval *a, *b; long axis; @@ -4646,12 +4671,12 @@ PHP_METHOD(NDArray, svd) { } /** - * NDArray::det + * NumPower::det */ ZEND_BEGIN_ARG_INFO(arginfo_ndarray_det, 0) ZEND_ARG_INFO(0, a) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, det) { +PHP_METHOD(NumPower, det) { NDArray *rtn, *nda; zval *a; ZEND_PARSE_PARAMETERS_START(1, 1) @@ -4667,13 +4692,13 @@ PHP_METHOD(NDArray, det) { } /** - * NDArray::sum + * NumPower::sum */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_sum, 0, 0, 1) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, sum) { +PHP_METHOD(NumPower, sum) { NDArray *rtn = NULL; zval *a; long axis; @@ -4701,13 +4726,13 @@ PHP_METHOD(NDArray, sum) { } /** - * NDArray::min + * NumPower::min */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_min, 0, 0, 1) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, min) { +PHP_METHOD(NumPower, min) { NDArray *rtn = NULL; zval *a; long axis; @@ -4736,13 +4761,13 @@ PHP_METHOD(NDArray, min) { } /** - * NDArray::max + * NumPower::max */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_max, 0, 0, 1) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, max) { +PHP_METHOD(NumPower, max) { NDArray *rtn = NULL; zval *a; long axis; @@ -4778,7 +4803,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_prod, 0, 0, 1) ZEND_ARG_INFO(0, a) ZEND_ARG_INFO(0, axis) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, prod) { +PHP_METHOD(NumPower, prod) { NDArray *rtn = NULL; zval *a; long axis; @@ -4809,7 +4834,7 @@ PHP_METHOD(NDArray, prod) { ZEND_BEGIN_ARG_INFO(arginfo_ndarray_array, 0) ZEND_ARG_INFO(0, a) ZEND_END_ARG_INFO() -PHP_METHOD(NDArray, array) { +PHP_METHOD(NumPower, array) { NDArray *rtn = NULL; zval *a; long axis; @@ -5087,166 +5112,165 @@ static const zend_function_entry class_arithmetic_methods[] = { PHP_FE_END }; -static const zend_function_entry class_NDArray_methods[] = { - ZEND_ME(NDArray, __construct, arginfo_construct, ZEND_ACC_PUBLIC) - ZEND_ME(NDArray, dump, arginfo_dump, ZEND_ACC_PUBLIC) - ZEND_ME(NDArray, dumpDevices, arginfo_dump_devices, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, gpu, arginfo_gpu, ZEND_ACC_PUBLIC) - ZEND_ME(NDArray, cpu, arginfo_cpu, ZEND_ACC_PUBLIC) - ZEND_ME(NDArray, isGPU, arginfo_is_gpu, ZEND_ACC_PUBLIC) - ZEND_ME(NDArray, setDevice, arginfo_setdevice, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, save, arginfo_save, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, load, arginfo_load, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) +static const zend_function_entry class_NumPower_methods[] = { + ZEND_ME(NumPower, __construct, arginfo_NumPower_construct, ZEND_ACC_PUBLIC) // EXTREMA - ZEND_ME(NDArray, min, arginfo_ndarray_min, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, max, arginfo_ndarray_max, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, maximum, arginfo_ndarray_maximum, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, minimum, arginfo_ndarray_minimum, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, argmax, arginfo_ndarray_argmax, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, argmin, arginfo_ndarray_argmin, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, min, arginfo_ndarray_min, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, max, arginfo_ndarray_max, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, maximum, arginfo_ndarray_maximum, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, minimum, arginfo_ndarray_minimum, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, argmax, arginfo_ndarray_argmax, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, argmin, arginfo_ndarray_argmin, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // MANIPULATION - ZEND_ME(NDArray, reshape, arginfo_reshape, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, toArray, arginfo_toArray, ZEND_ACC_PUBLIC) - ZEND_ME(NDArray, toImage, arginfo_toImage, ZEND_ACC_PUBLIC) - ZEND_ME(NDArray, copy, arginfo_ndarray_copy, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, shape, arginfo_ndarray_shape, ZEND_ACC_PUBLIC) - ZEND_ME(NDArray, flatten, arginfo_ndarray_flat, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, atleast1d, arginfo_ndarray_atleast_1d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, atleast2d, arginfo_ndarray_atleast_2d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, atleast3d, arginfo_ndarray_atleast_3d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, transpose, arginfo_ndarray_transpose, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, slice, arginfo_slice, ZEND_ACC_PUBLIC) - ZEND_ME(NDArray, append, arginfo_ndarray_append, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, expandDims, arginfo_ndarray_expand_dims, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, squeeze, arginfo_ndarray_squeeze, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, flip, arginfo_ndarray_flip, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, swapAxes, arginfo_ndarray_swapaxes, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, rollAxis, arginfo_ndarray_rollaxis, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, moveAxis, arginfo_ndarray_moveaxis, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, verticalStack, arginfo_ndarray_vstack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, horizontalStack, arginfo_ndarray_hstack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, depthStack, arginfo_ndarray_dstack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, columnStack, arginfo_ndarray_column_stack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, concatenate, arginfo_ndarray_concatenate, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, copy, arginfo_ndarray_copy, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, flatten, arginfo_ndarray_flat, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, atleast1d, arginfo_ndarray_atleast_1d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, atleast2d, arginfo_ndarray_atleast_2d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, atleast3d, arginfo_ndarray_atleast_3d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, transpose, arginfo_ndarray_transpose, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, append, arginfo_ndarray_append, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, expandDims, arginfo_ndarray_expand_dims, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, squeeze, arginfo_ndarray_squeeze, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, flip, arginfo_ndarray_flip, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, swapAxes, arginfo_ndarray_swapaxes, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, rollAxis, arginfo_ndarray_rollaxis, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, moveAxis, arginfo_ndarray_moveaxis, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, verticalStack, arginfo_ndarray_vstack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, horizontalStack, arginfo_ndarray_hstack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, depthStack, arginfo_ndarray_dstack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, columnStack, arginfo_ndarray_column_stack, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, concatenate, arginfo_ndarray_concatenate, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // INDEXING - ZEND_ME(NDArray, diagonal, arginfo_ndarray_diagonal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, diagonal, arginfo_ndarray_diagonal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // INITIALIZERS - ZEND_ME(NDArray, zeros, arginfo_ndarray_zeros, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, ones, arginfo_ndarray_ones, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, arange, arginfo_ndarray_arange, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, identity, arginfo_ndarray_identity, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, diag, arginfo_ndarray_diag, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, full, arginfo_ndarray_full, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, fill, arginfo_fill, ZEND_ACC_PUBLIC) - ZEND_ME(NDArray, array, arginfo_ndarray_array, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, fromImage, arginfo_ndarray_fromimage, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, zeros, arginfo_ndarray_zeros, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, ones, arginfo_ndarray_ones, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, arange, arginfo_ndarray_arange, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, identity, arginfo_ndarray_identity, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, diag, arginfo_ndarray_diag, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, full, arginfo_ndarray_full, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, array, arginfo_ndarray_array, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, fromImage, arginfo_ndarray_fromimage, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // RANDOM - ZEND_ME(NDArray, normal, arginfo_ndarray_normal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, truncatedNormal, arginfo_ndarray_truncated_normal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, standardNormal, arginfo_ndarray_standard_normal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, poisson, arginfo_ndarray_poisson, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, uniform, arginfo_ndarray_uniform, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, randomBinomial, arginfo_ndarray_binomial, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, normal, arginfo_ndarray_normal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, truncatedNormal, arginfo_ndarray_truncated_normal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, standardNormal, arginfo_ndarray_standard_normal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, poisson, arginfo_ndarray_poisson, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, uniform, arginfo_ndarray_uniform, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, randomBinomial, arginfo_ndarray_binomial, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // LINALG - ZEND_ME(NDArray, matmul, arginfo_ndarray_matmul, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, svd, arginfo_ndarray_svd, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, det, arginfo_ndarray_det, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, dot, arginfo_ndarray_dot, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, inner, arginfo_ndarray_inner, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, outer, arginfo_ndarray_outer, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, cholesky, arginfo_ndarray_cholesky, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, qr, arginfo_ndarray_qr, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, eig, arginfo_ndarray_eig, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, cond, arginfo_ndarray_cond, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, norm, arginfo_ndarray_norm, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, trace, arginfo_ndarray_trace, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, solve, arginfo_ndarray_solve, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, inv, arginfo_ndarray_inv, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, lstsq, arginfo_ndarray_lstsq, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, lu, arginfo_ndarray_lu, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, matrixRank, arginfo_ndarray_matrix_rank, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, convolve2d, arginfo_ndarray_convolve2d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, correlate2d, arginfo_ndarray_correlate2d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, matmul, arginfo_ndarray_matmul, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, svd, arginfo_ndarray_svd, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, det, arginfo_ndarray_det, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, dot, arginfo_ndarray_dot, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, inner, arginfo_ndarray_inner, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, outer, arginfo_ndarray_outer, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, cholesky, arginfo_ndarray_cholesky, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, qr, arginfo_ndarray_qr, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, eig, arginfo_ndarray_eig, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, cond, arginfo_ndarray_cond, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, norm, arginfo_ndarray_norm, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, trace, arginfo_ndarray_trace, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, solve, arginfo_ndarray_solve, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, inv, arginfo_ndarray_inv, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, lstsq, arginfo_ndarray_lstsq, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, lu, arginfo_ndarray_lu, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, matrixRank, arginfo_ndarray_matrix_rank, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, convolve2d, arginfo_ndarray_convolve2d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, correlate2d, arginfo_ndarray_correlate2d, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // DNN - ZEND_ME(NDArray, dnnConv2dForward, arginfo_ndarray_dnn_conv2d_forward, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, dnnConv2dBackward, arginfo_ndarray_dnn_conv2d_backward, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, dnnConv1dForward, arginfo_ndarray_dnn_conv1d_forward, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, dnnConv2dForward, arginfo_ndarray_dnn_conv2d_forward, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, dnnConv2dBackward, arginfo_ndarray_dnn_conv2d_backward, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, dnnConv1dForward, arginfo_ndarray_dnn_conv1d_forward, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // LOGIC - ZEND_ME(NDArray, all, arginfo_ndarray_all, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, allClose, arginfo_ndarray_allclose, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, equal, arginfo_ndarray_equal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, greater, arginfo_ndarray_greater, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, greaterEqual, arginfo_ndarray_greaterequal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, less, arginfo_ndarray_less, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, lessEqual, arginfo_ndarray_lessequal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, notEqual, arginfo_ndarray_notequal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, all, arginfo_ndarray_all, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, allClose, arginfo_ndarray_allclose, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, equal, arginfo_ndarray_equal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, greater, arginfo_ndarray_greater, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, greaterEqual, arginfo_ndarray_greaterequal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, less, arginfo_ndarray_less, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, lessEqual, arginfo_ndarray_lessequal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, notEqual, arginfo_ndarray_notequal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // MATH - ZEND_ME(NDArray, abs, arginfo_ndarray_abs, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, square, arginfo_ndarray_square, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, sqrt, arginfo_ndarray_sqrt, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, exp, arginfo_ndarray_exp, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, expm1, arginfo_ndarray_expm1, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, exp2, arginfo_ndarray_exp2, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, log, arginfo_ndarray_log, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, log2, arginfo_ndarray_log2, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, logb, arginfo_ndarray_logb, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, log10, arginfo_ndarray_log10, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, log1p, arginfo_ndarray_log1p, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, sin, arginfo_ndarray_sin, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, cos, arginfo_ndarray_cos, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, tan, arginfo_ndarray_tan, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, arcsin, arginfo_ndarray_arcsin, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, arccos, arginfo_ndarray_arccos, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, arctan, arginfo_ndarray_arctan, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, arctan2, arginfo_ndarray_arctan2, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, degrees, arginfo_ndarray_degrees, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, radians, arginfo_ndarray_radians, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, sinh, arginfo_ndarray_sinh, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, cosh, arginfo_ndarray_cosh, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, tanh, arginfo_ndarray_tanh, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, arcsinh, arginfo_ndarray_arcsinh, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, arccosh, arginfo_ndarray_arccosh, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, arctanh, arginfo_ndarray_arctanh, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, rint, arginfo_ndarray_rint, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, fix, arginfo_ndarray_fix, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, floor, arginfo_ndarray_floor, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, ceil, arginfo_ndarray_ceil, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, trunc, arginfo_ndarray_trunc, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, sinc, arginfo_ndarray_sinc, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, negative, arginfo_ndarray_negative, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, positive, arginfo_ndarray_positive, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, sign, arginfo_ndarray_sign, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, clip, arginfo_ndarray_clip, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, round, arginfo_ndarray_round, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, rsqrt, arginfo_ndarray_rsqrt, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, reciprocal, arginfo_ndarray_reciprocal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, abs, arginfo_ndarray_abs, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, square, arginfo_ndarray_square, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, sqrt, arginfo_ndarray_sqrt, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, exp, arginfo_ndarray_exp, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, expm1, arginfo_ndarray_expm1, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, exp2, arginfo_ndarray_exp2, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, log, arginfo_ndarray_log, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, log2, arginfo_ndarray_log2, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, logb, arginfo_ndarray_logb, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, log10, arginfo_ndarray_log10, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, log1p, arginfo_ndarray_log1p, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, sin, arginfo_ndarray_sin, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, cos, arginfo_ndarray_cos, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, tan, arginfo_ndarray_tan, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, arcsin, arginfo_ndarray_arcsin, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, arccos, arginfo_ndarray_arccos, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, arctan, arginfo_ndarray_arctan, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, arctan2, arginfo_ndarray_arctan2, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, degrees, arginfo_ndarray_degrees, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, radians, arginfo_ndarray_radians, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, sinh, arginfo_ndarray_sinh, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, cosh, arginfo_ndarray_cosh, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, tanh, arginfo_ndarray_tanh, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, arcsinh, arginfo_ndarray_arcsinh, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, arccosh, arginfo_ndarray_arccosh, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, arctanh, arginfo_ndarray_arctanh, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, rint, arginfo_ndarray_rint, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, fix, arginfo_ndarray_fix, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, floor, arginfo_ndarray_floor, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, ceil, arginfo_ndarray_ceil, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, trunc, arginfo_ndarray_trunc, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, sinc, arginfo_ndarray_sinc, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, negative, arginfo_ndarray_negative, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, positive, arginfo_ndarray_positive, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, sign, arginfo_ndarray_sign, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, clip, arginfo_ndarray_clip, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, round, arginfo_ndarray_round, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, rsqrt, arginfo_ndarray_rsqrt, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, reciprocal, arginfo_ndarray_reciprocal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // STATISTICS - ZEND_ME(NDArray, mean, arginfo_ndarray_mean, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, median, arginfo_ndarray_median, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, variance, arginfo_ndarray_variance, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, average, arginfo_ndarray_average, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, std, arginfo_ndarray_std, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, quantile, arginfo_ndarray_quantile, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, mean, arginfo_ndarray_mean, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, median, arginfo_ndarray_median, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, variance, arginfo_ndarray_variance, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, average, arginfo_ndarray_average, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, std, arginfo_ndarray_std, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, quantile, arginfo_ndarray_quantile, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // ARITHMETICS - ZEND_ME(NDArray, add, arginfo_ndarray_add, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, subtract, arginfo_ndarray_subtract, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, pow, arginfo_ndarray_pow, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, divide, arginfo_ndarray_divide, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, multiply, arginfo_ndarray_multiply, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, sum, arginfo_ndarray_sum, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, prod, arginfo_ndarray_prod, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - ZEND_ME(NDArray, mod, arginfo_ndarray_mod, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, add, arginfo_ndarray_add, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, subtract, arginfo_ndarray_subtract, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, pow, arginfo_ndarray_pow, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, divide, arginfo_ndarray_divide, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, multiply, arginfo_ndarray_multiply, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, sum, arginfo_ndarray_sum, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, prod, arginfo_ndarray_prod, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, mod, arginfo_ndarray_mod, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, dumpDevices, arginfo_dump_devices, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, setDevice, arginfo_setdevice, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, load, arginfo_load, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, save, arginfo_save, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_FE_END +}; + +static const zend_function_entry class_NDArray_methods[] = { + ZEND_ME(NDArray, __construct, arginfo_construct, ZEND_ACC_PUBLIC) + ZEND_ME(NDArray, dump, arginfo_dump, ZEND_ACC_PUBLIC) + ZEND_ME(NDArray, gpu, arginfo_gpu, ZEND_ACC_PUBLIC) + ZEND_ME(NDArray, cpu, arginfo_cpu, ZEND_ACC_PUBLIC) + ZEND_ME(NDArray, isGPU, arginfo_is_gpu, ZEND_ACC_PUBLIC) ZEND_ME(NDArray, size, arginfo_size, ZEND_ACC_PUBLIC) ZEND_ME(NDArray, count, arginfo_count, ZEND_ACC_PUBLIC) @@ -5262,12 +5286,36 @@ static const zend_function_entry class_NDArray_methods[] = { ZEND_ME(NDArray, offsetUnset, arginfo_offsetunset, ZEND_ACC_PUBLIC) ZEND_ME(NDArray, __serialize, arginfo_serialize, ZEND_ACC_PUBLIC) ZEND_ME(NDArray, __unserialize, arginfo_unserialize, ZEND_ACC_PUBLIC) + + ZEND_ME(NDArray, reshape, arginfo_reshape, ZEND_ACC_PUBLIC) + ZEND_ME(NDArray, toArray, arginfo_toArray, ZEND_ACC_PUBLIC) + ZEND_ME(NDArray, toImage, arginfo_toImage, ZEND_ACC_PUBLIC) + ZEND_ME(NDArray, slice, arginfo_slice, ZEND_ACC_PUBLIC) + ZEND_ME(NDArray, shape, arginfo_ndarray_shape, ZEND_ACC_PUBLIC) + ZEND_ME(NDArray, fill, arginfo_fill, ZEND_ACC_PUBLIC) ZEND_FE_END }; static zend_class_entry *register_class_NDArray(zend_class_entry *class_entry_Iterator, zend_class_entry *class_entry_Countable, zend_class_entry *class_entry_ArrayAccess) { zend_class_entry ce, *class_entry; - INIT_CLASS_ENTRY(ce, "NumPower", class_NDArray_methods); + INIT_CLASS_ENTRY(ce, "NDArray", class_NDArray_methods); + ndarray_objects_init(&ce); + ce.create_object = ndarray_create_object; + class_entry = zend_register_internal_class(&ce); + zend_class_implements(class_entry, 3, class_entry_Iterator, class_entry_Countable, class_entry_ArrayAccess); + + zval property_id_default_value; + ZVAL_UNDEF(&property_id_default_value); + zend_string *property_id_name = zend_string_init("id", sizeof("id") - 1, 1); + zend_declare_typed_property(class_entry, property_id_name, &property_id_default_value, ZEND_ACC_PRIVATE, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); + zend_string_release(property_id_name); + + return class_entry; +} + +static zend_class_entry *register_class_NumPower(zend_class_entry *class_entry_Iterator, zend_class_entry *class_entry_Countable, zend_class_entry *class_entry_ArrayAccess) { + zend_class_entry ce, *class_entry; + INIT_CLASS_ENTRY(ce, "NumPower", class_NumPower_methods); ndarray_objects_init(&ce); ce.create_object = ndarray_create_object; class_entry = zend_register_internal_class(&ce); @@ -5297,6 +5345,7 @@ static zend_class_entry *register_class_ArithmeticOperand(zend_class_entry *clas PHP_MINIT_FUNCTION(ndarray) { phpsci_ce_NDArray = register_class_NDArray(zend_ce_iterator, zend_ce_countable, zend_ce_arrayaccess); phpsci_ce_ArithmeticOperand = register_class_ArithmeticOperand(zend_ce_iterator, zend_ce_countable, zend_ce_arrayaccess); + phpsci_ce_NumPower = register_class_NumPower(zend_ce_iterator, zend_ce_countable, zend_ce_arrayaccess); REGISTER_LONG_CONSTANT("NUMPOWER_CPU", 0, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("NUMPOWER_CUDA", 1, CONST_CS | CONST_PERSISTENT); return SUCCESS; diff --git a/php_numpower.h b/php_numpower.h index 4898251..1e0b393 100644 --- a/php_numpower.h +++ b/php_numpower.h @@ -62,6 +62,7 @@ ZEND_FUNCTION(print_r_); PHPAPI zend_class_entry *phpsci_ce_NDArray; +PHPAPI zend_class_entry *phpsci_ce_NumPower; PHPAPI zend_class_entry *phpsci_ce_ArithmeticOperand; # define PHP_NDARRAY_VERSION "0.7.0" diff --git a/src/debug.c b/src/debug.c index 9a3b4e5..6ca4ddc 100644 --- a/src/debug.c +++ b/src/debug.c @@ -240,15 +240,14 @@ NDArray_DumpDevices() { printf(" Compute capability: %d.%d\n", deviceProp.major, deviceProp.minor); printf(" Total global memory: %zu bytes\n", deviceProp.totalGlobalMem); printf(" Max threads per block: %d\n", deviceProp.maxThreadsPerBlock); + printf(" Warp size: %d\n", deviceProp.warpSize); + printf(" Multi processor count: %d\n", deviceProp.multiProcessorCount); printf(" Max threads in X-dimension of block: %d\n", deviceProp.maxThreadsDim[0]); printf(" Max threads in Y-dimension of block: %d\n", deviceProp.maxThreadsDim[1]); printf(" Max threads in Z-dimension of block: %d\n", deviceProp.maxThreadsDim[2]); printf(" Max grid size in X-dimension: %d\n", deviceProp.maxGridSize[0]); printf(" Max grid size in Y-dimension: %d\n", deviceProp.maxGridSize[1]); printf(" Max grid size in Z-dimension: %d\n", deviceProp.maxGridSize[2]); - printf(" Max grid size in Z-dimension: %d\n", deviceProp.maxGridSize[2]); - printf(" Max grid size in Z-dimension: %d\n", deviceProp.maxGridSize[2]); - printf(" Max grid size in Z-dimension: %d\n", deviceProp.maxGridSize[2]); printf("---------------------------------------------------------------------------\n"); } printf("\n==============================================================================\n"); From f9c2d2e21556a422cedb3a103383e3f768f86c4d Mon Sep 17 00:00:00 2001 From: Aleksei Nechaev Date: Sun, 30 Mar 2025 08:36:14 +0700 Subject: [PATCH 6/7] moveAsix dimensions fixed, zts added --- CMakeLists.txt | 3 + Makefile.frag | 7 +- config.m4 | 1 + numpower.c | 397 ++++++++++++++++++++++++--------------------- src/buffer.c | 140 ++++++++++------ src/buffer.h | 3 + src/manipulation.c | 115 ++++++++++--- src/manipulation.h | 4 +- src/sanitizers.c | 96 +++++++++++ src/sanitizers.h | 9 + 10 files changed, 514 insertions(+), 261 deletions(-) create mode 100644 src/sanitizers.c create mode 100644 src/sanitizers.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 95d0fc8..f317a2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ include_directories(/usr/local/include/php) include_directories(/usr/local/include) include_directories(.) include_directories(src) +include_directories(src/zvals) include_directories(src/ndmath) include_directories(src/ndmath/cuda) @@ -25,6 +26,8 @@ add_executable(numpower src/ndmath/linalg.h src/ndmath/statistics.c src/ndmath/statistics.h + src/sanitizers.c + src/sanitizers.h src/buffer.c src/buffer.h src/debug.c diff --git a/Makefile.frag b/Makefile.frag index 3b15a48..519834e 100755 --- a/Makefile.frag +++ b/Makefile.frag @@ -65,7 +65,7 @@ COMMON_FLAGS = $(DEFS) $(INCLUDES) $(EXTRA_INCLUDES) $(CPPFLAGS) $(PHP_FRAMEWORK install-cuda: rm ./.libs -rf mkdir ./.libs - $(NVCC) -I. -I $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -c $(builddir)./numpower.c -shared -Xcompiler -fPIC -o .libs/numpower.o + $(NVCC) -I. -I $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -c $(builddir)./numpower.c -shared -Xcompiler -fPIC -o .libs/numpower.o $(NVCC) -I. -I $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -c $(builddir)./src/buffer.c -shared -Xcompiler -fPIC -o .libs/buffer.o $(NVCC) -I. -I $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -c $(builddir)./src/debug.c -shared -Xcompiler -fPIC -o .libs/debug.o $(NVCC) -I. -I $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -c $(builddir)./src/indexing.c -shared -Xcompiler -fPIC -o .libs/indexing.o @@ -73,6 +73,7 @@ install-cuda: $(NVCC) -I. -I $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -c $(builddir)./src/iterators.c -shared -Xcompiler -fPIC -o .libs/iterators.o $(CC) -I. -I $(CXX) $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -c $(builddir)./src/logic.c -shared -fPIC -o .libs/logic.o $(CC) -I. -I $(CXX) $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -c $(builddir)./src/manipulation.c -shared -fPIC -o .libs/manipulation.o + $(CC) -I. -I $(CXX) $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -c $(builddir)./src/sanitizers.c -shared -fPIC -o .libs/sanitizers.o $(CC) -I. -I $(CXX) $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -c $(builddir)./src/ndarray.c -shared -fPIC -o .libs/ndarray.o $(NVCC) -I. -I $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -c $(builddir)./src/types.c -shared -Xcompiler -fPIC -o .libs/types.o $(CC) -I. -I $(CXX) $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -c $(builddir)./src/ndmath/arithmetics.c -shared -fPIC -o .libs/arithmetics.o @@ -85,7 +86,9 @@ install-cuda: $(NVCC) -I. -I $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -c $(builddir)./src/ndmath/cuda/cuda_math.cu -shared -Xcompiler -fPIC -o .libs/cuda_math.o $(NVCC) -I. -I $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -c $(builddir)./src/ndmath/cuda/cuda_dnn.cu -shared -Xcompiler -fPIC -o .libs/cuda_dnn.o $(NVCC) -I. -I $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -c $(builddir)./src/ndmath/statistics.c -shared -Xcompiler -fPIC -o .libs/statistics.o - $(NVCC) -shared .libs/numpower.o .libs/signal.o .libs/initializers.o .libs/double_math.o .libs/ndarray.o .libs/debug.o .libs/statistics.o .libs/calculation.o .libs/buffer.o .libs/dnn.o .libs/cuda_dnn.o .libs/logic.o .libs/gpu_alloc.o .libs/linalg.o .libs/manipulation.o .libs/iterators.o .libs/indexing.o .libs/arithmetics.o .libs/types.o .libs/cuda_math.o $(CFLAGS_CLEAN) -o .libs/ndarray.so + $(NVCC) -shared .libs/numpower.o .libs/signal.o .libs/initializers.o .libs/double_math.o .libs/ndarray.o .libs/debug.o .libs/statistics.o .libs/calculation.o .libs/buffer.o .libs/dnn.o \ + .libs/cuda_dnn.o .libs/logic.o .libs/gpu_alloc.o .libs/linalg.o .libs/manipulation.o .libs/iterators.o .libs/indexing.o .libs/arithmetics.o .libs/types.o .libs/sanitizers.o \ + .libs/cuda_math.o $(CFLAGS_CLEAN) -o .libs/ndarray.so cp ./.libs/ndarray.so $(phplibdir)/ndarray.so cp ./.libs/ndarray.so $(EXTENSION_DIR)/ndarray.so diff --git a/config.m4 b/config.m4 index f4a6ac4..1674381 100755 --- a/config.m4 +++ b/config.m4 @@ -135,6 +135,7 @@ if test "$PHP_NDARRAY" != "no"; then src/ndmath/calculation.c \ src/ndmath/statistics.c \ src/ndmath/signal.c \ + src/sanitizers.c \ src/types.c, $ext_shared) fi diff --git a/numpower.c b/numpower.c index 475aac6..cb72071 100644 --- a/numpower.c +++ b/numpower.c @@ -26,6 +26,7 @@ #include "src/ndmath/signal.h" #include "src/ndmath/calculation.h" #include "src/dnn.h" +#include "src/sanitizers.h" #ifdef HAVE_CUBLAS #include @@ -34,6 +35,10 @@ #include "src/gpu_alloc.h" #endif +#ifdef ZTS +#include "TSRM.h" +#endif + #ifdef HAVE_GD #endif @@ -49,44 +54,54 @@ static zend_object_handlers ndarray_object_handlers; static zend_object_handlers numpower_object_handlers; static zend_object_handlers arithmetic_object_handlers; -int * -zval_axis_argument(zval *arg, char *name, int *outsize) -{ - int *result; - zend_string *key; - zend_ulong idx; - zval *val; - *outsize = 0; - if (Z_TYPE_P(arg) != IS_LONG && Z_TYPE_P(arg) != IS_ARRAY) - { - zend_throw_error(NULL, "`%s` argument must be either integer or an array of integers.", name); - return NULL; - } - if (Z_TYPE_P(arg) == IS_LONG) { - result = emalloc(sizeof(int)); - *result = zval_get_long(arg); - *outsize = 1; - return result; - } - - result = emalloc(sizeof(int) * zend_array_count(Z_ARRVAL_P(arg))); - ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arg), idx, key, val) { - if (Z_TYPE_P(val) != IS_LONG) { - efree(result); - zend_throw_error(NULL, "`%s` argument must be either integer or an array of integers.", name); - return NULL; - } - result[idx] = zval_get_long(val); - (*outsize)++; - } ZEND_HASH_FOREACH_END(); - return result; -} - int get_object_uuid(zval* obj) { return Z_LVAL_P(OBJ_PROP_NUM(Z_OBJ_P(obj), 0)); } +/** + * @brief Initializes a new PHP object representing an NDArray or returns a scalar value. + * + * This function handles conversion of an internal NDArray structure into either: + * - A PHP object of type `phpsci_ce_NDArray` for multi-dimensional arrays + * - A direct PHP scalar value for 0-dimensional arrays (single values) + * + * @param[in] array Pointer to the internal NDArray structure to convert. If NULL, + * throws an exception and returns immediately. + * @param[out] return_value zval that will be initialized either as: + * - An NDArray object (for dim > 0) + * - A double scalar value (for 0-dim arrays) + * + * @note For NDArrays (dim > 0): + * 1. Adds the array to the global buffer for tracking + * 2. Creates a new PHP object of class `phpsci_ce_NDArray` + * 3. Stores the array's UUID as object property #0 + * + * @note For 0-dimensional arrays: + * 1. Extracts the scalar value as double + * 2. Frees the NDArray memory + * 3. Returns the scalar directly + * + * @warning If array is NULL, throws an exception via RETURN_THROWS() + * @warning The NDArray memory is managed differently based on dimensionality: + * - For dim > 0: Memory is tracked via the global buffer + * - For dim 0: Memory is freed immediately + */ +void ndarray_init_new_object(NDArray* array, zval* return_value) { + if (array == NULL) { + RETURN_THROWS(); + return; + } + if (NDArray_NDIM(array) > 0) { + add_to_buffer(array); + object_init_ex(return_value, phpsci_ce_NDArray); + ZVAL_LONG(OBJ_PROP_NUM(Z_OBJ_P(return_value), 0), NDArray_UUID(array)); + } else { + ZVAL_DOUBLE(return_value, NDArray_GetFloatScalar(array)); + NDArray_FREE(array); + } +} + NDArray* ZVAL_TO_NDARRAY(zval* obj) { if (Z_TYPE_P(obj) == IS_ARRAY) { return Create_NDArray_FromZval(obj); @@ -135,21 +150,6 @@ void CHECK_INPUT_AND_FREE(zval *a, NDArray *nda) { #endif } -void RETURN_NDARRAY(NDArray* array, zval* return_value) { - if (array == NULL) { - RETURN_THROWS(); - return; - } - if (NDArray_NDIM(array) > 0) { - add_to_buffer(array); - object_init_ex(return_value, phpsci_ce_NDArray); - ZVAL_LONG(OBJ_PROP_NUM(Z_OBJ_P(return_value), 0), NDArray_UUID(array)); - } else { - ZVAL_DOUBLE(return_value, NDArray_GetFloatScalar(array)); - NDArray_FREE(array); - } -} - NDArray** ARRAY_OF_NDARRAYS(zval *array, int *size) { zval *val; @@ -227,7 +227,7 @@ static int ndarray_do_operation_ex(zend_uchar opcode, zval *result, zval *op1, z } CHECK_INPUT_AND_FREE(op1, nda); CHECK_INPUT_AND_FREE(op2, ndb); - RETURN_NDARRAY(rtn, result); + ndarray_init_new_object(rtn, result); if (rtn != NULL) { return SUCCESS; } @@ -368,8 +368,8 @@ void RETURN_2NDARRAY(NDArray* array1, NDArray* array2, zval* return_value) { add_to_buffer(array1); add_to_buffer(array2); - RETURN_NDARRAY(array1, &a); - RETURN_NDARRAY(array2, &b); + ndarray_init_new_object(array1, &a); + ndarray_init_new_object(array2, &b); array_init(return_value); add_next_index_object(return_value, Z_OBJ(a)); add_next_index_object(return_value, Z_OBJ(b)); @@ -544,7 +544,7 @@ PHP_METHOD(NDArray, gpu) { return; } rtn = NDArray_ToGPU(array); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); #else zend_throw_error(NULL, "No GPU device available or CUDA not enabled"); RETURN_NULL(); @@ -563,7 +563,7 @@ PHP_METHOD(NDArray, cpu) { return; } rtn = NDArray_ToCPU(array); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } ZEND_BEGIN_ARG_INFO(arginfo_is_gpu, 0) @@ -613,7 +613,7 @@ PHP_METHOD(NumPower, load) { Z_PARAM_STR(name) ZEND_PARSE_PARAMETERS_END(); NDArray *rtn = NDArray_Load(name->val); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } ZEND_BEGIN_ARG_INFO(arginfo_save, 0) @@ -690,7 +690,7 @@ PHP_METHOD(NDArray, reshape) { NDArray_FREE(shape); } CHECK_INPUT_AND_FREE(a, target); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } PHP_FUNCTION(print_r_) { @@ -752,7 +752,7 @@ PHP_METHOD(NumPower, zeros) { } rtn = NDArray_Zeros(shape, NDArray_NUMELEMENTS(nda), NDARRAY_TYPE_FLOAT32, NDARRAY_DEVICE_CPU); NDArray_FREE(nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -784,7 +784,7 @@ PHP_METHOD(NumPower, equal) { CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -816,7 +816,7 @@ PHP_METHOD(NumPower, greater) { CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -848,7 +848,7 @@ PHP_METHOD(NumPower, greaterEqual) { CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -880,7 +880,7 @@ PHP_METHOD(NumPower, less) { CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -912,7 +912,7 @@ PHP_METHOD(NumPower, lessEqual) { CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -944,7 +944,7 @@ PHP_METHOD(NumPower, notEqual) { CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -964,7 +964,7 @@ PHP_METHOD(NumPower, identity) { Z_PARAM_LONG(size) ZEND_PARSE_PARAMETERS_END(); rtn = NDArray_Identity((int)size); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1008,7 +1008,7 @@ PHP_METHOD(NumPower, normal) { rtn = NDArray_Normal(loc, scale, shape, NDArray_NUMELEMENTS(nda), accelerator_i); NDArray_FREE(nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1054,7 +1054,7 @@ PHP_METHOD(NumPower, truncatedNormal) { rtn = NDArray_TruncatedNormal(loc, scale, shape, NDArray_NUMELEMENTS(nda), accelerator_i); NDArray_FREE(nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1086,7 +1086,7 @@ PHP_METHOD(NumPower, randomBinomial) { } rtn = NDArray_Binomial(ishape, NDArray_NUMELEMENTS(nda), (int)n, (float)p); NDArray_FREE(nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1134,7 +1134,7 @@ PHP_METHOD(NumPower, standardNormal) { rtn = NDArray_StandardNormal(NDArray_ToIntVector(nda), NDArray_NUMELEMENTS(nda)); NDArray_FREE(nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1186,7 +1186,7 @@ PHP_METHOD(NumPower, poisson) { rtn = NDArray_Poisson(lam, NDArray_ToIntVector(nda), NDArray_NUMELEMENTS(nda)); NDArray_FREE(nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1221,7 +1221,7 @@ PHP_METHOD(NumPower, uniform) { } rtn = NDArray_Uniform(low, high, shape, NDArray_NUMELEMENTS(nda)); NDArray_FREE(nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1245,7 +1245,7 @@ PHP_METHOD(NumPower, diag) { if (Z_TYPE_P(target) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1269,7 +1269,7 @@ PHP_METHOD(NumPower, diagonal) { if (Z_TYPE_P(target) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1321,7 +1321,7 @@ PHP_METHOD(NumPower, full) { efree(new_shape); NDArray_FREE(nd_shape); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1348,7 +1348,7 @@ PHP_METHOD(NumPower, ones) { shape = NDArray_ToIntVector(nda); rtn = NDArray_Ones(shape, NDArray_NUMELEMENTS(nda), NDARRAY_TYPE_FLOAT32); NDArray_FREE(nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1379,7 +1379,7 @@ PHP_METHOD(NumPower, arange) { step = 1.0f; } rtn = NDArray_Arange(start, stop, step); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1420,7 +1420,7 @@ PHP_METHOD(NumPower, all) { zend_throw_error(NULL, "Not implemented"); return; rtn = single_reduce(nda, &axis_i, &NDArray_All); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } } @@ -1517,7 +1517,7 @@ PHP_METHOD(NumPower, transpose) { CHECK_INPUT_AND_FREE(array, nda); if (ZEND_NUM_ARGS() == 2) efree(dims); if (rtn == NULL) return; - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1553,7 +1553,7 @@ PHP_METHOD(NumPower, copy) { return; } CHECK_INPUT_AND_FREE(array, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1578,7 +1578,7 @@ PHP_METHOD(NumPower, atleast1d) { NDArray *output = NDArray_AtLeast1D(nda); CHECK_INPUT_AND_FREE(array, nda); - RETURN_NDARRAY(output, return_value); + ndarray_init_new_object(output, return_value); } /** @@ -1603,7 +1603,7 @@ PHP_METHOD(NumPower, atleast2d) { NDArray *output = NDArray_AtLeast2D(nda); CHECK_INPUT_AND_FREE(array, nda); - RETURN_NDARRAY(output, return_value); + ndarray_init_new_object(output, return_value); } /** @@ -1629,7 +1629,7 @@ PHP_METHOD(NumPower, atleast3d) { NDArray *output = NDArray_AtLeast3D(nda); CHECK_INPUT_AND_FREE(array, nda); - RETURN_NDARRAY(output, return_value); + ndarray_init_new_object(output, return_value); } /** @@ -1674,7 +1674,7 @@ PHP_METHOD(NumPower, flatten) { } rtn = NDArray_Flatten(nda); CHECK_INPUT_AND_FREE(a, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1702,7 +1702,7 @@ PHP_METHOD(NumPower, abs) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1737,7 +1737,7 @@ PHP_METHOD(NumPower, sin) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1772,7 +1772,7 @@ PHP_METHOD(NumPower, cos) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1807,7 +1807,7 @@ PHP_METHOD(NumPower, tan) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1842,7 +1842,7 @@ PHP_METHOD(NumPower, arcsin) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1877,7 +1877,7 @@ PHP_METHOD(NumPower, rsqrt) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1912,7 +1912,7 @@ PHP_METHOD(NumPower, arccos) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1947,7 +1947,7 @@ PHP_METHOD(NumPower, arctan) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -1984,7 +1984,7 @@ PHP_METHOD(NumPower, arctan2) { } CHECK_INPUT_AND_FREE(x, ndx); CHECK_INPUT_AND_FREE(y, ndy); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2019,7 +2019,7 @@ PHP_METHOD(NumPower, degrees) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2054,7 +2054,7 @@ PHP_METHOD(NumPower, sinh) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2089,7 +2089,7 @@ PHP_METHOD(NumPower, cosh) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2124,7 +2124,7 @@ PHP_METHOD(NumPower, tanh) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2159,7 +2159,7 @@ PHP_METHOD(NumPower, arcsinh) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2194,7 +2194,7 @@ PHP_METHOD(NumPower, arccosh) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2217,7 +2217,7 @@ PHP_METHOD(NumPower, fromImage) { Z_PARAM_BOOL(channelLast) ZEND_PARSE_PARAMETERS_END(); rtn = NDArray_FromGD(image, channelLast); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2252,7 +2252,7 @@ PHP_METHOD(NumPower, arctanh) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2287,7 +2287,7 @@ PHP_METHOD(NumPower, rint) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2322,7 +2322,7 @@ PHP_METHOD(NumPower, fix) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2357,7 +2357,7 @@ PHP_METHOD(NumPower, trunc) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2392,7 +2392,7 @@ PHP_METHOD(NumPower, sinc) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2427,7 +2427,7 @@ PHP_METHOD(NumPower, negative) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2462,7 +2462,7 @@ PHP_METHOD(NumPower, positive) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2497,7 +2497,7 @@ PHP_METHOD(NumPower, reciprocal) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } @@ -2533,7 +2533,7 @@ PHP_METHOD(NumPower, sign) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2576,7 +2576,7 @@ PHP_METHOD(NumPower, clip) { if (Z_TYPE_P(array) != IS_ARRAY) { CHECK_INPUT_AND_FREE(array, nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2606,7 +2606,7 @@ PHP_METHOD(NumPower, maximum) { CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2636,7 +2636,7 @@ PHP_METHOD(NumPower, minimum) { CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } @@ -2672,7 +2672,7 @@ PHP_METHOD(NumPower, argmax) { rtn = NDArray_ArgMinMaxCommon(nda, (int)axis, keepdims, true); if (rtn == NULL) return; CHECK_INPUT_AND_FREE(a, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2707,7 +2707,7 @@ PHP_METHOD(NumPower, argmin) { rtn = NDArray_ArgMinMaxCommon(nda, (int)axis, keepdims, false); if (rtn == NULL) return; CHECK_INPUT_AND_FREE(a, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2765,7 +2765,7 @@ PHP_METHOD(NumPower, mean) { NDArray_FREE(nda); } CHECK_INPUT_AND_FREE(array, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2808,7 +2808,7 @@ PHP_METHOD(NumPower, median) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2854,7 +2854,7 @@ PHP_METHOD(NumPower, std) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2889,7 +2889,7 @@ PHP_METHOD(NumPower, quantile) { } CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(q, ndq); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2926,7 +2926,7 @@ PHP_METHOD(NumPower, average) { CHECK_INPUT_AND_FREE(weights, ndw); } CHECK_INPUT_AND_FREE(array, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -2972,7 +2972,7 @@ PHP_METHOD(NumPower, variance) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3007,7 +3007,7 @@ PHP_METHOD(NumPower, ceil) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3045,7 +3045,7 @@ PHP_METHOD(NumPower, round) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3080,7 +3080,7 @@ PHP_METHOD(NumPower, floor) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3115,7 +3115,7 @@ PHP_METHOD(NumPower, radians) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3149,7 +3149,7 @@ PHP_METHOD(NumPower, sqrt) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3175,7 +3175,7 @@ PHP_METHOD(NumPower, square) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3209,7 +3209,7 @@ PHP_METHOD(NumPower, exp) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3235,7 +3235,7 @@ PHP_METHOD(NumPower, exp2) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3270,7 +3270,7 @@ PHP_METHOD(NumPower, expm1) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3304,7 +3304,7 @@ PHP_METHOD(NumPower, log) { if (Z_TYPE_P(array) == IS_ARRAY) { NDArray_FREE(nda); } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3336,7 +3336,7 @@ PHP_METHOD(NumPower, logb) { #endif } CHECK_INPUT_AND_FREE(array, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3368,7 +3368,7 @@ PHP_METHOD(NumPower, log10) { #endif } CHECK_INPUT_AND_FREE(array, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3400,7 +3400,7 @@ PHP_METHOD(NumPower, log1p) { #endif } CHECK_INPUT_AND_FREE(array, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3432,7 +3432,7 @@ PHP_METHOD(NumPower, log2) { #endif } CHECK_INPUT_AND_FREE(array, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3465,7 +3465,7 @@ PHP_METHOD(NumPower, subtract) { rtn = NDArray_Subtract_Float(nda, ndb); CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3498,7 +3498,7 @@ PHP_METHOD(NumPower, mod) { rtn = NDArray_Mod_Float(nda, ndb); CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(a, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3532,7 +3532,7 @@ PHP_METHOD(NumPower, pow) { rtn = NDArray_Pow_Float(nda, ndb); CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3566,7 +3566,7 @@ PHP_METHOD(NumPower, multiply) { CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3598,7 +3598,7 @@ PHP_METHOD(NumPower, divide) { rtn = NDArray_Divide_Float(nda, ndb); CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3631,7 +3631,7 @@ PHP_METHOD(NumPower, add) { rtn = NDArray_Add_Float(nda, ndb); CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3666,7 +3666,7 @@ PHP_METHOD(NumPower, expandDims) { if (rtn == NULL) { return; } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3703,7 +3703,7 @@ PHP_METHOD(NumPower, squeeze) { if (rtn == NULL) { return; } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3738,7 +3738,7 @@ PHP_METHOD(NumPower, flip) { if (rtn == NULL) { return; } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3770,7 +3770,7 @@ PHP_METHOD(NumPower, swapAxes) { if (rtn == NULL) { return; } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3803,48 +3803,66 @@ PHP_METHOD(NumPower, rollAxis) { if (rtn == NULL) { return; } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** * NumPower::moveAxis */ ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_moveaxis, 0, 0, 3) -ZEND_ARG_INFO(0, a) -ZEND_ARG_INFO(0, source) -ZEND_ARG_INFO(0, destination) + ZEND_ARG_INFO(0, a) + ZEND_ARG_INFO(0, source) + ZEND_ARG_INFO(0, destination) ZEND_END_ARG_INFO() PHP_METHOD(NumPower, moveAxis) { - NDArray *rtn = NULL; zval *a; zval *source, *destination; + ZEND_PARSE_PARAMETERS_START(3, 3) Z_PARAM_ZVAL(a) Z_PARAM_ZVAL(source) Z_PARAM_ZVAL(destination) ZEND_PARSE_PARAMETERS_END(); - int src_size, dest_size; - int *src = zval_axis_argument(source, "source", &src_size); - int *dest = zval_axis_argument(destination, "destination", &dest_size); - if (src == NULL) return; - if (dest == NULL) return; NDArray *nda = ZVAL_TO_NDARRAY(a); if (nda == NULL) { + zend_throw_error(NULL, "Invalid NDArray provided."); return; } - rtn = NDArray_Moveaxis(nda, src, dest, src_size, dest_size); + int ndim = NDArray_NDIM(nda); + int src_size, dest_size; - CHECK_INPUT_AND_FREE(a, nda); + int *src = zval_parameter_to_normalized_axis_argument(source, "source", ndim, &src_size); + if (src == NULL) { + return; + } + + int *dest = zval_parameter_to_normalized_axis_argument(destination, "destination", ndim, &dest_size); + if (dest == NULL) { + efree(src); + return; + } + + if (src_size != dest_size) { + zend_throw_error(NULL, "`source` and `destination` must have the same number of elements."); + efree(src); + efree(dest); + return; + } + + NDArray *result = ndarray_moveaxis(nda, src, dest, src_size); efree(src); efree(dest); - if (rtn == NULL) { + + if (result == NULL) { return; } - RETURN_NDARRAY(rtn, return_value); + + ndarray_init_new_object(result, return_value); } + /** * NumPower::verticalStack */ @@ -3866,7 +3884,7 @@ PHP_METHOD(NumPower, verticalStack) { NDArray_FREE(ndarrays[i]); } efree(ndarrays); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3890,7 +3908,7 @@ PHP_METHOD(NumPower, horizontalStack) { NDArray_FREE(ndarrays[i]); } efree(ndarrays); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3914,7 +3932,7 @@ PHP_METHOD(NumPower, depthStack) { NDArray_FREE(ndarrays[i]); } efree(ndarrays); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3938,7 +3956,7 @@ PHP_METHOD(NumPower, columnStack) { NDArray_FREE(ndarrays[i]); } efree(ndarrays); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -3975,7 +3993,7 @@ PHP_METHOD(NumPower, concatenate) { NDArray_FREE(ndarrays[i]); } efree(ndarrays); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4011,7 +4029,7 @@ ZEND_END_ARG_INFO() CHECK_INPUT_AND_FREE(array, ndarrays[0]); CHECK_INPUT_AND_FREE(values, ndarrays[1]); efree(ndarrays); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4044,7 +4062,7 @@ PHP_METHOD(NumPower, matmul) { } CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4075,7 +4093,7 @@ PHP_METHOD(NumPower, inner) { CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4106,7 +4124,7 @@ PHP_METHOD(NumPower, outer) { CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4136,7 +4154,7 @@ PHP_METHOD(NumPower, dot) { rtn = NDArray_Dot(nda, ndb); CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4161,7 +4179,7 @@ PHP_METHOD(NumPower, trace) { return; } CHECK_INPUT_AND_FREE(a, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4213,7 +4231,7 @@ PHP_METHOD(NumPower, cholesky) { return; } CHECK_INPUT_AND_FREE(a, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4245,7 +4263,7 @@ PHP_METHOD(NumPower, solve) { CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4276,7 +4294,7 @@ PHP_METHOD(NumPower, lstsq) { CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4360,7 +4378,7 @@ PHP_METHOD(NumPower, matrixRank) { } CHECK_INPUT_AND_FREE(a, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } @@ -4388,7 +4406,7 @@ PHP_METHOD(NumPower, dnnConv2dForward) { } CHECK_INPUT_AND_FREE(input, ndinput); CHECK_INPUT_AND_FREE(filters, ndfilters); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4415,7 +4433,7 @@ PHP_METHOD(NumPower, dnnConv1dForward) { } CHECK_INPUT_AND_FREE(input, ndinput); CHECK_INPUT_AND_FREE(filters, ndfilters); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4505,7 +4523,7 @@ PHP_METHOD(NumPower, convolve2d) { } CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4566,7 +4584,7 @@ PHP_METHOD(NumPower, correlate2d) { } CHECK_INPUT_AND_FREE(a, nda); CHECK_INPUT_AND_FREE(b, ndb); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4593,7 +4611,7 @@ PHP_METHOD(NumPower, norm) { rtn = NDArray_Norm(nda, (int)order); CHECK_INPUT_AND_FREE(a, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4617,7 +4635,7 @@ PHP_METHOD(NumPower, cond) { return; } CHECK_INPUT_AND_FREE(a, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4641,7 +4659,7 @@ PHP_METHOD(NumPower, inv) { rtn = NDArray_Inverse(nda); CHECK_INPUT_AND_FREE(a, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4688,7 +4706,7 @@ PHP_METHOD(NumPower, det) { } rtn = NDArray_Det(nda); CHECK_INPUT_AND_FREE(a, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4722,7 +4740,7 @@ PHP_METHOD(NumPower, sum) { return; } CHECK_INPUT_AND_FREE(a, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4757,7 +4775,7 @@ PHP_METHOD(NumPower, min) { return; } CHECK_INPUT_AND_FREE(a, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } /** @@ -4796,7 +4814,7 @@ PHP_METHOD(NumPower, max) { return; } CHECK_INPUT_AND_FREE(a, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } ZEND_BEGIN_ARG_INFO_EX(arginfo_ndarray_prod, 0, 0, 1) @@ -4828,7 +4846,7 @@ PHP_METHOD(NumPower, prod) { return; } CHECK_INPUT_AND_FREE(a, nda); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } ZEND_BEGIN_ARG_INFO(arginfo_ndarray_array, 0) @@ -4845,7 +4863,7 @@ PHP_METHOD(NumPower, array) { if (nda == NULL) { return; } - RETURN_NDARRAY(nda, return_value); + ndarray_init_new_object(nda, return_value); } ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_slice, 0, 0, IS_MIXED, 0) @@ -4891,7 +4909,7 @@ PHP_METHOD(NDArray, slice) { if (rtn == NULL) { return; } - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); } ZEND_BEGIN_ARG_INFO(arginfo_size, 0) @@ -4928,7 +4946,7 @@ PHP_METHOD(NDArray, current) { NDArray* ndarray = ZVALUUID_TO_NDARRAY(obj_uuid); NDArray* result = NDArrayIteratorPHP_GET(ndarray); add_to_buffer(result); - RETURN_NDARRAY(result, return_value); + ndarray_init_new_object(result, return_value); } PHP_METHOD(NDArray, key) { @@ -4998,7 +5016,7 @@ PHP_METHOD(NDArray, offsetGet) { ndarray->iterator->current_index = (int) Z_LVAL_P(offset); NDArray *rtn = NDArrayIterator_GET(ndarray); NDArrayIterator_REWIND(ndarray); - RETURN_NDARRAY(rtn, return_value); + ndarray_init_new_object(rtn, return_value); return; } zend_throw_error(NULL, "Invalid offset"); @@ -5369,6 +5387,13 @@ PHP_MINFO_FUNCTION(ndarray) { } PHP_MSHUTDOWN_FUNCTION(ndarray) { + buffer_free(); +#ifdef ZTS + if (MAIN_MEM_STACK.lock) { + tsrm_mutex_free(MAIN_MEM_STACK.lock); + MAIN_MEM_STACK.lock = NULL; + } +#endif return SUCCESS; } diff --git a/src/buffer.c b/src/buffer.c index 885d96c..3a5f7d8 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -5,6 +5,10 @@ #include "string.h" #include "ndarray.h" +#ifdef ZTS +#include "TSRM.h" +#endif + /** * MEMORY STACK * @@ -12,30 +16,22 @@ */ struct MemoryStack MAIN_MEM_STACK; -void buffer_dump() { - printf("\nMAIN_MEM_STACK.totalAllocated: %d", MAIN_MEM_STACK.totalAllocated); - printf("\nMAIN_MEM_STACK.totalFreed: %d\n", MAIN_MEM_STACK.totalFreed); -} - -/** - * If CARRAY_GC_DEBUG env is True, CArray Garbage Collector - * will print debug messages when destructing objects. - */ -static int -CArrayBuffer_ISDEBUGON() { - if (getenv("NDARRAY_BUFFER_DEBUG") == NULL) { - return 0; - } - if (!strcmp(getenv("NDARRAY_BUFFER_DEBUG"), "0")) { - return 0; - } - return 1; +#ifdef ZTS +static void ndarray_init_globals(void) { + MAIN_MEM_STACK.lock = tsrm_mutex_alloc(); } +#endif /** * Initialize MemoryStack Buffer */ void buffer_init(int size) { +#ifdef ZTS + if (!MAIN_MEM_STACK.lock) { + ndarray_init_globals(); + } +#endif + MAIN_MEM_STACK.buffer = (NDArray**)emalloc(size * sizeof(NDArray*)); MAIN_MEM_STACK.bufferSize = size; MAIN_MEM_STACK.numElements = 0; @@ -49,73 +45,125 @@ void buffer_init(int size) { * Free the buffer */ void buffer_free() { +#ifdef ZTS + tsrm_mutex_lock(MAIN_MEM_STACK.lock); +#endif + if (MAIN_MEM_STACK.buffer != NULL) { efree(MAIN_MEM_STACK.buffer); MAIN_MEM_STACK.buffer = NULL; + MAIN_MEM_STACK.numElements = 0; + MAIN_MEM_STACK.bufferSize = 0; } + +#ifdef ZTS + tsrm_mutex_unlock(MAIN_MEM_STACK.lock); +#endif +} + +/** + * Debug dump of buffer stats + */ +void buffer_dump() { +#ifdef ZTS + tsrm_mutex_lock(MAIN_MEM_STACK.lock); +#endif + + printf("\nMAIN_MEM_STACK.totalAllocated: %d", MAIN_MEM_STACK.totalAllocated); + printf("\nMAIN_MEM_STACK.totalFreed: %d\n", MAIN_MEM_STACK.totalFreed); + +#ifdef ZTS + tsrm_mutex_unlock(MAIN_MEM_STACK.lock); +#endif } /** - * @param uuid + * Free specific NDArray in buffer */ void buffer_ndarray_free(int uuid) { - if (MAIN_MEM_STACK.buffer != NULL) { - if (MAIN_MEM_STACK.lastFreed == -1) { - MAIN_MEM_STACK.lastFreed = uuid; - } - if (MAIN_MEM_STACK.buffer[uuid] != NULL) { - NDArray_FREE(MAIN_MEM_STACK.buffer[uuid]); - MAIN_MEM_STACK.buffer[uuid] = NULL; - MAIN_MEM_STACK.totalFreed++; - } - if (MAIN_MEM_STACK.numElements - 1 == 0) { - buffer_free(); - } +#ifdef ZTS + tsrm_mutex_lock(MAIN_MEM_STACK.lock); +#endif + + if (MAIN_MEM_STACK.buffer != NULL && + uuid >= 0 && + uuid < MAIN_MEM_STACK.numElements && + MAIN_MEM_STACK.buffer[uuid] != NULL) { + + NDArray_FREE(MAIN_MEM_STACK.buffer[uuid]); + MAIN_MEM_STACK.buffer[uuid] = NULL; + MAIN_MEM_STACK.lastFreed = uuid; + MAIN_MEM_STACK.totalFreed++; } + +#ifdef ZTS + tsrm_mutex_unlock(MAIN_MEM_STACK.lock); +#endif } /** - * @param uuid + * Get NDArray from buffer by UUID */ NDArray* buffer_get(int uuid) { +#ifdef ZTS + tsrm_mutex_lock(MAIN_MEM_STACK.lock); +#endif + + assert(uuid >= 0 && uuid < MAIN_MEM_STACK.numElements); assert(MAIN_MEM_STACK.buffer[uuid] != NULL); - return MAIN_MEM_STACK.buffer[uuid]; + NDArray* result = MAIN_MEM_STACK.buffer[uuid]; + +#ifdef ZTS + tsrm_mutex_unlock(MAIN_MEM_STACK.lock); +#endif + + return result; } /** - * Add CArray to MemoryStack (Buffer) and retrieve MemoryPointer - * - * @param array CArray CArray to add into the stack - * @param size size_t Size of CArray in bytes + * Add NDArray to global buffer */ void add_to_buffer(NDArray* ndarray) { +#ifdef ZTS + tsrm_mutex_lock(MAIN_MEM_STACK.lock); +#endif + if (MAIN_MEM_STACK.buffer == NULL) { buffer_init(1); } + if (MAIN_MEM_STACK.lastFreed > -1) { ndarray->uuid = MAIN_MEM_STACK.lastFreed; MAIN_MEM_STACK.buffer[MAIN_MEM_STACK.lastFreed] = ndarray; MAIN_MEM_STACK.lastFreed = -1; +#ifdef ZTS + tsrm_mutex_unlock(MAIN_MEM_STACK.lock); +#endif return; } - // Increase the buffer size if necessary if (MAIN_MEM_STACK.numElements >= MAIN_MEM_STACK.bufferSize) { - int newSize = (MAIN_MEM_STACK.bufferSize == 0) ? 1 : (MAIN_MEM_STACK.bufferSize * 2); - NDArray** newBuffer = (NDArray**)erealloc(MAIN_MEM_STACK.buffer, newSize * sizeof(NDArray*)); - if (newBuffer == NULL) { - php_printf("Failed to allocate memory for the buffer"); + int newSize = MAIN_MEM_STACK.bufferSize * 2; + NDArray** newBuffer = (NDArray**)erealloc(MAIN_MEM_STACK.buffer, + newSize * sizeof(NDArray*)); + if (!newBuffer) { + php_error_docref(NULL, E_WARNING, + "Failed to allocate memory for the buffer"); +#ifdef ZTS + tsrm_mutex_unlock(MAIN_MEM_STACK.lock); +#endif return; } MAIN_MEM_STACK.buffer = newBuffer; MAIN_MEM_STACK.bufferSize = newSize; } - // Set the NDArray's uuid to its position in the buffer ndarray->uuid = MAIN_MEM_STACK.numElements; - - // Add the NDArray to the buffer MAIN_MEM_STACK.buffer[MAIN_MEM_STACK.numElements] = ndarray; MAIN_MEM_STACK.numElements++; MAIN_MEM_STACK.totalAllocated++; -} \ No newline at end of file + +#ifdef ZTS + tsrm_mutex_unlock(MAIN_MEM_STACK.lock); +#endif +} diff --git a/src/buffer.h b/src/buffer.h index 7c28545..bc74d7a 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -13,6 +13,9 @@ struct MemoryStack { int totalGPUAllocated; int totalAllocated; int totalFreed; +#ifdef ZTS + tsrm_mutex_t lock; +#endif }; extern struct MemoryStack MAIN_MEM_STACK; diff --git a/src/manipulation.c b/src/manipulation.c index 3bf0099..a7981d4 100644 --- a/src/manipulation.c +++ b/src/manipulation.c @@ -844,53 +844,118 @@ NDArray_Rollaxis(NDArray *a, int axis, int start) return result; } +/** + * @brief Validates and normalizes axis indices. + * + * This function checks if each axis index in the provided array is within the valid range + * for the given number of dimensions. Negative indices are adjusted to their positive counterparts. + * + * @author Henrique Borba + * @author Aleksei Nechaev + * + * @param[in,out] axes Array of axis indices to be validated and adjusted. + * @param[in] n_axes Number of axes in the array. + * @param[in] ndim Number of dimensions in the target array. + * @return 0 on success; -1 and throws a PHP error on failure. + * + * @warning The function throws a PHP error if any axis index is out of bounds. + */ +static inline int check_and_adjust_axes(int *axes, int n_axes, int ndim) { + for (int i = 0; i < n_axes; i++) { + if (axes[i] < -ndim || axes[i] >= ndim) { + zend_throw_error(NULL, "Axis %d is out of bounds for array with %d dimensions.", axes[i], ndim); + return -1; + } + if (axes[i] < 0) { + axes[i] += ndim; + } + } + return 0; +} -NDArray* -NDArray_Moveaxis(NDArray *a, int* src, int* dest, int n_source, int n_dest) -{ - NDArray *result; - int order[NDARRAY_MAX_DIMS]; - int n = NDArray_NDIM(a); +/** + * @brief Moves specified axes of an NDArray to new positions. + * + * This function takes a multi-dimensional array and repositions the specified axes + * to new locations, returning a new array with the updated axis order. + * + * @author Henrique Borba + * @author Aleksei Nechaev + * + * @param a Pointer to the source NDArray. + * @param src Array of source axis indices to be moved. + * @param dest Array of destination indices for the moved axes. + * @param n_axes Number of axes to move. + * @return Pointer to a new NDArray with the reordered axes, or NULL on error. + * + * @note The function assumes that the arrays `src` and `dest` have a length equal to `n_axes`. + * @warning If non-unique or invalid axis indices are provided, the function will throw an error and return NULL. + */ +NDArray* ndarray_moveaxis(NDArray *a, int* src, int* dest, int n_axes) { + int ndim = NDArray_NDIM(a); - if (check_and_adjust_axis_msg(src, n) < 0) { + // Validate and adjust axes + if (check_and_adjust_axes(src, n_axes, ndim) < 0) { return NULL; } - if (check_and_adjust_axis_msg(dest, n) < 0) { + if (check_and_adjust_axes(dest, n_axes, ndim) < 0) { return NULL; } - if (n_source != n_dest) { - zend_throw_error(NULL, "`source` and `destination` must have the same number of elements."); - return NULL; + // Ensure axes are unique + for (int i = 0; i < n_axes; i++) { + for (int j = i + 1; j < n_axes; j++) { + if (src[i] == src[j] || dest[i] == dest[j]) { + zend_throw_error(NULL, "Axes in `source` and `destination` must be unique."); + return NULL; + } + } } - int found; - int count_order = 0; - for (int i = 0; i < n; i++) { - found = 0; - for (int j = 0; j < n_source; j++) { - if (src[j] == i) found = 1; + // Create new axis order + int order[NDARRAY_MAX_DIMS]; + int remaining_axes[NDARRAY_MAX_DIMS]; + int r_count = 0; + + // Initialize array of remaining axes + for (int i = 0; i < ndim; i++) { + int found = 0; + for (int j = 0; j < n_axes; j++) { + if (src[j] == i) { + found = 1; + break; + } } if (!found) { - order[count_order] = i; - count_order++; + remaining_axes[r_count++] = i; } } - for (int i = 0; i < n_source; i++) { - if (dest[i] < 0) { - dest[i] = dest[i] + (count_order + 1); + // Insert axes into new positions + int dest_positions[NDARRAY_MAX_DIMS]; + for (int i = 0; i < ndim; i++) { + dest_positions[i] = -1; + } + for (int i = 0; i < n_axes; i++) { + dest_positions[dest[i]] = src[i]; + } + + int r_idx = 0, s_idx = 0; + for (int i = 0; i < ndim; i++) { + if (dest_positions[i] != -1) { + order[i] = dest_positions[i]; + } else { + order[i] = remaining_axes[r_idx++]; } - order[dest[i]] = src[i]; - count_order++; } NDArray_Dims order_dims; - order_dims.len = count_order; + order_dims.len = ndim; order_dims.ptr = order; return NDArray_Transpose(a, &order_dims); } + NDArray* NDArray_Concatenate(NDArray **arrays, int narrays, int axis) { diff --git a/src/manipulation.h b/src/manipulation.h index 5e09686..f4c7292 100644 --- a/src/manipulation.h +++ b/src/manipulation.h @@ -3,7 +3,6 @@ #include "ndarray.h" - NDArray* NDArray_Transpose(NDArray *a, NDArray_Dims *permute); NDArray* NDArray_Reshape(NDArray *target, int *new_shape, int ndim); NDArray* NDArray_Flatten(NDArray *target); @@ -22,11 +21,12 @@ NDArray* NDArray_Flip(NDArray *a, NDArray *axis); NDArray* NDArray_Squeeze(NDArray *a, NDArray *axis); NDArray* NDArray_SwapAxes(NDArray *a, int axis1, int axis2); NDArray* NDArray_Rollaxis(NDArray *a, int axis, int start); -NDArray* NDArray_Moveaxis(NDArray *a, int* src, int* dest, int n_source, int n_dest); +NDArray* ndarray_moveaxis(NDArray *a, int* src, int* dest, int n_axes); NDArray* NDArray_Concatenate(NDArray **arrays, int narrays, int axis); NDArray* NDArray_VSTACK(NDArray **arrays, int narrays); NDArray* NDArray_HSTACK(NDArray **arrays, int narrays); NDArray* NDArray_DSTACK(NDArray **arrays, int narrays); NDArray* NDArray_ColumnStack(NDArray **arrays, int narrays); int NDArray_ConvertMultiAxis(NDArray *axis_in, int ndim, bool *out_axis_flags); + #endif //PHPSCI_NDARRAY_MANIPULATION_H diff --git a/src/sanitizers.c b/src/sanitizers.c new file mode 100644 index 0000000..ce70499 --- /dev/null +++ b/src/sanitizers.c @@ -0,0 +1,96 @@ +#include + +/** + * @brief Validates and converts an axis argument (integer or array) to normalized form. + * + * Processes a zval argument representing array axes, which can be either: + * - A single integer (normalized to 0 <= axis < ndim) + * - An array of integers (each normalized similarly) + * + * @author Henrique Borba + * @author Aleksei Nechaev + * + * @param[in] arg Input zval (must be IS_LONG or IS_ARRAY of IS_LONG) + * @param[in] name Argument name for error messages + * @param[in] ndim Number of dimensions in target array + * @param[out] outsize Output parameter for result array size + * @return Array of normalized axes (allocated via emalloc), NULL on failure + * + * @note Caller must efree() the returned array + * @warning Throws PHP errors on invalid input (wrong type, out-of-bound values) + */ +int* zval_parameter_to_normalized_axis_argument(zval *arg, const char *name, int ndim, int *outsize) { + int *result = NULL; + + // Initialize output size + *outsize = 0; + + /* Case 1: Single integer axis */ + if (Z_TYPE_P(arg) == IS_LONG) { + int axis = Z_LVAL_P(arg); + + // Validate axis bounds [-ndim, ndim) + if (axis < -ndim || axis >= ndim) { + zend_throw_error(NULL, "Axis %d is out of bounds for array with %d dimensions in argument `%s`.", + axis, ndim, name); + return NULL; + } + + // Normalize negative axis + if (axis < 0) { + axis += ndim; + } + + result = emalloc(sizeof(int)); + + result[0] = axis; + *outsize = 1; + + return result; + } + + /* Case 2: Array of axes */ + if (Z_TYPE_P(arg) == IS_ARRAY) { + int count = zend_hash_num_elements(Z_ARRVAL_P(arg)); + result = emalloc(sizeof(int) * count); + + int i = 0; + zval *val; + + // Iterate through array elements + ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(arg), val) { + // Validate element type + if (Z_TYPE_P(val) != IS_LONG) { + efree(result); + zend_throw_error(NULL, "All elements of argument `%s` must be integers.", name); + + return NULL; + } + + int axis = Z_LVAL_P(val); + + // Validate bounds for each axis + if (axis < -ndim || axis >= ndim) { + efree(result); + zend_throw_error(NULL, + "Axis %d is out of bounds for array with %d dimensions in argument `%s`.", + axis, ndim, name); + return NULL; + } + + // Normalize negative indices + if (axis < 0) { + axis += ndim; + } + + result[i++] = axis; + } ZEND_HASH_FOREACH_END(); + + *outsize = count; + return result; + } + + /* Invalid type case */ + zend_throw_error(NULL, "Argument `%s` must be either an integer or an array of integers.", name); + return NULL; +} diff --git a/src/sanitizers.h b/src/sanitizers.h new file mode 100644 index 0000000..d4b87d0 --- /dev/null +++ b/src/sanitizers.h @@ -0,0 +1,9 @@ +#ifndef PHPSCI_NDARRAY_SANITIZERS_H +#define PHPSCI_NDARRAY_SANITIZERS_H + +#include +#include "ndarray.h" + +int* zval_parameter_to_normalized_axis_argument(zval *arg, const char *name, int ndim, int *outsize); + +#endif //PHPSCI_NDARRAY_SANITIZERS_H From 9dcbb40664789d13c620c2ac84bf8279e92ab689 Mon Sep 17 00:00:00 2001 From: Aleksei Nechaev Date: Wed, 16 Apr 2025 12:51:49 +0300 Subject: [PATCH 7/7] increase matmul comp. speed --- numpower.c | 24 ++++++++++++------ src/ndmath/linalg.c | 60 +++++++++++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/numpower.c b/numpower.c index cb72071..3665503 100644 --- a/numpower.c +++ b/numpower.c @@ -113,15 +113,12 @@ NDArray* ZVAL_TO_NDARRAY(zval* obj) { return NDArray_CreateFromDoubleScalar(Z_DVAL_P(obj)); } if (Z_TYPE_P(obj) == IS_OBJECT) { - zend_class_entry* ce = NULL; - ce = Z_OBJCE_P(obj); - zend_string* class_name = Z_OBJ_P(obj)->ce->name; - if (strcmp(ZSTR_VAL(class_name), "NDArray") == 0) { - if (ce == phpsci_ce_NDArray) { - return buffer_get(get_object_uuid(obj)); - } + zend_class_entry *ce = Z_OBJCE_P(obj); + if (instanceof_function(ce, phpsci_ce_NDArray)) { + return buffer_get(get_object_uuid(obj)); } #ifdef HAVE_GD + zend_string* class_name = Z_OBJ_P(obj)->ce->name; /* Check if the zend_object class name is "GdImage" */ if (strcmp(ZSTR_VAL(class_name), "GdImage") == 0) { return NDArray_FromGD(obj, false); @@ -4032,6 +4029,17 @@ ZEND_END_ARG_INFO() ndarray_init_new_object(rtn, return_value); } +/** + * NumPower::syncDevice + */ +ZEND_BEGIN_ARG_INFO(arginfo_ndarray_devicesync, 0) +ZEND_END_ARG_INFO() +PHP_METHOD(NumPower, syncDevice) { +#ifdef HAVE_CUBLAS + cudaDeviceSynchronize(); +#endif +} + /** * NumPower::matmul */ @@ -4042,7 +4050,6 @@ ZEND_END_ARG_INFO() PHP_METHOD(NumPower, matmul) { NDArray *rtn = NULL; zval *a, *b; - long axis; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_ZVAL(a) Z_PARAM_ZVAL(b) @@ -5207,6 +5214,7 @@ static const zend_function_entry class_NumPower_methods[] = { ZEND_ME(NumPower, dnnConv2dForward, arginfo_ndarray_dnn_conv2d_forward, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NumPower, dnnConv2dBackward, arginfo_ndarray_dnn_conv2d_backward, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ME(NumPower, dnnConv1dForward, arginfo_ndarray_dnn_conv1d_forward, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + ZEND_ME(NumPower, syncDevice, arginfo_ndarray_devicesync, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) // LOGIC ZEND_ME(NumPower, all, arginfo_ndarray_all, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) diff --git a/src/ndmath/linalg.c b/src/ndmath/linalg.c index 2e8ca48..4dc6ced 100644 --- a/src/ndmath/linalg.c +++ b/src/ndmath/linalg.c @@ -49,35 +49,47 @@ NDArray_FMatmul(NDArray *a, NDArray *b) { NDArray* result = NDArray_Zeros(output_shape, 2, NDARRAY_TYPE_FLOAT32, NDArray_DEVICE(a)); + int m = NDArray_SHAPE(a)[0]; + int k = NDArray_SHAPE(a)[1]; + int n = NDArray_SHAPE(b)[1]; + + float alpha = 1.0f; + float beta = 0.0f; + if (NDArray_DEVICE(a) == NDARRAY_DEVICE_GPU) { - // Perform GPU matrix multiplication #ifdef HAVE_CUBLAS - cublasHandle_t handle; - cublasCreate(&handle); - - float* deviceResult; - size_t sizeResult = NDArray_NUMELEMENTS(result) * sizeof(float); - - vmalloc((void**)&deviceResult, sizeResult); - int m = NDArray_SHAPE(a)[0]; - int n = NDArray_SHAPE(b)[1]; - int k = NDArray_SHAPE(a)[1]; - float alpha = 1.0f; - float beta = 0.0f; - - cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, n, m, k, &alpha, NDArray_FDATA(b), n, NDArray_FDATA(a), k, &beta, deviceResult, n); - vfree(result->data); - result->data = (void*)deviceResult; - cublasDestroy(handle); + static cublasHandle_t handle = NULL; + static bool handle_initialized = false; + + if (!handle_initialized) { + cublasCreate(&handle); + cublasSetMathMode(handle, CUBLAS_TENSOR_OP_MATH); + handle_initialized = true; + } + + cublasSgemm( + handle, + CUBLAS_OP_N, CUBLAS_OP_N, + n, m, k, + &alpha, + NDArray_FDATA(b), n, + NDArray_FDATA(a), k, + &beta, + (float*)NDArray_FDATA(result), n + ); #endif } else { - // Perform CPU matrix multiplication - cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, - NDArray_SHAPE(a)[0], NDArray_SHAPE(b)[1], NDArray_SHAPE(a)[1], - 1.0f, NDArray_FDATA(a), NDArray_SHAPE(a)[1], - NDArray_FDATA(b), NDArray_SHAPE(b)[1], - 0.0f, NDArray_FDATA(result), NDArray_SHAPE(b)[1]); + cblas_sgemm( + CblasRowMajor, CblasNoTrans, CblasNoTrans, + m, n, k, + alpha, + NDArray_FDATA(a), k, + NDArray_FDATA(b), n, + beta, + NDArray_FDATA(result), n + ); } + return result; }