-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
746 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: tests | ||
|
||
on: [push] | ||
|
||
jobs: | ||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
vendor/ | ||
*.log | ||
composer.lock | ||
composer.phar | ||
tests/.phpunit.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,28 @@ | ||
# rindow-math-buffer-ffi | ||
The Buffer for math libraries | ||
============================= | ||
Status: | ||
[![Build Status](https://github.com/rindow/rindow-math-buffer-ffi/workflows/tests/badge.svg)](https://github.com/rindow/rindow-math-buffer-ffi/actions) | ||
[![Downloads](https://img.shields.io/packagist/dt/rindow/rindow-math-buffer-ffi)](https://packagist.org/packages/rindow/rindow-math-buffer-ffi) | ||
[![Latest Stable Version](https://img.shields.io/packagist/v/rindow/rindow-math-buffer-ffi)](https://packagist.org/packages/rindow/rindow-math-buffer-ffi) | ||
[![License](https://img.shields.io/packagist/l/rindow/rindow-math-buffer-ffi)](https://packagist.org/packages/rindow/rindow-math-buffer-ffi) | ||
|
||
"The rindow math buffer ffi" is a buffer for the math library. Available in libraries with FFI interface. | ||
|
||
- Provides Universal Buffer for 1-dimension for data exchange between C,C+ language and PHP. | ||
|
||
|
||
Please see the documents about Buffer objects on [Rindow Mathematics](https://rindow.github.io/mathematics/matrix/arrayobjects.html#buffer-object) web pages. | ||
|
||
Requirements | ||
============ | ||
|
||
- PHP 8.1 or PHP8.2 or PHP8.3 | ||
- Linux or Windows | ||
|
||
How to setup | ||
============ | ||
Install using composer. | ||
|
||
$ composer require rindow/rindow-math-buffer-ffi | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "rindow/rindow-math-buffer-ffi", | ||
"type": "library", | ||
"description": "The Buffer for math libraries", | ||
"keywords": ["rindow","math","matrix","buffer","FFI"], | ||
"license": "BSD-3-Clause", | ||
"require": { | ||
"php": "^8.1", | ||
"interop-phpobjects/polite-math": "~1.0.6" | ||
}, | ||
"require-dev": { | ||
"ext-ffi": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Rindow\\Math\\Buffer\\FFI\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
<?php | ||
namespace Rindow\Math\Buffer\FFI; | ||
|
||
use Interop\Polite\Math\Matrix\LinearBuffer; | ||
use Interop\Polite\Math\Matrix\NDArray; | ||
use TypeError; | ||
use InvalidArgumentException; | ||
use OutOfRangeException; | ||
use LogicException; | ||
use FFI; | ||
|
||
class Buffer implements LinearBuffer | ||
{ | ||
const MAX_BYTES = 2147483648; // 2**31 | ||
static protected ?FFI $ffi = null; | ||
|
||
protected static $typeString = [ | ||
NDArray::bool => 'uint8_t', | ||
NDArray::int8 => 'int8_t', | ||
NDArray::int16 => 'int16_t', | ||
NDArray::int32 => 'int32_t', | ||
NDArray::int64 => 'int64_t', | ||
NDArray::uint8 => 'uint8_t', | ||
NDArray::uint16 => 'uint16_t', | ||
NDArray::uint32 => 'uint32_t', | ||
NDArray::uint64 => 'uint64_t', | ||
//NDArray::float8 => 'N/A', | ||
//NDArray::float16 => 'N/A', | ||
NDArray::float32 => 'float', | ||
NDArray::float64 => 'double', | ||
//NDArray::complex16 => 'N/A', | ||
//NDArray::complex32 => 'N/A', | ||
NDArray::complex64 => 'rindow_complex_float', | ||
NDArray::complex128 => 'rindow_complex_double', | ||
]; | ||
protected static $valueSize = [ | ||
NDArray::bool => 1, | ||
NDArray::int8 => 1, | ||
NDArray::int16 => 2, | ||
NDArray::int32 => 4, | ||
NDArray::int64 => 8, | ||
NDArray::uint8 => 1, | ||
NDArray::uint16 => 2, | ||
NDArray::uint32 => 4, | ||
NDArray::uint64 => 8, | ||
//NDArray::float8 => 'N/A', | ||
//NDArray::float16 => 'N/A', | ||
NDArray::float32 => 4, | ||
NDArray::float64 => 8, | ||
//NDArray::complex16 => 'N/A', | ||
//NDArray::complex32 => 'N/A', | ||
NDArray::complex64 => 8, | ||
NDArray::complex128 => 16, | ||
]; | ||
|
||
protected int $size; | ||
protected int $dtype; | ||
protected object $data; | ||
|
||
public function __construct(int $size, int $dtype) | ||
{ | ||
if(self::$ffi===null) { | ||
$code = file_get_contents(__DIR__.'/buffer.h'); | ||
self::$ffi = FFI::cdef($code); | ||
} | ||
if(!isset(self::$typeString[$dtype])) { | ||
throw new InvalidArgumentException("Invalid data type"); | ||
} | ||
$limitsize = intdiv(self::MAX_BYTES,self::$valueSize[$dtype]); | ||
if($size>=$limitsize) { | ||
throw new InvalidArgumentException("Data size is too large."); | ||
} | ||
$this->size = $size; | ||
$this->dtype = $dtype; | ||
$declaration = self::$typeString[$dtype]; | ||
$this->data = self::$ffi->new("{$declaration}[{$size}]"); | ||
} | ||
|
||
protected function assertOffset($method, mixed $offset) : void | ||
{ | ||
if(!is_int($offset)) { | ||
throw new TypeError($method.'(): Argument #1 ($offset) must be of type int'); | ||
} | ||
if($offset<0 || $offset>=$this->size) { | ||
throw new OutOfRangeException($method.'(): Index invalid or out of range'); | ||
} | ||
} | ||
|
||
protected function assertOffsetIsInt($method, mixed $offset) : void | ||
{ | ||
if(!is_int($offset)) { | ||
throw new TypeError($method.'(): Argument #1 ($offset) must be of type int'); | ||
} | ||
} | ||
|
||
protected function isComplex($dtype=null) : bool | ||
{ | ||
$dtype = $dtype ?? $this->dtype; | ||
return $dtype==NDArray::complex64||$dtype==NDArray::complex128; | ||
} | ||
|
||
public function dtype() : int | ||
{ | ||
return $this->dtype; | ||
} | ||
|
||
public function value_size() : int | ||
{ | ||
return $this::$valueSize[$this->dtype]; | ||
} | ||
|
||
public function addr(int $offset) : FFI\CData | ||
{ | ||
return FFI::addr($this->data[$offset]); | ||
} | ||
|
||
public function count() : int | ||
{ | ||
return $this->size; | ||
} | ||
|
||
public function offsetExists(mixed $offset) : bool | ||
{ | ||
$this->assertOffsetIsInt('offsetExists',$offset); | ||
return ($offset>=0)&&($offset<$this->size); | ||
} | ||
|
||
public function offsetGet(mixed $offset): mixed | ||
{ | ||
$this->assertOffset('offsetGet',$offset); | ||
$value = $this->data[$offset]; | ||
if($this->dtype===NDArray::bool) { | ||
$value = $value ? true : false; | ||
} | ||
return $value; | ||
} | ||
|
||
public function offsetSet(mixed $offset, mixed $value): void | ||
{ | ||
$this->assertOffset('offsetSet',$offset); | ||
if($this->isComplex()) { | ||
if(is_array($value)) { | ||
[$real,$imag] = $value; | ||
} elseif(is_object($value)) { | ||
$real = $value->real; | ||
$imag = $value->imag; | ||
} else { | ||
$type = gettype($value); | ||
throw new InvalidArgumentException("Cannot convert to complex number.: ".$type); | ||
} | ||
$value = self::$ffi->new(self::$typeString[$this->dtype]); | ||
$value->real = $real; | ||
$value->imag = $imag; | ||
} | ||
$this->data[$offset] = $value; | ||
} | ||
|
||
public function offsetUnset(mixed $offset): void | ||
{ | ||
//$this->assertOffsetIsInt('offsetUnset',$offset); | ||
throw new LogicException("Illigal Operation"); | ||
} | ||
|
||
public function dump() : string | ||
{ | ||
$byte = self::$valueSize[$this->dtype] * $this->size; | ||
$buf = self::$ffi->new("char[$byte]"); | ||
FFI::memcpy($buf,$this->data,$byte); | ||
return FFI::string($buf,$byte); | ||
} | ||
|
||
public function load(string $string) : void | ||
{ | ||
$byte = self::$valueSize[$this->dtype] * $this->size; | ||
$strlen = strlen($string); | ||
if($strlen!=$byte) { | ||
throw new InvalidArgumentException("Unmatch data size. buffer size is $byte. $strlen byte given."); | ||
} | ||
FFI::memcpy($this->data,$string,$byte); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
namespace Rindow\Math\Buffer\FFI; | ||
|
||
use FFI; | ||
|
||
class BufferFactory | ||
{ | ||
public function isAvailable() : bool | ||
{ | ||
return class_exists(FFI::class); | ||
} | ||
|
||
public function Buffer(int $size, int $dtype) : Buffer | ||
{ | ||
return new Buffer($size, $dtype); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#define FFI_SCOPE "Rindow\\Math\\Buffer\\FFI" | ||
|
||
typedef struct _rindow_complex_float { float real, imag; } rindow_complex_float; | ||
typedef struct _rindow_complex_double { double real, imag; } rindow_complex_double; | ||
//typedef struct { xdouble real, imag; } openblas_complex_xdouble; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/cache/ | ||
/data/ | ||
/tmp/ | ||
.phpunit.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?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'; | ||
} | ||
|
||
define('RINDOWTEST_TEMP_DIR',__DIR__.'/tmp'); | ||
if(!file_exists(RINDOWTEST_TEMP_DIR)) { | ||
mkdir(RINDOWTEST_TEMP_DIR); | ||
} | ||
#if(!class_exists('PHPUnit\Framework\TestCase')) { | ||
# include __DIR__.'/travis/patch55.php'; | ||
#} |
Oops, something went wrong.