# PHP support for matrix operations - BLAS, LAPACK.

- Thread: https://externals.io/message/119086
- Original archive: https://news-web.php.net/php.internals/119086
- Started by j adams on 2022-12-04T23:43:24+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #119086 — j adams — 2022-12-04T23:43:24+00:00

URL: https://externals.io/message/119086#119086

I apologize if I have the wrong mailing list here. I'm hoping ot ask the
PHP developer community if there's any appetite for functions to handle
matrix operations and scientific computing.

I took a course on machine learning which had us code solutions using
matlab/octave. This year I embarked on trying to translate those functions
to PHP and have been quite surprised to learn that pure php is quite
inefficient for matrix multiplication. I've been trying to chase down an
effective way to perform matrix operations in PHP and posting my questions
and progress on this forum at PHPBuilder:
https://board.phpbuilder.com/d/10403480-converting-matlab-machine-learning-routines-to-php-need-matrix-operations

Fast matrix operations seem to be absolutely critical for machine learning,
at least for Support Vector Machines and Neural Networks. I know that there
is a FANN extension, but it seems to me that it would be beneficial for
PHP's use in scientific applications if it had support -- either natively
or via some extension -- for matrix operations and possibly other
statistical or scientific computations. Python has numpy, for example.

How does everyone feel about BLAS or LAPACK extension for PHP? I'm poorly
equipped to cook up such a thing, but these libraries already exist and
could offer great improvements in performance.

---

## #119087 — Deleu — 2022-12-05T01:27:00+00:00

URL: https://externals.io/message/119086#119087 · In reply to: #119086

> I apologize if I have the wrong mailing list here. I'm hoping ot ask the
> PHP developer community if there's any appetite for functions to handle
> matrix operations and scientific computing.
>
> I took a course on machine learning which had us code solutions using
> matlab/octave. This year I embarked on trying to translate those functions
> to PHP and have been quite surprised to learn that pure php is quite
> inefficient for matrix multiplication. I've been trying to chase down an
> effective way to perform matrix operations in PHP and posting my questions
> and progress on this forum at PHPBuilder:
>
> https://board.phpbuilder.com/d/10403480-converting-matlab-machine-learning-routines-to-php-need-matrix-operations
>
> Fast matrix operations seem to be absolutely critical for machine learning,
> at least for Support Vector Machines and Neural Networks. I know that there
> is a FANN extension, but it seems to me that it would be beneficial for
> PHP's use in scientific applications if it had support -- either natively
> or via some extension -- for matrix operations and possibly other
> statistical or scientific computations. Python has numpy, for example.
>
> How does everyone feel about BLAS or LAPACK extension for PHP? I'm poorly
> equipped to cook up such a thing, but these libraries already exist and
> could offer great improvements in performance.
>

I remember studying some ML algorithms back in 2015 and I felt PHP was
lacking a lot in terms of matrix operations, specially invert a matrix. It
would have been cool to have some fundamental functionalities baked in.

---

## #119089 — Christoph M. Becker — 2022-12-05T10:06:23+00:00

URL: https://externals.io/message/119086#119089 · In reply to: #119086

> I apologize if I have the wrong mailing list here. I'm hoping ot ask the
> PHP developer community if there's any appetite for functions to handle
> matrix operations and scientific computing.
>
> I took a course on machine learning which had us code solutions using
> matlab/octave. This year I embarked on trying to translate those functions
> to PHP and have been quite surprised to learn that pure php is quite
> inefficient for matrix multiplication. I've been trying to chase down an
> effective way to perform matrix operations in PHP and posting my questions
> and progress on this forum at PHPBuilder:
> https://board.phpbuilder.com/d/10403480-converting-matlab-machine-learning-routines-to-php-need-matrix-operations
>
> Fast matrix operations seem to be absolutely critical for machine learning,
> at least for Support Vector Machines and Neural Networks. I know that there
> is a FANN extension, but it seems to me that it would be beneficial for
> PHP's use in scientific applications if it had support -- either natively
> or via some extension -- for matrix operations and possibly other
> statistical or scientific computations. Python has numpy, for example.
>
> How does everyone feel about BLAS or LAPACK extension for PHP? I'm poorly
> equipped to cook up such a thing, but these libraries already exist and
> could offer great improvements in performance.

