| [0b990d] | 1 | //
 | 
|---|
 | 2 | // repldiag.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 | // ReplDiagSCMatrix member functions
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | static ClassDesc ReplDiagSCMatrix_cd(
 | 
|---|
 | 46 |   typeid(ReplDiagSCMatrix),"ReplDiagSCMatrix",1,"public DiagSCMatrix",
 | 
|---|
 | 47 |   0, 0, 0);
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 | ReplDiagSCMatrix::ReplDiagSCMatrix(const RefSCDimension&a,ReplSCMatrixKit*k):
 | 
|---|
 | 50 |   DiagSCMatrix(a,k)
 | 
|---|
 | 51 | {
 | 
|---|
 | 52 |   matrix = new double[a->n()];
 | 
|---|
 | 53 |   init_blocklist();
 | 
|---|
 | 54 | }
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 | void
 | 
|---|
 | 57 | ReplDiagSCMatrix::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(&matrix[d->blocks()->start(i)], 0,
 | 
|---|
 | 66 |              sizeof(double)*(d->blocks()->fence(i)
 | 
|---|
 | 67 |                              - d->blocks()->start(i)));
 | 
|---|
 | 68 |     }
 | 
|---|
 | 69 | }
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 | void
 | 
|---|
 | 72 | ReplDiagSCMatrix::after_elemop()
 | 
|---|
 | 73 | {
 | 
|---|
 | 74 |   messagegrp()->sum(matrix, d->n());
 | 
|---|
 | 75 | }
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 | void
 | 
|---|
 | 78 | ReplDiagSCMatrix::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 SCMatrixDiagSubBlock(d->blocks()->start(i),
 | 
|---|
 | 88 |                                    d->blocks()->fence(i),
 | 
|---|
 | 89 |                                    d->blocks()->start(i),
 | 
|---|
 | 90 |                                    matrix));
 | 
|---|
 | 91 |     }
 | 
|---|
 | 92 | }
 | 
|---|
 | 93 | 
 | 
|---|
 | 94 | ReplDiagSCMatrix::~ReplDiagSCMatrix()
 | 
|---|
 | 95 | {
 | 
|---|
 | 96 |   if (matrix)
 | 
|---|
 | 97 |       delete[] matrix;
 | 
|---|
 | 98 |   matrix=0;
 | 
|---|
 | 99 | }
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 | double
 | 
|---|
 | 102 | ReplDiagSCMatrix::get_element(int i) const
 | 
|---|
 | 103 | {
 | 
|---|
 | 104 |   return matrix[i];
 | 
|---|
 | 105 | }
 | 
|---|
 | 106 | 
 | 
|---|
 | 107 | void
 | 
|---|
 | 108 | ReplDiagSCMatrix::set_element(int i,double a)
 | 
|---|
 | 109 | {
 | 
|---|
 | 110 |   matrix[i] = a;
 | 
|---|
 | 111 | }
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 | void
 | 
|---|
 | 114 | ReplDiagSCMatrix::accumulate_element(int i,double a)
 | 
|---|
 | 115 | {
 | 
|---|
 | 116 |   matrix[i] += a;
 | 
|---|
 | 117 | }
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 | void
 | 
|---|
 | 120 | ReplDiagSCMatrix::assign_val(double val)
 | 
|---|
 | 121 | {
 | 
|---|
 | 122 |   int n = d->n();
 | 
|---|
 | 123 |   for (int i=0; i<n; i++) matrix[i] = val;
 | 
|---|
 | 124 | }
 | 
|---|
 | 125 | 
 | 
|---|
 | 126 | void
 | 
|---|
 | 127 | ReplDiagSCMatrix::accumulate(const DiagSCMatrix*a)
 | 
|---|
 | 128 | {
 | 
|---|
 | 129 |   // make sure that the argument is of the correct type
 | 
|---|
 | 130 |   const ReplDiagSCMatrix* la
 | 
|---|
 | 131 |     = require_dynamic_cast<const ReplDiagSCMatrix*>(a,"ReplDiagSCMatrix::accumulate");
 | 
|---|
 | 132 | 
 | 
|---|
 | 133 |   // make sure that the dimensions match
 | 
|---|
 | 134 |   if (!dim()->equiv(la->dim())) {
 | 
|---|
 | 135 |       ExEnv::errn() << indent << "ReplDiagSCMatrix::accumulate(SCMatrix*a): "
 | 
|---|
 | 136 |            << "dimensions don't match\n";
 | 
|---|
 | 137 |       abort();
 | 
|---|
 | 138 |     }
 | 
|---|
 | 139 | 
 | 
|---|
 | 140 |   int nelem = n();
 | 
|---|
 | 141 |   for (int i=0; i<nelem; i++) matrix[i] += la->matrix[i];
 | 
|---|
 | 142 | }
 | 
