void main_thread( AppData& app_data, CommonData& common, const std::string& screen_name ) { #ifdef CLOUD_TRACE_USE_NV_copy_image x11::Display display(screen_name.empty()?nullptr:screen_name.c_str()); #else const x11::Display& display = common.display; (void)screen_name; #endif static int visual_attribs[] = { GLX_DRAWABLE_TYPE , GLX_PIXMAP_BIT, GLX_RENDER_TYPE , GLX_RGBA_BIT, GLX_X_VISUAL_TYPE , GLX_TRUE_COLOR, GLX_RED_SIZE , 8, GLX_GREEN_SIZE , 8, GLX_BLUE_SIZE , 8, GLX_ALPHA_SIZE , 8, None }; glx::FBConfig fbc = glx::FBConfigs( display, visual_attribs ).FindBest(display); x11::VisualInfo vi(display, fbc); x11::Pixmap xpm(display, vi, 8, 8); glx::Pixmap gpm(display, vi, xpm); #ifdef CLOUD_TRACE_USE_NV_copy_image glx::Context context(display, fbc, 3, 3); #else glx::Context context(display, fbc, common.context, 3, 3); #endif context.MakeCurrent(gpm); common.thread_ready.Signal(); common.master_ready.Wait(); if(!common.Done()) { try { thread_loop(app_data, common, display, context); } catch(...) { common.PushError(std::current_exception()); } } context.Release(display); }
QPixmap pixmapForIndex(int index, const QPalette& pal, bool transparentBackground = false) const { if (index < 0 || index > 18) index = 0; QPixmap xpm(m_xpms[index]); if (transparentBackground) return xpm; QPixmap px(xpm.size()); QColor bg( pal.color(QPalette::Base) ); // paint bg with to avoid invisible black-on-black painting bg.setAlpha(127); px.fill(bg); QPainter p(&px); p.drawPixmap(0, 2, xpm); return px; }
ModuleInfo parseModuleInfo(const utils::Buffer& buf) { utils::StructReader sr(reinterpret_cast<const char*>(buf.getPtr())); try { std::string name = sr.getStringValue("gui_name"); std::string identifier = sr.getStringValue("identifier"); std::string group = sr.getStringValue("group"); std::string inputs = sr.getStringValue("inputs"); std::string outputs = sr.getStringValue("outputs"); std::string moduleType = sr.getStringValue("type"); try { std::vector<PlugInfo> ins = processArray(inputs); std::vector<PlugInfo> outs = processArray(outputs); if (moduleType == "xpm") { const char* ptr = reinterpret_cast<const char*>(buf.getPtr()); const char* offset = ptr + strlen(ptr) + 1; int newLen = buf.getLen() - strlen(ptr) - 1; utils::AutoPtr<Xpm> xpm(new Xpm(offset,newLen)); return ModuleInfo(identifier,name,group,ins,outs,xpm); } else { throw std::runtime_error("MUIltkj"); } } catch (std::logic_error&) { std::cerr << "Buffer = <" << buf.getPtr() << ">" << std::endl; } } catch (std::runtime_error& e) { std::string msg = "Fehler beim Parsen der ModulInfo: "; msg += e.what(); throw std::runtime_error(msg.c_str()); } }
void example_thread_main(ExampleThreadData& data) { const ExampleThreadData::Common& common = data.common(); const x11::ScreenNames& sn = common.screen_names; // pick one of the available display screens // for this thread std::size_t disp_idx = data.thread_index; if(sn.size() > 1) ++disp_idx; disp_idx %= sn.size(); // open the picked display x11::Display display(sn[disp_idx].c_str()); // initialize the pixelmaps and the sharing context x11::Pixmap xpm(display, common.vi, 8, 8); glx::Pixmap gpm(display, common.vi, xpm); glx::Context ctx(display, common.fbc, common.ctx, 3, 3); ctx.MakeCurrent(gpm); // signal that the context is created common.thread_ready.Signal(); // wait for the example to be created // in the main thread common.master_ready.Wait(); // if something failed - quit if(common.failure) { common.thread_ready.Signal(); return; } else { try { assert(common.example); // call makeExampleThread std::unique_ptr<ExampleThread> example_thread( makeExampleThread( *common.example, data.thread_index, common.example_params ) ); data.example_thread = example_thread.get(); // signal that it is created common.thread_ready.Signal(); // wait for the main thread common.master_ready.Wait(); // if something failed - quit if(common.failure) return; // start rendering while(!common.done && !common.failure) { example_thread->Render(common.clock); glFlush(); } data.example_thread = nullptr; } catch(...) { data.example_thread = nullptr; throw; } } ctx.Release(display); }
void ScriptExtender::scanFile(const QString& filename){ QFile file(filename); if (!file.open(QIODevice::ReadOnly | QIODevice::Text )){ qDebug() << "Error launching script"; return; } QString in = file.readAll(); QRegExp rx; rx = QRegExp( "<FILTER>(.+)</FILTER>" ); rx.setMinimal(true); if (!rx.indexIn(in,0))return; QString filter = rx.cap(1); //for each found function rx = QRegExp( "<FUNCTION>(.+)</FUNCTION>" ); rx.setMinimal(true); for (int pos = 0; (pos = rx.indexIn(in, pos)) != -1; pos += rx.matchedLength()) { QStringList split = rx.cap(1).split(" "); switch (split.size()){ case 1: slotlist << SSlot("",split.at(0), slotlist.size(), QRegExp(filter),filename ); break; case 2: slotlist << SSlot(split.at(0), split.at(1),slotlist.size(), QRegExp(filter),filename ); break; default: qDebug() << "<FUNCTION>....</FUNCTION> with illegal content"; } } rx = QRegExp( "<ACTION>(.+)</ACTION>" ); rx.setMinimal(true); for (int pos = 0; (pos = rx.indexIn(in, pos)) != -1; pos += rx.matchedLength()) { QRegExp tx; tx = QRegExp( "<TEXT>(.+)</TEXT>" ); if (!tx.indexIn(rx.cap(1),0))return; QString text = tx.cap(1); tx = QRegExp( "<SLOT>(.+)</SLOT>" ); if (!tx.indexIn(rx.cap(1),0))return; QString slot = tx.cap(1); const char** icon = xpm(rx.cap(1)); actionlist << SAction(icon,text,QString("1%1").arg(slot),QRegExp(filter)); } //qDebug() << " sssssss" << SLOT(test()); }
Problem_Representation_Type const operator()( Chromosome_Args ... ch_args ) { //initialize first generation randomly for ( std::size_t i = 0; i != population_per_generation; ++i ) current_population.push_back( chromosome_type( ch_args... ) ); //std::cerr << "\ninitialized " << population_per_generation << " chromosomes.\n"; std::size_t debug_counter = 0; best_fitness = std::numeric_limits<Fitness_Type>::max(); srand ( unsigned ( time (NULL) ) ); //for random_shuffle Fitness_Type total_fit; Problem_Representation_Type pr_0; Problem_Representation_Type pr_1; Problem_Representation_Type pr_2; Problem_Representation_Type pr_3; std::ofstream elite_out( "elite.dat" ); std::ofstream elite_fit( "elite_fit.dat"); for (;;) { //evaluate //feng::for_each( current_population.begin(), current_population.end(), [](chromosome_type& chrom) { Evaluation_Method()(Chromosome_Problem_Translation_Method()(chrom)); }); //std::cerr << "\nevaluating........"; #if 0 for ( auto& ch : current_population ) if ( ch.modification_after_last_evaluation_flag ) { Chromosome_Problem_Translation_Method()( *(ch.chrom), pr ); ch.fit = Evaluation_Method()(pr); //Evaluation_Method()(Chromosome_Problem_Translation_Method()(ch)); ch.modification_after_last_evaluation_flag = false; } #endif #if 1 std::thread t0( parallel_evaluator(), current_population.begin(), current_population.begin()+(current_population.size()/4), &pr_0 ); std::thread t1( parallel_evaluator(), current_population.begin()+(current_population.size()/4), current_population.begin()+(current_population.size()/2), &pr_1 ); std::thread t2( parallel_evaluator(), current_population.begin()+(current_population.size()/2), current_population.begin()+(current_population.size()*3/4), &pr_2 ); std::thread t3( parallel_evaluator(), current_population.begin()+(current_population.size()*3/4), current_population.end(), &pr_3 ); //parallel_evaluator()( current_population.begin()+(current_population.size()*3/4), current_population.end(), &pr_3 ); t0.join(); t1.join(); t2.join(); t3.join(); #endif //using nth_element? //mutate duplicated chromosome? std::sort( current_population.begin(), current_population.end() ); auto const elite_one = *(current_population.begin()); Chromosome_Problem_Translation_Method()( *(elite_one.chrom), pr ); //keep the best individual of the current generation if ( elite_one.fit < best_fitness ) { debug_counter++; best_fitness = elite_one.fit; best_one = pr; elite_out << best_one; elite_fit << best_fitness; if ( debug_counter & 0xf ) { elite_out << "\n"; elite_fit << "\n"; } else { elite_out << std::endl; elite_fit << std::endl; } } current_population.resize( std::distance( current_population.begin(), std::unique( current_population.begin(), current_population.end() )) ); if ( current_population.size() < population_per_generation ) { //generate someone randomly and evaluate for ( std::size_t i = current_population.size(); i != population_per_generation; ++i ) { auto ch = chromosome_type( ch_args...); Chromosome_Problem_Translation_Method()( *(ch.chrom), pr ); ch.fit = Evaluation_Method()(pr); ch.modification_after_last_evaluation_flag = false; current_population.push_back( ch ); } } else { //shuffle the resize std::random_shuffle( current_population.begin(), current_population.end() ); current_population.resize( population_per_generation ); } std::sort( current_population.begin(), current_population.end() ); //total_fit = 0; //for( auto& ch : current_population ) // total_fit += ch.fit; //std::cout.precision(20); //std::cout << total_fit << "\n"; //if timeout, return output elite auto& t_manager = feng::singleton<time_manager>::instance(); if ( t_manager.is_timeout() ) { std::cerr << "\nthe residual for the best individual is " << best_fitness; elite_out.close(); elite_fit.close(); return best_one; //return pr; } //std::cerr << "\nElite of " << debug_counter++ << " is \n" << pr; //std::cerr << "\nElite of " << debug_counter << " is \n" << *(elite_one.chrom); //select auto& xpm = feng::singleton<xover_probability_manager<>>::instance(); std::size_t const selection_number = xpm(population_per_generation); //selected_population_for_xover_father.resize(selection_number); //selected_population_for_xover_mother.resize(selection_number); selected_population_for_xover_father.clear(); selected_population_for_xover_mother.clear(); //std::cerr << "\nselecting............." << selection_number; auto& xs_manager = feng::singleton<xover_selection_manager>::instance(); for ( std::size_t i = 0; i != selection_number; ++i ) { //selected_population_for_xover_father[i] = current_population[xs_manager()]; //selected_population_for_xover_mother[i] = current_population[xs_manager()]; const std::size_t select1 = xs_manager(); const std::size_t select2 = xs_manager(); //selected_population_for_xover_father.push_back(current_population[xs_manager()]); //selected_population_for_xover_mother.push_back(current_population[xs_manager()]); selected_population_for_xover_father.push_back(current_population[select1]); selected_population_for_xover_mother.push_back(current_population[select2]); } //std::cerr << "\nselectedted"; //xover #if 0 //not working as selected population for xover are pure reference of current populaiton for ( std::size_t i = 0; i != selection_number; ++i ) feng::for_each( selected_population_for_xover_father[i].begin(), selected_population_for_xover_father[i].end(), selected_population_for_xover_mother[i].begin(), Chromosome_Xover_Method() ); current_population.reserve( population_per_generation + selection_number + selection_number ); //append newly generated genes into current population current_population.insert( current_population.begin()+population_per_generation, selected_population_for_xover_father.begin(), selected_population_for_xover_father.end() ); current_population.insert( current_population.begin()+population_per_generation+selection_number, selected_population_for_xover_mother.begin(), selected_population_for_xover_mother.end() ); #endif //std::cerr << "\nxover............."; for ( std::size_t i = 0; i != selection_number; ++i ) { chromosome_type son( ch_args... ); chromosome_type daughter( ch_args... ); feng::for_each( selected_population_for_xover_father[i].begin(), selected_population_for_xover_father[i].end(), selected_population_for_xover_mother[i].begin(), son.begin(), daughter.begin(), Chromosome_Xover_Method() ); current_population.push_back( son ); current_population.push_back( daughter ); } //mark new generation not evaluated feng::for_each( current_population.begin()+population_per_generation, current_population.end(), [](chromosome_type& ch) { ch.modification_after_last_evaluation_flag = true; } ); //std::cerr << "\nxovered"; //mutate //std::cerr << "\nmutating"; auto& mpm = feng::singleton<mutation_probability_manager<>>::instance(); mpm.reset(); for ( auto& chromosome : current_population ) if ( mpm.should_mutate_current_gene() ) { for ( auto& gene : chromosome ) Chromosome_Mutation_Method()(gene); //makr muated ones as not evaluated chromosome.modification_after_last_evaluation_flag = true; } //std::cerr << "\nmutated"; //goto evaluate } assert( !"Should never reach here!!" ); return Problem_Representation_Type(); }