char * big_sort(char * input_file, char * output_file) { IntStream input = NULL; IntStream partial1 = NULL; IntStream partial2 = NULL; IntStream buffer = NULL; IntStream temp = NULL; int A[MAX_BUFFER_SIZE]; int n; char * result; input = new_int_stream_from_file(input_file, 0); if (!input) { printf("Unable to create the input stream!\n"); return NULL; } partial1 = new_int_stream_from_file("partial1.txt", 1); if (!partial1) { destroy_int_stream(input); printf("Unable to create partial results stream 1\n"); return NULL; } partial2 = new_int_stream_from_file("partial2.txt", 1); if (!partial2) { destroy_int_stream(input); destroy_int_stream(partial1); printf("Unable to create partial results stream 2\n"); return NULL; } buffer = new_int_stream_for_array(); if (!buffer) { destroy_int_stream(input); destroy_int_stream(partial1); destroy_int_stream(partial2); printf("Unable to create buffer stream\n"); return NULL; } while ((n = read_batch(input, A)) > 0) { merge_sort_in_place(A, n); hook_int_stream_to_array(buffer, A, n, 0, n - 1); merge(buffer, partial1, partial2); reset_int_stream(partial1); reset_int_stream(partial2); temp = partial1; partial1 = partial2; partial2 = temp; } result = get_filename(partial1); rename(result, output_file); return output_file; }
void dump_clocks_terse(FILE *writeFile){ static uint64_t totalThreads = 0; static uint64_t ** aperf_val = NULL, ** mperf_val = NULL, ** tsc_val = NULL; if (!totalThreads) { totalThreads = num_devs(); clocks_storage(&aperf_val, &mperf_val, &tsc_val); } int thread_idx; read_batch(CLOCKS_DATA); for(thread_idx=0; thread_idx<totalThreads; thread_idx++){ fprintf(writeFile, "%20lu %20lu %20lu ", *aperf_val[thread_idx], *mperf_val[thread_idx], *tsc_val[thread_idx]); } }
void dump_clocks_readable(FILE * writeFile) { static uint64_t totalThreads = 0; static uint64_t ** aperf_val = NULL, ** mperf_val = NULL, ** tsc_val = NULL; if (!totalThreads) { totalThreads = num_devs(); clocks_storage(&aperf_val, &mperf_val, &tsc_val); } #ifdef LIBMSR_DEBUG fprintf(stderr, "DEBUG: (clocks_readable) totalThreads is %lu\n", totalThreads); #endif int thread_idx; read_batch(CLOCKS_DATA); for(thread_idx=0; thread_idx<totalThreads; thread_idx++){ fprintf(writeFile, "aperf%02d:%20lu mperf%02d:%20lu tsc%02d:%20lu\n", thread_idx, *aperf_val[thread_idx], thread_idx, *mperf_val[thread_idx], thread_idx, *tsc_val[thread_idx]); } }
int read_rapl_data(void) { static struct rapl_data *rapl = NULL; static uint64_t *rapl_flags = NULL; static short init = 0; static uint64_t sockets = 0; int s; if (!init) { sockets = num_sockets(); if (rapl_storage(&rapl, &rapl_flags)) { return -1; } create_rapl_data_batch(rapl_flags, rapl); rapl->now.tv_sec = 0; rapl->now.tv_usec = 0; rapl->old_now.tv_sec = 0; rapl->old_now.tv_usec = 0; rapl->elapsed = 0; for (s = 0; s < sockets; s++) { rapl->pkg_joules[s] = 0; rapl->old_pkg_joules[s] = 0; rapl->dram_joules[s] = 0; rapl->old_dram_joules[s] = 0; } } //p = &rapl[socket]; #ifdef LIBMSR_DEBUG fprintf(stderr, "%s %s::%d DEBUG: (read_rapl_data): socket=%lu at address %p\n", getenv("HOSTNAME"), __FILE__, __LINE__, num_sockets(), rapl); #endif /* Move current variables to "old" variables. */ rapl->old_now.tv_sec = rapl->now.tv_sec; rapl->old_now.tv_usec = rapl->now.tv_usec; /* Grab a timestamp. */ gettimeofday(&(rapl->now), NULL); if (init) { rapl->elapsed = (rapl->now.tv_sec - rapl->old_now.tv_sec) + (rapl->now.tv_usec - rapl->old_now.tv_usec)/1000000.0; for (s = 0; s < sockets; s++) { /* Make sure the pkg energy status register exists. */ if (*rapl_flags & PKG_ENERGY_STATUS) { #ifdef LIBMSR_DEBUG fprintf(stderr, "DEBUG: socket %lu msr 0x611 has destination %p\n", sockets, rapl->pkg_bits); #endif rapl->old_pkg_bits[s] = *rapl->pkg_bits[s]; rapl->old_pkg_joules[s] = rapl->pkg_joules[s]; } #ifdef LIBMSR_DEBUG fprintf(stderr, "DEBUG: (read_rapl_data): made it to 1st mark\n"); #endif /* Make sure the pkg perf status register exists. */ if (*rapl_flags & PKG_PERF_STATUS) { libmsr_error_handler("read_rapl_data(): MSR_PKG_PERF_STATUS not yet implemented", LIBMSR_ERROR_NOT_IMPLEMENTED_YET, getenv("HOSTNAME"), __FILE__, __LINE__); } /* Make sure the dram energy status register exists. */ if (*rapl_flags & DRAM_ENERGY_STATUS) { rapl->old_dram_bits[s] = *rapl->dram_bits[s]; rapl->old_dram_joules[s] = rapl->dram_joules[s]; } /* Make sure the dram perf status register exists. */ if (*rapl_flags & DRAM_PERF_STATUS) { libmsr_error_handler("read_rapl_data(): MSR_DRAM_PERF_STATUS not yet implemented", LIBMSR_ERROR_NOT_IMPLEMENTED_YET, getenv("HOSTNAME"), __FILE__, __LINE__); } } } read_batch(RAPL_DATA); for (s = 0; s < sockets; s++) { if (*rapl_flags & DRAM_ENERGY_STATUS) { #ifdef LIBMSR_DEBUG fprintf(stderr, "DEBUG: (read_rapl_data): translating dram\n"); #endif translate(s, rapl->dram_bits[s], &rapl->dram_joules[s], BITS_TO_JOULES_DRAM); } if (*rapl_flags & PKG_ENERGY_STATUS) { #ifdef LIBMSR_DEBUG fprintf(stderr, "DEBUG: (read_rapl_data): translating pkg\n"); #endif translate(s, rapl->pkg_bits[s], &rapl->pkg_joules[s], BITS_TO_JOULES); } #ifdef LIBMSR_DEBUG fprintf(stderr, "DEBUG: socket %d\n", s); fprintf(stderr, "DEBUG: elapsed %f\n", rapl->elapsed); fprintf(stderr, "DEBUG: pkg_bits %lx\n", *rapl->pkg_bits[s]); fprintf(stderr, "DEBUG: pkg_joules %lf\n", rapl->pkg_joules[s]); fprintf(stderr, "DEBUG: pkg_watts %lf\n", rapl->pkg_watts[s]); fprintf(stderr, "DEBUG: delta_joules %lf\n", rapl->pkg_delta_joules[s]); #endif } init = 1; return 0; }
void get_rapl_power_unit(struct rapl_units *ru) { static int init = 0; static uint64_t sockets = 0; static uint64_t **val = NULL; int i; sockets = num_sockets(); if (!init) { init = 1; val = (uint64_t **) libmsr_calloc(sockets, sizeof(uint64_t *)); allocate_batch(RAPL_UNIT, sockets); load_socket_batch(MSR_RAPL_POWER_UNIT, val, RAPL_UNIT); } read_batch(RAPL_UNIT); /* Initialize the units used for each socket. */ for (i = 0; i < sockets; i++) { // See figure 14-16 for bit fields. // 1 1 1 1 1 // 9 6 5 2 1 8 7 4 3 0 // // 1010 0001 0000 0000 0011 // // A 1 0 0 3 //ru[i].msr_rapl_power_unit = 0xA1003; ru[i].msr_rapl_power_unit = *val[i]; /* Default is 1010b or 976 microseconds. */ /* Storing (1/(2^TU))^-1 for maximum precision. */ ru[i].seconds = (double)(1 << (MASK_VAL(ru[i].msr_rapl_power_unit, 19, 16))); /* Default is 10000b or 15.3 microjoules. */ /* Storing (1/(2^ESU))^-1 for maximum precision. */ ru[i].joules = (double)(1 << (MASK_VAL(ru[i].msr_rapl_power_unit, 12, 8))); #ifdef LIBMSR_DEBUG fprintf(stderr, "DEBUG: joules unit is %f register has %lx\n", ru[i].joules, ru[i].msr_rapl_power_unit); #endif /* Default is 0011b or 1/8 Watts. */ ru[i].watts = ((1.0)/((double)(1 << (MASK_VAL(ru[i].msr_rapl_power_unit, 3, 0))))); #ifdef LIBMSR_DEBUG fprintf(stdout, "Pkg %d MSR_RAPL_POWER_UNIT\n", i); fprintf(stdout, "Raw: %f sec, %f J, %f watts\n", ru[i].seconds, ru[i].joules, ru[i].watts); fprintf(stdout, "Adjusted: %f sec, %f J, %f watts\n", 1/ru[i].seconds, 1/ru[i].joules, ru[i].watts); #endif } /* Check consistency between packages. */ uint64_t *tmp = (uint64_t *) libmsr_calloc(sockets, sizeof(uint64_t)); for (i = 0; i < sockets; i++) { read_msr_by_coord(i, 0, 0, MSR_RAPL_POWER_UNIT, tmp); double energy = (double)(1 << (MASK_VAL(ru[i].msr_rapl_power_unit, 12, 8))); double seconds = (double)(1 << (MASK_VAL(ru[i].msr_rapl_power_unit, 19, 16))); double power = ((1.0)/((double)(1 << (MASK_VAL(ru[i].msr_rapl_power_unit, 3, 0))))); if (energy != ru[i].joules || power != ru[i].watts || seconds != ru[i].seconds) { libmsr_error_handler("get_rapl_power_unit(): Inconsistent rapl power units across packages", LIBMSR_ERROR_RUNTIME, getenv("HOSTNAME"), __FILE__, __LINE__); } } }