void FeatureFloodCount::initialize() { // Clear the feature marking maps and region counters and other data structures for (auto map_num = decltype(_maps_size)(0); map_num < _maps_size; ++map_num) { _feature_maps[map_num].clear(); _partial_feature_sets[map_num].clear(); if (_var_index_mode) _var_index_maps[map_num].clear(); _halo_ids[map_num].clear(); } _feature_sets.clear(); // Calculate the thresholds for this iteration _step_threshold = _element_average_value + _threshold; _step_connecting_threshold = _element_average_value + _connecting_threshold; _ghosted_entity_ids.clear(); // Reset the feature count and max local size _feature_count = 0; _entity_var_to_features.clear(); clearDataStructures(); }
void FeatureFloodCount::communicateAndMerge() { // First we need to transform the raw data into a usable data structure prepareDataForTransfer(); /** * The libMesh packed range routines handle the communication of the individual * string buffers. Here we need to create a container to hold our type * to serialize. It'll always be size one because we are sending a single * byte stream of all the data to other processors. The stream need not be * the same size on all processors. */ std::vector<std::string> send_buffers(1); /** * Additionally we need to create a different container to hold the received * byte buffers. The container type need not match the send container type. * However, We do know the number of incoming buffers (num processors) so we'll * go ahead and use a vector. */ std::vector<std::string> recv_buffers; if (_is_master) recv_buffers.reserve(_app.n_processors()); serialize(send_buffers[0]); // Free up as much memory as possible here before we do global communication clearDataStructures(); /** * Send the data from all processors to the root to create a complete * global feature map. */ _communicator.gather_packed_range(0, (void *)(nullptr), send_buffers.begin(), send_buffers.end(), std::back_inserter(recv_buffers)); if (_is_master) { // The root process now needs to deserialize and merge all of the data deserialize(recv_buffers); recv_buffers.clear(); mergeSets(true); } // Make sure that feature count is communicated to all ranks _communicator.broadcast(_feature_count); }
/*! \internal */ QSimplex::~QSimplex() { clearDataStructures(); }
/*! \internal Sets the new constraints in the simplex solver and returns whether the problem is feasible. This method sets the new constraints, normalizes them, creates the simplex matrix and runs the first simplex phase. */ bool QSimplex::setConstraints(const QList<QSimplexConstraint *> newConstraints) { //////////////////////////// // Reset to initial state // //////////////////////////// clearDataStructures(); if (newConstraints.isEmpty()) return true; // we are ok with no constraints // Make deep copy of constraints. We need this copy because we may change // them in the simplification method. for (int i = 0; i < newConstraints.size(); ++i) { QSimplexConstraint *c = new QSimplexConstraint; c->constant = newConstraints[i]->constant; c->ratio = newConstraints[i]->ratio; c->variables = newConstraints[i]->variables; constraints << c; } // Remove constraints of type Var == K and replace them for their value. if (!simplifyConstraints(&constraints)) { qWarning() << "QSimplex: No feasible solution!"; clearDataStructures(); return false; } /////////////////////////////////////// // Prepare variables and constraints // /////////////////////////////////////// // Set Variables direct mapping. // "variables" is a list that provides a stable, indexed list of all variables // used in this problem. QSet<QSimplexVariable *> variablesSet; for (int i = 0; i < constraints.size(); ++i) variablesSet += \ QSet<QSimplexVariable *>::fromList(constraints[i]->variables.keys()); variables = variablesSet.toList(); // Set Variables reverse mapping // We also need to be able to find the index for a given variable, to do that // we store in each variable its index. for (int i = 0; i < variables.size(); ++i) { // The variable "0" goes at the column "1", etc... variables[i]->index = i + 1; } // Normalize Constraints // In this step, we prepare the constraints in two ways: // Firstly, we modify all constraints of type "LessOrEqual" or "MoreOrEqual" // by the adding slack or surplus variables and making them "Equal" constraints. // Secondly, we need every single constraint to have a direct, easy feasible // solution. Constraints that have slack variables are already easy to solve, // to all the others we add artificial variables. // // At the end we modify the constraints as follows: // - LessOrEqual: SLACK variable is added. // - Equal: ARTIFICIAL variable is added. // - More or Equal: ARTIFICIAL and SURPLUS variables are added. int variableIndex = variables.size(); QList <QSimplexVariable *> artificialList; for (int i = 0; i < constraints.size(); ++i) { QSimplexVariable *slack; QSimplexVariable *surplus; QSimplexVariable *artificial; Q_ASSERT(constraints[i]->helper.first == 0); Q_ASSERT(constraints[i]->artificial == 0); switch(constraints[i]->ratio) { case QSimplexConstraint::LessOrEqual: slack = new QSimplexVariable; slack->index = ++variableIndex; constraints[i]->helper.first = slack; constraints[i]->helper.second = 1.0; break; case QSimplexConstraint::MoreOrEqual: surplus = new QSimplexVariable; surplus->index = ++variableIndex; constraints[i]->helper.first = surplus; constraints[i]->helper.second = -1.0; // fall through case QSimplexConstraint::Equal: artificial = new QSimplexVariable; constraints[i]->artificial = artificial; artificialList += constraints[i]->artificial; break; } } // All original, slack and surplus have already had its index set // at this point. We now set the index of the artificial variables // as to ensure they are at the end of the variable list and therefore // can be easily removed at the end of this method. firstArtificial = variableIndex + 1; for (int i = 0; i < artificialList.size(); ++i) artificialList[i]->index = ++variableIndex; artificialList.clear(); ///////////////////////////// // Fill the Simplex matrix // ///////////////////////////// // One for each variable plus the Basic and BFS columns (first and last) columns = variableIndex + 2; // One for each constraint plus the objective function rows = constraints.size() + 1; matrix = (qreal *)malloc(sizeof(qreal) * columns * rows); if (!matrix) { qWarning() << "QSimplex: Unable to allocate memory!"; return false; } for (int i = columns * rows - 1; i >= 0; --i) matrix[i] = 0.0; // Fill Matrix for (int i = 1; i <= constraints.size(); ++i) { QSimplexConstraint *c = constraints[i - 1]; if (c->artificial) { // Will use artificial basic variable setValueAt(i, 0, c->artificial->index); setValueAt(i, c->artificial->index, 1.0); if (c->helper.second != 0.0) { // Surplus variable setValueAt(i, c->helper.first->index, c->helper.second); } } else { // Slack is used as the basic variable Q_ASSERT(c->helper.second == 1.0); setValueAt(i, 0, c->helper.first->index); setValueAt(i, c->helper.first->index, 1.0); } QHash<QSimplexVariable *, qreal>::const_iterator iter; for (iter = c->variables.constBegin(); iter != c->variables.constEnd(); ++iter) { setValueAt(i, iter.key()->index, iter.value()); } setValueAt(i, columns - 1, c->constant); } // Set objective for the first-phase Simplex. // Z = -1 * sum_of_artificial_vars for (int j = firstArtificial; j < columns - 1; ++j) setValueAt(0, j, 1.0); // Maximize our objective (artificial vars go to zero) solveMaxHelper(); // If there is a solution where the sum of all artificial // variables is zero, then all of them can be removed and yet // we will have a feasible (but not optimal) solution for the // original problem. // Otherwise, we clean up our structures and report there is // no feasible solution. if ((valueAt(0, columns - 1) != 0.0) && (qAbs(valueAt(0, columns - 1)) > 0.00001)) { qWarning() << "QSimplex: No feasible solution!"; clearDataStructures(); return false; } // Remove artificial variables. We already have a feasible // solution for the first problem, thus we don't need them // anymore. clearColumns(firstArtificial, columns - 2); return true; }
bool SynCELocalKonnector::writeSyncees() { bool ret = false; kdDebug( 2120 ) << "SynCELocalKonnector::writeSyncees()..." << endl; if ( _actualSyncType & TODOS ) { if ( !mCalendarFile.isEmpty() ) { purgeRemovedEntries( mTodoSyncee ); TodoSyncHistory c1Helper( mTodoSyncee, storagePath() + mMd5sumTodo ); c1Helper.save(); if ( !mCalendar.save( mCalendarFile ) ) { KMessageBox::error(0, "Error writing to calendar " + mCalendarFile + ". Please check permissions.", QString("Error writing to local calendar")); emit synceeWriteError( this ); goto error; } } else { KMessageBox::error(0, "You didn't configure the Task-Synchronizer. Please do so and synchronize again.", QString("Error writing to local calendar")); emit synceeWriteError( this ); goto error; } } if ( _actualSyncType & EVENTS ) { if ( !mCalendarFile.isEmpty() ) { purgeRemovedEntries( mEventSyncee ); EventSyncHistory c2Helper( mEventSyncee, storagePath() + mMd5sumEvent ); c2Helper.save(); if ( !mCalendar.save( mCalendarFile ) ) { KMessageBox::error(0, "Error writing to calendar " + mCalendarFile + ". Please check permissions.", QString("Error writing to local calendar")); emit synceeWriteError( this ); goto error; } } else { KMessageBox::error(0, "You didn't configure the Appointment-Synchronizer. Please do so and synchronize again.", QString("Error writing to local calendar")); emit synceeWriteError( this ); goto error; } } if ( _actualSyncType & CONTACTS ) { if ( !mAddressBookFile.isEmpty() ) { purgeRemovedEntries( mAddressBookSyncee ); KABC::Ticket *ticket; ticket = mAddressBook.requestSaveTicket(); if ( !ticket ) { KMessageBox::error(0, "Error during ticket-request to save " + mAddressBookFile + ".", QString("Error writing to local addressbook")); emit synceeWriteError( this ); goto error; } if ( !mAddressBook.save( ticket ) ) { KMessageBox::error(0, "Error writing to addressbook " + mAddressBookFile + ". Please check permissions.", QString("Error writing to local addressbook")); emit synceeWriteError( this ); goto error; } AddressBookSyncHistory aHelper( mAddressBookSyncee, storagePath() + "/" + mMd5sumAbk ); aHelper.save(); } else { KMessageBox::error(0, "You didn't configure the Contact-Synchronizer. Please do so and synchronize again.", QString("Error writing to local addressbook")); emit synceeWriteError( this ); goto error; } } emit synceesWritten( this ); ret = true; error: clearDataStructures(); return ret; }