| [0b990d] | 1 | //
 | 
|---|
 | 2 | // distvect.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 <iostream>
 | 
|---|
 | 29 | #include <iomanip>
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | #include <math.h>
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 | #include <util/misc/formio.h>
 | 
|---|
 | 34 | #include <util/keyval/keyval.h>
 | 
|---|
 | 35 | #include <math/scmat/dist.h>
 | 
|---|
 | 36 | #include <math/scmat/cmatrix.h>
 | 
|---|
 | 37 | #include <math/scmat/elemop.h>
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | using namespace std;
 | 
|---|
 | 40 | using namespace sc;
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
 | 43 | // DistSCVector member functions
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | static ClassDesc DistSCVector_cd(
 | 
|---|
 | 46 |   typeid(DistSCVector),"DistSCVector",1,"public SCVector",
 | 
|---|
 | 47 |   0, 0, 0);
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 | DistSCVector::DistSCVector(const RefSCDimension&a, DistSCMatrixKit*k):
 | 
|---|
 | 50 |   SCVector(a,k)
 | 
|---|
 | 51 | {
 | 
|---|
 | 52 |   init_blocklist();
 | 
|---|
 | 53 | }
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 | int
 | 
|---|
 | 56 | DistSCVector::block_to_node(int i) const
 | 
|---|
 | 57 | {
 | 
|---|
 | 58 |   return (i)%messagegrp()->n();
 | 
|---|
 | 59 | }
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 | Ref<SCMatrixBlock>
 | 
|---|
 | 62 | DistSCVector::block_to_block(int i) const
 | 
|---|
 | 63 | {
 | 
|---|
 | 64 |   int offset = i;
 | 
|---|
 | 65 |   int nproc = messagegrp()->n();
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 |   if ((offset%nproc) != messagegrp()->me()) return 0;
 | 
|---|
 | 68 | 
 | 
|---|
 | 69 |   SCMatrixBlockListIter I;
 | 
|---|
 | 70 |   for (I=blocklist->begin(); I!=blocklist->end(); I++) {
 | 
|---|
 | 71 |       if (I.block()->blocki == i)
 | 
|---|
 | 72 |           return I.block();
 | 
|---|
 | 73 |     }
 | 
|---|
 | 74 | 
 | 
|---|
 | 75 |   ExEnv::errn() << indent << "DistSCVector::block_to_block: internal error" << endl;
 | 
|---|
 | 76 |   abort();
 | 
|---|
 | 77 |   return 0;
 | 
|---|
 | 78 | }
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 | double *
 | 
|---|
 | 81 | DistSCVector::find_element(int i) const
 | 
|---|
 | 82 | {
 | 
|---|
 | 83 |   int bi, oi;
 | 
|---|
 | 84 |   d->blocks()->elem_to_block(i, bi, oi);
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 |   Ref<SCVectorSimpleBlock> blk; blk << block_to_block(bi);
 | 
|---|
 | 87 |   if (blk.nonnull()) {
 | 
|---|
 | 88 |       return &blk->dat()[oi];
 | 
|---|
 | 89 |     }
 | 
|---|
 | 90 |   else {
 | 
|---|
 | 91 |       return 0;
 | 
|---|
 | 92 |     }
 | 
|---|
 | 93 | }
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 | int
 | 
|---|
 | 96 | DistSCVector::element_to_node(int i) const
 | 
|---|
 | 97 | {
 | 
|---|
 | 98 |   int bi, oi;
 | 
|---|
 | 99 |   d->blocks()->elem_to_block(i, bi, oi);
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 |   return block_to_node(bi);
 | 
|---|
 | 102 | }
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 | void
 | 
|---|
 | 105 | DistSCVector::init_blocklist()
 | 
|---|
 | 106 | {
 | 
|---|
 | 107 |   int i;
 | 
|---|
 | 108 |   int nproc = messagegrp()->n();
 | 
|---|
 | 109 |   int me = messagegrp()->me();
 | 
|---|
 | 110 |   blocklist = new SCMatrixBlockList;
 | 
|---|
 | 111 |   SCMatrixBlock *b;
 | 
|---|
 | 112 |   for (i=0; i<d->blocks()->nblock(); i++) {
 | 
|---|
 | 113 |       if (i%nproc != me) continue;
 | 
|---|
 | 114 |       b = new SCVectorSimpleBlock(d->blocks()->start(i),
 | 
|---|
 | 115 |                                   d->blocks()->fence(i));
 | 
|---|
 | 116 |       b->blocki = i;
 | 
|---|
 | 117 |       blocklist->insert(b);
 | 
|---|
 | 118 |     }
 | 
|---|
 | 119 | }
 | 
|---|
 | 120 | 
 | 
|---|
 | 121 | DistSCVector::~DistSCVector()
 | 
|---|
 | 122 | {
 | 
|---|
 | 123 | }
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 | double
 | 
|---|
 | 126 | DistSCVector::get_element(int i) const
 | 
|---|
 | 127 | {
 | 
|---|
 | 128 |   double res;
 | 
|---|
 | 129 |   double *e = find_element(i);
 | 
|---|
 | 130 |   if (e) {
 | 
|---|
 | 131 |       res = *e;
 | 
|---|
 | 132 |       messagegrp()->bcast(res, messagegrp()->me());
 | 
|---|
 | 133 |     }
 | 
|---|
 | 134 |   else {
 | 
|---|
 | 135 |       messagegrp()->bcast(res, element_to_node(i));
 | 
|---|
 | 136 |     }
 | 
|---|
 | 137 |   return res;
 | 
|---|
 | 138 | }
 | 
|---|
 | 139 | 
 | 
|---|
 | 140 | void
 | 
|---|
 | 141 | DistSCVector::set_element(int i,double a)
 | 
|---|
 | 142 | {
 | 
|---|
 | 143 |   double *e = find_element(i);
 | 
|---|
 | 144 |   if (e) {
 | 
|---|
 | 145 |       *e = a;
 | 
|---|
 | 146 |     }
 | 
|---|
 | 147 | }
 | 
|---|
 | 148 | 
 | 
|---|
 | 149 | void
 | 
|---|
 | 150 | DistSCVector::accumulate_element(int i,double a)
 | 
|---|
 | 151 | {
 | 
|---|
 | 152 |   double *e = find_element(i);
 | 
|---|
 | 153 |   if (e) {
 | 
|---|
 | 154 |       *e += a;
 | 
|---|
 | 155 |     }
 | 
|---|
 | 156 | }
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 | void
 | 
|---|
 | 159 | DistSCVector::accumulate(const SCVector*a)
 | 
|---|
 | 160 | {
 | 
|---|
 | 161 |   // make sure that the argument is of the correct type
 | 
|---|
 | 162 |   const DistSCVector* la
 | 
|---|
 | 163 |     = require_dynamic_cast<const DistSCVector*>(a,"DistSCVector::accumulate");
 | 
|---|
 | 164 | 
 | 
|---|
 | 165 |   // make sure that the dimensions match
 | 
|---|
 | 166 |   if (!dim()->equiv(la->dim())) {
 | 
|---|
 | 167 |       ExEnv::errn() << indent << "DistSCVector::accumulate(SCVector*a): "
 | 
|---|
 | 168 |            << "dimensions don't match\n";
 | 
|---|
 | 169 |       abort();
 | 
|---|
 | 170 |     }
 | 
|---|
 | 171 | 
 | 
|---|
 | 172 |   SCMatrixBlockListIter i1, i2;
 | 
|---|
 | 173 |   for (i1=la->blocklist->begin(),i2=blocklist->begin();
 | 
|---|
 | 174 |        i1!=la->blocklist->end() && i2!=blocklist->end();
 | 
|---|
 | 175 |        i1++,i2++) {
 | 
|---|
 | 176 |       int n = i1.block()->ndat();
 | 
|---|
 | 177 |       if (n != i2.block()->ndat()) {
 | 
|---|
 | 178 |           ExEnv::errn() << indent << "DistSCVector::accumulate "
 | 
|---|
 | 179 |                << "mismatch: internal error" << endl;
 | 
|---|
 | 180 |           abort();
 | 
|---|
 | 181 |         }
 | 
|---|
 | 182 |       double *dat1 = i1.block()->dat();
 | 
|---|
 | 183 |       double *dat2 = i2.block()->dat();
 | 
|---|
 | 184 |       for (int i=0; i<n; i++) {
 | 
|---|
 | 185 |           dat2[i] += dat1[i];
 | 
|---|
 | 186 |         }
 | 
|---|
 | 187 |     }
 | 
|---|
 | 188 | }
 | 
