Skip to content

Commit

Permalink
type definitions and rotm
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichiis committed Apr 25, 2024
1 parent 4f2a6ab commit 1e46479
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 14 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
name: tests

on: [push]
on:
push:
branches:
- main

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

strategy:
Expand All @@ -21,6 +24,12 @@ jobs:
php_version: ${{ matrix.php }}
php_extensions: ffi

- name: PHP Static Analysys
uses: php-actions/phpstan@v3
with:
php_version: ${{ matrix.php }}
path: src/

- name: PHPUnit Tests
uses: php-actions/phpunit@v3
with:
Expand Down
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,25 @@ Ubuntu standard OpenCL drivers include:
- pocl-opencl-icd


Next, download and set up clbast.
Next, Setup clblast.

Download and Extract Archive file and Pack to deb
Install clbast on Ubuntu 22.04 or Debian 12 or later.
```shell
$ sudo apt install libclblast1
```

If you use Ubuntu 20.04 or Debian 11,
download and Extract Archive file and Pack to deb
```shell
$ cd /some/app/directory
$ composer require rindow/rindow-clblast-ffi
$ cp vendor/rindow/rindow-clblast-ffi/clblast-packdeb.sh .
$ sh ./clblast-packdeb.sh
$ sudo apt install ./clblast_X.X.X-X+ubuntuXX.XX_amd64.deb
$ sudo apt install ./clblast_X.X.X_amd64.deb
```

And then, Please install rindow-clblast-ffi if you have not already done so.
```shell
$ cd /some/app/directory
$ composer require rindow/rindow-clblast-ffi
```
15 changes: 8 additions & 7 deletions clblast-packdeb.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/usr/bin/bash

CLBLASTVERSION=1.6.2

. /etc/os-release
#CLBLASTVERSION=1.5.2

FILENAME=CLBlast-${CLBLASTVERSION}-linux-x86_64
PLATFORM=ubuntu${VERSION_ID}
#FILENAME=CLBlast-${CLBLASTVERSION}-Linux-x64
TARGET=./pkgwork


wget https://github.com/CNugteren/CLBlast/releases/download/${CLBLASTVERSION}/${FILENAME}.zip
#wget https://github.com/CNugteren/CLBlast/releases/download/${CLBLASTVERSION}/${FILENAME}.tar.xz

unzip ${FILENAME}.zip
tar xvf ${FILENAME}.tar.gz
Expand All @@ -25,7 +25,7 @@ Package: clblast
Maintainer: CLBlast Developers <CNugteren@users.noreply.github.com>
Architecture: amd64
Depends: libc6 (>= 2.14), ocl-icd-libopencl1 | libopencl1, ocl-icd-libopencl1 (>= 1.0) | libopencl-1.1-1
Version: ${CLBLASTVERSION}-1+${PLATFORM}
Version: ${CLBLASTVERSION}
Homepage: https://github.com/CNugteren/CLBlast/
Description: The tuned OpenCL BLAS library
CLBlast is a modern, lightweight, performant and tunable OpenCL BLAS library
Expand All @@ -36,9 +36,10 @@ EOS
mv ${TARGET}/usr/lib/pkgconfig/clblast.pc ./clblast.pc.orig
cat ./clblast.pc.orig | sed -e s/^prefix=.*$/prefix=\\/usr/ > ${TARGET}/usr/lib/pkgconfig/clblast.pc
rm ./clblast.pc.orig
rm clblast_${CLBLASTVERSION}-1+${PLATFORM}_amd64.deb
rm clblast_${CLBLASTVERSION}_amd64.deb
fakeroot dpkg-deb --build pkgwork .
rm ${FILENAME}.zip
rm ${FILENAME}.tar.gz
rm -f ${FILENAME}.tar.xz
rm -f ${FILENAME}.zip
rm -f ${FILENAME}.tar.gz
rm -rf ${FILENAME}
rm -rf ${TARGET}
22 changes: 22 additions & 0 deletions phpstan-interop.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
parameters:
ignoreErrors:
-
message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:dtype\\(\\)#"
-
message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:addr\\(\\)#"


# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:dump\\(\\)#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:value_size\\(\\)#"
# -
# message: "#function count expects array|Countable, Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray given#"
# -
# message: "#^Argument of an invalid type Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray supplied for foreach, only iterables are supported#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\DeviceBuffer\\:\\:bytes\\(\\)#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\DeviceBuffer\\:\\:copy\\(\\)#"
# -
# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\DeviceBuffer\\:\\:read\\(\\)#"
10 changes: 10 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
includes:
- phpstan-interop.neon

parameters:
level: 6
paths:
- src
ignoreErrors:
-
message: "#^Call to an undefined method FFI::#"
140 changes: 140 additions & 0 deletions src/Blas.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ public function copy(
try {
$Y->copy($queue,$X,$bytes,$src_offset,$dst_offset,$event);
} catch(RuntimeException $e) {
$status = $e->getCode();
throw new RuntimeException("CLBlast?copy error=$status", $status, $e);
}
// skip to move raw event
Expand Down Expand Up @@ -881,6 +882,145 @@ public function rot(
}
}

public function rotmg(
DeviceBuffer $D1, int $offsetD1,
DeviceBuffer $D2, int $offsetD2,
DeviceBuffer $B1, int $offsetB1,
DeviceBuffer $B2, int $offsetB2,
DeviceBuffer $P, int $offsetP,
CommandQueue $queue,// Rindow\OpenCL\CommandQueue
EventList $event=null, // Rindow\OpenCL\EventList
) : void
{
$ffi= $this->ffi;

//// Check Buffer A
//$this->assert_vector_buffer_spec("A", $A, 1, $offsetA, 1);
//// Check Buffer B
//$this->assert_vector_buffer_spec("B", $B, 1, $offsetB, 1);
//// Check Buffer C
//$this->assert_vector_buffer_spec("C", $C, 1, $offsetC, 1);
//// Check Buffer S
//$this->assert_vector_buffer_spec("S", $S, 1, $offsetS, 1);

// Check Buffer A and B and C and S
$dtype = $D1->dtype();
if($dtype!=$D2->dtype()||$dtype!=$B1->dtype()
||$dtype!=$B2->dtype()||$dtype!=$P->dtype()) {
throw new InvalidArgumentException("Unmatch data type for A,B,C and S");
}
$bufferD1_p = $ffi->cast("cl_mem",$D1->_getId());
$bufferD2_p = $ffi->cast("cl_mem",$D2->_getId());
$bufferB1_p = $ffi->cast("cl_mem",$B1->_getId());
$bufferB2_p = $ffi->cast("cl_mem",$B2->_getId());
$bufferP_p = $ffi->cast("cl_mem",$P->_getId());
$queue_p = $ffi->cast("cl_command_queue*",FFI::addr($queue->_getId()));
$event_p = null;
if($event) {
$event_obj = $event->_ffi()->new("cl_event[1]");
$event_p = $ffi->cast("cl_event[1]",$event_obj);
}

switch($dtype) {
case NDArray::float32:{
$status = $ffi->CLBlastSrotmg(
$bufferD1_p,$offsetD1,
$bufferD2_p,$offsetD2,
$bufferB1_p,$offsetB1,
$bufferB2_p,$offsetB2,
$bufferP_p,$offsetP,
$queue_p,$event_p
);
break;
}
case NDArray::float64:{
$status = $ffi->CLBlastDrotmg(
$bufferD1_p,$offsetD1,
$bufferD2_p,$offsetD2,
$bufferB1_p,$offsetB1,
$bufferB2_p,$offsetB2,
$bufferP_p,$offsetP,
$queue_p,$event_p
);
break;
}
default: {
throw new InvalidArgumentException('Unsuppored data type');
}
}
if($status!=self::CLBlastSuccess) {
if($status==self::CLBlastNotImplemented) {
throw new RuntimeException("CLBlast?rotmg error=$status: Not Implemented", $status);
}
throw new RuntimeException("CLBlast?rotmg error=$status", $status);
}
if($event) {
$event->_move($event_obj);
}
}

