Actual source code: eindexspace.c


  3: /*
  4:       Makes a PETSc Map look like an esi::IndexSpace
  5: */

 7:  #include esi/petsc/indexspace.h

  9: esi::petsc::IndexSpace<int>::IndexSpace(MPI_Comm icomm, int n, int N)
 10: {
 12:   PetscMapCreateMPI(icomm,n,N,&this->map);if (ierr) return;
 13:   this->pobject = (PetscObject)this->map;
 14:   PetscObjectGetComm((PetscObject)this->map,&this->comm);if (ierr) return;
 15: }

 17: esi::petsc::IndexSpace<int>::IndexSpace(::esi::IndexSpace<int> &sourceIndexSpace)
 18: {
 19:   int      ierr,n,N;
 20:   MPI_Comm *icomm;

 22:   sourceIndexSpace.getRunTimeModel("MPI",reinterpret_cast<void *&>(icomm));if (ierr) return;
 23:   sourceIndexSpace.getGlobalSize(N);if (ierr) return;
 24:   {
 25:     ::esi::IndexSpace<int> *amap;

 27:     sourceIndexSpace.getInterface("esi::IndexSpace",reinterpret_cast<void *&>(amap));if (ierr) return;
 28:     amap->getLocalSize(n);if (ierr) return;
 29:   }
 30:   PetscMapCreateMPI(*icomm,n,N,&this->map);if (ierr) return;
 31:   this->pobject = (PetscObject)this->map;
 32:   PetscObjectGetComm((PetscObject)this->map,&this->comm);if (ierr) return;
 33: }

 35: esi::petsc::IndexSpace<int>::IndexSpace(PetscMap sourceIndexSpace)
 36: {
 37:   PetscObjectReference((PetscObject) sourceIndexSpace);
 38:   this->map = sourceIndexSpace;
 39:   this->pobject = (PetscObject)this->map;
 40:   PetscObjectGetComm((PetscObject)sourceIndexSpace,&this->comm);
 41: }

 43: esi::petsc::IndexSpace<int>::~IndexSpace()
 44: {
 46:   PetscMapDestroy(this->map); if (ierr) return;
 47: }

 49: /* ---------------esi::Object methods ------------------------------------------------------------ */
 50: ::esi::ErrorCode esi::petsc::IndexSpace<int>::getInterface(const char* name, void *& iface)
 51: {
 52:   PetscTruth flg;
 53:   if (PetscStrcmp(name,"esi::Object",&flg),flg){
 54:     iface = (void *) (::esi::Object *) this;
 55:   } else if (PetscStrcmp(name,"esi::IndexSpace",&flg),flg){
 56:     iface = (void *) (::esi::IndexSpace<int> *) this;
 57:   } else if (PetscStrcmp(name,"esi::petsc::IndexSpace",&flg),flg){
 58:     iface = (void *) (::esi::petsc::IndexSpace<int> *) this;
 59:   } else if (PetscStrcmp(name,"PetscMap",&flg),flg){
 60:     iface = (void *) this->map;
 61:   } else {
 62:     iface = 0;
 63:   }
 64:   return 0;
 65: }

 67: ::esi::ErrorCode esi::petsc::IndexSpace<int>::getInterfacesSupported(::esi::Argv * list)
 68: {
 69:   list->appendArg("esi::Object");
 70:   list->appendArg("esi::IndexSpace");
 71:   list->appendArg("esi::petsc::IndexSpace");
 72:   list->appendArg("PetscMap");
 73:   return 0;
 74: }


 77: /* -------------- esi::IndexSpace methods --------------------------------------------*/
 78: ::esi::ErrorCode esi::petsc::IndexSpace<int>::getGlobalSize(int &globalSize)
 79: {
 80:   return PetscMapGetSize(this->map,&globalSize);
 81: }

 83: ::esi::ErrorCode esi::petsc::IndexSpace<int>::getLocalSize(int &localSize)
 84: {
 85:   return PetscMapGetLocalSize(this->map,&localSize);
 86: }

 88: ::esi::ErrorCode esi::petsc::IndexSpace<int>::getLocalPartitionOffset(int &localoffset)
 89: {
 90:   return PetscMapGetLocalRange(this->map,&localoffset,PETSC_IGNORE);
 91: }

 93: ::esi::ErrorCode esi::petsc::IndexSpace<int>::getGlobalPartitionOffsets(int *globaloffsets)
 94: {
 95:   int ierr,*iglobaloffsets;
 96:   int size;

 98:   PetscMapGetGlobalRange(this->map,&iglobaloffsets);
 99:   MPI_Comm_size(this->comm,&size);
100:   PetscMemcpy(globaloffsets,iglobaloffsets,(size+1)*sizeof(int));
101:   return 0;
102: }

