Skip to content

Commit

Permalink
version 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichiis committed Mar 20, 2024
1 parent 099615e commit 6b87fbc
Show file tree
Hide file tree
Showing 66 changed files with 15,680 additions and 11,438 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ on: [push]

jobs:
no_ext_tests:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04

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

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

- name: Composer
uses: php-actions/composer@v6
Expand All @@ -24,6 +24,6 @@ jobs:
uses: php-actions/phpunit@v3
with:
configuration: tests/phpunit.xml
version: 8.5
version: 10.5
php_version: ${{ matrix.php }}

memory_limit: 512M
87 changes: 50 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,65 @@ 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 "rindow_openblas" extension
- BLAS functions
- Functions useful for machine learning
- Tools for integrating C/C++ through the FFI (OpenBLAS,Rindow-Matlib,CLBlast etc.)
- GPU support on your laptop without n-vidia (OpenCL with Intel,AMD etc.)
- Useful linear algebra and random number capabilities



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

Requirements
============

- PHP8.0 or PHP 8.1 or PHP8.2
- PHP7.2 or PHP7.3 or PHP7.4 is not supported in this release. Please use Release 1.1, which supports PHP7.2 or PHP7.3 or PHP7.4 or PHP 8.0.



### Download the rindow_openblas extension
- PHP 8.1 or PHP8.2 or PHP8.3
- PHP7.2, PHP7.3, PHP7.4 and PHP 8.0 are not supported in this release. Please use Release 1.1, which supports them.

### Strong recommend ###
You can perform very fast N-dimensional array operations in conjunction

- [Pre-build binaries](https://github.com/rindow/rindow-openblas/releases)
- [Build from source](https://github.com/rindow/rindow-openblas)

### Acceleration with GPU

You can use GPU acceleration on OpenCL.

- [rindow-math-matrix-matlibffi](https://github.com/rindow/rindow-math-matrix-matlibffi): plug-in drivers for OpenBLAS,Rindow-Matlib,OpenCL,CLBlast for FFI
- Pre-build binaries
- [rindow-opencl](https://github.com/rindow/rindow-opencl/releases)
- [rindow-clblast](https://github.com/rindow/rindow-clblast/releases)
- Build from source
- [rindow-opencl](https://github.com/rindow/rindow-opencl)
- [rindow-clblast](https://github.com/rindow/rindow-clblast)
- [Rindow matlib](https://github.com/rindow/rindow-matlib/releases)
- [OpenBLAS](https://github.com/xianyi/OpenBLAS/releases)
- [CLBlast](https://github.com/CNugteren/CLBlast/releases)

*Note:*
Please see the [rindow-math-matrix-matlibffi](https://github.com/rindow/rindow-math-matrix-matlibffi) to setup plug-in and pre-build binaries.

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

Tested on 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. I made a temporary fix to make it look like it would work, but gave up on careful testing.
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.
How to Setup
============
Set it up using composer.

```shell
$ composer require rindow-math-matrix
```

You can use it as is, but you will need to speed it up to process at a practical speed.

And then, Set up pre-build binaries for the required high-speed calculation libraries. Click [here](https://github.com/rindow/rindow-math-matrix-matlibffi) for details.

```shell
$ composer require rindow-math-matrix-matlibffi
```

Sample programs
===============
```php
<?php
// sample.php
include __DIR__.'/vendor/autoload.php';
use Rindow\Math\Matrix\MatrixOperator;

$mo = new MatrixOperator();
$a = $mo->array([[1,2],[3,4]]);
$b = $mo->array([[2,3],[4,5]]);
$c = $mo->cross($a,$b);
echo $mo->toString($c,indent:true)."\n";
```
```shell
$ php sample.php
[
[10,13],
[22,29]
]
```
10 changes: 4 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
"keywords": ["rindow","math","matrix","N-dimension","operation"],
"license": "BSD-3-Clause",
"require": {
"php": "^8.0|^8.1|^8.2",
"interop-phpobjects/polite-math": "~1.0.4"
"php": "^8.1",
"interop-phpobjects/polite-math": "~1.0.6"
},
"suggest": {
"ext-rindow_openblas": "0.3.0 strongly recommended for speed",
"ext-rindow_opencl": "0.1.4 GPU/OpenCL support",
"ext-rindow_clblast": "0.1.2 BLAS on GPU/OpenCL support",
"rindow/math-plot": "for OpenCL tunning"
"rindow/math-plot": "for OpenCL tunning",
"rindow/rindow-math-matrix-matlibffi": "~1.0.0"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 15 additions & 0 deletions src/C.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Rindow\Math\Matrix;

use Interop\Polite\Math\Matrix\NDArray;

function C(
float $r=null,
float $i=null,
) : Complex
{
$r = $r ?? 0.0;
$i = $i ?? 0.0;
return new Complex($r,$i);
}

18 changes: 18 additions & 0 deletions src/Complex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Rindow\Math\Matrix;

// typedef struct { float real, imag; } openblas_complex_float;

class Complex
{
public function __construct(
public readonly float $real,
public readonly float $imag,
) {}

public function __toString() : string
{
$and = ($this->imag>=0)?'+':'';
return "{$this->real}{$and}{$this->imag}i";
}
}
106 changes: 106 additions & 0 deletions src/ComplexUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
namespace Rindow\Math\Matrix;

require_once __DIR__.'/C.php';
//use function Rindow\Math\Matrix\C;
use Interop\Polite\Math\Matrix\NDArray;

trait ComplexUtils
{
protected function cbuild(float $r=null,float $i=null) : object
{
return C($r,i:$i);
}

protected function crebuild(object $value) : object
{
return C($value->real,i:$value->imag);
}

protected function cisobject(mixed $value) : bool
{
if(!is_object($value)) {
return false;
}
return (property_exists($value,'real') &&
property_exists($value,'imag') );
}

protected function cobjecttype(mixed $value) : string
{
if(is_object($value)) {
return get_class($value);
}
return gettype($value);
}

protected function cistype(int $dtype=null) : bool
{
return $dtype==NDArray::complex64||$dtype==NDArray::complex128;
}

protected function ciszero(object $value) : bool
{
return $value->real==0 && $value->imag==0;
}

protected function cisone(object $value) : bool
{
return $value->real==1 && $value->imag==0;
}

protected function cabs(object $value) : float
{
return sqrt($value->real*$value->real + $value->imag*$value->imag);
}

protected function cconj(object $value) : object
{
return C($value->real,i:-$value->imag);
}

protected function cadd(object $x,object $y) : object
{
return C(
($x->real+$y->real),
i:($x->imag+$y->imag)
);
}

protected function csub(object $x,object $y) : object
{
return C(
($x->real-$y->real),
i:($x->imag-$y->imag)
);
}

protected function cmul(object $x,object $y) : object
{
return C(
(($x->real*$y->real)-($x->imag*$y->imag)),
i:(($x->real*$y->imag)+($x->imag*$y->real))
);
}

protected function cdiv(object $x,object $y) : object
{
$denominator = $y->real * $y->real + $y->imag * $y->imag;
if($denominator==0) {
return C(NAN,i:NAN);
}
$real = (($x->real*$y->real)+($x->imag*$y->imag))/$denominator;
$imag = (($x->imag*$y->real)-($x->real*$y->imag))/$denominator;
return C($real,i:$imag);
}

protected function csqrt(object $x) : object
{
$r = sqrt($x->real*$x->real + $x->imag*$x->imag);
$theta = atan2($x->imag, $x->real) / 2.0;
return C(
(sqrt($r)*cos($theta)),
i:(sqrt($r)*sin($theta))
);
}
}
49 changes: 49 additions & 0 deletions src/Drivers/AbstractDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace Rindow\Math\Matrix\Drivers;

use LogicException;

abstract class AbstractDriver implements Driver
{
protected string $LOWEST_VERSION = '1000.1000.1000';
protected string $OVER_VERSION = '0.0.0';

protected function assertExtensionVersion($name,$lowestVersion,$overVersion)
{
$currentVersion = phpversion($name);
if(version_compare($currentVersion,$lowestVersion)<0||
version_compare($currentVersion,$overVersion)>=0 ) {
throw new LogicException($name.' '.$currentVersion.' is an unsupported version. '.
'Supported versions are greater than or equal to '.$lowestVersion.
' and less than '.$overVersion.'.');
}
}

protected function assertVersion()
{
$this->assertExtensionVersion($this->extName,
$this->LOWEST_VERSION,
$this->OVER_VERSION);
}

public function name() : string
{
return $this->extName();
}

public function isAvailable() : bool
{
return extension_loaded($this->extName);
}

public function extName() : string
{
return $this->extName;
}

public function version() : string
{
return phpversion($this->extName);
}

}
Loading

0 comments on commit 6b87fbc

Please sign in to comment.