| 1 | // | 
|---|
| 2 | // dist.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 | #ifdef __GNUC__ | 
|---|
| 29 | #pragma implementation | 
|---|
| 30 | #endif | 
|---|
| 31 |  | 
|---|
| 32 | #include <iostream> | 
|---|
| 33 | #include <math.h> | 
|---|
| 34 |  | 
|---|
| 35 | #include <util/misc/formio.h> | 
|---|
| 36 | #include <util/keyval/keyval.h> | 
|---|
| 37 | #include <math/scmat/dist.h> | 
|---|
| 38 | #include <math/scmat/cmatrix.h> | 
|---|
| 39 | #include <math/scmat/elemop.h> | 
|---|
| 40 |  | 
|---|
| 41 | using namespace std; | 
|---|
| 42 | using namespace sc; | 
|---|
| 43 |  | 
|---|
| 44 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 45 | // DistSCMatrixKit member functions | 
|---|
| 46 |  | 
|---|
| 47 | static ClassDesc DistSCMatrixKit_cd( | 
|---|
| 48 | typeid(DistSCMatrixKit),"DistSCMatrixKit",1,"public SCMatrixKit", | 
|---|
| 49 | 0, create<DistSCMatrixKit>, 0); | 
|---|
| 50 |  | 
|---|
| 51 | DistSCMatrixKit::DistSCMatrixKit(const Ref<MessageGrp> &grp) | 
|---|
| 52 | { | 
|---|
| 53 | // if grp is nonnull, then reset grp_ (it gets set to the default in the | 
|---|
| 54 | // default SCMatrixKit constructor | 
|---|
| 55 | if (grp.nonnull()) | 
|---|
| 56 | grp_ = grp; | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | DistSCMatrixKit::DistSCMatrixKit(const Ref<KeyVal>& keyval): | 
|---|
| 60 | SCMatrixKit(keyval) | 
|---|
| 61 | { | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | DistSCMatrixKit::~DistSCMatrixKit() | 
|---|
| 65 | { | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | SCMatrix* | 
|---|
| 69 | DistSCMatrixKit::matrix(const RefSCDimension&d1, const RefSCDimension&d2) | 
|---|
| 70 | { | 
|---|
| 71 | return new DistSCMatrix(d1,d2,this); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | SymmSCMatrix* | 
|---|
| 75 | DistSCMatrixKit::symmmatrix(const RefSCDimension&d) | 
|---|
| 76 | { | 
|---|
| 77 | return new DistSymmSCMatrix(d,this); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | DiagSCMatrix* | 
|---|
| 81 | DistSCMatrixKit::diagmatrix(const RefSCDimension&d) | 
|---|
| 82 | { | 
|---|
| 83 | return new DistDiagSCMatrix(d,this); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | SCVector* | 
|---|
| 87 | DistSCMatrixKit::vector(const RefSCDimension&d) | 
|---|
| 88 | { | 
|---|
| 89 | return new DistSCVector(d,this); | 
|---|
| 90 |  | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 94 | // DistSCMatrixKit member functions | 
|---|
| 95 |  | 
|---|
| 96 | DistSCMatrixListSubblockIter::DistSCMatrixListSubblockIter( | 
|---|
| 97 | Access access, | 
|---|
| 98 | const Ref<SCMatrixBlockList> &list, | 
|---|
| 99 | const Ref<MessageGrp> &grp | 
|---|
| 100 | ): | 
|---|
| 101 | SCMatrixListSubblockIter(access, list->deepcopy()), | 
|---|
| 102 | grp_(grp), | 
|---|
| 103 | out_(grp), | 
|---|
| 104 | in_(grp), | 
|---|
| 105 | step_(0), | 
|---|
| 106 | locallist_(list) | 
|---|
| 107 | { | 
|---|
| 108 | if (access == Write) { | 
|---|
| 109 | ExEnv::errn() << indent | 
|---|
| 110 | << "DistSCMatrixListSubblockIter: write access not allowed" | 
|---|
| 111 | << endl; | 
|---|
| 112 | abort(); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | if (grp->n() == 1) return; | 
|---|
| 116 |  | 
|---|
| 117 | out_.target(grp->me() == grp->n()-1 ? 0: grp->me()+1); | 
|---|
| 118 | in_.source(grp->me() == 0 ? grp->n()-1: grp->me()-1); | 
|---|
| 119 |  | 
|---|
| 120 | out_.copy_references(); | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | void | 
|---|
| 124 | DistSCMatrixListSubblockIter::begin() | 
|---|
| 125 | { | 
|---|
| 126 | if (step_ == grp_->n()) step_ = 0; | 
|---|
| 127 | else if (step_ != 0) { | 
|---|
| 128 | ExEnv::errn() << indent << "DistSCMatrixListSubblockIter::begin(): " | 
|---|
| 129 | << "step != 0: tried to begin in middle of iteration" | 
|---|
| 130 | << endl; | 
|---|
| 131 | abort(); | 
|---|
| 132 | } | 
|---|
| 133 | SCMatrixListSubblockIter::begin(); | 
|---|
| 134 | maybe_advance_list(); | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | void | 
|---|
| 138 | DistSCMatrixListSubblockIter::maybe_advance_list() | 
|---|
| 139 | { | 
|---|
| 140 | while (!ready() && grp_->n() > 1 && step_ < grp_->n() - 1) { | 
|---|
| 141 | advance_list(); | 
|---|
| 142 | } | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | void | 
|---|
| 146 | DistSCMatrixListSubblockIter::advance_list() | 
|---|
| 147 | { | 
|---|
| 148 | SavableState::save_state(list_.pointer(), out_); | 
|---|
| 149 | out_.flush(); | 
|---|
| 150 | list_ << SavableState::restore_state(in_); | 
|---|
| 151 | SCMatrixListSubblockIter::begin(); | 
|---|
| 152 | step_++; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | void | 
|---|
| 156 | DistSCMatrixListSubblockIter::next() | 
|---|
| 157 | { | 
|---|
| 158 | SCMatrixListSubblockIter::next(); | 
|---|
| 159 | maybe_advance_list(); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | DistSCMatrixListSubblockIter::~DistSCMatrixListSubblockIter() | 
|---|
| 163 | { | 
|---|
| 164 | if (access_ == Accum) { | 
|---|
| 165 | while (step_%grp_->n() != 0) { | 
|---|
| 166 | advance_list(); | 
|---|
| 167 | } | 
|---|
| 168 | SCMatrixBlockListIter i1, i2; | 
|---|
| 169 | for (i1=list_->begin(),i2=locallist_->begin(); | 
|---|
| 170 | i1!=list_->end() && i2!=locallist_->end(); | 
|---|
| 171 | i1++,i2++) { | 
|---|
| 172 | int n = i1.block()->ndat(); | 
|---|
| 173 | if (n != i2.block()->ndat()) { | 
|---|
| 174 | ExEnv::errn() << indent | 
|---|
| 175 | << "DistSCMatrixListSubblockIter: block mismatch: " | 
|---|
| 176 | << "internal error" << endl; | 
|---|
| 177 | abort(); | 
|---|
| 178 | } | 
|---|
| 179 | double *dat1 = i1.block()->dat(); | 
|---|
| 180 | double *dat2 = i2.block()->dat(); | 
|---|
| 181 | for (int i=0; i<n; i++) { | 
|---|
| 182 | dat2[i] += dat1[i]; | 
|---|
| 183 | } | 
|---|
| 184 | } | 
|---|
| 185 | } | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 189 |  | 
|---|
| 190 | // Local Variables: | 
|---|
| 191 | // mode: c++ | 
|---|
| 192 | // c-file-style: "CLJ" | 
|---|
| 193 | // End: | 
|---|