Context.C 24.5 KB
Newer Older
Francois Gygi committed
1 2 3 4 5
////////////////////////////////////////////////////////////////////////////////
//
// Context.C
//
////////////////////////////////////////////////////////////////////////////////
6
// $Id: Context.C,v 1.12 2006-03-07 07:03:46 fgygi Exp $
Francois Gygi committed
7 8 9 10 11 12 13 14 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <string>
using namespace std;

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

#include "Context.h"

#ifndef SCALAPACK
void Cblacs_pinfo(int *mypnum, int *nprocs)
{
    *mypnum = 0;
    *nprocs = 1;
    return;
}

void Cblacs_get(int icontxt, int what, int *val)
{
    *val=0;
    return;
}

void Cblacs_gridinit(int *icontxt, char order[], int nprow, int npcol)
{
    *icontxt=0;
    return;
}

void Cblacs_gridmap(int *icontxt, int *pmap, int ldpmap, int nprow, int npcol)
{
    pmap[0]=0;
    return;
}

void Cblacs_barrier(int icontxt, char* scope)
{
    return;
}

void Cblacs_abort(int icontxt, int errornum)
{
    return;
}

void Cblacs_gridexit(int icontxt)
{
    return;
}

void Cblacs_gridinfo(int icontxt, int *nprow, int *npcol, 
                                  int *myprow, int *mypcol)
{
    *nprow=1; *npcol=1; *myprow=0; *mypcol=0; return;
}

int Cblacs_pnum(int icontxt, int prow, int pcol)
{ return 0;}

void Cdgesd2d(int icontxt,int m,int n,double *A,int lda,int rdest,int cdest)
{ return; }

void Cdgerv2d(int icontxt,int m,int n,double *A,int lda,int rdest,int cdest)
{ return; }

void Cdgsum2d(int icontxt, char* scope, char* top, 
              int m, int n, double *a, int lda, int rdest, int cdest)
{ return; }

void Cdgamx2d(int icontxt, char scope[], char top[],int m,int n,double *A,
              int lda, int *ra, int *ca, int rcflag, int rdest, int cdest)
{ return; }

void Cdgamn2d(int icontxt, char scope[], char top[],int m,int n,double *A,
              int lda, int *ra, int *ca, int rcflag, int rdest, int cdest)
{ return; }

void Cdgebs2d(int icontxt, char scope[], char top[],int m,int n,double *A,
              int lda)
{ return; }

void Cdgebr2d(int icontxt, char scope[], char top[],int m,int n,double *A,
              int lda, int rsrc, int csrc)
{ return; }

void Cigesd2d(int icontxt,int m,int n,int *A,int lda,int rdest,int cdest)
{ return; }

void Cigerv2d(int icontxt,int m,int n,int *A,int lda,int rdest,int cdest)
{ return; }

void Cigsum2d(int icontxt, char* scope, char* top, 
              int m, int n, int *a, int lda, int rdest, int cdest)
{ return; }

void Cigamx2d(int icontxt, char scope[], char top[],int m,int n,int *A,int lda, 
              int *ra, int *ca, int rcflag, int rdest, int cdest)
{ return; }

void Cigamn2d(int icontxt, char scope[], char top[],int m,int n,int *A,int lda, 
              int *ra, int *ca, int rcflag, int rdest, int cdest)
{ return; }

void Cigebs2d(int icontxt, char scope[], char top[],int m,int n,int *A,
              int lda)
{ return; }

void Cigebr2d(int icontxt, char scope[], char top[],int m,int n,int *A,
              int lda, int rsrc, int csrc)
{ return; }

#endif

#if USE_MPI
#include <mpi.h>
#else
typedef int MPI_Comm;
const int MPI_COMM_NULL = 0;
#endif

struct ContextRep
{ 
  private:
  
  int ictxt_;
  int myrow_;
  int mycol_;
  int nprow_;
  int npcol_;
  int size_;
  int myproc_;
  int mype_;
  bool onpe0_;
  bool active_;
 
  vector<int> pmap_;
  MPI_Comm comm_;
 
  // keep assignment and copy constructors private
  ContextRep& operator=(const Context& c);
  ContextRep(const Context& c);

