Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Macros
msg.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 SINGA_COMMUNICATION_MSG_H_
23 #define SINGA_COMMUNICATION_MSG_H_
24 
25 // TODO(wangwei): make it a compiler argument
26 #define USE_ZMQ
27 
28 #include <utility>
29 #ifdef USE_ZMQ
30 #include <czmq.h>
31 #endif
32 
33 namespace singa {
40 inline int Addr(int grp, int id_or_proc, int type) {
41  return (grp << 16) | (id_or_proc << 8) | type;
42 }
43 
49 inline int AddrGrp(int addr) {
50  return addr >> 16;
51 }
52 
58 inline int AddrID(int addr) {
59  static const int mask = (1 << 8) - 1;
60  return (addr >> 8) & mask;
61 }
62 
68 inline int AddrProc(int addr) {
69  return AddrID(addr);
70 }
71 
76 inline int AddrType(int addr) {
77  static const int mask = (1 << 8) -1;
78  return addr & mask;
79 }
80 
91 class Msg {
92  public:
93  ~Msg();
94  Msg();
98  Msg(int src, int dst);
102  Msg(const Msg& msg);
106  void SwapAddr();
110  void AddFrame(const void* addr, int nBytes);
114  int FrameSize();
118  void* FrameData();
122  char* FrameStr();
126  void FirstFrame();
130  void LastFrame();
135  bool NextFrame();
152  int AddFormatFrame(const char *format, ...);
169  int ParseFormatFrame(const char* format, ...);
170 
171 #ifdef USE_ZMQ
172  void ParseFromZmsg(zmsg_t* msg);
173  zmsg_t* DumpToZmsg();
174 #endif
175 
179  int size() const;
184  inline void set_src(int addr) { src_ = addr; }
188  inline int src() const { return src_; }
193  inline void set_dst(int addr) { dst_ = addr; }
197  inline int dst() const { return dst_; }
201  inline void set_type(int type) { type_ = type; }
205  inline int type() const { return type_; }
212  inline void set_trgt(int val, int version) {
213  trgt_val_ = val;
214  trgt_version_ = version;
215  }
216  inline int trgt_val() const { return trgt_val_; }
217  inline int trgt_version() const { return trgt_version_; }
218 
219  protected:
220  int src_ = 0;
221  int dst_ = 0;
222  int type_ = 0;
223  int trgt_val_ = 0;
224  int trgt_version_ = 0;
225 #ifdef USE_ZMQ
226  zmsg_t* msg_ = nullptr;
227  zframe_t *frame_ = nullptr;
228 #endif
229 };
230 
231 inline void DeleteMsg(Msg** msg) {
232  delete *msg;
233  *msg = nullptr;
234 }
235 
236 } // namespace singa
237 
238 #endif // SINGA_COMMUNICATION_MSG_H_
void * FrameData()
int AddrType(int addr)
Parse msg type from addr.
Definition: msg.h:76
void set_type(int type)
Set msg type, e.g., kPut, kGet, kUpdate, kRequest.
Definition: msg.h:201
int FrameSize()
void set_dst(int addr)
Set destination addr.
Definition: msg.h:193
void set_src(int addr)
Set source addr.
Definition: msg.h:184
int size() const
void LastFrame()
Move the cursor to the last frame.
bool NextFrame()
Move the cursor to the next frame.
int type() const
Definition: msg.h:205
Msg used to transfer Param info (gradient or value), feature blob, etc between workers, stubs and servers.
Definition: msg.h:91
void FirstFrame()
Move the cursor to the first frame.
int dst() const
Definition: msg.h:197
int AddFormatFrame(const char *format,...)
Add a 'format' frame to the msg (like CZMQ's zsock_send).
int AddrID(int addr)
Parse worker/server id from addr.
Definition: msg.h:58
int AddrProc(int addr)
Parse worker/server procs from addr.
Definition: msg.h:68
int src() const
Definition: msg.h:188
int ParseFormatFrame(const char *format,...)
Parse the current frame added using AddFormatFrame(const char*, ...).
int Addr(int grp, int id_or_proc, int type)
Wrapper to generate message address.
Definition: msg.h:40
void set_trgt(int val, int version)
Set msg target.
Definition: msg.h:212
void AddFrame(const void *addr, int nBytes)
Add a frame (a chunck of bytes) into the message.
char * FrameStr()
void SwapAddr()
Swap the src/dst addr.
int AddrGrp(int addr)
Parse group id from addr.
Definition: msg.h:49