jade.C 20.1 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 171 172 173 174
  double diag_sum = 0.0, previous_diag_sum = 0.0;
  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++ )
    {
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
      //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
195 196 197 198 199
      // 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
200

Francois Gygi committed
201
#pragma omp parallel for
Francois Gygi committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
      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
225
      // apq now contains partial sums of matrix elements
Francois Gygi committed
226
      tm_comm.start();
227 228 229
      int len = apq.size();
      ctxt.dsum('c',len,1,&apq[0],len);
      tm_comm.stop();
Francois Gygi committed
230
      // apq now contains the matrix elements
231

Francois Gygi committed
232
#pragma omp parallel for
Francois Gygi committed
233 234 235 236 237 238 239 240 241 242
      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;
243

Francois Gygi committed
244 245 246 247 248
          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];
249

Francois Gygi committed
250 251
            const double h1 = app - aqq;
            const double h2 = 2.0 * apq[iapq];
252

Francois Gygi committed
253 254
            g11 += h1 * h1;
            g12 += h1 * h2;
255
            g22 += h2 * h2;
Francois Gygi committed
256
          }
257

Francois Gygi committed
258 259
          // the matrix g is real, symmetric
          // compute Jacobi rotation diagonalizing g
260

Francois Gygi committed
261 262 263 264 265 266 267 268 269 270 271 272 273
          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;
          }
274

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

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

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

299 300 301 302 303 304
          // apply the rotation R(p,q)
          //
          //           |  c   s |
          //  R(p,q) = |        |
          //           | -s   c |

Francois Gygi committed
305 306
          // U := U * R(p,q)^T
          // A := A * R(p,q)^T
307

Francois Gygi committed
308
          // apply rotation to columns of a and u
309

Francois Gygi committed
310 311 312 313
          // the drot function computes
          // c*x + s*y -> x
          //-s*x + c*y -> y
          // call drot with args c, -s
314 315

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

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

          // new value of off-diag element apq
Francois Gygi committed
333
          double diag_change_ipair = 0.0;
Francois Gygi committed
334 335 336 337 338 339
          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);
340

Francois Gygi committed
341 342
            // accumulate change in sum of squares of diag elements
            // note negative sign: decrease in offdiag is increase in diag
Francois Gygi committed
343
            diag_change_ipair -= 2.0 * ( apqnew*apqnew - apq[iapq]*apq[iapq] );
Francois Gygi committed
344
          }
Francois Gygi committed
345 346
#pragma omp critical
          diag_change -= diag_change_ipair;
Francois Gygi committed
347 348 349 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
        }
      } // 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
380

Francois Gygi committed
381 382 383 384 385 386
        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);

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

391
        // on each task except mycol==npcol-1
Francois Gygi committed
392 393 394
        // 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]]
395 396

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

400
        // on each task except mycol==0
Francois Gygi committed
401 402 403 404 405 406 407 408 409 410 411 412
        // 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;
413
          //cout << ctxt.mype() << ": received jglobal="
Francois Gygi committed
414 415 416 417 418 419 420 421
          //     << 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;
422
          //cout << ctxt.mype() << ": received jglobal="
Francois Gygi committed
423 424 425 426 427 428 429 430 431 432 433 434
          //     << 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));
          }
435
          memcpy(&sbuf_right[a.size()*mloc], ucol[bot[nploc-1]],
Francois Gygi committed
436 437 438 439 440 441 442 443 444 445
                 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));
          }
446
          memcpy(ucol[bot[nploc-1]], &rbuf_right[a.size()*mloc],
Francois Gygi committed
447
                 mloc*sizeof(double) );
448
          //cout << ctxt.mype() << ": received vector jglobal="
Francois Gygi committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
          //     << 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) );
467
          //cout << ctxt.mype() << ": received vector jglobal="
Francois Gygi committed
468 469 470 471 472 473 474 475
          //     << jglobal[top[0]] << " from left" << endl;
        }
        tm_comm.stop();
      } // if nploc > 0

      // end of step

    } // for irot
476

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

Francois Gygi committed
482 483 484 485 486 487 488 489
    // compute sum of squares of diagonal elements

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

Francois Gygi committed
535
  } // while !done
536

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
#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
559

560 561 562 563
  // 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
564
  {
565
    for ( int ipair = 0; ipair < nploc; ipair++ )
Francois Gygi committed
566
    {
567 568 569 570 571 572
      // 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
573
      {
574 575
        memcpy(&tmpmat[(nploc+ipair)*mloc],acol[k][bot[nploc-ipair-1]],
               mloc*sizeof(double));
Francois Gygi committed
576
      }
577
    }
578 579
    // copy tmpmat back to a[k]
    memcpy(acol[k][0],tmpmat,nloc*mloc*sizeof(double));
Francois Gygi committed
580
  }
581

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
  // 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
600
  // compute diagonal values
601 602
  for ( int k = 0; k < a.size(); k++ )
  {
Francois Gygi committed
603 604
    for ( int i = 0; i < a[k]->n(); i++ )
      adiag[k][i] = 0.0;
605 606 607 608 609 610 611 612 613 614 615
    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
616 617 618 619 620 621 622
    // 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
  }
623

624
#ifdef DEBUG
Francois Gygi committed
625 626
  if ( ctxt.onpe0() )
    cout << " jade: comm time: " << tm_comm.real() << endl;
627
#endif
Francois Gygi committed
628 629
  return nsweep;
}