Skip to content
This repository was archived by the owner on Nov 9, 2020. It is now read-only.

Commit e6bd75a

Browse files
committed
Implement concept skeleton of the module
1 parent 459481a commit e6bd75a

16 files changed

+600
-2
lines changed

README.md

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,86 @@
1-
# gpu
2-
:m: Handle communication with the BIIGLE GPU server
1+
# BIIGLE GPU Module
2+
3+
Handle communication between BIIGLE and the BIIGLE GPU server.
4+
5+
## Installation
6+
7+
This module needs to be installed both for the BIIGLE instance and the GPU server.
8+
9+
```
10+
composer config repositories.gpu vcs https://github.com/biigle/gpu
11+
composer require biigle/gpu
12+
```
13+
14+
### BIIGLE
15+
16+
The service provider and `Gpu` facade are auto discovered by BIIGLE.
17+
18+
### GPU Server
19+
20+
Add `$app->register(Biigle\Modules\Gpu\GpuServerServiceProvider::class);` to `bootstrap/app.php`.
21+
22+
## Configuration
23+
24+
todo
25+
26+
## Usage
27+
28+
To submit a job to the GPU server, it must extend `Biigle\Modules\Gpu\Jobs\GpuJob`. A job instance can be submitted with `Gpu::push($job)` and will be executed on the GPU server. To return the result data back to the BIIGLE instance, the job must call `$this->submitResponse($response)` at the end of its `handle` method. The response object must extend `Biigle\Jobs\Job` and will be executed on the BIIGLE instance where it can handle storage of the result data.
29+
30+
Be aware that the GPU server has no database access. All required information needs to be stored in the `GpuJob`.
31+
32+
**Example:**
33+
34+
```php
35+
use Gpu;
36+
use Biigle\Jobs\Job;
37+
use Biigle\Modules\Gpu\Jobs\GpuJob;
38+
39+
/*
40+
* Instances of this class are submitted to the GPU server with Gpu::push().
41+
*/
42+
class MyGpuJob extends GpuJob
43+
{
44+
/*
45+
* Instances of this class are created in BIIGLE.
46+
*/
47+
public function __construct($data)
48+
{
49+
$this->data = $data;
50+
}
51+
52+
/*
53+
* But the handle() method is executed on the GPU server.
54+
*/
55+
public function handle()
56+
{
57+
$result = $this->computeGpuStuff($data);
58+
$this->submitResponse(new MyGpuResponse($result));
59+
}
60+
}
61+
62+
/*
63+
* Instances of this class are used as argument for GpuJob::submitResponse.
64+
*/
65+
class MyGpuResponse extends Job
66+
{
67+
/*
68+
* Instances of this class are created on the GPU server.
69+
*/
70+
public function __construct($result)
71+
{
72+
$this->result = $result;
73+
}
74+
75+
/*
76+
* But the handle() method is executed in BIIGLE.
77+
*/
78+
public function handle()
79+
{
80+
$this->storeResultInDatabase($this->result);
81+
}
82+
}
83+
84+
// In the BIIGLE instance:
85+
Gpu::push(new MyGpuJob('some data'));
86+
```

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "biigle/gpu",
3+
"description": "Handle communication between BIIGLE and the BIIGLE GPU server.",
4+
"type": "library",
5+
"authors": [
6+
{
7+
"name": "Martin Zurowietz",
8+
"email": "martin@cebitec.uni-bielefeld.de"
9+
}
10+
],
11+
"require": {
12+
"guzzlehttp/guzzle": "~6.0"
13+
},
14+
"autoload": {
15+
"psr-4": {
16+
"Biigle\\Modules\\Gpu\\": "src"
17+
}
18+
},
19+
"extra": {
20+
"laravel": {
21+
"providers": [
22+
"Biigle\\Modules\\Gpu\\GpuServiceProvider"
23+
],
24+
"aliases": {
25+
"Gpu": "Biigle\\Modules\\Gpu\\Facades\\Gpu"
26+
}
27+
}
28+
}
29+
}

