testContext.C 3.21 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
// testContext.c
//
////////////////////////////////////////////////////////////////////////////////

#include <cassert>
#include <iostream>
#include <vector>
Francois Gygi committed
22
#include <cstdlib>
Francois Gygi committed
23 24
using namespace std;

25
#ifdef USE_MPI
Francois Gygi committed
26 27 28 29 30 31 32 33 34
#include <mpi.h>
#endif

#include "Context.h"

int main(int argc, char **argv)
{
  int mype;
  int npes;
35
#ifdef USE_MPI
Francois Gygi committed
36 37 38 39

  MPI_Init(&argc,&argv);
  MPI_Comm_size(MPI_COMM_WORLD, &npes);
  MPI_Comm_rank(MPI_COMM_WORLD, &mype);
40

41 42
  int nr = atoi(argv[1]);
  int nc = atoi(argv[2]);
43

44
  { // start Context scope
45

46
    Context ctxt;
Francois Gygi committed
47 48 49 50 51
    for ( int i = 0; i < npes; i++ )
    {
      MPI_Barrier(MPI_COMM_WORLD);
      if ( i == mype )
        cout << mype << ":" << ctxt.mype() << ":" << ctxt.myproc()
52
         << " base: " << ctxt;
Francois Gygi committed
53
    }
54 55

    vector<Context*> c;
56

57
    c.push_back(new Context(nr,nc));
Francois Gygi committed
58
#if 0
59 60
    if ( nr >= 2 && nc >= 2 )
      c.push_back(new Context(*c[0],2,2,1,1));
61
    for ( int icol = 0; icol < c[0]->npcol(); icol++ )
Francois Gygi committed
62
    {
63 64
      ctxt.barrier();
      c.push_back(new Context(*c[0],c[0]->nprow(),1,0,icol));
Francois Gygi committed
65
    }
Francois Gygi committed
66
#endif
67

Francois Gygi committed
68 69
    for ( int i = 0; i < c.size(); i++ )
    {
70
      Context* pc = c[i];
Francois Gygi committed
71 72 73 74 75
      for ( int i = 0; i < npes; i++ )
      {
        MPI_Barrier(MPI_COMM_WORLD);
        if ( i == mype )
          cout << mype << ":" << pc->mype() << ":" << pc->myproc()
76
           << " at (" << pc->myrow() << "," << pc->mycol() << ")"
77
           << " in c" << i << ": " << *pc;
Francois Gygi committed
78
      }
Francois Gygi committed
79
    }
80

81 82 83 84 85
#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);
86
    cout << mype << ": mype_in_c1: " << mype_in_c1
87 88
         << " size_of_c1=" << size_of_c1
         << " comm[c1]=" << comm << endl;
89

Francois Gygi committed
90
    // test dgsum2d function
91 92 93 94 95 96
    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;
97

98 99 100 101 102
#endif
    for ( int i = 0; i < c.size(); i++ )
    {
      delete c[i];
    }
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
//   // 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;
//   }
119

120
#if 0
Francois Gygi committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  }
#endif
  } // end Context scope

  MPI_Finalize();

#else

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

#endif
}