Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichiis committed Mar 20, 2024
1 parent b455a81 commit 032da02
Show file tree
Hide file tree
Showing 14 changed files with 561 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
* text=auto
/tests export-ignore
/vendor export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
32 changes: 32 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: tests

on: [push]

jobs:
no_ext_tests:
runs-on: ubuntu-22.04

strategy:
fail-fast: true
matrix:
php: ['8.1','8.2','8.3']

steps:
- name: Checkout codes
uses: "actions/checkout@v4"

- name: Composer
uses: php-actions/composer@v6
with:
php_version: ${{ matrix.php }}
php_extensions: ffi

- name: PHPUnit Tests
uses: php-actions/phpunit@v3
with:
configuration: tests/phpunit.xml
version: 10.5
php_version: ${{ matrix.php }}
php_extensions: ffi
args: --filter ReleaseTest

5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vendor/
*.log
composer.lock
composer.phar
tests/.phpunit.*
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2018, rindow
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
175 changes: 174 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,174 @@
# rindow-math-matrix-matlibffi
Rindow Math Matrix's Drivers for Matlib with PHP extensions
===========================================================
Status:
[![Build Status](https://github.com/rindow/rindow-math-matrix-matlibffi/workflows/tests/badge.svg)](https://github.com/rindow/rindow-math-matrix-matlibffi/actions)
[![Downloads](https://img.shields.io/packagist/dt/rindow/rindow-math-matrix-matlibffi)](https://packagist.org/packages/rindow/rindow-math-matrix-matlibffi)
[![Latest Stable Version](https://img.shields.io/packagist/v/rindow/rindow-math-matrix-matlibffi)](https://packagist.org/packages/rindow/rindow-math-matrix-matlibffi)
[![License](https://img.shields.io/packagist/l/rindow/rindow-math-matrix-matlibffi)](https://packagist.org/packages/rindow/rindow-math-matrix-matlibffi)

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 (or PHP extensions)
- Useful linear algebra and random number capabilities

Please see the documents on [Rindow mathematics projects](https://rindow.github.io/mathematics/) web pages.

Rindow Math Matrix's repository is [here](https://github.com/rindow/rindow-math-matrix/).

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 Ubuntu 20.04 LTS 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.

- Pre-build binaries
- [Rindow Matlib](https://github.com/rindow/rindow-matlib/releases)
- [OpenBLAS](https://github.com/xianyi/OpenBLAS/releases)
- [CLBlast](https://github.com/CNugteren/CLBlast/releases)

### 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

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

extension=ffi
```

```shell
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
```

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


### Setup for Ubuntu

Install each library using the apt command.

Make sure FFI extension is enabled.
```shell
$ php -m | grep FFI
FFI
```

Install the fast matrix calculation library.
And then set the rindow-matlib to serial mode for use with PHP.
```shell
$ mkdir -p /your/project/directory
$ cd /your/project/directory
$ sudo apt install libopenblas-base libpapacke
$ 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
$ 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
```

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

```shell
$ 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.
Please download the CLBlast installation script from the rindow-clblast-ffi release page.

```shell
$ 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-1+ubuntuXX.XX_amd64.deb
```

Install the rindow-math-matrix on your project directory.
```shell
$ composer require rindow/rindow-math-matrix
$ composer require rindow/rindow-math-matrix-matlibffi
```

### Check driver status
You can check the driver settings by running the sample below.
```php
<?php
// status.php
include_once __DIR__.'/vendor/autoload.php';
use Rindow\Math\Matrix\MatrixOperator;

$mo = new MatrixOperator();

echo $mo->service()->info();
```

```shell
$ php status.php
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
```

### 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.

Windows 10 standard OpenCL driver worked fine, but it was very slow and occasionally crashed.

And it worked fine and fast in Ubuntu 20.04 + beignet-opencl-icd environment.
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "rindow/rindow-math-matrix-matlibffi",
"type": "library",
"description": "Rindow Math Matrix's Drivers for Matlib with FFI",
"keywords": ["rindow","math","matrix","N-dimension","operation"],
"license": "BSD-3-Clause",
"require": {
"php": "^8.1",
"ext-ffi": "*",
"interop-phpobjects/polite-math": "~1.0.6",
"rindow/rindow-math-matrix": "~2.0.0",
"rindow/rindow-math-buffer-ffi": "~1.0.0",
"rindow/rindow-openblas-ffi": "~1.0.0",
"rindow/rindow-matlib-ffi": "~1.0.0",
"rindow/rindow-opencl-ffi": "~1.0.0",
"rindow/rindow-clblast-ffi": "~1.0.0"
},
"suggest": {
"rindow/rindow-math-plot": "for OpenCL tunning"
},
"autoload": {
"psr-4": {
"Rindow\\Math\\Matrix\\Drivers\\MatlibFFI\\": "src/"
}
}
}
59 changes: 59 additions & 0 deletions src/MatlibFFI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
namespace Rindow\Math\Matrix\Drivers\MatlibFFI;

use Rindow\Math\Buffer\FFI\BufferFactory;
use Rindow\OpenBLAS\FFI\OpenBLASFactory;
use Rindow\Matlib\FFI\MatlibFactory;
use Rindow\OpenCL\FFI\OpenCLFactory;
use Rindow\CLBlast\FFI\CLBlastFactory;
use Rindow\Math\Matrix\Drivers\MatlibCL\MatlibCLFactory;
use Rindow\Math\Matrix\Drivers\AbstractMatlibService;

class MatlibFFI extends AbstractMatlibService
{
protected $name = 'matlib_ffi';

public function __construct(
object $bufferFactory=null,
object $mathFactory=null,
object $openblasFactory=null,
object $openclFactory=null,
object $clblastFactory=null,
object $blasCLFactory=null,
object $mathCLFactory=null,
object $bufferCLFactory=null,
)
{
if($bufferFactory===null && class_exists(BufferFactory::class)) {
$bufferFactory = new BufferFactory();
}
if($openblasFactory===null && class_exists(OpenBLASFactory::class)) {
$openblasFactory = new OpenBLASFactory();
}
if($mathFactory===null && class_exists(MatlibFactory::class)) {
$mathFactory = new MatlibFactory();
}
if($openclFactory===null && class_exists(OpenCLFactory::class)) {
$openclFactory = new OpenCLFactory();
}
$bufferCLFactory = $bufferCLFactory ?? $openclFactory;
if($clblastFactory===null && class_exists(CLBlastFactory::class)) {
$clblastFactory = new CLBlastFactory();
}
$blasCLFactory = $blasCLFactory ?? $clblastFactory;
if($mathCLFactory===null && class_exists(MatlibCLFactory::class)) {
$mathCLFactory = new MatlibCLFactory();
}

parent::__construct(
bufferFactory:$bufferFactory,
openblasFactory:$openblasFactory,
mathFactory:$mathFactory,
openclFactory:$openclFactory,
clblastFactory:$clblastFactory,
blasCLFactory:$blasCLFactory,
mathCLFactory:$mathCLFactory,
bufferCLFactory:$bufferCLFactory,
);
}
}
2 changes: 2 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cache/
data/
13 changes: 13 additions & 0 deletions tests/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
ini_set('short_open_tag', '1');

date_default_timezone_set('UTC');
#ini_set('short_open_tag',true);
if(file_exists(__DIR__.'/../vendor/autoload.php')) {
$loader = require_once __DIR__.'/../vendor/autoload.php';
} else {
$loader = require_once __DIR__.'/init_autoloader.php';
}
#if(!class_exists('PHPUnit\Framework\TestCase')) {
# include __DIR__.'/travis/patch55.php';
#}
Loading

0 comments on commit 032da02

Please sign in to comment.