|---|
 | 143 | 
 | 
|---|
 | 144 | double
 | 
|---|
 | 145 | ReplDiagSCMatrix::invert_this()
 | 
|---|
 | 146 | {
 | 
|---|
 | 147 |   double det = 1.0;
 | 
|---|
 | 148 |   int nelem = n();
 | 
|---|
 | 149 |   for (int i=0; i<nelem; i++) {
 | 
|---|
 | 150 |       det *= matrix[i];
 | 
|---|
 | 151 |       matrix[i] = 1.0/matrix[i];
 | 
|---|
 | 152 |     }
 | 
|---|
 | 153 |   return det;
 | 
|---|
 | 154 | }
 | 
|---|
 | 155 | 
 | 
|---|
 | 156 | double
 | 
|---|
 | 157 | ReplDiagSCMatrix::determ_this()
 | 
|---|
 | 158 | {
 | 
|---|
 | 159 |   double det = 1.0;
 | 
|---|
 | 160 |   int nelem = n();
 | 
|---|
 | 161 |   for (int i=0; i < nelem; i++) {
 | 
|---|
 | 162 |     det *= matrix[i];
 | 
|---|
 | 163 |   }
 | 
|---|
 | 164 |   return det;
 | 
|---|
 | 165 | }
 | 
|---|
 | 166 | 
 | 
|---|
 | 167 | double
 | 
|---|
 | 168 | ReplDiagSCMatrix::trace()
 | 
|---|
 | 169 | {
 | 
|---|
 | 170 |   double tr = 0;
 | 
|---|
 | 171 |   int nelem = n();
 | 
|---|
 | 172 |   for (int i=0; i < nelem; i++) {
 | 
|---|
 | 173 |     tr += matrix[i];
 | 
|---|
 | 174 |   }
 | 
|---|
 | 175 |   return tr;
 | 
|---|
 | 176 | }
 | 
|---|
 | 177 | 
 | 
|---|
 | 178 | void
 | 
|---|
 | 179 | ReplDiagSCMatrix::gen_invert_this()
 | 
|---|
 | 180 | {
 | 
|---|
 | 181 |   int nelem = n();
 | 
|---|
 | 182 |   for (int i=0; i < nelem; i++) {
 | 
|---|
 | 183 |     if (fabs(matrix[i]) > 1.0e-8)
 | 
|---|
 | 184 |       matrix[i] = 1.0/matrix[i];
 | 
|---|
 | 185 |     else
 | 
|---|
 | 186 |       matrix[i] = 0;
 | 
|---|
 | 187 |   }
 | 
|---|
 | 188 | }
 | 
|---|
 | 189 | 
 | 
|---|
 | 190 | void
 | 
|---|
 | 191 | ReplDiagSCMatrix::element_op(const Ref<SCElementOp>& op)
 | 
|---|
 | 192 | {
 | 
|---|
 | 193 |   if (op->has_side_effects()) before_elemop();
 | 
|---|
 | 194 |   SCMatrixBlockListIter i;
 | 
|---|
 | 195 |   for (i = blocklist->begin(); i != blocklist->end(); i++) {
 | 
|---|
 | 196 |       op->process_base(i.block());
 | 
|---|
 | 197 |     }
 | 
|---|
 | 198 |   if (op->has_side_effects()) after_elemop();
 | 
|---|
 | 199 |   if (op->has_collect()) op->collect(messagegrp());
 | 
|---|
 | 200 | }
 | 
|---|
 | 201 | 
 | 
|---|
 | 202 | void
 | 
|---|
 | 203 | ReplDiagSCMatrix::element_op(const Ref<SCElementOp2>& op,
 | 
|---|
 | 204 |                               DiagSCMatrix* m)
 | 