|---|
 | 189 | 
 | 
|---|
 | 190 | void
 | 
|---|
 | 191 | DistSCVector::accumulate(const SCMatrix*a)
 | 
|---|
 | 192 | {
 | 
|---|
 | 193 |   // make sure that the argument is of the correct type
 | 
|---|
 | 194 |   const DistSCMatrix* la
 | 
|---|
 | 195 |     = require_dynamic_cast<const DistSCMatrix*>(a,"DistSCVector::accumulate");
 | 
|---|
 | 196 | 
 | 
|---|
 | 197 |   // make sure that the dimensions match
 | 
|---|
 | 198 |   if (!((la->rowdim()->equiv(dim()) && la->coldim()->n() == 1)
 | 
|---|
 | 199 |         || (la->coldim()->equiv(dim()) && la->rowdim()->n() == 1))) {
 | 
|---|
 | 200 |       ExEnv::errn() << indent << "DistSCVector::accumulate(SCMatrix*a): "
 | 
|---|
 | 201 |            << "dimensions don't match\n";
 | 
|---|
 | 202 |       abort();
 | 
|---|
 | 203 |     }
 | 
|---|
 | 204 | 
 | 
|---|
 | 205 |   SCMatrixBlockListIter I, J;
 | 
|---|
 | 206 |   for (I = la->blocklist->begin(), J = blocklist->begin();
 | 
|---|
 | 207 |        I != la->blocklist->end() && J != blocklist->end();
 | 
|---|
 | 208 |        I++,J++) {
 | 
|---|
 | 209 |       int n = I.block()->ndat();
 | 
|---|
 | 210 |       if (n != J.block()->ndat()) {
 | 
|---|
 | 211 |           ExEnv::errn() << indent << "DistSCVector::accumulate(SCMatrix*a): "
 | 
|---|
 | 212 |                << "block lists do not match" << endl;
 | 
|---|
 | 213 |           abort();
 | 
|---|
 | 214 |         }
 | 
|---|
 | 215 |       double *dati = I.block()->dat();
 | 
|---|
 | 216 |       double *datj = J.block()->dat();
 | 
|---|
 | 217 |       for (int i=0; i<n; i++) datj[i] += dati[i];
 | 
|---|
 | 218 |     }
 | 
|---|
 | 219 | }
 | 
|---|
 | 220 | 
 | 
|---|
 | 221 | void
 | 
|---|
 | 222 | DistSCVector::assign_v(SCVector*a)
 | 
