00001 #ifndef FILTER_CHAIN_CPP
00002 #define FILTER_CHAIN_CPP
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "FilterChain.h"
00038 #include "FilterFactory.h"
00039 #include "Utilities.h"
00040 #include "MPIHelper.h"
00041 #include "MPIStats.h"
00042 #include "EST.h"
00043
00044
00045 #define NO_ERROR 0
00046
00047
00048 FilterChain* FilterChain::ptrInstance = NULL;
00049
00050 void
00051 FilterChain::showArguments(std::ostream& os) {
00052 for (size_t i = 0; (i < chain.size()); i++) {
00053 chain[i]->showArguments(os);
00054 }
00055 }
00056
00057 bool
00058 FilterChain::parseArguments(int& argc, char **argv) {
00059 bool flag = true;
00060 for (size_t i = 0; (i < chain.size()); i++) {
00061 if (!chain[i]->parseArguments(argc, argv)) {
00062 flag = false;
00063 }
00064 }
00065 return flag;
00066 }
00067
00068 bool
00069 FilterChain::addFilter(Filter* h) {
00070 ASSERT( h != NULL );
00071 chain.push_back(h);
00072
00073 return true;
00074 }
00075
00076 int
00077 FilterChain::initialize() {
00078 int result;
00079 for (size_t i = 0; (i < chain.size()); i++) {
00080 if ((result = chain[i]->initialize()) != NO_ERROR) {
00081
00082 std::cerr << "Error initializing filter "
00083 << chain[i]->getName() << std::endl;
00084 return result;
00085 }
00086 }
00087 return 0;
00088 }
00089
00090 void
00091 FilterChain::finalize() {
00092 for (size_t i = 0; (i < chain.size()); i++) {
00093 chain[i]->finalize();
00094 }
00095 }
00096
00097 Filter*
00098 FilterChain::getFilter(const std::string& name) const {
00099 for(size_t i = 0; (i < chain.size()); i++) {
00100 if (chain[i]->getName() == name) {
00101
00102 return chain[i];
00103 }
00104 }
00105
00106 return NULL;
00107 }
00108
00109 FilterChain::~FilterChain() {
00110 for(size_t i = 0; (i < chain.size()); i++) {
00111 delete chain[i];
00112 }
00113 chain.clear();
00114 }
00115
00116 FilterChain::FilterChain() {
00117
00118 }
00119
00120 void
00121 FilterChain::printStats(std::ostream& os, const int rank) const {
00122 os << "Filters on process with Rank " << rank << "\n"
00123 << "-------------------------------------------\n";
00124 for (size_t i = 0; (i < chain.size()); i++) {
00125 chain[i]->printStats(os);
00126 }
00127 }
00128
00129
00130 FilterChain*
00131 FilterChain::setupChain(const char* filterStr, ClusterMaker *clusterMaker) {
00132
00133 ASSERT ( ptrInstance == NULL );
00134 ptrInstance = new FilterChain();
00135
00136 if (filterStr == NULL) {
00137 return NULL;
00138 }
00139
00140 std::string fStr(filterStr);
00141 FilterChain* filterChain = ptrInstance;
00142 ASSERT ( filterChain != NULL );
00143
00144
00145 while (!fStr.empty()) {
00146
00147 const std::string::size_type hyphenLoc = fStr.find('-');
00148 const std::string name = fStr.substr(0, hyphenLoc);
00149
00150 Filter *filter =
00151 FilterFactory::create(name.c_str(), clusterMaker);
00152 if (filter == NULL) {
00153
00154 return NULL;
00155 }
00156
00157
00158 filterChain->addFilter(filter);
00159
00160 if (hyphenLoc == std::string::npos) {
00161
00162 fStr.clear();
00163 } else {
00164
00165 fStr = fStr.substr(hyphenLoc + 1);
00166 }
00167 }
00168
00169 return filterChain;
00170 }
00171
00172 void
00173 FilterChain::getOwnedESTidx(int& startIndex, int& endIndex) {
00174
00175
00176
00177
00178
00179 const int ESTsPerProcess = EST::getESTList().size() / MPI_GET_SIZE();
00180 const int ExtraESTs = EST::getESTList().size() % MPI_GET_SIZE();
00181 const int MyRank = MPI_GET_RANK();
00182
00183
00184
00185 startIndex = MyRank * ESTsPerProcess;
00186
00187 if (MyRank <= ExtraESTs) {
00188
00189
00190
00191 startIndex = ((ESTsPerProcess + 1) * MyRank);
00192 } else {
00193 startIndex += ExtraESTs;
00194 }
00195
00196
00197 endIndex = startIndex + ESTsPerProcess;
00198 if (MyRank < ExtraESTs) {
00199
00200
00201 endIndex++;
00202 }
00203 }
00204
00205 int
00206 FilterChain::applyFilters(ClusterMaker *clusterMaker) {
00207 ASSERT ( ptrInstance != NULL );
00208
00209 const int prevEstCount = EST::getESTCount();
00210
00211 int resultCode;
00212 if ((resultCode = ptrInstance->initialize()) != 0) {
00213
00214 return resultCode;
00215 }
00216
00217
00218 int startIndex, endIndex;
00219 getOwnedESTidx(startIndex, endIndex);
00220 for(int estIdx = startIndex; (estIdx < endIndex); estIdx++) {
00221
00222 if (!EST::getEST(estIdx)->hasBeenProcessed()) {
00223 ptrInstance->applyFilters(estIdx);
00224 }
00225 }
00226
00227
00228
00229 if (MPI_GET_SIZE() > 1) {
00230 allToAllBroadcast(clusterMaker);
00231 }
00232
00233 ptrInstance->finalize();
00234
00235 if (prevEstCount != EST::getESTCount()) {
00236 std::cerr << "The filters did not remove all the dummy ESTs they "
00237 << "added. This is a programming error. Aborting!\n";
00238 resultCode = 2;
00239 }
00240
00241
00242 return resultCode;
00243 }
00244
00245 void
00246 FilterChain::allToAllBroadcast(ClusterMaker *clusterMaker) {
00247 const int ProcCount = MPI_GET_SIZE();
00248 ASSERT (ProcCount > 1);
00249
00250
00251 std::vector<int> localSuperList;
00252 for(size_t i = 0; (i < ptrInstance->chain.size()); i++) {
00253 ptrInstance->chain[i]->addFilterData(localSuperList);
00254 }
00255
00256 localSuperList.push_back(-1);
00257 localSuperList.push_back(0);
00258
00259
00260
00261 for(int rank = 0; (rank < ProcCount); rank++) {
00262 if (rank == MPI_GET_RANK()) {
00263
00264 int size = localSuperList.size();
00265 MPI_BCAST(&size, 1, MPI_INT, rank);
00266 MPI_BCAST(&localSuperList[0], size, MPI_INT, rank);
00267 } else {
00268
00269 int size = 0;
00270 MPI_BCAST(&size, 1, MPI_INT, rank);
00271 ASSERT ( size > 0 );
00272 std::vector<int> remoteSuperList;
00273 remoteSuperList.reserve(size);
00274 MPI_BCAST(&remoteSuperList[0], size, MPI_INT, rank);
00275
00276 Filter::processFilterData(remoteSuperList, clusterMaker);
00277 }
00278 }
00279 }
00280
00281 #endif