  public:
  
  int ictxt() const { return ictxt_; }
  int myrow() const { return myrow_; }
  int mycol() const { return mycol_; }
  int nprow() const { return nprow_; }
  int npcol() const { return npcol_; }
 
  // number of processes in the context
  // returns -1 if current process is not part of this context
  int size() const { return size_; }
  // position of current process in row-major order
  // returns -1 if current process is not part of this context
  int myproc() const { return myproc_; }
  int mype() const { return mype_; }
169
  int pmap(int irow, int icol) const { return pmap_[irow+nprow_*icol]; }
Francois Gygi committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
 
  bool onpe0(void) const { return onpe0_; }
  bool active(void) const { return active_; }
  void abort(int ierr) const { Cblacs_abort(ictxt_,ierr); }
  void barrier(void) const { Cblacs_barrier(ictxt_,"A"); }
  void barrier(char scope) const { Cblacs_barrier(ictxt_,&scope); }
  
  void dsend(int m, int n, double* a, int lda, int rdest, int cdest) const
  {
    Cdgesd2d(ictxt_,m,n,a,lda,rdest,cdest);
  }
 
  void drecv(int m, int n, double* a, int lda, int rsrc, int csrc) const
  {
    Cdgerv2d(ictxt_,m,n,a,lda,rsrc,csrc);
  }
 
  void dsum(char scope, char topology, int m, int n, double* a, int lda,
            int rdest, int cdest) const
  {
    Cdgsum2d(ictxt_,&scope,&topology,m,n,a,lda,rdest,cdest);
  }
 
  void dmax(char scope, char topology, int m, int n, double* a, int lda,
            int rdest, int cdest) const
  {
    Cdgamx2d(ictxt_,&scope,&topology,m,n,a,lda,(int*)0,(int*)0,-1,rdest,cdest);
  }
 
  void dmax(char scope, char topology, int m, int n, double* a, int lda,
            int* ra, int* ca, int rcflag, int rdest, int cdest) const
  {
    Cdgamx2d(ictxt_,&scope,&topology,m,n,a,lda,ra,ca,rcflag,rdest,cdest);
  }
 
  void dmin(char scope, char topology, int m, int n, double* a, int lda,
            int* ra, int* ca, int rcflag, int rdest, int cdest) const
  {
    Cdgamn2d(ictxt_,&scope,&topology,m,n,a,lda,ra,ca,rcflag,rdest,cdest);
  }
 
  void dmin(char scope, char topology, int m, int n, double* a, int lda,
            int rdest, int cdest) const
  {
    Cdgamn2d(ictxt_,&scope,&topology,m,n,a,lda,(int*)0,(int*)0,-1,rdest,cdest);
  }
 
  void dbcast_send(char scope, char topology, 
                   int m, int n, double* a,int lda) const
  {
    Cdgebs2d(ictxt_,&scope,&topology,m,n,a,lda);
  }
 
  void dbcast_recv(char scope, char topology, int m, int n, double* a, int lda,
               int rsrc, int csrc) const
  {
    Cdgebr2d(ictxt_,&scope,&topology,m,n,a,lda,rsrc,csrc);
  }
 
  void isend(int m, int n, int* a, int lda, int rdest, int cdest) const
  {
    Cigesd2d(ictxt_,m,n,a,lda,rdest,cdest);
  }
 
  void irecv(int m, int n, int* a, int lda, int rsrc, int csrc) const
  {
    Cigerv2d(ictxt_,m,n,a,lda,rsrc,csrc);
  }
 
  void isum(char scope, char topology, int m, int n, int* a, int lda,
            int rdest, int cdest) const
  {
    Cigsum2d(ictxt_,&scope,&topology,m,n,a,lda,rdest,cdest);
  }
 
  void imax(char scope, char topology, int m, int n, int* a, int lda,
            int* ra, int* ca, int rcflag, int rdest, int cdest) const
  {
    Cigamx2d(ictxt_,&scope,&topology,m,n,a,lda,ra,ca,rcflag,rdest,cdest);
  }
 
