| 1 | //
 | 
|---|
| 2 | // messtest.cc
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc.
 | 
|---|
| 5 | //
 | 
|---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.com>
 | 
|---|
| 7 | // Maintainer: LPS
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // This file is part of the SC Toolkit.
 | 
|---|
| 10 | //
 | 
|---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify
 | 
|---|
| 12 | // it under the terms of the GNU Library General Public License as published by
 | 
|---|
| 13 | // the Free Software Foundation; either version 2, or (at your option)
 | 
|---|
| 14 | // any later version.
 | 
|---|
| 15 | //
 | 
|---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful,
 | 
|---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 19 | // GNU Library General Public License for more details.
 | 
|---|
| 20 | //
 | 
|---|
| 21 | // You should have received a copy of the GNU Library General Public License
 | 
|---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
 | 
|---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 | 
|---|
| 24 | //
 | 
|---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7.
 | 
|---|
| 26 | //
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #include <util/misc/formio.h>
 | 
|---|
| 29 | #include <util/keyval/keyval.h>
 | 
|---|
| 30 | #include <util/class/class.h>
 | 
|---|
| 31 | #include <util/state/state.h>
 | 
|---|
| 32 | #include <util/misc/bug.h>
 | 
|---|
| 33 | #include <util/group/message.h>
 | 
|---|
| 34 | #include <util/group/mstate.h>
 | 
|---|
| 35 | #include <util/group/hcube.h>
 | 
|---|
| 36 | 
 | 
|---|
| 37 | using namespace std;
 | 
|---|
| 38 | using namespace sc;
 | 
|---|
| 39 | 
 | 
|---|
| 40 | // Force linkages:
 | 
|---|
| 41 | //#ifndef __PIC__
 | 
|---|
| 42 | #ifndef PUMAGON
 | 
|---|
| 43 | #   include <util/group/messshm.h>
 | 
|---|
| 44 |     static ForceLink<ShmMessageGrp> fl0;
 | 
|---|
| 45 | #endif
 | 
|---|
| 46 | # ifdef HAVE_MPI
 | 
|---|
| 47 | #   include <util/group/messmpi.h>
 | 
|---|
| 48 |     static ForceLink<MPIMessageGrp> fl2;
 | 
|---|
| 49 | # endif
 | 
|---|
| 50 | //#endif
 | 
|---|
| 51 | 
 | 
|---|
| 52 | class A: virtual public SavableState {
 | 
|---|
| 53 |   private:
 | 
|---|
| 54 |     int ia;
 | 
|---|
| 55 |     int n;
 | 
|---|
| 56 |     int* array;
 | 
|---|
| 57 |     double d;
 | 
|---|
| 58 |   public:
 | 
|---|
| 59 |     A(int size);
 | 
|---|
| 60 |     A(const Ref<KeyVal>&);
 | 
|---|
| 61 |     A(StateIn&);
 | 
|---|
| 62 |     ~A();
 | 
|---|
| 63 |     void save_data_state(StateOut&);
 | 
|---|
| 64 |     inline int& a() { return ia; };
 | 
|---|
| 65 |     virtual void print (ostream&s = cout)
 | 
|---|
| 66 |     {
 | 
|---|
| 67 |       s << "A::a = " << a() << '\n';
 | 
|---|
| 68 |       s << "A::array = {";
 | 
|---|
| 69 |       for (int i=0; i<n; i++) s << array[i] << ' ';
 | 
|---|
| 70 |       s << "}\n";
 | 
|---|
| 71 |     }
 | 
|---|
| 72 | };
 | 
|---|
| 73 | 
 | 
|---|
| 74 | A::A(int size):
 | 
|---|
| 75 |   ia(1),
 | 
|---|
| 76 |   n(size),
 | 
|---|
| 77 |   array(new int[size]),
 | 
|---|
| 78 |   d(-1.24)
 | 
|---|
| 79 | {
 | 
|---|
| 80 |   for (int i=0; i<size; i++) array[i] = size - i - 1;
 | 
|---|
| 81 | }
 | 
|---|
| 82 | A::A(const Ref<KeyVal>&keyval):
 | 
|---|
| 83 |   ia(keyval->intvalue("a")),
 | 
|---|
| 84 |   n(keyval->intvalue("n")),
 | 
|---|
| 85 |   d(-1.24)
 | 
|---|
| 86 | 
 | 
|---|
| 87 | {
 | 
|---|
| 88 |   array = new int[n];
 | 
|---|
| 89 |   for (int i=0; i<n; i++) array[i] = i + 10000;
 | 
|---|
| 90 | }
 | 
|---|
| 91 | A::A(StateIn&s):
 | 
|---|
| 92 |   SavableState(s)
 | 