public function rotm(
int $n,
DeviceBuffer $X, int $offsetX, int $incX,
DeviceBuffer $Y, int $offsetY, int $incY,
DeviceBuffer $P, int $offsetP,
CommandQueue $queue,// Rindow\OpenCL\CommandQueue
EventList $event=null, // Rindow\OpenCL\EventList
) : void
{
$ffi= $this->ffi;

// Check Buffer A and B and C and S
$dtype = $X->dtype();
if($dtype!=$Y->dtype()) {
throw new InvalidArgumentException("Unmatch data type for X and Y");
}
$bufferX_p = $ffi->cast("cl_mem",$X->_getId());
$bufferY_p = $ffi->cast("cl_mem",$Y->_getId());
$bufferP_p = $ffi->cast("cl_mem",$P->_getId());
$queue_p = $ffi->cast("cl_command_queue*",FFI::addr($queue->_getId()));
$event_p = null;
if($event) {
$event_obj = $event->_ffi()->new("cl_event[1]");
$event_p = $ffi->cast("cl_event[1]",$event_obj);
}

switch($dtype) {
case NDArray::float32:{
$status = $ffi->CLBlastSrotm(
$n,
$bufferX_p,$offsetX,$incX,
$bufferY_p,$offsetY,$incY,
$bufferP_p,$offsetP,
$queue_p,$event_p
);
break;
}
case NDArray::float64:{
$status = $ffi->CLBlastDrotm(
$n,
$bufferX_p,$offsetX,$incX,
$bufferY_p,$offsetY,$incY,
$bufferP_p,$offsetP,
$queue_p,$event_p
);
break;
}
default: {
throw new InvalidArgumentException('Unsuppored data type');
}
}
if($status!=self::CLBlastSuccess) {
if($status==self::CLBlastNotImplemented) {
throw new RuntimeException("CLBlast?rotm error=$status: Not Implemented", $status);
}
throw new RuntimeException("CLBlast?rotm error=$status", $status);
}
if($event) {
$event->_move($event_obj);
}
}

public function gemv(
int $order,
int $trans,
Expand Down
7 changes: 6 additions & 1 deletion src/CLBlastFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ class CLBlastFactory
{
private static ?FFI $ffi = null;
public static ?FFI $ffipf = null;
/** @var array<string> $libs_win */
protected array $libs_win = ['clblast.dll'];
protected array $libs_linux = ['libclblast.so'];
/** @var array<string> $libs_linux */
protected array $libs_linux = ['libclblast.so.1'];

/**
* @param array<string> $libFiles
*/
public function __construct(
string $headerFile=null,
array $libFiles=null,
Expand Down
3 changes: 2 additions & 1 deletion src/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use InvalidArgumentException;
use OutOfRangeException;
use LogicException;
use RuntimeException;
use FFI;
use Rindow\OpenCL\FFI\Buffer as DeviceBuffer;
use Rindow\OpenCL\FFI\CommandQueue;
Expand Down Expand Up @@ -455,7 +456,7 @@ public function axpyBatched(
DeviceBuffer $X, HostBuffer $offsetsX, int $offsetX, int $incX,
DeviceBuffer $Y, HostBuffer $offsetsY, int $offsetY, int $incY,
int $batch_count,
CommandQueue $queue, EventList $event
CommandQueue $queue, EventList $event=null
) : void
{
$ffi = $this->ffi;
Expand Down
8 changes: 8 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
namespace Rindow\CLBlast\FFI;

use Interop\Polite\Math\Matrix\NDArray;
use InvalidArgumentException;

class cl_float2_t {
/** @var array<float> $s */
public array $s;
}

trait Utils
{
Expand All @@ -10,12 +16,14 @@ protected function toComplex(object $from,int $dtype) : object
$ffi = $this->ffi;
switch($dtype) {
case NDArray::complex64: {
/** @var cl_float2_t $to */
$to = $ffi->new('cl_float2');
$to->s[0] = $from->real;
$to->s[1] = $from->imag;
break;
}
case NDArray::complex128: {
/** @var cl_float2_t $to */
$to = $ffi->new('cl_double2');
$to->s[0] = $from->real;
$to->s[1] = $from->imag;
Expand Down
Loading

0 comments on commit 1e46479

Please sign in to comment.