  void imax(char scope, char topology, int m, int n, int* a, int lda,
            int rdest, int cdest) const
  {
    Cigamx2d(ictxt_,&scope,&topology,m,n,a,lda,(int*)0,(int*)0,-1,rdest,cdest);
  }
 
  void imin(char scope, char topology, int m, int n, int* a, int lda,
            int* ra, int* ca, int rcflag, int rdest, int cdest) const
  {
    Cigamn2d(ictxt_,&scope,&topology,m,n,a,lda,ra,ca,rcflag,rdest,cdest);
  }
 
  void imin(char scope, char topology, int m, int n, int* a, int lda,
            int rdest, int cdest) const
  {
    Cigamn2d(ictxt_,&scope,&topology,m,n,a,lda,(int*)0,(int*)0,-1,rdest,cdest);
  }
 
  void ibcast_send(char scope, char topology, 
                   int m, int n, int* a,int lda) const
  {
    Cigebs2d(ictxt_,&scope,&topology,m,n,a,lda);
  }
 
  void ibcast_recv(char scope, char topology, int m, int n, int* a, int lda,
               int rsrc, int csrc) const
  {
    Cigebr2d(ictxt_,&scope,&topology,m,n,a,lda,rsrc,csrc);
  }
  
  void string_send(string& s, int rdest, int cdest) const
  {
283
#if USE_MPI
Francois Gygi committed
284 285 286 287 288 289 290 291
    int len = s.size();
    isend(1,1,&len,1,rdest,cdest);
    int ilen = len/(sizeof(int)/sizeof(char));
    if ( len%(sizeof(int)/sizeof(char)) != 0 ) ilen++;
    int* ibuf = new int[ilen];
    s.copy((char*)ibuf,string::npos);
    isend(ilen,1,ibuf,1,rdest,cdest);
    delete [] ibuf;
292
#endif
Francois Gygi committed
293 294 295 296
  }
  
  void string_recv(string& s, int rsrc, int csrc) const
  {
297
#if USE_MPI
Francois Gygi committed
298 299 300 301 302 303 304 305 306
    int len = -1;
    irecv(1,1,&len,1,rsrc,csrc);
    int ilen = len/(sizeof(int)/sizeof(char));
    if ( len%(sizeof(int)/sizeof(char)) != 0 ) ilen++;
    int* ibuf = new int[ilen];
    irecv(ilen,1,ibuf,1,rsrc,csrc);
    s.resize(len);
    s.assign((char*)ibuf,len);
    delete [] ibuf;
307
#endif
Francois Gygi committed
308 309 310 311
  }
  
  void string_bcast(string& s, int isrc) const
  {
312
#if USE_MPI
Francois Gygi committed
313 314 315 316 317 318 319
    int len;
    if ( mype() == isrc )
    {
      len = s.length();
    }
    MPI_Bcast(&len,1,MPI_INT,isrc,comm());
    char* buf = new char[len+1];
Francois Gygi committed
320 321 322 323 324 325 326
    // s is initialized only on task isrc
    if ( mype() == isrc )
    {
      s.copy(buf,string::npos);
      buf[len]=0;
      assert(buf[len]=='\0');
    }
Francois Gygi committed
327 328 329
    MPI_Bcast(buf,len+1,MPI_CHAR,isrc,comm());
    s = buf;
    delete [] buf;
330
#endif
Francois Gygi committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344
  }
 
  bool operator==(const ContextRep& c) const
  { return (ictxt_==c.ictxt());}
 
  // MPI communicator for this context. Returns MPI_COMM_NULL if
  // this process is not part of the context
  MPI_Comm comm(void) const { return comm_; }
 
  // Constructors

  // default global context: construct a single-row global ContextRep
  explicit ContextRep();
 
345 346
  // global ContextRep of size nprow * npcol with column major order
  explicit ContextRep(int nprow, int npcol);
Francois Gygi committed
347
 
348 349 350 351
  // construct a ContextRep of size nprow*npcol using the processes
  // in context c starting at process (irstart,icstart) 
  explicit ContextRep(const ContextRep &c, int nprow, int npcol, 
    int irstart, int icstart);
Francois Gygi committed
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 383 384 385 386 387 388
 
