ShapeEnum<D, MultiIndex> extend(const ShapeEnum<D, MultiIndex>* source) { std::vector< ShapeSlice<D, MultiIndex> > slices(source->n_slices()+1); std::size_t offset = 1; slices[0] = source->slice(0); for (int islice = 0; islice < source->n_slices(); islice++) { slices[islice+1] = _extend(source->slice(islice), offset); offset += slices[islice+1].size(); } MultiIndex limits = source->limits(); for (dim_t d = 0; d < D; d++) { limits[d] += 1; } return {std::move(slices), offset, limits}; }
void IMG_setmodules(Image_t *img) { assert(img != NULL); IMG_begintransaction(img); sqlq_t slices(img->db, "SELECT module, address, size FROM tab_modslice"); while(slices.step()) { int modid = slices.col_int(0); uaddr_t addr = slices.col_int(1); int size = slices.col_int(2); sqlq_t label(img->db, "UPDATE tab_label SET module=@A WHERE address >= @B AND address < @C"); //printf("slice %d %08X %d\n", modid, addr, size); label.bind_int(1, modid); label.bind_int(2, addr); label.bind_int(3, addr + size); label.run(); sqlq_t reloc(img->db, "UPDATE tab_reloc SET module=@A WHERE address >= @B AND address < @C"); reloc.bind_int(1, modid); reloc.bind_int(2, addr); reloc.bind_int(3, addr + size); reloc.run(); } IMG_endtransaction(img); }
int PieChart::qt_metacall(QMetaObject::Call _c, int _id, void **_a) { _id = QDeclarativeItem::qt_metacall(_c, _id, _a); if (_id < 0) return _id; #ifndef QT_NO_PROPERTIES if (_c == QMetaObject::ReadProperty) { void *_v = _a[0]; switch (_id) { case 0: *reinterpret_cast< QDeclarativeListProperty<PieSlice>*>(_v) = slices(); break; case 1: *reinterpret_cast< QString*>(_v) = name(); break; } _id -= 2; } else if (_c == QMetaObject::WriteProperty) { void *_v = _a[0]; switch (_id) { case 1: setName(*reinterpret_cast< QString*>(_v)); break; } _id -= 2; } else if (_c == QMetaObject::ResetProperty) { _id -= 2; } else if (_c == QMetaObject::QueryPropertyDesignable) { _id -= 2; } else if (_c == QMetaObject::QueryPropertyScriptable) { _id -= 2; } else if (_c == QMetaObject::QueryPropertyStored) { _id -= 2; } else if (_c == QMetaObject::QueryPropertyEditable) { _id -= 2; } else if (_c == QMetaObject::QueryPropertyUser) { _id -= 2; } #endif // QT_NO_PROPERTIES return _id; }
void Pruner<FT>::optimize_coefficients_local_adjust_incr_prob(/*io*/ vector<double> &pr) { int trials, tours, maxi, ind; FT old_cf, old_cf0, old_cfs, new_cf, old_b; double current_max; vector<double> detailed_cost(n); vector<double> slices(n, 10.0); // (b[i+1] - b[i])/slice will be used as step vec b(n); load_coefficients(b, pr); // initial cost old_cf0 = target_function(b); tours = 0; while (1) { tours++; // old cost old_cf = target_function(b); // find bottleneck index old_cfs = single_enum_cost(b, &(detailed_cost)); current_max = 0.0; maxi = 0; for (int i = 0; i < n; i++) { if (detailed_cost[i] > current_max) { current_max = detailed_cost[i]; maxi = i; } } ind = n - maxi - 1; if (ind <= 1) break; #ifdef BALANCE_HEURISTIC_PRUNER_OPTIMIZE if (old_cfs > sqrt(old_cf) / 10.0) break; #endif for (int i = ind; i >= 1; --i) { if (b[i] <= b[i - 1]) continue; trials = 0; // fixed i-1, trying to increase b[i-1] while (1) { // old cost old_cf = target_function(b); // try increase old_b = b[i - 1]; b[i - 1] = b[i - 1] + (b[i] - b[i - 1]) / slices[i - 1]; // new cost new_cf = target_function(b); // cerr << " i = " << i << " old_cf = " << old_cf << " new_cf = " << // new_cf << endl; // if not improved -- recover if (new_cf >= (old_cf * 1.2)) { b[i - 1] = old_b; break; } else { if (slices[i - 1] < 1024) slices[i - 1] = slices[i - 1] * 1.2; } trials++; if (trials >= 10) break; } } new_cf = target_function(b); if (new_cf > (old_cf0 * 1.1) || tours > 4) break; } #ifdef DEBUG_PRUNER_OPTIMIZE_TC cerr << "# [TuningProb]" << endl; cerr << b << endl; cerr << "# [TuningProb] all_enum_cost = " << repeated_enum_cost(b) << endl; cerr << "# [TuningProb] succ_probability = " << measure_metric(b) << endl; #endif save_coefficients(pr, b); }
void Pruner<FT>::optimize_coefficients_local_adjust_decr_single(/*io*/ vector<double> &pr) { int maxi, lasti, consecutive_fails; double improved_ratio, current_max = 0.0; FT old_cf, old_cfs, new_cf, old_b; vector<double> detailed_cost(n); vector<double> slices(n, 10.0); // (b[i+1] - b[i])/slice will be used as step vector<int> thresholds(n, 3); vec b(n); load_coefficients(b, pr); lasti = -1; // last failed index, make sure we do not try it again in // the next time consecutive_fails = 0; // number of consecutive failes; break if // reaches it improved_ratio = 0.995; // if reduced by 0.995, descent while (1) { // old cost old_cf = target_function(b); // find bottleneck index old_cfs = single_enum_cost(b, &(detailed_cost)); // heuristic #ifdef BALANCE_HEURISTIC_PRUNER_OPTIMIZE if (old_cfs < sqrt(old_cf) / 10.0) break; #endif current_max = 0.0; maxi = 0; for (int i = 0; i < n; i++) { if ((i != (n - lasti - 1)) && (thresholds[n - i - 1] > 0)) { if (detailed_cost[i] > current_max) { current_max = detailed_cost[i]; maxi = i; } } } // b[ind] is the one to be reduced int ind = n - maxi - 1; old_b = b[ind]; if (ind != 0) { b[ind] = b[ind] - (b[ind] - b[ind - 1]) / slices[ind]; } else { break; } // new cost new_cf = target_function(b); // if not improved -- recover if (new_cf >= (old_cf * improved_ratio)) { b[ind] = old_b; lasti = ind; thresholds[lasti]--; consecutive_fails++; } else { // cerr << " improved from " << old_cf << " to " << new_cf << endl; if (slices[ind] < 1024) slices[ind] = slices[ind] * 1.05; consecutive_fails = 0; } // quit after 10 consecutive failes if (consecutive_fails > 10) { break; } } #ifdef DEBUG_PRUNER_OPTIMIZE_TC cerr << "# [TuningCost]" << endl; cerr << b << endl; cerr << "# [TuningCost] all_enum_cost = " << repeated_enum_cost(b) << endl; cerr << "# [TuningCost] succ_probability = " << measure_metric(b) << endl; #endif save_coefficients(pr, b); }
ShapeEnum<D,MultiIndex> generate(const Shape& shape) const { std::vector< std::vector<MultiIndex> > mindices; // check multi-index type for compatibility { MultiIndex test; for (dim_t d = 0; d < D; d++) { test[d] = shape.bbox(d); if (test[d] != shape.bbox(d)) { throw std::runtime_error("multi-index type is not suitable. reason: overflow"); } } } // initialize slice vectors { std::size_t sum = 0; for (dim_t d = 0; d < D; d++) { sum += shape.bbox(d)+1; } mindices.resize(sum); } // enumerate shape & store all multi-indices { MultiIndex index{}; //zero initialize std::size_t islice = 0; while (true) { // iterate over last axis for (dim_t i = 0; i <= shape.limit(static_cast<std::array<int,D> >(index).data(),D-1); i++) { index[D-1] = i; // if (use_dict) // mindices[islice+i].dict_[index] = mindices[islice+i].table_.size(); mindices[islice+i].push_back(index); } index[D-1] = 0; // iterate over other axes if (D > 1) { dim_t j = D-2; while ((int)index[j] == shape.limit(static_cast<std::array<int,D> >(index).data(),j)) { islice -= index[j]; index[j] = 0; if (j == 0) goto enumeration_complete; else j = j-1; } islice += 1; index[j] += 1; } else break; } enumeration_complete: (void)0; } std::vector< ShapeSlice<D,MultiIndex> > slices(mindices.size()); std::size_t offset = 0; // number of entries in all previous slices std::size_t islice = 0; // ordinal of current slice for (; islice < mindices.size() && mindices[islice].size() != 0; islice++) { ShapeSlice<D,MultiIndex> slice{std::move(mindices[islice]), offset}; offset += slice.size(); slices[islice] = std::move(slice); } slices.resize(islice); // total number of entries in all slices size_t size = offset; MultiIndex limits; for (dim_t d = 0; d < D; d++) limits[d] = shape.bbox(d); return {std::move(slices), size, limits}; }
bool WorkerThread::AssignTask( TaskBase * newTask ) { // check if there is only one thread.. if so, just bail if(Pool_->WorkersCount() == 1) return false; // if there is no root task (threads are idle / finishing sub-tasks) if( Pool_->CurrentCompletion_ == nullptr ) { // ASSERT that this is the main thread assert( WorkerThread::This()->Index_ == 0 ); // enforce all workers idle Pool_->WaitUntilIdle(); // attempt to split the task by the number of threads scoped_ptr<TaskBase::Range> slices(newTask->Split( Pool_->WorkersCount() )); if(slices != nullptr) { // assign each thread a task (INCLUDING THIS ONE) for(uint32 i = 0; i < slices->size(); ++i) { WorkerThread * w = Pool_->Workers_[i]; //boost::mutex::scoped_lock worker_lock( w->TaskMutex_ ); MutexLock(w->TaskMutex_); (*slices)[i]->Completion_->Set(true); w->Tasks_.push_back( (*slices)[i] ); MutexUnlock(w->TaskMutex_); } // set the root task in the pool to newTask Pool_->CurrentCompletion_ = newTask->Completion_; // wake all the threads Pool_->WakeAll(); return true;// return the love } } // lock this thread's task vector { //boost::mutex::scoped_lock this_lock(TaskMutex_); MutexLock(TaskMutex_); // if the task vector is full, return false if( Tasks_.size() >= WorkerThread::TaskCapacity ) { MutexUnlock(TaskMutex_); return false; } // set the task as busy newTask->Completion_->Set(true); // assign the job Tasks_.push_back( newTask ); MutexUnlock(TaskMutex_); } // if there is no root task (threads are idle / finishing sub-tasks) if( Pool_->CurrentCompletion_ == nullptr ) { // set the root task in the pool to newTask Pool_->CurrentCompletion_ = newTask->Completion_; // wake any idle threads Pool_->WakeAll(); } return true; // return the love }
void Disk::drawQuadric(GLUquadric* i_quadric) { gluDisk(i_quadric, m_inner_radius, m_outer_radius, slices(), LOOPS); }
uint8_t parse_GPRMC(char *nmeaString, GPSFix *fix) { uint8_t nmeaStringIndex = 0; uint8_t nmeaDelimCount = 0; uint8_t nmeaDelimIndex[11] = {0}; uint8_t nmeaEOM = 0; uint8_t nmeaChecksumCalc, nmeaChecksumRecv = 0; char temp[8]; while (nmeaString[nmeaStringIndex] != '\n') { // find the indices of the comma delimiters in the NMEA sentence if (nmeaString[nmeaStringIndex] == ',') { nmeaDelimIndex[nmeaDelimCount] = nmeaStringIndex; nmeaDelimCount++; } // find index of the * delimiter at the end of the NMEA sentence // end of message (EOM) if (nmeaString[nmeaStringIndex] == '*') { nmeaEOM = nmeaStringIndex; } nmeaStringIndex++; } // calculate the checksum of the NMEA sentence for (nmeaStringIndex = 1; nmeaStringIndex < nmeaEOM; nmeaStringIndex++) { nmeaChecksumCalc ^= nmeaString[nmeaStringIndex]; } // retrieve the checksum of the NMEA sentence calculated by the GPS temp[0] = nmeaString[nmeaEOM+1]; temp[1] = nmeaString[nmeaEOM+2]; nmeaChecksumRecv = (uint8_t)strtol(temp, NULL, 16); // check wether the calculated and the received checksums match if (nmeaChecksumCalc != nmeaChecksumRecv) return 1; // retrieve UTC time information memset(temp, 0, 8); slices(nmeaString, nmeaDelimIndex[0]+1, nmeaDelimIndex[0]+3, temp); fix->utc_hours = atoi(temp); memset(temp, 0, 8); slices(nmeaString, nmeaDelimIndex[0]+3, nmeaDelimIndex[0]+5, temp); fix->utc_minutes = atoi(temp); memset(temp, 0, 8); slices(nmeaString, nmeaDelimIndex[0]+5, nmeaDelimIndex[1], temp); fix->utc_seconds = atoi(temp); fix->status = nmeaString[nmeaDelimIndex[1]+1]; // retrieve latitude information memset(temp, 0, 8); slices(nmeaString, nmeaDelimIndex[2]+1, nmeaDelimIndex[2]+3, temp); fix->lat_deg = atoi(temp); fix->lat = (float)atof(temp); memset(temp, 0, 8); slices(nmeaString, nmeaDelimIndex[2]+3, nmeaDelimIndex[3], temp); fix->lat_moa = (float)atof(temp); fix->lat += (float)atof(temp)/60.0; fix->lat_hemi = nmeaString[nmeaDelimIndex[3]+1]; // retrieve longitude information memset(temp, 0, 8); slices(nmeaString, nmeaDelimIndex[4]+1, nmeaDelimIndex[4]+4, temp); fix->lon_deg = atoi(temp); fix->lon = (float)atof(temp); memset(temp, 0, 8); slices(nmeaString, nmeaDelimIndex[4]+4, nmeaDelimIndex[5], temp); fix->lon_moa = (float)atof(temp); fix->lon += (float)atof(temp)/60.0; fix->lon_hemi = nmeaString[nmeaDelimIndex[5]+1]; // retrieve speed in knots memset(temp, 0, 8); slices(nmeaString, nmeaDelimIndex[6]+1, nmeaDelimIndex[7], temp); fix->speed_kn = (float)atof(temp); fix->speed_kmh = fix->speed_kn * 1.852; // retrieve true course memset(temp, 0, 8); slices(nmeaString, nmeaDelimIndex[7]+1, nmeaDelimIndex[8], temp); fix->track = (float)atof(temp); // retrieve UTC date information memset(temp, 0, 8); slices(nmeaString, nmeaDelimIndex[8]+1, nmeaDelimIndex[8]+3, temp); fix->utc_day = atoi(temp); memset(temp, 0, 8); slices(nmeaString, nmeaDelimIndex[8]+3, nmeaDelimIndex[8]+5, temp); fix->utc_month = atoi(temp); memset(temp, 0, 8); slices(nmeaString, nmeaDelimIndex[8]+5, nmeaDelimIndex[9], temp); fix->utc_year = atoi(temp); // retrieve magnetic deviation angle and direction memset(temp, 0, 8); slices(nmeaString, nmeaDelimIndex[9]+1, nmeaDelimIndex[10], temp); fix->mag_dev_angle = (float)atof(temp); fix->mag_dev_direction = nmeaString[nmeaDelimIndex[10]+1]; return 0; }