Skip to content

BourneSuper/matrix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Matrix

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.

Requirement

Install

  1. Generate Makefile
cd matrix/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
  1. Modify Makefile
    sh bs_matrix.mk.sh

  2. make and install
    make bs_matrix

  3. (optional) Copy the file _bs_matrix_ide_helper.php to your IDE directory

Usage

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.

Help with Test

install composer first,then

composer install
sh unit_tests/runTest.sh 

LISENSE

Matrix is licensed under MIT.

About

Matrix is a PHP extension. It can do parallel computing base on CUDA.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published