|---|
 | 223 | {
 | 
|---|
 | 224 |   // make sure that the argument is of the correct type
 | 
|---|
 | 225 |   DistSCVector* la
 | 
|---|
 | 226 |     = require_dynamic_cast<DistSCVector*>(a,"DistSCVector::assign_v");
 | 
|---|
 | 227 | 
 | 
|---|
 | 228 |   // make sure that the dimensions match
 | 
|---|
 | 229 |   if (!dim()->equiv(la->dim())) {
 | 
|---|
 | 230 |       ExEnv::errn() << indent << "DistSCVector::assign_v(SCVector*a): "
 | 
|---|
 | 231 |            << "dimensions don't match\n";
 | 
|---|
 | 232 |       abort();
 | 
|---|
 | 233 |     }
 | 
|---|
 | 234 | 
 | 
|---|
 | 235 |   SCMatrixBlockListIter i1, i2;
 | 
|---|
 | 236 |   for (i1=la->blocklist->begin(),i2=blocklist->begin();
 | 
|---|
 | 237 |        i1!=la->blocklist->end() && i2!=blocklist->end();
 | 
|---|
 | 238 |        i1++,i2++) {
 | 
|---|
 | 239 |       int n = i1.block()->ndat();
 | 
|---|
 | 240 |       if (n != i2.block()->ndat()) {
 | 
|---|
 | 241 |           ExEnv::errn() << indent << "DistSCVector::assign "
 | 
|---|
 | 242 |                << "mismatch: internal error" << endl;
 | 
|---|
 | 243 |           abort();
 | 
|---|
 | 244 |         }
 | 
|---|
 | 245 |       double *dat1 = i1.block()->dat();
 | 
|---|
 | 246 |       double *dat2 = i2.block()->dat();
 | 
|---|
 | 247 |       for (int i=0; i<n; i++) {
 | 
|---|
 | 248 |           dat2[i] = dat1[i];
 | 
|---|
 | 249 |         }
 | 
|---|
 | 250 |     }
 | 
|---|
 | 251 | }
 | 
|---|
 | 252 | 
 | 
|---|
 | 253 | void
 | 
|---|
 | 254 | DistSCVector::assign_p(const double*a)
 | 
|---|
 | 255 | {
 | 
|---|
 | 256 |   SCMatrixBlockListIter I;
 | 
|---|
 | 257 |   for (I=blocklist->begin();
 | 
|---|
 | 258 |        I!=blocklist->end();
 | 
|---|
 | 259 |        I++) {
 | 
|---|
 | 260 |       Ref<SCVectorSimpleBlock> b = dynamic_cast<SCVectorSimpleBlock*>(I.block());
 | 
|---|
 | 261 |       if (b.null()) {
 | 
|---|
 | 262 |           ExEnv::errn() << indent << "DistSCVector::assign "
 | 
|---|
 | 263 |                << "mismatch: internal error" << endl;
 | 
|---|
 | 264 |           abort();
 | 
|---|
 | 265 |         }
 | 
|---|
 | 266 |       int n = b->ndat();
 | 
|---|
 | 267 |       const double *dat1 = &a[b->istart];
 | 
|---|
 | 268 |       double *dat2 = b->dat();
 | 
|---|
 | 269 |       for (int i=0; i<n; i++) {
 | 
|---|
 | 270 |           dat2[i] = dat1[i];
 | 
|---|
 | 271 |         }
 | 
|---|
 | 272 |     }
 | 
|---|
 | 273 | }
 | 
|---|
 | 274 | 
 | 
|---|
 | 275 | double
 | 
|---|
 | 276 | DistSCVector::scalar_product(SCVector*a)
 | 
