Skip to content

Commit 6502022

Browse files
Implemented CRubix::clip, CRubix::clipLower and CRubix::clipUpper
1 parent f3f5256 commit 6502022

File tree

5 files changed

+64
-12
lines changed

5 files changed

+64
-12
lines changed

config.m4

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ PHP_ARG_WITH(carray, whether to enable CArray computing library,
44
PHP_ARG_ENABLE(carray-opencl, whether to enable OpenCL support,
55
[ --enable-carray-opencl whether to enable OpenCL support], no, no)
66

7+
78
if test "$PHP_CARRAY" != "no"; then
89
AC_DEFINE([HAVE_CARRAY],1 ,[whether to enable CArray computing library])
910

@@ -130,6 +131,16 @@ PHP_CHECK_LIBRARY(lapacke,LAPACKE_sgetrf,
130131
-llapacke
131132
])
132133

134+
PHP_CHECK_LIBRARY(omp,omp_get_num_threads,
135+
[
136+
AC_DEFINE(HAVE_OMP,1,[ ])
137+
AC_MSG_RESULT([OpenMP found])
138+
PHP_ADD_LIBRARY(omp,,CARRAY_SHARED_LIBADD)
139+
],[
140+
AC_MSG_RESULT([OpenMP not found])
141+
],[
142+
-fopenmp
143+
])
133144

