| 1 | # Licensed to the Apache Software Foundation (ASF) under one | ||
| 2 | # or more contributor license agreements. See the NOTICE file | ||
| 3 | # distributed with this work for additional information | ||
| 4 | # regarding copyright ownership. The ASF licenses this file | ||
| 5 | # to you under the Apache License, Version 2.0 (the | ||
| 6 | # "License"); you may not use this file except in compliance | ||
| 7 | # with the License. You may obtain a copy of the License at | ||
| 8 | # | ||
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | # | ||
| 11 | # Unless required by applicable law or agreed to in writing, | ||
| 12 | # software distributed under the License is distributed on an | ||
| 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| 14 | # KIND, either express or implied. See the License for the | ||
| 15 | # specific language governing permissions and limitations | ||
| 16 | # under the License. | ||
| 17 | |||
| 18 | package AI::MXNet::KVStoreServer; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use AI::MXNet::Base; | ||
| 22 | use AI::MXNet::KVStore; | ||
| 23 | use Storable; | ||
| 24 | use MIME::Base64; | ||
| 25 | use Mouse; | ||
| 26 | use AI::MXNet::Function::Parameters; | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | AI::MXNet::KVStoreServer - The key-value store server. | ||
| 31 | =cut | ||
| 32 | |||
| 33 | =head2 new | ||
| 34 | |||
| 35 | Initialize a new KVStoreServer. | ||
| 36 | |||
| 37 | Parameters | ||
| 38 | ---------- | ||
| 39 | kvstore : AI::MXNet::KVStore | ||
| 40 | =cut | ||
| 41 | |||
| 42 | has 'kvstore' => (is => 'ro', isa => 'AI::MXNet::KVStore', required => 1); | ||
| 43 | has 'handle' => (is => 'ro', isa => 'KVStoreHandle', default => sub { shift->kvstore->handle }, lazy => 1); | ||
| 44 | has 'init_logging' => (is => 'rw', isa => 'Int', default => 0); | ||
| 45 | |||
| 46 | |||
| 47 | # return the server controller | ||
| 48 | method _controller() | ||
| 49 | { | ||
| 50 | return sub { | ||
| 51 | my ($cmd_id, $cmd_body) = @_; | ||
| 52 | if (not $self->init_logging) | ||
| 53 | { | ||
| 54 | ## TODO write logging | ||
| 55 | $self->init_logging(1); | ||
| 56 | } | ||
| 57 | if($cmd_id == 0) | ||
| 58 | { | ||
| 59 | my $optimizer = Storable::thaw(MIME::Base64::decode_base64($cmd_body)); | ||
| 60 | $self->kvstore->set_optimizer($optimizer); | ||
| 61 | } | ||
| 62 | else | ||
| 63 | { | ||
| 64 | my $rank = $self->kvstore->rank; | ||
| 65 | print("server $rank, unknown command ($cmd_id, $cmd_body)\n"); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | =head2 run | ||
| 71 | |||
| 72 | run the server, whose behavior is like | ||
| 73 | >>> while receive(x): | ||
| 74 | ... if is_command x: controller(x) | ||
| 75 | ... else if is_key_value x: updater(x) | ||
| 76 | =cut | ||
| 77 | |||
| 78 | method run() | ||
| 79 | { | ||
| 80 | check_call(AI::MXNetCAPI::KVStoreRunServer($self->handle, $self->_controller)); | ||
| 81 | } | ||
| 82 | |||
| 83 | # Start server/scheduler | ||
| 84 | func _init_kvstore_server_module() | ||
| 85 | { | ||
| 86 | my $is_worker = check_call(AI::MXNetCAPI::KVStoreIsWorkerNode()); | ||
| 87 | if($is_worker == 0) | ||
| 88 | { | ||
| 89 | my $kvstore = AI::MXNet::KVStore->create('dist'); | ||
| 90 | my $server = __PACKAGE__->new(kvstore => $kvstore); | ||
| 91 | $server->run(); | ||
| 92 | exit(0); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | _init_kvstore_server_module(); | ||
| 97 | |||
| 98 | 1; |