|---|
 | 277 | {
 | 
|---|
 | 278 |   // make sure that the argument is of the correct type
 | 
|---|
 | 279 |   DistSCVector* la
 | 
|---|
 | 280 |     = require_dynamic_cast<DistSCVector*>(a,"DistSCVector::scalar_product");
 | 
|---|
 | 281 | 
 | 
|---|
 | 282 |   // make sure that the dimensions match
 | 
|---|
 | 283 |   if (!dim()->equiv(la->dim())) {
 | 
|---|
 | 284 |       ExEnv::errn() << indent << "DistSCVector::scalar_product(SCVector*a): "
 | 
|---|
 | 285 |            << "dimensions don't match\n";
 | 
|---|
 | 286 |       abort();
 | 
|---|
 | 287 |     }
 | 
|---|
 | 288 | 
 | 
|---|
 | 289 |   double result = 0.0;
 | 
|---|
 | 290 |   SCMatrixBlockListIter i1, i2;
 | 
|---|
 | 291 |   for (i1=la->blocklist->begin(),i2=blocklist->begin();
 | 
|---|
 | 292 |        i1!=la->blocklist->end() && i2!=blocklist->end();
 | 
|---|
 | 293 |        i1++,i2++) {
 | 
|---|
 | 294 |       int n = i1.block()->ndat();
 | 
|---|
 | 295 |       if (n != i2.block()->ndat()) {
 | 
|---|
 | 296 |           ExEnv::errn() << indent << "DistSCVector::scalar_product: block mismatch: "
 | 
|---|
 | 297 |                << "internal error" << endl;
 | 
|---|
 | 298 |           abort();
 | 
|---|
 | 299 |         }
 | 
|---|
 | 300 |       double *dat1 = i1.block()->dat();
 | 
|---|
 | 301 |       double *dat2 = i2.block()->dat();
 | 
|---|
 | 302 |       for (int i=0; i<n; i++) {
 | 
|---|
 | 303 |           result += dat2[i] * dat1[i];
 | 
|---|
 | 304 |         }
 | 
|---|
 | 305 |     }
 | 
|---|
 | 306 |   messagegrp()->sum(result);
 | 
|---|
 | 307 |   return result;
 | 
|---|
 | 308 | }
 | 
|---|
 | 309 | 
 | 
|---|
 | 310 | void
 | 
|---|
 | 311 | DistSCVector::element_op(const Ref<SCElementOp>& op)
 | 
|---|
 | 312 | {
 | 
|---|
 | 313 |   SCMatrixBlockListIter i;
 | 
|---|
 | 314 |   for (i = blocklist->begin(); i != blocklist->end(); i++) {
 | 
|---|
 | 315 |       op->process_base(i.block());
 | 
|---|
 | 316 |     }
 | 
|---|
 | 317 |   if (op->has_collect()) op->collect(messagegrp());
 | 
|---|
 | 318 | }
 | 
|---|
 | 319 | 
 | 
|---|
 | 320 | void
 | 
|---|
 | 321 | DistSCVector::element_op(const Ref<SCElementOp2>& op,
 | 
|---|
 | 322 |                           SCVector* m)
 | 
|---|
 | 323 | {
 | 
|---|
 | 324 |   DistSCVector *lm
 | 
|---|
 | 325 |       = require_dynamic_cast<DistSCVector*>(m, "DistSCVector::element_op");
 | 
|---|
 | 326 | 
 | 
|---|
 | 327 |   if (!dim()->equiv(lm->dim())) {
 | 
|---|
 | 328 |       ExEnv::errn() << indent << "DistSCVector: bad element_op\n";
 | 
|---|
 | 329 |       abort();
 | 
|---|
 | 330 |     }
 | 
|---|
 | 331 | 
 | 
|---|
 | 332 |   SCMatrixBlockListIter i, j;
 | 
|---|
 | 333 |   for (i = blocklist->begin(), j = lm->blocklist->begin();
 | 
|---|
 | 334 |        i != blocklist->end();
 | 
|---|
 | 335 |        i++, j++) {
 | 
|---|
 | 336 |       op->process_base(i.block(), j.block());
 | 
|---|
 | 337 |     }
 | 
|---|
 | 338 |   if (op->has_collect()) op->collect(messagegrp());
 | 
|---|
 | 339 | }
 | 
|---|
 | 340 | 
 | 
|---|
 | 341 | void
 | 
|---|
 | 342 | DistSCVector::element_op(const Ref<SCElementOp3>& op,
 | 
|---|
 | 343 |                           SCVector* m,SCVector* n)
 | 