|---|
 | 205 | {
 | 
|---|
 | 206 |   ReplDiagSCMatrix *lm
 | 
|---|
 | 207 |       = require_dynamic_cast<ReplDiagSCMatrix*>(m,"ReplDiagSCMatrix::element_op");
 | 
|---|
 | 208 | 
 | 
|---|
 | 209 |   if (!dim()->equiv(lm->dim())) {
 | 
|---|
 | 210 |       ExEnv::errn() << indent << "ReplDiagSCMatrix: bad element_op\n";
 | 
|---|
 | 211 |       abort();
 | 
|---|
 | 212 |     }
 | 
|---|
 | 213 |   if (op->has_side_effects()) before_elemop();
 | 
|---|
 | 214 |   if (op->has_side_effects_in_arg()) lm->before_elemop();
 | 
|---|
 | 215 |   SCMatrixBlockListIter i, j;
 | 
|---|
 | 216 |   for (i = blocklist->begin(), j = lm->blocklist->begin();
 | 
|---|
 | 217 |        i != blocklist->end();
 | 
|---|
 | 218 |        i++, j++) {
 | 
|---|
 | 219 |       op->process_base(i.block(), j.block());
 | 
|---|
 | 220 |     }
 | 
|---|
 | 221 |   if (op->has_side_effects()) after_elemop();
 | 
|---|
 | 222 |   if (op->has_side_effects_in_arg()) lm->after_elemop();
 | 
|---|
 | 223 |   if (op->has_collect()) op->collect(messagegrp());
 | 
|---|
 | 224 | }
 | 
|---|
 | 225 | 
 | 
|---|
 | 226 | void
 | 
|---|
 | 227 | ReplDiagSCMatrix::element_op(const Ref<SCElementOp3>& op,
 | 
|---|
 | 228 |                               DiagSCMatrix* m,DiagSCMatrix* n)
 | 
|---|
 | 229 | {
 | 
|---|
 | 230 |   ReplDiagSCMatrix *lm
 | 
|---|
 | 231 |       = require_dynamic_cast<ReplDiagSCMatrix*>(m,"ReplDiagSCMatrix::element_op");
 | 
|---|
 | 232 |   ReplDiagSCMatrix *ln
 | 
|---|
 | 233 |       = require_dynamic_cast<ReplDiagSCMatrix*>(n,"ReplDiagSCMatrix::element_op");
 | 
|---|
 | 234 | 
 | 
|---|
 | 235 |   if (!dim()->equiv(lm->dim()) || !dim()->equiv(ln->dim())) {
 | 
|---|
 | 236 |       ExEnv::errn() << indent << "ReplDiagSCMatrix: bad element_op\n";
 | 
|---|
 | 237 |       abort();
 | 
|---|
 | 238 |     }
 | 
|---|
 | 239 |   if (op->has_side_effects()) before_elemop();
 | 
|---|
 | 240 |   if (op->has_side_effects_in_arg1()) lm->before_elemop();
 | 
|---|
 | 241 |   if (op->has_side_effects_in_arg2()) ln->before_elemop();
 | 
|---|
 | 242 |   SCMatrixBlockListIter i, j, k;
 | 
|---|
 | 243 |   for (i = blocklist->begin(),
 | 
|---|
 | 244 |            j = lm->blocklist->begin(),
 | 
|---|
 | 245 |            k = ln->blocklist->begin();
 | 
|---|
 | 246 |        i != blocklist->end();
 | 
|---|
 | 247 |        i++, j++, k++) {
 | 
|---|
 | 248 |       op->process_base(i.block(), j.block(), k.block());
 | 
|---|
 | 249 |     }
 | 
|---|
 | 250 |   if (op->has_side_effects()) after_elemop();
 | 
|---|
 | 251 |   if (op->has_side_effects_in_arg1()) lm->after_elemop();
 | 
|---|
 | 252 |   if (op->has_side_effects_in_arg2()) ln->after_elemop();
 | 
|---|
 | 253 |   if (op->has_collect()) op->collect(messagegrp());
 | 
|---|
 | 254 | }
 | 
|---|
 | 255 | 
 | 
|---|
 | 256 | // from Ed Seidl at the NIH (with a bit of hacking)
 | 
|---|
 | 257 | void
 | 
|---|
 | 258 | ReplDiagSCMatrix::vprint(const char *title, ostream& os, int prec) const
 | 
