dc_comm_base.hpp

00001 /*
00002 This file is part of GraphLab.
00003 
00004 GraphLab is free software: you can redistribute it and/or modify
00005 it under the terms of the GNU Lesser General Public License as 
00006 published by the Free Software Foundation, either version 3 of 
00007 the License, or (at your option) any later version.
00008 
00009 GraphLab is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU Lesser General Public License for more details.
00013 
00014 You should have received a copy of the GNU Lesser General Public 
00015 License along with GraphLab.  If not, see <http://www.gnu.org/licenses/>.
00016 */
00017 
00018 #ifndef DC_COMM_BASE_HPP
00019 #define DC_COMM_BASE_HPP
00020 #include <vector>
00021 #include <string>
00022 #include <map>
00023 #include <graphlab/rpc/dc_types.hpp>
00024 #include <graphlab/rpc/dc_internal_types.hpp>
00025 #include <graphlab/rpc/dc_receive.hpp>
00026 namespace graphlab {
00027 namespace dc_impl {  
00028 
00029   
00030 /**
00031 The base class of all comms implementations
00032 */
00033 class dc_comm_base {
00034  public:
00035    
00036   dc_comm_base();
00037   
00038   virtual size_t capabilities() const  = 0;
00039   /**
00040    Parses initialization parameters. Most of these parameters are
00041    user provided, or provided on a higher level initialization system.
00042    It is entirely up to the comm implementation how these parameters to be treated.
00043    The descriptions here are largely prescriptive.
00044    All machines are called with the same initialization parameters (of course with the 
00045    exception of curmachineid)
00046 
00047    The expected behavior is that 
00048    this fuction should pause until all communication has been set up
00049    and returns the number of systems in the network.
00050    After which, all other remaining public functions (numprocs(), send(), etc)
00051    should operate normally. Every received message should immediate trigger the 
00052    attached receiver
00053    
00054    machines: a vector of string over machine IDs. This is typically provided by the user
00055              or through some other initialization mechanism
00056    initstring: Additional parameters passed by the user
00057    curmachineid: The ID of the current machine. Will be size_t(-1) if this is not available.
00058                  (Some comm protocols will negotiate this itself.)
00059    
00060    receiver: the receiving object
00061   */
00062   virtual void init(const std::vector<std::string> &machines,
00063             const std::map<std::string,std::string> &initopts,
00064             procid_t curmachineid,
00065             std::vector<dc_receive*> receiver) = 0;
00066 
00067   /// Must close all connections when this function is called
00068   virtual void close() = 0;
00069   
00070   virtual ~dc_comm_base() {}
00071   virtual procid_t numprocs() const = 0;
00072   
00073   virtual procid_t procid() const = 0;
00074   
00075   virtual size_t network_bytes_sent() const = 0;
00076   virtual size_t network_bytes_received() const = 0;
00077 
00078   
00079   /** returns true if the channel to the target
00080   machine is truly open. The dc_comm_base specification allows
00081   for lazy channels which are not created until it is used.
00082   For such implementations, this function should return true
00083   if the channel has been created, and false otherwise. Non-lazy
00084   implementations should return true all the time.
00085   The invariant to ensure is that this function must return true
00086   for a target machine ID if a packet has been sent from this machine
00087   to the target before this call.
00088   */
00089   virtual bool channel_active(size_t target) const = 0;
00090   
00091   /**
00092    Sends the string of length len to the target machine dest.
00093    Only valid after call to init();
00094    Establishes a connection if necessary
00095   */
00096   virtual void send(size_t target, const char* buf, size_t len) = 0;
00097  
00098   virtual void send2(size_t target, 
00099              const char* buf1, const size_t len1,
00100              const char* buf2, const size_t len2) = 0; 
00101 
00102   // not required and not used
00103   virtual void flush(size_t target) = 0;
00104 };
00105 
00106 } // namespace dc_impl
00107 } // namespace graphlab
00108 #endif
00109