rindow/rindow-math-matrix-matlibffi

Rindow Math Matrix's Drivers for Matlib with FFI

1.0.2 2024-04-29 06:46 UTC

This package is auto-updated.

Last update: 2024-10-29 07:47:22 UTC


README

Status: Build Status Downloads Latest Stable Version License

This package is matlib drivers for Rindow-math-matrix. These drivers act as adapters to drive PHP FFI. Each PHP C Libraries requires a separate download and installation of a binary file appropriate for your environment's PHP version and OS version.

Rindow Math Matrix is the fundamental package for scientific matrix operation

  • A powerful N-dimensional array object
  • Sophisticated (broadcasting) functions
  • Tools for integrating C/C++ through the FFI
  • Useful linear algebra and random number capabilities

Please see the documents on Rindow mathematics projects web pages.

Rindow Math Matrix's repository is here.

Requirements

  • PHP 8.1 or PHP8.2 or PHP8.3
  • Rindow Math Matrix v2.0.0
  • Rindow Matlib 1.0.0 or later
  • OpenBLAS 0.3.20 or later
  • OpenCL 1.1 or later
  • CLBlast 1.5.2 or later
  • Windows 10/11 or Linux(Ubuntu 20.04 or Debian 12 or later)

Download pre-build binaries from each projects

You can perform very fast N-dimensional array operations in conjunction. Download the pre-build binary files from each project's release page.

Setup for Windows

Download the binary file, unzip it, and copy it to the execution directory.

  • rindow-matlib-X.X.X-win64.zip
  • OpenBLAS-X.X.X-x64.zip
  • CLBlast-X.X.X-windows-x64.zip

Add FFI extension to php.ini

C:\TMP> cd \path\to\php\directory
C:\PHP> notepad php.ini

extension=ffi
C:\TMP> PATH %PATH%;\path\to\binary\directories\bin
C:\TMP> cd \your\progject\directory
C:\PRJ> composer require rindow/rindow-math-matrix
C:\PRJ> composer require rindow/rindow-math-matrix-matlibffi
C:\PRJ> vendor/bin/rindow-math-matrix
Service Level   : Accelerated
Buffer Factory  : Rindow\Math\Buffer\FFI\BufferFactory
BLAS Driver     : Rindow\OpenBLAS\FFI\Blas(THREAD)
LAPACK Driver   : Rindow\OpenBLAS\FFI\Lapack
Math Driver     : Rindow\Matlib\FFI\Matlib(OPENMP)
OpenCL Factory  : Rindow\OpenCL\FFI\OpenCLFactory
CLBlast Factory : Rindow\CLBlast\FFI\CLBlastFactory

The OpenCL 1.2 environment is already set up if you are using the Windows standard driver.

If you add the -v option as shown below, the driver loading status at boot time will be displayed. It will help with troubleshooting.

C:\PRJ> vendor/bin/rindow-math-matrix -v

Setup for Linux

Install each library using the apt command.

Make sure FFI extension is enabled.

$ php -m | grep FFI
FFI

Download the pre-build binary file.

Please install using the apt command.

$ sudo apt install ./rindow-matlib_X.X.X_amd64.deb

Since rindow-matlib currently uses OpenMP, choose the OpenMP version for OpenBLAS as well.

Using the pthread version of OpenBLAS can cause conflicts and become unstable and slow. This issue does not occur on Windows.

$ sudo apt install libopenblas0-openmp liblapacke

If you want to use GPU, install the OpenCL environment. In addition, there are the following drivers.

  • mesa-opencl-icd
  • beignet-opencl-icd
  • intel-opencl-icd
  • nvidia-opencl-icd-xxx
  • pocl-opencl-icd
$ sudo apt install clinfo
$ sudo apt install mesa-opencl-icd
$ sudo mkdir -p /usr/local/usr/lib
$ sudo ln -s /usr/lib/clc /usr/local/usr/lib/clc

And then, Install the fast matrix calculation library for OpenCL. If you use Ubuntu22.04 or Debian 12 or later, You can install it from distribution packages.

$ sudo apt install libclblast1

If You use Ubuntu20.04 or Debian 11, You need to download clblast from Github and make deb file. Please download the CLBlast installation script from the rindow-clblast-ffi release page.