  ~ContextRep();
 
  void print(ostream& os) const;
};

////////////////////////////////////////////////////////////////////////////////
ContextRep::ContextRep() : ictxt_(-1), myrow_(-1), mycol_(-1)
{
  // construct a single row global context
  int nprocs;
  char order='R';
  Cblacs_pinfo( &mype_, &nprocs );
  nprow_ = 1;
  npcol_ = nprocs;

  Cblacs_get(0, 0, &ictxt_ );
  Cblacs_gridinit( &ictxt_, &order, nprow_, npcol_ );

  // get values of nprow_, npcol_, myrow_ and mycol_ in the new context
  if ( ictxt_ >= 0 )
    Cblacs_gridinfo(ictxt_, &nprow_, &npcol_, &myrow_, &mycol_);
  
  size_ = nprow_ * npcol_;
  myproc_ = myrow_ < 0 ? -1 : mycol_ + npcol_ * myrow_;
  onpe0_ = ( mype_ == 0 );
  active_ = ( ictxt_ >= 0 );

  pmap_.resize(size_);
  for ( int i = 0; i < size_; i++ )
    pmap_[i] = i;

#if USE_MPI
  MPI_Group group_world, subgroup;
  MPI_Comm_group(MPI_COMM_WORLD,&group_world);
  MPI_Group_incl(group_world,size_,&pmap_[0],&subgroup);
  MPI_Comm_create(MPI_COMM_WORLD,subgroup,&comm_);
389 390
  MPI_Group_free(&group_world);
  MPI_Group_free(&subgroup);
Francois Gygi committed
391 392 393 394 395 396
#else
  comm_ = 0;
#endif
}

////////////////////////////////////////////////////////////////////////////////
397
ContextRep::ContextRep(int nprow, int npcol) :
Francois Gygi committed
398 399
  ictxt_(-1), myrow_(-1), mycol_(-1), nprow_(nprow), npcol_(npcol)
{
400 401
  int nprocs;
  char order = 'C';
Francois Gygi committed
402 403 404
  Cblacs_pinfo( &mype_, &nprocs );
  if ( nprocs < nprow * npcol )
  {
405 406 407
    cout << " nprocs=" << nprocs << endl;
    cout << " Context nprow*npcol > nprocs" << endl;
    Cblacs_abort(ictxt_, 1);
Francois Gygi committed
408 409 410 411 412 413 414 415 416
  }
  Cblacs_get(0, 0, &ictxt_ );
  Cblacs_gridinit( &ictxt_, &order, nprow, npcol );

  // get values of nprow_, npcol_, myrow_ and mycol_ in the new context
  if ( ictxt_ >= 0 )
    Cblacs_gridinfo(ictxt_, &nprow_, &npcol_, &myrow_, &mycol_);
  
  size_ = nprow_ * npcol_;
417
  myproc_ = Cblacs_pnum(ictxt_,myrow_,mycol_);
Francois Gygi committed
418 419 420 421
  onpe0_ = ( mype_ == 0 );
  active_ = ( ictxt_ >= 0 );
  
  pmap_.resize(size_);
422 423 424 425 426 427 428 429
  // column-major order
  int i = 0;
  for ( int ic = 0; ic < npcol; ic++ )
    for ( int ir = 0; ir < nprow; ir++ )
    {
      pmap_[ir+nprow*ic] = i;
      i++;
    }
Francois Gygi committed
430 431 432 433 434 435

#if USE_MPI
  MPI_Group group_world, subgroup;
  MPI_Comm_group(MPI_COMM_WORLD,&group_world);
  MPI_Group_incl(group_world,size_,&pmap_[0],&subgroup);
  MPI_Comm_create(MPI_COMM_WORLD,subgroup,&comm_);
436 437
  MPI_Group_free(&group_world);
  MPI_Group_free(&subgroup);
Francois Gygi committed
438 439 440 441 442 443
#else
  comm_ = 0;
#endif
}
    
////////////////////////////////////////////////////////////////////////////////
444 445
ContextRep::ContextRep(const ContextRep& c, int nprow, int npcol, 
  int irstart, int icstart) :
