EpetraExt Development
EpetraExt_ZoltanMpiDistributor.cpp
Go to the documentation of this file.
00001 //@HEADER
00002 // ***********************************************************************
00003 //
00004 //     EpetraExt: Epetra Extended - Linear Algebra Services Package
00005 //                 Copyright (2011) Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 //@HEADER
00041 
00042 #include "EpetraExt_ZoltanMpiDistributor.h"
00043 #include "EpetraExt_ZoltanMpiComm.h"
00044 
00045 
00046 //==============================================================================
00047 // constructor
00048 EPETRAEXT_DEPRECATED
00049 EpetraExt::ZoltanMpiDistributor::ZoltanMpiDistributor(const EpetraExt::ZoltanMpiComm &Comm)
00050  : Epetra_Object("EpetraExt::ZoltanMpiDistributor"),
00051    plan_(0),
00052    comm_(Comm.GetMpiComm()),  
00053    tag_(Comm.GetMpiTag()),
00054    epComm_(&Comm)
00055 {
00056 }
00057 
00058 //==============================================================================
00059 // constructor
00060 EPETRAEXT_DEPRECATED
00061 EpetraExt::ZoltanMpiDistributor::ZoltanMpiDistributor( const EpetraExt::ZoltanMpiDistributor &Distributor)
00062  : Epetra_Object("EpetraExt::ZoltanMpiDistributor"),
00063    plan_(0),
00064    comm_(Distributor.comm_),  
00065    tag_(Distributor.tag_),
00066    epComm_(Distributor.epComm_)
00067 {
00068   EPETRA_CHK_ERR(Zoltan_Comm_Create_Copy (&plan_, Distributor.plan_));
00069 }
00070 
00071 //==============================================================================
00072 // destructor
00073 EPETRAEXT_DEPRECATED
00074 EpetraExt::ZoltanMpiDistributor::~ZoltanMpiDistributor()
00075 {
00076   Zoltan_Comm_Destroy(&plan_);
00077 }
00078 
00079 //==============================================================================
00080 // CreateFromSends Method
00081 // - create communication plan given a known list of procs to send to
00082 EPETRAEXT_DEPRECATED
00083 int EpetraExt::ZoltanMpiDistributor::CreateFromSends (
00084  const int  &NumExportIDs,
00085  const int  *ExportPIDs,
00086  const bool &Deterministic,
00087  int        &NumRemoteIDs)
00088 {
00089   EPETRA_CHK_ERR (Zoltan_Comm_Create (&plan_, (int) NumExportIDs,
00090      (int*) ExportPIDs, comm_, tag_, &NumRemoteIDs));            
00091   return 0;
00092 }
00093 
00094 //==============================================================================
00095 // CreateFromRecvs Method
00096 // - create communication plan given a known list of procs to recv from
00097 EPETRAEXT_DEPRECATED
00098 int EpetraExt::ZoltanMpiDistributor::CreateFromRecvs (
00099  const int  &NumRemoteIDs,
00100  const int  *RemoteGIDs,
00101  const int  *RemotePIDs,
00102  const bool &Deterministic,
00103  int  &NumExportIDs,
00104  int *&ExportGIDs,
00105  int *&ExportPIDs)
00106 {
00107     int i, myproc, nprocs;
00108     MPI_Comm_rank (comm_, &myproc);
00109     MPI_Comm_size (comm_, &nprocs);
00110     
00111     ZoltanMpiDistributor tmpdist (*epComm_);
00112     
00113     int *proclist = 0, *imports = 0;
00114     if (NumRemoteIDs > 0)  {
00115        proclist = new int [NumRemoteIDs];
00116        imports  = new int [2 * NumRemoteIDs];
00117        for (i = 0; i < NumRemoteIDs; i++)  {
00118           proclist[i]     = RemotePIDs[i];
00119           imports [2*i]   = RemoteGIDs[i];
00120           imports [2*i+1] = myproc;
00121           }
00122        }
00123        
00124     EPETRA_CHK_ERR (Zoltan_Comm_Create (&tmpdist.plan_, (int) NumRemoteIDs,
00125      proclist, tmpdist.comm_, tmpdist.tag_, &NumExportIDs));
00126      
00127     int *exports = 0; 
00128     if (NumExportIDs > 0)  {
00129        exports    = new int [2 * NumExportIDs];
00130        ExportGIDs = new int [NumExportIDs];
00131        ExportPIDs = new int [NumExportIDs];
00132        }
00133     else  {
00134        ExportGIDs = 0;
00135        ExportPIDs = 0;
00136        }       
00137     
00138     EPETRA_CHK_ERR (Zoltan_Comm_Do (tmpdist.plan_, tmpdist.tag_,
00139      (char*) imports, 2*sizeof(int), (char*) exports));
00140      
00141     for (i = 0; i < NumExportIDs; i++)  {
00142        ExportGIDs[i] = exports[2*i];
00143        ExportPIDs[i] = exports[2*i+1];
00144        }
00145 
00146     EPETRA_CHK_ERR (Zoltan_Comm_Create (&plan_, (int) NumExportIDs,
00147      (int*) ExportPIDs, comm_, tag_, (int*) NumRemoteIDs));
00148      
00149     if (proclist)  delete [] proclist;
00150     if (imports)   delete [] imports;
00151     if (exports)   delete [] exports;       
00152     return 0; 
00153 }
00154 
00155 //==============================================================================
00156 // Do method
00157 EPETRAEXT_DEPRECATED
00158 int EpetraExt::ZoltanMpiDistributor::Do (
00159  char      *exports,
00160  int       size,
00161  int       &len_imports,
00162  char      *imports)
00163 {
00164     EPETRA_CHK_ERR (Zoltan_Comm_Do_Post (plan_,tag_,exports,size,imports));
00165     EPETRA_CHK_ERR (Zoltan_Comm_Do_Wait (plan_,tag_,exports,size,imports));
00166     return 0;
00167 }
00168 
00169 //==============================================================================
00170 // DoReverse method
00171 EPETRAEXT_DEPRECATED
00172 int EpetraExt::ZoltanMpiDistributor::DoReverse (
00173  char      *exports,
00174  int       size,
00175  int       &len_imports,
00176  char      *imports)
00177 {
00178     EPETRA_CHK_ERR (Zoltan_Comm_Do_Reverse_Post (plan_, tag_, exports,
00179      size, 0, imports));
00180     EPETRA_CHK_ERR (Zoltan_Comm_Do_Reverse_Wait (plan_, tag_, exports,
00181      size, 0, imports));
00182     return 0;
00183 }
00184     
00185 //==============================================================================
00186 // Do_Posts Method
00187 EPETRAEXT_DEPRECATED
00188 int EpetraExt::ZoltanMpiDistributor::DoPosts (
00189  char      *exports,
00190  int       size,
00191  int       &len_imports,
00192  char      *imports)
00193 {
00194     EPETRA_CHK_ERR (Zoltan_Comm_Do_Post (plan_, tag_, exports, size, imports));   
00195     return 0;               
00196 }
00197     
00198 //==============================================================================
00199 // Do_Waits Method
00200 EPETRAEXT_DEPRECATED
00201 int EpetraExt::ZoltanMpiDistributor::DoWaits ()
00202 {
00203     EPETRA_CHK_ERR (Zoltan_Comm_Do_Wait (plan_, tag_, 0, 0, imports));
00204     return 0;
00205 }
00206 
00207 //==============================================================================--------------------------------------------
00208 // DoReverse_Posts Method
00209 EPETRAEXT_DEPRECATED
00210 int EpetraExt::ZoltanMpiDistributor::DoReversePosts (
00211  char      *exports,
00212  int       size,
00213  int       &len_imports,
00214  char      *imports)
00215 {
00216     EPETRA_CHK_ERR (Zoltan_Comm_Do_Reverse_Post (plan_, tag_, exports, size, 0, imports));
00217     return 0;
00218 }
00219 
00220 //==============================================================================
00221 // DoReverse_Waits Method
00222 EPETRAEXT_DEPRECATED
00223 int EpetraExt::ZoltanMpiDistributor::DoReverseWaits ()
00224 {
00225     EPETRA_CHK_ERR (Zoltan_Comm_Do_Reverse_Wait (plan_, tag_, 0, 0, 0, 0));
00226     return 0; 
00227 }
00228 
00229 //==============================================================================
00230 // Resize Method 
00231 EPETRAEXT_DEPRECATED
00232 int EpetraExt::ZoltanMpiDistributor::Resize (
00233  int *sizes)
00234 {
00235     int * sum_recv_sizes = 0;
00236     EPETRA_CHK_ERR (Zoltan_Comm_Resize (plan_, sizes, tag_, sum_recv_sizes));
00237     return 0;
00238 }
00239 
00240 //==============================================================================
00241 // Do method with variable size objects
00242 EPETRAEXT_DEPRECATED
00243 int EpetraExt::ZoltanMpiDistributor::Do (
00244  char       *exports,
00245  int        obj_size,
00246  int       *&sizes,
00247  int        &len_imports,
00248  char       *imports)
00249 {
00250     int junk;
00251     EPETRA_CHK_ERR (Zoltan_Comm_Resize  (plan_, (int*) sizes, tag_, &junk));
00252     EPETRA_CHK_ERR (Zoltan_Comm_Do_Post (plan_, tag_, exports, 1, imports));
00253     EPETRA_CHK_ERR (Zoltan_Comm_Do_Wait (plan_, tag_, exports, 1, imports));
00254     return 0;      
00255 }
00256 
00257 //==============================================================================
00258 // DoReverse method with variable size objects
00259 EPETRAEXT_DEPRECATED
00260 int EpetraExt::ZoltanMpiDistributor::DoReverse (
00261  char       *exports,
00262  int        obj_size,
00263  int       *&sizes,
00264  int        &len_imports,
00265  char       *imports)
00266 {
00267    EPETRA_CHK_ERR (Zoltan_Comm_Do_Reverse_Post (plan_, tag_, exports, 1, sizes, imports));
00268    EPETRA_CHK_ERR (Zoltan_Comm_Do_Reverse_Wait (plan_, tag_, exports, 1, sizes, imports));
00269    return 0;      
00270 }
00271    
00272 //==============================================================================
00273 // Do_Posts Method with variable size objects
00274 EPETRAEXT_DEPRECATED
00275 int EpetraExt::ZoltanMpiDistributor::DoPosts (
00276  char       *exports,
00277  int        obj_size,
00278  int       *&sizes,
00279  int        &len_imports,
00280  char       *imports)
00281 {
00282     int junk;
00283     EPETRA_CHK_ERR (Zoltan_Comm_Resize  (plan_, (int*) sizes, tag_, &junk));
00284     EPETRA_CHK_ERR (Zoltan_Comm_Do_Post (plan_, tag_, exports, 1, imports));
00285     return 0;      
00286 }
00287 
00288 //==============================================================================
00289 // DoReverse_Posts Method with variable size objects
00290 EPETRAEXT_DEPRECATED
00291 int EpetraExt::ZoltanMpiDistributor::DoReversePosts (
00292  char       *exports,
00293  int        obj_size,
00294  int       *&sizes,
00295  int        &len_imports,
00296  char       *imports)
00297 {
00298     EPETRA_CHK_ERR (Zoltan_Comm_Do_Reverse_Post (plan_, tag_, exports, 1, sizes, imports));
00299     return 0;      
00300 }
00301 
00302 //==============================================================================
00303 // Print method
00304 EPETRAEXT_DEPRECATED
00305 void EpetraExt::ZoltanMpiDistributor::Print( ostream & os) const
00306 {
00307   int i, j;
00308   int nsends, *send_procs, *send_lengths, send_nvals, send_max_size, *send_list;
00309   int nrecvs, *recv_procs, *recv_lengths, recv_nvals, recv_total_size;
00310   int *recv_list, self_msg;
00311   
00312   Zoltan_Comm_Info (plan_, &nsends, 0, 0, &send_nvals, &send_max_size, 0,
00313    &nrecvs, 0, 0, &recv_nvals, &recv_total_size, 0, &self_msg);
00314 
00315   send_procs   = new int [nsends];
00316   send_lengths = new int [nsends];
00317   send_list    = new int [send_nvals];
00318   recv_procs   = new int [nrecvs];
00319   recv_lengths = new int [nrecvs];
00320   recv_list    = new int [recv_nvals];
00321 
00322   Zoltan_Comm_Info (plan_, 0, send_procs, send_lengths, 0, 0, send_list, 0,
00323   recv_procs, recv_lengths, 0, 0, recv_list, 0);
00324 
00325   os << "nsends: " << nsends << endl;
00326   os << "procs_to: ";
00327   for( i = 0; i < nsends; i++ )
00328     os << " " << send_procs[i];
00329   os << endl;
00330   os<< "lengths_to: ";
00331   for( i = 0; i < nsends; i++ )
00332     os << " " << send_lengths[i];
00333   os << endl;
00334   os << "indices_to: ";
00335 //  int k = 0;
00336 //  for( i = 0; i < nsends; i++ )
00337 //  {
00338 //    for( j = 0; j < send_lengths[i]; j++ )
00339 //      os << " " << plan_->indices_to[j+k];
00340 //    k += send_lengths[i];
00341 //  }
00342   os << endl;
00343   os << "nrecvs: " << nrecvs << endl;
00344   os << "procs_from: ";
00345   for( i = 0; i < nrecvs; i++ )
00346     os << " " << recv_procs[i];
00347   os << endl;
00348   os << "lengths_from: ";
00349   for( i = 0; i < nrecvs; i++ )
00350     os << " " << recv_lengths[i];
00351   os << endl;
00352 /*
00353   os << "indices_from: ";
00354   k = 0;
00355   for( i = 0; i < nrecvs; i++ )
00356   {
00357     for( j = 0; j < recv_lengths[i]; j++ )
00358       os << " " << plan_->indices_from[j+k];
00359     k += recv_lengths[i];
00360   }
00361 */
00362   os << "self_msg: " << self_msg << endl;
00363   os << "max_send_length: " << send_max_size << endl;
00364   os << "total_recv_length: " << recv_total_size << endl;
00365   os << endl;
00366 
00367   delete [] send_procs;
00368   delete [] send_lengths;
00369   delete [] send_list;
00370   delete [] recv_procs;
00371   delete [] recv_lengths;
00372   delete [] recv_list;
00373   
00374   return;
00375 }
00376 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines