Supports GPU acceleration on PHP

Overview

We initially chose OpenCL because it supports a variety of arithmetic accelerations, including GPUs. As a result, you can expect faster GPU speeds even in an inexpensive notebook PC environment.

To speed up numerical operations, you need not only support for OpenCL, but also a calculation library that uses OpenCL. CLBlast is a BLAS compatible library currently under development. In addition to this, Rindow Math Matrix allows you to perform other MATH functions and various necessary operations on OpenCL.

Rindow OpenCL FFI

OpenCL is available from PHP through Rindow OpenCL FFI. OpenCL version is limited to 1.2 and is compatible with a wide range of environments.

Our goal is to use it with the Rindow neural network library, so it currently only has the bare minimum of functionality. It will be expanded in the future.

Rindow CLBlast FFI

CLBlast is a BLAS library on top of OpenCL. Click here for details.

Rindow CLBlast FFI is a PHP binding for the above library.

OpenCLMath library for Rindow Math Matrix

Provides useful functions on OpenCL that are not included in the BLAS library.

Requirements

  • PHP8.1 or PHP8.2 or PHP8.3
  • interop-phpobjects/polite-math 1.0.6 or later
  • OpenCL 1.1 or later drivers/libraries.
  • Windows 10, 11 or Linux (Ubuntu 20.04 or Debian 12 or later)
  • Rindow Math Matrix
  • Rindow OpenCL FFI
  • Rindow CLBlast FFI

GPU/OpenCL support for Windows

OpenCL can be used by default on Windows.

Please download the pre-build binaries and set the execution path.

C:TEMP>PATH %PATH%;C:\CLBlast\bin;C:\OpenBLAS\bin;C:\Matlib\bin

Configure the PHP. Edit php.ini to use FFI extension

extension = ffi

Set up rindow-math-matrix to use OpenCL using composer. And make sure you are in Advanced mode.

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

If you are unable to successfully set the target GPU, please check the OpenCL device status using the clinfo command.

C:tutorials>vendor\bin\clinfo
Number of platforms(1)
Platform(0)
    CL_PLATFORM_NAME=Intel(R) OpenCL
    CL_PLATFORM_PROFILE=FULL_PROFILE
....
...
..

GPU/OpenCL support for Linux

Install the libraries required.

  • Install OpenBLAS with apt command
  • Download the latest version of Rindow-Matlib’s pre-built binary files from https://github.com/rindow/rindow-matlib/releases.
  • Install the downloaded deb file using the apt command.
  • Set Rindow-Matlib to serial mode for use with PHP.
$ sudo apt install libopenblas0-openmp liblapacke
$ wget https://github.com/rindow/rindow-matlib/releases/download/X.X.X/rindow-matlib_X.X.X_amd64.deb
$ sudo apt install ./rindow-matlib_X.X.X_amd64.deb

But if you are not allowed to use the openmp version of openblas, there is another way. Click here for more information.

It is essential that OpenCL works properly in the Linux environment. (That’s quite difficult)

Install the OpenCL environment.

$ sudo apt install clinfo
$ sudo apt install intel-opencl-icd

Linux standard OpenCL drivers include:

  • mesa-opencl-icd
  • beignet-opencl-icd
  • intel-opencl-icd
  • nvidia-opencl-icd-xxx
  • pocl-opencl-icd

The standard Linux OpenCL driver does not work properly, so we deal with it on a case-by-case basis to make it work somehow for each driver and version.

Check that OpenCL is running using the clinfo command.

$ clinfo
Number of platforms                               1
  Platform Name                                   Intel Gen OCL Driver
  Platform Vendor                                 Intel
....
...
..

Download and install the CLBlast library. Scripts are available for easy download and installation.

  • Check the latest version: CLBlast library
  • Copy script
  • Change the version at the beginning of the script
  • Run script and create deb file
  • Install deb file
$ sudo apt install libclblast1

If you are using Ubuntu 20.04, your distribution does not include clblast. Download and install the clblast pre-built binaries.

$ cp vendor/rindow/rindow-clblast-ffi/clblast-packdeb.sh .
$ vi clblast-packdeb.sh
CLBLASTVERSION=1.6.2   <===== change
$ sh clblast-packdeb.sh
$ sudo apt install ./clblast_X.X.X-1_amd64.deb

Configure the rindow-math-matrix.

  • Verify that rindow-math-matrix is Accelerated and the OpenCL driver is recognized.
$ composer rindow/rindow-math-matrix
$ composer rindow/rindow-math-matrix-ffi
$ vendor\bin\rindow-math-matrix
Service Level   : Accelerated
Buffer Factory  : Rindow\Math\Buffer\FFI\BufferFactory
BLAS Driver     : Rindow\OpenBLAS\FFI\Blas
LAPACK Driver   : Rindow\OpenBLAS\FFI\Lapack
Math Driver     : Rindow\Matlib\FFI\Matlib
OpenCL Factory  : Rindow\OpenCL\FFI\OpenCLFactory
CLBlast Factory : Rindow\CLBlast\FFI\CLBlastFactory