bool RegionChooser::on_button_press_event(GdkEventButton* event) { if (!instrument) return true; const int k = int(event->x / (get_width() - 1) * 128.0); if (event->type == GDK_BUTTON_PRESS) { if (event->y >= REGION_BLOCK_HEIGHT) { int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 : int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1; currentActiveKey = k; keyboard_key_hit_signal.emit(k, velocity); } } if (event->y >= REGION_BLOCK_HEIGHT) return true; if (event->type == GDK_BUTTON_PRESS && event->button == 3) { gig::Region* r = get_region(k); if (r) { region = r; queue_draw(); region_selected(); dimensionManager.set_region(region); popup_menu_inside_region->popup(event->button, event->time); } else { new_region_pos = k; popup_menu_outside_region->popup(event->button, event->time); } } else { if (is_in_resize_zone(event->x, event->y)) { get_window()->pointer_grab(false, Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK, Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW), event->time); resize.active = true; } else { gig::Region* r = get_region(k); if (r) { region = r; queue_draw(); region_selected(); dimensionManager.set_region(region); get_window()->pointer_grab(false, Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK, Gdk::Cursor(Gdk::FLEUR), event->time); move.active = true; move.from_x = event->x; move.pos = 0; } } } return true; }
int cal_pulseEnergy (double *in, double frac_on, int n, double out[2]) { // define the on_pulse range, frac_on is the fraction of the phase // define the off_pulse range, frac_off is the fraction of the phase int num_on = (int)(n*frac_on); int num_off = (int)(n*(1.0-frac_on)); double on_0[num_on]; double off_0[num_off]; double on[num_on]; double off[num_on]; int index_on; int index_off; int i; double baseline = 0.0; double on_energy = 0.0; double off_energy = 0.0; index_on = def_on_pulse (n, in, frac_on); index_off = def_off_pulse (n, in, 1.0-frac_on); get_region (n, index_on, in, on_0, frac_on); get_region (n, index_off, in, off_0, 1.0-frac_on); // remove baseline for (i = 0; i < num_off; i++) { baseline += off_0[i]; } baseline = baseline/num_off; for (i = 0; i < num_on; i++) { on[i] = (on_0[i]-baseline); off[i] = (off_0[i]-baseline); //s_norm[i] = (s[i]-baseline)/(s_peak-baseline); //printf ("%lf\n", on[i]); } // calculate on pulse and off pulse energy for (i = 0; i < num_on; i++) { on_energy += on[i]; off_energy += off[i]; //printf ("%d %lf\n", i, off_energy); } out[0] = on_energy; out[1] = off_energy; //printf ("%lf %lf\n", on_energy, off_energy); return 0; }
void DatabaseIO::create_group(EntityType type, const std::string & /*type_name*/, const std::vector<std::string> &group_spec, const SideSet * /*set_type*/) { // Not generalized yet... This only works for T == SideSet if (type != SIDESET) { return; } int64_t entity_count = 0; int64_t df_count = 0; // Create the new set... auto new_set = new SideSet(this, group_spec[0]); get_region()->add(new_set); // Find the member SideSets... for (size_t i = 1; i < group_spec.size(); i++) { SideSet *set = get_region()->get_sideset(group_spec[i]); if (set != nullptr) { SideBlockContainer side_blocks = set->get_side_blocks(); for (auto &sbold : side_blocks) { size_t side_count = sbold->get_property("entity_count").get_int(); auto sbnew = new SideBlock(this, sbold->name(), sbold->topology()->name(), sbold->parent_element_topology()->name(), side_count); int64_t id = sbold->get_property("id").get_int(); sbnew->property_add(Property("set_offset", entity_count)); sbnew->property_add(Property("set_df_offset", df_count)); sbnew->property_add(Property("id", id)); new_set->add(sbnew); size_t old_df_count = sbold->get_property("distribution_factor_count").get_int(); if (old_df_count > 0) { std::string storage = "Real["; storage += Utils::to_string(sbnew->topology()->number_nodes()); storage += "]"; sbnew->field_add( Field("distribution_factors", Field::REAL, storage, Field::MESH, side_count)); } entity_count += side_count; df_count += old_df_count; } } else { IOSS_WARNING << "WARNING: While creating the grouped surface '" << group_spec[0] << "', the surface '" << group_spec[i] << "' does not exist. " << "This surface will skipped and not added to the group.\n\n"; } } }
AxisAlignedBoundingBox DatabaseIO::get_bounding_box(const Ioss::ElementBlock *eb) const { if (elementBlockBoundingBoxes.empty()) { // Calculate the bounding boxes for all element blocks... std::vector<double> coordinates; Ioss::NodeBlock * nb = get_region()->get_node_blocks()[0]; nb->get_field_data("mesh_model_coordinates", coordinates); ssize_t nnode = nb->get_property("entity_count").get_int(); ssize_t ndim = nb->get_property("component_degree").get_int(); Ioss::ElementBlockContainer element_blocks = get_region()->get_element_blocks(); size_t nblock = element_blocks.size(); std::vector<double> minmax; minmax.reserve(6 * nblock); for (auto &block : element_blocks) { double xmin, ymin, zmin, xmax, ymax, zmax; if (block->get_database()->int_byte_size_api() == 8) { std::vector<int64_t> connectivity; block->get_field_data("connectivity_raw", connectivity); calc_bounding_box(ndim, nnode, coordinates, connectivity, xmin, ymin, zmin, xmax, ymax, zmax); } else { std::vector<int> connectivity; block->get_field_data("connectivity_raw", connectivity); calc_bounding_box(ndim, nnode, coordinates, connectivity, xmin, ymin, zmin, xmax, ymax, zmax); } minmax.push_back(xmin); minmax.push_back(ymin); minmax.push_back(zmin); minmax.push_back(-xmax); minmax.push_back(-ymax); minmax.push_back(-zmax); } util().global_array_minmax(minmax, Ioss::ParallelUtils::DO_MIN); for (size_t i = 0; i < element_blocks.size(); i++) { Ioss::ElementBlock * block = element_blocks[i]; const std::string & name = block->name(); AxisAlignedBoundingBox bbox(minmax[6 * i + 0], minmax[6 * i + 1], minmax[6 * i + 2], -minmax[6 * i + 3], -minmax[6 * i + 4], -minmax[6 * i + 5]); elementBlockBoundingBoxes[name] = bbox; } } return elementBlockBoundingBoxes[eb->name()]; }
// Utility function that may be used by derived classes. Determines // whether all elements in the model have the same face topology. // This can be used to speed-up certain algorithms since they don't // have to check each face (or group of faces) individually. void DatabaseIO::set_common_side_topology() const { DatabaseIO *new_this = const_cast<DatabaseIO *>(this); bool first = true; ElementBlockContainer element_blocks = get_region()->get_element_blocks(); for (auto block : element_blocks) { size_t element_count = block->get_property("entity_count").get_int(); // Check face types. if (element_count > 0) { if (commonSideTopology != nullptr || first) { first = false; ElementTopology *side_type = block->topology()->boundary_type(); if (commonSideTopology == nullptr) { // First block new_this->commonSideTopology = side_type; } if (commonSideTopology != side_type) { // Face topologies differ in mesh new_this->commonSideTopology = nullptr; return; } } } } }
// Check topology of all sides (face/edges) in model... void DatabaseIO::check_side_topology() const { // The following code creates the sideTopology sets which contain // a list of the side topologies in this model. // // If sideTopology.size() > 1 --> the model has sides with mixed // topology (i.e., quads and tris). // // If sideTopology.size() == 1 --> the model has homogeneous sides // and each side is of the topology type 'sideTopology[0]' // // This is used in other code speed up some tests. // Spheres and Circle have no faces/edges, so handle them special... bool all_sphere = true; if (sideTopology.empty()) { // Set contains (parent_element, boundary_topology) pairs... std::set<std::pair<const ElementTopology *, const ElementTopology *>> side_topo; ElementBlockContainer element_blocks = get_region()->get_element_blocks(); for (auto &block : element_blocks) { const ElementTopology *elem_type = block->topology(); const ElementTopology *side_type = elem_type->boundary_type(); if (side_type == nullptr) { // heterogeneous sides. Iterate through... int size = elem_type->number_boundaries(); for (int i = 1; i <= size; i++) { side_type = elem_type->boundary_type(i); side_topo.insert(std::make_pair(elem_type, side_type)); all_sphere = false; } } else { // homogenous sides. side_topo.insert(std::make_pair(elem_type, side_type)); all_sphere = false; } } if (all_sphere) { // If we end up here, the model either contains all spheres, // or there are no element blocks in the model... const ElementTopology *ftopo = ElementTopology::factory("unknown"); if (element_blocks.empty()) { side_topo.insert(std::make_pair(ftopo, ftopo)); } else { const ElementBlock *block = *element_blocks.begin(); side_topo.insert(std::make_pair(block->topology(), ftopo)); } } assert(!side_topo.empty()); assert(sideTopology.empty()); // Copy into the sideTopology container... DatabaseIO *new_this = const_cast<DatabaseIO *>(this); std::copy(side_topo.begin(), side_topo.end(), std::back_inserter(new_this->sideTopology)); } assert(!sideTopology.empty()); }
/* Creates data buffers for the first backing_len bytes of a block allocation and queues them to be written */ static u8 *create_backing(struct block_allocation *alloc, unsigned long backing_len) { if (DIV_ROUND_UP(backing_len, info.block_size) > EXT4_NDIR_BLOCKS) critical_error("indirect backing larger than %d blocks", EXT4_NDIR_BLOCKS); u8 *data = calloc(backing_len, 1); if (!data) critical_error_errno("calloc"); u8 *ptr = data; for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) { u32 region_block; u32 region_len; u32 len; get_region(alloc, ®ion_block, ®ion_len); len = min(region_len * info.block_size, backing_len); queue_data_block(ptr, len, region_block); ptr += len; backing_len -= len; } return data; }
//Record new region and delete it when RegionMgr destroy. void RegionMgr::addToRegionTab(Region * ru) { ASSERT(REGION_id(ru) > 0, ("should generate new region via newRegion()")); ASSERT0(get_region(REGION_id(ru)) == NULL); ASSERT0(REGION_id(ru) < m_ru_count); m_id2ru.set(REGION_id(ru), ru); }
void RegionMgr::estimateEV( OUT UINT & num_call, OUT UINT & num_ru, bool scan_call, bool scan_inner_region) { for (UINT i = 0; i < getNumOfRegion(); i++) { Region * ru = get_region(i); if (ru == NULL) { continue; } num_ru++; ASSERT0(ru->is_function() || ru->is_program()); if (scan_call) { ru->scanCallAndReturnList(num_ru, scan_inner_region); } List<IR const*> * call_list = ru->get_call_list(); if (call_list != NULL) { num_call += call_list->get_elem_count(); } } num_ru = MAX(4, xcom::getNearestPowerOf2(num_ru)); num_call = MAX(4, xcom::getNearestPowerOf2(num_call)); }
region * create_region(unsigned int uid, int x, int y, const terrain * t) { region * r = 0; region **bucket = 0; while (!uid || r) { uid = (unsigned int)genrand_int31(); r = get_region(uid); }; assert(t); r = (region *)malloc(sizeof(region)); if (r) { memset(r, 0, sizeof(region)); bucket = ®ionhash[uid & RHASHMASK]; r->nexthash_ = *bucket; *bucket = r; r->uid = uid; r->x = x; r->y = y; r->terrain = t; ql_push(®ions, r); } return r; }
void print_digit(int const& x, int const& line, int const& size) { std::list<Line> commands; // TODO: overly complicated. Just need single print lines below // TODO: print_builder doesnt need to execute multiple commands switch (get_region(line, size)) { case 1: if (x == 1 || x == 4) { commands.push_back(Blank); } else { commands.push_back(Hor); } break; case 2: if (x == 5 || x == 6) { commands.push_back(Left); } else if (x == 1 || x == 2 || x == 3 || x == 7) { commands.push_back(Right); } else { commands.push_back(Double); } break; case 3: if (x == 1 || x == 7 || x == 0) { commands.push_back(Blank); } else { commands.push_back(Hor); } break; case 4: if (x == 2) { commands.push_back(Left); } else if (x == 6 || x == 8 || x == 0) { commands.push_back(Double); } else { commands.push_back(Right); } break; case 5: if (x == 1 || x == 4 || x == 7) { commands.push_back(Blank); } else { commands.push_back(Hor); } break; } print_builder(size, commands); }
static int dump_region(int num, struct frba_t *frba) { struct region_t region; int ret; ret = get_region(frba, num, ®ion); if (ret) return ret; printf(" Flash Region %d (%s): %08x - %08x %s\n", num, region_name(num), region.base, region.limit, region.size < 1 ? "(unused)" : ""); return ret; }
/* Queues each chunk of a file to be written to contiguous data block regions */ static void extent_create_backing_file(struct block_allocation *alloc, u64 backing_len, const char *filename) { off_t offset = 0; for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) { u32 region_block; u32 region_len; u32 len; get_region(alloc, ®ion_block, ®ion_len); len = min(region_len * info.block_size, backing_len); queue_data_file(filename, offset, len, region_block); offset += len; backing_len -= len; } }
//Dump regions recorded via addToRegionTab(). void RegionMgr::dump(bool dump_inner_region) { if (g_tfile == NULL || getNumOfRegion() == 0) { return; } g_indent = 0; note("\n==---- DUMP ALL Registered Region ----=="); for (UINT id = 0; id < getNumOfRegion(); id++) { Region * ru = get_region(id); if (ru == NULL) { continue; } ru->dump(dump_inner_region); } fflush(g_tfile); }
//This function destroy region, and free the region id //to next region alloction. void RegionMgr::deleteRegion(Region * ru, bool collect_id) { ASSERT0(ru); UINT id = REGION_id(ru); ASSERT(get_region(id), ("not registered region")); delete ru; if (collect_id && id != 0) { m_id2ru.set(id, NULL); m_free_ru_id.append_head(id); } #ifdef _DEBUG_ ASSERT0(m_num_allocated != 0); m_num_allocated--; #endif }
void RegionMgr::dumpRelationGraph(CHAR const* name) { if (getNumOfRegion() == 0) { return; } if (name == NULL) { name = "graph_region_relation_graph.vcg"; } unlink(name); xcom::Graph g; for (UINT id = 0; id < getNumOfRegion(); id++) { Region * ru = get_region(id); if (ru == NULL || ru->get_parent() == NULL) { continue; } g.addEdge(ru->get_parent()->id(), ru->id()); } g.dump_vcg(name); }
/* #define TREAT_SHAREABLE_PAGES 1 */ static long proc_residentsize(struct proc * pp) { struct pregion *prp; struct region *rp; long rtot = 0; long stot = 0; long s1tot = 0; /* init shareable region array */ if (shareable[RT_STEXT] == 0) shareable[RT_STEXT] = shareable[RT_SHMEM] = shareable[RT_MAPFILE] = 1 ; prp = pp->p_region; if (prp == 0) return 0; for (; prp && (prp = get_pregion((void *) (prp))) && prp->p_reg && (rp = get_region((void *) (prp->p_reg))); prp = prp->p_next) { if (shareable[rp->r_type]) /* account for shared pgs separately */ { stot += (rp->r_nvalid / rp->r_refcnt); s1tot += rp->r_nvalid; } else rtot += rp->r_nvalid; } #if defined(TREAT_SHAREABLE_PAGES) && TREAT_SHAREABLE_PAGES == 1 rtot += stot; /* accumulate and spread over users */ #endif #if defined(TREAT_SHAREABLE_PAGES) && TREAT_SHAREABLE_PAGES == 1 rtot += s1tot; /* accumulate as if private */ #endif return rtot * NBPP / 1024;; }
static JSBool region_to_g_argument(JSContext *context, jsval value, const char *arg_name, GjsArgumentType argument_type, GITransfer transfer, gboolean may_be_null, GArgument *arg) { JSObject *obj; cairo_region_t *region; obj = JSVAL_TO_OBJECT(value); region = get_region(context, obj); if (!region) return JS_FALSE; if (transfer == GI_TRANSFER_EVERYTHING) cairo_region_destroy(region); arg->v_pointer = region; return JS_TRUE; }
/* Creates data buffers for the first backing_len bytes of a block allocation and queues them to be written */ static u8 *extent_create_backing(struct block_allocation *alloc, u64 backing_len) { u8 *data = calloc(backing_len, 1); if (!data) critical_error_errno("calloc"); u8 *ptr = data; for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) { u32 region_block; u32 region_len; u32 len; get_region(alloc, ®ion_block, ®ion_len); len = min(region_len * info.block_size, backing_len); queue_data_block(ptr, len, region_block); ptr += len; backing_len -= len; } return data; }
/* * ======== dmm_map_memory ======== * Purpose: * Add a mapping block to the reserved chunk. DMM assumes that this block * will be mapped in the DSP/IVA's address space. DMM returns an error if a * mapping overlaps another one. This function stores the info that will be * required later while unmapping the block. */ int dmm_map_memory(struct dmm_object *dmm_mgr, u32 addr, u32 size) { struct dmm_object *dmm_obj = (struct dmm_object *)dmm_mgr; struct map_page *chunk; int status = 0; spin_lock(&dmm_obj->dmm_lock); /* Find the Reserved memory chunk containing the DSP block to * be mapped */ chunk = (struct map_page *)get_region(addr); if (chunk != NULL) { /* Mark the region 'mapped', leave the 'reserved' info as-is */ chunk->mapped = true; chunk->mapped_size = (size / PG_SIZE4K); } else status = -ENOENT; spin_unlock(&dmm_obj->dmm_lock); dev_dbg(bridge, "%s dmm_mgr %p, addr %x, size %x\n\tstatus %x, " "chunk %p", __func__, dmm_mgr, addr, size, status, chunk); return status; }
int dmm_map_memory(struct dmm_object *dmm_mgr, u32 addr, u32 size) { struct dmm_object *dmm_obj = (struct dmm_object *)dmm_mgr; struct map_page *chunk; int status = 0; spin_lock(&dmm_obj->dmm_lock); /* */ chunk = (struct map_page *)get_region(addr); if (chunk != NULL) { /* */ chunk->mapped = true; chunk->mapped_size = (size / PG_SIZE4K); } else status = -ENOENT; spin_unlock(&dmm_obj->dmm_lock); dev_dbg(bridge, "%s dmm_mgr %p, addr %x, size %x\n\tstatus %x, " "chunk %p", __func__, dmm_mgr, addr, size, status, chunk); return status; }
TITANIUM_PROPERTY_GETTER(View, region) { return MapRegionTypev2_to_js(get_context(), get_region()); }
int main(int argc, char* argv[]) { char q[512]; MYSQL_ROW row; MYSQL_RES* res; int err = init_network(); if (err) { return err; } for (int year = 1990; year <= 2011; year++) { std::cout << "Querying " << year << "\n"; snprintf(q, 512, "select count(value) from entries where year=%d and inserted>(select updated from visualizations where name='GAP2' and year=%d)", year, year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } res = mysql_use_result(mysql); if ((row = mysql_fetch_row(res)) == 0 || atoi(row[0]) == 0) { mysql_free_result(res); continue; } mysql_free_result(res); err = read_network(year); if (err) { return err; } std::cout << "Calculating " << year << "\n"; Basetype* in_flow = create_array(0); Basetype* out_flow = create_array(0); int regions_size = regions.size(); Basetype* total_output = new Basetype[regions_size]; for (int r = 0; r < regions_size; r++) { total_output[r] = 0; } for (int v = 0; v < network_size; v++) { for (int w = 0; w < network_size; w++) { in_flow[v] += flows[w][v]; out_flow[v] += flows[v][w]; } total_output[get_region(v)] += out_flow[v]; } int sectors_size = sectors.size(); Basetype** in_flow_by_sector = new Basetype*[sectors_size]; for (int i = 0; i < sectors_size; i++) { in_flow_by_sector[i] = create_array(0); } for (int v = 0; v < network_size; v++) { for (int w = 0; w < network_size; w++) { in_flow_by_sector[get_sector(v)][w] += flows[v][w]; } } snprintf(q, 512, "delete from visualization_data where visualization in (select id from visualizations where (name='GAP1' or name='GAP2') and year=%d)", year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } snprintf(q, 512, "select id from visualizations where name='GAP1' and year=%d", year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } res = mysql_use_result(mysql); row = mysql_fetch_row(res); int id1 = atoi(row[0]); mysql_free_result(res); snprintf(q, 512, "select id from visualizations where name='GAP2' and year=%d", year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } res = mysql_use_result(mysql); row = mysql_fetch_row(res); int id2 = atoi(row[0]); mysql_free_result(res); for (int r = 0; r < regions_size; r++) { Basetype* damage1 = create_array(0); Basetype* damage2 = create_array(0); int js; #pragma omp parallel default(shared) private(js) { #pragma omp for schedule(guided) nowait for (js = 0; js < network_size; js++) { if (get_region(js) == r) { damage1[js] = 1; } else { for (int i = 0; i < sectors_size; i++) { Basetype damage = flows[get_index(i, r)][js] / in_flow_by_sector[i][js]; if (damage1[js] < damage) { damage1[js] = damage; } } } } } #pragma omp parallel default(shared) private(js) { #pragma omp for schedule(guided) nowait for (js = 0; js < network_size; js++) { if (get_region(js) == r) { damage2[js] = 1; } else { for (int i = 0; i < sectors_size; i++) { Basetype damage = 0; for (int r = 0; r < regions_size; r++) { int ir = get_index(i, r); damage += damage1[ir] * flows[ir][js] / in_flow_by_sector[i][js]; } if (damage2[js] < damage) { damage2[js] = damage; } } } } } Basetype* region_damage1 = new Basetype[regions_size]; Basetype* region_damage2 = new Basetype[regions_size]; for (int r = 0; r < regions_size; r++) { region_damage1[r] = 0; region_damage2[r] = 0; } for (int js = 0; js < network_size; js++) { int s = get_region(js); if (total_output[s] > 0) { region_damage1[s] += damage1[js] * out_flow[js] / total_output[s]; region_damage2[s] += damage2[js] * out_flow[js] / total_output[s]; } } delete[] damage1; delete[] damage2; std::stringstream query1("insert into visualization_data (visualization, region_from, region_to, value) values ", std::ios_base::app | std::ios_base::out); bool first1 = true; std::stringstream query2("insert into visualization_data (visualization, region_from, region_to, value) values ", std::ios_base::app | std::ios_base::out); bool first2 = true; for (int s = 0; s < regions_size; s++) { region_damage1[s] = round(region_damage1[s] * 1000) / 1000; if (region_damage1[s] > 0) { if (first1) { first1 = false; } else { query1 << ","; } query1 << "(" << id1 << ",'" << regions[r] << "','" << regions[s] << "'," << region_damage1[s] << ")"; } region_damage2[s] = round(region_damage2[s] * 1000) / 1000; if (region_damage2[s] > 0) { if (first2) { first2 = false; } else { query2 << ","; } query2 << "(" << id2 << ",'" << regions[r] << "','" << regions[s] << "'," << region_damage2[s] << ")"; } } if (!first1) { if (mysql_query(mysql, query1.str().c_str())) { std::cerr << mysql_error(mysql) << "\n"; return -2; } } if (!first2) { if (mysql_query(mysql, query2.str().c_str())) { std::cerr << mysql_error(mysql) << "\n"; return -2; } } delete[] region_damage1; delete[] region_damage2; } delete[] in_flow; delete[] out_flow; free_double_array(flows); for (int i = 0; i < sectors_size; i++) { delete[] in_flow_by_sector[i]; } delete[] in_flow_by_sector; delete[] total_output; snprintf(q, 512, "update visualizations set updated=now() where (name='GAP1' or name='GAP2') and year=%d", year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } } return disconnect(); }
static struct block_allocation *do_inode_allocate_extents( struct ext4_inode *inode, u64 len) { u32 block_len = DIV_ROUND_UP(len, info.block_size); struct block_allocation *alloc = allocate_blocks(block_len + 1); u32 extent_block = 0; u32 file_block = 0; struct ext4_extent *extent; u64 blocks; if (alloc == NULL) { error("Failed to allocate %d blocks\n", block_len + 1); return NULL; } int allocation_len = block_allocation_num_regions(alloc); if (allocation_len <= 3) { reduce_allocation(alloc, 1); } else { reserve_oob_blocks(alloc, 1); extent_block = get_oob_block(alloc, 0); } if (!extent_block) { struct ext4_extent_header *hdr = (struct ext4_extent_header *)&inode->i_block[0]; hdr->eh_magic = EXT4_EXT_MAGIC; hdr->eh_entries = allocation_len; hdr->eh_max = 3; hdr->eh_generation = 0; hdr->eh_depth = 0; extent = (struct ext4_extent *)&inode->i_block[3]; } else { struct ext4_extent_header *hdr = (struct ext4_extent_header *)&inode->i_block[0]; hdr->eh_magic = EXT4_EXT_MAGIC; hdr->eh_entries = 1; hdr->eh_max = 3; hdr->eh_generation = 0; hdr->eh_depth = 1; struct ext4_extent_idx *idx = (struct ext4_extent_idx *)&inode->i_block[3]; idx->ei_block = 0; idx->ei_leaf_lo = extent_block; idx->ei_leaf_hi = 0; idx->ei_unused = 0; u8 *data = calloc(info.block_size, 1); if (!data) critical_error_errno("calloc"); queue_data_block(data, info.block_size, extent_block); if (((int)(info.block_size - sizeof(struct ext4_extent_header) / sizeof(struct ext4_extent))) < allocation_len) { error("File size %llu is too big to fit in a single extent block\n", len); return NULL; } hdr = (struct ext4_extent_header *)data; hdr->eh_magic = EXT4_EXT_MAGIC; hdr->eh_entries = allocation_len; hdr->eh_max = (info.block_size - sizeof(struct ext4_extent_header)) / sizeof(struct ext4_extent); hdr->eh_generation = 0; hdr->eh_depth = 0; extent = (struct ext4_extent *)(data + sizeof(struct ext4_extent_header)); } for (; !last_region(alloc); extent++, get_next_region(alloc)) { u32 region_block; u32 region_len; get_region(alloc, ®ion_block, ®ion_len); extent->ee_block = file_block; extent->ee_len = region_len; extent->ee_start_hi = 0; extent->ee_start_lo = region_block; file_block += region_len; } if (extent_block) block_len += 1; blocks = (u64)block_len * info.block_size / 512; inode->i_flags |= EXT4_EXTENTS_FL; inode->i_size_lo = len; inode->i_size_high = len >> 32; inode->i_blocks_lo = blocks; inode->osd2.linux2.l_i_blocks_high = blocks >> 32; rewind_alloc(alloc); return alloc; }
int main(int argc, char* argv[]) { char q[512]; MYSQL_ROW row; MYSQL_RES* res; int err = init_network(); if (err) { return err; } for (int year = 1990; year <= 2011; year++) { std::cout << "Querying " << year << "\n"; snprintf(q, 512, "select count(value) from entries where year=%d and inserted>(select updated from visualizations where name='Flow Centrality' and year=%d)", year, year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } res = mysql_use_result(mysql); if((row = mysql_fetch_row(res)) == 0 || atoi(row[0]) == 0) { mysql_free_result(res); continue; } mysql_free_result(res); err = read_network(year); if (err) { return err; } std::cout << "Calculating " << year << "\n"; err = disconnect(); if (err) { return err; } Basetype* betweenness = create_array(0); Basetype* in_flow = create_array(0); Basetype* out_flow = create_array(0); for (int v = 0; v < network_size; v++) { for (int w = 0; w < network_size; w++) { in_flow[v] += flows[w][v]; out_flow[v] += flows[v][w]; } } // Algorithm according to "A space-efficient parallel algorithm for computing betweenness centrality in distributed memory", p. 2 int s; #pragma omp parallel default(none) shared(betweenness, flows, network_size, in_flow, std::cerr) { #pragma omp for schedule(guided) nowait for (s = 0; s < network_size; s++) { std::vector<int> S; std::queue<int> PQ; BasetypeInt* sigma; Basetype* delta; Basetype* dist; BasetypeInt** P; BasetypeInt* P_size; Basetype* pipe; sigma = create_array_int(0); sigma[s] = 1; delta = create_array(0); P = create_double_int_array(0); P_size = create_array_int(0); dist = create_array(-1); dist[s] = 0; pipe = create_array(0); pipe[s] = -1; PQ.push(s); while (!PQ.empty()) { int v = PQ.front(); PQ.pop(); for (std::vector<int>::iterator it = S.begin(); it != S.end(); it++) { if (*it == v) { S.erase(it); break; } } S.push_back(v); for (int w = 0; w < network_size; w++) { if (w != v && w != s && flows[v][w] > 0) { Basetype c_v_w = flows[v][w]; Basetype new_pipe; if (pipe[v] < 0) { new_pipe = c_v_w; } else { new_pipe = std::min(pipe[v], c_v_w); } if (pipe[w] < new_pipe || (pipe[w] == new_pipe && dist[w] > dist[v] + 1)) { // Better best path via v PQ.push(w); pipe[w] = new_pipe; dist[w] = dist[v] + 1; sigma[w] = 0; P_size[w] = 0; } if (pipe[w] == new_pipe && dist[w] == dist[v] + 1 && !in_array(v,P[w],P_size[w])) { // Some best path via v sigma[w] += sigma[v]; P[w][P_size[w]] = v; P_size[w]++; } } } } while (!S.empty()) { int w = S.back(); S.pop_back(); for (int v_index = 0; v_index < P_size[w]; v_index++) { int v = P[w][v_index]; delta[v] += ((1 + delta[w]) * (Basetype) sigma[v]) / (Basetype) sigma[w]; } if (w != s) { #pragma omp atomic betweenness[w] += delta[w]; } } delete[] delta; delete[] sigma; delete[] dist; delete[] P_size; free_double_int_array(P); delete[] pipe; } } err = connect(); if (err) { return err; } snprintf(q, 512, "delete from visualization_data where visualization in (select id from visualizations where name='Flow Centrality' and year=%d)", year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } snprintf(q, 512, "select id from visualizations where name='Flow Centrality' and year=%d", year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } res = mysql_use_result(mysql); row = mysql_fetch_row(res); int id = atoi(row[0]); mysql_free_result(res); std::stringstream query("insert into visualization_data (visualization, sector_from, region_from, value) values ", std::ios_base::app | std::ios_base::out); for (int js = 0; js < network_size; js++) { if (js > 0) { query << ","; } query << "(" << id << ",'" << sectors[get_sector(js)] << "','" << regions[get_region(js)] << "'," << betweenness[js] << ")"; } if (mysql_query(mysql, query.str().c_str())) { std::cerr << mysql_error(mysql) << "\n"; return -2; } snprintf(q, 512, "update visualizations set updated=now() where name='Flow Centrality' and year=%d", year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } delete[] betweenness; free_double_array(flows); } return 0; }
int main() { printf("Start"); int exit_code = 0; init(); WINDOW* win; if ((win= initscr()) == NULL) { printf("can't init ncurses"); return 1; } mvaddstr(3,3,"Start\n"); refresh(); do { uint8_t* data; uint64_t* regions = calloc( (W / REGION_RES) * (H / REGION_RES), sizeof(double) ); uint32_t timestamp; int32_t err = freenect_sync_get_video( (void**)(&data), ×tamp, 0, FREENECT_VIDEO_RGB ); if (err) { delwin(win); endwin(); refresh(); printf("can't access kinect\n"); exit(1); } uint32_t offset; uint32_t max = W*H; for(offset = 0; offset < max; offset += 3 ) { uint8_t r = data[offset]; uint8_t g = data[offset+1]; uint8_t b = data[offset+2]; struct rgb_color rgb = {r,g,b}; struct hsv_color hsv = rgb_to_hsv(rgb); /* * euclidean distance from 'perfect red' */ /* uint16_t distance = sqrt( pow((255 - r),2) + pow(g,2) + pow(b,2) ); */ if ( (hsv.val > 32) && (hsv.hue < 24 || hsv.hue > 232) && (hsv.sat > 128) ) { regions[get_region(offset)] ++; } /* double cosT = r / sqrt( pow(r,2) + pow(g,2) + pow(b,2) ); regions[get_region(offset)] += cosT; */ } //int obstacle = 0; int red_regions = 0; int i,j; int base_row = 7; int base_col = 5; for (i = 0; i < (H / REGION_RES); i++) { mvaddstr(base_row + 3 * i, base_col, "|"); for (j = 0; j < (W / REGION_RES); j++) { uint64_t region_count = regions[(i * (W / REGION_RES)) + j]; char buf[16]; if (region_count > R_THRESH) { //obstacle = 1; red_regions ++; sprintf(buf, "(%"PRIu64") ", region_count); } else { sprintf(buf, "%"PRIu64" ", region_count); } mvaddstr( base_row + 3 * i, base_col + 6 * (j+1), buf ); } mvaddstr( base_row + 3 * i, base_col + 6 * ((W/REGION_RES)+1), "|"); } char buf[8]; sprintf(buf, "%d ", red_regions); mvaddstr(5,3,buf); /* if(obstacle) { mvaddstr(5,3,"WARNING!\n"); } else { mvaddstr(5,3,"Safe....\n"); } */ refresh(); free(regions); } while (LOOP); delwin(win); endwin(); refresh(); return exit_code; }
void RegionChooser::manage_dimensions() { gig::Region* region = get_region(); if (!region) return; dimensionManager.show(region); }
int main(){ Simulation *sim = malloc(sizeof(struct simulation)); Settings *sett = malloc(sizeof(struct settings)); Grid *g = malloc(sizeof(struct grid)); Boundary regionBoundaryMap[9]; int i, j, k; double PositionComponentofForceX; double PositionComponentofForceY; double Bz; double TangentVelocityComponentOfForceX; double TangentVelocityComponentOfForceY; double Mass; double vxminus; double vyminus; double t_z; double s_z; double vxprime; double vyprime; double vxplus; double vyplus; double timeStep = sett->timeStep; int region; /**X index of local origin i.e. nearest grid point BEFORE particle push*/ int xStart; /**Y index of local origin i.e. nearest grid point BEFORE particle push*/ int yStart; /**X index of local origin i.e. nearest grid point AFTER particle push*/ int xEnd; /**Y index of local origin i.e. nearest grid point AFTER particle push*/ int yEnd; /**Normalized local x coordinate BEFORE particle push*/ double x; /**Normalized local y coordinate BEFORE particle push*/ double y; /**Normalized distance covered in X direction*/ double deltaX; /**Normalized distance covered in X direction*/ double deltaY; readInputData(sim, g, sett); createBoundaryMap(regionBoundaryMap, sett->simulationWidth, sett->simulationHeight); for(j = 0; j < sett->iterations; j++){ for(i = 0; i < sett->numOfParticles; i++){ /*--------------------------------------------------------------------------/ /---------------- Step 1: particlePush()------------------------------------/ /--------------------------------------------------------------------------*/ //a) particle.storePosition() sim->particles[i].prevX = sim->particles[i].x; sim->particles[i].prevY = sim->particles[i].y; //b)solver.timeStep(particle, force, timeStep)/----Boris solver-----/ PositionComponentofForceX = getPositionComponentofForceX(sim->particles[i]); PositionComponentofForceY = getPositionComponentofForceY(sim->particles[i]); Bz = getBz(sim->particles[i]); TangentVelocityComponentOfForceX = getTangentVelocityComponentOfForceX(sim->particles[i]); TangentVelocityComponentOfForceY = getTangentVelocityComponentOfForceY(sim->particles[i]); Mass = sim->particles[i].mass; // remember for complete() sim->particles[i].prevpositionComponentForceX = PositionComponentofForceX; sim->particles[i].prevpositionComponentForceY = PositionComponentofForceY; sim->particles[i].prevBz = Bz; sim->particles[i].prevtangentVelocityComponentOfForceX = TangentVelocityComponentOfForceX; sim->particles[i].prevtangentVelocityComponentOfForceY = TangentVelocityComponentOfForceY; vxminus = sim->particles[i].vx + PositionComponentofForceX * timeStep / (2.0 * Mass); vyminus = sim->particles[i].vy + PositionComponentofForceY * timeStep / (2.0 * Mass); t_z = sim->particles[i].charge * Bz * timeStep / (2.0 * Mass); //t vector s_z = 2 * t_z / (1 + t_z * t_z); //s vector vxprime = vxminus + vyminus * t_z; vyprime = vyminus - vxminus * t_z; vxplus = vxminus + vyprime * s_z; vyplus = vyminus - vxprime * s_z; sim->particles[i].vx = vxplus + PositionComponentofForceX * timeStep / (2.0 * Mass) + TangentVelocityComponentOfForceX * timeStep / Mass; sim->particles[i].vy = vyplus + PositionComponentofForceY * timeStep / (2.0 * Mass) + TangentVelocityComponentOfForceY * timeStep / Mass; sim->particles[i].x = sim->particles[i].x + sim->particles[i].vx * timeStep; sim->particles[i].y = sim->particles[i].y + sim->particles[i].vy * timeStep; //c) boundaries.applyOnParticleCenter(solver, force, particle, timeStep) region = get_region(sim->particles[i].x, sim->particles[i].x, sim->particles[i].y, sim->particles[i].y, sett->simulationWidth, sett->simulationHeight); sim->particles[i].x = sim->particles[i].x - regionBoundaryMap[region].xoffset; sim->particles[i].prevX = sim->particles[i].prevX - regionBoundaryMap[region].xoffset; sim->particles[i].y = sim->particles[i].y - regionBoundaryMap[region].yoffset; sim->particles[i].prevY = sim->particles[i].prevY - regionBoundaryMap[region].yoffset; /*--------------------------------------------------------------------------/ /---Step 4: interpolation.interpolateToGrid(particles, grid, tstep)---------/ /--------------------------------------------------------------------------*/ resetCurrent(g); x = sim->particles[i].prevX / g->cellWidth; y = sim->particles[i].prevY / g->cellHeight; xStart = (int) floor(x + 0.5); yStart = (int) floor(y + 0.5); deltaX = sim->particles[i].x / g->cellWidth; deltaY = sim->particles[i].y / g->cellHeight; xEnd = (int) floor(deltaX + 0.5); yEnd = (int) floor(deltaY + 0.5); deltaX -= x; deltaY -= y; x -= xStart; y -= yStart; double pCharge = sim->particles[i].charge; //4-boundary move? if (xStart == xEnd && yStart == yEnd) { fourBoundaryMove(xStart, yStart, x, y, deltaX, deltaY, pCharge, g); } //7-boundary move? else if (xStart == xEnd || yStart == yEnd) { sevenBoundaryMove(x, y, xStart, yStart, xEnd, yEnd, deltaX, deltaY, pCharge, g); } // 10-boundary move else { tenBoundaryMove(x, y, xStart, yStart, xEnd, yEnd, deltaX, deltaY, pCharge, g); } createBoundaryCells(g); } } // for(i = 0; i < sett->numOfParticles; i++){ // // fscanf(inParticles, "%lg", &sim->particles[i].x); // printf("%lg\n", sim->particles[i].x); // // fscanf(inParticles, "%lg", &sim->particles[i].y); // printf("%lg\n", sim->particles[i].y); // // fscanf(inParticles, "%lg", &sim->particles[i].radius); // printf("%lg\n", sim->particles[i].radius); // // fscanf(inParticles, "%lg", &sim->particles[i].vx); // printf("%lg\n", sim->particles[i].vx); // // fscanf(inParticles, "%lg", &sim->particles[i].vy); // printf("%lg\n", sim->particles[i].vy); // // fscanf(inParticles, "%lg", &sim->particles[i].ax); // printf("%lg\n", sim->particles[i].ax); // // fscanf(inParticles, "%lg", &sim->particles[i].ay); // printf("%lg\n", sim->particles[i].ay); // // fscanf(inParticles, "%lg", &sim->particles[i].mass); // printf("%lg\n", sim->particles[i].mass); // // fscanf(inParticles, "%lg", &sim->particles[i].charge); // printf("%lg\n", sim->particles[i].charge); // // fscanf(inParticles, "%lg", &sim->particles[i].prevX); // printf("%lg\n", sim->particles[i].prevX); // // fscanf(inParticles, "%lg", &sim->particles[i].prevY); // printf("%lg\n", sim->particles[i].prevY); // // fscanf(inParticles, "%lg", &sim->particles[i].Ex); // printf("%lg\n", sim->particles[i].Ex); // // fscanf(inParticles, "%lg", &sim->particles[i].Ey); // printf("%lg\n", sim->particles[i].Ey); // // fscanf(inParticles, "%lg", &sim->particles[i].Bz); // printf("%lg\n", sim->particles[i].Bz); // // fscanf(inParticles, "%lg", &sim->particles[i].prevpositionComponentForceX); // printf("%lg\n", sim->particles[i].prevpositionComponentForceX); // // fscanf(inParticles, "%lg", &sim->particles[i].prevpositionComponentForceY); // printf("%lg\n", sim->particles[i].prevpositionComponentForceY); // // fscanf(inParticles, "%lg", &sim->particles[i].prevtangentVelocityComponentOfForceX); // printf("%lg\n", sim->particles[i].prevtangentVelocityComponentOfForceX); // // fscanf(inParticles, "%lg", &sim->particles[i].prevtangentVelocityComponentOfForceY); // printf("%lg\n", sim->particles[i].prevtangentVelocityComponentOfForceY); // // fscanf(inParticles, "%lg", &sim->particles[i].prevnormalVelocityComponentOfForceX); // printf("%lg\n", sim->particles[i].prevnormalVelocityComponentOfForceX); // // fscanf(inParticles, "%lg", &sim->particles[i].prevnormalVelocityComponentOfForceY); // printf("%lg\n", sim->particles[i].prevnormalVelocityComponentOfForceY); // // fscanf(inParticles, "%lg", &sim->particles[i].prevBz); // printf("%lg\n", sim->particles[i].prevBz); // // fscanf(inParticles, "%lg", &sim->particles[i].prevLinearDragCoefficient); // printf("%lg\n", sim->particles[i].prevLinearDragCoefficient); // } return 0; }
bool RegionChooser::on_button_press_event(GdkEventButton* event) { if (!instrument) return true; const int w = get_width() - 1; const int k = x_to_key(event->x, w); if (event->type == GDK_BUTTON_PRESS) { if (event->y >= REGION_BLOCK_HEIGHT) { int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 : int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1; currentActiveKey = k; keyboard_key_hit_signal.emit(k, velocity); } } // left mouse button double click if (event->type == GDK_2BUTTON_PRESS && event->button == 1) { if (event->y < REGION_BLOCK_HEIGHT) { // show dimension manager dialog for this region manage_dimensions(); } } if (event->y >= REGION_BLOCK_HEIGHT) return true; if (event->type == GDK_BUTTON_PRESS && event->button == 3) { gig::Region* r = get_region(k); if (r) { region = r; queue_draw(); region_selected(); dimensionManager.set_region(region); popup_menu_inside_region->popup(event->button, event->time); } else { new_region_pos = k; popup_menu_outside_region->popup(event->button, event->time); } } else { if (is_in_resize_zone(event->x, event->y)) { #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2 get_window()->pointer_grab(false, Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK, Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW), event->time); #else Glib::wrap(event->device, true)->grab(get_window(), Gdk::OWNERSHIP_NONE, false, Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK, Gdk::Cursor::create(Gdk::SB_H_DOUBLE_ARROW), event->time); #endif resize.active = true; } else { gig::Region* r = get_region(k); if (r) { region = r; queue_draw(); region_selected(); dimensionManager.set_region(region); #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2 get_window()->pointer_grab(false, Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK, Gdk::Cursor(Gdk::FLEUR), event->time); #else Glib::wrap(event->device, true)->grab(get_window(), Gdk::OWNERSHIP_NONE, false, Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK, Gdk::Cursor::create(Gdk::FLEUR), event->time); #endif move.active = true; move.offset = event->x - key_to_x(region->KeyRange.low, w); } } } return true; }