|---|
| 93 | {
 | 
|---|
| 94 |   cout << "getting d" << endl;
 | 
|---|
| 95 |   s.get(d);
 | 
|---|
| 96 |   cout << "getting ia" << endl;
 | 
|---|
| 97 |   s.get(ia);
 | 
|---|
| 98 |   cout << "getting array" << endl;
 | 
|---|
| 99 |   s.get(n);
 | 
|---|
| 100 |   s.get(array);
 | 
|---|
| 101 |   cout << "got everything" << endl;
 | 
|---|
| 102 | }
 | 
|---|
| 103 | A::~A()
 | 
|---|
| 104 | {
 | 
|---|
| 105 |   delete[] array;
 | 
|---|
| 106 | }
 | 
|---|
| 107 | void
 | 
|---|
| 108 | A::save_data_state(StateOut&s)
 | 
|---|
| 109 | {
 | 
|---|
| 110 |   cout << "putting d" << endl;
 | 
|---|
| 111 |   s.put(d);
 | 
|---|
| 112 |   cout << "putting ia" << endl;
 | 
|---|
| 113 |   s.put(ia);
 | 
|---|
| 114 |   cout << "putting array" << endl;
 | 
|---|
| 115 |   s.put(n);
 | 
|---|
| 116 |   s.put(array,n);
 | 
|---|
| 117 |   cout << "put everything" << endl;
 | 
|---|
| 118 | }
 | 
|---|
| 119 | 
 | 
|---|
| 120 | static ClassDesc A_cd(
 | 
|---|
| 121 |   typeid(A),"A",1,"virtual public SavableState",
 | 
|---|
| 122 |   0, create<A>, create<A>);
 | 
|---|
| 123 | 
 | 
|---|
| 124 | void test(const Ref<MessageGrp>&, int source, int target);
 | 
|---|
| 125 | void test_hcube(int nproc, int root, int fwd);
 | 
|---|
| 126 | 
 | 
|---|
| 127 | int
 | 
|---|
| 128 | main(int argc, char**argv)
 | 
|---|
| 129 | {
 | 
|---|
| 130 |   Ref<MessageGrp> grp = MessageGrp::initial_messagegrp(argc, argv);
 | 
|---|
| 131 | 
 | 
|---|
| 132 |   Ref<Debugger> debugger;
 | 
|---|
| 133 | 
 | 
|---|
| 134 |   if (grp.null()) {
 | 
|---|
| 135 |       const char* input = SRCDIR "/messtest.in";
 | 
|---|
| 136 |       const char* keyword = "message";
 | 
|---|
| 137 | 
 | 
|---|
| 138 |       if (argc >= 2) input = argv[1];
 | 
|---|
| 139 |       if (argc >= 3) keyword = argv[2];
 | 
|---|
| 140 | 
 | 
|---|
| 141 |       Ref<KeyVal> keyval = new ParsedKeyVal(input);
 | 
|---|
| 142 | 
 | 
|---|
| 143 |       grp << keyval->describedclassvalue(keyword);
 | 
|---|
| 144 | 
 | 
|---|
| 145 |       debugger << keyval->describedclassvalue(":debug");
 | 
|---|
| 146 | 
 | 
|---|
| 147 |       if (grp.null()) {
 | 
|---|
| 148 |           cerr << scprintf("Couldn't initialize MessageGrp\n");
 | 
|---|
| 149 |           abort();
 | 
|---|
| 150 |         }
 | 
|---|
| 151 |     }
 | 
|---|
| 152 | 
 | 
|---|
| 153 |   if (debugger.nonnull()) {
 | 
|---|
| 154 |       debugger->set_exec(argv[0]);
 | 
|---|
| 155 |       debugger->set_prefix(grp->me());
 | 
|---|
| 156 |     }
 | 
|---|
| 157 | 
 | 
|---|
| 158 |   Debugger::set_default_debugger(debugger);
 | 
|---|
| 159 | 
 | 
|---|
| 160 |   grp->sync();
 | 
|---|
| 161 |   if (grp->n() > 1) {
 | 
|---|
| 162 |       BcastState bc(grp,1);
 | 
|---|
| 163 |       bc.bcast(debugger);
 | 
|---|
| 164 |       bc.flush();
 | 
|---|
| 165 |     }
 | 
|---|
| 166 |   grp->sync();
 | 
|---|
| 167 |   if (debugger.nonnull()) {
 | 
|---|
| 168 |       debugger->set_exec(argv[0]);
 | 
|---|
| 169 |       debugger->set_prefix(grp->me());
 | 
|---|
| 170 |       debugger->traceback();
 | 
|---|
| 171 |     }
 | 
|---|
| 172 |   grp->sync();
 | 
|---|
| 173 | 
 | 
|---|
| 174 |   if (0 && grp->me() == 0) {
 | 
|---|
| 175 |       test_hcube(3, 0, 1);
 | 
|---|
| 176 |       test_hcube(39, 0, 1);
 | 
|---|
| 177 |       test_hcube(16, 0, 1);
 | 
|---|
| 178 |       test_hcube(17, 4, 1);
 | 
|---|
| 179 |       test_hcube(17, 4, 0);
 | 
|---|
| 180 |       test_hcube(1, 0, 0);
 | 
|---|
| 181 |     }
 | 
|---|
| 182 | 
 | 
|---|
| 183 |   grp->sync();
 | 
|---|
| 184 | 
 | 
|---|
| 185 |   if (grp->n() >= 3) {
 | 
|---|
| 186 |       test(grp, 2, 1);
 | 
|---|
| 187 |     }
 | 
|---|
| 188 |   else {
 | 
|---|
| 189 |       test(grp, 0, 0);
 | 
|---|
| 190 |     }
 | 
|---|
| 191 | 
 | 
|---|
| 192 |   int testsum = 1;
 | 
|---|
| 193 |   grp->sum(&testsum,1);
 | 
|---|
| 194 |   if (testsum != grp->n()) {
 | 
|---|
| 195 |       cerr << scprintf("WARNING: sum wrong\n");
 | 
|---|
| 196 |     }
 | 
|---|
| 197 | 
 | 
|---|
| 198 |   double testdsum = 1.0;
 | 
|---|
| 199 |   grp->sum(&testdsum,1);
 | 
|---|
| 200 |   cout << scprintf("on %d testdsum = %4.1f\n", grp->me(), testdsum);
 | 
|---|
| 201 | 
 | 
|---|
| 202 |   grp->sync();
 | 
|---|
| 203 |   grp = 0;
 | 
|---|
| 204 |   return 0;
 | 
|---|
| 205 | }
 | 
