vtf-logo

shells/parallel/ZoltanIface.h

Go to the documentation of this file.
00001 // -*- C++ -*- 
00002 //
00003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00004 //
00005 //                                   Fehmi Cirak
00006 //                        California Institute of Technology
00007 //                           (C) 2004 All Rights Reserved
00008 //
00009 // <LicenseText>
00010 //
00011 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00012 //
00013 #ifndef ZOLTANIFACE_H
00014 #define ZOLTANIFACE_H
00015 #include <set>
00016 #include <vector>
00017 #include <algorithm>
00018 
00019 #include "zoltan.h"
00020 #include "mpi.h"
00021 
00022 
00023 
00024 // wrapper struct for passing the stl vectors through zoltan
00025 namespace parallel {
00026     typedef struct PointSetContS {
00027         std::vector<double>   _coordinate;
00028         std::vector<int>      _globalID;
00029     } PointSetCont;
00030 }    
00031 
00032 
00033 // function declarations
00034 namespace parallel{
00035     // initialize zoltan
00036     struct Zoltan_Struct* zoltanIfaceInit(const MPI_Comm& comm);
00037 
00038     // main interface for zoltan
00039     template<typename Iterator>
00040     bool zoltanIfacePartition (struct Zoltan_Struct *lb, 
00041                                PointSetCont *pointSet, 
00042                                Iterator insElements);
00043 
00044     // delete zoltan
00045     void zoltanFinalize(struct Zoltan_Struct *lb);
00046 
00047   extern "C" {    
00048     // call back functions
00049     int callBackNumElems(void *data, int *ierr);    
00050     void callBackElemList(void *data, int num_gid_entries, int num_lid_entries,
00051                           ZOLTAN_ID_PTR global_id, ZOLTAN_ID_PTR local_id, 
00052                           int wgt_dim, float *obj_wgts, int *ierr);
00053     void callBackCoords(void *data, int num_gid_entries, int num_lid_entries, 
00054                         ZOLTAN_ID_PTR global_id,ZOLTAN_ID_PTR local_id,
00055                         double *geom_vec, int *ierr);
00056     int callBackDimension(void *data, int *ierr);
00057     // callback function pointer type
00058     typedef void (*zoltan_cft)();
00059   }
00060 }
00061 
00062 
00063 
00064 // templated functions
00065 template<typename Iterator>
00066 bool parallel::zoltanIfacePartition (struct Zoltan_Struct *lb, 
00067                                      PointSetCont *pointSet, 
00068                                      Iterator insElements) 
00069 {       
00070     // register call back functions
00071     Zoltan_Set_Fn(lb, ZOLTAN_NUM_GEOM_FN_TYPE, 
00072                   reinterpret_cast<zoltan_cft>(callBackDimension), pointSet);
00073     Zoltan_Set_Fn(lb, ZOLTAN_GEOM_FN_TYPE, 
00074                   reinterpret_cast<zoltan_cft>(callBackCoords), pointSet);  
00075     Zoltan_Set_Fn(lb, ZOLTAN_NUM_OBJ_FN_TYPE,  
00076                   reinterpret_cast<zoltan_cft>(callBackNumElems), pointSet); 
00077     Zoltan_Set_Fn(lb, ZOLTAN_OBJ_LIST_FN_TYPE,                     
00078                   reinterpret_cast<zoltan_cft>(callBackElemList), pointSet); 
00079     
00080     // call the load balancer 
00081     ZOLTAN_ID_PTR imp_global_ids, exp_global_ids, imp_local_ids, exp_local_ids;
00082     int changes, num_imp, num_exp, *imp_procs, *exp_procs;
00083     int num_gid_entries, num_lid_entries;
00084     Zoltan_LB_Balance(lb, &changes, &num_gid_entries, &num_lid_entries, 
00085                       &num_imp, &imp_global_ids, &imp_local_ids, &imp_procs,  
00086                       &num_exp, &exp_global_ids, &exp_local_ids, &exp_procs); 
00087     
00088     // check if zoltan has changed something
00089     if (!changes) {
00090         std::copy(pointSet->_globalID.begin(), pointSet->_globalID.end(), 
00091                   insElements);    
00092         return false;
00093     }    
00094 
00095     // do some set operations to find the elements on this processor
00096     // note that set operations work only on sorted ranges
00097     std::vector<int> current(pointSet->_globalID);
00098     std::sort(current.begin(), current.end());
00099     
00100     // add the global element ID's on this processor to insElements iterator
00101     std::vector<int> exported(exp_global_ids, exp_global_ids+num_exp);
00102     std::sort(exported.begin(), exported.end());
00103     
00104     std::set<int> tmpSet;
00105     std::insert_iterator<std::set<int> > insTmpSet(tmpSet, tmpSet.begin());
00106     std::set_difference(current.begin(), current.end(), 
00107                         exported.begin(), exported.end(), insTmpSet );
00108     
00109     std::vector<int> imported(imp_global_ids, imp_global_ids+num_imp);
00110     std::sort(imported.begin(), imported.end());
00111     
00112     std::set_union(tmpSet.begin(), tmpSet.end(), 
00113                    imported.begin(), imported.end(), insElements);
00114     
00115     return true;
00116 }
00117 
00118 #endif

Generated on Fri Aug 24 13:00:24 2007 for SFC Thin-Shell Finite Element Solver by  doxygen 1.4.7