There is already <https://pecl.php.net/package/Tensor>, which should
serve these needs.

--
Christoph M. Becker

---

## #119093 — Christoph M. Becker — 2022-12-06T14:29:56+00:00

URL: https://externals.io/message/119086#119093

> Christoph, thanks for this tip.
>
> Unfortunately, pecl refuses to install Tensor either on my Macbook Pro or
> on my Ubuntu 20 workstation. Both machines complain about being unable to
> find cblas.h. I ran 'sudo pecl install tensor' which has quite a bit of
> output. The problem seems to happen when it attempts to run make:
>
> running: make
> /bin/bash /tmp/pear/temp/pear-build-root7MZ6PH/tensor-3.0.2/libtool
> --mode=compile cc -I. -I/tmp/pear/temp/tensor/ext
> -I/tmp/pear/temp/pear-build-root7MZ6PH/tensor-3.0.2/include
> -I/tmp/pear/temp/pear-build-root7MZ6PH/tensor-3.0.2/main
> -I/tmp/pear/temp/tensor/ext -I/usr/include/php/20210902
> -I/usr/include/php/20210902/main -I/usr/include/php/20210902/TSRM
> -I/usr/include/php/20210902/Zend -I/usr/include/php/20210902/ext
> -I/usr/include/php/20210902/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -O3
> -ffast-math -DZEND_COMPILE_DL_EXT=1 -c /tmp/pear/temp/tensor/ext/tensor.c
> -o tensor.lo  -MMD -MF tensor.dep -MT tensor.lo
> libtool: compile:  cc -I. -I/tmp/pear/temp/tensor/ext
> -I/tmp/pear/temp/pear-build-root7MZ6PH/tensor-3.0.2/include
> -I/tmp/pear/temp/pear-build-root7MZ6PH/tensor-3.0.2/main
> -I/tmp/pear/temp/tensor/ext -I/usr/include/php/20210902
> -I/usr/include/php/20210902/main -I/usr/include/php/20210902/TSRM
> -I/usr/include/php/20210902/Zend -I/usr/include/php/20210902/ext
> -I/usr/include/php/20210902/ext/date/lib -DHAVE_CONFIG_H -g -O2 -O3
> -ffast-math -DZEND_COMPILE_DL_EXT=1 -c /tmp/pear/temp/tensor/ext/tensor.c
> -MMD -MF tensor.dep -MT tensor.lo  -fPIC -DPIC -o .libs/tensor.o
> /tmp/pear/temp/tensor/ext/tensor.c:25:10: fatal error: cblas.h: No such
> file or directory
>    25 | #include "cblas.h"
>       |          ^~~~~~~~~
> compilation terminated.
> make: *** [Makefile:208: tensor.lo] Error 1
> ERROR: `make' failed

I think you need to install the OpenBLAS dev package[1].

If in doubt, file an issue at the tensor bug tracker[2].

[1] <https://github.com/RubixML/Tensor#optional-to-compile-extension>
[2] <https://github.com/RubixML/Tensor/issues>

--
Christoph M. Becker

---

## #119182 — j adams — 2022-12-17T19:51:36+00:00

URL: https://externals.io/message/119086#119182 · In reply to: #119093

Christopher:

I have carefully followed the instructions under 'Manually compiling the
extension' in your first link[1]. When I attempted `make`, it failed,
ending with the following errors. I'll be filing an issue.

/home/jaith/biz/machine-learning/tensor/Tensor/ext/kernel/main.c: In
function ‘zephir_function_exists’:
/home/jaith/biz/machine-learning/tensor/Tensor/ext/kernel/main.c:285:101:
warning: comparison between pointer and integer
  285 |  if (zend_hash_str_exists(CG(function_table),
Z_STRVAL_P(function_name), Z_STRLEN_P(function_name)) != NULL) {
      |
                                ^~
/home/jaith/biz/machine-learning/tensor/Tensor/ext/kernel/main.c: In
function ‘zephir_function_exists_ex’:
/home/jaith/biz/machine-learning/tensor/Tensor/ext/kernel/main.c:301:76:
warning: comparison between pointer and integer
  301 |  if (zend_hash_str_exists(CG(function_table), function_name,
function_len) != NULL) {
      |
       ^~
/home/jaith/biz/machine-learning/tensor/Tensor/ext/kernel/main.c: In
function ‘zephir_get_arg’:
/home/jaith/biz/machine-learning/tensor/Tensor/ext/kernel/main.c:571:9:
error: too many arguments to function ‘zend_forbid_dynamic_call’
  571 |     if (zend_forbid_dynamic_call("func_get_arg()") == FAILURE) {
      |         ^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/php/20220829/main/php.h:35,
                 from
/home/jaith/biz/machine-learning/tensor/Tensor/ext/kernel/main.c:16:
/usr/include/php/20220829/Zend/zend_API.h:782:39: note: declared here
  782 | static zend_always_inline zend_result zend_forbid_dynamic_call(void)
      |                                       ^~~~~~~~~~~~~~~~~~~~~~~~
make: *** [Makefile:213: kernel/main.lo] Error 1


[1] <https://github.com/RubixML/Tensor#optional-to-compile-extension>

On Tue, Dec 6, 2022 at 6:29 AM Christoph M. Becker <cmbecker69@gmx.de>
wrote:

> 
>
> > […]
>
> I think you need to install the OpenBLAS dev package[1].
>
> If in doubt, file an issue at the tensor bug tracker[2].
>
> [1] <https://github.com/RubixML/Tensor#optional-to-compile-extension>
> [2] <https://github.com/RubixML/Tensor/issues>
>
> --
> Christoph M. Becker

---

## #119228 — j adams — 2023-01-04T23:13:31+00:00

URL: https://externals.io/message/119086#119228 · In reply to: #119182

I have supplied this information as issue #34 on the github project that
appears to maintain the tensor extension.
https://github.com/RubixML/Tensor/issues/34

I had commented on issue #33, where someone attempts to fix tensor for php
8.2 about 3 weeks ago, but there have been no responses:
https://github.com/RubixML/Tensor/issues/33

It's surprisingly difficult to get fast matrix operations in PHP, given how
fundamental they are to machine learning.




> Christopher:
>
> I have carefully followed the instructions under 'Manually compiling the
> extension' in your first link[1]. When I attempted `make`, it failed,
> ending with the following errors. I'll be filing an issue.
>
> /home/jaith/biz/machine-learning/tensor/Tensor/ext/kernel/main.c: In
> function ‘zephir_function_exists’:
> /home/jaith/biz/machine-learning/tensor/Tensor/ext/kernel/main.c:285:101:
> warning: comparison between pointer and integer
>   285 |  if (zend_hash_str_exists(CG(function_table),
> Z_STRVAL_P(function_name), Z_STRLEN_P(function_name)) != NULL) {
>       |
>                                   ^~
> /home/jaith/biz/machine-learning/tensor/Tensor/ext/kernel/main.c: In
> function ‘zephir_function_exists_ex’:
> /home/jaith/biz/machine-learning/tensor/Tensor/ext/kernel/main.c:301:76:
> warning: comparison between pointer and integer
>   301 |  if (zend_hash_str_exists(CG(function_table), function_name,
> function_len) != NULL) {
>       |
>          ^~
> /home/jaith/biz/machine-learning/tensor/Tensor/ext/kernel/main.c: In
> function ‘zephir_get_arg’:
> /home/jaith/biz/machine-learning/tensor/Tensor/ext/kernel/main.c:571:9:
> error: too many arguments to function ‘zend_forbid_dynamic_call’
>   571 |     if (zend_forbid_dynamic_call("func_get_arg()") == FAILURE) {
>       |         ^~~~~~~~~~~~~~~~~~~~~~~~
> In file included from /usr/include/php/20220829/main/php.h:35,
>                  from
> /home/jaith/biz/machine-learning/tensor/Tensor/ext/kernel/main.c:16:
> /usr/include/php/20220829/Zend/zend_API.h:782:39: note: declared here
>   782 | static zend_always_inline zend_result
> zend_forbid_dynamic_call(void)
>       |                                       ^~~~~~~~~~~~~~~~~~~~~~~~
> make: *** [Makefile:213: kernel/main.lo] Error 1
>
>
> [1] <https://github.com/RubixML/Tensor#optional-to-compile-extension>
>
> On Tue, Dec 6, 2022 at 6:29 AM Christoph M. Becker <cmbecker69@gmx.de>
> wrote:
>
> > […]
