Matrix is a PHP extension. It can do parallel computing based on CUDA.
Why should we use GPU to do cumputation ? Because it can run 1000+ times faster than CPU when solve a parallel computation.
What's more, neural network of AI are full of computation of matrix , so it can be helpful.
- CUDA-enabled GPU
- CUDA Toolkit
- PHP version >= 7.0
- Generate Makefile
cd matrix/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config-
Modify Makefile
sh bs_matrix.mk.sh -
make and install
make bs_matrix -
(optional) Copy the file _bs_matrix_ide_helper.php to your IDE directory
1. Matrix multiply
<?php
function randomArr( $height, $width ){
$arr = BS\matrix\Util::initArrayBySize($height);
for($i = 0 ; $i < $height; $i++){
$tempArr = BS\matrix\Util::initArrayBySize($width);
for ($j = 0 ; $j < $width; $j++){
$tempArr[] = rand(1,1000) + rand(1,1000) / 1000 ;
}
$arr[] = $tempArr;
}
return $arr;
}
$matrixA = randomArr( 640, 480 ); //A( 640, 480 )
$matrixB = randomArr( 480, 320 ); //B( 480, 320 )
$blas = new BS\matrix\BLAS();
$gpuCalculatedArr = $blas->multiply( $matrixA, $matrixB ); //A( 640, 480 ) x B( 480, 320 ) , 1000+ times faster than CPU computation
var_dump( $gpuCalculatedArr );//gpuCalculatedArr(640, 320)
?>2. Hadamard product
<?php
$arrA = [
[ 1, 2, 3 ],
[ 4, 5, 6 ],
];
$arrB = [
[ 2, 2, 2 ],
[ 2, 2, 2 ],
];
$gpuCalculatedArr = BS\matrix\Math::hadamardProduct( $arrA, $arrB );
var_dump( $gpuCalculatedArr );
?>3. Get GPU's name
<?php
$deviceCount = BS\matrix\Util::cudaGetDeviceCount();
printf( "CUDA device count: %d \n", $deviceCount );
$deviceId = $deviceCount - 1;
printf( "CUDA device id: %d, name: %s \n", $deviceId, Util::getDeviceNameById($deviceId) );
?>4. Create and init an array with certain capacity
<?php
$arr = BS\matrix\Util::initArrayBySize( $capacity ); //this can slightly improve performce when access, insert or update an array
?>Want More?
Please see _bs_matrix_ide_helper.php.
install composer first,then
composer install
sh unit_tests/runTest.sh Matrix is licensed under MIT.