Francois Gygi committed
446 447 448
ictxt_(-1), myrow_(-1), mycol_(-1), nprow_(nprow), npcol_(npcol)
{
  assert(c.active());
449
  vector<int> gmap_;
Francois Gygi committed
450 451 452 453
  
  int nprocs;
  Cblacs_pinfo( &mype_, &nprocs );
  // construct a (nprow*npcol) context using the processes in c
454 455 456
  // located at (irstart,icstart)
  if ( irstart < 0 || icstart < 0 || 
       irstart+nprow > c.nprow() || icstart+npcol > c.npcol() )
Francois Gygi committed
457
  {
458
    cout << " Context::Context: cut rectangle: invalid parameters" << endl;
459
    cout << irstart << " " << icstart << " " << nprow << " " << npcol << endl;
Francois Gygi committed
460 461 462
    Cblacs_abort(ictxt_, 1);
  }
  pmap_.resize(nprow*npcol);
463
  gmap_.resize(nprow*npcol);
Francois Gygi committed
464 465
  // build pmap
  int i = 0;
466 467 468 469 470 471 472
  for ( int ic = icstart; ic < icstart+npcol; ic++ )
    for ( int ir = irstart; ir < irstart+nprow; ir++ )
    {
      pmap_[i] = c.pmap(ir,ic);
      gmap_[i] = ic + c.npcol()*ir;
      i++;
    }
Francois Gygi committed
473 474
  
  Cblacs_get(c.ictxt(), 10, &ictxt_ );
475
  Cblacs_gridmap(&ictxt_,&gmap_[0],nprow,nprow,npcol);
Francois Gygi committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
  
  // get values of nprow_, npcol_, myrow_ and mycol_ in the new context
  if ( ictxt_ >= 0 )
    Cblacs_gridinfo(ictxt_, &nprow_, &npcol_, &myrow_, &mycol_);
  
  size_ = nprow_ * npcol_;
  myproc_ = myrow_ < 0 ? -1 : mycol_ + npcol_ * myrow_;
  onpe0_ = ( mype_ == 0 );
  active_ = ( ictxt_ >= 0 );
  
#if USE_MPI
  MPI_Group c_group, subgroup;
  MPI_Comm_group(c.comm(),&c_group);
  MPI_Group_incl(c_group,size_,&pmap_[0],&subgroup);
  MPI_Comm_create(c.comm(),subgroup,&comm_);
491 492
  MPI_Group_free(&c_group);
  MPI_Group_free(&subgroup);
Francois Gygi committed
493 494 495 496 497 498 499 500 501 502 503 504
#else
  comm_ = 0;
#endif
}

////////////////////////////////////////////////////////////////////////////////
ContextRep::~ContextRep()
{
  if ( myrow_ != -1 ) 
  {
    // cout << " ContextRep destructor called on ictxt = " << ictxt_ << endl;
    Cblacs_gridexit( ictxt_ );
505
#if USE_MPI
Francois Gygi committed
506
    MPI_Comm_free(&comm_);
507
#endif
Francois Gygi committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
  }
}

////////////////////////////////////////////////////////////////////////////////
void ContextRep::print(ostream& os) const
{
  if ( active_ )
  {
    os << " " << nprow_ << "x" << npcol_ << " ";
    os << "{ ";
    for ( int ir = 0; ir < nprow_; ir++ )
    {
      os << "{ ";
      for ( int ic = 0; ic < npcol_; ic++ )
      {
523
        os << pmap_[ir+nprow_*ic] << " ";
Francois Gygi committed
524 525 526
      }
      os << "} ";
    }
527 528 529 530 531 532
    //os << "} (ictxt=" << ictxt_ << ")" << endl;
    os << "} (ictxt=" << ictxt_ << ")";
    int handle;
    Cblacs_get(ictxt_,10,&handle);
    os << "(handle=" << handle << ")";
    os << "(comm=" << comm_<< ")" << endl;
Francois Gygi committed
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
  }
  else
  {
    os << " (inactive)" << endl;
  }
}

////////////////////////////////////////////////////////////////////////////////
class ContextImpl
{
  private:
  