104: ::esi::ErrorCode esi::petsc::IndexSpace<int>::getGlobalPartitionSizes(int *globalsizes)
105: {
106:   int ierr,i,n,*globalranges;


109:   MPI_Comm_size(this->comm,&n);
110:   PetscMapGetGlobalRange(this->map,&globalranges);
111:   for (i=0; i<n; i++) {
112:     globalsizes[i] = globalranges[i+1] - globalranges[i];
113:   }
114:   return 0;
115: }

117:   /* -------------------------------------------------------------------------*/
118: namespace esi{namespace petsc{

120: template<class Ordinal> class IndexSpaceFactory : public virtual ::esi::IndexSpaceFactory<Ordinal>
121: {
122:   public:

124:     // Destructor.
125:   virtual ~IndexSpaceFactory(void){};

127:     // Interface for gov::cca::Component
128: #if defined(PETSC_HAVE_CCA)
129:     virtual void setServices(gov::cca::Services *svc)
130:     {
131:       svc->addProvidesPort(this,svc->createPortInfo("getIndexSpace", "esi::IndexSpaceFactory", 0));
132:     };
133: #endif

135:     // Construct a IndexSpace
136:     virtual ::esi::ErrorCode getIndexSpace(const char * name,void *comm,int m,::esi::IndexSpace<Ordinal>*&v)
137:     {
138:       PetscTruth ismpi;
139:       int PetscStrcmp(name,"MPI",&ismpi);
140:       if (!ismpi) SETERRQ1(1,"%s not supported, only MPI supported as RunTimeModel",name);
141:       v = new esi::petsc::IndexSpace<Ordinal>(*(MPI_Comm*)comm,m,PETSC_DETERMINE);
142:       return 0;
143:     };

145: };
146: }}
147: EXTERN_C_BEGIN
148: #if defined(PETSC_HAVE_CCA)
149: gov::cca::Component *create_esi_petsc_indexspacefactory(void)
150: {
151:   return dynamic_cast<gov::cca::Component *>(new esi::petsc::IndexSpaceFactory<int>);
152: }
153: #else
154: ::esi::IndexSpaceFactory<int> *create_esi_petsc_indexspacefactory(void)
155: {
156:   return dynamic_cast< ::esi::IndexSpaceFactory<int>*>(new esi::petsc::IndexSpaceFactory<int>);
157: }
158: #endif
159: EXTERN_C_END

161: ::esi::petsc::IndexSpaceFactory<int> ISFInstForIntel64CompilerBug;

163: #if defined(PETSC_HAVE_TRILINOS)
164: #define PETRA_MPI /* used by Ptera to indicate MPI code */
165: #include "Petra_ESI_IndexSpace.h"

167: template<class Ordinal> class Petra_ESI_IndexSpaceFactory : public virtual ::esi::IndexSpaceFactory<Ordinal>
168: {
169:   public:

171:     // Destructor.
172:   virtual ~Petra_ESI_IndexSpaceFactory(void){};

174:     // Interface for gov::cca::Component
175: #if defined(PETSC_HAVE_CCA)
176:     virtual void setServices(gov::cca::Services *svc)
177:     {
178:       svc->addProvidesPort(this,svc->createPortInfo("getIndexSpace", "esi::IndexSpaceFactory", 0));
179:     };
180: #endif

182:     // Construct a IndexSpace
183:     virtual ::esi::ErrorCode getIndexSpace(const char * name,void *comm,int m,::esi::IndexSpace<Ordinal>*&v)
184:     {
185:       PetscTruth ismpi;
186:       int PetscStrcmp(name,"MPI",&ismpi);
187:       if (!ismpi) SETERRQ1(1,"%s not supported, only MPI supported as RunTimeModel",name);
188:       Petra_Comm *pcomm = new Petra_Comm(*(MPI_Comm*)comm);
189:       v = new Petra_ESI_IndexSpace<Ordinal>(-1,m,0,*pcomm);
190:       if (!v) SETERRQ(1,"Unable to create Petra_ESI_IndexSpace");
191:       return 0;
192:     };

194: };
195: EXTERN_C_BEGIN
196: #if defined(PETSC_HAVE_CCA)
197: gov::cca::Component *create_petra_esi_indexspacefactory(void)
198: {
199:   return dynamic_cast<gov::cca::Component *>(new Petra_ESI_IndexSpaceFactory<int>);
200: }
201: #else
202: ::esi::IndexSpaceFactory<int> *create_petra_esi_indexspacefactory(void)
203: {
204:   return dynamic_cast< ::esi::IndexSpaceFactory<int> *>(new Petra_ESI_IndexSpaceFactory<int>);
205: }
206: #endif
207: EXTERN_C_END
208: #endif