src/Adapters/LocalAdapter.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Biigle\Modules\Gpu\Adapters;
4+
5+
use Biigle\Jobs\Job;
6+
use Biigle\Modules\Gpu\Jobs\GpuJob;
7+
use Biigle\Modules\Gpu\Contracts\Adapter;
8+
9+
// Push the GpuJob on the "gpu" queue. In the "local" setup the BIIGLE instance and the
10+
// GPU server share a common Redis cache. This adapter pushes new jobs from BIIGLE to
11+
// the "gpu" queue and the GPU server picks them up from there. The response is pushed
12+
// from the GPU server to the regular queue and BIIGLE picks it up from there.
13+
//
14+
// In the local setup, only the "worker" service of the GPU server is running.
15+
16+
class LocalAdapter implements Adapter
17+
{
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
public function push(GpuJob $job)
22+
{
23+
//
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function pushResponse(Job $response)
30+
{
31+
//
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function setConfig(array $config)
38+
{
39+
//
40+
}
41+
}

src/Adapters/OpenStackAdapter.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
// Put this adapter to its own package so not everybody is forced to install all the
4+
// OpenStack PHP packages!
5+
6+
// This is a wrapper for the Remote adapter. Before the job is pushed, this adapter
7+
// checks if an OpenStack compute instance is running with the GPU server. It tracks the
8+
// IPs of the currently active GPU server(s) using the cache. If none is running, it
9+
// boots one up using a configured image that automatically starts the GPU server. It
10+
// polls the compute instance until the GPU server responds (with an "idle-since"
11+
// endpoint?). Then it submits the job using the Remote adapter.
12+
//
13+
// This adapter also regularly polls any active GPU servers. If one is idle for a
14+
// configured time span, the compute instance is destroyed.
15+
//
16+
// All this implies that the adapter can manage one or more compute instances. The
17+
// number can be configured. If more than one instance is available, jobs are submitted
18+
// round-robin.

src/Adapters/RemoteAdapter.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Biigle\Modules\Gpu\Adapters;
4+
5+
use Biigle\Jobs\Job;
6+
use Biigle\Modules\Gpu\Jobs\GpuJob;
7+
use Biigle\Modules\Gpu\Contracts\Adapter;
8+
9+
// Gpu jobs are serialized and transferred via HTTP to the configured URL of the GPU
10+
// server. The GPU server unserializes the job and processes it. The response is
11+
// serialized again and transferred back using the responseUrl of the job object.
12+
// In BIIGLE, the response is unserialized and pushed to the regular queue.
13+
//
14+
// This adapter assembles the response URL. It contains a long, unique token for each
15+
// job. The token is stored in the database until the job response is returned.
16+
17+
class RemoteAdapter implements Adapter
18+
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function push(GpuJob $job)
23+
{
24+
//
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function pushResponse(Job $response)
31+
{
32+
//
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function setConfig(array $config)
39+
{
40+
//
41+
}
42+
}

src/Contracts/Adapter.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Biigle\Modules\Gpu\Contracts;
4+
5+
use Biigle\Jobs\Job;
6+
use Biigle\Modules\Gpu\Jobs\GpuJob;
7+
8+
interface Adapter
9+
{
10+
/**
11+
* Submit a job to the GPU server.
12+
*
13+
* @param GpuJob $job
14+
*/
15+
public function push(GpuJob $job);
16+
17+
/**
18+
* Return a job response back to BIIGLE.
19+
*
20+
* @param Job $response
21+
*/
22+
public function pushResponse(Job $response);
23+
24+
/**
25+
* Set the configuration of the adapter.
26+
*
27+
* @param array $config
28+
*/
29+
public function setConfig(array $config);
30+
}

src/Contracts/GpuJob.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Biigle\Modules\Gpu\Contracts;
4+
5+
use Biigle\Jobs\Job;
6+
7+
interface GpuJob
8+
{
9+
/**
10+
* Set the URL of the API endpoint to post the response of this job to.
11+
*
12+
* @param string $url
13+
*/
14+
public function setResponseUrl($url);
15+
16+
/**
17+
* Get the URL of the API endpoint to post the response of this job to.
18+
*
19+
* @return string
20+
*/
21+
public function getResponseUrl();
22+
23+
/**
24+
* Submit the response with the results of the processed job.
25+
*
26+
* @param Job $response
27+
*/
28+
public function submitResponse(Job $response);
29+
}

src/Facades/Gpu.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Biigle\Modules\Gpu\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class Gpu extends Facade
8+
{
9+
/**
10+
* Get the registered name of the component.
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor()
15+
{
16+
return 'gpu';
17+
}
18+
}

src/GpuJobHandler.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Biigle\Modules\Gpu;
4+
5+
use Biigle\Jobs\Job;
6+
use Biigle\Modules\Gpu\Jobs\GpuJob;
7+
use Biigle\Modules\Gpu\Contracts\Adapter;
8+
9+
class GpuJobHandler
10+
{
11+
/**
12+
* The adapter to use for submitting the jobs.
13+
*
14+
* @var Adapter
15+
*/
16+
protected $adapter;
17+
18+
/**
19+
* Create a new instance.
20+
*
21+
* @param array $config
22+
*/
23+
public function __construct(array $config)
24+
{
25+
$this->adapter = new $config['adapter'];
26+
$this->adapter->setConfig($config);
27+
}
28+
29+
/**
30+
* Submit a job to the GPU server.
31+
*
32+
* @param GpuJob $job
33+
*/
34+
public function push(GpuJob $job)
35+
{
36+
$this->adapter->push($job);
37+
}
38+
39+
/**
40+
* Return a job response back to BIIGLE.
41+
*
42+
* @param Job $response
43+
*/
44+
public function pushResponse(Job $response)
45+
{
46+
$this->adapter->pushResponse($job);
47+
}
48+
}

src/GpuServerServiceProvider.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Biigle\Modules\Gpu;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class GpuServerServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap the application events.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
$this->publishes([
17+
__DIR__.'/config/gpu.php' => base_path('config/gpu.php'),
18+
], 'config');
19+
}
20+
21+
/**
22+
* Register the service provider.
23+
*
24+
* @return void
25+
*/
26+
public function register()
27+
{
28+
$this->mergeConfigFrom(__DIR__.'/config/gpu.php', 'gpu');
29+
}
30+
}

0 commit comments

Comments
 (0)