| 1 | // | 
|---|
| 2 | // replrect.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 | #include <math/scmat/mops.h> | 
|---|
| 39 |  | 
|---|
| 40 | using namespace std; | 
|---|
| 41 | using namespace sc; | 
|---|
| 42 |  | 
|---|
| 43 | extern "C" { | 
|---|
| 44 | int sing_(double *q, int *lq, int *iq, double *s, double *p, | 
|---|
| 45 | int *lp, int *ip, double *a, int *la, int *m, int *n, | 
|---|
| 46 | double *w); | 
|---|
| 47 | }; | 
|---|
| 48 |  | 
|---|
| 49 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 50 | // ReplSCMatrix member functions | 
|---|
| 51 |  | 
|---|
| 52 | static ClassDesc ReplSCMatrix_cd( | 
|---|
| 53 | typeid(ReplSCMatrix),"ReplSCMatrix",1,"public SCMatrix", | 
|---|
| 54 | 0, 0, 0); | 
|---|
| 55 |  | 
|---|
| 56 | static double ** | 
|---|
| 57 | init_rect_rows(double *data, int ni,int nj) | 
|---|
| 58 | { | 
|---|
| 59 | double** r = new double*[ni]; | 
|---|
| 60 | int i; | 
|---|
| 61 | for (i=0; i<ni; i++) r[i] = &data[i*nj]; | 
|---|
| 62 | return r; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | ReplSCMatrix::ReplSCMatrix(const RefSCDimension&a,const RefSCDimension&b, | 
|---|
| 66 | ReplSCMatrixKit*k): | 
|---|
| 67 | SCMatrix(a,b,k) | 
|---|
| 68 | { | 
|---|
| 69 | int nr = a->n(); | 
|---|
| 70 | int nc = b->n(); | 
|---|
| 71 |  | 
|---|
| 72 | matrix = new double[nr*nc]; | 
|---|
| 73 |  | 
|---|
| 74 | rows = init_rect_rows(matrix,nr,nc); | 
|---|
| 75 |  | 
|---|
| 76 | init_blocklist(); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | void | 
|---|
| 80 | ReplSCMatrix::before_elemop() | 
|---|
| 81 | { | 
|---|
| 82 | // zero out the blocks not in my block list | 
|---|
| 83 | int i, j, index; | 
|---|
| 84 | int nc = d2->n(); | 
|---|
| 85 | int nproc = messagegrp()->n(); | 
|---|
| 86 | int me = messagegrp()->me(); | 
|---|
| 87 | for (i=0, index=0; i<d1->blocks()->nblock(); i++) { | 
|---|
| 88 | for (j=0; j<d2->blocks()->nblock(); j++, index++) { | 
|---|
| 89 | if (index%nproc == me) continue; | 
|---|
| 90 | for (int ii=d1->blocks()->start(i); | 
|---|
| 91 | ii<d1->blocks()->fence(i); ii++) { | 
|---|
| 92 | for (int jj=d2->blocks()->start(j); | 
|---|
| 93 | jj<d2->blocks()->fence(j); jj++) { | 
|---|
| 94 | matrix[ii*nc + jj] = 0.0; | 
|---|
| 95 | } | 
|---|
| 96 | } | 
|---|
| 97 | } | 
|---|
| 98 | } | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | void | 
|---|
| 102 | ReplSCMatrix::after_elemop() | 
|---|
| 103 | { | 
|---|
| 104 | messagegrp()->sum(matrix, d1->n()*d2->n()); | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | void | 
|---|
| 108 | ReplSCMatrix::init_blocklist() | 
|---|
| 109 | { | 
|---|
| 110 | int i, j, index; | 
|---|
| 111 | int nc = d2->n(); | 
|---|
| 112 | int nproc = messagegrp()->n(); | 
|---|
| 113 | int me = messagegrp()->me(); | 
|---|
| 114 | blocklist = new SCMatrixBlockList; | 
|---|
| 115 | for (i=0, index=0; i<d1->blocks()->nblock(); i++) { | 
|---|
| 116 | for (j=0; j<d2->blocks()->nblock(); j++, index++) { | 
|---|
| 117 | if (index%nproc == me) { | 
|---|
| 118 | blocklist->insert( | 
|---|
| 119 | new SCMatrixRectSubBlock(d1->blocks()->start(i), | 
|---|
| 120 | d1->blocks()->fence(i), | 
|---|
| 121 | nc, | 
|---|
| 122 | d2->blocks()->start(j), | 
|---|
| 123 | d2->blocks()->fence(j), | 
|---|
| 124 | matrix)); | 
|---|
| 125 | } | 
|---|
| 126 | } | 
|---|
| 127 | } | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | ReplSCMatrix::~ReplSCMatrix() | 
|---|
| 131 | { | 
|---|
| 132 | if (matrix) delete[] matrix; | 
|---|
| 133 | if (rows) delete[] rows; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | int | 
|---|
| 137 | ReplSCMatrix::compute_offset(int i,int j) const | 
|---|
| 138 | { | 
|---|
| 139 | if (i<0 || j<0 || i>=d1->n() || j>=d2->n()) { | 
|---|
| 140 | ExEnv::errn() << indent << "ReplSCMatrix: index out of bounds" << endl; | 
|---|
| 141 | abort(); | 
|---|
| 142 | } | 
|---|
| 143 | return i*(d2->n()) + j; | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | double | 
|---|
| 147 | ReplSCMatrix::get_element(int i,int j) const | 
|---|
| 148 | { | 
|---|
| 149 | int off = compute_offset(i,j); | 
|---|
| 150 | return matrix[off]; | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | void | 
|---|
| 154 | ReplSCMatrix::set_element(int i,int j,double a) | 
|---|
| 155 | { | 
|---|
| 156 | int off = compute_offset(i,j); | 
|---|
| 157 | matrix[off] = a; | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | void | 
|---|
| 161 | ReplSCMatrix::accumulate_element(int i,int j,double a) | 
|---|
| 162 | { | 
|---|
| 163 | int off = compute_offset(i,j); | 
|---|
| 164 | matrix[off] += a; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | SCMatrix * | 
|---|
| 168 | ReplSCMatrix::get_subblock(int br, int er, int bc, int ec) | 
|---|
| 169 | { | 
|---|
| 170 | int nsrow = er-br+1; | 
|---|
| 171 | int nscol = ec-bc+1; | 
|---|
| 172 |  | 
|---|
| 173 | if (nsrow > nrow() || nscol > ncol()) { | 
|---|
| 174 | ExEnv::errn() << indent << "ReplSCMatrix::get_subblock: trying to get too big a" | 
|---|
| 175 | << "subblock (" << nsrow << "," << nscol | 
|---|
| 176 | << ") from (" << nrow() << "," << ncol() << ")" << endl; | 
|---|
| 177 | abort(); | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | RefSCDimension dnrow = (nsrow==nrow()) ? rowdim().pointer() | 
|---|
| 181 | : new SCDimension(nsrow); | 
|---|
| 182 | RefSCDimension dncol = (nscol==ncol()) ? coldim().pointer() | 
|---|
| 183 | : new SCDimension(nscol); | 
|---|
| 184 |  | 
|---|
| 185 | SCMatrix * sb = kit()->matrix(dnrow,dncol); | 
|---|
| 186 | sb->assign(0.0); | 
|---|
| 187 |  | 
|---|
| 188 | ReplSCMatrix *lsb = | 
|---|
| 189 | require_dynamic_cast<ReplSCMatrix*>(sb, "ReplSCMatrix::get_subblock"); | 
|---|
| 190 |  | 
|---|
| 191 | for (int i=0; i < nsrow; i++) | 
|---|
| 192 | for (int j=0; j < nscol; j++) | 
|---|
| 193 | lsb->rows[i][j] = rows[i+br][j+bc]; | 
|---|
| 194 |  | 
|---|
| 195 | return sb; | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | void | 
|---|
| 199 | ReplSCMatrix::assign_subblock(SCMatrix*sb, int br, int er, int bc, int ec, | 
|---|
| 200 | int source_br, int source_bc) | 
|---|
| 201 | { | 
|---|
| 202 | ReplSCMatrix *lsb = require_dynamic_cast<ReplSCMatrix*>(sb, | 
|---|
| 203 | "ReplSCMatrix::assign_subblock"); | 
|---|
| 204 |  | 
|---|
| 205 | int nsrow = er-br+1; | 
|---|
| 206 | int nscol = ec-bc+1; | 
|---|
| 207 |  | 
|---|
| 208 | if (nsrow > nrow() || nscol > ncol()) { | 
|---|
| 209 | ExEnv::errn() << indent | 
|---|
| 210 | << "ReplSCMatrix::assign_subblock: trying to assign too big a" | 
|---|
| 211 | << "subblock (" << nsrow << "," << nscol | 
|---|
| 212 | << ") to (" << nrow() << "," << ncol() << ")" << endl;; | 
|---|
| 213 | abort(); | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | for (int i=0; i < nsrow; i++) | 
|---|
| 217 | for (int j=0; j < nscol; j++) | 
|---|
| 218 | rows[i+br][j+bc] = lsb->rows[source_br + i][source_bc + j]; | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | void | 
|---|
| 222 | ReplSCMatrix::accumulate_subblock(SCMatrix*sb, int br, int er, int bc, int ec, | 
|---|
| 223 | int source_br, int source_bc) | 
|---|
| 224 | { | 
|---|
| 225 | ReplSCMatrix *lsb = require_dynamic_cast<ReplSCMatrix*>(sb, | 
|---|
| 226 | "ReplSCMatrix::accumulate_subblock"); | 
|---|
| 227 |  | 
|---|
| 228 | int nsrow = er-br+1; | 
|---|
| 229 | int nscol = ec-bc+1; | 
|---|
| 230 |  | 
|---|
| 231 | if (nsrow > nrow() || nscol > ncol()) { | 
|---|
| 232 | ExEnv::errn() << indent | 
|---|
| 233 | << "ReplSCMatrix::accumulate_subblock: trying to accumulate to big a" | 
|---|
| 234 | << "subblock (" << nsrow << "," << nscol | 
|---|
| 235 | << ") to (" << nrow() << "," << ncol() << ")" << endl; | 
|---|
| 236 | abort(); | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | for (int i=0; i < nsrow; i++) | 
|---|
| 240 | for (int j=0; j < nscol; j++) | 
|---|
| 241 | rows[i+br][j+bc] += lsb->rows[source_br + i][source_bc + j]; | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | SCVector * | 
|---|
| 245 | ReplSCMatrix::get_row(int i) | 
|---|
| 246 | { | 
|---|
| 247 | if (i >= nrow()) { | 
|---|
| 248 | ExEnv::errn() << indent << "ReplSCMatrix::get_row: trying to get invalid row " | 
|---|
| 249 | << i << " max " << nrow() << endl; | 
|---|
| 250 | abort(); | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 | SCVector * v = kit()->vector(coldim()); | 
|---|
| 254 |  | 
|---|
| 255 | ReplSCVector *lv = | 
|---|
| 256 | require_dynamic_cast<ReplSCVector*>(v, "ReplSCMatrix::get_row"); | 
|---|
| 257 |  | 
|---|
| 258 | for (int j=0; j < ncol(); j++) | 
|---|
| 259 | lv->set_element(j,rows[i][j]); | 
|---|
| 260 |  | 
|---|
| 261 | return v; | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | void | 
|---|
| 265 | ReplSCMatrix::assign_row(SCVector *v, int i) | 
|---|
| 266 | { | 
|---|
| 267 | if (i >= nrow()) { | 
|---|
| 268 | ExEnv::errn() << indent << "ReplSCMatrix::assign_row: trying to assign invalid row " | 
|---|
| 269 | << i << " max " << nrow() << endl; | 
|---|
| 270 | abort(); | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | if (v->n() != ncol()) { | 
|---|
| 274 | ExEnv::errn() << indent << "ReplSCMatrix::assign_row: vector is wrong size, " | 
|---|
| 275 | << "is " << v->n() << ", should be " << ncol() << endl; | 
|---|
| 276 | abort(); | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | ReplSCVector *lv = | 
|---|
| 280 | require_dynamic_cast<ReplSCVector*>(v, "ReplSCMatrix::assign_row"); | 
|---|
| 281 |  | 
|---|
| 282 | for (int j=0; j < ncol(); j++) | 
|---|
| 283 | rows[i][j] = lv->get_element(j); | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | void | 
|---|
| 287 | ReplSCMatrix::accumulate_row(SCVector *v, int i) | 
|---|
| 288 | { | 
|---|
| 289 | if (i >= nrow()) { | 
|---|
| 290 | ExEnv::errn() << indent | 
|---|
| 291 | << "ReplSCMatrix::accumulate_row: trying to accumulate invalid row " | 
|---|
| 292 | << i << " max " << nrow() << endl; | 
|---|
| 293 | abort(); | 
|---|
| 294 | } | 
|---|
| 295 |  | 
|---|
| 296 | if (v->n() != ncol()) { | 
|---|
| 297 | ExEnv::errn() << indent << "ReplSCMatrix::accumulate_row: vector is wrong size, " | 
|---|
| 298 | << "is " << v->n() << ", should be " << ncol() << endl; | 
|---|
| 299 | abort(); | 
|---|
| 300 | } | 
|---|
| 301 |  | 
|---|
| 302 | ReplSCVector *lv = | 
|---|
| 303 | require_dynamic_cast<ReplSCVector*>(v, "ReplSCMatrix::accumulate_row"); | 
|---|
| 304 |  | 
|---|
| 305 | for (int j=0; j < ncol(); j++) | 
|---|
| 306 | rows[i][j] += lv->get_element(j); | 
|---|
| 307 | } | 
|---|
| 308 |  | 
|---|
| 309 | SCVector * | 
|---|
| 310 | ReplSCMatrix::get_column(int i) | 
|---|
| 311 | { | 
|---|
| 312 | if (i >= ncol()) { | 
|---|
| 313 | ExEnv::errn() << indent << "ReplSCMatrix::get_column: trying to get invalid column " | 
|---|
| 314 | << i << " max " << ncol() << endl; | 
|---|
| 315 | abort(); | 
|---|
| 316 | } | 
|---|
| 317 |  | 
|---|
| 318 | SCVector * v = kit()->vector(rowdim()); | 
|---|
| 319 |  | 
|---|
| 320 | ReplSCVector *lv = | 
|---|
| 321 | require_dynamic_cast<ReplSCVector*>(v, "ReplSCMatrix::get_column"); | 
|---|
| 322 |  | 
|---|
| 323 | for (int j=0; j < nrow(); j++) | 
|---|
| 324 | lv->set_element(j,rows[j][i]); | 
|---|
| 325 |  | 
|---|
| 326 | return v; | 
|---|
| 327 | } | 
|---|
| 328 |  | 
|---|
| 329 | void | 
|---|
| 330 | ReplSCMatrix::assign_column(SCVector *v, int i) | 
|---|
| 331 | { | 
|---|
| 332 | if (i >= ncol()) { | 
|---|
| 333 | ExEnv::errn() << indent | 
|---|
| 334 | << "ReplSCMatrix::assign_column: trying to assign invalid column " | 
|---|
| 335 | << i << " max " << ncol() << endl; | 
|---|
| 336 | abort(); | 
|---|
| 337 | } | 
|---|
| 338 |  | 
|---|
| 339 | if (v->n() != nrow()) { | 
|---|
| 340 | ExEnv::errn() << indent << "ReplSCMatrix::assign_column: vector is wrong size, " | 
|---|
| 341 | << "is " << v->n() << ", should be " << nrow() << endl; | 
|---|
| 342 | abort(); | 
|---|
| 343 | } | 
|---|
| 344 |  | 
|---|
| 345 | ReplSCVector *lv = | 
|---|
| 346 | require_dynamic_cast<ReplSCVector*>(v, "ReplSCMatrix::assign_column"); | 
|---|
| 347 |  | 
|---|
| 348 | for (int j=0; j < nrow(); j++) | 
|---|
| 349 | rows[j][i] = lv->get_element(j); | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 | void | 
|---|
| 353 | ReplSCMatrix::accumulate_column(SCVector *v, int i) | 
|---|
| 354 | { | 
|---|
| 355 | if (i >= ncol()) { | 
|---|
| 356 | ExEnv::errn() << indent | 
|---|
| 357 | << "ReplSCMatrix::accumulate_column: trying to accumulate invalid" | 
|---|
| 358 | << " column" << i << " max " << ncol() << endl; | 
|---|
| 359 | abort(); | 
|---|
| 360 | } | 
|---|
| 361 |  | 
|---|
| 362 | if (v->n() != nrow()) { | 
|---|
| 363 | ExEnv::errn() << indent << "ReplSCMatrix::accumulate_column: vector is wrong size, " | 
|---|
| 364 | << "is " << v->n() << ", should be " << nrow() << endl; | 
|---|
| 365 | abort(); | 
|---|
| 366 | } | 
|---|
| 367 |  | 
|---|
| 368 | ReplSCVector *lv = | 
|---|
| 369 | require_dynamic_cast<ReplSCVector*>(v, "ReplSCMatrix::accumulate_column"); | 
|---|
| 370 |  | 
|---|
| 371 | for (int j=0; j < nrow(); j++) | 
|---|
| 372 | rows[j][i] += lv->get_element(j); | 
|---|
| 373 | } | 
|---|
| 374 |  | 
|---|
| 375 | void | 
|---|
| 376 | ReplSCMatrix::assign_val(double a) | 
|---|
| 377 | { | 
|---|
| 378 | int n = d1->n() * d2->n(); | 
|---|
| 379 | for (int i=0; i<n; i++) matrix[i] = a; | 
|---|
| 380 | } | 
|---|
| 381 |  | 
|---|
| 382 | void | 
|---|
| 383 | ReplSCMatrix::accumulate_product_rr(SCMatrix*a,SCMatrix*b) | 
|---|
| 384 | { | 
|---|
| 385 | const char* name = "ReplSCMatrix::accumulate_product_rr"; | 
|---|
| 386 | // make sure that the arguments are of the correct type | 
|---|
| 387 | ReplSCMatrix* la = require_dynamic_cast<ReplSCMatrix*>(a,name); | 
|---|
| 388 | ReplSCMatrix* lb = require_dynamic_cast<ReplSCMatrix*>(b,name); | 
|---|
| 389 |  | 
|---|
| 390 | // make sure that the dimensions match | 
|---|
| 391 | if (!rowdim()->equiv(la->rowdim()) || !coldim()->equiv(lb->coldim()) || | 
|---|
| 392 | !la->coldim()->equiv(lb->rowdim())) { | 
|---|
| 393 | ExEnv::errn() << indent | 
|---|
| 394 | << "ReplSCMatrix::accumulate_product_rr(SCMatrix*a,SCMatrix*b): " | 
|---|
| 395 | << "dimensions don't match" << endl; | 
|---|
| 396 | abort(); | 
|---|
| 397 | } | 
|---|
| 398 |  | 
|---|
| 399 | #if 0 | 
|---|
| 400 | cmat_transpose_matrix(lb->rows, la->ncol(), this->ncol()); | 
|---|
| 401 | double** btrans; | 
|---|
| 402 | btrans = new double*[this->ncol()]; | 
|---|
| 403 | btrans[0] = lb->rows[0]; | 
|---|
| 404 | cmat_matrix_pointers(btrans,btrans[0],this->ncol(),la->ncol()); | 
|---|
| 405 |  | 
|---|
| 406 | Ref<SCElementOp> op = new SCElementDot(la->rows, btrans, la->ncol()); | 
|---|
| 407 | element_op(op); | 
|---|
| 408 |  | 
|---|
| 409 | cmat_transpose_matrix(btrans,this->ncol(),la->ncol()); | 
|---|
| 410 | delete[] btrans; | 
|---|
| 411 | #else | 
|---|
| 412 | int i,j,k; | 
|---|
| 413 | int ii,jj; | 
|---|
| 414 |  | 
|---|
| 415 | int nr = la->nrow(); | 
|---|
| 416 | int nc = lb->ncol(); | 
|---|
| 417 | int ncc = la->ncol(); | 
|---|
| 418 |  | 
|---|
| 419 | if (nr==0 || nc==0 || ncc==0) | 
|---|
| 420 | return; | 
|---|
| 421 |  | 
|---|
| 422 | int nproc = messagegrp()->n(); | 
|---|
| 423 | int me = messagegrp()->me(); | 
|---|
| 424 | int mod = nr%nproc; | 
|---|
| 425 | int nirow = nr/nproc + ((mod <= me) ? 0 : 1); | 
|---|
| 426 | int istart = (nr/nproc)*me + ((mod <= me) ? mod : me); | 
|---|
| 427 | int iend = istart+nirow; | 
|---|
| 428 |  | 
|---|
| 429 | double ** ablock = cmat_new_square_matrix(D1); | 
|---|
| 430 | double ** bblock = cmat_new_square_matrix(D1); | 
|---|
| 431 | double ** cblock = cmat_new_square_matrix(D1); | 
|---|
| 432 |  | 
|---|
| 433 | for (i=istart; i < iend; i += D1) { | 
|---|
| 434 | int ni = iend-i; | 
|---|
| 435 | if (ni > D1) ni = D1; | 
|---|
| 436 |  | 
|---|
| 437 | for (j=0; j < nc; j += D1) { | 
|---|
| 438 | int nj = nc-j; | 
|---|
| 439 | if (nj > D1) nj = D1; | 
|---|
| 440 |  | 
|---|
| 441 | memset(cblock[0], 0, sizeof(double)*D1*D1); | 
|---|
| 442 |  | 
|---|
| 443 | for (k=0; k < ncc; k += D1) { | 
|---|
| 444 | int nk = ncc-k; | 
|---|
| 445 | if (nk > D1) nk = D1; | 
|---|
| 446 |  | 
|---|
| 447 | copy_block(ablock, la->rows, i, ni, k, nk); | 
|---|
| 448 | copy_trans_block(bblock, lb->rows, j, nj, k, nk); | 
|---|
| 449 | mult_block(ablock, bblock, cblock, ni, nj, nk); | 
|---|
| 450 | } | 
|---|
| 451 |  | 
|---|
| 452 | for (ii=0; ii < ni; ii++) | 
|---|
| 453 | for (jj=0; jj < nj; jj++) | 
|---|
| 454 | rows[i+ii][j+jj] += cblock[ii][jj]; | 
|---|
| 455 | } | 
|---|
| 456 | } | 
|---|
| 457 |  | 
|---|
| 458 | for (i=0; i < nproc; i++) { | 
|---|
| 459 | nirow = nr/nproc + ((mod <= i) ? 0 : 1); | 
|---|
| 460 | istart = (nr/nproc)*i + ((mod <= i) ? mod : i); | 
|---|
| 461 | if (!nirow) | 
|---|
| 462 | break; | 
|---|
| 463 | messagegrp()->bcast(rows[istart], nirow*nc, i); | 
|---|
| 464 | } | 
|---|
| 465 |  | 
|---|
| 466 | cmat_delete_matrix(ablock); | 
|---|
| 467 | cmat_delete_matrix(bblock); | 
|---|
| 468 | cmat_delete_matrix(cblock); | 
|---|
| 469 |  | 
|---|
| 470 | #endif | 
|---|
| 471 | } | 
|---|
| 472 |  | 
|---|
| 473 | // does the outer product a x b.  this must have rowdim() == a->dim() and | 
|---|
| 474 | // coldim() == b->dim() | 
|---|
| 475 | void | 
|---|
| 476 | ReplSCMatrix::accumulate_outer_product(SCVector*a,SCVector*b) | 
|---|
| 477 | { | 
|---|
| 478 | const char* name = "ReplSCMatrix::accumulate_outer_product"; | 
|---|
| 479 | // make sure that the arguments are of the correct type | 
|---|
| 480 | ReplSCVector* la = require_dynamic_cast<ReplSCVector*>(a,name); | 
|---|
| 481 | ReplSCVector* lb = require_dynamic_cast<ReplSCVector*>(b,name); | 
|---|
| 482 |  | 
|---|
| 483 | // make sure that the dimensions match | 
|---|
| 484 | if (!rowdim()->equiv(la->dim()) || !coldim()->equiv(lb->dim())) { | 
|---|
| 485 | ExEnv::errn() << indent | 
|---|
| 486 | << "ReplSCMatrix::accumulate_outer_product(SCVector*a,SCVector*b): " | 
|---|
| 487 | << "dimensions don't match" << endl; | 
|---|
| 488 | abort(); | 
|---|
| 489 | } | 
|---|
| 490 |  | 
|---|
| 491 | int nr = a->n(); | 
|---|
| 492 | int nc = b->n(); | 
|---|
| 493 | int i, j; | 
|---|
| 494 | double* adat = la->vector; | 
|---|
| 495 | double* bdat = lb->vector; | 
|---|
| 496 | double** thisdat = rows; | 
|---|
| 497 | for (i=0; i<nr; i++) { | 
|---|
| 498 | for (j=0; j<nc; j++) { | 
|---|
| 499 | thisdat[i][j] += adat[i] * bdat[j]; | 
|---|
| 500 | } | 
|---|
| 501 | } | 
|---|
| 502 | } | 
|---|
| 503 |  | 
|---|
| 504 | void | 
|---|
| 505 | ReplSCMatrix::accumulate_product_rs(SCMatrix*a,SymmSCMatrix*b) | 
|---|
| 506 | { | 
|---|
| 507 | const char* name = "ReplSCMatrix::accumulate_product_rs"; | 
|---|
| 508 | // make sure that the arguments are of the correct type | 
|---|
| 509 | ReplSCMatrix* la = require_dynamic_cast<ReplSCMatrix*>(a,name); | 
|---|
| 510 | ReplSymmSCMatrix* lb = require_dynamic_cast<ReplSymmSCMatrix*>(b,name); | 
|---|
| 511 |  | 
|---|
| 512 | // make sure that the dimensions match | 
|---|
| 513 | if (!rowdim()->equiv(la->rowdim()) || !coldim()->equiv(lb->dim()) || | 
|---|
| 514 | !la->coldim()->equiv(lb->dim())) { | 
|---|
| 515 | ExEnv::errn() << indent | 
|---|
| 516 | << "ReplSCMatrix::accumulate_product_rs(SCMatrix*a,SymmSCMatrix*b): " | 
|---|
| 517 | << "dimensions don't match" << endl; | 
|---|
| 518 | ExEnv::err0() << indent << "rowdim():" << endl; | 
|---|
| 519 | rowdim().print(); | 
|---|
| 520 | ExEnv::err0() << indent << "coldim():" << endl; | 
|---|
| 521 | coldim().print(); | 
|---|
| 522 | ExEnv::err0() << indent << "la->rowdim():" << endl; | 
|---|
| 523 | la->rowdim().print(); | 
|---|
| 524 | ExEnv::err0() << indent << "la->coldim():" << endl; | 
|---|
| 525 | la->coldim().print(); | 
|---|
| 526 | ExEnv::err0() << indent << "lb->dim():" << endl; | 
|---|
| 527 | lb->dim().print(); | 
|---|
| 528 | abort(); | 
|---|
| 529 | } | 
|---|
| 530 |  | 
|---|
| 531 | double **cd = rows; | 
|---|
| 532 | double **ad = la->rows; | 
|---|
| 533 | double **bd = lb->rows; | 
|---|
| 534 | int ni = a->rowdim().n(); | 
|---|
| 535 | int njk = b->dim().n(); | 
|---|
| 536 | int i, j, k; | 
|---|
| 537 | for (i=0; i<ni; i++) { | 
|---|
| 538 | for (j=0; j<njk; j++) { | 
|---|
| 539 | for (k=0; k<=j; k++) { | 
|---|
| 540 | cd[i][k] += ad[i][j]*bd[j][k]; | 
|---|
| 541 | } | 
|---|
| 542 | for (; k<njk; k++) { | 
|---|
| 543 | cd[i][k] += ad[i][j]*bd[k][j]; | 
|---|
| 544 | } | 
|---|
| 545 | } | 
|---|
| 546 | } | 
|---|
| 547 | } | 
|---|
| 548 |  | 
|---|
| 549 | void | 
|---|
| 550 | ReplSCMatrix::accumulate_product_rd(SCMatrix*a,DiagSCMatrix*b) | 
|---|
| 551 | { | 
|---|
| 552 | const char* name = "ReplSCMatrix::accumulate_product_rd"; | 
|---|
| 553 | // make sure that the arguments are of the correct type | 
|---|
| 554 | ReplSCMatrix* la = require_dynamic_cast<ReplSCMatrix*>(a,name); | 
|---|
| 555 | ReplDiagSCMatrix* lb = require_dynamic_cast<ReplDiagSCMatrix*>(b,name); | 
|---|
| 556 |  | 
|---|
| 557 | // make sure that the dimensions match | 
|---|
| 558 | if (!rowdim()->equiv(la->rowdim()) || !coldim()->equiv(lb->dim()) || | 
|---|
| 559 | !la->coldim()->equiv(lb->dim())) { | 
|---|
| 560 | ExEnv::errn() << indent | 
|---|
| 561 | << "ReplSCMatrix::accumulate_product_rd(SCMatrix*a,DiagSCMatrix*b): " | 
|---|
| 562 | << "dimensions don't match" << endl; | 
|---|
| 563 | abort(); | 
|---|
| 564 | } | 
|---|
| 565 |  | 
|---|
| 566 | double **cd = rows; | 
|---|
| 567 | double **ad = la->rows; | 
|---|
| 568 | double *bd = lb->matrix; | 
|---|
| 569 | int ni = a->rowdim().n(); | 
|---|
| 570 | int nj = b->dim().n(); | 
|---|
| 571 | int i, j; | 
|---|
| 572 | for (i=0; i<ni; i++) { | 
|---|
| 573 | for (j=0; j<nj; j++) { | 
|---|
| 574 | cd[i][j] += ad[i][j]*bd[j]; | 
|---|
| 575 | } | 
|---|
| 576 | } | 
|---|
| 577 | } | 
|---|
| 578 |  | 
|---|
| 579 | void | 
|---|
| 580 | ReplSCMatrix::accumulate(const SCMatrix*a) | 
|---|
| 581 | { | 
|---|
| 582 | // make sure that the arguments is of the correct type | 
|---|
| 583 | const ReplSCMatrix* la | 
|---|
| 584 | = require_dynamic_cast<const ReplSCMatrix*>(a,"ReplSCMatrix::accumulate"); | 
|---|
| 585 |  | 
|---|
| 586 | // make sure that the dimensions match | 
|---|
| 587 | if (!rowdim()->equiv(la->rowdim()) || !coldim()->equiv(la->coldim())) { | 
|---|
| 588 | ExEnv::errn() << indent << "ReplSCMatrix::accumulate(SCMatrix*a): " | 
|---|
| 589 | << "dimensions don't match" << endl; | 
|---|
| 590 | abort(); | 
|---|
| 591 | } | 
|---|
| 592 |  | 
|---|
| 593 | int nelem = this->ncol() * this->nrow(); | 
|---|
| 594 | int i; | 
|---|
| 595 | for (i=0; i<nelem; i++) matrix[i] += la->matrix[i]; | 
|---|
| 596 | } | 
|---|
| 597 |  | 
|---|
| 598 | void | 
|---|
| 599 | ReplSCMatrix::accumulate(const SymmSCMatrix*a) | 
|---|
| 600 | { | 
|---|
| 601 | // make sure that the arguments is of the correct type | 
|---|
| 602 | const ReplSymmSCMatrix* la | 
|---|
| 603 | = require_dynamic_cast<const ReplSymmSCMatrix*>(a,"ReplSCMatrix::accumulate"); | 
|---|
| 604 |  | 
|---|
| 605 | // make sure that the dimensions match | 
|---|
| 606 | if (!rowdim()->equiv(la->dim()) || !coldim()->equiv(la->dim())) { | 
|---|
| 607 | ExEnv::errn() << indent << "ReplSCMatrix::accumulate(SymmSCMatrix*a): " | 
|---|
| 608 | << "dimensions don't match" << endl; | 
|---|
| 609 | abort(); | 
|---|
| 610 | } | 
|---|
| 611 |  | 
|---|
| 612 | int n = this->ncol(); | 
|---|
| 613 | double *dat = la->matrix; | 
|---|
| 614 | int i, j; | 
|---|
| 615 | for (i=0; i<n; i++) { | 
|---|
| 616 | for (j=0; j<i; j++) { | 
|---|
| 617 | double tmp = *dat; | 
|---|
| 618 | matrix[i*n+j] += tmp; | 
|---|
| 619 | matrix[j*n+i] += tmp; | 
|---|
| 620 | dat++; | 
|---|
| 621 | } | 
|---|
| 622 | matrix[i*n+i] += *dat++; | 
|---|
| 623 | } | 
|---|
| 624 | } | 
|---|
| 625 |  | 
|---|
| 626 | void | 
|---|
| 627 | ReplSCMatrix::accumulate(const DiagSCMatrix*a) | 
|---|
| 628 | { | 
|---|
| 629 | // make sure that the arguments is of the correct type | 
|---|
| 630 | const ReplDiagSCMatrix* la | 
|---|
| 631 | = require_dynamic_cast<const ReplDiagSCMatrix*>(a,"ReplSCMatrix::accumulate"); | 
|---|
| 632 |  | 
|---|
| 633 | // make sure that the dimensions match | 
|---|
| 634 | if (!rowdim()->equiv(la->dim()) || !coldim()->equiv(la->dim())) { | 
|---|
| 635 | ExEnv::errn() << indent << "ReplSCMatrix::accumulate(DiagSCMatrix*a): " | 
|---|
| 636 | << "dimensions don't match" << endl; | 
|---|
| 637 | abort(); | 
|---|
| 638 | } | 
|---|
| 639 |  | 
|---|
| 640 | int n = this->ncol(); | 
|---|
| 641 | double *dat = la->matrix; | 
|---|
| 642 | int i; | 
|---|
| 643 | for (i=0; i<n; i++) { | 
|---|
| 644 | matrix[i*n+i] += *dat++; | 
|---|
| 645 | } | 
|---|
| 646 | } | 
|---|
| 647 |  | 
|---|
| 648 | void | 
|---|
| 649 | ReplSCMatrix::accumulate(const SCVector*a) | 
|---|
| 650 | { | 
|---|
| 651 | // make sure that the arguments is of the correct type | 
|---|
| 652 | const ReplSCVector* la | 
|---|
| 653 | = require_dynamic_cast<const ReplSCVector*>(a,"ReplSCVector::accumulate"); | 
|---|
| 654 |  | 
|---|
| 655 | // make sure that the dimensions match | 
|---|
| 656 | if (!((rowdim()->equiv(la->dim()) && coldim()->n() == 1) | 
|---|
| 657 | || (coldim()->equiv(la->dim()) && rowdim()->n() == 1))) { | 
|---|
| 658 | ExEnv::errn() << indent << "ReplSCMatrix::accumulate(SCVector*a): " | 
|---|
| 659 | << "dimensions don't match" << endl; | 
|---|
| 660 | abort(); | 
|---|
| 661 | } | 
|---|
| 662 |  | 
|---|
| 663 | int n = this->ncol(); | 
|---|
| 664 | int i; | 
|---|
| 665 | double *dat = la->vector; | 
|---|
| 666 | for (i=0; i<n; i++) { | 
|---|
| 667 | matrix[i*n+i] += dat[i]; | 
|---|
| 668 | } | 
|---|
| 669 | } | 
|---|
| 670 |  | 
|---|
| 671 | void | 
|---|
| 672 | ReplSCMatrix::transpose_this() | 
|---|
| 673 | { | 
|---|
| 674 | cmat_transpose_matrix(rows,nrow(),ncol()); | 
|---|
| 675 | delete[] rows; | 
|---|
| 676 | rows = new double*[ncol()]; | 
|---|
| 677 | cmat_matrix_pointers(rows,matrix,ncol(),nrow()); | 
|---|
| 678 | RefSCDimension tmp = d1; | 
|---|
| 679 | d1 = d2; | 
|---|
| 680 | d2 = tmp; | 
|---|
| 681 | init_blocklist(); | 
|---|
| 682 | } | 
|---|
| 683 |  | 
|---|
| 684 | double | 
|---|
| 685 | ReplSCMatrix::invert_this() | 
|---|
| 686 | { | 
|---|
| 687 | if (nrow() != ncol()) { | 
|---|
| 688 | ExEnv::errn() << indent << "ReplSCMatrix::invert_this: matrix is not square" << endl; | 
|---|
| 689 | abort(); | 
|---|
| 690 | } | 
|---|
| 691 | return cmat_invert(rows,0,nrow()); | 
|---|
| 692 | } | 
|---|
| 693 |  | 
|---|
| 694 | double | 
|---|
| 695 | ReplSCMatrix::determ_this() | 
|---|
| 696 | { | 
|---|
| 697 | if (nrow() != ncol()) { | 
|---|
| 698 | ExEnv::errn() << indent << "ReplSCMatrix::determ_this: matrix is not square" << endl; | 
|---|
| 699 | abort(); | 
|---|
| 700 | } | 
|---|
| 701 | return cmat_determ(rows,0,nrow()); | 
|---|
| 702 | } | 
|---|
| 703 |  | 
|---|
| 704 | double | 
|---|
| 705 | ReplSCMatrix::trace() | 
|---|
| 706 | { | 
|---|
| 707 | if (nrow() != ncol()) { | 
|---|
| 708 | ExEnv::errn() << indent << "ReplSCMatrix::trace: matrix is not square" << endl; | 
|---|
| 709 | abort(); | 
|---|
| 710 | } | 
|---|
| 711 | double ret=0; | 
|---|
| 712 | int i; | 
|---|
| 713 | for (i=0; i < nrow(); i++) | 
|---|
| 714 | ret += rows[i][i]; | 
|---|
| 715 | return ret; | 
|---|
| 716 | } | 
|---|
| 717 |  | 
|---|
| 718 | void | 
|---|
| 719 | ReplSCMatrix::svd_this(SCMatrix *U, DiagSCMatrix *sigma, SCMatrix *V) | 
|---|
| 720 | { | 
|---|
| 721 | ReplSCMatrix* lU = | 
|---|
| 722 | require_dynamic_cast<ReplSCMatrix*>(U,"ReplSCMatrix::svd_this"); | 
|---|
| 723 | ReplSCMatrix* lV = | 
|---|
| 724 | require_dynamic_cast<ReplSCMatrix*>(V,"ReplSCMatrix::svd_this"); | 
|---|
| 725 | ReplDiagSCMatrix* lsigma = | 
|---|
| 726 | require_dynamic_cast<ReplDiagSCMatrix*>(sigma,"ReplSCMatrix::svd_this"); | 
|---|
| 727 |  | 
|---|
| 728 | RefSCDimension mdim = rowdim(); | 
|---|
| 729 | RefSCDimension ndim = coldim(); | 
|---|
| 730 | int m = mdim.n(); | 
|---|
| 731 | int n = ndim.n(); | 
|---|
| 732 |  | 
|---|
| 733 | RefSCDimension pdim; | 
|---|
| 734 | if (m == n && m == sigma->dim().n()) | 
|---|
| 735 | pdim = sigma->dim(); | 
|---|
| 736 | else if (m<n) | 
|---|
| 737 | pdim = mdim; | 
|---|
| 738 | else | 
|---|
| 739 | pdim = ndim; | 
|---|
| 740 |  | 
|---|
| 741 | int p = pdim.n(); | 
|---|
| 742 |  | 
|---|
| 743 | if (!mdim->equiv(lU->rowdim()) || | 
|---|
| 744 | !mdim->equiv(lU->coldim()) || | 
|---|
| 745 | !ndim->equiv(lV->rowdim()) || | 
|---|
| 746 | !ndim->equiv(lV->coldim()) || | 
|---|
| 747 | !pdim->equiv(sigma->dim())) { | 
|---|
| 748 | ExEnv::errn() << indent << "ReplSCMatrix: svd_this: dimension mismatch" << endl; | 
|---|
| 749 | abort(); | 
|---|
| 750 | } | 
|---|
| 751 |  | 
|---|
| 752 | // form a fortran style matrix for the SVD routines | 
|---|
| 753 | double *dA = new double[m*n]; | 
|---|
| 754 | double *dU = new double[m*m]; | 
|---|
| 755 | double *dV = new double[n*n]; | 
|---|
| 756 | double *dsigma = new double[n]; | 
|---|
| 757 | double *w = new double[(3*p-1>m)?(3*p-1):m]; | 
|---|
| 758 |  | 
|---|
| 759 | int i,j; | 
|---|
| 760 | for (i=0; i<m; i++) { | 
|---|
| 761 | for (j=0; j<n; j++) { | 
|---|
| 762 | dA[i + j*m] = this->rows[i][j]; | 
|---|
| 763 | } | 
|---|
| 764 | } | 
|---|
| 765 |  | 
|---|
| 766 | int three = 3; | 
|---|
| 767 |  | 
|---|
| 768 | sing_(dU, &m, &three, dsigma, dV, &n, &three, dA, &m, &m, &n, w); | 
|---|
| 769 |  | 
|---|
| 770 | for (i=0; i<m; i++) { | 
|---|
| 771 | for (j=0; j<m; j++) { | 
|---|
| 772 | lU->rows[i][j] = dU[i + j*m]; | 
|---|
| 773 | } | 
|---|
| 774 | } | 
|---|
| 775 |  | 
|---|
| 776 | for (i=0; i<n; i++) { | 
|---|
| 777 | for (j=0; j<n; j++) { | 
|---|
| 778 | lV->rows[i][j] = dV[i + j*n]; | 
|---|
| 779 | } | 
|---|
| 780 | } | 
|---|
| 781 |  | 
|---|
| 782 | for (i=0; i<p; i++) { | 
|---|
| 783 | lsigma->matrix[i] = dsigma[i]; | 
|---|
| 784 | } | 
|---|
| 785 |  | 
|---|
| 786 | delete[] dA; | 
|---|
| 787 | delete[] dU; | 
|---|
| 788 | delete[] dV; | 
|---|
| 789 | delete[] dsigma; | 
|---|
| 790 | delete[] w; | 
|---|
| 791 | } | 
|---|
| 792 |  | 
|---|
| 793 | double | 
|---|
| 794 | ReplSCMatrix::solve_this(SCVector*v) | 
|---|
| 795 | { | 
|---|
| 796 | ReplSCVector* lv = | 
|---|
| 797 | require_dynamic_cast<ReplSCVector*>(v,"ReplSCMatrix::solve_this"); | 
|---|
| 798 |  | 
|---|
| 799 | // make sure that the dimensions match | 
|---|
| 800 | if (!rowdim()->equiv(lv->dim())) { | 
|---|
| 801 | ExEnv::errn() << indent << "ReplSCMatrix::solve_this(SCVector*v): " | 
|---|
| 802 | << "dimensions don't match" << endl; | 
|---|
| 803 | abort(); | 
|---|
| 804 | } | 
|---|
| 805 |  | 
|---|
| 806 | return cmat_solve_lin(rows,0,lv->vector,nrow()); | 
|---|
| 807 | } | 
|---|
| 808 |  | 
|---|
| 809 | void | 
|---|
| 810 | ReplSCMatrix::schmidt_orthog(SymmSCMatrix *S, int nc) | 
|---|
| 811 | { | 
|---|
| 812 | int i,j,ij; | 
|---|
| 813 | int m; | 
|---|
| 814 |  | 
|---|
| 815 | ReplSymmSCMatrix* lS = | 
|---|
| 816 | require_dynamic_cast<ReplSymmSCMatrix*>(S,"ReplSCMatrix::schmidt_orthog"); | 
|---|
| 817 |  | 
|---|
| 818 | // make sure that the dimensions match | 
|---|
| 819 | if (!rowdim()->equiv(lS->dim())) { | 
|---|
| 820 | ExEnv::errn() << indent << "ReplSCMatrix::schmidt_orthog(): " | 
|---|
| 821 | << "dimensions don't match" << endl; | 
|---|
| 822 | abort(); | 
|---|
| 823 | } | 
|---|
| 824 |  | 
|---|
| 825 | #if 0 | 
|---|
| 826 | cmat_schmidt(rows,lS->matrix,nrow(),nc); | 
|---|
| 827 | #else | 
|---|
| 828 | int me = messagegrp()->me(); | 
|---|
| 829 | int nproc = messagegrp()->n(); | 
|---|
| 830 | int nr = nrow(); | 
|---|
| 831 |  | 
|---|
| 832 | double vtmp; | 
|---|
| 833 | double *v = new double[nr]; | 
|---|
| 834 | double *cm = new double[nr]; | 
|---|
| 835 |  | 
|---|
| 836 | double **sblock = cmat_new_square_matrix(D1); | 
|---|
| 837 |  | 
|---|
| 838 | int mod = nc%nproc; | 
|---|
| 839 | int ncoli = nc/nproc + (mod <= me ? 0 : 1); | 
|---|
| 840 | int cstart = (nc/nproc)*me + (mod <= me ? mod : me); | 
|---|
| 841 | int cend = cstart+ncoli; | 
|---|
| 842 |  | 
|---|
| 843 | // copy my columns to a rows of temp matrix | 
|---|
| 844 | double **cols = cmat_new_rect_matrix(ncoli, nr); | 
|---|
| 845 | for (i=cstart; i < cend; i++) | 
|---|
| 846 | for (j=0; j < nr; j++) | 
|---|
| 847 | cols[i-cstart][j] = rows[j][i]; | 
|---|
| 848 |  | 
|---|
| 849 | for (m=0; m < nc; m++) { | 
|---|
| 850 | // who has this column | 
|---|
| 851 | for (i=0; i < nproc; i++) { | 
|---|
| 852 | int ni = nc/nproc + (mod <= i ? 0 : 1); | 
|---|
| 853 | int csi = (nc/nproc)*i + (mod <= i ? mod : i); | 
|---|
| 854 | if (m >= csi && m < csi+ni) { | 
|---|
| 855 | if (i==me) | 
|---|
| 856 | memcpy(cm, cols[m-csi], sizeof(double)*nr); | 
|---|
| 857 | messagegrp()->bcast(cm, nr, i); | 
|---|
| 858 | break; | 
|---|
| 859 | } | 
|---|
| 860 | } | 
|---|
| 861 |  | 
|---|
| 862 | memset(v, 0, sizeof(double)*nr); | 
|---|
| 863 |  | 
|---|
| 864 | for (i=ij=0; i < nr; i += D1) { | 
|---|
| 865 | int ni = nr-i; | 
|---|
| 866 | if (ni > D1) ni = D1; | 
|---|
| 867 |  | 
|---|
| 868 | for (j=0; j < nr; j += D1, ij++) { | 
|---|
| 869 | if (ij%nproc != me) | 
|---|
| 870 | continue; | 
|---|
| 871 |  | 
|---|
| 872 | int nj = nr-j; | 
|---|
| 873 | if (nj > D1) nj = D1; | 
|---|
| 874 |  | 
|---|
| 875 | copy_sym_block(sblock, lS->rows, i, ni, j, nj); | 
|---|
| 876 |  | 
|---|
| 877 | for (int ii=0; ii < ni; ii++) | 
|---|
| 878 | for (int jj=0; jj < nj; jj++) | 
|---|
| 879 | v[i+ii] += cm[j+jj]*sblock[ii][jj]; | 
|---|
| 880 | } | 
|---|
| 881 | } | 
|---|
| 882 |  | 
|---|
| 883 | messagegrp()->sum(v, nr); | 
|---|
| 884 |  | 
|---|
| 885 | for (i=0,vtmp=0.0; i < nr; i++) | 
|---|
| 886 | vtmp += v[i]*cm[i]; | 
|---|
| 887 |  | 
|---|
| 888 | if (!vtmp) { | 
|---|
| 889 | ExEnv::errn() << "cmat_schmidt: bogus" << endl; | 
|---|
| 890 | abort(); | 
|---|
| 891 | } | 
|---|
| 892 |  | 
|---|
| 893 | if (vtmp < 1.0e-15) | 
|---|
| 894 | vtmp = 1.0e-15; | 
|---|
| 895 |  | 
|---|
| 896 | vtmp = 1.0/sqrt(vtmp); | 
|---|
| 897 |  | 
|---|
| 898 | for (i=0; i < nr; i++) { | 
|---|
| 899 | v[i] *= vtmp; | 
|---|
| 900 | cm[i] *= vtmp; | 
|---|
| 901 | } | 
|---|
| 902 |  | 
|---|
| 903 | if (m < nc-1) { | 
|---|
| 904 | for (i=m+1; i < nc; i++) { | 
|---|
| 905 | if (i < cstart) | 
|---|
| 906 | continue; | 
|---|
| 907 | if (i >= cend) | 
|---|
| 908 | break; | 
|---|
| 909 |  | 
|---|
| 910 | double *ci = cols[i-cstart]; | 
|---|
| 911 |  | 
|---|
| 912 | for (j=0,vtmp=0.0; j < nr; j++) | 
|---|
| 913 | vtmp += v[j] * ci[j]; | 
|---|
| 914 | for (j=0; j < nr; j++) | 
|---|
| 915 | ci[j] -= vtmp * cm[j]; | 
|---|
| 916 | } | 
|---|
| 917 | } | 
|---|
| 918 |  | 
|---|
| 919 | // if I own cm then put it back into cols | 
|---|
| 920 | if (m >= cstart && m < cend) | 
|---|
| 921 | memcpy(cols[m-cstart], cm, sizeof(double)*nr); | 
|---|
| 922 | } | 
|---|
| 923 |  | 
|---|
| 924 | // now collect columns again | 
|---|
| 925 | for (i=0; i < nproc; i++) { | 
|---|
| 926 | int ni = nc/nproc + (mod <= i ? 0 : 1); | 
|---|
| 927 | int csi = (nc/nproc)*i + (mod <= i ? mod : i); | 
|---|
| 928 | for (j=0; j < ni; j++) { | 
|---|
| 929 | if (i==me) { | 
|---|
| 930 | messagegrp()->bcast(cols[j], nr, i); | 
|---|
| 931 | for (int k=0; k < nr; k++) | 
|---|
| 932 | rows[k][j+csi] = cols[j][k]; | 
|---|
| 933 | } | 
|---|
| 934 | else { | 
|---|
| 935 | messagegrp()->bcast(cm, nr, i); | 
|---|
| 936 | for (int k=0; k < nr; k++) | 
|---|
| 937 | rows[k][j+csi] = cm[k]; | 
|---|
| 938 | } | 
|---|
| 939 | } | 
|---|
| 940 | } | 
|---|
| 941 |  | 
|---|
| 942 | cmat_delete_matrix(sblock); | 
|---|
| 943 | cmat_delete_matrix(cols); | 
|---|
| 944 | delete[] v; | 
|---|
| 945 | delete[] cm; | 
|---|
| 946 | #endif | 
|---|
| 947 | } | 
|---|
| 948 |  | 
|---|
| 949 | int | 
|---|
| 950 | ReplSCMatrix::schmidt_orthog_tol(SymmSCMatrix *S, double tol, double *res) | 
|---|
| 951 | { | 
|---|
| 952 | ReplSymmSCMatrix* lS = | 
|---|
| 953 | require_dynamic_cast<ReplSymmSCMatrix*>(S,"ReplSCMatrix::schmidt_orthog_tol"); | 
|---|
| 954 |  | 
|---|
| 955 | // make sure that the dimensions match | 
|---|
| 956 | if (!rowdim()->equiv(lS->dim())) { | 
|---|
| 957 | ExEnv::errn() << indent << "ReplSCMatrix::schmidt_orthog_tol(): " << | 
|---|
| 958 | "dimensions don't match" << endl; | 
|---|
| 959 | abort(); | 
|---|
| 960 | } | 
|---|
| 961 |  | 
|---|
| 962 | int northog; | 
|---|
| 963 |  | 
|---|
| 964 | if (messagegrp()->me() == 0) { | 
|---|
| 965 | northog = cmat_schmidt_tol(rows,lS->matrix,nrow(),ncol(),tol,res); | 
|---|
| 966 | } | 
|---|
| 967 |  | 
|---|
| 968 | // make sure everybody ends up with the same data | 
|---|
| 969 | messagegrp()->bcast(northog); | 
|---|
| 970 | messagegrp()->bcast(*res); | 
|---|
| 971 | for (int i=0; i<nrow(); i++) { | 
|---|
| 972 | messagegrp()->bcast(rows[i],ncol()); | 
|---|
| 973 | } | 
|---|
| 974 |  | 
|---|
| 975 | return northog; | 
|---|
| 976 | } | 
|---|
| 977 |  | 
|---|
| 978 | void | 
|---|
| 979 | ReplSCMatrix::element_op(const Ref<SCElementOp>& op) | 
|---|
| 980 | { | 
|---|
| 981 | if (op->has_side_effects()) before_elemop(); | 
|---|
| 982 | SCMatrixBlockListIter i; | 
|---|
| 983 | for (i = blocklist->begin(); i != blocklist->end(); i++) { | 
|---|
| 984 | op->process_base(i.block()); | 
|---|
| 985 | } | 
|---|
| 986 | if (op->has_side_effects()) after_elemop(); | 
|---|
| 987 | if (op->has_collect()) op->collect(messagegrp()); | 
|---|
| 988 | } | 
|---|
| 989 |  | 
|---|
| 990 | void | 
|---|
| 991 | ReplSCMatrix::element_op(const Ref<SCElementOp2>& op, | 
|---|
| 992 | SCMatrix* m) | 
|---|
| 993 | { | 
|---|
| 994 | ReplSCMatrix *lm | 
|---|
| 995 | = require_dynamic_cast<ReplSCMatrix*>(m,"ReplSCMatrix::element_op"); | 
|---|
| 996 |  | 
|---|
| 997 | if (!rowdim()->equiv(lm->rowdim()) || !coldim()->equiv(lm->coldim())) { | 
|---|
| 998 | ExEnv::errn() << indent << "ReplSCMatrix: bad element_op" << endl; | 
|---|
| 999 | abort(); | 
|---|
| 1000 | } | 
|---|
| 1001 | if (op->has_side_effects()) before_elemop(); | 
|---|
| 1002 | if (op->has_side_effects_in_arg()) lm->before_elemop(); | 
|---|
| 1003 | SCMatrixBlockListIter i, j; | 
|---|
| 1004 | for (i = blocklist->begin(), j = lm->blocklist->begin(); | 
|---|
| 1005 | i != blocklist->end(); | 
|---|
| 1006 | i++, j++) { | 
|---|
| 1007 | op->process_base(i.block(), j.block()); | 
|---|
| 1008 | } | 
|---|
| 1009 | if (op->has_side_effects()) after_elemop(); | 
|---|
| 1010 | if (op->has_side_effects_in_arg()) lm->after_elemop(); | 
|---|
| 1011 | if (op->has_collect()) op->collect(messagegrp()); | 
|---|
| 1012 | } | 
|---|
| 1013 |  | 
|---|
| 1014 | void | 
|---|
| 1015 | ReplSCMatrix::element_op(const Ref<SCElementOp3>& op, | 
|---|
| 1016 | SCMatrix* m,SCMatrix* n) | 
|---|
| 1017 | { | 
|---|
| 1018 | ReplSCMatrix *lm | 
|---|
| 1019 | = require_dynamic_cast<ReplSCMatrix*>(m,"ReplSCMatrix::element_op"); | 
|---|
| 1020 | ReplSCMatrix *ln | 
|---|
| 1021 | = require_dynamic_cast<ReplSCMatrix*>(n,"ReplSCMatrix::element_op"); | 
|---|
| 1022 |  | 
|---|
| 1023 | if (!rowdim()->equiv(lm->rowdim()) || !coldim()->equiv(lm->coldim()) || | 
|---|
| 1024 | !rowdim()->equiv(ln->rowdim()) || !coldim()->equiv(ln->coldim())) { | 
|---|
| 1025 | ExEnv::errn() << indent << "ReplSCMatrix: bad element_op" << endl; | 
|---|
| 1026 | abort(); | 
|---|
| 1027 | } | 
|---|
| 1028 | if (op->has_side_effects()) before_elemop(); | 
|---|
| 1029 | if (op->has_side_effects_in_arg1()) lm->before_elemop(); | 
|---|
| 1030 | if (op->has_side_effects_in_arg2()) ln->before_elemop(); | 
|---|
| 1031 | SCMatrixBlockListIter i, j, k; | 
|---|
| 1032 | for (i = blocklist->begin(), | 
|---|
| 1033 | j = lm->blocklist->begin(), | 
|---|
| 1034 | k = ln->blocklist->begin(); | 
|---|
| 1035 | i != blocklist->end(); | 
|---|
| 1036 | i++, j++, k++) { | 
|---|
| 1037 | op->process_base(i.block(), j.block(), k.block()); | 
|---|
| 1038 | } | 
|---|
| 1039 | if (op->has_side_effects()) after_elemop(); | 
|---|
| 1040 | if (op->has_side_effects_in_arg1()) lm->after_elemop(); | 
|---|
| 1041 | if (op->has_side_effects_in_arg2()) ln->after_elemop(); | 
|---|
| 1042 | if (op->has_collect()) op->collect(messagegrp()); | 
|---|
| 1043 | } | 
|---|
| 1044 |  | 
|---|
| 1045 | // from Ed Seidl at the NIH | 
|---|
| 1046 | void | 
|---|
| 1047 | ReplSCMatrix::vprint(const char *title, ostream& os, int prec) const | 
|---|
| 1048 | { | 
|---|
| 1049 | int ii,jj,kk,nn; | 
|---|
| 1050 | int i,j; | 
|---|
| 1051 | int lwidth,width; | 
|---|
| 1052 | double max=this->maxabs(); | 
|---|
| 1053 |  | 
|---|
| 1054 | if (messagegrp()->me() != 0) return; | 
|---|
| 1055 |  | 
|---|
| 1056 | max = (max==0.0) ? 1.0 : log10(max); | 
|---|
| 1057 | if (max < 0.0) max=1.0; | 
|---|
| 1058 |  | 
|---|
| 1059 | lwidth = prec + 5 + (int) max; | 
|---|
| 1060 | width = 75/(lwidth+SCFormIO::getindent(os)); | 
|---|
| 1061 |  | 
|---|
| 1062 | os.setf(ios::fixed,ios::floatfield); os.precision(prec); | 
|---|
| 1063 | os.setf(ios::right,ios::adjustfield); | 
|---|
| 1064 |  | 
|---|
| 1065 | if (title) | 
|---|
| 1066 | os << endl << indent << title << endl; | 
|---|
| 1067 | else | 
|---|
| 1068 | os << endl; | 
|---|
| 1069 |  | 
|---|
| 1070 | if (nrow()==0 || ncol()==0) { | 
|---|
| 1071 | os << indent << "empty matrix" << endl; | 
|---|
| 1072 | return; | 
|---|
| 1073 | } | 
|---|
| 1074 |  | 
|---|
| 1075 | for (ii=jj=0;;) { | 
|---|
| 1076 | ii++; jj++; | 
|---|
| 1077 | kk=width*jj; | 
|---|
| 1078 | nn = (ncol() > kk) ? kk : ncol(); | 
|---|
| 1079 |  | 
|---|
| 1080 | // print column indices | 
|---|
| 1081 | os << indent; | 
|---|
| 1082 | for (i=ii; i <= nn; i++) | 
|---|
| 1083 | os << setw(lwidth) << i; | 
|---|
| 1084 | os << endl; | 
|---|
| 1085 |  | 
|---|
| 1086 | // print the rows | 
|---|
| 1087 | for (i=0; i < nrow() ; i++) { | 
|---|
| 1088 | os << setw(5) << i+1; | 
|---|
| 1089 | for (j=ii-1; j < nn; j++) | 
|---|
| 1090 | os << setw(lwidth) << rows[i][j]; | 
|---|
| 1091 | os << endl; | 
|---|
| 1092 | } | 
|---|
| 1093 | os << endl; | 
|---|
| 1094 |  | 
|---|
| 1095 | if (ncol() <= kk) { | 
|---|
| 1096 | os.flush(); | 
|---|
| 1097 | return; | 
|---|
| 1098 | } | 
|---|
| 1099 |  | 
|---|
| 1100 | ii=kk; | 
|---|
| 1101 | } | 
|---|
| 1102 | } | 
|---|
| 1103 |  | 
|---|
| 1104 | Ref<SCMatrixSubblockIter> | 
|---|
| 1105 | ReplSCMatrix::local_blocks(SCMatrixSubblockIter::Access access) | 
|---|
| 1106 | { | 
|---|
| 1107 | return new ReplSCMatrixListSubblockIter(access, blocklist, | 
|---|
| 1108 | messagegrp(), | 
|---|
| 1109 | matrix, d1->n()*d2->n()); | 
|---|
| 1110 | } | 
|---|
| 1111 |  | 
|---|
| 1112 | Ref<SCMatrixSubblockIter> | 
|---|
| 1113 | ReplSCMatrix::all_blocks(SCMatrixSubblockIter::Access access) | 
|---|
| 1114 | { | 
|---|
| 1115 | if (access == SCMatrixSubblockIter::Write) { | 
|---|
| 1116 | ExEnv::errn() << indent << "ReplSCMatrix::all_blocks: " | 
|---|
| 1117 | << "Write access permitted for local blocks only" | 
|---|
| 1118 | << endl; | 
|---|
| 1119 | abort(); | 
|---|
| 1120 | } | 
|---|
| 1121 | Ref<SCMatrixBlockList> allblocklist = new SCMatrixBlockList(); | 
|---|
| 1122 | allblocklist->insert(new SCMatrixRectSubBlock(0, d1->n(), d1->n(), | 
|---|
| 1123 | 0, d2->n(), matrix)); | 
|---|
| 1124 | return new ReplSCMatrixListSubblockIter(access, allblocklist, | 
|---|
| 1125 | messagegrp(), | 
|---|
| 1126 | matrix, d1->n()*d2->n()); | 
|---|
| 1127 | } | 
|---|
| 1128 |  | 
|---|
| 1129 | Ref<ReplSCMatrixKit> | 
|---|
| 1130 | ReplSCMatrix::skit() | 
|---|
| 1131 | { | 
|---|
| 1132 | return dynamic_cast<ReplSCMatrixKit*>(kit().pointer()); | 
|---|
| 1133 | } | 
|---|
| 1134 |  | 
|---|
| 1135 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 1136 |  | 
|---|
| 1137 | // Local Variables: | 
|---|
| 1138 | // mode: c++ | 
|---|
| 1139 | // c-file-style: "CLJ" | 
|---|
| 1140 | // End: | 
|---|