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

Francois Gygi committed
231 232 233 234 235 236 237 238 239 240
      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;
241

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

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

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

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

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

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

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

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

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

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

Francois Gygi committed
306
          // apply rotation to columns of a and u
307

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

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

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

          // new value of off-diag element apq
Francois Gygi committed
331 332 333 334 335 336
          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);
337

Francois Gygi committed
338 339 340 341 342 343 344 345 346 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
            // accumulate change in sum of squares of diag elements
            // note negative sign: decrease in offdiag is increase in diag
            diag_change -= 2.0 * ( apqnew*apqnew - apq[iapq]*apq[iapq] );
          }
        }
      } // 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
375

Francois Gygi committed
376 377 378 379 380 381
        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);

382
        // on each task except mycol==npcol-1
Francois Gygi committed
383 384 385
        // send jglobal[bot[nploc-1]] to the right
        // if jglobal != -1 send vector bot[nploc-1] to the right

386
        // on each task except mycol==npcol-1
Francois Gygi committed
387 388 389
        // 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]]
390 391

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

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

      // end of step

    } // for irot
471

Francois Gygi committed
472 473 474 475
    // sweep is complete
    tm_comm.start();
    ctxt.dsum('r',1,1,&diag_change,1);
    tm_comm.stop();
476

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

Francois Gygi committed
530
  } // while !done
531

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
#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
554

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

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

619
#ifdef DEBUG
Francois Gygi committed
620 621
  if ( ctxt.onpe0() )
    cout << " jade: comm time: " << tm_comm.real() << endl;
622
#endif
Francois Gygi committed
623 624
  return nsweep;
}