testContext.C 2.57 KB
Newer Older
Francois Gygi committed
1 2 3 4 5
////////////////////////////////////////////////////////////////////////////////
//
// testContext.c
//
////////////////////////////////////////////////////////////////////////////////
6
// $Id: testContext.C,v 1.6 2006-03-07 07:09:06 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

#include <cassert>
#include <iostream>
#include <vector>
using namespace std;

#ifdef USE_MPI  
#include <mpi.h>
#endif

#include "Context.h"

int main(int argc, char **argv)
{
  int mype;
  int npes;
#ifdef USE_MPI  

  MPI_Init(&argc,&argv);
  MPI_Comm_size(MPI_COMM_WORLD, &npes);
  MPI_Comm_rank(MPI_COMM_WORLD, &mype);
  
29 30
  int nr = atoi(argv[1]);
  int nc = atoi(argv[2]);
Francois Gygi committed
31
  
32
  { // start Context scope
Francois Gygi committed
33
  
34 35 36 37 38
    Context ctxt;
    cout << mype << ":" << ctxt.mype() << ":" << ctxt.myproc() 
         << " base: " << ctxt;

    vector<Context*> c;
Francois Gygi committed
39
    
40
    c.push_back(new Context(nr,nc));
41 42
    if ( nr >= 2 && nc >= 2 )
      c.push_back(new Context(*c[0],2,2,1,1));
43
    for ( int icol = 0; icol < c[0]->npcol(); icol++ )
Francois Gygi committed
44
    {
45 46
      ctxt.barrier();
      c.push_back(new Context(*c[0],c[0]->nprow(),1,0,icol));
Francois Gygi committed
47 48 49 50
    }
    
    for ( int i = 0; i < c.size(); i++ )
    {
51 52 53 54
      Context* pc = c[i];
      cout << mype << ":" << pc->mype() << ":" << pc->myproc()
           << " at (" << pc->myrow() << "," << pc->mycol() << ")" 
           << " in c" << i << ": " << *pc;
Francois Gygi committed
55 56
    }
    
57 58 59 60 61 62 63 64
#if 0
    MPI_Comm comm = c[1]->comm();
    int mype_in_c1,size_of_c1;
    MPI_Comm_rank(comm,&mype_in_c1);
    MPI_Comm_size(comm,&size_of_c1);
    cout << mype << ": mype_in_c1: " << mype_in_c1 
         << " size_of_c1=" << size_of_c1
         << " comm[c1]=" << comm << endl;
Francois Gygi committed
65 66
    
    // test dgsum2d function
67 68 69 70 71 72
    double a = c[1]->mype();
    cout << c[1]->mype() << ": a     = " << a << endl;
    c[1]->dsum('R',1,1,&a,1);
    cout << c[1]->mype() << ": a_sum_row = " << a << endl;
    c[1]->dsum('C',1,1,&a,1);
    cout << c[1]->mype() << ": a_sum_all = " << a << endl;
Francois Gygi committed
73
    
74 75 76 77 78
#endif
    for ( int i = 0; i < c.size(); i++ )
    {
      delete c[i];
    }
Francois Gygi committed
79
    
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
//   // test reference counting
//   if ( npes%2 == 0 && npes >= 4 )
//   {
//     Context *c1 = new Context(npes/2,2);
//     cout << "c1: " << *c1 << endl;
//     Context *c2;
//     if ( c1->active() )
//       c2 = new Context(*c1,npes/2,npes/2,1);
//     else
//       c2 = 0;
//     // this line causes crash: Context *c2 = new Context(*c1,1,1,1);
//     delete c1;
//     if ( c2 != 0 ) cout << c2->mype() << " c2: " << *c2;
//     delete c2;
//   }
  
#if 0
Francois Gygi committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  }
#endif
  } // end Context scope

  MPI_Finalize();

#else

  mype=0;
  npes=1;
  {
    BlacsContext c1(1,1);
    cout << " c1.ictxt = " << c1.ictxt() << endl;
  }

#endif
}