Skip to content

Commit

Permalink
allocate aligned size
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichiis committed May 20, 2024
1 parent 0ed8dd5 commit 2623eed
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 121 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ jobs:
fail-fast: true
matrix:
include:
- name: macOS
os: macos-latest
php: '8.3'
- name: PHP8.1
os: ubuntu-latest
php: '8.1'
Expand All @@ -28,6 +25,12 @@ jobs:
- name: Windows
os: windows-latest
php: '8.3'
- name: macOS
os: macos-latest
php: '8.3'
- name: macOS-x86_64
os: macos-13
php: '8.3'

steps:
- name: Setup PHP ${{ matrix.php }}
Expand Down Expand Up @@ -71,4 +74,3 @@ jobs:

- name: PHPUnit Tests
run: phpunit -c tests

13 changes: 13 additions & 0 deletions arch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@


Linux amd64
Linux fv-az1106-239 6.5.0-1021-azure #22~22.04.1-Ubuntu SMP Tue Apr 30 16:08:18 UTC 2024 x86_64

Windows x64
Windows NT fv-az653-785 10.0 build 20348 (Windows Server 2022) AMD64

Intel Mac
Darwin Mac-1716203764718.local 22.6.0 Darwin Kernel Version 22.6.0: Mon Feb 19 19:48:53 PST 2024; root:xnu-8796.141.3.704.6~1/RELEASE_X86_64 x86_64

M1 Mac
Darwin Mac-1716204565015.local 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:50 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_VMAPPLE arm64
16 changes: 14 additions & 2 deletions src/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public function __construct(int $size, int $dtype)
}

if (self::$ffi === null) {
$code = @file_get_contents(__DIR__ . '/buffer.h');
$header = __DIR__ . '/buffer.h';
$code = @file_get_contents($header);
if ($code === false) {
throw new RuntimeException("Unable to read buffer.h file");
}
Expand All @@ -89,9 +90,19 @@ public function __construct(int $size, int $dtype)
$this->size = $size;
$this->dtype = $dtype;
$declaration = self::$typeString[$dtype];
$size = $this->aligned($size,$dtype,16); // 128bit
$this->data = self::$ffi->new("{$declaration}[{$size}]");
}

protected function aligned(int $size, int $dtype,int $base) : int
{
$valueSize = self::$valueSize[$dtype];
$bytes = $size*$valueSize;
$alignedBytes = intdiv(($bytes+$base-1),$base)*$base;
$alignedSize = intdiv(($alignedBytes+$valueSize-1),$valueSize)*$valueSize;
return $alignedSize;
}

protected function assertOffset(string $method, mixed $offset) : void
{
if(!is_int($offset)) {
Expand Down Expand Up @@ -181,7 +192,8 @@ public function offsetUnset(mixed $offset): void
public function dump() : string
{
$byte = self::$valueSize[$this->dtype] * $this->size;
$buf = self::$ffi->new("char[$byte]");
$alignedBytes = $this->aligned($byte,NDArray::int8,128);
$buf = self::$ffi->new("char[$alignedBytes]");
FFI::memcpy($buf,$this->data,$byte);
return FFI::string($buf,$byte);
}
Expand Down
88 changes: 0 additions & 88 deletions src/BufferNoHeader.php

This file was deleted.

62 changes: 35 additions & 27 deletions tests/RindowTest/Math/Matrix/Buffer/FFI/BufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public function setUp() : void
$this->factory = new BufferFactory();
}

protected function notSupportComplex() : bool
{
//return PHP_OS==='Darwin';
return false;
}

//public function testExtensionVersion()
//{
// $this->assertEquals('0.1.7',phpversion('rindow_openblas'));
Expand Down Expand Up @@ -108,33 +114,35 @@ public function testDtypesAndOffsetOfDtypes()
$this->assertTrue(is_float($buf[0]));
$this->assertEquals(0.5,$buf[2]);

$buf = $this->factory->Buffer(3,NDArray::complex64);
$this->assertEquals(NDArray::complex64,$buf->dtype());
$this->assertEquals(3,count($buf));
$this->assertEquals(8,$buf->value_size());
$this->assertEquals(NDArray::complex64,$buf->dtype());
$buf[1] = [1.5,2.5];
$buf[2] = (object)['real'=>3.5,'imag'=>4.5];
$vv = $buf[1];
$this->assertEquals(1.5,$vv->real);
$this->assertEquals(2.5,$vv->imag);
$vv = $buf[2];
$this->assertEquals(3.5,$vv->real);
$this->assertEquals(4.5,$vv->imag);

$buf = $this->factory->Buffer(3,NDArray::complex128);
$this->assertEquals(NDArray::complex128,$buf->dtype());
$this->assertEquals(3,count($buf));
$this->assertEquals(16,$buf->value_size());
$this->assertEquals(NDArray::complex128,$buf->dtype());
$buf[1] = [1.5,2.5];
$buf[2] = (object)['real'=>3.5,'imag'=>4.5];
$vv = $buf[1];
$this->assertEquals(1.5,$vv->real);
$this->assertEquals(2.5,$vv->imag);
$vv = $buf[2];
$this->assertEquals(3.5,$vv->real);
$this->assertEquals(4.5,$vv->imag);
if(!$this->notSupportComplex()) {
$buf = $this->factory->Buffer(3,NDArray::complex64);
$this->assertEquals(NDArray::complex64,$buf->dtype());
$this->assertEquals(3,count($buf));
$this->assertEquals(8,$buf->value_size());
$this->assertEquals(NDArray::complex64,$buf->dtype());
$buf[1] = [1.5,2.5];
$buf[2] = (object)['real'=>3.5,'imag'=>4.5];
$vv = $buf[1];
$this->assertEquals(1.5,$vv->real);
$this->assertEquals(2.5,$vv->imag);
$vv = $buf[2];
$this->assertEquals(3.5,$vv->real);
$this->assertEquals(4.5,$vv->imag);

$buf = $this->factory->Buffer(3,NDArray::complex128);
$this->assertEquals(NDArray::complex128,$buf->dtype());
$this->assertEquals(3,count($buf));
$this->assertEquals(16,$buf->value_size());
$this->assertEquals(NDArray::complex128,$buf->dtype());
$buf[1] = [1.5,2.5];
$buf[2] = (object)['real'=>3.5,'imag'=>4.5];
$vv = $buf[1];
$this->assertEquals(1.5,$vv->real);
$this->assertEquals(2.5,$vv->imag);
$vv = $buf[2];
$this->assertEquals(3.5,$vv->real);
$this->assertEquals(4.5,$vv->imag);
}
}

public function testOffsetExists()
Expand Down

0 comments on commit 2623eed

Please sign in to comment.