Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Macros
server.h
1 /************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21 
22 #ifndef INCLUDE_TRAINER_SERVER_H_
23 #define INCLUDE_TRAINER_SERVER_H_
24 #include <memory>
25 #include <unordered_map>
26 #include <utils/param.h>
27 #include <utils/updater.h>
28 #include "proto/job.pb.h"
29 #include "communication/socket.h"
30 
31 namespace singa {
32 /* Repsond to worker's get/put/udpate request, and periodically syncing with
33  * other servers.
34  *
35  * Normally, the Server creates a response message for each request which
36  * will be sent back to the one who issued the request. However, if the request
37  * are not processed successfully, the original message will be returned. The
38  * sever does not know the returned message (response or the original message),
39  * it just sends it to the router. The router will decide to re-send the
40  * request to the server or send it to the worker.
41  */
42 class Server{
43  public:
44  Server(int thread_id, int group_id, int server_id);
45  virtual ~Server();
46  void Setup(const UpdaterProto& proto,
47  const std::vector<int>& slice2group,
48  const std::vector<int>& slice2server);
49  void Run();
50  const int grp_id() const {
51  return grp_id_;
52  }
53  const int id() const {
54  return id_;
55  }
56 
57  protected:
58 
65  virtual Msg* HandleGet(Msg** msg);
66 
88  const std::vector<Msg*> HandleUpdate(Msg **msg);
89 
96  virtual Msg* HandlePut(Msg **msg);
97 
109  virtual Msg* HandleSyncRequest(Msg** msg);
110 
121  void HandleSyncResponse(Msg** msg);
122 
123  protected:
124  int thread_id_,grp_id_, id_;
127  std::unordered_map<int, ParamEntry*> shard_;
128  std::vector<int> slice2group_, slice2server_;
130  std::vector<int> nUpdates_;
132  std::vector<int> nPendingSync_;
133  std::vector<Blob<float>> last_sync_;
134  std::unordered_map<int, std::vector<Msg*>> buffer_requests_;
135 };
136 } /* Server */
137 #endif //INCLUDE_TRAINER_SERVER_H_
virtual Msg * HandlePut(Msg **msg)
Process PUT request.
virtual Msg * HandleGet(Msg **msg)
Process GET request.
Updater * updater_
map from slice ID to slice and deleted in the destructor
Definition: server.h:125
std::vector< int > slice2server_
num of updates from last sync with master server group for a param/slice
Definition: server.h:128
Msg used to transfer Param info (gradient or value), feature blob, etc between workers, stubs and servers.
Definition: msg.h:91
virtual Msg * HandleSyncRequest(Msg **msg)
Handle sync request from other server groups.
Updater for Param.
Definition: updater.h:88
std::vector< int > nUpdates_
num of sync requests that have not been responded
Definition: server.h:130
Definition: server.h:42
const std::vector< Msg * > HandleUpdate(Msg **msg)
Process Update request.
void HandleSyncResponse(Msg **msg)
Handle sync response.