  ContextRep* rep;
  int* pcount;
  
  public:
  
  ContextRep* operator->() { return rep; }
  ContextRep* get_rep(void) { return rep; }
  ContextImpl(ContextRep* pp) : rep(pp), pcount(new int(1)) {}
  ContextImpl(const ContextImpl& r) : rep(r.rep), pcount(r.pcount)
  { (*pcount)++; }
  
  ContextImpl& operator=(const ContextImpl& r)
  {
    if ( rep == r.rep ) return *this;
    if ( --(*pcount) == 0 )
    {
      delete rep;
      delete pcount;
    }
    rep = r.rep;
    pcount = r.pcount;
    (*pcount)++;
    return *this;
  }
  
  ~ContextImpl(void)
  {
    if ( --(*pcount) == 0 )
    {
      delete rep;
      delete pcount;
    }
  }
};

////////////////////////////////////////////////////////////////////////////////
Context::Context(void) : pimpl_(new ContextImpl(new ContextRep())) {}

////////////////////////////////////////////////////////////////////////////////
584 585
Context::Context(int nprow, int npcol):  
pimpl_(new ContextImpl(new ContextRep(nprow,npcol))) {}
Francois Gygi committed
586 587
 
////////////////////////////////////////////////////////////////////////////////
588 589 590 591
Context::Context(const Context &c, int nprow, int npcol, 
        int irstart, int icstart) : 
pimpl_(new ContextImpl(new ContextRep(*(c.pimpl_)->get_rep(),nprow,npcol,
irstart,icstart))) {}
Francois Gygi committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
 
////////////////////////////////////////////////////////////////////////////////
Context::~Context() { delete pimpl_; }

////////////////////////////////////////////////////////////////////////////////
Context::Context(const Context& c) : pimpl_(new ContextImpl(*(c.pimpl_))) {}

////////////////////////////////////////////////////////////////////////////////
Context& Context::operator=(const Context& rhs)
{
  if ( &rhs == this ) return *this;
  *pimpl_ = *(rhs.pimpl_);
  return *this;
}

////////////////////////////////////////////////////////////////////////////////
int Context::ictxt() const { return (*pimpl_)->ictxt(); }

////////////////////////////////////////////////////////////////////////////////
int Context::myrow() const { return (*pimpl_)->myrow(); }

////////////////////////////////////////////////////////////////////////////////
int Context::mycol() const { return (*pimpl_)->mycol(); }

////////////////////////////////////////////////////////////////////////////////
int Context::nprow() const { return (*pimpl_)->nprow(); }

////////////////////////////////////////////////////////////////////////////////
int Context::npcol() const { return (*pimpl_)->npcol(); }

////////////////////////////////////////////////////////////////////////////////
int Context::size() const { return (*pimpl_)->size(); }

////////////////////////////////////////////////////////////////////////////////
int Context::myproc() const { return (*pimpl_)->myproc(); }

////////////////////////////////////////////////////////////////////////////////
int Context::mype() const { return (*pimpl_)->mype(); }

////////////////////////////////////////////////////////////////////////////////
int Context::pmap(int irow, int icol) const
{ return (*pimpl_)->pmap(irow,icol); }

////////////////////////////////////////////////////////////////////////////////
bool Context::onpe0(void) const { return (*pimpl_)->onpe0(); }

////////////////////////////////////////////////////////////////////////////////
bool Context::active(void) const { return (*pimpl_)->active(); }

////////////////////////////////////////////////////////////////////////////////
void Context::abort(int ierr) const { (*pimpl_)->abort(ierr); }

////////////////////////////////////////////////////////////////////////////////
void Context::barrier(void) const { (*pimpl_)->barrier(); }

////////////////////////////////////////////////////////////////////////////////
void Context::barrier(char scope) const { (*pimpl_)->barrier(scope); }

////////////////////////////////////////////////////////////////////////////////
void Context::dsend(int m, int n, double* a, 
  int lda, int rdest, int cdest) const
{ (*pimpl_)->dsend(m,n,a,lda,rdest,cdest); }