|---|
| 206 | 
 | 
|---|
| 207 | void
 | 
|---|
| 208 | test_hcube(int nproc, int root, int fwd)
 | 
|---|
| 209 | {
 | 
|---|
| 210 |   int i, j;
 | 
|---|
| 211 |   Ref<GlobalMsgIter> *gmi = new Ref<GlobalMsgIter>[nproc];
 | 
|---|
| 212 |   for (i=0; i<nproc; i++) {
 | 
|---|
| 213 |       gmi[i] =  new HypercubeGMI(nproc, i, root);
 | 
|---|
| 214 |     }
 | 
|---|
| 215 |   int iter = 1;
 | 
|---|
| 216 |   for (j=0; j<nproc; j++) {
 | 
|---|
| 217 |       if (fwd) {
 | 
|---|
| 218 |           gmi[j]->forwards();
 | 
|---|
| 219 |         }
 | 
|---|
| 220 |       else {
 | 
|---|
| 221 |           gmi[j]->backwards();
 | 
|---|
| 222 |         }
 | 
|---|
| 223 |     }
 | 
|---|
| 224 |   while (!gmi[0]->done()) {
 | 
|---|
| 225 |       cout << scprintf("------ step %d of %d ------\n", iter, gmi[0]->n());
 | 
|---|
| 226 |       for (j=0; j<nproc; j++) {
 | 
|---|
| 227 |           if (gmi[j]->send()) {
 | 
|---|
| 228 |               if (0 <= gmi[j]->sendto() && gmi[j]->sendto() < nproc) {
 | 
|---|
| 229 |                   if (gmi[gmi[j]->sendto()]->recvfrom() == j) {
 | 
|---|
| 230 |                       cout << scprintf(" %d -> %d\n", j, gmi[j]->sendto());
 | 
|---|
| 231 |                     }
 | 
|---|
| 232 |                   else {
 | 
|---|
| 233 |                       cout << scprintf(" %d -> (%d)\n", j, gmi[j]->sendto());
 | 
|---|
| 234 |                     }
 | 
|---|
| 235 |                 }
 | 
|---|
| 236 |               else {
 | 
|---|
| 237 |                   cout << scprintf(" %d -> %d?\n", j, gmi[j]->sendto());
 | 
|---|
| 238 |                 }
 | 
|---|
| 239 |             }
 | 
|---|
| 240 |           else if (gmi[j]->recv()) {
 | 
|---|
| 241 |               if (0 <= gmi[j]->recvfrom() && gmi[j]->recvfrom() < nproc) {
 | 
|---|
| 242 |                   if (gmi[gmi[j]->recvfrom()]->sendto() == j) {
 | 
|---|
| 243 |                       // to be printed by sender
 | 
|---|
| 244 |                     }
 | 
|---|
| 245 |                   else {
 | 
|---|
| 246 |                       cout << scprintf(" (%d) -> %d\n", gmi[j]->recvfrom(), j);
 | 
|---|
| 247 |                     }
 | 
|---|
| 248 |                 }
 | 
|---|
| 249 |               else {
 | 
|---|
| 250 |                   cout << scprintf(" %d? -> %d\n", gmi[j]->recvfrom(), j);
 | 
|---|
| 251 |                 }
 | 
|---|
| 252 |             }
 | 
|---|
| 253 |         }
 | 
|---|
| 254 |       for (j=0; j<nproc; j++) gmi[j]->next();
 | 
|---|
| 255 |       iter++;
 | 
|---|
| 256 |     }
 | 
|---|
| 257 |   cout.flush();
 | 
|---|
| 258 | }
 | 
