void UniversalUpdates::PerformUniversalUpdates( bool resources_already_loaded, const QList< Resource* > &resources, const QHash< QString, QString > &updates ) { QHash< QString, QString > html_updates; QHash< QString, QString > css_updates; QHash< QString, QString > xml_updates; tie( html_updates, css_updates, xml_updates ) = SeparateHtmlCssXmlUpdates( updates ); QList< HTMLResource* > html_resources; QList< CSSResource* > css_resources; OPFResource *opf_resource = NULL; NCXResource *ncx_resource = NULL; int num_files = resources.count(); for ( int i = 0; i < num_files; ++i ) { Resource *resource = resources.at( i ); if ( resource->Type() == Resource::HTMLResourceType ) html_resources.append( qobject_cast< HTMLResource* >( resource ) ); else if ( resource->Type() == Resource::CSSResourceType ) css_resources.append( qobject_cast< CSSResource* >( resource ) ); else if ( resource->Type() == Resource::OPFResourceType ) opf_resource = qobject_cast< OPFResource* >( resource ); else if ( resource->Type() == Resource::NCXResourceType ) ncx_resource = qobject_cast< NCXResource* >( resource ); } QFutureSynchronizer<void> sync; if ( resources_already_loaded ) { sync.addFuture( QtConcurrent::map( html_resources, boost::bind( UpdateOneHTMLFile, _1, html_updates, css_updates ) ) ); sync.addFuture( QtConcurrent::map( css_resources, boost::bind( UpdateOneCSSFile, _1, css_updates ) ) ); } else { sync.addFuture( QtConcurrent::map( html_resources, boost::bind( LoadAndUpdateOneHTMLFile, _1, html_updates, css_updates ) ) ); sync.addFuture( QtConcurrent::map( css_resources, boost::bind( LoadAndUpdateOneCSSFile, _1, css_updates ) ) ); } // We can't schedule these with QtConcurrent because they // will (indirectly) call QTextDocument::setPlainText, and if // a tab is open for the ncx/opf, then an event needs to be sent // to the tab widget. Events can't cross threads, and we crash. UpdateNCXFile( ncx_resource, xml_updates ); UpdateOPFFile( opf_resource, xml_updates ); sync.waitForFinished(); }
void ImportHTML::UpdateFiles(HTMLResource &html_resource, QString & source, const QHash<QString, QString> &updates) { Q_ASSERT(&html_resource != NULL); QHash<QString, QString> html_updates; QHash<QString, QString> css_updates; QString newsource = source; QString currentpath = html_resource.GetCurrentBookRelPath(); std::tie(html_updates, css_updates, std::ignore) = UniversalUpdates::SeparateHtmlCssXmlUpdates(updates); QList<Resource *> all_files = m_Book->GetFolderKeeper().GetResourceList(); int num_files = all_files.count(); QList<CSSResource *> css_resources; for (int i = 0; i < num_files; ++i) { Resource *resource = all_files.at(i); if (resource->Type() == Resource::CSSResourceType) { css_resources.append(qobject_cast<CSSResource *>(resource)); } } QFutureSynchronizer<void> sync; sync.addFuture(QtConcurrent::map(css_resources, std::bind(UniversalUpdates::LoadAndUpdateOneCSSFile, std::placeholders::_1, css_updates))); html_resource.SetText(PerformHTMLUpdates(newsource, html_updates, css_updates, currentpath)()); html_resource.SetCurrentBookRelPath(""); sync.waitForFinished(); }
QHash< QString, QString > ImportOEBPS::LoadFolderStructure() { QList< QString > keys = m_Files.keys(); int num_files = keys.count(); QFutureSynchronizer< tuple< QString, QString > > sync; for ( int i = 0; i < num_files; ++i ) { QString id = keys.at( i ); sync.addFuture( QtConcurrent::run( this, &ImportOEBPS::LoadOneFile, m_Files.value( id ), m_FileMimetypes.value( id ) ) ); } sync.waitForFinished(); QList< QFuture< tuple< QString, QString > > > futures = sync.futures(); int num_futures = futures.count(); QHash< QString, QString > updates; for ( int i = 0; i < num_futures; ++i ) { tuple< QString, QString > result = futures.at( i ).result(); updates[ result.get< 0 >() ] = result.get< 1 >(); } updates.remove( UPDATE_ERROR_STRING ); return updates; }
void train(const TemplateList &data) { Mat m; OpenCVUtils::toMat(data.data()).convertTo(m, CV_64F); const QList<int> labels = data.indexProperty(inputVariable); const int dims = m.cols; std::vector<Mat> mv, av, bv; split(m, mv); for (size_t c = 0; c < mv.size(); c++) { av.push_back(Mat(1, dims, CV_64FC1)); bv.push_back(Mat(1, dims, CV_64FC1)); } QFutureSynchronizer<void> futures; for (size_t c = 0; c < mv.size(); c++) { for (int i=0; i<dims; i++) futures.addFuture(QtConcurrent::run(_train, method, mv[c].col(i), labels, &av[c].at<double>(0, i), &bv[c].at<double>(0, i))); av[c] = av[c].reshape(1, data.first().m().rows); bv[c] = bv[c].reshape(1, data.first().m().rows); } futures.waitForFinished(); merge(av, a); merge(bv, b); a.convertTo(a, data.first().m().type()); b.convertTo(b, data.first().m().type()); OpenCVUtils::saveImage(a, Globals->property("CENTER_TRAIN_A").toString()); OpenCVUtils::saveImage(b, Globals->property("CENTER_TRAIN_B").toString()); }
int br_iterate_utemplates_file(FILE *file, br_utemplate_callback callback, br_callback_context context, bool parallel) { int count = 0; QFutureSynchronizer<void> futures; while (true) { br_utemplate t = (br_utemplate) malloc(sizeof(br_universal_template)); if (!read_buffer(file, (char*) t, sizeof(br_universal_template))) { free(t); break; } t = (br_utemplate) realloc(t, sizeof(br_universal_template) + t->mdSize + t->fvSize); if (!read_buffer(file, (char*) &t->data, t->mdSize + t->fvSize)) { free(t); // Try to rewind header read if (fseek(file, -sizeof(br_universal_template), SEEK_CUR)) qFatal("Unable to recover from partial template read!"); break; } if (parallel) futures.addFuture(QtConcurrent::run(callAndFree, callback, t, context)); else callAndFree(callback, t, context); count++; } return count; }
//------------------------------------------------------------------------------------------- int AsciiSource::parseWindowMultithreaded(QVector<AsciiFileData>& window, int col, double* v, int start, const QString& field) { if (!_fileBuffer.readWindow(window)) return 0; QFutureSynchronizer<int> readFutures; foreach (const AsciiFileData& chunk, window) { QFuture<int> future = QtConcurrent::run(&_reader, &AsciiDataReader::readFieldFromChunk, chunk, col, v, start, field); readFutures.addFuture(future); }
//! [0] void someFunction() { QFutureSynchronizer<void> synchronizer; ... synchronizer.addFuture(QtConcurrent::run(anotherFunction)); synchronizer.addFuture(QtConcurrent::map(list, mapFunction)); return; // QFutureSynchronizer waits for all futures to finish }
janus_error janus_create_gallery(const char *data_path, janus_metadata metadata, janus_gallery gallery, bool verbose) { TemplateIterator ti(metadata, true); TemplateData templateData = ti.next(); QFutureSynchronizer<void> futures; while (!templateData.templateIDs.empty()) { futures.addFuture(QtConcurrent::run(_janus_create_template, data_path, templateData, gallery, verbose)); templateData = ti.next(); } futures.waitForFinished(); return JANUS_SUCCESS; }
void View::tick() { // Handle collision detections concurrently QFutureSynchronizer<void> sync; for(vector<SceneObject *>::iterator it = m_objects.begin(); it != m_objects.end(); it++) { SceneObject *obj = *it; QFuture<void> future = QtConcurrent::run(&m_snowEmitter, &SnowEmitter::collisionDetect, obj); sync.addFuture(future); } sync.waitForFinished(); m_snowEmitter.tick(); // Flag this view for repainting (Qt will call paintGL() soon after) update(); }
//------------------------------------------------------------------------------------------- int AsciiSource::parseWindowMultithreaded(QVector<AsciiFileData>& window, int col, double* v, int start, const QString& field) { updateFieldProgress(tr("reading ...")); for (int i = 0; i < window.size(); i++) { if (!window[i].read()) { return 0; } _progress++; updateFieldProgress(tr("reading ...")); } updateFieldProgress(tr("parsing ...")); QFutureSynchronizer<int> readFutures; foreach (const AsciiFileData& chunk, window) { QFuture<int> future = QtConcurrent::run(&_reader, &AsciiDataReader::readFieldFromChunk, chunk, col, v, start, field); readFutures.addFuture(future); }
// Loads the referenced files into the main folder of the book; // as the files get a new name, the references are updated QHash<QString, QString> ImportHTML::LoadFolderStructure(const QString & source) { QStringList mediapaths = XhtmlDoc::GetPathsToMediaFiles(source); QStringList stylepaths = XhtmlDoc::GetPathsToStyleFiles(source); QFutureSynchronizer<QHash<QString, QString>> sync; sync.addFuture(QtConcurrent::run(this, &ImportHTML::LoadMediaFiles, mediapaths)); sync.addFuture(QtConcurrent::run(this, &ImportHTML::LoadStyleFiles, stylepaths)); sync.waitForFinished(); QList<QFuture<QHash<QString, QString>>> futures = sync.futures(); int num_futures = futures.count(); QHash<QString, QString> updates; for (int i = 0; i < num_futures; ++i) { updates.unite(futures.at(i).result()); } return updates; }
void br_iterate_utemplates_file(FILE *file, br_utemplate_callback callback, br_callback_context context, bool parallel) { QFutureSynchronizer<void> futures; while (true) { br_utemplate t = (br_utemplate) malloc(sizeof(br_universal_template)); if (!read_buffer(file, (char*) t, sizeof(br_universal_template), true)) { free(t); return; } t = (br_utemplate) realloc(t, sizeof(br_universal_template) + t->size); read_buffer(file, (char*) &t->data, t->size, false); if (parallel) futures.addFuture(QtConcurrent::run(callAndFree, callback, t, context)); else callAndFree(callback, t, context); } futures.waitForFinished(); }
void ctkCmdLineModuleExplorerModulesSettings::applySettings() { QStringList oldModules = this->previousPropertyValue(ctkCmdLineModuleExplorerConstants::KEY_REGISTERED_MODULES).toStringList(); QStringList newModules = this->propertyValue(ctkCmdLineModuleExplorerConstants::KEY_REGISTERED_MODULES).toStringList(); QStringList removedModules; QStringList addedModules = newModules; foreach(const QString& oldModule, oldModules) { if (!newModules.contains(oldModule)) { removedModules << oldModule; } else { addedModules.removeAll(oldModule); } } this->setCursor(Qt::BusyCursor); QFuture<void> future1 = QtConcurrent::mapped(removedModules, ctkCmdLineModuleConcurrentUnRegister(this->ModuleManager)); QFuture<ctkCmdLineModuleReference> future2 = QtConcurrent::mapped(addedModules, ctkCmdLineModuleConcurrentRegister(this->ModuleManager)); ctkSettingsPanel::applySettings(); QFutureSynchronizer<void> sync; sync.addFuture(future1); sync.addFuture(future2); sync.waitForFinished(); this->ModulesRegistered = true; this->pathsAdded(addedModules); this->ModulesRegistered = false; this->unsetCursor(); ctkCmdLineModuleExplorerUtils::messageBoxModuleRegistration(addedModules, future2.results(), this->ModuleManager->validationMode()); }
void train(const TemplateList &data) { // Don't bother if the transform is untrainable if (!trainable) return; QList<TemplateList> templatesList; foreach (const Template &t, data) { if ((templatesList.size() != t.size()) && !templatesList.isEmpty()) qWarning("Independent::train (%s) template %s of size %d differs from expected size %d.", qPrintable(objectName()), qPrintable(t.file.name), t.size(), templatesList.size()); while (templatesList.size() < t.size()) templatesList.append(TemplateList()); for (int i=0; i<t.size(); i++) templatesList[i].append(Template(t.file, t[i])); } while (transforms.size() < templatesList.size()) transforms.append(transform->clone()); QFutureSynchronizer<void> futures; for (int i=0; i<templatesList.size(); i++) futures.addFuture(QtConcurrent::run(_train, transforms[i], &templatesList[i])); futures.waitForFinished(); }
float QtReduce(std::vector<float>& data, const int numberOfThreads, const int chunkSize) { std::vector<float> separate_results(numberOfThreads, FLT_MAX); QFutureSynchronizer<void> synchronizer; for(int i = 0; i < numberOfThreads; i++) synchronizer.addFuture(QtConcurrent::run(findLocalMinimum, data.begin()+i*chunkSize, data.begin()+(i+1)*chunkSize, separate_results.data()+i)); synchronizer.waitForFinished(); // serial reduce float min(FLT_MAX); for (int i = 0; i < numberOfThreads; i++) if (separate_results[i] < min) min = separate_results[i]; return min; }
QStringList UniversalUpdates::PerformUniversalUpdates(bool resources_already_loaded, const QList<Resource *> &resources, const QHash<QString, QString> &updates, const QList<XMLResource *> &non_well_formed) { QStringList updatekeys = updates.keys(); QHash<QString, QString> html_updates; QHash<QString, QString> css_updates; QHash<QString, QString> xml_updates; std::tie(html_updates, css_updates, xml_updates) = SeparateHtmlCssXmlUpdates(updates); QList<HTMLResource *> html_resources; QList<CSSResource *> css_resources; OPFResource *opf_resource = NULL; NCXResource *ncx_resource = NULL; int num_files = resources.count(); for (int i = 0; i < num_files; ++i) { Resource *resource = resources.at(i); if (resource->Type() == Resource::HTMLResourceType) { html_resources.append(qobject_cast<HTMLResource *>(resource)); } else if (resource->Type() == Resource::CSSResourceType) { css_resources.append(qobject_cast<CSSResource *>(resource)); } else if (resource->Type() == Resource::OPFResourceType) { opf_resource = qobject_cast<OPFResource *>(resource); } else if (resource->Type() == Resource::NCXResourceType) { ncx_resource = qobject_cast<NCXResource *>(resource); } } QFutureSynchronizer<void> sync; QFuture<QString> html_future; QFuture<void> css_future; if (resources_already_loaded) { html_future = QtConcurrent::mapped(html_resources, std::bind(UpdateOneHTMLFile, std::placeholders::_1, html_updates, css_updates)); css_future = QtConcurrent::map(css_resources, std::bind(UpdateOneCSSFile, std::placeholders::_1, css_updates)); } else { html_future = QtConcurrent::mapped(html_resources, std::bind(LoadAndUpdateOneHTMLFile, std::placeholders::_1, html_updates, css_updates, non_well_formed)); css_future = QtConcurrent::map(css_resources, std::bind(LoadAndUpdateOneCSSFile, std::placeholders::_1, css_updates)); } sync.addFuture(html_future); sync.addFuture(css_future); // We can't schedule these with QtConcurrent because they // will (indirectly) call QTextDocument::setPlainText, and if // a tab is open for the ncx/opf, then an event needs to be sent // to the tab widget. Events can't cross threads, and we crash. const QString ncx_result = UpdateNCXFile(ncx_resource, xml_updates); const QString opf_result = UpdateOPFFile(opf_resource, xml_updates); sync.waitForFinished(); // Now assemble our list of errors if any. QStringList load_update_errors; for (int i = 0; i < html_future.results().count(); i++) { const QString html_error = html_future.resultAt(i); if (!html_error.isEmpty()) { load_update_errors.append(html_error); } } if (!ncx_result.isEmpty()) { load_update_errors.append(ncx_result); } if (!opf_result.isEmpty()) { load_update_errors.append(opf_result); } return load_update_errors; }