Zoltan2
Zoltan2_MachineRepresentation.hpp
Go to the documentation of this file.
00001 #ifndef _ZOLTAN2_MACHINEREPRESENTATION_HPP_
00002 #define _ZOLTAN2_MACHINEREPRESENTATION_HPP_
00003 
00004 #include <Teuchos_Comm.hpp>
00005 #include <Teuchos_CommHelpers.hpp>
00006 namespace Zoltan2{
00007 
00012 template <typename pcoord_t>
00013 class MachineRepresentation{
00014 
00015 public:
00016     int networkDim;
00017     int numProcs;
00018 
00019     pcoord_t **procCoords;
00020     RCP<const Comm<int> > comm;
00021 
00025     MachineRepresentation(RCP<const Comm<int> > comm_):
00026         networkDim(0), numProcs(comm_->getSize()), procCoords(0), comm(Teuchos::rcp_const_cast<const Comm<int> >(comm_)){
00027         // WIll need this constructor to be specific to RAAMP (MD).
00028         // Will need a default constructor using, e.g., GeometricGenerator
00029         // or nothing at all, for when RAAMP is not available as TPL.
00030         //
00031         // (AG) In addition, need to be able to run without special
00032         // privileges in system (e.g., on hopper).
00033         // Notes:  For now, all cores connected to same NIC will get the
00034         // same coordinates; later, we could add extra coordinate dimensions
00035         // to represent nodes or dies (using hwloc info through RAAMP
00036         // data object).
00037 
00038         // (MD) will modify mapping test to use machine representation
00039         // #ifdef HAVE_ZOLTAN2_OVIS
00040 
00041         // Call initializer for RAAMP data object (AG)
00042 
00043         //get network dimension.
00044         //TODO change.
00045         // Call RAAMP Data Object to get the network dimension (AG)
00046         networkDim = 3;
00047 
00048         //allocate memory for processor coordinates.
00049         procCoords = new pcoord_t *[networkDim];
00050         for (int i = 0; i < networkDim; ++i){
00051             procCoords[i] = new pcoord_t [numProcs];
00052             memset (procCoords[i], 0, sizeof(pcoord_t) * numProcs);
00053         }
00054         //obtain the coordinate of the processor.
00055         this->getMyCoordinate(/*pcoord_t &xyz[networkDim]*/);
00056         // copy xyz into appropriate spot in procCoords. (MD)
00057 
00058         //reduceAll the coordinates of each processor.
00059         this->gatherMachineCoordinates();
00060     }
00061 
00062 
00066     MachineRepresentation(RCP<Comm<int> > comm_):
00067         networkDim(0), numProcs(comm_->getSize()), procCoords(0), comm(comm_){
00068         // WIll need this constructor to be specific to RAAMP (MD).
00069         // Will need a default constructor using, e.g., GeometricGenerator
00070         // or nothing at all, for when RAAMP is not available as TPL.
00071         //
00072         // (AG) In addition, need to be able to run without special
00073         // privileges in system (e.g., on hopper).  
00074         // Notes:  For now, all cores connected to same NIC will get the
00075         // same coordinates; later, we could add extra coordinate dimensions
00076         // to represent nodes or dies (using hwloc info through RAAMP
00077         // data object).
00078 
00079         // (MD) will modify mapping test to use machine representation
00080         // #ifdef HAVE_ZOLTAN2_OVIS
00081 
00082         // Call initializer for RAAMP data object (AG)
00083 
00084         //get network dimension.
00085         //TODO change.
00086         // Call RAAMP Data Object to get the network dimension (AG)
00087         networkDim = 3;
00088 
00089         //allocate memory for processor coordinates.
00090         procCoords = new pcoord_t *[networkDim];
00091         for (int i = 0; i < networkDim; ++i){
00092             procCoords[i] = new pcoord_t [numProcs];
00093             memset (procCoords[i], 0, sizeof(pcoord_t) * numProcs);
00094         }
00095         //obtain the coordinate of the processor.
00096         this->getMyCoordinate(/*pcoord_t &xyz[networkDim]*/);
00097         // copy xyz into appropriate spot in procCoords. (MD)
00098 
00099         //reduceAll the coordinates of each processor.
00100         this->gatherMachineCoordinates();
00101     }
00102 
00103 
00107     void getMyCoordinate(/* pcoord_t &xyz[networkDim]*/){
00108 
00109         // Call RAAMP system to get coordinates and store in xyz (MD)
00110         // What is the RAAMP call?  (AG)
00111         // AG will return a view (pointer) to RAAMP's data.
00112         // We will copy it into xyz.
00113 
00114         // The code below may be good for the default constructor, perhaps,
00115         // but it should copy the data into xyz instead of the procCoords.
00116         int myRank = comm->getRank();
00117 
00118         int slice = int (pow( double(numProcs), double(1.0 / networkDim)) + 0.5 );
00119 
00120         int m = myRank;
00121         for (int i = 0; i < networkDim; ++i){
00122             procCoords[i][myRank] = m / int(pow(slice, double(networkDim - i - 1)));
00123             m = m % int(pow(double(slice), double(networkDim - i - 1)));
00124         }
00125     }
00126 
00130     void gatherMachineCoordinates(){
00131         pcoord_t *tmpVect = new pcoord_t [numProcs];
00132 
00133         for (int i = 0; i < networkDim; ++i){
00134             reduceAll<int, pcoord_t>(
00135                     *comm,
00136                     Teuchos::REDUCE_SUM,
00137                     numProcs,
00138                     procCoords[i],
00139                     tmpVect);
00140             pcoord_t *tmp = tmpVect;
00141             tmpVect = procCoords[i];
00142             procCoords[i] = tmp;
00143         }
00144         delete [] tmpVect;
00145     }
00146 
00150     ~MachineRepresentation() {
00151         for (int i = 0; i < networkDim; ++i){
00152             delete [] procCoords[i];
00153         }
00154         delete []procCoords;
00155         // Free/release THE RAAMP Data Object.
00156         // Deinitialize/finalize/whatever (AG)
00157     }
00158 
00162     int getProcDim() const{
00163         return networkDim;
00164     }
00165 
00169     pcoord_t** getProcCoords() const{
00170         return procCoords;
00171     }
00172 
00176     int getNumProcs() const{
00177         return numProcs;
00178     }
00179 
00180 };
00181 }
00182 #endif