Skip to content

Instantly share code, notes, and snippets.

@krakjoe
Last active June 11, 2019 09:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krakjoe/8313601 to your computer and use it in GitHub Desktop.
Save krakjoe/8313601 to your computer and use it in GitHub Desktop.
SMP Fibonacci with pthreads
<?php
/**
* SMP Fibonacci
*
* Fibonacci sequencing is often used to show the virtues of such things as HHVM
*
* People think of it as a task that cannot be multi-threaded, this is flat out wrong.
*
* It can be multi-threaded, even in PHP, to great effect.
*
* Creating threads is not as "free" as in C, however, there comes a time (roughly the same in PHP as in C)
* that actually using multiple threads can show a measurable benefit to performance.
*
* Where that happens depends on the hardware in use, on my hardware, around the 30th fibonacci number can see
* gains from using just two threads as opposed to one.
*
* The 35th sequence:
*
* [joe@fiji pthreads]$ time php -dextension=pthreads.so ../php-src/fib.php 35 0
* 9227465
* real 0m8.042s
* user 0m8.032s
* sys 0m0.003s
* [joe@fiji pthreads]$ time php -dextension=pthreads.so ../php-src/fib.php 35 1
* 9227465
* real 0m5.168s
* user 0m8.258s
* sys 0m0.003s
*
* Okay, that's a 3 second saving, not bad, lots can be done in three seconds ...
*
* The 40th sequence:
*
* [joe@fiji pthreads]$ time php -dextension=pthreads.so ../php-src/fib.php 40 0
* 102334155
* real 1m33.009s (roughly equivalent to one year)
* user 1m32.893s
* sys 0m0.003s
* [joe@fiji pthreads]$ time php -dextension=pthreads.so ../php-src/fib.php 40 1
* 102334155
* real 0m56.689s (still a long time, but much less)
* user 1m31.623s
* sys 0m0.000s
*
* This time a ~37 second saving; the world can be conquered in 37 seconds.
*
* I was going to do a console program that took your input and stayed in memory, but I got bored ... you do it ...
*
* Someone pointed out that even a minute to calculate the 40th number slow ... and it is ...
* The point is that one minute is still much less than a minute and a half ...
*
* [joe@fiji php-src]$ time php -dextension=pthreads.so fib.php 30 0
* 832040
* real 0m0.738s
* user 0m0.734s
* sys 0m0.002s
* [joe@fiji php-src]$ time php -dextension=pthreads.so fib.php 30 1
* 832040
* real 0m0.474s
* user 0m0.748s
* sys 0m0.003s
*
* Finally, this is not meant to show anything other than it is possible to write an SMP Fibonacci calculator in PHP
*/
class FWorker extends Worker {
public function run(){}
}
class FTask extends Stackable {
public $result;
public function __construct($input) {
$this->input = $input;
$this->result = 0;
}
public function run() {
$this->synchronized(function($worker){
$this->result = fib(
$this->input - 1, false, $worker);
$this->notify();
}, $this->worker);
}
}
function fib($n, $thread, &$worker) {
if ($n < 2) {
return $n;
}
if ($thread && $n > 29) {
$l = new FTask($n);
$worker->stack($l);
$r = fib($n-2, false, $worker);
$l = $l->synchronized(function($task){
if (!$task->result) {
$task->wait();
}
return $task->result;
}, $l);
} else {
$l = fib($n-1, false, $worker);
$r = fib($n-2, false, $worker);
}
return $l + $r;
}
$worker = new FWorker();
$worker->start(PTHREADS_INHERIT_FUNCTIONS);
echo fib($argv[1], $argv[2] ? (boolean) $argv[2] : false, $worker);
$worker->shutdown();
exit;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment