mpbase.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. // Documentation for this file can be found at http://mgweb.mg.seisint.com/WebHelp/mp/html/mpbase_hpp.html
  14. #ifndef MPBASE_HPP
  15. #define MPBASE_HPP
  16. #ifndef mp_decl
  17. #define mp_decl DECL_IMPORT
  18. #endif
  19. #include "jutil.hpp"
  20. #include "jsocket.hpp"
  21. interface INode;
  22. interface INodeIterator;
  23. interface INodeCompare
  24. {
  25. virtual int docompare(const INode *left, const INode *right) const;
  26. };
  27. typedef unsigned rank_t;
  28. #define RANK_NULL ((rank_t)-1)
  29. #define RANK_ALL ((rank_t)-2)
  30. #define RANK_RANDOM ((rank_t)-3)
  31. #define RANK_ALL_OTHER ((rank_t)-4)
  32. enum GroupRelation { GRidentical, GRdifferentorder, GRbasesubset, GRwrappedsuperset, GRsubset, GRsuperset, GRintersection, GRdisjoint };
  33. interface IGroup: extends IInterface
  34. {
  35. virtual rank_t ordinality() const =0;
  36. virtual rank_t rank() const =0; // rank of calling process (RANK_NULL if not in group)
  37. virtual rank_t rank(INode *node) const =0; // rank of node (RANK_NULL if not in group)
  38. virtual rank_t rank(const SocketEndpoint &ep) const =0; // rank of node (RANK_NULL if not in group)
  39. virtual bool isMember() const =0; // true if calling member is in group
  40. virtual bool isMember(INode *node) const =0; // true if node member of group
  41. virtual GroupRelation compare(const IGroup *grp) const =0; // compares two groups
  42. virtual bool equals(const IGroup *grp) const =0; // faster than compare
  43. virtual bool overlaps(const IGroup *grp) const =0; // true if intersection non-empty
  44. virtual void translate(const IGroup *othergroup, rank_t nranks, const rank_t *otherranks,
  45. rank_t *resranks ) const =0; // translate ranks in othergroup to this group ranks
  46. // NB *** all the below do not change the group but return a new group as result ***
  47. virtual IGroup *subset(rank_t start,rank_t num) const =0; // selection
  48. virtual IGroup *subset(const rank_t *order,rank_t num) const =0;// re-ordered selection
  49. virtual IGroup *combine(const IGroup *) const =0; // union
  50. virtual IGroup *diff(const IGroup *) const =0; // difference
  51. virtual IGroup *intersect(const IGroup *) const =0; // intersection
  52. virtual IGroup *add(INode *node) const =0; // appends a node
  53. virtual IGroup *add(INode *node,unsigned pos) const =0; // inserts a node at pos
  54. virtual IGroup *remove(unsigned pos) const =0; // removes a node
  55. virtual IGroup *swap(rank_t,rank_t) const =0; // reorder
  56. virtual IGroup *rotate(int num) const =0; // rotate (+ve or -ve)
  57. virtual INodeIterator *getIterator(rank_t start=0,rank_t num=RANK_NULL) const = 0;
  58. virtual INode &queryNode(rank_t) const = 0;
  59. virtual INode *getNode(rank_t) const = 0;
  60. virtual void serialize(MemoryBuffer &tgt) const = 0;
  61. virtual StringBuffer &getText(StringBuffer &text) const = 0;
  62. virtual void getSocketEndpoints(SocketEndpointArray &sea) const = 0;
  63. virtual unsigned distance(const IpAddress &ip) const = 0;
  64. virtual unsigned distance(const IGroup *grp) const = 0;
  65. };
  66. interface IGroupResolver: extends IInterface
  67. {
  68. virtual IGroup *lookup(const char *logicalgroupname) = 0;
  69. virtual bool find(IGroup *grp,StringBuffer &name, bool add) = 0;
  70. };
  71. interface INode: extends IInterface
  72. {
  73. virtual const SocketEndpoint &endpoint() const = 0;
  74. virtual bool equals(const INode *node) const = 0;
  75. virtual unsigned getHash() const = 0;
  76. virtual bool isLocalTo(INode *node) const = 0; // is same machine as node
  77. virtual bool isHost() const = 0; // is same machine as running this process
  78. virtual void serialize(MemoryBuffer &tgt) = 0;
  79. };
  80. interface INodeIterator : extends IInterface
  81. {
  82. public:
  83. virtual bool first() = 0;
  84. virtual bool next() = 0;
  85. virtual bool isValid() = 0;
  86. virtual INode &query() = 0;
  87. virtual INode &get() = 0;
  88. };
  89. #define ForEachNodeInGroup(x,y) rank_t numItems##x = (y).ordinality(); \
  90. for(rank_t x = 0; x < numItems##x; ++x)
  91. #define ForEachOtherNodeInGroup(x,y) rank_t numItems##x = (y).ordinality(); \
  92. rank_t myrank##x = (y).rank(); \
  93. for(rank_t x = 0; x < numItems##x; ++x) \
  94. if (x!=myrank##x)
  95. extern mp_decl INode *createINode(const SocketEndpoint &ep);
  96. extern mp_decl INode *createINodeIP(const IpAddress &ip,unsigned short port);
  97. extern mp_decl INode *createINode(const char *name,unsigned short port=0);
  98. extern mp_decl INode *deserializeINode(MemoryBuffer &src);
  99. extern mp_decl IGroup *createIGroup(rank_t num,INode **);
  100. extern mp_decl IGroup *createIGroup(rank_t num,const SocketEndpoint *);
  101. extern mp_decl IGroup *createIGroup(SocketEndpointArray &);
  102. extern mp_decl IGroup *createIGroup(const char *endpointlist,unsigned short defport=0); // takes socketendpointlist or result of toText
  103. extern mp_decl IGroup *deserializeIGroup(MemoryBuffer &src);
  104. extern mp_decl INode *queryNullNode();
  105. extern mp_decl INode * queryMyNode();
  106. extern mp_decl void initMyNode(unsigned short port);
  107. // Exceptions
  108. interface mp_decl IMP_Exception: extends IException
  109. {
  110. virtual const SocketEndpoint &queryEndpoint() const = 0;
  111. };
  112. enum MessagePassingError
  113. {
  114. MPERR_ok,
  115. MPERR_connection_failed, // connection dropped (or could not be made)
  116. MPERR_process_not_in_group, // using an 'inner' communicator when not part of it's group
  117. MPERR_protocol_version_mismatch, // incompatible version of MP being used
  118. MPERR_link_closed // raised if other end closed (e.g. aborted) during a specific recv or probe
  119. };
  120. extern mp_decl void setNodeCaching(bool on);
  121. #endif