|---|
 | 259 | {
 | 
|---|
 | 260 |   int i;
 | 
|---|
 | 261 |   int lwidth;
 | 
|---|
 | 262 |   double max=this->maxabs();
 | 
|---|
 | 263 | 
 | 
|---|
 | 264 |   if (messagegrp()->me() != 0) return;
 | 
|---|
 | 265 | 
 | 
|---|
 | 266 |   max = (max==0.0) ? 1.0 : log10(max);
 | 
|---|
 | 267 |   if (max < 0.0) max=1.0;
 | 
|---|
 | 268 | 
 | 
|---|
 | 269 |   lwidth = prec+5+(int) max;
 | 
|---|
 | 270 | 
 | 
|---|
 | 271 |   os.setf(ios::fixed,ios::floatfield); os.precision(prec);
 | 
|---|
 | 272 |   os.setf(ios::right,ios::adjustfield);
 | 
|---|
 | 273 | 
 | 
|---|
 | 274 |   if (title)
 | 
|---|
 | 275 |     os << endl << indent << title << endl;
 | 
|---|
 | 276 |   else
 | 
|---|
 | 277 |     os << endl;
 | 
|---|
 | 278 | 
 | 
|---|
 | 279 |   if (n()==0) {
 | 
|---|
 | 280 |     os << indent << "empty matrix\n";
 | 
|---|
 | 281 |     return;
 | 
|---|
 | 282 |   }
 | 
|---|
 | 283 | 
 | 
|---|
 | 284 |   for (i=0; i<n(); i++)
 | 
|---|
 | 285 |     os << indent << setw(5) << i+1 << setw(lwidth) << matrix[i] << endl;
 | 
|---|
 | 286 |   os << endl;
 | 
|---|
 | 287 | 
 | 
|---|
 | 288 |   os.flush();
 | 
|---|
 | 289 | }
 | 
|---|
 | 290 | 
 | 
|---|
 | 291 | Ref<SCMatrixSubblockIter>
 | 
|---|
 | 292 | ReplDiagSCMatrix::local_blocks(SCMatrixSubblockIter::Access access)
 | 
|---|
 | 293 | {
 | 
|---|
 | 294 |   return new ReplSCMatrixListSubblockIter(access, blocklist,
 | 
|---|
 | 295 |                                           messagegrp(),
 | 
|---|
 | 296 |                                           matrix, d->n());
 | 
|---|
 | 297 | }
 | 
|---|
 | 298 | 
 | 
|---|
 | 299 | Ref<SCMatrixSubblockIter>
 | 
|---|
 | 300 | ReplDiagSCMatrix::all_blocks(SCMatrixSubblockIter::Access access)
 | 
|---|
 | 301 | {
 | 
|---|
 | 302 |   if (access == SCMatrixSubblockIter::Write) {
 | 
|---|
 | 303 |       ExEnv::errn() << "ReplDiagSCMatrix::all_blocks: "
 | 
|---|
 | 304 |            << "Write access permitted for local blocks only"
 | 
|---|
 | 305 |            << endl;
 | 
|---|
 | 306 |       abort();
 | 
|---|
 | 307 |     }
 | 
|---|
 | 308 |   Ref<SCMatrixBlockList> allblocklist = new SCMatrixBlockList();
 | 
|---|
 | 309 |   allblocklist->insert(new SCMatrixDiagSubBlock(0, d->n(), 0, matrix));
 | 
|---|
 | 310 |   return new ReplSCMatrixListSubblockIter(access, allblocklist,
 | 
|---|
 | 311 |                                           messagegrp(),
 | 
|---|
 | 312 |                                           matrix, d->n());
 | 
|---|
 | 313 | }
 | 
|---|
 | 314 | 
 | 
|---|
 | 315 | Ref<ReplSCMatrixKit>
 | 
|---|
 | 316 | ReplDiagSCMatrix::skit()
 | 
|---|
 | 317 | {
 | 
|---|
 | 318 |   return dynamic_cast<ReplSCMatrixKit*>(kit().pointer());
 | 
|---|
 | 319 | }
 | 
|---|
 | 320 | 
 | 
|---|
 | 321 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
 | 322 | 
 | 
|---|
 | 323 | // Local Variables:
 | 
|---|
 | 324 | // mode: c++
 | 
|---|
 | 325 | // c-file-style: "CLJ"
 | 
|---|
 | 326 | // End:
 | 
|---|