|---|
 | 344 | {
 | 
|---|
 | 345 |   DistSCVector *lm
 | 
|---|
 | 346 |       = require_dynamic_cast<DistSCVector*>(m, "DistSCVector::element_op");
 | 
|---|
 | 347 |   DistSCVector *ln
 | 
|---|
 | 348 |       = require_dynamic_cast<DistSCVector*>(n, "DistSCVector::element_op");
 | 
|---|
 | 349 | 
 | 
|---|
 | 350 |   if (!dim()->equiv(lm->dim()) || !dim()->equiv(ln->dim())) {
 | 
|---|
 | 351 |       ExEnv::errn() << indent << "DistSCVector: bad element_op\n";
 | 
|---|
 | 352 |       abort();
 | 
|---|
 | 353 |     }
 | 
|---|
 | 354 |   SCMatrixBlockListIter i, j, k;
 | 
|---|
 | 355 |   for (i = blocklist->begin(),
 | 
|---|
 | 356 |            j = lm->blocklist->begin(),
 | 
|---|
 | 357 |            k = ln->blocklist->begin();
 | 
|---|
 | 358 |        i != blocklist->end();
 | 
|---|
 | 359 |        i++, j++, k++) {
 | 
|---|
 | 360 |       op->process_base(i.block(), j.block(), k.block());
 | 
|---|
 | 361 |     }
 | 
|---|
 | 362 |   if (op->has_collect()) op->collect(messagegrp());
 | 
|---|
 | 363 | }
 | 
|---|
 | 364 | 
 | 
|---|
 | 365 | void
 | 
|---|
 | 366 | DistSCVector::accumulate_product_rv(SCMatrix *pa, SCVector *pb)
 | 
|---|
 | 367 | {
 | 
|---|
 | 368 |   const char* name = "DistSCMatrix::accumulate_product_rv";
 | 
|---|
 | 369 |   // make sure that the arguments are of the correct type
 | 
|---|
 | 370 |   DistSCMatrix* a = require_dynamic_cast<DistSCMatrix*>(pa,name);
 | 
|---|
 | 371 |   DistSCVector* b = require_dynamic_cast<DistSCVector*>(pb,name);
 | 
|---|
 | 372 | 
 | 
|---|
 | 373 |   // make sure that the dimensions match
 | 
|---|
 | 374 |   if (!dim()->equiv(a->rowdim()) || !a->coldim()->equiv(b->dim())) {
 | 
|---|
 | 375 |       ExEnv::errn() << indent
 | 
|---|
 | 376 |            << "DistSCVector::accumulate_product_rv(SCMatrix*a,SCVector*b): "
 | 
|---|
 | 377 |            << "dimensions don't match\n";
 | 
|---|
 | 378 |       abort();
 | 
|---|
 | 379 |     }
 | 
|---|
 | 380 | 
 | 
|---|
 | 381 |   a->create_vecform(DistSCMatrix::Row);
 | 
|---|
 | 382 |   a->vecform_op(DistSCMatrix::CopyToVec);
 | 
|---|
 | 383 | 
 | 
|---|
 | 384 |   int n = dim()->n();
 | 
|---|
 | 385 |   double *res = new double[n];
 | 
|---|
 | 386 | 
 | 
|---|
 | 387 |   for (int i=0; i<n; i++) res[i] = 0.0;
 | 
|---|
 | 388 | 
 | 
|---|
 | 389 |   Ref<SCMatrixSubblockIter> I = b->all_blocks(SCMatrixSubblockIter::Read);
 | 
|---|
 | 390 |   for (I->begin(); I->ready(); I->next()) {
 | 
|---|
 | 391 |       Ref<SCVectorSimpleBlock> blk
 | 
|---|
 | 392 |           = dynamic_cast<SCVectorSimpleBlock*>(I->block());
 | 
|---|
 | 393 |       int n = blk->iend - blk->istart;
 | 
|---|
 | 394 |       int joff = blk->istart;
 | 
|---|
 | 395 |       double *data = blk->data;
 | 
|---|
 | 396 |       for (int i=0; i<a->nvec; i++) {
 | 
|---|
 | 397 |           double *aveci = a->vec[i];
 | 
|---|
 | 398 |           for (int j=0; j<n; j++) {
 | 
|---|
 | 399 |               res[i+a->vecoff] += aveci[j+joff]*data[j];
 | 
|---|
 | 400 |             }
 | 
|---|
 | 401 |         }
 | 
|---|
 | 402 |     }
 | 
|---|
 | 403 | 
 | 
|---|
 | 404 |   a->delete_vecform();
 | 
|---|
 | 405 | 
 | 
|---|
 | 406 |   messagegrp()->sum(res, n);
 | 
|---|
 | 407 |   I = local_blocks(SCMatrixSubblockIter::Accum);
 | 
|---|
 | 408 |   for (I->begin(); I->ready(); I->next()) {
 | 
|---|
 | 409 |       Ref<SCVectorSimpleBlock> blk
 | 
|---|
 | 410 |           = dynamic_cast<SCVectorSimpleBlock*>(I->block());
 | 
|---|
 | 411 |       int n = blk->iend - blk->istart;
 | 
|---|
 | 412 |       int ioff = blk->istart;
 | 
|---|
 | 413 |       double *data = blk->data;
 | 
|---|
 | 414 |       for (int i=0; i<n; i++) {
 | 
|---|
 | 415 |           data[i] += res[i+ioff];
 | 
|---|
 | 416 |         }
 | 
|---|
 | 417 |     }
 | 
|---|
 | 418 |   I = 0;
 | 
|---|
 | 419 | 
 | 
|---|
 | 420 |   delete[] res;
 | 
|---|
 | 421 | }
 | 