void Context::drecv(int m, int n, double* a, 
  int lda, int rsrc, int csrc) const
{ (*pimpl_)->drecv(m,n,a,lda,rsrc,csrc); }

void Context::dsum(char scope, char topology, 
  int m, int n, double* a, int lda, int rdest, int cdest) const
{ (*pimpl_)->dsum(scope,topology,m,n,a,lda,rdest,cdest); }

void Context::dsum(char scope, int m, int n, double* a, int lda) const
{ (*pimpl_)->dsum(scope,' ',m,n,a,lda,-1,-1); }

void Context::dsum(int m, int n, double* a, int lda) const
{ (*pimpl_)->dsum('A',' ',m,n,a,lda,-1,-1); }

void Context::dmax(char scope, char topology, 
  int m, int n, double* a, int lda, int* ra, int* ca, int rcflag, 
  int rdest, int cdest) const
{ (*pimpl_)->dmax(scope,topology,m,n,a,lda,ra,ca,rcflag,rdest,cdest); }

void Context::dmax(char scope, char topology, 
  int m, int n, double* a, int lda, int rdest, int cdest) const
{ (*pimpl_)->dmax(scope,topology,m,n,a,lda,rdest,cdest); }

void Context::dmax(char scope, int m, int n, double* a, int lda) const
{ (*pimpl_)->dmax(scope,' ',m,n,a,lda,-1,-1); }

void Context::dmax(int m, int n, double* a, int lda) const
{ (*pimpl_)->dmax('A',' ',m,n,a,lda,-1,-1); }

void Context::dmin(char scope, char topology, 
  int m, int n, double* a, int lda, int* ra, int* ca, int rcflag, 
  int rdest, int cdest) const
{ (*pimpl_)->dmin(scope,topology,m,n,a,lda,ra,ca,rcflag,rdest,cdest); }

void Context::dmin(char scope, char topology, 
  int m, int n, double* a, int lda, int rdest, int cdest) const
{ (*pimpl_)->dmin(scope,topology,m,n,a,lda,rdest,cdest); }

void Context::dmin(char scope, int m, int n, double* a, int lda) const
{ (*pimpl_)->dmin(scope,' ',m,n,a,lda,-1,-1); }

void Context::dmin(int m, int n, double* a, int lda) const
{ (*pimpl_)->dmin('A',' ',m,n,a,lda,-1,-1); }

void Context::dbcast_send(char scope, char topology, 
  int m, int n, double* a, int lda) const
{ (*pimpl_)->dbcast_send(scope,topology,m,n,a,lda); }

void Context::dbcast_send(char scope, int m, int n, double* a, int lda) const
{ (*pimpl_)->dbcast_send(scope,' ',m,n,a,lda); }

void Context::dbcast_send(int m, int n, double* a, int lda) const
{ (*pimpl_)->dbcast_send('A',' ',m,n,a,lda); }

void Context::dbcast_recv(char scope, char topology, 
  int m, int n, double* a, int lda, int rsrc,int csrc) const
{ (*pimpl_)->dbcast_recv(scope,topology,m,n,a,lda,rsrc,csrc); }

void Context::dbcast_recv(char scope, 
  int m, int n, double* a, int lda, int rsrc, int csrc) const
{ (*pimpl_)->dbcast_recv(scope,' ',m,n,a,lda,rsrc,csrc); }

void Context::dbcast_recv(int m, int n, double* a, int lda, 
  int rsrc,int csrc) const
{ (*pimpl_)->dbcast_recv('A',' ',m,n,a,lda,rsrc,csrc); }

////////////////////////////////////////////////////////////////////////////////
void Context::isend(int m, int n, int* a, 
  int lda, int rdest, int cdest) const
{ (*pimpl_)->isend(m,n,a,lda,rdest,cdest); }

void Context::irecv(int m, int n, int* a, 
  int lda, int rsrc, int csrc) const
{ (*pimpl_)->irecv(m,n,a,lda,rsrc,csrc); }

void Context::isum(char scope, char topology, 
  int m, int n, int* a, int lda, int rdest, int cdest) const
{ (*pimpl_)->isum(scope,topology,m,n,a,lda,rdest,cdest); }

