int main(int argc, char ** args) { char *source; int count; long elapsed; struct timespec timer; int i; for (i = 1; i < argc; ++i) { timer_start(&timer); long filesize = readFileToBuffer(args[i], &source); elapsed = timer_end(timer); printf("%lu bytes of file '%s' loaded in %.2fms\n", filesize, args[i], elapsed/1000000.0); timer_start(&timer); count = kaesvesCountAbcabc(source); elapsed = timer_end(timer); printf("kaesve reports %d matches in %.2fms\n", count, elapsed/1000000.0); timer_start(&timer); count = mytherinsCountAbcabc(source); elapsed = timer_end(timer); printf("mytherin reports %d matches in %.2fms\n", count, elapsed/1000000.0); printf("\n"); free(source); } }
Solution cplex_solve(TSP tsp, Solution prev, CPLEX cplex) { double distance; double *x = malloc(tsp.cols * sizeof(double)); Solution solution; solution.i = prev.i + 1; solution.n = 0; solution.subtours = malloc(sizeof(Subtour *) * (tsp.n / 3)); struct timespec cplex_start = timer_start(); CPXmipopt(cplex.env, cplex.lp); // solve the next cycle CPXsolution(cplex.env, cplex.lp, NULL, &solution.distance, x, NULL, NULL, NULL); solution.cplex_time = timer_end(cplex_start); struct timespec code_start = timer_start(); // Collect all active solution variables int count = 0; int *vars = malloc(sizeof(int) * tsp.n); for (int i = 0; i < tsp.cols; ++i) { if (x[i] == 0) continue; vars[count++] = i; if (count == tsp.n) break; // All active solution vars found } while (subtour_exists(tsp, vars)) { subtour_insert(subtour_get(tsp, vars), solution.subtours, &(solution.n)); } free(vars); solution.work_time = timer_end(code_start); return solution; }
void dump_flushes(void) { char filename[256]; unsigned long n = 0; struct flush *f; FILE *fp; unsigned long v; struct timer t; timer_init(CLOCK_REALTIME, &t); timer_start(&t); snprintf(filename, 256, "/lustre/medusa/merritt/dump.%d", getpid()); fp = fopen(filename, "w"); if (!fp) return; fprintf(fp, "bytes latency synchrony time execlat\n"); while (n < num_flushes) { f = &flushes[n]; fprintf(fp, "%lu %lu %d %lu %lu\n", f->bytes, f->lat, f->blocking, f->ts.tv_sec * 1000000000UL + f->ts.tv_nsec, f->exec); n++; } fclose(fp); v = timer_end(&t, MICROSECONDS); printf("> %s %lu usec\n", __func__, v); }
void waitNanos(int n){ struct timespec timeNS = timer_start(); long dT; while(dT<n){ dT = timer_end(timeNS); } dT/=1000; printf("%ld \n",dT); }
bool loop_calc( ddTableDealsPBN * dealsp, ddTablesRes * resp, allParResults * parp, dealPBN * deal_list, ddTableResults * table_list, int number) { #ifdef BATCHTIMES printf("%8s %24s\n", "Hand no.", "Time"); #endif int filter[5] = {0, 0, 0, 0, 0}; for (int i = 0; i < number; i += input_number) { int count = (i + input_number > number ? number - i : input_number); dealsp->noOfTables = count; for (int j = 0; j < count; j++) { strcpy(dealsp->deals[j].cards, deal_list[i + j].remainCards); } timer_start(); int ret; if ((ret = CalcAllTablesPBN(dealsp, -1, filter, resp, parp)) != RETURN_NO_FAULT) { printf("loop_solve i %i: Return %d\n", i, ret); exit(0); } tu = timer_end(); #ifdef BATCHTIMES printf("%8d (%5.1f%%) %15d\n", i + count, 100. * (i + count) / static_cast<double>(number), tu); fflush(stdout); #endif for (int j = 0; j < count; j++) if (! compare_TABLE(&resp->results[j], &table_list[i + j])) { printf("loop_calc table i %d, j %d: Difference\n", i, j); print_TABLE( &resp->results[j] ); print_TABLE( &table_list[i + j]) ; } } #ifdef BATCHTIMES printf("\n"); #endif return true; }
void loop_solve( boardsPBN * bop, solvedBoards * solvedbdp, dealPBN * deal_list, futureTricks * fut_list, int number) { #ifdef BATCHTIMES printf("%8s %24s\n", "Hand no.", "Time"); #endif for (int i = 0; i < number; i += input_number) { int count = (i + input_number > number ? number - i : input_number); bop->noOfBoards = count; for (int j = 0; j < count; j++) { bop->deals[j] = deal_list[i + j]; bop->target[j] = -1; bop->solutions[j] = 3; bop->mode[j] = 1; } timer_start(); int ret; if ((ret = SolveAllChunks(bop, solvedbdp, 1)) != RETURN_NO_FAULT) { printf("loop_solve i %i: Return %d\n", i, ret); exit(0); } tu = timer_end(); #ifdef BATCHTIMES printf("%8d (%5.1f%%) %15d\n", i + count, 100. * (i + count) / static_cast<double>(number), tu); fflush(stdout); #endif for (int j = 0; j < count; j++) { if (! compare_FUT(&solvedbdp->solvedBoard[j], &fut_list[i + j])) { printf("loop_solve i %d, j %d: Difference\n", i, j); } } } #ifdef BATCHTIMES printf("\n"); #endif }
int main() { input(); timer_begin(); sort_index(); dfs(0, 0); timer_end(); fprintf(stderr, "n = %d, dfs_cnt = %d, duration = %f\n", n, dfs_cnt, timer_duration()); output(); return 0; }
int main () { struct timespec vartime = timer_start(); for(double a = 0; a < 10000000; a++ ){ exp(a); } long time_elapsed_nanos = timer_end(vartime); printf("exp taken (nanoseconds): %ld\n", time_elapsed_nanos); vartime = timer_start(); srand(time(NULL)); double x = (double)rand(); double m = (double)rand(); double s = (double)rand(); int n=0; for(double i = 1; i < 10000000; i++){ pdf(x,m,s); } time_elapsed_nanos = timer_end(vartime); printf("pdf taken (nanoseconds): %ld\n", time_elapsed_nanos); return(0); }
int main (int argc, char *argv[]) { timer_obj_t tmr; while (1) { timer_start(&tmr); nano_seconds_sleep(999999999); timer_end(&tmr); printf("%ld seconds %ld nanoseconds elapsed (%lld)\n", tmr.end.tv_sec - tmr.start.tv_sec, tmr.end.tv_nsec - tmr.start.tv_nsec, timer_delay_nsecs(&tmr)); } }
static void cleanup() { timer_end(); freeoptions(""); #if !NO_MENU rm_invitfile(); #endif #ifdef SOCKET sock_flags &= ~CONNECTED; rmsocket(); #endif #if ALLEGRO && WIN32 && !ALLEGRO_USE_CONSOLE fclose(stdout); delete_file("stdout.tmp"); #endif }
int main(int argc, char *argv[]) { int rc; int num_tasks; int task_id; double *old, *current, *next, *ret; int t_max, i_max; double time; rc = MPI_Init(&argc, &argv); // Initialize MPI runtime if (rc != MPI_SUCCESS) { // Check for success fprintf(stderr, "Unable to set up MPI\n"); MPI_Abort(MPI_COMM_WORLD, rc); // Abort MPI runtime } /* Parse commandline args */ if (argc < 3) { printf("Usage: %s i_max t_max num_threads [initial_data]\n", argv[0]); printf(" - i_max: number of discrete amplitude points, should be >2\n"); printf(" - t_max: number of discrete timesteps, should be >=1\n"); printf(" - num_threads: number of threads to use for simulation, " "should be >=1\n"); printf(" - initial_data: select what data should be used for the first " "two generation.\n"); printf(" Available options are:\n"); printf(" * sin: one period of the sinus function at the start.\n"); printf(" * sinfull: entire data is filled with the sinus.\n"); printf(" * gauss: a single gauss-function at the start.\n"); printf(" * file <2 filenames>: allows you to specify a file with on " "each line a float for both generations.\n"); return EXIT_FAILURE; } i_max = atoi(argv[1]); t_max = atoi(argv[2]); if (i_max < 3) { printf("argument error: i_max should be >2.\n"); return EXIT_FAILURE; } if (t_max < 1) { printf("argument error: t_max should be >=1.\n"); return EXIT_FAILURE; } if (task_id == 0) { timer_start(); } /* Get MPI rankings */ MPI_Comm_size(MPI_COMM_WORLD, &num_tasks); MPI_Comm_rank(MPI_COMM_WORLD, &task_id); /* Scale i_max to the number of threads * with halo cells in mind(left and right) */ i_max = (i_max/num_tasks) + 2; /* Allocate and initialize buffers. */ old = malloc(i_max * sizeof(double)); current = malloc(i_max * sizeof(double)); next = malloc(i_max * sizeof(double)); if (old == NULL || current == NULL || next == NULL) { fprintf(stderr, "Could not allocate enough memory, aborting.\n"); return EXIT_FAILURE; } memset(old, 0, i_max * sizeof(double)); memset(current, 0, i_max * sizeof(double)); memset(next, 0, i_max * sizeof(double)); /* How should we will our first two generations? This is determined by the * optional further commandline arguments. */ if (argc > 3) { if (strcmp(argv[3], "sin") == 0) { fill(old, 1, i_max/4, 0, 2*3.14, num_tasks, task_id, sin); fill(current, 2, i_max/4, 0, 2*3.14, num_tasks, task_id, sin); } else if (strcmp(argv[3], "sinfull") == 0) { fill(old, 1, i_max-2, 0, 10*3.14, num_tasks, task_id, sin); fill(current, 2, i_max-3, 0, 10*3.14, num_tasks, task_id, sin); } else if (strcmp(argv[3], "gauss") == 0) { fill(old, 1, i_max/4, -3, 3, num_tasks, task_id, gauss); fill(current, 2, i_max/4, -3, 3, num_tasks, task_id, gauss); } else if (strcmp(argv[3], "file") == 0) { if (argc < 6) { printf("No files specified!\n"); return EXIT_FAILURE; } file_read_double_array(argv[4], old, i_max); file_read_double_array(argv[5], current, i_max); } else { printf("Unknown initial mode: %s.\n", argv[3]); return EXIT_FAILURE; } } else { /* Default to sinus. */ fill(old, 1, i_max/4, 0, 2*3.14, num_tasks, task_id, sin); fill(current, 2, i_max/4, 0, 2*3.14, num_tasks, task_id, sin); } /* Call the actual simulation that should be implemented in simulate.c. */ ret = simulate(i_max, t_max, old, current, next, num_tasks, task_id); /* If not controlling process, send message that calculation is done */ MPI_Barrier(MPI_COMM_WORLD); if (!task_id) { time = timer_end(); printf("Took %g seconds\n", time); printf("Normalized: %g seconds\n", time / (1. * i_max * t_max)); /* Write own data */ file_write_double_array("result.txt", ret+1, i_max-1, "w"); /* Receive the data from all the other users */ for (int i = 1; i < num_tasks; i++) { MPI_Recv(ret, i_max, MPI_DOUBLE, i, MSG_WRITE, MPI_COMM_WORLD, NULL); file_write_double_array("result.txt", ret+1, i_max-1, "a+"); } } /* Non-Controller */ if (task_id) { /* Send to controller */ MPI_Send(ret, i_max, MPI_DOUBLE, 0, MSG_WRITE, MPI_COMM_WORLD); } MPI_Finalize(); fprintf(stderr, "Finalized\n"); free(old); free(current); free(next); fprintf(stderr, "Done\n"); return EXIT_SUCCESS; }
void perform_avl_tree_test (avl_tree_t *avlt, int use_odd_numbers) { int i, lo, hi, d, fail_search; int rv; int fine, not_fine; char *oddness = use_odd_numbers ? "odd" : "even"; char *reverse = use_odd_numbers ? "even" : "odd"; unsigned long long int bytes_used; double megabytes_used; void *fwdata, *searched, *found, *removed; chunk_manager_parameters_t chparams; printf("size of ONE avl node is: %lu bytes\n", sizeof(avl_node_t)); /* ** Fill opposing ends of array, converge in middle. ** This gives some sort of randomness to data. */ printf("filling array of size %d with %s number data\n", MAX_SZ, oddness); d = use_odd_numbers ? 1 : 0; lo = 0; hi = MAX_SZ - 1; while (1) { data[lo++] = d; d += 2; data[hi--] = d; d += 2; if (lo > hi) break; } if (max_value_reached < d) { max_value_reached = d + 10; printf("max value recorded so far is %d\n", max_value_reached); } chparams.initial_number_of_chunks = max_value_reached + 10; chparams.grow_size = 1024; avl_tree_init(avlt, 1, int_compare, NULL, &chparams); /* enter all array data into avl tree */ printf("now entering all %s number data into the avl tree\n", oddness); timer_start(&timr); for (i = 0; i < MAX_SZ; i++) { fwdata = &data[i]; rv = avl_tree_insert(avlt, fwdata, &found); if (rv != 0) { printf("populate_data: avl_tree_insert error: %d failed\n", i); } } timer_end(&timr); timer_report(&timr, MAX_SZ, NULL); OBJECT_MEMORY_USAGE(avlt, bytes_used, megabytes_used); printf("total memory used by the avl tree of %d nodes: %llu bytes (%lf Mbytes)\n", avlt->n, bytes_used, megabytes_used); printf("searching for non existant data\n"); fine = not_fine = 0; timer_start(&timr); for (i = max_value_reached; i < (max_value_reached + EXTRA); i++) { searched = &i; rv = avl_tree_search(avlt, searched, &found); if ((rv == 0) || found) { not_fine++; } else { fine++; } } timer_end(&timr); timer_report(&timr, EXTRA, NULL); printf("expected %d, NOT expected %d\n", fine, not_fine); /* now search all data that should be found (all of them) */ printf("now searching all %s numbers in the avl tree\n", oddness); fine = not_fine = 0; timer_start(&timr); for (i = 0; i < MAX_SZ; i++) { searched = &data[i]; rv = avl_tree_search(avlt, searched, &found); if ((rv != 0) || (data[i] != *((int*) found))) { not_fine++; } else { fine++; } } timer_end(&timr); timer_report(&timr, MAX_SZ, NULL); printf("found %d as expected and %d as NOT expected\n", fine, not_fine); /* now search for all entries that should NOT be in the tree */ printf("now searching for all %s numbers in the avl tree\n", reverse); fine = not_fine = 0; d = use_odd_numbers ? 0 : 1; timer_start(&timr); for (i = 0; i < MAX_SZ; i++) { searched = &d; rv = avl_tree_search(avlt, searched, &found); if ((rv == 0) || found) { not_fine++; } else { fine++; } d += 2; } timer_end(&timr); timer_report(&timr, MAX_SZ, NULL); printf("%d as expected and %d as NOT expected\n", fine, not_fine); #if 0 int tree_nodes = avlt->n; printf("now deleting the whole tree (%d nodes)\n", tree_nodes); timer_start(&timr); avl_tree_destroy(avlt); timer_end(&timr); timer_report(&timr, tree_nodes, NULL); return; #endif // 0 /* now delete one entry at a time and search it (should not be there) */ printf("now deleting and searching\n"); fine = not_fine = fail_search = 0; timer_start(&timr); for (i = 0; i < MAX_SZ; i++) { searched = &data[i]; rv = avl_tree_remove(avlt, searched, &removed); if ((rv != 0) || (data[i] != *((int*) removed))) { not_fine++; } else { fine++; } rv = avl_tree_search(avlt, searched, &found); if ((rv == 0) || found) { fail_search++; } } timer_end(&timr); timer_report(&timr, MAX_SZ*2, NULL); printf("deleted %d, could NOT delete %d, erroneous search %d\n", fine, not_fine, fail_search); OBJECT_MEMORY_USAGE(avlt, bytes_used, megabytes_used); printf("total memory used by the avl tree (%d nodes) after deletes: " "%llu bytes (%f Mbytes)\n", avlt->n, bytes_used, megabytes_used); avl_tree_destroy(avlt); }
bool loop_play( boardsPBN * bop, playTracesPBN * playsp, solvedPlays * solvedplp, dealPBN * deal_list, playTracePBN * play_list, solvedPlay * trace_list, int number) { #ifdef BATCHTIMES printf("%8s %24s\n", "Hand no.", "Time"); #endif for (int i = 0; i < number; i += input_number) { int count = (i + input_number > number ? number - i : input_number); bop->noOfBoards = count; playsp->noOfBoards = count; for (int j = 0; j < count; j++) { bop->deals[j] = deal_list[i + j]; bop->target[j] = 0; bop->solutions[j] = 3; bop->mode[j] = 1; playsp->plays[j] = play_list[i + j]; } timer_start(); int ret; if ((ret = AnalyseAllPlaysPBN(bop, playsp, solvedplp, 1)) != RETURN_NO_FAULT) { printf("loop_play i %i: Return %d\n", i, ret); exit(0); } tu = timer_end(); #ifdef BATCHTIMES printf("%8d (%5.1f%%) %15d\n", i + count, 100. * (i + count) / static_cast<double>(number), tu); fflush(stdout); #endif for (int j = 0; j < count; j++) { if (! compare_TRACE(&solvedplp->solved[j], &trace_list[i + j])) { printf("loop_play i %d, j %d: Difference\n", i, j); // printf("trace_list[%d]: \n", i+j); // print_TRACE(&trace_list[i+j]); // printf("solvedplp[%d]: \n", j); // print_TRACE(&solvedplp->solved[j]); } } } #ifdef BATCHTIMES printf("\n"); #endif return true; }
/* timer stuff */ void GlWindow::timer_init (void) { connect (&m_timer, SIGNAL (timeout ()), this, SLOT (timer_end ())); m_timer.start (10000); m_frame_offset = 0; }
int main(int argc, char ** argv) { // enhanced usage, useful for testing if (argc != 1 && argc != 2 && argc != 7) { fprintf(stderr, "Usage: %s [[threads] yMin yMax xMin xMax dxy]\n", argv[0]); fprintf(stderr, "Either specify no args, or only threads, or all args.\n"); return -2; } // determine amount of threads if (argc > 1) omp_set_num_threads(atoi(argv[1])); else omp_set_num_threads(4); // set constants if supplied if (argc == 7) { yMin = atof(argv[2]); yMax = atof(argv[3]); xMin = atof(argv[4]); xMax = atof(argv[5]); dxy = atof(argv[6]); } double time; timer_start(); double cx, cy; double zx, zy, new_zx; unsigned char n; int nx, ny; // The Mandelbrot calculation is to iterate the equation // z = z*z + c, where z and c are complex numbers, z is initially // zero, and c is the coordinate of the point being tested. If // the magnitude of z remains less than 2 for ever, then the point // c is in the Mandelbrot set. We write out the number of iterations // before the magnitude of z exceeds 2, or UCHAR_MAX, whichever is // smaller. nx = 0; ny = 0; nx = (xMax - xMin) / dxy; ny = (yMax - yMin) / dxy; int i, j; unsigned char * buffer = malloc(nx * ny * sizeof(unsigned char)); if (buffer == NULL) { fprintf (stderr, "Couldn't malloc buffer!\n"); return EXIT_FAILURE; } // do the calculations parallel #pragma omp parallel for private(i, j, cx, zx, zy, n, new_zx, cy) for (i = 0; i < ny; i++) { cy = yMin - dxy + i * dxy; for (j = 0; j < nx; j++) { cx = xMin - dxy + j * dxy; zx = 0.0; zy = 0.0; n = 0; while ((zx*zx + zy*zy < 4.0) && (n != UCHAR_MAX)) { new_zx = zx*zx - zy*zy + cx; zy = 2.0*zx*zy + cy; zx = new_zx; n++; } buffer[i * nx + j] = n; } } time = timer_end(); fprintf (stderr, "Took %g seconds.\nNow writing file...\n", time); fwrite(buffer, sizeof(unsigned char), nx * ny, stdout); fprintf (stderr, "All done! To process the image: convert -depth 8 -size " \ "%dx%d gray:output out.jpg\n", nx, ny); return 0; }
int main(int argc, char *argv[]) { FILE *ifile, *ofile, *ofile2; const char *ofname, *ofname2; Image *img; unsigned char *edge_pix; PList *edge_pts; float threshold, radius; #ifdef MTRACE setenv("MALLOC_TRACE", "mtrace.out", 0); mtrace(); #endif // check args if (argc < 2) { fprintf(stderr, "Usage: %s IN.bmp [BLUR_RADIUS [THRESHOLD [OUT.bmp [POINTS.txt]]]]\n", argv[0]); return 1; } // open input image ifile = fopen(argv[1], "r"); if (!ifile) { fprintf(stderr, "Error opening '%s'.\n", argv[1]); return 1; } // read input image img = Image_createFromBMP(ifile); fclose(ifile); if (!img) { LOG("Image_createFromBMP failed"); return -1; } // read blur radius radius = (argc >= 3) ? atof(argv[2]) : -1.f; if (!isfinite(radius) || radius < 0.f) { radius = 5.f; fprintf(stderr, "Using default blur radius: %g\n", radius); } // read threshold threshold = (argc >= 4) ? atof(argv[3]) : -1.f; if (!isfinite(threshold) || threshold < 0.f) { threshold = 10.f; fprintf(stderr, "Using default threshold: %g\n", threshold); } // open output image ofname = (argc >= 5) ? argv[4] : "out.bmp"; ofile = fopen(ofname, "wb"); if (!ofile) fprintf(stderr, "Error opening '%s'.\n", ofname); // open output points file ofname2 = (argc >= 6) ? argv[5] : "pts.txt"; ofile2 = fopen(ofname2, "w"); if (!ofile2) fprintf(stderr, "Error opening '%s'.\n", ofname2); // create output structures edge_pix = (unsigned char *) malloc(img->w * img->h); if (!edge_pix) { LOG("alloc edge_pix failed"); return -1; } edge_pts = PList_create(); if (!edge_pts) LOG("alloc edge_pts failed"); // find edge pixels timer_start(); struct timeval tmp = start_time; // hack so other calls don't affect main timer detect_edges(img, radius, threshold, edge_pix, edge_pts); start_time = tmp; elapsed = 0.0; timer_end("detect_edges"); // write output image (edge pixels) if (ofile && edge_pix) { grayscale_writeBMP(edge_pix, img->w, img->h, ofile); fclose(ofile); } // write list of edge points if (ofile2 && edge_pts) { int i, n = edge_pts->len; for (i = 0; i < n; i++) { P p = edge_pts->pts[i]; fprintf(ofile2, "%04d,%04d\n", p.y, p.x); } fclose(ofile2); } // cleanup Image_free(img); free(edge_pix); PList_free(edge_pts); #ifdef MTRACE muntrace(); #endif return 0; }
bool f_test(void) { int i = TEST_COUNT, count = 0, failed; file_id_t id; struct f_test_arr_t arr[FILE_ID_END]; WORD w_c, tim; float ave; BYTE *p; f_write(FILE4, 0, (const BYTE *)"AAAAAAA", 7); f_write(FILE7, 0, (const BYTE *)"AAAAAAA", 7); //printf("\n\n=================BEGIN TEST %u clock/s ==============\n", TIMER_CLOCK); while (i--) { rand_800(test_str, BUF_SIZE); rand_arr(arr, FILE_ID_END); failed = 0; //printf("\n"); w_c = 0; tim = 0; //fprintf(stderr, "-----------A-----------\n"); for (id = FILE1; id < FILE_ID_END; id++) { if (id == FILE4 || id == FILE7) continue; timer_start(); f_write(id, arr[id].offset, test_str, arr[id].len); if (memcmp(f_read(id, arr[id].offset, BUF, arr[id].len), test_str, arr[id].len) != 0) { fprintf(stderr, "memcmp failed after write. FILE%d offset %lu, len %lu\n", id+1, arr[id].offset, arr[id].len); } tim += timer_end(); w_c += arr[id].len; //fprintf(stderr, "f_write(FILE%d, addr %lu, \toffset %lu, \tlen %lu)\t\t...\n", id + 1, f_addr(id), arr[id].offset, arr[id].len); } //fprintf(stderr, "-----------Z-----------\n"); ave = (tim / TIMER_CLOCK) / w_c * 1000.0 * 1000; //printf("write %lu byte use %lu clock, average %.2fus/B\n", w_c, tim, ave); w_c = 0; tim = 0; for (id = FILE1; id < FILE_ID_END; id++) { #if 1 if (id == FILE4 || id == FILE7) { w_c += 7; timer_start(); p = f_read(id, 0, BUF, 7); tim += timer_end(); if (memcmp(p, "AAAAAAA", 7) != 0) { fprintf(stderr, "memcmy failed. FILE%u, offset %d, len %d\n", id +1, 0, 7); failed = 1; return false; } else { ;//printf("f_read(FILE%d, \toffset %d, \tlen %d)\t\tok\n", id + 1, 0, 7); } continue; } timer_start(); p = f_read(id, arr[id].offset, BUF, arr[id].len); tim += timer_end(); if (memcmp(p, test_str, arr[id].len) != 0) { fprintf(stderr, "memcmy failed. FILE%u, offset %lu, len %lu\n", id +1, arr[id].offset, arr[id].len); failed = 1; w_c += arr[id].len; return false; } else { w_c += arr[id].len; //fprintf(stderr, "f_read(FILE%d, \toffset %d, \tlen %d)\t\tok\n", id + 1, arr[id].offset, arr[id].len); } #else timer_start(); f_read(id, arr[id].offset, BUF, arr[id].len); tim += timer_end(); w_c += arr[id].len; #endif } ave = (tim / TIMER_CLOCK) / w_c * 1000.0 * 1000; //printf("read %lu byte use %lu clock, average %.2fus/B\n", w_c, tim, ave); if (!failed) count++; if (failed) break; //f_sync(); } #if 0 tim = 0; timer_start(); f_erase(FILE9); tim += timer_end(); printf("\nerase %lu byte use %lu clock\n", f_size(FILE9), tim); tim = 0; for (i = f_size(FILE9) / 32 ; i >= 0; i--) { //memset(BUF, 0, sizeof(BUF)); //printf("file 9 len = %d\n", fs.file[FILE9].file_len); timer_start(); f_write(FILE9, f_len(FILE9), (const BYTE *)"12345678901234567890123456789012", 32); tim += timer_end(); //f_read(FILE9, fs.file[FILE9].file_len - 32, BUF, 32); //printf("read value is %s\n", BUF); } ave = (tim / TIMER_CLOCK) / f_len(FILE9) * 1000 * 1000; printf("append %lu byte use %lu clock, average %.2fus/B \n", f_len(FILE9), tim, ave); #endif printf("=====Test %d, pass %d, failed %d.=====\n", TEST_COUNT, count, TEST_COUNT - count); f_sync(); //f_dump(); if (count != TEST_COUNT) return false; else return true; }
int main(int argc, char *argv[]){ if (argc < 3){ fprintf(stderr, "Usage: %s [matrix1] [matrix2]\n", argv[0]); exit(1); } // MPI Magic int rank, size, namelen; char name[MPI_MAX_PROCESSOR_NAME]; MPI::Status stat; MPI::Init(argc, argv); size = MPI::COMM_WORLD.Get_size(); rank = MPI::COMM_WORLD.Get_rank(); MPI::Get_processor_name(name, namelen); MPILOG("reading mtx files"); Matrix * m1 = new Matrix(argv[1]); Matrix * m2 = new Matrix(argv[2]); int m1_rows_size = m1->rows.size()-1; int m2_rows_size = m2->rows.size()-1; timer_init(); if(rank == 0){ int row_count = m1_rows_size; //13; //m1->rows.size()-1 // count number of rows per node int * rows = new int[size]; for(int i=0; i<size; i++) rows[i] = 0; int curr = 1; for(int i=0; i<row_count; i++, curr++){ if(curr >= size) curr = 1; rows[curr]++; } timer_start(); int range[2]; range[0] = 0; for(int i=1; i<size; i++){ range[1] = rows[i]; // MPILOG_SHORT(); // printf("distribute: %d -> (%d, %d)\n", i, range[0], range[1]); MPI::COMM_WORLD.Send(&range, 2, MPI::INT, i, TAG); range[0] += range[1]; } MPILOG_SHORT(); printf("distribution time: %f\n", timer_end()); Matrix * res = new Matrix(); res->rows.push_back(0); res->n = m1->n; res->m = m2->m; res->copyMatcode(m1); res->rows.resize(m2_rows_size+1); row_result * results = new row_result[row_count]; timer_start(); // gather data for(int i=1; i<size; i++){ for(int k=0; k<rows[i]; k++){ int row; MPI::COMM_WORLD.Recv(&row, 1, MPI::INT, i, TAG, stat); row_result * result = &results[row]; MPI::COMM_WORLD.Recv(&result->len, 1, MPI::INT, i, TAG, stat); result->vals = new double[result->len]; result->cols = new int[result->len]; MPI::COMM_WORLD.Recv(result->vals, result->len, MPI::DOUBLE, i, TAG, stat); MPI::COMM_WORLD.Recv(result->cols, result->len, MPI::INT, i, TAG, stat); } MPILOG_SHORT(); printf("got all data from %d\n", i); } MPILOG_SHORT(); printf("gathering time: %f\n", timer_end()); // debug disply // for(int i=0; i<row_count; i++){ // printf("Results[%d]\n", i); // printf("vals = [ "); // for(int j=0; j<results[i].len; j++){ // printf("%f, ", results[i].vals[j]); // } // printf(" ]\n"); // // printf("cols = [ "); // for(int j=0; j<results[i].len; j++){ // printf("%d, ", results[i].cols[j]); // } // printf(" ]\n\n"); // } timer_start(); // accumulate data form nodes int acc = 0; for(unsigned int i=0; i<m1_rows_size; i++){ int len = results[i].len; double * vals = results[i].vals; int * cols = results[i].cols; for(int j=0; j<len; j++){ res->vals.push_back(vals[j]); res->cols.push_back(cols[j]); } res->rows[i+1] = acc + len; acc += len; } MPILOG_SHORT(); printf("data accumulation time: %f\n", timer_end()); res->c = (unsigned int)res->vals.size(); res->saveToFile("matrices/_mpi_result.mtx"); } else { // MPILOG("performing transposition"); timer_start(); m2->transpose(); MPILOG_SHORT(); printf("transposition time: %f\n", timer_end()); // read messages from master until row_id == -1 int range[2]; MPI::COMM_WORLD.Recv(&range, 2, MPI::INT, MASTER, TAG, stat); // compute! int end = range[0] + range[1]; float compute_time; float send_time; MPILOG_SHORT(); printf("performing row computation (%d - %d)\n", range[0], end); timer_start(); for(int i=range[0]; i<end; i++){ // MPILOG_SHORT(); // printf("computing row %d\n", i); vector<double> local_vals; vector<int> local_cols; for(unsigned int j=0; j<m2_rows_size; j++){ double sum = 0; for(int k=m1->rows[i]; k<m1->rows[i+1]; k++){ for(int l=m2->rows[j]; l<m2->rows[j+1]; l++){ if(m1->cols[k] == m2->cols[l]){ sum += m1->vals[k] * m2->vals[l]; } } } if(sum != 0){ local_vals.push_back(sum); local_cols.push_back(j); } } // convert data int len = (int)local_vals.size(); double * plain_vals = &local_vals.front(); int * plain_cols = &local_cols.front(); // i, len, vals, cols MPI::COMM_WORLD.Send(&i, 1, MPI::INT, MASTER, TAG); MPI::COMM_WORLD.Send(&len, 1, MPI::INT, MASTER, TAG); MPI::COMM_WORLD.Send(plain_vals, len, MPI::DOUBLE, MASTER, TAG); MPI::COMM_WORLD.Send(plain_cols, len, MPI::INT, MASTER, TAG); } MPILOG_SHORT(); printf("computation time: %f\n", timer_end()); } MPI::Finalize(); return 0; }
int main(int argc, char *argv[]) { double *old, *current, *next; int t_max, i_max, num_threads; double time; /* Parse commandline args: i_max t_max num_threads */ if (argc < 4) { printf("Usage: %s i_max t_max num_threads [initial_data]\n", argv[0]); printf(" - i_max: number of discrete amplitude points, should be >2\n"); printf(" - t_max: number of discrete timesteps, should be >=1\n"); printf(" - num_threads: number of threads to use for simulation, " "should be >=1\n"); printf(" - initial_data: select what data should be used for the first " "two generation.\n"); printf(" Available options are:\n"); printf(" * sin: one period of the sinus function at the start.\n"); printf(" * sinfull: entire data is filled with the sinus.\n"); printf(" * gauss: a single gauss-function at the start.\n"); printf(" * file <2 filenames>: allows you to specify a file with on " "each line a float for both generations.\n"); return EXIT_FAILURE; } i_max = atoi(argv[1]); t_max = atoi(argv[2]); num_threads = atoi(argv[3]); if (i_max < 3) { printf("argument error: i_max should be >2.\n"); return EXIT_FAILURE; } if (t_max < 1) { printf("argument error: t_max should be >=1.\n"); return EXIT_FAILURE; } if (num_threads < 1) { printf("argument error: num_threads should be >=1.\n"); return EXIT_FAILURE; } /* Allocate and initialize buffers. */ old = malloc(i_max * sizeof(double)); current = malloc(i_max * sizeof(double)); next = malloc(i_max * sizeof(double)); if (old == NULL || current == NULL || next == NULL) { fprintf(stderr, "Could not allocate enough memory, aborting.\n"); return EXIT_FAILURE; } memset(old, 0, i_max * sizeof(double)); memset(current, 0, i_max * sizeof(double)); memset(next, 0, i_max * sizeof(double)); /* How should we will our first two generations? */ if (argc > 4) { if (strcmp(argv[4], "sin") == 0) { fill(old, 1, i_max/4, 0, 2*3.14, sin); fill(current, 2, i_max/4, 0, 2*3.14, sin); } else if (strcmp(argv[4], "sinfull") == 0) { fill(old, 1, i_max-2, 0, 10*3.14, sin); fill(current, 2, i_max-3, 0, 10*3.14, sin); } else if (strcmp(argv[4], "gauss") == 0) { fill(old, 1, i_max/4, -3, 3, gauss); fill(current, 2, i_max/4, -3, 3, gauss); } else if (strcmp(argv[4], "file") == 0) { if (argc < 7) { printf("No files specified!\n"); return EXIT_FAILURE; } file_read_double_array(argv[5], old, i_max); file_read_double_array(argv[6], current, i_max); } else { printf("Unknown initial mode: %s.\n", argv[4]); return EXIT_FAILURE; } } else { /* Default to sinus. */ fill(old, 1, i_max/4, 0, 2*3.14, sin); fill(current, 2, i_max/4, 0, 2*3.14, sin); } timer_start(); /* Call the actual simulation that should be implemented in simulate.c. */ simulate(i_max, t_max, num_threads, old, current, next); time = timer_end(); printf("Took %g seconds\n", time); printf("Normalized: %g seconds\n", time / (i_max * t_max)); file_write_double_array("result.txt", current, i_max); free(old); free(current); free(next); return EXIT_SUCCESS; }
static int batch_deliver(struct cuda_rpc *rpc, struct cuda_packet *return_pkt) { int exit_errno; struct cuda_pkt_batch *batch = &rpc->batch; size_t payload_len = 0UL; struct flush *f; struct timer t; printd(DBG_INFO, "pkts = %lu size = %lu\n", batch->header.num_pkts, batch->header.bytes_used); timer_init(CLOCK_REALTIME, &t); f = &flushes[num_flushes]; clock_gettime(CLOCK_REALTIME, &f->ts); f->bytes = sizeof(batch->header); f->blocking = false; FAIL_ON_CONN_ERR( conn_put(&rpc->sockconn, &batch->header, sizeof(batch->header)) ); #if defined(NIC_SDP) f = &flushes[++num_flushes]; clock_gettime(CLOCK_REALTIME, &f->ts); f->bytes = batch->header.bytes_used + ZCPY_TRIGGER_SZ; timer_start(&t); // ignored if batch is non-blocking FAIL_ON_CONN_ERR( conn_put(&rpc->sockconn, batch->buffer, batch->header.bytes_used + ZCPY_TRIGGER_SZ) ); #else FAIL_ON_CONN_ERR( conn_put(&rpc->sockconn, batch->buffer, batch->header.bytes_used) ); #endif #ifndef NO_PIPELINING if (last_pkt(rpc)->is_sync) { /* only expect a return packet if last is sync */ #endif #if defined(NIC_SDP) f->blocking = true; f->bytes += sizeof(*return_pkt) + ZCPY_TRIGGER_SZ; FAIL_ON_CONN_ERR( conn_get(&rpc->sockconn, return_pkt, sizeof(*return_pkt) + ZCPY_TRIGGER_SZ) ); #else FAIL_ON_CONN_ERR( conn_get(&rpc->sockconn, return_pkt, sizeof(*return_pkt)) ); #endif payload_len = return_pkt->len - sizeof(*return_pkt); if (payload_len > 0) { #if defined(NIC_SDP) f->bytes += payload_len + ZCPY_TRIGGER_SZ; f->has_ret_payload = true; FAIL_ON_CONN_ERR( conn_get(&rpc->sockconn, (return_pkt + 1), payload_len + ZCPY_TRIGGER_SZ) ); #else FAIL_ON_CONN_ERR( conn_get(&rpc->sockconn, (return_pkt + 1), payload_len) ); #endif } #ifndef NO_PIPELINING f->lat = timer_end(&t, MICROSECONDS); f->exec = return_pkt->execlat; } #endif ++num_flushes; batch_clear(batch); return 0; fail: return exit_errno; }
void run(context_t& ctx, bool directed) { bool is_master = ctx.dc.procid() == 0; timer_start(is_master); #ifdef GRANULA granula::operation powergraphJob("PowerGraph", "Id.Unique", "Job", "Id.Unique"); granula::operation loadGraph("PowerGraph", "Id.Unique", "LoadGraph", "Id.Unique"); if(is_master) { cout<<powergraphJob.getOperationInfo("StartTime", powergraphJob.getEpoch())<<endl; cout<<loadGraph.getOperationInfo("StartTime", loadGraph.getEpoch())<<endl; } #endif // process parmaeters global_directed = directed; // load graph timer_next("load graph"); graph_type graph(ctx.dc); load_graph(graph, ctx); graph.finalize(); #ifdef GRANULA if(is_master) { cout<<loadGraph.getOperationInfo("EndTime", loadGraph.getEpoch())<<endl; } #endif // start engine timer_next("initialize engine"); graphlab::omni_engine<triangle_count> engine(ctx.dc, graph, "synchronous", ctx.clopts); engine.signal_all(); #ifdef GRANULA granula::operation processGraph("PowerGraph", "Id.Unique", "ProcessGraph", "Id.Unique"); if(is_master) { cout<<processGraph.getOperationInfo("StartTime", processGraph.getEpoch())<<endl; } #endif // run algorithm timer_next("run algorithm"); engine.start(); #ifdef GRANULA if(is_master) { cout<<processGraph.getOperationInfo("EndTime", processGraph.getEpoch())<<endl; } #endif #ifdef GRANULA granula::operation offloadGraph("PowerGraph", "Id.Unique", "OffloadGraph", "Id.Unique"); if(is_master) { cout<<offloadGraph.getOperationInfo("StartTime", offloadGraph.getEpoch())<<endl; } #endif // print output if (ctx.output_enabled) { timer_next("print output"); vector<pair<graphlab::vertex_id_type, vertex_data_type> > data; collect_vertex_data(graph, data, is_master); for (size_t i = 0; i < data.size(); i++) { (*ctx.output_stream) << data[i].first << " " << data[i].second.clustering_coef << endl; } } timer_end(); #ifdef GRANULA if(is_master) { cout<<offloadGraph.getOperationInfo("EndTime", offloadGraph.getEpoch())<<endl; cout<<powergraphJob.getOperationInfo("EndTime", powergraphJob.getEpoch())<<endl; } #endif }
int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "usage: %s [type] [input]\n", appname); fprintf(stderr, "type: specifies the dictionary type:\n"); fprintf(stderr, " h: height-balanced tree\n"); fprintf(stderr, " p: path-reduction tree\n"); fprintf(stderr, " r: red-black tree\n"); fprintf(stderr, " t: treap\n"); fprintf(stderr, " s: splay tree\n"); fprintf(stderr, " w: weight-balanced tree\n"); fprintf(stderr, " S: skiplist\n"); fprintf(stderr, " H: hashtable\n"); fprintf(stderr, " 2: hashtable 2\n"); fprintf(stderr, "input: text file consisting of newline-separated keys\n"); exit(EXIT_FAILURE); } srand(0xdeadbeef); dict_malloc_func = xmalloc; const char type = argv[1][0]; const char *container_name = NULL; dict *dct = create_dictionary(type, &container_name); if (!dct) quit("can't create container"); ASSERT(dict_verify(dct)); const size_t malloced_save = malloced; FILE *fp = fopen(argv[2], "r"); if (fp == NULL) quit("cant open file '%s': %s", argv[2], strerror(errno)); unsigned nwords = 0; char buf[512]; while (fgets(buf, sizeof(buf), fp)) ++nwords; if (!nwords) quit("nothing read from file"); char **words = xmalloc(sizeof(*words) * nwords); rewind(fp); for (unsigned i = 0; i < nwords && fgets(buf, sizeof(buf), fp); i++) { strtok(buf, "\n"); words[i] = xstrdup(buf); } fclose(fp); malloced = malloced_save; size_t total_comp = 0, total_hash = 0, total_rotations = 0; struct rusage start, end; struct timeval total = { 0, 0 }; timer_start(&start); for (unsigned i = 0; i < nwords; i++) { bool inserted = false; void **datum_location = dict_insert(dct, words[i], &inserted); if (!inserted) quit("insert #%d failed for '%s'", i, words[i]); ASSERT(datum_location != NULL); ASSERT(*datum_location == NULL); *datum_location = words[i]; } timer_end(&start, &end, &total); printf(" %s container: %.02fkB\n", container_name, malloced_save * 1e-3); printf(" %s memory: %.02fkB\n", container_name, malloced * 1e-3); printf(" %s insert: %6.03f s (%9zu cmp, %9zu hash)\n", container_name, (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6, comp_count, hash_count); total_comp += comp_count; comp_count = 0; total_hash += hash_count; hash_count = 0; if (type != 'H' && type != '2' && type != 'S') { tree_base *tree = dict_private(dct); printf("insert rotations: %zu\n", tree->rotation_count); total_rotations += tree->rotation_count; tree->rotation_count = 0; } ASSERT(dict_verify(dct)); unsigned n = dict_count(dct); if (n != nwords) quit("bad count (%u - should be %u)!", n, nwords); dict_itor *itor = dict_itor_new(dct); timer_start(&start); n = 0; ASSERT(dict_itor_first(itor)); do { ASSERT(dict_itor_valid(itor)); ASSERT(dict_itor_key(itor) == *dict_itor_data(itor)); ++n; } while (dict_itor_next(itor)); timer_end(&start, &end, &total); printf(" %s fwd iterate: %6.03f s\n", container_name, (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6); if (n != nwords) warn("Fwd iteration returned %u items - should be %u", n, nwords); timer_start(&start); n = 0; ASSERT(dict_itor_last(itor)); do { ASSERT(dict_itor_valid(itor)); ASSERT(dict_itor_key(itor) == *dict_itor_data(itor)); ++n; } while (dict_itor_prev(itor)); timer_end(&start, &end, &total); printf(" %s rev iterate: %6.03f s\n", container_name, (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6); if (n != nwords) warn("Rev iteration returned %u items - should be %u", n, nwords); dict_itor_free(itor); /* shuffle(words, nwords); */ timer_start(&start); for (unsigned i = 0; i < nwords; i++) { char *p = dict_search(dct, words[i]); if (!p) quit("lookup failed for '%s'", buf); if (p != words[i]) quit("bad data for '%s', got '%s' instead", words[i], p); } timer_end(&start, &end, &total); printf(" %s good search: %6.03f s (%9zu cmp, %9zu hash)\n", container_name, (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6, comp_count, hash_count); total_comp += comp_count; comp_count = 0; total_hash += hash_count; hash_count = 0; if (type != 'H' && type != '2' && type != 'S') { tree_base *tree = dict_private(dct); printf("search rotations: %zu\n", tree->rotation_count); total_rotations += tree->rotation_count; tree->rotation_count = 0; } timer_start(&start); for (unsigned i = 0; i < nwords; i++) { int rv = rand() % strlen(words[i]); words[i][rv]++; dict_search(dct, words[i]); words[i][rv]--; } timer_end(&start, &end, &total); printf(" %s bad search: %6.03f s (%9zu cmp, %9zu hash)\n", container_name, (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6, comp_count, hash_count); total_comp += comp_count; comp_count = 0; total_hash += hash_count; hash_count = 0; /* shuffle(words, nwords); */ timer_start(&start); for (unsigned i = 0; i < nwords; i++) { if (!dict_remove(dct, words[i])) quit("removing #%d '%s' failed!\n", i, words[i]); } timer_end(&start, &end, &total); printf(" %s remove: %6.03f s (%9zu cmp, %9zu hash)\n", container_name, (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6, comp_count, hash_count); total_comp += comp_count; comp_count = 0; total_hash += hash_count; hash_count = 0; if (type != 'H' && type != '2' && type != 'S') { tree_base *tree = dict_private(dct); printf("remove rotations: %zu\n", tree->rotation_count); total_rotations += tree->rotation_count; tree->rotation_count = 0; } ASSERT(dict_verify(dct)); if ((n = dict_count(dct)) != 0) quit("error - count not zero (%u)!", n); dict_free(dct); printf(" %s total: %6.03f s (%9zu cmp, %9zu hash)\n", container_name, (total.tv_sec * 1000000 + total.tv_usec) * 1e-6, total_comp, total_hash); if (type != 'H' && type != '2' && type != 'S') { printf(" total rotations: %zu\n", total_rotations); } FREE(words); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int num_threads; int vec_size; int custom_reduce_method; double accu; double time; double *vec; if (argc < 4) { printf("Usage: %s num_threads vec_size [reduce method]\n", argv[0]); printf(" - num_threads: number of threads to use for simulation, " "should be >=1\n"); printf(" - vec_size: size of the vector to do reduction on 10^n" "should be >=10\n"); printf(" - [reduce_method]: custom | sequential."); return EXIT_FAILURE; } num_threads = atoi(argv[1]); vec_size = pow(10, atoi(argv[2])); if (num_threads < 1) { printf("argument error: num_threads should be >=1.\n"); return EXIT_FAILURE; } if (vec_size < 4) { printf("argument error: vec_size should be >=4.\n"); return EXIT_FAILURE; } if (strcmp(argv[3], "sequential") == 0) { custom_reduce_method = 0; } else { custom_reduce_method = 1; } vec = (double*)malloc(vec_size * sizeof(double)); /* Fill a vector with a sinus values */ #pragma omp parallel for for (int i = 0; i < vec_size; i++) { vec[i] = sin(i); } omp_set_num_threads(num_threads); /* Start timing the effeciency of openMP */ timer_start(); /* Calculate a reduced vector with openMP */ if (custom_reduce_method) { accu = reduce2(fun, vec, vec_size, 0); /* accu = sum(vec, vec_size); */ } else { accu = reduce(fun, vec, vec_size, 0); } /* Stop timing */ time = timer_end(); printf("%d, %d, %g\n",num_threads, vec_size, time); free(vec); vec = NULL; return EXIT_SUCCESS; }
int main(int argc, char **argv) { bool shuffle_keys = true; if (argc != 3) { fprintf(stderr, "usage: %s [type] [input]\n", appname); fprintf(stderr, "type: specifies the dictionary type:\n"); fprintf(stderr, " h: height-balanced tree\n"); fprintf(stderr, " p: path-reduction tree\n"); fprintf(stderr, " r: red-black tree\n"); fprintf(stderr, " t: treap\n"); fprintf(stderr, " s: splay tree\n"); fprintf(stderr, " w: weight-balanced tree\n"); fprintf(stderr, " S: skiplist\n"); fprintf(stderr, " H: hashtable\n"); fprintf(stderr, " 2: hashtable 2\n"); fprintf(stderr, "input: text file consisting of newline-separated keys\n"); exit(EXIT_FAILURE); } srand(0xdeadbeef); dict_malloc_func = xmalloc; const char type = argv[1][0]; const char *container_name = NULL; dict *dct = create_dictionary(type, &container_name); if (!dct) quit("can't create container"); ASSERT(dict_verify(dct)); ASSERT(comp_count == 0); ASSERT(hash_count == 0); const size_t malloced_save = malloced; FILE *fp = fopen(argv[2], "r"); if (fp == NULL) quit("cant open file '%s': %s", argv[2], strerror(errno)); size_t nwords = 0; char buf[512]; while (fgets(buf, sizeof(buf), fp)) ++nwords; if (!nwords) quit("nothing read from file"); char **words = xmalloc(sizeof(*words) * nwords); rewind(fp); size_t words_read = 0; while (words_read < nwords && fgets(buf, sizeof(buf), fp)) { strtok(buf, "\n"); words[words_read++] = xstrdup(buf); } fclose(fp); if (words_read < nwords) quit("Only read %zu/%zu words!", words_read, nwords); printf("Loaded %zu keys from %s.\n", nwords, argv[2]); malloced = malloced_save; size_t total_comp = 0, total_hash = 0, total_rotations = 0; struct rusage start, end; struct timeval total = { 0, 0 }; timer_start(&start); for (unsigned i = 0; i < nwords; i++) { dict_insert_result result = dict_insert(dct, words[i]); if (!result.inserted) quit("insert #%d failed for '%s'", i, words[i]); ASSERT(result.datum_ptr != NULL); ASSERT(*result.datum_ptr == NULL); *result.datum_ptr = words[i]; } timer_end(&start, &end, &total); printf(" %s container: %.02fkB\n", container_name, malloced_save * 1e-3); printf(" %s memory: %.02fkB\n", container_name, malloced * 1e-3); printf(" %s insert: %6.03fs %9zu cmp (%.02f/insert)", container_name, (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6, comp_count, comp_count / (double) nwords); if (hash_count) printf(" %9zu hash", hash_count); printf("\n"); total_comp += comp_count; comp_count = 0; total_hash += hash_count; hash_count = 0; if (dict_is_sorted(dct) && type != 'S') { tree_base *tree = dict_private(dct); printf(" min path length: %zu\n", tree_min_path_length(tree)); printf(" max path length: %zu\n", tree_max_path_length(tree)); printf(" tot path length: %zu\n", tree_total_path_length(tree)); printf("insert rotations: %zu\n", tree->rotation_count); total_rotations += tree->rotation_count; tree->rotation_count = 0; } else if (type == 'S') { size_t counts[16] = { 0 }; size_t num_counts = skiplist_link_count_histogram(dict_private(dct), counts, sizeof(counts) / sizeof(counts[0])); size_t count_sum = 0; for (size_t i = 0; i <= num_counts; ++i) { printf("skiplist %zu-node(s): %zu\n", i, counts[i]); count_sum += counts[i]; } ASSERT(count_sum == nwords); } ASSERT(dict_verify(dct)); comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */ size_t n = dict_count(dct); if (n != nwords) quit("bad count (%u - should be %u)!", n, nwords); dict_itor *itor = dict_itor_new(dct); timer_start(&start); n = 0; ASSERT(dict_itor_first(itor)); do { ASSERT(dict_itor_valid(itor)); ASSERT(dict_itor_key(itor) == *dict_itor_datum(itor)); ++n; } while (dict_itor_next(itor)); timer_end(&start, &end, &total); printf(" %s fwd iterate: %6.03fs\n", container_name, (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6); if (n != nwords) warn("Fwd iteration returned %u items - should be %u", n, nwords); ASSERT(dict_verify(dct)); comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */ timer_start(&start); n = 0; ASSERT(dict_itor_last(itor)); do { ASSERT(dict_itor_valid(itor)); ASSERT(dict_itor_key(itor) == *dict_itor_datum(itor)); ++n; } while (dict_itor_prev(itor)); timer_end(&start, &end, &total); printf(" %s rev iterate: %6.03fs\n", container_name, (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6); if (n != nwords) warn("Rev iteration returned %u items - should be %u", n, nwords); dict_itor_free(itor); if (shuffle_keys) shuffle(words, nwords); ASSERT(dict_verify(dct)); comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */ timer_start(&start); for (unsigned i = 0; i < nwords; i++) { void **p = dict_search(dct, words[i]); if (!p) quit("lookup failed for '%s'", buf); if (*p != words[i]) quit("bad data for '%s', got '%s' instead", words[i], *(char **)p); } timer_end(&start, &end, &total); printf(" %s good search: %6.03fs %9zu cmp (%.02f/search)", container_name, (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6, comp_count, comp_count / (double) nwords); if (hash_count) printf(" %9zu hash", hash_count); printf("\n"); total_comp += comp_count; comp_count = 0; total_hash += hash_count; hash_count = 0; if (type != 'H' && type != '2' && type != 'S') { tree_base *tree = dict_private(dct); printf("search rotations: %zu\n", tree->rotation_count); total_rotations += tree->rotation_count; tree->rotation_count = 0; } ASSERT(dict_verify(dct)); comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */ timer_start(&start); for (unsigned i = 0; i < nwords; i++) { unsigned rv = dict_rand() % strlen(words[i]); words[i][rv]++; dict_search(dct, words[i]); words[i][rv]--; } timer_end(&start, &end, &total); printf(" %s bad search: %6.03fs %9zu cmp (%.02f/search)", container_name, (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6, comp_count, comp_count / (double) nwords); if (hash_count) printf(" %9zu hash", hash_count); printf("\n"); total_comp += comp_count; comp_count = 0; total_hash += hash_count; hash_count = 0; ASSERT(dict_verify(dct)); comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */ if (shuffle_keys) shuffle(words, nwords); timer_start(&start); for (unsigned i = 0; i < nwords; i++) { dict_remove_result result = dict_remove(dct, words[i]); if (!result.removed) quit("removing #%d '%s' failed!\n", i, words[i]); ASSERT(result.key == words[i]); ASSERT(result.datum == words[i]); } timer_end(&start, &end, &total); printf(" %s remove: %6.03fs %9zu cmp (%.2f/remove)", container_name, (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6, comp_count, comp_count / (double)nwords); if (hash_count) printf(" %9zu hash", hash_count); printf("\n"); total_comp += comp_count; comp_count = 0; total_hash += hash_count; hash_count = 0; if (type != 'H' && type != '2' && type != 'S') { tree_base *tree = dict_private(dct); printf("remove rotations: %zu\n", tree->rotation_count); total_rotations += tree->rotation_count; tree->rotation_count = 0; } ASSERT(dict_verify(dct)); comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */ if ((n = dict_count(dct)) != 0) quit("error - count not zero (%u)!", n); dict_free(dct, key_str_free); printf(" %s total: %6.03fs %9zu cmp", container_name, (total.tv_sec * 1000000 + total.tv_usec) * 1e-6, total_comp); if (total_hash) printf(" %9zu hash", total_hash); printf("\n"); if (type != 'H' && type != '2' && type != 'S') { printf(" total rotations: %zu\n", total_rotations); } FREE(words); exit(EXIT_SUCCESS); }