void timer_end(const char *label) { timer_stop(); fprintf(stderr, "%20s: ", label ? label : ""); timer_print(); timer_reset(); }
int test_timersleep() { timer_init(); TimerData timer; timer_start(&timer); timer_sleep(1E4); timer_stop(&timer); if (timer_print(&timer) < 0.01) goto fail; if (timer_print(&timer) > 0.015) goto fail; timer_finalize(); return 1; fail: timer_finalize(); return 0; }
int test_timerprint_noinit() { TimerData timer; timer_reset(&timer); double time = timer_print(&timer); if (time != 0) goto fail; return 1; fail: return 0; }
static void timer_callback(int fd, enum ev_type type, void *param) { static int i; if (i >= TEVSZ) abort(); tevbuf[i++] = rdtsc(); if (i == TEVSZ) { mevent_delete(tevp); timer_print(); } }
int main(int argc, char **argv) { int opt, exp = -1, var = -1; bool err = false; while ((opt = getopt(argc, argv, "n:v:")) != -1) { if (opt == 'n') exp = atoi(optarg); else if (opt == 'v') var = atoi(optarg); else err = true; } if (err || exp < 0 || var < 0 || var >= 2) { fprintf(stderr, "Usage: %s -n log2(size) -v variant\n", argv[0]); return 1; } int n = 1 << exp; size_t size = n * n * sizeof(int); int *src = NULL, *dst = NULL; posix_memalign((void **)&src, getpagesize(), size); posix_memalign((void **)&dst, getpagesize(), size); printf("Generate matrix %d x %d (%ld KiB)\n", n, n, size >> 10); fill(src, n); bzero(dst, size); flush_cache(); printf("Performing matrix transposition.\n"); _timer_t timer; timer_reset(&timer); timer_start(&timer); if (var == 0) transpose1(dst, src, n); else transpose2(dst, src, n); timer_stop(&timer); timer_print(&timer); free(src); free(dst); return 0; }
int test_timerprint() { TimerData timer; timer_reset(&timer); timer_init(); double time = timer_print(&timer); if (time != 0) goto fail; uint64_t cycles = timer_printCycles(&timer); if (cycles != 0) goto fail; timer_finalize(); return 1; fail: timer_finalize(); return 0; }
static int lua_likwid_getClock(lua_State* L) { TimerData timer; double runtime, start, stop; if (timer_isInitialized == 0) { timer_init(); timer_isInitialized = 1; } start = lua_tonumber(L,1); stop = lua_tonumber(L,2); timer.start.int64 = (uint64_t)start; timer.stop.int64 = (uint64_t)stop; runtime = timer_print(&timer); lua_pushnumber(L, runtime); return 1; }
void daemon_start(struct timespec interval) { daemon_run = 1; perfmon_startCounters(); timer_start(&timeData); while (1) { if (daemon_run) { timer_stop(&timeData); perfmon_readCounters(); perfmon_logCounterResults( timer_print(&timeData) ); timer_start(&timeData); } nanosleep( &interval, NULL); } }
int test_timerprint_stop() { TimerData timer; timer_init(); timer_reset(&timer); timer_start(&timer); timer_stop(&timer); double time = timer_print(&timer); if (time > 1) goto fail; if (time == 0) goto fail; uint64_t cycles = timer_printCycles(&timer); if (cycles == 0) goto fail; if (cycles > timer_getCpuClock()) goto fail; timer_finalize(); return 1; fail: timer_finalize(); return 0; }
void malloc_test (void) { struct timespec start_time; struct timespec end_time; void **buffer = (void*) calloc(REPETITIONS, sizeof(void*)); timer_start(&start_time); for(size_t i = 0; i < REPETITIONS; i++) { buffer[i] = malloc(BUFFER_SIZE); } timer_stop(&end_time); timer_print("malloc", &start_time, &end_time); for(size_t i = 0; i < REPETITIONS; i++) { free(buffer[i]); } free(buffer); }
int main(int argc, char *argv[]) { exit_if_false(argc == 2,"exactly one input argument required"); timer_start(); //-------------------------------------------------------------------// // counters int i, n; // file paths char *input_file_path, *geometry_file_path, *case_file_path, *data_file_path, *data_numbered_file_path, *display_file_path, *display_numbered_file_path; exit_if_false(geometry_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating geometry file path"); exit_if_false(case_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating case file path"); exit_if_false(data_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating data file path"); exit_if_false(data_numbered_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating data numbered file path"); exit_if_false(display_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating display file path"); exit_if_false(display_numbered_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating display numbered file path"); // print string char *print; exit_if_false(print = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating the print string"); // mesh structures int n_nodes, n_faces, n_elements, n_boundaries_old = 0, n_boundaries, n_terms; struct NODE *node; struct FACE *face; struct ELEMENT *element; struct BOUNDARY *boundary_old = NULL, *boundary; struct TERM *term; EXPRESSION *initial = NULL; // solution vectors int n_u_old = 0, n_u; double *u_old = NULL, *u; // files FILE *input_file, *case_file, *data_file, *geometry_file, *display_file; //-------------------------------------------------------------------// // opening the input file print_info("opening the input file %s",argv[1]); input_file_path = argv[1]; input_file = fopen(input_file_path,"r"); exit_if_false(input_file != NULL,"opening %s",input_file_path); // reading the case file path print_info("reading the case file path"); exit_if_false(fetch_value(input_file,"case_file_path",'s',case_file_path) == FETCH_SUCCESS,"reading case_file_path from %s",input_file_path); print_continue(case_file_path); // reading the number of variables, variable names and orders print_info("reading the variables"); int n_variables_old = 0, n_variables; exit_if_false(fetch_value(input_file,"number_of_variables",'i',&n_variables) == FETCH_SUCCESS,"reading number_of_variables from %s",input_file_path); int *variable_order_old = NULL, *variable_order = (int *)malloc(n_variables * sizeof(int)); exit_if_false(variable_order != NULL,"allocating orders"); exit_if_false(fetch_vector(input_file, "variable_order", 'i', n_variables, variable_order) == FETCH_SUCCESS,"reading variable_order from %s",input_file_path); char **variable_name = allocate_character_matrix(NULL,n_variables,MAX_STRING_LENGTH); exit_if_false(variable_name != NULL,"allocating variable names"); warn_if_false(fetch_vector(input_file,"variable_name",'s',n_variables,variable_name) == FETCH_SUCCESS,"reading variable_name from %s",input_file_path); for(i = 0; i < n_variables; i ++) print_continue("%s order %i",variable_name[i],variable_order[i]); // reading the number of inner and outer iterations to perform print_info("reading the numbers of iterations"); int outer_iteration = 0, inner_iteration; int n_outer_iterations, n_inner_iterations, data_n_outer_iterations, display_n_outer_iterations; exit_if_false(fetch_value(input_file,"number_of_inner_iterations",'i',&n_inner_iterations) == FETCH_SUCCESS,"reading number_of_inner_iterations from %s",input_file_path); exit_if_false(fetch_value(input_file,"number_of_outer_iterations",'i',&n_outer_iterations) == FETCH_SUCCESS,"reading number_of_outer_iterations from %s",input_file_path); print_continue("%i outer of %i inner iterations to be done",n_outer_iterations,n_inner_iterations); // read existing case and data case_file = fopen(case_file_path,"r"); if(case_file != NULL) { print_info("reading existing case file %s",case_file_path); read_case(case_file, &n_variables_old, &variable_order_old, &n_nodes, &node, &n_faces, &face, &n_elements, &element, &n_boundaries_old, &boundary_old); fclose(case_file); n = 0; for(i = 0; i < n_variables; i ++) n += n_elements*ORDER_TO_N_BASIS(variable_order[i]); if(fetch_value(input_file,"initial_data_file_path",'s',data_file_path) == FETCH_SUCCESS) { print_info("reading existing data file %s",data_file_path); exit_if_false(data_file = fopen(data_file_path,"r"),"opening %s",data_file_path); read_data(data_file, &n_u_old, &u_old, &outer_iteration); fclose(data_file); exit_if_false(n_u_old == n,"case and initial data does not match"); } } // construct new case else { print_info("reading the geometry file path"); exit_if_false(fetch_value(input_file,"geometry_file_path",'s',geometry_file_path) == FETCH_SUCCESS,"reading geometry_file_path from %s",input_file_path); print_continue(geometry_file_path); print_info("reading the geometry file %s",geometry_file_path); exit_if_false(geometry_file = fopen(geometry_file_path,"r"),"opening %s",geometry_file_path); read_geometry(geometry_file, &n_nodes, &node, &n_faces, &face, &n_elements, &element); fclose(geometry_file); print_continue("%i nodes, %i faces and %i elements",n_nodes,n_faces,n_elements); print_info("generating additional connectivity and geometry"); process_geometry(n_nodes, node, n_faces, face, n_elements, element); } // read the data file path and output frequency print_info("reading the data file path and output frequency"); exit_if_false(fetch_value(input_file,"data_file_path",'s',data_file_path) == FETCH_SUCCESS,"reading data_file_path from %s",input_file_path); if(fetch_value(input_file,"data_number_of_outer_iterations",'i',&data_n_outer_iterations) != FETCH_SUCCESS) data_n_outer_iterations = n_outer_iterations + outer_iteration; print_continue("data to be written to %s every %i outer iterations",data_file_path,data_n_outer_iterations); // read boundaries print_info("reading boundaries"); boundaries_input(input_file, n_faces, face, &n_boundaries, &boundary); print_continue("%i boundaries",n_boundaries); // read terms print_info("reading PDE terms"); terms_input(input_file, &n_terms, &term); print_continue("%i terms",n_terms); // update unknown indices and values print_info("updating the numbering of the degrees of freedom"); update_element_unknowns(n_variables_old, n_variables, variable_order_old, variable_order, n_elements, element, n_u_old, &n_u, u_old, &u); print_continue("%i degrees of freedom",n_u); // update face boundaries print_info("updating the face boundary associations"); update_face_boundaries(n_variables, n_faces, face, n_boundaries, boundary); // update integration print_info("updating integration"); i = update_face_integration(n_variables_old, n_variables, variable_order_old, variable_order, n_faces, face); if(i) print_continue("updated %i face quadratures",i); i = update_element_integration(n_variables_old, n_variables, variable_order_old, variable_order, n_elements, element); if(i) print_continue("updated %i element quadratures",i); // update numerics print_info("updating numerics"); i = update_face_numerics(n_variables_old, n_variables, variable_order_old, variable_order, n_faces, face, n_boundaries_old, boundary_old); if(i) print_continue("updated %i face interpolations",i); i = update_element_numerics(n_variables_old, n_variables, variable_order_old, variable_order, n_elements, element); if(i) print_continue("updated %i element interpolations",i); // write case file print_info("writing case file %s",case_file_path); exit_if_false(case_file = fopen(case_file_path,"w"),"opening %s",case_file_path); write_case(case_file, n_variables, variable_order, n_nodes, node, n_faces, face, n_elements, element, n_boundaries, boundary); fclose(case_file); // read the display file path and output frequency print_info("reading the display file path and output frequency"); if( fetch_value(input_file,"display_file_path",'s',display_file_path) == FETCH_SUCCESS && fetch_value(input_file,"display_number_of_outer_iterations",'i',&display_n_outer_iterations) == FETCH_SUCCESS ) print_continue("display to be written to %s every %i outer iterations",display_file_path,display_n_outer_iterations); else { display_n_outer_iterations = 0; warn_if_false(0,"display files will not be written"); } // initialise if(initial_input(input_file, n_variables, &initial)) { print_info("initialising the degrees of freedom"); initialise_values(n_variables, variable_order, n_elements, element, initial, u); } //-------------------------------------------------------------------// // allocate and initialise the system print_info("allocating and initialising the system"); SPARSE system = NULL; initialise_system(n_variables, variable_order, n_elements, element, n_u, &system); double *residual, *max_residual, *du, *max_du; exit_if_false(residual = (double *)malloc(n_u * sizeof(double)),"allocating the residuals"); exit_if_false(max_residual = (double *)malloc(n_variables * sizeof(double)),"allocating the maximum residuals"); exit_if_false(du = (double *)malloc(n_u * sizeof(double)),"allocating du"); exit_if_false(max_du = (double *)malloc(n_variables * sizeof(double)),"allocating the maximum changes"); exit_if_false(u_old = (double *)realloc(u_old, n_u * sizeof(double)),"re-allocating u_old"); timer_reset(); // iterate print_info("iterating"); n_outer_iterations += outer_iteration; for(; outer_iteration < n_outer_iterations; outer_iteration ++) { print_output("iteration %i", outer_iteration); for(i = 0; i < n_u; i ++) u_old[i] = u[i]; for(inner_iteration = 0; inner_iteration < n_inner_iterations; inner_iteration ++) { calculate_system(n_variables, variable_order, n_faces, face, n_elements, element, n_terms, term, n_u, u_old, u, system, residual); exit_if_false(sparse_solve(system, du, residual) == SPARSE_SUCCESS,"solving system"); for(i = 0; i < n_u; i ++) u[i] -= du[i]; calculate_maximum_changes_and_residuals(n_variables, variable_order, n_elements, element, du, max_du, residual, max_residual); for(i = 0; i < n_variables; i ++) sprintf(&print[26*i],"%.6e|%.6e ",max_du[i],max_residual[i]); print_continue("%s",print); } slope_limit(n_variables, variable_order, n_nodes, node, n_elements, element, n_boundaries, boundary, u); if(data_n_outer_iterations && outer_iteration % data_n_outer_iterations == 0) { generate_numbered_file_path(data_numbered_file_path, data_file_path, outer_iteration); print_info("writing data to %s",data_numbered_file_path); exit_if_false(data_file = fopen(data_numbered_file_path,"w"),"opening %s",data_numbered_file_path); write_data(data_file, n_u, u, outer_iteration); fclose(data_file); } if(display_n_outer_iterations && outer_iteration % display_n_outer_iterations == 0) { generate_numbered_file_path(display_numbered_file_path, display_file_path, outer_iteration); print_info("writing display to %s",data_numbered_file_path); exit_if_false(display_file = fopen(display_numbered_file_path,"w"),"opening %s",display_numbered_file_path); write_display(display_file, n_variables, variable_name, variable_order, n_elements, element, n_u, u); fclose(display_file); } timer_print(); } //-------------------------------------------------------------------// print_info("freeing all memory"); fclose(input_file); free(geometry_file_path); free(case_file_path); free(data_file_path); free(data_numbered_file_path); free(display_file_path); free(display_numbered_file_path); free(print); destroy_nodes(n_nodes,node); destroy_faces(n_faces,face,n_variables); destroy_elements(n_elements,element,n_variables); destroy_boundaries(n_boundaries_old,boundary_old); destroy_boundaries(n_boundaries,boundary); destroy_terms(n_terms,term); destroy_initial(n_variables,initial); free(variable_order_old); free(variable_order); destroy_matrix((void *)variable_name); free(u_old); free(u); sparse_destroy(system); free(residual); free(max_residual); free(du); free(max_du); return 0; }
int main (int argc, char** argv) { int socket_fd = -1; int optInfo = 0; int optClock = 0; int optStethoscope = 0; int optSockets = 0; double runtime; int hasDRAM = 0; int c; bstring argString; bstring eventString = bfromcstr("CLOCK"); int numSockets=1; int numThreads=0; int threadsSockets[MAX_NUM_NODES*2]; int threads[MAX_NUM_THREADS]; threadsSockets[0] = 0; if (argc == 1) { HELP_MSG; exit (EXIT_SUCCESS); } while ((c = getopt (argc, argv, "+c:hiM:ps:v")) != -1) { switch (c) { case 'c': CHECK_OPTION_STRING; numSockets = bstr_to_cpuset_physical((uint32_t*) threadsSockets, argString); bdestroy(argString); optSockets = 1; break; case 'h': HELP_MSG; exit (EXIT_SUCCESS); case 'i': optInfo = 1; break; case 'M': /* Set MSR Access mode */ CHECK_OPTION_STRING; accessClient_setaccessmode(str2int((char*) argString->data)); bdestroy(argString); break; case 'p': optClock = 1; break; case 's': CHECK_OPTION_STRING; optStethoscope = str2int((char*) argString->data); bdestroy(argString); break; case 'v': VERSION_MSG; exit (EXIT_SUCCESS); case '?': if (optopt == 's' || optopt == 'M' || optopt == 'c') { HELP_MSG; } else if (isprint (optopt)) { fprintf (stderr, "Unknown option `-%c'.\n", optopt); } else { fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt); } exit( EXIT_FAILURE); default: HELP_MSG; exit (EXIT_SUCCESS); } } if (!lock_check()) { fprintf(stderr,"Access to performance counters is locked.\n"); exit(EXIT_FAILURE); } if (optClock && optind == argc) { fprintf(stderr,"Commandline option -p requires an executable.\n"); exit(EXIT_FAILURE); } if (optSockets && !optStethoscope && optind == argc) { fprintf(stderr,"Commandline option -c requires an executable if not used in combination with -s.\n"); exit(EXIT_FAILURE); } if (cpuid_init() == EXIT_FAILURE) { fprintf(stderr, "CPU not supported\n"); exit(EXIT_FAILURE); } if (numSockets > cpuid_topology.numSockets) { fprintf(stderr, "System has only %d sockets but %d are given on commandline\n", cpuid_topology.numSockets, numSockets); exit(EXIT_FAILURE); } numa_init(); /* consider NUMA node as power unit for the moment */ accessClient_init(&socket_fd); msr_init(socket_fd); timer_init(); /* check for supported processors */ if ((cpuid_info.model == SANDYBRIDGE_EP) || (cpuid_info.model == SANDYBRIDGE) || (cpuid_info.model == IVYBRIDGE) || (cpuid_info.model == IVYBRIDGE_EP) || (cpuid_info.model == HASWELL) || (cpuid_info.model == NEHALEM_BLOOMFIELD) || (cpuid_info.model == NEHALEM_LYNNFIELD) || (cpuid_info.model == NEHALEM_WESTMERE)) { power_init(numa_info.nodes[0].processors[0]); } else { fprintf (stderr, "Query Turbo Mode only supported on Intel Nehalem/Westmere/SandyBridge/IvyBridge/Haswell processors!\n"); exit(EXIT_FAILURE); } double clock = (double) timer_getCpuClock(); printf(HLINE); printf("CPU name:\t%s \n",cpuid_info.name); printf("CPU clock:\t%3.2f GHz \n", (float) clock * 1.E-09); printf(HLINE); if (optInfo) { if (power_info.turbo.numSteps != 0) { printf("Base clock:\t%.2f MHz \n", power_info.baseFrequency ); printf("Minimal clock:\t%.2f MHz \n", power_info.minFrequency ); printf("Turbo Boost Steps:\n"); for (int i=0; i < power_info.turbo.numSteps; i++ ) { printf("C%d %.2f MHz \n",i+1, power_info.turbo.steps[i] ); } } printf(HLINE); } if (cpuid_info.model == SANDYBRIDGE_EP) { hasDRAM = 1; } else if ((cpuid_info.model != SANDYBRIDGE) && (cpuid_info.model != SANDYBRIDGE_EP) && (cpuid_info.model != IVYBRIDGE) && (cpuid_info.model != IVYBRIDGE_EP) && (cpuid_info.model != HASWELL)) { fprintf (stderr, "RAPL not supported on this processor!\n"); exit(EXIT_FAILURE); } if (optInfo) { printf("Thermal Spec Power: %g Watts \n", power_info.tdp ); printf("Minimum Power: %g Watts \n", power_info.minPower); printf("Maximum Power: %g Watts \n", power_info.maxPower); printf("Maximum Time Window: %g micro sec \n", power_info.maxTimeWindow); printf(HLINE); exit(EXIT_SUCCESS); } if (optClock) { affinity_init(); argString = bformat("S%u:0-%u", threadsSockets[0], cpuid_topology.numCoresPerSocket-1); for (int i=1; i<numSockets; i++) { bstring tExpr = bformat("@S%u:0-%u", threadsSockets[i], cpuid_topology.numCoresPerSocket-1); bconcat(argString, tExpr); } numThreads = bstr_to_cpuset(threads, argString); bdestroy(argString); perfmon_init(numThreads, threads, stdout); perfmon_setupEventSet(eventString, NULL); } { PowerData pDataPkg[MAX_NUM_NODES*2]; PowerData pDataDram[MAX_NUM_NODES*2]; printf("Measure on sockets: %d", threadsSockets[0]); for (int i=1; i<numSockets; i++) { printf(", %d", threadsSockets[i]); } printf("\n"); if (optStethoscope) { if (optClock) { perfmon_startCounters(); } else { for (int i=0; i<numSockets; i++) { int cpuId = numa_info.nodes[threadsSockets[i]].processors[0]; if (hasDRAM) power_start(pDataDram+i, cpuId, DRAM); power_start(pDataPkg+i, cpuId, PKG); } } sleep(optStethoscope); if (optClock) { perfmon_stopCounters(); perfmon_printCounterResults(); perfmon_finalize(); } else { for (int i=0; i<numSockets; i++) { int cpuId = numa_info.nodes[threadsSockets[i]].processors[0]; power_stop(pDataPkg+i, cpuId, PKG); if (hasDRAM) power_stop(pDataDram+i, cpuId, DRAM); } } runtime = (double) optStethoscope; } else { TimerData time; argv += optind; bstring exeString = bfromcstr(argv[0]); for (int i=1; i<(argc-optind); i++) { bconchar(exeString, ' '); bcatcstr(exeString, argv[i]); } printf("%s\n",bdata(exeString)); if (optClock) { perfmon_startCounters(); } else { for (int i=0; i<numSockets; i++) { int cpuId = numa_info.nodes[threadsSockets[i]].processors[0]; if (hasDRAM) power_start(pDataDram+i, cpuId, DRAM); power_start(pDataPkg+i, cpuId, PKG); } timer_start(&time); } if (system(bdata(exeString)) == EOF) { fprintf(stderr, "Failed to execute %s!\n", bdata(exeString)); exit(EXIT_FAILURE); } if (optClock) { perfmon_stopCounters(); perfmon_printCounterResults(); perfmon_finalize(); } else { timer_stop(&time); for (int i=0; i<numSockets; i++) { int cpuId = numa_info.nodes[threadsSockets[i]].processors[0]; power_stop(pDataPkg+i, cpuId, PKG); if (hasDRAM) power_stop(pDataDram+i, cpuId, DRAM); } runtime = timer_print(&time); } } if (!optClock) { printf("Runtime: %g second \n",runtime); printf(HLINE); for (int i=0; i<numSockets; i++) { printf("Socket %d\n",threadsSockets[i]); printf("Domain: PKG \n"); printf("Energy consumed: %g Joules \n", power_printEnergy(pDataPkg+i)); printf("Power consumed: %g Watts \n", power_printEnergy(pDataPkg+i) / runtime ); if (hasDRAM) { printf("Domain: DRAM \n"); printf("Energy consumed: %g Joules \n", power_printEnergy(pDataDram+i)); printf("Power consumed: %g Watts \n", power_printEnergy(pDataDram+i) / runtime ); } printf("\n"); } } } #if 0 if ( cpuid_hasFeature(TM2) ) { thermal_init(0); printf("Current core temperatures:\n"); for (uint32_t i = 0; i < cpuid_topology.numCoresPerSocket; i++ ) { printf("Core %d: %u C\n", numa_info.nodes[socketId].processors[i], thermal_read(numa_info.nodes[socketId].processors[i])); } } #endif msr_finalize(); return EXIT_SUCCESS; }
/* Main program. */ int main(void) { int i, j, n_samples, max_n, step_n; int array_size; int radix; test_item_t a[N_ITEMS], test_array[N_ITEMS]; test_item_t *timing_array, *copy_array; timing_t *t; /* Assign random values to the key of each array element. */ rand_array(a, N_ITEMS, 100); /* Now test quicksort(). */ memcpy(test_array, a, sizeof(test_array)); printf("array before quicksort\n = "); print_array(test_array, N_ITEMS); printf("\n\n"); quicksort(test_array, N_ITEMS, sizeof(test_item_t), item_cmp); printf("array after quicksort\n = "); print_array(test_array, N_ITEMS); printf("\n\n"); /* Now test mergesort0(). */ memcpy(test_array, a, sizeof(test_array)); printf("array before mergesort0\n = "); print_array(test_array, N_ITEMS); printf("\n\n"); mergesort0(test_array, N_ITEMS, sizeof(test_item_t), item_cmp); printf("array after mergesort0\n = "); print_array(test_array, N_ITEMS); printf("\n\n"); /* Now test mergesort(). */ memcpy(test_array, a, sizeof(test_array)); printf("array before mergesort\n = "); print_array(test_array, N_ITEMS); printf("\n\n"); mergesort(test_array, N_ITEMS, sizeof(test_item_t), item_cmp); printf("array after mergesort\n = "); print_array(test_array, N_ITEMS); printf("\n\n"); /* Now test radix sort. */ memcpy(test_array, a, sizeof(test_array)); printf("array before radixsort\n = "); print_array(test_array, N_ITEMS); printf("\n\n"); radixsort(test_array, N_ITEMS, sizeof(test_item_t), get_value, 10); printf("array after radixsort\n = "); print_array(test_array, N_ITEMS); printf("\n\n"); /* Now test heapsort. */ memcpy(test_array, a, sizeof(test_array)); printf("array before heapsort\n = "); print_array(test_array, N_ITEMS); printf("\n\n"); heapsort(test_array, N_ITEMS, sizeof(test_item_t), item_cmp); printf("array after heapsort\n = "); print_array(test_array, N_ITEMS); printf("\n\n"); /* Time the quicksort and mergesort sorting functions. */ printf("Enter the number of samples to use: "); scanf("%d", &n_samples); printf("Enter the maximum array length to sort: "); scanf("%d", &max_n); printf("Enter the step size for array lengths: "); scanf("%d", &step_n); t = timing_alloc(5); /* Five different sorting algorithms. */ printf("\nResults (n, qsort, quicksort, mergesort, mergesort0, heapsort) (msec)\n" ); for(i = step_n; i <= max_n; i += step_n) { array_size = i * sizeof(test_item_t); timing_array = malloc(array_size); copy_array = malloc(array_size); rand_array(copy_array, i, MAX_VALUE); timing_reset(t); for(j = 0; j < n_samples; j++) { memcpy(timing_array, copy_array, array_size); timing_start(); qsort(timing_array, i, sizeof(test_item_t), item_cmp); timing_stop(t,0); memcpy(timing_array, copy_array, array_size); timing_start(); quicksort(timing_array, i, sizeof(test_item_t), item_cmp); timing_stop(t,1); memcpy(timing_array, copy_array, array_size); timing_start(); mergesort(timing_array, i, sizeof(test_item_t), item_cmp); timing_stop(t,2); memcpy(timing_array, copy_array, array_size); timing_start(); mergesort0(timing_array, i, sizeof(test_item_t), item_cmp); timing_stop(t,3); memcpy(timing_array, copy_array, array_size); timing_start(); heapsort(timing_array, i, sizeof(test_item_t), item_cmp); timing_stop(t,4); } printf("%d", i); timing_print(t,"\t%.2f",n_samples); putchar('\n'); free(timing_array); free(copy_array); } timing_free(t); /* Time radix sort on the largest array, using different radix sizes. */ printf("\nRadix Sort Results. Using n = %d\n", max_n); printf("(radix, time)\n"); array_size = max_n * sizeof(test_item_t); timing_array = malloc(array_size); copy_array = malloc(array_size); rand_array(copy_array, max_n, MAX_VALUE); for(radix = 2; radix <= max_n; radix <<= 1) { timer_reset(); for(j = 0; j < n_samples; j++) { memcpy(timing_array, copy_array, array_size); timer_start(); radixsort(timing_array, max_n, sizeof(test_item_t), get_value, radix); timer_stop(); } printf("%d", radix); timer_print("\t%.2f", n_samples); putchar('\n'); } free(timing_array); free(copy_array); return 0; }