|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Rubix\ML\Backends; |
| 4 | + |
| 5 | +use Rubix\ML\Backends\Tasks\Task; |
| 6 | +use Rubix\ML\Specifications\ExtensionIsLoaded; |
| 7 | +use Rubix\ML\Specifications\SwooleExtensionIsLoaded; |
| 8 | +use RuntimeException; |
| 9 | +use Swoole\Atomic; |
| 10 | +use Swoole\Process; |
| 11 | + |
| 12 | +use function Swoole\Coroutine\run; |
| 13 | + |
| 14 | +/** |
| 15 | + * Swoole |
| 16 | + * |
| 17 | + * Works both with Swoole and OpenSwoole. |
| 18 | + * |
| 19 | + * @category Machine Learning |
| 20 | + * @package Rubix/ML |
| 21 | + */ |
| 22 | +class Swoole implements Backend |
| 23 | +{ |
| 24 | + /** |
| 25 | + * The queue of tasks to be processed in parallel. |
| 26 | + */ |
| 27 | + protected array $queue = []; |
| 28 | + |
| 29 | + private int $cpus; |
| 30 | + |
| 31 | + private int $hasIgbinary; |
| 32 | + |
| 33 | + public function __construct() |
| 34 | + { |
| 35 | + SwooleExtensionIsLoaded::create()->check(); |
| 36 | + |
| 37 | + $this->cpus = swoole_cpu_num(); |
| 38 | + $this->hasIgbinary = ExtensionIsLoaded::with('igbinary')->passes(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Queue up a deferred task for backend processing. |
| 43 | + * |
| 44 | + * @internal |
| 45 | + * |
| 46 | + * @param Task $task |
| 47 | + * @param callable(mixed,mixed):void $after |
| 48 | + * @param mixed $context |
| 49 | + */ |
| 50 | + public function enqueue(Task $task, ?callable $after = null, $context = null) : void |
| 51 | + { |
| 52 | + $this->queue[] = function () use ($task, $after, $context) { |
| 53 | + $result = $task(); |
| 54 | + |
| 55 | + if ($after) { |
| 56 | + $after($result, $context); |
| 57 | + } |
| 58 | + |
| 59 | + return $result; |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Process the queue and return the results. |
| 65 | + * |
| 66 | + * @internal |
| 67 | + * |
| 68 | + * @return mixed[] |
| 69 | + */ |
| 70 | + public function process() : array |
| 71 | + { |
| 72 | + $results = []; |
| 73 | + |
| 74 | + $maxMessageLength = new Atomic(0); |
| 75 | + $workerProcesses = []; |
| 76 | + |
| 77 | + $currentCpu = 0; |
| 78 | + |
| 79 | + foreach ($this->queue as $index => $queueItem) { |
| 80 | + $workerProcess = new Process( |
| 81 | + function (Process $worker) use ($maxMessageLength, $queueItem) { |
| 82 | + $serialized = $this->serialize($queueItem()); |
| 83 | + |
| 84 | + $serializedLength = strlen($serialized); |
| 85 | + $currentMaxSerializedLength = $maxMessageLength->get(); |
| 86 | + |
| 87 | + if ($serializedLength > $currentMaxSerializedLength) { |
| 88 | + $maxMessageLength->set($serializedLength); |
| 89 | + } |
| 90 | + |
| 91 | + $worker->exportSocket()->send($serialized); |
| 92 | + }, |
| 93 | + // redirect_stdin_and_stdout |
| 94 | + false, |
| 95 | + // pipe_type |
| 96 | + SOCK_DGRAM, |
| 97 | + // enable_coroutine |
| 98 | + true, |
| 99 | + ); |
| 100 | + |
| 101 | + $workerProcess->setAffinity([$currentCpu]); |
| 102 | + $workerProcess->setBlocking(false); |
| 103 | + $workerProcess->start(); |
| 104 | + |
| 105 | + $workerProcesses[$index] = $workerProcess; |
| 106 | + |
| 107 | + $currentCpu = ($currentCpu + 1) % $this->cpus; |
| 108 | + } |
| 109 | + |
| 110 | + run(function () use ($maxMessageLength, &$results, $workerProcesses) { |
| 111 | + foreach ($workerProcesses as $index => $workerProcess) { |
| 112 | + $status = $workerProcess->wait(); |
| 113 | + |
| 114 | + if (0 !== $status['code']) { |
| 115 | + throw new RuntimeException('Worker process exited with an error'); |
| 116 | + } |
| 117 | + |
| 118 | + $socket = $workerProcess->exportSocket(); |
| 119 | + |
| 120 | + if ($socket->isClosed()) { |
| 121 | + throw new RuntimeException('Coroutine socket is closed'); |
| 122 | + } |
| 123 | + |
| 124 | + $maxMessageLengthValue = $maxMessageLength->get(); |
| 125 | + |
| 126 | + $receivedData = $socket->recv($maxMessageLengthValue); |
| 127 | + $unserialized = $this->unserialize($receivedData); |
| 128 | + |
| 129 | + $results[] = $unserialized; |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + return $results; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Flush the queue |
| 138 | + */ |
| 139 | + public function flush() : void |
| 140 | + { |
| 141 | + $this->queue = []; |
| 142 | + } |
| 143 | + |
| 144 | + private function serialize(mixed $data) : string |
| 145 | + { |
| 146 | + if ($this->hasIgbinary) { |
| 147 | + return igbinary_serialize($data); |
| 148 | + } |
| 149 | + |
| 150 | + return serialize($data); |
| 151 | + } |
| 152 | + |
| 153 | + private function unserialize(string $serialized) : mixed |
| 154 | + { |
| 155 | + if ($this->hasIgbinary) { |
| 156 | + return igbinary_unserialize($serialized); |
| 157 | + } |
| 158 | + |
| 159 | + return unserialize($serialized); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Return the string representation of the object. |
| 164 | + * |
| 165 | + * @internal |
| 166 | + * |
| 167 | + * @return string |
| 168 | + */ |
| 169 | + public function __toString() : string |
| 170 | + { |
| 171 | + return 'Swoole'; |
| 172 | + } |
| 173 | +} |
0 commit comments