$ wget https://github.com/rindow/rindow-clblast-ffi/releases/download/X.X.X/clblast-packdeb.zip
$ unzip clblast-packdeb.zip
$ sh clblast-packdeb.sh
$ sudo apt install ./clblast_X.X.X_amd64.deb

And then, Install the rindow-math-matrix on your project directory.

$ composer require rindow/rindow-math-matrix
$ composer require rindow/rindow-math-matrix-matlibffi
$ vendor/bin/rindow-math-matrix
Service Level   : Accelerated
Buffer Factory  : Rindow\Math\Buffer\FFI\BufferFactory
BLAS Driver     : Rindow\OpenBLAS\FFI\Blas(OPENMP)
LAPACK Driver   : Rindow\OpenBLAS\FFI\Lapack
Math Driver     : Rindow\Matlib\FFI\Matlib(OPENMP)
OpenCL Factory  : Rindow\OpenCL\FFI\OpenCLFactory
CLBlast Factory : Rindow\CLBlast\FFI\CLBlastFactory

If you add the -v option as shown below, the driver loading status at boot time will be displayed. It will help with troubleshooting.

$ vendor/bin/rindow-math-matrix -v

Check driver status

You can check the driver settings by running the sample below.

<?php
// status.php
include_once __DIR__.'/vendor/autoload.php';
use Rindow\Math\Matrix\MatrixOperator;

$mo = new MatrixOperator();

echo $mo->service()->info();
$ php status.php
Service Level   : Accelerated
Buffer Factory  : Rindow\Math\Buffer\FFI\BufferFactory
BLAS Driver     : Rindow\OpenBLAS\FFI\Blas(OPENMP)
LAPACK Driver   : Rindow\OpenBLAS\FFI\Lapack
Math Driver     : Rindow\Matlib\FFI\Matlib(OPENMP)
OpenCL Factory  : Rindow\OpenCL\FFI\OpenCLFactory
CLBlast Factory : Rindow\CLBlast\FFI\CLBlastFactory

Troubleshooting for Linux

Since rindow-matlib currently uses OpenMP, choose the OpenMP version for OpenBLAS as well.

Using the pthread version of OpenBLAS can cause conflicts and become unstable and slow. This issue does not occur on Windows.

If you have already installed the pthread version of OpenBLAS,

$ sudo apt remove libopenblas0-pthread

But if you can't remove it, you can switch to it using the update-alternatives command.

$ sudo update-alternatives --config libopenblas.so.0-x86_64-linux-gnu
$ sudo update-alternatives --config liblapack.so.3-x86_64-linux-gnu

If you really want to use the pthread version of OpenBLAS, please switch to the serial version of rindow-matlib.

There are no operational mode conflicts with OpenBLAS on Windows.

But, If you really want to use the pthread version of OpenBLAS, please switch to the serial version of rindow-matlib.

$ sudo update-alternatives --config librindowmatlib.so
There are 2 choices for the alternative librindowmatlib.so (providing /usr/lib/librindowmatlib.so).

  Selection    Path                                             Priority   Status
------------------------------------------------------------
* 0            /usr/lib/rindowmatlib-openmp/librindowmatlib.so   95        auto mode
  1            /usr/lib/rindowmatlib-openmp/librindowmatlib.so   95        manual mode
  2            /usr/lib/rindowmatlib-serial/librindowmatlib.so   90        manual mode

Press <enter> to keep the current choice[*], or type selection number: 2

Choose the "rindowmatlib-serial".

Acceleration with GPU

You can use GPU acceleration on OpenCL.

Note:

This OpenCL support extension works better in your environment and helps speed up your laptop environment without n-NVIDIA.

Tested on Ivy-bridge and AMD's Bobcat architecture APU.

In the Windows environment, Integrated GPU usage was more effective than CPU, and it worked comfortably.

However, OLD AMD APU on Linux, libclc used in linux standard mesa-opencl-icd is very buggy and slow. If you have testable hardware, please test using the proprietary driver.

On the other hand, I tested with Ivy-bridge of Intel CPU and Integrated GPU.

It now works comfortably with various adjustments on Windows 10 Standard OpenCL Driver. However, the old Intel Integrated GPU is not very high compared to its CPU performance, so please use the right person in the right place.

And it worked fine and fast in Ubuntu 20.04 + beignet-opencl-icd environment.