|---|
 | 422 | 
 | 
|---|
 | 423 | void
 | 
|---|
 | 424 | DistSCVector::convert(double *res) const
 | 
|---|
 | 425 | {
 | 
|---|
 | 426 |   int n = dim()->n();
 | 
|---|
 | 427 |   for (int i=0; i<n; i++) res[i] = 0.0;
 | 
|---|
 | 428 | 
 | 
|---|
 | 429 |   Ref<SCMatrixSubblockIter> I = ((DistSCVector*)this)->local_blocks(SCMatrixSubblockIter::Read);
 | 
|---|
 | 430 |   for (I->begin(); I->ready(); I->next()) {
 | 
|---|
 | 431 |       Ref<SCVectorSimpleBlock> blk
 | 
|---|
 | 432 |           = dynamic_cast<SCVectorSimpleBlock*>(I->block());
 | 
|---|
 | 433 |       int ni = blk->iend - blk->istart;
 | 
|---|
 | 434 |       int ioff = blk->istart;
 | 
|---|
 | 435 |       double *data = blk->data;
 | 
|---|
 | 436 |       for (int i=0; i<ni; i++) {
 | 
|---|
 | 437 |           res[i+ioff] = data[i];
 | 
|---|
 | 438 |         }
 | 
|---|
 | 439 |     }
 | 
|---|
 | 440 | 
 | 
|---|
 | 441 |   messagegrp()->sum(res, n);
 | 
|---|
 | 442 | }
 | 
|---|
 | 443 | 
 | 
|---|
 | 444 | void
 | 
|---|
 | 445 | DistSCVector::convert(SCVector *v)
 | 
|---|
 | 446 | {
 | 
|---|
 | 447 |   SCVector::convert(v);
 | 
|---|
 | 448 | }
 | 
|---|
 | 449 | 
 | 
|---|
 | 450 | Ref<SCMatrixSubblockIter>
 | 
|---|
 | 451 | DistSCVector::local_blocks(SCMatrixSubblockIter::Access access)
 | 