|---|
| 259 | 
 | 
|---|
| 260 | void
 | 
|---|
| 261 | test(const Ref<MessageGrp>& grp, int source, int target)
 | 
|---|
| 262 | {
 | 
|---|
| 263 |   Ref<A> a,b;
 | 
|---|
| 264 |   const int nca = 1000000;
 | 
|---|
| 265 |   char ca[nca];
 | 
|---|
| 266 |   
 | 
|---|
| 267 |   if (grp->me() == source) {
 | 
|---|
| 268 |       StateSend so(grp);
 | 
|---|
| 269 |       //so.set_buffer_size(5);
 | 
|---|
| 270 |       so.target(target);
 | 
|---|
| 271 |       a = new A(10);
 | 
|---|
| 272 |       SavableState::save_state(a,so);
 | 
|---|
| 273 |       so.flush();
 | 
|---|
| 274 |       grp->send(target, ca, nca);
 | 
|---|
| 275 |       if (source != target) grp->recv(target, ca, nca);
 | 
|---|
| 276 |     }
 | 
|---|
| 277 | 
 | 
|---|
| 278 |   if (grp->me() == target) {
 | 
|---|
| 279 |       StateRecv si(grp);
 | 
|---|
| 280 |       //si.set_buffer_size(5);
 | 
|---|
| 281 |       si.source(source);
 | 
|---|
| 282 |       b << SavableState::restore_state(si);
 | 
|---|
| 283 |       if (source != target) grp->send(source, ca, nca);
 | 
|---|
| 284 |       grp->recv(source, ca, nca);
 | 
|---|
| 285 |     }
 | 
|---|
| 286 | 
 | 
|---|
| 287 |   if (grp->me() == target) {
 | 
|---|
| 288 |       cout << "target:" << endl;
 | 
|---|
| 289 |       b->print();
 | 
|---|
| 290 |     }
 | 
|---|
| 291 | 
 | 
|---|
| 292 |   grp->sync();
 | 
|---|
| 293 | 
 | 
|---|
| 294 |   if (grp->me() == source) {
 | 
|---|
| 295 |       cout << "source:" << endl;
 | 
|---|
| 296 |       a->print();
 | 
|---|
| 297 |     }
 | 
|---|
| 298 | 
 | 
|---|
| 299 |   ///////////////////////////////////////////////////
 | 
|---|
| 300 |   // Test broadcast
 | 
|---|
| 301 | 
 | 
|---|
| 302 |   if (source != target) {
 | 
|---|
| 303 |       grp->sync();
 | 
|---|
| 304 | 
 | 
|---|
| 305 |       b = 0;
 | 
|---|
| 306 |   
 | 
|---|
| 307 |       if (grp->me() == source) {
 | 
|---|
| 308 |           BcastStateSend so(grp);
 | 
|---|
| 309 |           SavableState::save_state(a,so);
 | 
|---|
| 310 |         }
 | 
|---|
| 311 |       else {
 | 
|---|
| 312 |           BcastStateRecv si(grp,source);
 | 
|---|
| 313 |           b << SavableState::restore_state(si);
 | 
|---|
| 314 |         }
 | 
|---|
| 315 | 
 | 
|---|
| 316 |       if (grp->me() == target) {
 | 
|---|
| 317 |           cout << "bcast target:" << endl;
 | 
|---|
| 318 |           b->print();
 | 
|---|
| 319 |         }
 | 
|---|
| 320 |     }
 | 
|---|
| 321 | 
 | 
|---|
| 322 | }
 | 
|---|
| 323 | 
 | 
|---|
| 324 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
| 325 | 
 | 
|---|
| 326 | // Local Variables:
 | 
|---|
| 327 | // mode: c++
 | 
|---|
| 328 | // c-file-style: "CLJ"
 | 
|---|
| 329 | // End:
 | 
|---|