//returns -1 on fail, number of vertices on success int load_off_mesh(FILE* fp, jmesh *jm){ fseek(fp, 0, SEEK_SET); //ensure we are at the start of the file if(is_off_file(fp) != 0){ fprintf(stderr, "not an off file\n"); return -1; } int i; i=get_verts_faces(fp, jm); if(i != 3){ fprintf(stderr,"off file corrupt on line 2\n"); return -1; } i=get_tuples(fp, jm); if(i != jm->nvert){ fprintf(stderr,"off file corrupt on tuple line %d\n", i); return -1; } i=get_faces(fp, jm); if(i != jm->ntri){ fprintf(stderr,"off file corrupt on polygon line %d\n", i); return -1; } get_normals(jm); get_centroid(jm, jm->center); return i; }
void assign_closest_serial(It begin, It end, int K, leveldb::DB* work_db, const std::vector<GDELTMini>& centroids, vuad& totals, vuai& cluster_sizes) { GDELTMini current_row; for (auto itit = begin; itit != end; ++itit) { auto& range = *itit; for (range.reset(); range.should_continue(); range.it->Next()) { read(range.it->value(), current_row); int min = -1; double min_dist = std::numeric_limits<double>::infinity(); for (int i = 0; i < K; ++i) { double d = Rho(current_row, centroids[i]); if (d < min_dist) { min = i; min_dist = d; } } CHECK(min >= 0); add_atomic_double(totals[min].get(), min_dist); auto i = cluster_sizes[min]->fetch_add(1) + 1; int prev = get_centroid(work_db, range.it->key(), K); set_centroid(work_db, range.it->key(), prev, min, K); } } }
point* defuzzify(fuzzy_engine* engine) { uint8_t consequents_no = engine->consequents->length; rule_consequent** consequent_list = (rule_consequent**)os_malloc( consequents_no * sizeof(rule_consequent*)); uint8_t count = 0, i = 0; linked_list_node* node = engine->consequents->head; while(node != 0) { consequent_list[count] = (rule_consequent*)node->data; node = node->next; count++; } order_consequents_geometrically(consequent_list, consequents_no); point* raw_points = get_raw_polygon_points(consequent_list, consequents_no); // debugging // for(i = 0; i < consequents_no * 4; i++) { // printf("%.2f, %.2f\n", raw_points[i].x, raw_points[i].y); // } linked_list* points = get_polygon_points(raw_points, consequents_no * 4); // debugging // node = points->head; // point * p; // while(node != 0) { // p = (point*)node->data; // printf("%.2f, %.2f\n", p->x, p->y); // node = node->next; // } point* centroid = get_centroid(points); // free everything linked_list_node *aux_node; node = points->head; while(node != 0) { aux_node = node->next; os_free(node->data); os_free(node); node = aux_node; } os_free(points); os_free(raw_points); os_free(consequent_list); // reset rules linked_list_node *rule_node = engine->rules->head; while(rule_node != 0) { fuzzy_rule* rule = rule_node->data; rule->consequent->result = 0; rule->result = 0; rule_node = rule_node->next; } return centroid; }
void Circle::move(double dx, double dy) { for (size_t i = 0; i < num_points; ++i) { pts[i].x += dx; pts[i].y += dy; } centroid = get_centroid(); }
/* makeStraightEdge: * * FIX: handle ports on boundary? */ void makeStraightEdge(graph_t * g, edge_t * e, int et, splineInfo* sinfo) { pointf dumb[4]; node_t *n = agtail(e); node_t *head = aghead(e); int e_cnt = ED_count(e); int curved = (et == ET_CURVED); pointf perp; pointf del; edge_t *e0; int i, j, xstep, dx; double l_perp; pointf dumber[4]; pointf p, q; p = dumb[1] = dumb[0] = add_pointf(ND_coord(n), ED_tail_port(e).p); q = dumb[2] = dumb[3] = add_pointf(ND_coord(head), ED_head_port(e).p); if ((e_cnt == 1) || Concentrate) { if (curved) bend(dumb,get_centroid(g)); clip_and_install(e, aghead(e), dumb, 4, sinfo); addEdgeLabels(g, e, p, q); return; } e0 = e; if (APPROXEQPT(dumb[0], dumb[3], MILLIPOINT)) { /* degenerate case */ dumb[1] = dumb[0]; dumb[2] = dumb[3]; del.x = 0; del.y = 0; } else { perp.x = dumb[0].y - dumb[3].y; perp.y = dumb[3].x - dumb[0].x; l_perp = LEN(perp.x, perp.y); xstep = GD_nodesep(g->root); dx = xstep * (e_cnt - 1) / 2; dumb[1].x = dumb[0].x + (dx * perp.x) / l_perp; dumb[1].y = dumb[0].y + (dx * perp.y) / l_perp; dumb[2].x = dumb[3].x + (dx * perp.x) / l_perp; dumb[2].y = dumb[3].y + (dx * perp.y) / l_perp; del.x = -xstep * perp.x / l_perp; del.y = -xstep * perp.y / l_perp; } for (i = 0; i < e_cnt; i++) { if (aghead(e0) == head) { p = dumb[0]; q = dumb[3]; for (j = 0; j < 4; j++) { dumber[j] = dumb[j]; } } else { p = dumb[3]; q = dumb[0]; for (j = 0; j < 4; j++) { dumber[3 - j] = dumb[j]; } } if (et == ET_PLINE) { Ppoint_t pts[4]; Ppolyline_t spl, line; line.pn = 4; line.ps = pts; for (j=0; j < 4; j++) { pts[j] = dumber[j]; } make_polyline (line, &spl); clip_and_install(e0, aghead(e0), spl.ps, spl.pn, sinfo); } else clip_and_install(e0, aghead(e0), dumber, 4, sinfo); addEdgeLabels(g, e0, p, q); e0 = ED_to_virt(e0); dumb[1].x += del.x; dumb[1].y += del.y; dumb[2].x += del.x; dumb[2].y += del.y; } }
int main(const int argc, const char** argv) { // omp_set_num_threads(8); // Problem size and other parameters const int nBodies = (argc > 1 ? atoi(argv[1]) : 16384); const int nSteps = 10; // Duration of test const float dt = 0.01f; // Body propagation time step Centroid c; // Body data stored as an Array of Structures (AoS) struct BodyType bodies[nBodies]; // Initialize random number generator and bodies srand(0); float randmax; randmax = (float) RAND_MAX; for(int i = 0; i < nBodies; i++) { bodies[i].x = ((float) rand())/randmax; bodies[i].y = ((float) rand())/randmax; bodies[i].z = ((float) rand())/randmax; bodies[i].vx = ((float) rand())/randmax; bodies[i].vy = ((float) rand())/randmax; bodies[i].vz = ((float) rand())/randmax; } // Compute initial center of mass c = get_centroid(nBodies, bodies); printf("Initial center of mass: (%g, %g, %g)\n", c.x, c.y, c.z); // Perform benchmark printf("\n\033[1mNBODY Version 02\033[0m\n"); printf("\nPropagating %d bodies using %d thread on %s...\n\n", nBodies, omp_get_num_threads(), "CPU"); double rate = 0, dRate = 0; // Benchmarking data const int skipSteps = 3; // Set this to a positive int to skip warm-up steps printf("\033[1m%5s %10s %10s %8s\033[0m\n", "Step", "Time, s", "Interact/s", "GFLOP/s"); fflush(stdout); for (int step = 1; step <= nSteps; step++) { const double tStart = omp_get_wtime(); // Start timing MoveBodies(nBodies, bodies, dt); const double tEnd = omp_get_wtime(); // End timing // These are for calculating flop rate. It ignores symmetry and // estimates 20 flops per body-body interaction in MoveBodies const float HztoInts = (float)nBodies * (float)(nBodies-1) ; const float HztoGFLOPs = 20.0*1e-9*(float)nBodies*(float)(nBodies-1); if (step > skipSteps) { // Collect statistics rate += HztoGFLOPs / (tEnd - tStart); dRate += HztoGFLOPs * HztoGFLOPs / ((tEnd-tStart)*(tEnd-tStart)); } printf("%5d %10.3e %10.3e %8.1f %s\n", step, (tEnd-tStart), HztoInts/(tEnd-tStart), HztoGFLOPs/(tEnd-tStart), (step<=skipSteps?"*":"")); fflush(stdout); } rate/=(double)(nSteps-skipSteps); dRate=sqrt(fabs(dRate/(double)(nSteps-skipSteps)-rate*rate)); printf("-----------------------------------------------------\n"); printf("\033[1m%s %4s \033[42m%10.1f +- %.1f GFLOP/s\033[0m\n", "Average performance:", "", rate, dRate); printf("-----------------------------------------------------\n"); printf("* - warm-up, not included in average\n\n"); // Compute final center of mass c = get_centroid(nBodies, bodies); printf("Final center of mass: (%g, %g, %g)\n", c.x, c.y, c.z); }