| 1 | // | 
|---|
| 2 | // replvect.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/repl.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 | // ReplSCVector member functions | 
|---|
| 44 |  | 
|---|
| 45 | static ClassDesc ReplSCVector_cd( | 
|---|
| 46 | typeid(ReplSCVector),"ReplSCVector",1,"public SCVector", | 
|---|
| 47 | 0, 0, 0); | 
|---|
| 48 |  | 
|---|
| 49 | ReplSCVector::ReplSCVector(const RefSCDimension&a,ReplSCMatrixKit*k): | 
|---|
| 50 | SCVector(a,k) | 
|---|
| 51 | { | 
|---|
| 52 | vector = new double[a->n()]; | 
|---|
| 53 | init_blocklist(); | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | void | 
|---|
| 57 | ReplSCVector::before_elemop() | 
|---|
| 58 | { | 
|---|
| 59 | // zero out the blocks not in my block list | 
|---|
| 60 | int i; | 
|---|
| 61 | int nproc = messagegrp()->n(); | 
|---|
| 62 | int me = messagegrp()->me(); | 
|---|
| 63 | for (i=0; i<d->blocks()->nblock(); i++) { | 
|---|
| 64 | if (i%nproc == me) continue; | 
|---|
| 65 | memset(&vector[d->blocks()->start(i)], 0, | 
|---|
| 66 | sizeof(double)*(d->blocks()->fence(i) | 
|---|
| 67 | - d->blocks()->start(i))); | 
|---|
| 68 | } | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | void | 
|---|
| 72 | ReplSCVector::after_elemop() | 
|---|
| 73 | { | 
|---|
| 74 | messagegrp()->sum(vector, d->n()); | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | void | 
|---|
| 78 | ReplSCVector::init_blocklist() | 
|---|
| 79 | { | 
|---|
| 80 | int i; | 
|---|
| 81 | int nproc = messagegrp()->n(); | 
|---|
| 82 | int me = messagegrp()->me(); | 
|---|
| 83 | blocklist = new SCMatrixBlockList; | 
|---|
| 84 | for (i=0; i<d->blocks()->nblock(); i++) { | 
|---|
| 85 | if (i%nproc != me) continue; | 
|---|
| 86 | blocklist->insert( | 
|---|
| 87 | new SCVectorSimpleSubBlock(d->blocks()->start(i), | 
|---|
| 88 | d->blocks()->fence(i), | 
|---|
| 89 | d->blocks()->start(i), | 
|---|
| 90 | vector)); | 
|---|
| 91 | } | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | ReplSCVector::~ReplSCVector() | 
|---|
| 95 | { | 
|---|
| 96 | if (vector) | 
|---|
| 97 | delete[] vector; | 
|---|
| 98 | vector=0; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | double | 
|---|
| 102 | ReplSCVector::get_element(int i) const | 
|---|
| 103 | { | 
|---|
| 104 | return vector[i]; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | void | 
|---|
| 108 | ReplSCVector::set_element(int i,double a) | 
|---|
| 109 | { | 
|---|
| 110 | vector[i] = a; | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | void | 
|---|
| 114 | ReplSCVector::accumulate_element(int i,double a) | 
|---|
| 115 | { | 
|---|
| 116 | vector[i] += a; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | void | 
|---|
| 120 | ReplSCVector::accumulate_product_rv(SCMatrix*a,SCVector*b) | 
|---|
| 121 | { | 
|---|
| 122 | const char* name = "ReplSCVector::accumulate_product_rv"; | 
|---|
| 123 | // make sure that the arguments are of the correct type | 
|---|
| 124 | ReplSCMatrix* la = require_dynamic_cast<ReplSCMatrix*>(a,name); | 
|---|
| 125 | ReplSCVector* lb = require_dynamic_cast<ReplSCVector*>(b,name); | 
|---|
| 126 |  | 
|---|
| 127 | // make sure that the dimensions match | 
|---|
| 128 | if (!dim()->equiv(la->rowdim()) || !la->coldim()->equiv(lb->dim())) { | 
|---|
| 129 | ExEnv::out0() << indent << "dim():" << endl << incindent; | 
|---|
| 130 | dim().print(); | 
|---|
| 131 | ExEnv::out0() << decindent; | 
|---|
| 132 | ExEnv::out0() << indent << "la->rowdim():" << endl << incindent; | 
|---|
| 133 | la->rowdim().print(); | 
|---|
| 134 | ExEnv::out0() << decindent; | 
|---|
| 135 | ExEnv::out0() << indent << "la->coldim():" << endl << incindent; | 
|---|
| 136 | la->coldim().print(); | 
|---|
| 137 | ExEnv::out0() << decindent; | 
|---|
| 138 | ExEnv::out0() << indent << "lb->dim():" << endl << incindent; | 
|---|
| 139 | lb->dim().print(); | 
|---|
| 140 | ExEnv::out0() << decindent; | 
|---|
| 141 | ExEnv::out0() << indent | 
|---|
| 142 | << "ReplSCVector::accumulate_product_rv(SCMatrix*a,SCVector*b): " | 
|---|
| 143 | << "dimensions don't match\n"; | 
|---|
| 144 | abort(); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | cmat_mxm(la->rows, 0, | 
|---|
| 148 | &lb->vector, 1, | 
|---|
| 149 | &vector, 1, | 
|---|
| 150 | n(), la->ncol(), 1, | 
|---|
| 151 | 1); | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | void | 
|---|
| 155 | ReplSCVector::accumulate_product_sv(SymmSCMatrix*a,SCVector*b) | 
|---|
| 156 | { | 
|---|
| 157 | const char* name = "ReplSCVector::accumulate_product_sv"; | 
|---|
| 158 | // make sure that the arguments are of the correct type | 
|---|
| 159 | ReplSymmSCMatrix* la = require_dynamic_cast<ReplSymmSCMatrix*>(a,name); | 
|---|
| 160 | ReplSCVector* lb = require_dynamic_cast<ReplSCVector*>(b,name); | 
|---|
| 161 |  | 
|---|
| 162 | // make sure that the dimensions match | 
|---|
| 163 | if (!dim()->equiv(la->dim()) || !la->dim()->equiv(lb->dim())) { | 
|---|
| 164 | ExEnv::errn() << indent | 
|---|
| 165 | << "ReplSCVector::accumulate_product_sv(SymmSCMatrix*a,SCVector*b): " | 
|---|
| 166 | << "dimensions don't match\n"; | 
|---|
| 167 | abort(); | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | double** adat = la->rows; | 
|---|
| 171 | double* bdat = lb->vector; | 
|---|
| 172 | double tmp; | 
|---|
| 173 | int n = dim()->n(); | 
|---|
| 174 | int i, j; | 
|---|
| 175 | for (i=0; i<n; i++) { | 
|---|
| 176 | tmp = 0.0; | 
|---|
| 177 | for (j=0; j<=i; j++) { | 
|---|
| 178 | tmp += adat[i][j] * bdat[j]; | 
|---|
| 179 | } | 
|---|
| 180 | for (; j<n; j++) { | 
|---|
| 181 | tmp += adat[j][i] * bdat[j]; | 
|---|
| 182 | } | 
|---|
| 183 | vector[i] += tmp; | 
|---|
| 184 | } | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | void | 
|---|
| 188 | ReplSCVector::accumulate(const SCVector*a) | 
|---|
| 189 | { | 
|---|
| 190 | // make sure that the argument is of the correct type | 
|---|
| 191 | const ReplSCVector* la | 
|---|
| 192 | = require_dynamic_cast<const ReplSCVector*>(a,"ReplSCVector::accumulate"); | 
|---|
| 193 |  | 
|---|
| 194 | // make sure that the dimensions match | 
|---|
| 195 | if (!dim()->equiv(la->dim())) { | 
|---|
| 196 | ExEnv::errn() << indent << "ReplSCVector::accumulate(SCVector*a): " | 
|---|
| 197 | << "dimensions don't match\n"; | 
|---|
| 198 | abort(); | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | int nelem = d->n(); | 
|---|
| 202 | int i; | 
|---|
| 203 | for (i=0; i<nelem; i++) vector[i] += la->vector[i]; | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | void | 
|---|
| 207 | ReplSCVector::accumulate(const SCMatrix*a) | 
|---|
| 208 | { | 
|---|
| 209 | // make sure that the argument is of the correct type | 
|---|
| 210 | const ReplSCMatrix *la | 
|---|
| 211 | = require_dynamic_cast<const ReplSCMatrix*>(a,"ReplSCVector::accumulate"); | 
|---|
| 212 |  | 
|---|
| 213 | // make sure that the dimensions match | 
|---|
| 214 | if (!((la->rowdim()->equiv(dim()) && la->coldim()->n() == 1) | 
|---|
| 215 | || (la->coldim()->equiv(dim()) && la->rowdim()->n() == 1))) { | 
|---|
| 216 | ExEnv::errn() << indent << "ReplSCVector::accumulate(SCMatrix*a): " | 
|---|
| 217 | << "dimensions don't match\n"; | 
|---|
| 218 | abort(); | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | int nelem = d->n(); | 
|---|
| 222 | int i; | 
|---|
| 223 | for (i=0; i<nelem; i++) vector[i] += la->matrix[i]; | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | void | 
|---|
| 227 | ReplSCVector::assign_val(double a) | 
|---|
| 228 | { | 
|---|
| 229 | int nelem = d->n(); | 
|---|
| 230 | int i; | 
|---|
| 231 | for (i=0; i<nelem; i++) vector[i] = a; | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | void | 
|---|
| 235 | ReplSCVector::assign_v(SCVector*a) | 
|---|
| 236 | { | 
|---|
| 237 | // make sure that the argument is of the correct type | 
|---|
| 238 | ReplSCVector* la | 
|---|
| 239 | = require_dynamic_cast<ReplSCVector*>(a,"ReplSCVector::assign_v"); | 
|---|
| 240 |  | 
|---|
| 241 | // make sure that the dimensions match | 
|---|
| 242 | if (!dim()->equiv(la->dim())) { | 
|---|
| 243 | ExEnv::errn() << indent << "ReplSCVector::assign_v(SCVector*a): " | 
|---|
| 244 | << "dimensions don't match\n"; | 
|---|
| 245 | abort(); | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | int nelem = d->n(); | 
|---|
| 249 | int i; | 
|---|
| 250 | for (i=0; i<nelem; i++) vector[i] = la->vector[i]; | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 | void | 
|---|
| 254 | ReplSCVector::assign_p(const double*a) | 
|---|
| 255 | { | 
|---|
| 256 | int nelem = d->n(); | 
|---|
| 257 | int i; | 
|---|
| 258 | for (i=0; i<nelem; i++) vector[i] = a[i]; | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | double | 
|---|
| 262 | ReplSCVector::scalar_product(SCVector*a) | 
|---|
| 263 | { | 
|---|
| 264 | // make sure that the argument is of the correct type | 
|---|
| 265 | ReplSCVector* la | 
|---|
| 266 | = require_dynamic_cast<ReplSCVector*>(a,"ReplSCVector::scalar_product"); | 
|---|
| 267 |  | 
|---|
| 268 | // make sure that the dimensions match | 
|---|
| 269 | if (!dim()->equiv(la->dim())) { | 
|---|
| 270 | ExEnv::errn() << indent << "ReplSCVector::scalar_product(SCVector*a): " | 
|---|
| 271 | << "dimensions don't match\n"; | 
|---|
| 272 | abort(); | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | int nelem = d->n(); | 
|---|
| 276 | int i; | 
|---|
| 277 | double result = 0.0; | 
|---|
| 278 | for (i=0; i<nelem; i++) result += vector[i] * la->vector[i]; | 
|---|
| 279 | return result; | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | void | 
|---|
| 283 | ReplSCVector::element_op(const Ref<SCElementOp>& op) | 
|---|
| 284 | { | 
|---|
| 285 | if (op->has_side_effects()) before_elemop(); | 
|---|
| 286 | SCMatrixBlockListIter i; | 
|---|
| 287 | for (i = blocklist->begin(); i != blocklist->end(); i++) { | 
|---|
| 288 | op->process_base(i.block()); | 
|---|
| 289 | } | 
|---|
| 290 | if (op->has_side_effects()) after_elemop(); | 
|---|
| 291 | if (op->has_collect()) op->collect(messagegrp()); | 
|---|
| 292 | } | 
|---|
| 293 |  | 
|---|
| 294 | void | 
|---|
| 295 | ReplSCVector::element_op(const Ref<SCElementOp2>& op, | 
|---|
| 296 | SCVector* m) | 
|---|
| 297 | { | 
|---|
| 298 | ReplSCVector *lm | 
|---|
| 299 | = require_dynamic_cast<ReplSCVector*>(m, "ReplSCVector::element_op"); | 
|---|
| 300 |  | 
|---|
| 301 | if (!dim()->equiv(lm->dim())) { | 
|---|
| 302 | ExEnv::errn() << indent << "ReplSCVector: bad element_op\n"; | 
|---|
| 303 | abort(); | 
|---|
| 304 | } | 
|---|
| 305 |  | 
|---|
| 306 | if (op->has_side_effects()) before_elemop(); | 
|---|
| 307 | if (op->has_side_effects_in_arg()) lm->before_elemop(); | 
|---|
| 308 | SCMatrixBlockListIter i, j; | 
|---|
| 309 | for (i = blocklist->begin(), j = lm->blocklist->begin(); | 
|---|
| 310 | i != blocklist->end(); | 
|---|
| 311 | i++, j++) { | 
|---|
| 312 | op->process_base(i.block(), j.block()); | 
|---|
| 313 | } | 
|---|
| 314 | if (op->has_side_effects()) after_elemop(); | 
|---|
| 315 | if (op->has_side_effects_in_arg()) lm->after_elemop(); | 
|---|
| 316 | if (op->has_collect()) op->collect(messagegrp()); | 
|---|
| 317 | } | 
|---|
| 318 |  | 
|---|
| 319 | void | 
|---|
| 320 | ReplSCVector::element_op(const Ref<SCElementOp3>& op, | 
|---|
| 321 | SCVector* m,SCVector* n) | 
|---|
| 322 | { | 
|---|
| 323 | ReplSCVector *lm | 
|---|
| 324 | = require_dynamic_cast<ReplSCVector*>(m, "ReplSCVector::element_op"); | 
|---|
| 325 | ReplSCVector *ln | 
|---|
| 326 | = require_dynamic_cast<ReplSCVector*>(n, "ReplSCVector::element_op"); | 
|---|
| 327 |  | 
|---|
| 328 | if (!dim()->equiv(lm->dim()) || !dim()->equiv(ln->dim())) { | 
|---|
| 329 | ExEnv::errn() << indent << "ReplSCVector: bad element_op\n"; | 
|---|
| 330 | abort(); | 
|---|
| 331 | } | 
|---|
| 332 | if (op->has_side_effects()) before_elemop(); | 
|---|
| 333 | if (op->has_side_effects_in_arg1()) lm->before_elemop(); | 
|---|
| 334 | if (op->has_side_effects_in_arg2()) ln->before_elemop(); | 
|---|
| 335 | SCMatrixBlockListIter i, j, k; | 
|---|
| 336 | for (i = blocklist->begin(), | 
|---|
| 337 | j = lm->blocklist->begin(), | 
|---|
| 338 | k = ln->blocklist->begin(); | 
|---|
| 339 | i != blocklist->end(); | 
|---|
| 340 | i++, j++, k++) { | 
|---|
| 341 | op->process_base(i.block(), j.block(), k.block()); | 
|---|
| 342 | } | 
|---|
| 343 | if (op->has_side_effects()) after_elemop(); | 
|---|
| 344 | if (op->has_side_effects_in_arg1()) lm->after_elemop(); | 
|---|
| 345 | if (op->has_side_effects_in_arg2()) ln->after_elemop(); | 
|---|
| 346 | if (op->has_collect()) op->collect(messagegrp()); | 
|---|
| 347 | } | 
|---|
| 348 |  | 
|---|
| 349 | // from Ed Seidl at the NIH (with a bit of hacking) | 
|---|
| 350 | void | 
|---|
| 351 | ReplSCVector::vprint(const char *title, ostream& os, int prec) const | 
|---|
| 352 | { | 
|---|
| 353 | int i; | 
|---|
| 354 | int lwidth; | 
|---|
| 355 | double max=this->maxabs(); | 
|---|
| 356 |  | 
|---|
| 357 | if (messagegrp()->me() != 0) return; | 
|---|
| 358 |  | 
|---|
| 359 | max = (max==0.0) ? 1.0 : log10(max); | 
|---|
| 360 | if (max < 0.0) max=1.0; | 
|---|
| 361 |  | 
|---|
| 362 | lwidth = prec + 5 + (int) max; | 
|---|
| 363 |  | 
|---|
| 364 | os.setf(ios::fixed,ios::floatfield); os.precision(prec); | 
|---|
| 365 | os.setf(ios::right,ios::adjustfield); | 
|---|
| 366 |  | 
|---|
| 367 | if (title) | 
|---|
| 368 | os << endl << indent << title << endl; | 
|---|
| 369 | else | 
|---|
| 370 | os << endl; | 
|---|
| 371 |  | 
|---|
| 372 | if (n()==0) { | 
|---|
| 373 | os << indent << "empty vector\n"; | 
|---|
| 374 | return; | 
|---|
| 375 | } | 
|---|
| 376 |  | 
|---|
| 377 | for (i=0; i < n(); i++) | 
|---|
| 378 | os << indent << setw(5) << i+1 << setw(lwidth) << vector[i] << endl; | 
|---|
| 379 | os << endl; | 
|---|
| 380 |  | 
|---|
| 381 | os.flush(); | 
|---|
| 382 | } | 
|---|
| 383 |  | 
|---|
| 384 | Ref<SCMatrixSubblockIter> | 
|---|
| 385 | ReplSCVector::local_blocks(SCMatrixSubblockIter::Access access) | 
|---|
| 386 | { | 
|---|
| 387 | return new ReplSCMatrixListSubblockIter(access, blocklist, | 
|---|
| 388 | messagegrp(), | 
|---|
| 389 | vector, d->n()); | 
|---|
| 390 | } | 
|---|
| 391 |  | 
|---|
| 392 | Ref<SCMatrixSubblockIter> | 
|---|
| 393 | ReplSCVector::all_blocks(SCMatrixSubblockIter::Access access) | 
|---|
| 394 | { | 
|---|
| 395 | if (access == SCMatrixSubblockIter::Write) { | 
|---|
| 396 | ExEnv::errn() << indent << "ReplSCVector::all_blocks: " | 
|---|
| 397 | << "Write access permitted for local blocks only" | 
|---|
| 398 | << endl; | 
|---|
| 399 | abort(); | 
|---|
| 400 | } | 
|---|
| 401 | Ref<SCMatrixBlockList> allblocklist = new SCMatrixBlockList(); | 
|---|
| 402 | allblocklist->insert(new SCVectorSimpleSubBlock(0, d->n(), 0, vector)); | 
|---|
| 403 | return new ReplSCMatrixListSubblockIter(access, allblocklist, | 
|---|
| 404 | messagegrp(), | 
|---|
| 405 | vector, d->n()); | 
|---|
| 406 | } | 
|---|
| 407 |  | 
|---|
| 408 | Ref<ReplSCMatrixKit> | 
|---|
| 409 | ReplSCVector::skit() | 
|---|
| 410 | { | 
|---|
| 411 | return dynamic_cast<ReplSCMatrixKit*>(kit().pointer()); | 
|---|
| 412 | } | 
|---|
| 413 |  | 
|---|
| 414 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 415 |  | 
|---|
| 416 | // Local Variables: | 
|---|
| 417 | // mode: c++ | 
|---|
| 418 | // c-file-style: "CLJ" | 
|---|
| 419 | // End: | 
|---|