134145
PHP_NEW_EXTENSION(carray,
135146
phpsci.c \
@@ -181,5 +192,3 @@ PHP_NEW_EXTENSION(carray,
181192
PHP_INSTALL_HEADERS([ext/carray], [phpsci.h, kernel/carray.h, kernel/types.h, kernel/buffer.h])
182193
PHP_SUBST(CARRAY_SHARED_LIBADD)
183194
fi
184-
185-

kernel/clip.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ CArray_Clip(CArray * self, CArray * min, CArray * max, MemoryPointer * out_ptr)
227227

228228
out = emalloc(sizeof(CArray));
229229
out = CArray_NewFromDescr(out,
230-
indescr, CArray_NDIM(self),
230+
CArray_DESCR(self), CArray_NDIM(self),
231231
CArray_DIMS(self),
232232
NULL, NULL,
233233
CArray_ISFORTRAN(self),
@@ -274,4 +274,4 @@ CArray_Clip(CArray * self, CArray * min, CArray * max, MemoryPointer * out_ptr)
274274
return out;
275275
fail:
276276
return NULL;
277-
}
277+
}

kernel/interfaces/rubix.c

+47-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <kernel/buffer.h>
1616
#include <kernel/search.h>
1717
#include <kernel/round.h>
18+
#include <kernel/clip.h>
1819
#include <kernel/calculation.h>
1920
#include <kernel/carray.h>
2021
#include <phpsci.h>
@@ -26,6 +27,10 @@
2627
#include "lapacke.h"
2728
#include "cblas.h"
2829

30+
#ifdef HAVE_OMP
31+
#include <omp.h>
32+
#endif
33+
2934
/**
3035
* RubixML/Tensor/Matrix::identity
3136
*/
@@ -1632,6 +1637,7 @@ PHP_METHOD(CRubix, min)
16321637

16331638
rtn_ca = CArray_Zeros(newshape, 1, TYPE_INTEGER, &order, &rtn_ptr);
16341639
it = CArray_NewIter(target_ca);
1640+
16351641
do {
16361642
if (tmp_int > IT_IDATA(it)[0] || i == 0) {
16371643
IDATA(rtn_ca)[j] = IT_IDATA(it)[0];
@@ -1654,6 +1660,7 @@ PHP_METHOD(CRubix, min)
16541660

16551661
rtn_ca = CArray_Zeros(newshape, 1, TYPE_DOUBLE, &order, &rtn_ptr);
16561662
it = CArray_NewIter(target_ca);
1663+
16571664
do {
16581665
if (tmp_double > IT_DDATA(it)[0] || i == 0) {
16591666
DDATA(rtn_ca)[j] = IT_DDATA(it)[0];
@@ -1874,7 +1881,6 @@ PHP_METHOD(CRubix, quantile)
18741881
return;
18751882
}
18761883

1877-
18781884
// For 2-D Tensor
18791885
x = q * (CArray_DIMS(target_ca)[1] - 1.0) + 1.0;
18801886
x_hat = (int)x;
@@ -1891,9 +1897,9 @@ PHP_METHOD(CRubix, quantile)
18911897
result = CArray_NewFromDescr(result, descr, 1, dims, NULL, NULL, 0, NULL);
18921898

18931899
for (i = 0; i < CArray_DIMS(target_ca)[0]; i++) {
1894-
key = i * CArray_DIMS(target_ca)[1];
1895-
t = DDATA(sorted_ca)[(key + (x_hat - 1))];
1896-
DDATA(result)[i] = t + remainder * (DDATA(sorted_ca)[key + x_hat] - t);
1900+
key = i * CArray_DIMS(target_ca)[1];
1901+
t = DDATA(sorted_ca)[(key + (x_hat - 1))];
1902+
DDATA(result)[i] = t + remainder * (DDATA(sorted_ca)[key + x_hat] - t);
18971903
}
18981904

18991905
add_to_buffer(&out_ptr, result, sizeof(CArray));
@@ -2002,7 +2008,44 @@ PHP_METHOD(CRubix, ceil)
20022008
*/
20032009
PHP_METHOD(CRubix, clip)
20042010
{
2011+
MemoryPointer ptr_a, ptr_min, ptr_max, ptr_rtn;
2012+
CArray * ca_a, * ca_min = NULL, * ca_max = NULL, * rtn = NULL;
2013+
zval * a, * a_min, * a_max;
2014+
ZEND_PARSE_PARAMETERS_START(3, 3)
2015+
Z_PARAM_ZVAL(a)
2016+
Z_PARAM_ZVAL(a_min)
2017+
Z_PARAM_ZVAL(a_max)
2018+
ZEND_PARSE_PARAMETERS_END();
2019+
ZVAL_TO_MEMORYPOINTER(a, &ptr_a, NULL);
2020+
ZVAL_TO_MEMORYPOINTER(a_min, &ptr_min, NULL);
2021+
ZVAL_TO_MEMORYPOINTER(a_max, &ptr_max, NULL);
2022+
2023+
ca_a = CArray_FromMemoryPointer(&ptr_a);
2024+
2025+
if (Z_TYPE_P(a_min) != IS_NULL) {
2026+
ca_min = CArray_FromMemoryPointer(&ptr_min);
2027+
}
2028+
if (Z_TYPE_P(a_max) != IS_NULL) {
2029+
ca_max = CArray_FromMemoryPointer(&ptr_max);
2030+
}
2031+
2032+
rtn = CArray_Clip(ca_a, ca_min, ca_max, &ptr_rtn);
2033+
2034+
FREE_FROM_MEMORYPOINTER(&ptr_a);
2035+
2036+
if (Z_TYPE_P(a_min) != IS_NULL) {
2037+
FREE_FROM_MEMORYPOINTER(&ptr_min);
2038+
}
2039+
2040+
if (Z_TYPE_P(a_max) != IS_NULL) {
2041+
FREE_FROM_MEMORYPOINTER(&ptr_max);
2042+
}
2043+
2044+
if (rtn == NULL) {
2045+
return;
2046+
}
20052047

2048+
RETURN_MEMORYPOINTER(return_value, &ptr_rtn);
20062049
}
20072050

20082051
/**

kernel/interfaces/rubix.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ PHP_METHOD(CRubix, covariance); //OK PASSED
8484
PHP_METHOD(CRubix, round);
8585
PHP_METHOD(CRubix, floor); //OK PASSED
8686
PHP_METHOD(CRubix, ceil); //OK PASSED
87-
PHP_METHOD(CRubix, clip);
88-
PHP_METHOD(CRubix, clipLower);
89-
PHP_METHOD(CRubix, clipUpper);
87+
PHP_METHOD(CRubix, clip); //OK PASSED
88+
PHP_METHOD(CRubix, clipLower); //OK
89+
PHP_METHOD(CRubix, clipUpper); //OK PASSED
9090
PHP_METHOD(CRubix, sign);
9191
PHP_METHOD(CRubix, negate); //OK PASSED
9292
PHP_METHOD(CRubix, insert);

phpsci.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ ZVAL_TO_MEMORYPOINTER(zval * obj, MemoryPointer * ptr, char * type)
124124
throw_overflow_exception("CArrays only works with int32 and "
125125
"float64 values, LONG INT detected.");
126126
}
127-
128127
IDATA(self)[0] = (int)zval_get_long(obj);
129128
add_to_buffer(ptr, self, sizeof(CArray));
130129
efree(dims);
@@ -2725,6 +2724,7 @@ static zend_function_entry crubix_class_methods[] =
27252724
PHP_ME(CRubix, subMatrix, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
27262725
PHP_ME(CRubix, solve, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
27272726
PHP_ME(CRubix, quantile, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
2727+
PHP_ME(CRubix, clip, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
27282728

27292729
PHP_ME(CRubix, log, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
27302730
PHP_ME(CRubix, log1p, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)

0 commit comments

Comments
 (0)