jade.C 20.2 KB
Newer Older
Francois Gygi committed
1 2
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
3 4 5 6
// Copyright (c) 2008 The Regents of the University of California
//
// This file is part of Qbox
//
Francois Gygi committed
7 8
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
Francois Gygi committed
9 10 11 12 13 14
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// jade.C
//
////////////////////////////////////////////////////////////////////////////////

#include <cmath>
#include <cassert>
#include <vector>
#include <deque>
#include <algorithm>
#include <limits> // epsilon
#include <iostream>
#include <iomanip>
#ifdef USE_MPI
#include <mpi.h>
#endif

#ifdef SCALAPACK
#include "blacs.h"
#endif

#include "Context.h"
#include "Matrix.h"
#include "blas.h"
#include "Timer.h"
using namespace std;

41
int jade(int maxsweep, double tol, vector<DoubleMatrix*> a,
Francois Gygi committed
42 43 44 45 46 47
  DoubleMatrix& u, vector<vector<double> >& adiag)
{
  Timer tm_comm;
  const bool debug_diag_sum = false;

  const double eps = numeric_limits<double>::epsilon();
48 49 50 51 52 53 54
  if (tol<eps)
  {
    if ( u.context().onpe0() )
    {
        cout << " warning: jade tol=" << tol << " < eps=" << eps << endl;
    }
  }
Francois Gygi committed
55 56 57
  const Context& ctxt = u.context();
  // The input matrices are *a[k]
  // the orthogonal transformation is returned in u
58 59
  // on exit, the matrices a[k] are maximally diagonal,
  // u contains the orthogonal transformation
Francois Gygi committed
60 61 62 63 64 65 66 67 68 69 70
  // adiag[k][i] contains the diagonal elements of the a[k]'s

  for ( int k = 0; k < a.size(); k++ )
  {
    assert(a[k]->context() == u.context());
    assert(a[k]->m()==a[k]->n());
    assert(a[k]->m()==u.n());
    assert(a[k]->m()==u.m());
    assert(a[k]->mb()==u.mb());
    assert(a[k]->nb()==u.nb());
  }
71

72 73
  int mloc = a[0]->mloc();
  int nloc = a[0]->nloc();
Francois Gygi committed
74
  //cout << ctxt.mype() << ": nloc: " << nloc << endl;
75

Francois Gygi committed
76 77 78 79 80 81 82
  // identify the last active process column
  // process columns beyond that column do not have any elements of a[k]
  // compute num_nblocks = total number of column blocks
  // if num_nblocks >= ctxt.npcol(), all process columns are active
  // otherwise, the last active process column has index num_nblocks-1
  const int num_nblocks = u.n() / u.nb() + ( u.n()%u.nb() == 0 ? 0 : 1 );
  const int last_active_process_col = min(ctxt.npcol()-1, num_nblocks-1);
83

Francois Gygi committed
84 85
  // initialize u with the identity
  u.identity();
86

Francois Gygi committed
87 88 89 90 91 92 93 94 95 96
  // eigenvalue array
  adiag.resize(a.size());
  for ( int k = 0; k < a.size(); k++ )
    adiag[k].resize(a[k]->n());

  // check if the local number of rows is odd
  const bool nloc_odd = ( a[0]->nloc()%2 != 0 );

  // if nloc is odd, auxiliary arrays are created to host an extra column
  // for both a[k] and u
97
  vector<vector<double> > a_aux(a.size());
Francois Gygi committed
98 99 100 101 102 103 104 105 106
  vector<double> u_aux;
  if ( nloc_odd )
  {
    for ( int k = 0; k < a.size(); k++ )
      a_aux[k].resize(mloc);
    u_aux.resize(mloc);
  }

  // compute local number of pairs nploc
107
  const int nploc = (a[0]->nloc()+1)/2;
Francois Gygi committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  // dimension of top and bot arrays is nploc: local number of pairs
  deque<int> top(nploc), bot(nploc);

  // compute total number of pairs np
  int np = nploc;
  ctxt.isum('r',1,1,&np,1);
  //cout << ctxt.mype() << ": np=" << np << endl;
  // initialize top and bot arrays
  // the pair i is (top[i],bot[i])
  // top[i] is the local index of the top column of pair i
  // bot[i] is the local index of the bottom column of pair i
  for ( int i = 0; i < nploc; i++ )
    top[i] = i;
  for ( int i = 0; i < nploc; i++ )
    bot[nploc-i-1] = nploc+i;
  // if top[i] or bot[i] == nloc,the data resides in the array a_aux or u_aux
124

Francois Gygi committed
125
  // jglobal: global column index
126
  // jglobal[i] is the global column index of the column residing in
Francois Gygi committed
127 128
  // the local vector i. If nloc_odd and i==2*nploc-1, jglobal[i] == -1
  vector<int> jglobal(2*nploc,-1);
129 130
  for ( int jblock = 0; jblock < a[0]->nblocks(); jblock++ )
    for ( int y = 0; y < a[0]->nbs(jblock); y++ )
Francois Gygi committed
131
      jglobal[y + jblock*a[0]->nb()] = a[0]->j(jblock,y);
132

Francois Gygi committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  // store addresses of columns of a and of u in acol and ucol
  vector<vector<double*> > acol(a.size());
  vector<double*> ucol(2*nploc);
  for ( int k = 0; k < a.size(); k++ )
  {
    acol[k].resize(2*nploc);
    for ( int i = 0; i < a[k]->nloc(); i++ )
      acol[k][i] = a[k]->valptr(i*a[k]->mloc());
    // if nloc is odd, store the address of vector 2*nploc-1
    if ( nloc_odd )
      acol[k][2*nploc-1] = &a_aux[k][0];
  }
  for ( int i = 0; i < u.nloc(); i++ )
    ucol[i] = u.valptr(i*u.mloc());
  // if nloc is odd, store the address of vector 2*nploc-1
  if ( nloc_odd )
    ucol[2*nploc-1] = &u_aux[0];
150

Francois Gygi committed
151 152 153 154
  //for ( int i = 0; i < acol.size(); i++ )
  //  cout << ctxt.mype() << ": acol[" << i << "]=" << acol[i] << endl;
  //for ( int i = 0; i < ucol.size(); i++ )
  //  cout << ctxt.mype() << ": ucol[" << i << "]=" << ucol[i] << endl;
155

Francois Gygi committed
156 157
  // the vectors of the pair (top[i],bot[i]) are located at
  // addresses acol[top[i]] and acol[bot[i]]
158

Francois Gygi committed
159 160
  bool done = false;
  int nsweep = 0;
161 162 163 164 165 166
  // allocate matrix element packed array apq
  // apq[3*ipair   + k*3*nploc] = apq[k][ipair]
  // apq[3*ipair+1 + k*3*nploc] = app[k][ipair]
  // apq[3*ipair+2 + k*3*nploc] = aqq[k][ipair]
  vector<double> apq(a.size()*3*nploc);

Francois Gygi committed
167 168 169 170
  double diag_sum = 0.0;
#ifdef DEBUG
  double previous_diag_sum = 0.0;
#endif
Francois Gygi committed
171 172 173 174 175 176 177
  while ( !done )
  {
    // sweep: process local pairs and rotate 2*np-1 times
    nsweep++;
    double diag_change = 0.0;
    for ( int irot = 0; irot < 2*np-1; irot++ )
    {
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
      //cout << ctxt.mype() << ": top[i]: ";
      //for ( int i = 0; i < nploc; i++ )
      //  cout << setw(3) << top[i];
      //cout << endl;

      //cout << ctxt.mype() << ": bot[i]: ";
      //for ( int i = 0; i < nploc; i++ )
      //  cout << setw(3) << bot[i];
      //cout << endl;

      //cout << ctxt.mype() << ": jglobal[top[i]]: ";
      //for ( int i = 0; i < nploc; i++ )
      //  cout << setw(3) << jglobal[top[i]];
      //cout << endl;

      //cout << ctxt.mype() << ": jglobal[bot[i]]: ";
      //for ( int i = 0; i < nploc; i++ )
      //  cout << setw(3) << jglobal[bot[i]];
      //cout << endl;

Francois Gygi committed
198 199 200 201 202
      // perform Jacobi rotations for all local pairs

      // compute off-diagonal matrix elements apq for all pairs
      // skip the pair if one or both of the vectors is a dummy vector
      // i.e. a vector having jglobal==-1
203

Francois Gygi committed
204
#pragma omp parallel for
Francois Gygi committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
      for ( int k = 0; k < a.size(); k++ )
      {
        for ( int ipair = 0; ipair < nploc; ipair++ )
        {
          const int iapq = 3*ipair + k*3*nploc;
          //cout << ctxt.mype() << ": computing apq for global pair "
          //     << jglobal[top[ipair]] << " " << jglobal[bot[ipair]] << endl;
          apq[iapq]   = 0.0;
          apq[iapq+1] = 0.0;
          apq[iapq+2] = 0.0;
          if ( jglobal[top[ipair]] >= 0 && jglobal[bot[ipair]] >= 0 )
          {
            const double *ap = acol[k][top[ipair]];
            const double *aq = acol[k][bot[ipair]];
            const double *up = ucol[top[ipair]];
            const double *uq = ucol[bot[ipair]];
            int one = 1;
            apq[iapq]   = ddot(&mloc,ap,&one,uq,&one);
            apq[iapq+1] = ddot(&mloc,ap,&one,up,&one);
            apq[iapq+2] = ddot(&mloc,aq,&one,uq,&one);
          }
        }
      } // for k
228
      // apq now contains partial sums of matrix elements
Francois Gygi committed
229
      tm_comm.start();
230 231 232
      int len = apq.size();
      ctxt.dsum('c',len,1,&apq[0],len);
      tm_comm.stop();
Francois Gygi committed
233
      // apq now contains the matrix elements
234

Francois Gygi committed
235
#pragma omp parallel for
Francois Gygi committed
236 237 238 239 240 241 242 243 244 245
      for ( int ipair = 0; ipair < nploc; ipair++ )
      {
        if ( jglobal[top[ipair]] >= 0 &&
             jglobal[bot[ipair]] >= 0 )
        {
          // compute rotation sine and cosine
          // Cardoso-Souloumiac expressions for the rotation angle

          // compute 2x2 matrix g
          double g11 = 0.0, g12 = 0.0, g22 = 0.0;
246

Francois Gygi committed
247 248 249 250 251
          for ( int k = 0; k < a.size(); k++ )
          {
            const int iapq = 3*ipair + k*3*nploc;
            const double app = apq[iapq+1];
            const double aqq = apq[iapq+2];
252

Francois Gygi committed
253 254
            const double h1 = app - aqq;
            const double h2 = 2.0 * apq[iapq];
255

Francois Gygi committed
256 257
            g11 += h1 * h1;
            g12 += h1 * h2;
258
            g22 += h2 * h2;
Francois Gygi committed
259
          }
260

Francois Gygi committed
261 262
          // the matrix g is real, symmetric
          // compute Jacobi rotation diagonalizing g
263

Francois Gygi committed
264 265 266 267 268 269 270 271 272 273 274 275 276
          double c = 1.0, s = 0.0, e1 = g11, e2 = g22;
          if ( g12*g12 > eps * eps * fabs(g11*g22) )
          {
            double tau = 0.5 * ( g22 - g11 ) / g12;
            double t = 1.0 / ( fabs(tau) + sqrt(1.0 + tau*tau));
            if ( tau < 0.0 ) t *= -1.0;
            c = 1.0 / sqrt(1.0 + t*t);
            s = t * c;

            // eigenvalues
            e1 -= t * g12;
            e2 += t * g12;
          }
277

Francois Gygi committed
278 279 280 281 282 283 284 285 286 287 288 289
          // components of eigenvector associated with the largest eigenvalue
          double x,y;
          if ( e1 > e2 )
          {
            x = c;
            y = -s;
          }
          else
          {
            x = s;
            y = c;
          }
290

Francois Gygi committed
291 292 293 294 295 296
          // choose eigenvector with x positive to ensure small angle
          if ( x < 0.0 )
          {
            x = -x;
            y = -y;
          }
297

Francois Gygi committed
298 299 300 301
          // compute Jacobi rotation R(p,q)
          c = sqrt(0.5*(x+1.0));
          s = y / sqrt(2.0*(x+1.0));

302 303 304 305 306 307
          // apply the rotation R(p,q)
          //
          //           |  c   s |
          //  R(p,q) = |        |
          //           | -s   c |

Francois Gygi committed
308 309
          // U := U * R(p,q)^T
          // A := A * R(p,q)^T
310

Francois Gygi committed
311
          // apply rotation to columns of a and u
312

Francois Gygi committed
313 314 315 316
          // the drot function computes
          // c*x + s*y -> x
          //-s*x + c*y -> y
          // call drot with args c, -s
317 318

          //cout << " p=" << jglobal[top[ipair]]
Francois Gygi committed
319
          //     << " q=" << jglobal[bot[ipair]]
320
          //     << " g11=" << g11 << " g22=" << g22
Francois Gygi committed
321 322 323 324 325 326
          //     << " g12=" << g12 << endl;
          //cout << " c=" << c << " s=" << s << endl;

          int one = 1;
          for ( int k = 0; k < a.size(); k++ )
          {
327 328
            double *ap = acol[k][top[ipair]];
            double *aq = acol[k][bot[ipair]];
Francois Gygi committed
329 330
            drot(&mloc,ap,&one,aq,&one,&c,&s);
          }
331
          double *up = ucol[top[ipair]];
Francois Gygi committed
332 333
          double *uq = ucol[bot[ipair]];
          drot(&mloc,up,&one,uq,&one,&c,&s);
334 335

          // new value of off-diag element apq
Francois Gygi committed
336
          double diag_change_ipair = 0.0;
Francois Gygi committed
337 338 339 340 341 342
          for ( int k = 0; k < a.size(); k++ )
          {
            const int iapq = 3*ipair + k*3*nploc;
            const double app = apq[iapq+1];
            const double aqq = apq[iapq+2];
            const double apqnew = (c*c-s*s)*apq[iapq] - c*s*(app-aqq);
343

Francois Gygi committed
344 345
            // accumulate change in sum of squares of diag elements
            // note negative sign: decrease in offdiag is increase in diag
Francois Gygi committed
346
            diag_change_ipair -= 2.0 * ( apqnew*apqnew - apq[iapq]*apq[iapq] );
Francois Gygi committed
347
          }
Francois Gygi committed
348 349
#pragma omp critical
          diag_change -= diag_change_ipair;
Francois Gygi committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
        }
      } // for ipair

      // all local pairs have been processed

      // rotate top and bot arrays
      if ( nploc > 0 )
      {
        bot.push_back(top.back());
        top.pop_back();
        top.push_front(bot.front());
        bot.pop_front();

        // make rotation skip element 0 on the first process column
        // if my process column is zero, swap top[0] and top[1]
        if ( ctxt.mycol() == 0 )
        {
          if ( nploc > 1 )
          {
            int tmp = top[0];
            top[0] = top[1];
            top[1] = tmp;
          }
          else
          {
            // if there is only one local pair, exchange top[0] and bot[0]
            int tmp = top[0];
            top[0] = bot[0];
            bot[0] = tmp;
          }
        }

        // exchange columns of a[k] and u
383

Francois Gygi committed
384 385 386 387 388 389
        int rbufi_left, rbufi_right, sbufi_left, sbufi_right;
        // send buffers contain k columns of a and one of u
        int bufsize = (a.size()+1)*a[0]->mloc();
        vector<double> sbuf_left(bufsize), sbuf_right(bufsize);
        vector<double> rbuf_left(bufsize), rbuf_right(bufsize);

390
        // on each task except mycol==npcol-1
Francois Gygi committed
391 392 393
        // send jglobal[bot[nploc-1]] to the right
        // if jglobal != -1 send vector bot[nploc-1] to the right

394
        // on each task except mycol==npcol-1
Francois Gygi committed
395 396 397
        // recv jglobal from the right
        // if jglobal != -1 recv a vector from the right into bot[nploc-1]
        // set value of jglobal[bot[nploc-1]]
398 399

        // on each task except mycol==0
Francois Gygi committed
400 401 402
        // send jglobal[top[0]] to the left
        // if jglobal != -1 send vector top[0] to the left

403
        // on each task except mycol==0
Francois Gygi committed
404 405 406 407 408 409 410 411 412 413 414 415
        // recv jglobal from the left
        // if jglobal != -1 recv a vector from the left into top[0]
        // set value of jglobal[top[0]]

        // exchange jglobal values first
        tm_comm.start();
        if ( ctxt.mycol() < last_active_process_col )
        {
          sbufi_right = jglobal[bot[nploc-1]];
          ctxt.isend(1,1,&sbufi_right,1,ctxt.myrow(),ctxt.mycol()+1);
          ctxt.irecv(1,1,&rbufi_right,1,ctxt.myrow(),ctxt.mycol()+1);
          jglobal[bot[nploc-1]] = rbufi_right;
416
          //cout << ctxt.mype() << ": received jglobal="
Francois Gygi committed
417 418 419 420 421 422 423 424
          //     << jglobal[bot[nploc-1]] << " from right" << endl;
        }
        if ( ctxt.mycol() != 0 )
        {
          sbufi_left = jglobal[top[0]];
          ctxt.isend(1,1,&sbufi_left,1,ctxt.myrow(),ctxt.mycol()-1);
          ctxt.irecv(1,1,&rbufi_left,1,ctxt.myrow(),ctxt.mycol()-1);
          jglobal[top[0]] = rbufi_left;
425
          //cout << ctxt.mype() << ": received jglobal="
Francois Gygi committed
426 427 428 429 430 431 432 433 434 435 436 437
          //     << jglobal[top[0]] << " from left" << endl;
        }

        // exchange column vectors

        if ( ctxt.mycol() < last_active_process_col )
        {
          for ( int k = 0; k < a.size(); k++ )
          {
            memcpy(&sbuf_right[k*mloc],acol[k][bot[nploc-1]],
                   mloc*sizeof(double));
          }
438
          memcpy(&sbuf_right[a.size()*mloc], ucol[bot[nploc-1]],
Francois Gygi committed
439 440 441 442 443 444 445 446 447 448
                 mloc*sizeof(double) );
          ctxt.dsend(bufsize,1,&sbuf_right[0],bufsize,
                     ctxt.myrow(),ctxt.mycol()+1);
          ctxt.drecv(bufsize,1,&rbuf_right[0],bufsize,
                     ctxt.myrow(),ctxt.mycol()+1);
          for ( int k = 0; k < a.size(); k++ )
          {
            memcpy(acol[k][bot[nploc-1]],&rbuf_right[k*mloc],
                   mloc*sizeof(double));
          }
449
          memcpy(ucol[bot[nploc-1]], &rbuf_right[a.size()*mloc],
Francois Gygi committed
450
                 mloc*sizeof(double) );
451
          //cout << ctxt.mype() << ": received vector jglobal="
Francois Gygi committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
          //     << jglobal[bot[nploc-1]] << " from right" << endl;
        }
        if ( ctxt.mycol() != 0 )
        {
          for ( int k = 0; k < a.size(); k++ )
          {
            memcpy(&sbuf_left[k*mloc],acol[k][top[0]],mloc*sizeof(double));
          }
          memcpy(&sbuf_left[a.size()*mloc],ucol[top[0]],mloc*sizeof(double) );
          ctxt.dsend(bufsize,1,&sbuf_left[0],bufsize,
                     ctxt.myrow(),ctxt.mycol()-1);
          ctxt.drecv(bufsize,1,&rbuf_left[0],bufsize,
                     ctxt.myrow(),ctxt.mycol()-1);
          for ( int k = 0; k < a.size(); k++ )
          {
            memcpy(acol[k][top[0]],&rbuf_left[k*mloc],mloc*sizeof(double) );
          }
          memcpy(ucol[top[0]],&rbuf_left[a.size()*mloc],mloc*sizeof(double) );
470
          //cout << ctxt.mype() << ": received vector jglobal="
Francois Gygi committed
471 472 473 474 475 476 477 478
          //     << jglobal[top[0]] << " from left" << endl;
        }
        tm_comm.stop();
      } // if nploc > 0

      // end of step

    } // for irot
479

Francois Gygi committed
480 481 482 483
    // sweep is complete
    tm_comm.start();
    ctxt.dsum('r',1,1,&diag_change,1);
    tm_comm.stop();
484

Francois Gygi committed
485 486 487 488 489 490
    // compute sum of squares of diagonal elements

    if ( debug_diag_sum )
    {
      // compute sum of squares of diagonal elements using current values
      // (after rotation)
Francois Gygi committed
491
#ifdef DEBUG
Francois Gygi committed
492
      previous_diag_sum = diag_sum;
Francois Gygi committed
493
#endif
Francois Gygi committed
494
      diag_sum = 0.0;
495 496 497 498 499
      for ( int k = 0; k < a.size(); k++ )
      {
        for ( int ipair = 0; ipair < nploc; ipair++ )
        {
          double tmp[2] = { 0.0, 0.0 };
Francois Gygi committed
500 501
          // compute the diagonal elements
          // skip dummy vectors
502 503 504 505 506
          int one = 1;
          if ( jglobal[top[ipair]] >= 0 )
          {
            const double *ap = acol[k][top[ipair]];
            const double *up = ucol[top[ipair]];
Francois Gygi committed
507 508
            tmp[0] = ddot(&mloc,ap,&one,up,&one);
          }
509 510 511 512
          if ( jglobal[bot[ipair]] >= 0 )
          {
            const double *aq = acol[k][bot[ipair]];
            const double *uq = ucol[bot[ipair]];
Francois Gygi committed
513 514
            tmp[1] = ddot(&mloc,aq,&one,uq,&one);
          }
515 516
          // tmp now contains partial sums of app and aqq
          ctxt.dsum('c',2,1,tmp,2);
Francois Gygi committed
517
          // tmp now contains the diagonal elements app and aqq
518
          diag_sum += tmp[0]*tmp[0] + tmp[1]*tmp[1];
Francois Gygi committed
519 520
        }
      }
521
      ctxt.dsum('r',1,1,&diag_sum,1);
522
#ifdef DEBUG
Francois Gygi committed
523 524
      const double diag_sum_increase = diag_sum - previous_diag_sum;
      if ( ctxt.onpe0() )
525 526
        cout << " jade: nsweep=" << nsweep
             << " dsum: "
Francois Gygi committed
527
             << setw(15) << setprecision(10) << diag_sum
528
             << " dsum_inc: "
Francois Gygi committed
529
             << setw(15) << setprecision(10) << diag_sum_increase << endl;
530
#endif
Francois Gygi committed
531
    }
532
#ifdef DEBUG
Francois Gygi committed
533
    if ( ctxt.onpe0() )
534
      cout << " jade: nsweep=" << nsweep
Francois Gygi committed
535 536
           << " dchange: "
           << setw(15) << setprecision(10) << diag_change << endl;
537
#endif
Francois Gygi committed
538
    done = ( ( fabs(diag_change) < tol ) || ( nsweep >= maxsweep ) );
539

Francois Gygi committed
540
  } // while !done
541

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
#ifdef DEBUG
  //print final values of jglobal[top[i]] and jglobal[bot[i]]
  cout << "final jglobal[top/bot] values:" << endl;
  cout << ctxt.mype() << ": top: ";
  for ( int i = 0; i < nploc; i++ )
    cout << " " << jglobal[top[i]];
  cout << endl;
  cout << ctxt.mype() << ": bot: ";
  for ( int i = 0; i < nploc; i++ )
    cout << " " << jglobal[bot[i]];
  cout << endl;
  //print final values of top and bot
  cout << "final top/bot values:" << endl;
  cout << ctxt.mype() << ": top: ";
  for ( int i = 0; i < nploc; i++ )
    cout << " " << top[i];
  cout << endl;
  cout << ctxt.mype() << ": bot: ";
  for ( int i = 0; i < nploc; i++ )
    cout << " " << bot[i];
  cout << endl;
#endif
564

565 566 567 568
  // rotate columns of a and u to restore original order
  double *tmpmat = new double[nloc*mloc];
  // rotate columns of a
  for ( int k = 0; k < a.size(); k++ )
Francois Gygi committed
569
  {
570
    for ( int ipair = 0; ipair < nploc; ipair++ )
Francois Gygi committed
571
    {
572 573 574 575 576 577
      // copy columns of a[k] to temporary array tmpmat in original order
      if ( jglobal[top[ipair]] >= 0 )
      {
        memcpy(&tmpmat[ipair*mloc], acol[k][top[ipair]], mloc*sizeof(double));
      }
      if ( jglobal[bot[nploc-ipair-1]] >= 0 )
Francois Gygi committed
578
      {
579 580
        memcpy(&tmpmat[(nploc+ipair)*mloc],acol[k][bot[nploc-ipair-1]],
               mloc*sizeof(double));
Francois Gygi committed
581
      }
582
    }
583 584
    // copy tmpmat back to a[k]
    memcpy(acol[k][0],tmpmat,nloc*mloc*sizeof(double));
Francois Gygi committed
585
  }
586

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
  // rotate columns of u
  for ( int ipair = 0; ipair < nploc; ipair++ )
  {
    // copy columns of u to temporary array tmpmat in original order
    if ( jglobal[top[ipair]] >= 0 )
    {
      memcpy(&tmpmat[ipair*mloc], ucol[top[ipair]], mloc*sizeof(double));
    }
    if ( jglobal[bot[nploc-ipair-1]] >= 0 )
    {
      memcpy(&tmpmat[(nploc+ipair)*mloc],ucol[bot[nploc-ipair-1]],
             mloc*sizeof(double));
    }
  }
  // copy tmpmat back to u
  memcpy(ucol[0],tmpmat,nloc*mloc*sizeof(double));
  delete [] tmpmat;

Francois Gygi committed
605
  // compute diagonal values
606 607
  for ( int k = 0; k < a.size(); k++ )
  {
Francois Gygi committed
608 609
    for ( int i = 0; i < a[k]->n(); i++ )
      adiag[k][i] = 0.0;
610 611 612 613 614 615 616 617 618 619 620
    for ( int jblock = 0; jblock < a[k]->nblocks(); jblock++ )
      for ( int y = 0; y < a[k]->nbs(jblock); y++ )
      {
        // j is the global column index
        int j = a[k]->j(jblock,y);
        int jjj = y + jblock*a[k]->nb();
        const double *ap = a[k]->valptr(jjj*a[k]->mloc());
        const double *up = u.valptr(jjj*u.mloc());
        int one = 1;
        adiag[k][j] = ddot(&mloc,ap,&one,up,&one);
      }
Francois Gygi committed
621 622 623 624 625 626 627
    // adiag[k][i] now contains the partial sums of the diagonal elements of a
    tm_comm.start();
    ctxt.dsum(a[k]->n(),1,&adiag[k][0],a[k]->n());
    tm_comm.stop();
    // adiag[k] contains the diagonal elements of a[k]
    // u contains the orthogonal transformation minimizing the spread
  }
628

629
#ifdef DEBUG
Francois Gygi committed
630 631
  if ( ctxt.onpe0() )
    cout << " jade: comm time: " << tm_comm.real() << endl;
632
#endif
Francois Gygi committed
633 634
  return nsweep;
}