|---|
 | 452 | {
 | 
|---|
 | 453 |   return new SCMatrixListSubblockIter(access, blocklist);
 | 
|---|
 | 454 | }
 | 
|---|
 | 455 | 
 | 
|---|
 | 456 | Ref<SCMatrixSubblockIter>
 | 
|---|
 | 457 | DistSCVector::all_blocks(SCMatrixSubblockIter::Access access)
 | 
|---|
 | 458 | {
 | 
|---|
 | 459 |   return new DistSCMatrixListSubblockIter(access, blocklist, messagegrp());
 | 
|---|
 | 460 | }
 | 
|---|
 | 461 | 
 | 
|---|
 | 462 | void
 | 
|---|
 | 463 | DistSCVector::vprint(const char *title, ostream& os, int prec) const
 | 
|---|
 | 464 | {
 | 
|---|
 | 465 |   double *data = new double[dim()->n()];
 | 
|---|
 | 466 |   convert(data);
 | 
|---|
 | 467 | 
 | 
|---|
 | 468 |   int i;
 | 
|---|
 | 469 |   int lwidth;
 | 
|---|
 | 470 |   double max=this->maxabs();
 | 
|---|
 | 471 | 
 | 
|---|
 | 472 |   max = (max==0.0) ? 1.0 : log10(max);
 | 
|---|
 | 473 |   if (max < 0.0) max=1.0;
 | 
|---|
 | 474 | 
 | 
|---|
 | 475 |   lwidth = prec+5+(int) max;
 | 
|---|
 | 476 | 
 | 
|---|
 | 477 |   os.setf(ios::fixed,ios::floatfield); os.precision(prec);
 | 
|---|
 | 478 |   os.setf(ios::right,ios::adjustfield);
 | 
|---|
 | 479 | 
 | 
|---|
 | 480 |   if (messagegrp()->me() == 0) {
 | 
|---|
 | 481 |     if (title)
 | 
|---|
 | 482 |       os << endl << indent << title << endl;
 | 
|---|
 | 483 |     else
 | 
|---|
 | 484 |       os << endl;
 | 
|---|
 | 485 | 
 | 
|---|
 | 486 |     if (n()==0) {
 | 
|---|
 | 487 |       os << indent << "empty vector\n";
 | 
|---|
 | 488 |       return;
 | 
|---|
 | 489 |     }
 | 
|---|
 | 490 | 
 | 
|---|
 | 491 |     for (i=0; i<n(); i++)
 | 
|---|
 | 492 |       os << indent << setw(5) << i+1 << setw(lwidth) << data[i] << endl;
 | 
|---|
 | 493 |     os << endl;
 | 
|---|
 | 494 | 
 | 
|---|
 | 495 |     os.flush();
 | 
|---|
 | 496 |   }
 | 
|---|
 | 497 | 
 | 
|---|
 | 498 |   delete[] data;
 | 
|---|
 | 499 | }
 | 
|---|
 | 500 | 
 | 
|---|
 | 501 | void
 | 
|---|
 | 502 | DistSCVector::error(const char *msg)
 | 
|---|
 | 503 | {
 | 
|---|
 | 504 |   ExEnv::errn() << indent << "DistSCVector: error: " << msg << endl;
 | 
|---|
 | 505 | }
 | 
|---|
 | 506 | 
 | 
|---|
 | 507 | Ref<DistSCMatrixKit>
 | 
|---|
 | 508 | DistSCVector::skit()
 | 
|---|
 | 509 | {
 | 
|---|
 | 510 |   return dynamic_cast<DistSCMatrixKit*>(kit().pointer());
 | 
|---|
 | 511 | }
 | 
|---|
 | 512 | 
 | 
|---|
 | 513 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
 | 514 | 
 | 
|---|
 | 515 | // Local Variables:
 | 
|---|
 | 516 | // mode: c++
 | 
|---|
 | 517 | // c-file-style: "CLJ"
 | 
|---|
 | 518 | // End:
 | 
|---|