void Context::isum(char scope, int m, int n, int* a, int lda) const
{ (*pimpl_)->isum(scope,' ',m,n,a,lda,-1,-1); }

void Context::isum(int m, int n, int* a, int lda) const
{ (*pimpl_)->isum('A',' ',m,n,a,lda,-1,-1); }

void Context::imax(char scope, char topology, 
  int m, int n, int* a, int lda, int* ra, int* ca, int rcflag, 
  int rdest, int cdest) const
{ (*pimpl_)->imax(scope,topology,m,n,a,lda,ra,ca,rcflag,rdest,cdest); }

void Context::imax(char scope, char topology, 
  int m, int n, int* a, int lda, int rdest, int cdest) const
{ (*pimpl_)->imax(scope,topology,m,n,a,lda,rdest,cdest); }

void Context::imax(char scope, int m, int n, int* a, int lda) const
{ (*pimpl_)->imax(scope,' ',m,n,a,lda,-1,-1); }

void Context::imax(int m, int n, int* a, int lda) const
{ (*pimpl_)->imax('A',' ',m,n,a,lda,-1,-1); }

void Context::imin(char scope, char topology, 
  int m, int n, int* a, int lda, int* ra, int* ca, int rcflag, 
  int rdest, int cdest) const
{ (*pimpl_)->imin(scope,topology,m,n,a,lda,ra,ca,rcflag,rdest,cdest); }

void Context::imin(char scope, char topology, 
  int m, int n, int* a, int lda, int rdest, int cdest) const
{ (*pimpl_)->imin(scope,topology,m,n,a,lda,rdest,cdest); }

void Context::imin(char scope, int m, int n, int* a, int lda) const
{ (*pimpl_)->imin(scope,' ',m,n,a,lda,-1,-1); }

void Context::imin(int m, int n, int* a, int lda) const
{ (*pimpl_)->imin('A',' ',m,n,a,lda,-1,-1); }

void Context::ibcast_send(char scope, char topology, 
  int m, int n, int* a, int lda) const
{ (*pimpl_)->ibcast_send(scope,topology,m,n,a,lda); }

void Context::ibcast_send(char scope, int m, int n, int* a, int lda) const
{ (*pimpl_)->ibcast_send(scope,' ',m,n,a,lda); }

void Context::ibcast_send(int m, int n, int* a, int lda) const
{ (*pimpl_)->ibcast_send('A',' ',m,n,a,lda); }

void Context::ibcast_recv(char scope, char topology, 
  int m, int n, int* a, int lda, int rsrc,int csrc) const
{ (*pimpl_)->ibcast_recv(scope,topology,m,n,a,lda,rsrc,csrc); }

void Context::ibcast_recv(char scope, 
  int m, int n, int* a, int lda, int rsrc, int csrc) const
{ (*pimpl_)->ibcast_recv(scope,' ',m,n,a,lda,rsrc,csrc); }

void Context::ibcast_recv(int m, int n, int* a, int lda, 
  int rsrc,int csrc) const
{ (*pimpl_)->ibcast_recv('A',' ',m,n,a,lda,rsrc,csrc); }

void Context::string_send(string& s, int rdest, int cdest) const
{ (*pimpl_)->string_send(s,rdest,cdest); }

void Context::string_recv(string& s, int rsrc, int csrc) const
{ (*pimpl_)->string_recv(s,rsrc,csrc); }

void Context::string_bcast(string& s, int isrc) const
{ (*pimpl_)->string_bcast(s,isrc); }

////////////////////////////////////////////////////////////////////////////////
bool Context::operator==(const Context& ctxt) const
{ return ( (*pimpl_)->ictxt() == ctxt.ictxt() ); }

////////////////////////////////////////////////////////////////////////////////
MPI_Comm Context::comm(void) const { return (*pimpl_)->comm(); }
 
////////////////////////////////////////////////////////////////////////////////
void Context::print(ostream& os) const { (*pimpl_)->print(os);}

////////////////////////////////////////////////////////////////////////////////
ostream& operator<<(ostream& os, const Context& c)
{
  c.print(os);
  return os;
}