opt_oct_t* opt_oct_assign_linexpr(ap_manager_t* man, bool destructive, opt_oct_t* o, ap_dim_t d, ap_linexpr0_t* lexpr, opt_oct_t* dest) { opt_oct_internal_t* pr = opt_oct_init_from_manager(man,AP_FUNID_ASSIGN_LINEXPR_ARRAY,2*(o->dim+1+5)); opt_uexpr u = opt_oct_uexpr_of_linexpr(pr,pr->tmp,lexpr,o->intdim,o->dim); opt_oct_mat_t* src; bool respect_closure; if(d>=o->dim){ return NULL; } if (dest && !dest->closed && !dest->m) /* definitively empty due to dest*/ return opt_oct_set_mat(pr,o,NULL,NULL,destructive); if (u.type==OPT_EMPTY) /* definitively empty due to empty expression */ return opt_oct_set_mat(pr,o,NULL,NULL,destructive); /* useful to close only for non-invertible assignments */ if ((u.type!=OPT_UNARY || u.i!=d) && pr->funopt->algorithm>=0) opt_oct_cache_closure(pr,o); src = o->closed ? o->closed : o->m; if (!src) return opt_oct_set_mat(pr,o,NULL,NULL,destructive); /* empty */ /* can / should we try to respect the closure */ respect_closure = (src==o->closed) && (pr->funopt->algorithm>=0) && (!dest); if (!destructive) src = opt_hmat_copy(src,o->dim); /* go */ #if defined(TIMING) start_timing(); #endif opt_hmat_assign(pr,u,src,o->dim,d,&respect_closure); #if defined(TIMING) record_timing(assign_linexpr_time); #endif /* exact on Q if zeroary or unary, closed arg and no conv error */ if (u.type==OPT_BINARY || u.type==OPT_OTHER) flag_incomplete; else if (num_incomplete || o->intdim) flag_incomplete; else if (!o->closed) flag_algo; else if (pr->conv) flag_conv; /* intersect with dest */ if (dest) { opt_oct_mat_t* src2 = dest->closed ? dest->closed : dest->m; meet_half(src,src,src2,o->dim,true); } if (respect_closure) return opt_oct_set_mat(pr,o,NULL,src,destructive); else return opt_oct_set_mat(pr,o,src,NULL,destructive); }
opt_oct_t* opt_oct_meet_lincons_array(ap_manager_t* man, bool destructive, opt_oct_t* o, ap_lincons0_array_t* array) { opt_oct_internal_t* pr = opt_oct_init_from_manager(man,AP_FUNID_MEET_LINCONS_ARRAY,2*(o->dim+8)); if (!o->closed && !o->m) /* definitively empty */ return opt_oct_set_mat(pr,o,NULL,NULL,destructive); else { bool exact, respect_closure; int i; opt_oct_mat_t * oo = o->closed ? o->closed : o->m; /* can / should we try to respect closure */ respect_closure = (oo==o->closed) && (pr->funopt->algorithm>=0); int size = 2*(o->dim)*(o->dim + 1); if (!destructive) oo = opt_hmat_copy(oo,o->dim); /* go */ #if defined(TIMING) start_timing(); #endif bool res = opt_hmat_add_lincons(pr,oo,o->intdim,o->dim,array,&exact,&respect_closure); #if defined(TIMING) record_timing(meet_lincons_time); #endif if (res) { /* empty */ if (!destructive) { opt_hmat_free(oo); oo = NULL; } return opt_oct_set_mat(pr,o,NULL,NULL,destructive); } else { /* exact if octagonal constraints & no conversion error */ if (num_incomplete || !exact) flag_incomplete; else if (pr->conv) flag_conv; if (respect_closure) return opt_oct_set_mat(pr,o,NULL,oo,destructive); else return opt_oct_set_mat(pr,o,oo,NULL,destructive); } } }
int main(int argc, char **argv) { key_data key; int inc = 10; int num_trials = 1; // Initialize the microblaze platform... init_platform(); // Set up the attack incremement if (inc > 0) { inc = 1 << inc; } printf("\nStudy increment: %d", inc); // Display the algorithm. printf("\nUsing algorithm \"%s\"", alg_name()); printf("\nAttacking key \"%s\"", argv[1]); // Display the number of trials... if (num_trials != 1) { printf("\nNumber of trials: %d", num_trials); } /* else if (out_file) printf("\nOutputting to file: %s", out_file); else if (in_file) printf("\nReading from file: %s", in_file); */ #ifdef NONE_AES printf("\nAttacking NONE_AES_CORE implementation"); #elif defined(SMALL_AES) printf("\nAttacking SMALL_AES_CORE implementation"); #else printf("\nAttacking traditional AES_CORE implementation"); #endif #ifdef DECRYPT_MODE printf("\nDECRYPT MODE"); #else printf("\nENCRYPT MODE"); #endif cache_evict_init(); int * results = malloc(num_trials * sizeof(int)); timing_pair * buffer = malloc(BUF_SIZE * sizeof(timing_pair)); timing_data * data = malloc(sizeof(timing_data)); if (data == 0x0) { printf("Data couldn't allocate.\n"); return; } if (buffer == 0x0) { printf("Buffer couldn't allocate.\n"); return; } FILE * in; //if (!USE_RANDOM_KEY) { printf("Initializing random key...\n"); re_key(&key, argv[1]); } fflush(stdout); /* if (out_file) { output_timings(out_file, buffer, inc, &key); return 1; } if (in_file) { char open_type = 'r'; in = fopen(in_file, &open_type); if (in == NULL) { printf("\nCould not open file: %s", in_file); return -1; } } */ int round, i; for (round = 1; round <= num_trials; round++) { printf("\nBeginning Attack #%d\n", round); init_data(data); //if (USE_RANDOM_KEY) // reseed with a random key re_key(&key, "/dev/random"); long long num_studies = 0; double clip_time = 0; int success = 0; long long max = MAX_STUDY; while (num_studies < max && !success) { printf("num_studies = %d\n", num_studies); int offset = num_studies % BUF_SIZE; if (inc > BUF_SIZE) { int read = 0; int num_read = BUF_SIZE; while (read < inc) { //printf("read = %d\n", read); generate_samples(buffer, &key); printf("buffer address %x:\n", buffer); printf("data address = %x\n", data); clip_time = calculate_clip_time(buffer, num_read); for (i = 0; i < BUF_SIZE && read < inc; i++) { //printf("i = %d\n", i); if (buffer[offset + i].time < clip_time) record_timing(data, &buffer[offset + i]); read++; } } } else { printf("inc <= BUF_SIZE\n"); if (offset == 0) { int num_read = BUF_SIZE; //if (!in_file) generate_samples(buffer, &key); printf("generate_samples done\n"); /*else { num_read = fread(buffer, sizeof(timing_pair), BUF_SIZE, in); if (num_read < BUF_SIZE) max = num_studies + num_read; printf("\nRead in %d samples ", num_read); }*/ clip_time = calculate_clip_time(buffer, num_read); printf("clip_time done\n"); } for (i = 0; i < inc; i++) printf("i (inc) = %d\n", i); if (buffer[offset + i].time < clip_time) record_timing(data, &buffer[offset + i]); } num_studies += inc; printf("\nchecking data!\n"); if (check_data(data, &key)) { printf( "\nKey recovered after studying %lld samples (< 2^%d)\n", num_studies, bound(num_studies)); success = 1; } else printf("\nNo success after studying %d samples", num_studies); } if (!success) printf("Attack failed after %d encryptions", num_studies); else { int j = round - 1; while (j > 0 && results[j - 1] > num_studies) { results[j] = results[j - 1]; j--; } results[j] = num_studies; printf("\nResults: "); int total = 0; for (j = 0; j < round; j++) { printf(" %d ", results[j]); total += results[j]; } printf("\nMin: %d", results[0]); printf("\nMax: %d", results[round - 1]); printf("\nMed: %d", results[round / 2]); printf("\nMean: %.2f", ((double) total) / round); } } // Clean up and terminate... cleanup_platform(); }
void oskar_simulator_run(oskar_Simulator* h, int* status) { int i, num_threads = 1, num_vis_blocks; if (*status) return; /* Check the visibilities are going somewhere. */ if (!h->vis_name #ifndef OSKAR_NO_MS && !h->ms_name #endif ) { oskar_log_error(h->log, "No output file specified."); #ifdef OSKAR_NO_MS if (h->ms_name) oskar_log_error(h->log, "OSKAR was compiled without Measurement Set support."); #endif *status = OSKAR_ERR_FILE_IO; return; } /* Initialise if required. */ oskar_simulator_check_init(h, status); /* Get the number of visibility blocks to be processed. */ num_vis_blocks = oskar_simulator_num_vis_blocks(h); /* Record memory usage. */ if (h->log && !*status) { oskar_log_section(h->log, 'M', "Initial memory usage"); #ifdef OSKAR_HAVE_CUDA for (i = 0; i < h->num_gpus; ++i) oskar_cuda_mem_log(h->log, 0, h->gpu_ids[i]); #endif system_mem_log(h->log); oskar_log_section(h->log, 'M', "Starting simulation..."); } /* Start simulation timer. */ oskar_timer_start(h->tmr_sim); /*----------------------------------------------------------------------- *-- START OF MULTITHREADED SIMULATION CODE ----------------------------- *-----------------------------------------------------------------------*/ /* Loop over blocks of observation time, running simulation and file * writing one block at a time. Simulation and file output are overlapped * by using double buffering, and a dedicated thread is used for file * output. * * Thread 0 is used for file writes. * Threads 1 to n (mapped to compute devices) do the simulation. * * Note that no write is launched on the first loop counter (as no * data are ready yet) and no simulation is performed for the last loop * counter (which corresponds to the last block + 1) as this iteration * simply writes the last block. */ #ifdef _OPENMP num_threads = h->num_devices + 1; omp_set_num_threads(num_threads); omp_set_nested(0); #else oskar_log_warning(h->log, "OpenMP not found: Using one compute device."); #endif oskar_simulator_reset_work_unit_index(h); #pragma omp parallel { int b, thread_id = 0, device_id = 0; /* Get host thread ID and device ID. */ #ifdef _OPENMP thread_id = omp_get_thread_num(); device_id = thread_id - 1; #endif /* Loop over simulation time blocks (+1, for the last write). */ for (b = 0; b < num_vis_blocks + 1; ++b) { if ((thread_id > 0 || num_threads == 1) && b < num_vis_blocks) oskar_simulator_run_block(h, b, device_id, status); if (thread_id == 0 && b > 0) { oskar_VisBlock* block; block = oskar_simulator_finalise_block(h, b - 1, status); oskar_simulator_write_block(h, block, b - 1, status); } /* Barrier 1: Reset work unit index. */ #pragma omp barrier if (thread_id == 0) oskar_simulator_reset_work_unit_index(h); /* Barrier 2: Synchronise before moving to the next block. */ #pragma omp barrier if (thread_id == 0 && b < num_vis_blocks && h->log && !*status) oskar_log_message(h->log, 'S', 0, "Block %*i/%i (%3.0f%%) " "complete. Simulation time elapsed: %.3f s", disp_width(num_vis_blocks), b+1, num_vis_blocks, 100.0 * (b+1) / (double)num_vis_blocks, oskar_timer_elapsed(h->tmr_sim)); } } /*----------------------------------------------------------------------- *-- END OF MULTITHREADED SIMULATION CODE ------------------------------- *-----------------------------------------------------------------------*/ /* Record memory usage. */ if (h->log && !*status) { oskar_log_section(h->log, 'M', "Final memory usage"); #ifdef OSKAR_HAVE_CUDA for (i = 0; i < h->num_gpus; ++i) oskar_cuda_mem_log(h->log, 0, h->gpu_ids[i]); #endif system_mem_log(h->log); } /* If there are sources in the simulation and the station beam is not * normalised to 1.0 at the phase centre, the values of noise RMS * may give a very unexpected S/N ratio! * The alternative would be to scale the noise to match the station * beam gain but that would require knowledge of the station beam * amplitude at the phase centre for each time and channel. */ if (h->log && oskar_telescope_noise_enabled(h->tel) && !*status) { int have_sources, amp_calibrated; have_sources = (h->num_sky_chunks > 0 && oskar_sky_num_sources(h->sky_chunks[0]) > 0); amp_calibrated = oskar_station_normalise_final_beam( oskar_telescope_station_const(h->tel, 0)); if (have_sources && !amp_calibrated) { const char* a = "WARNING: System noise added to visibilities"; const char* b = "without station beam normalisation enabled."; const char* c = "This will give an invalid signal to noise ratio."; oskar_log_line(h->log, 'W', ' '); oskar_log_line(h->log, 'W', '*'); oskar_log_message(h->log, 'W', -1, a); oskar_log_message(h->log, 'W', -1, b); oskar_log_message(h->log, 'W', -1, c); oskar_log_line(h->log, 'W', '*'); oskar_log_line(h->log, 'W', ' '); } } /* Record times and summarise output files. */ if (h->log && !*status) { size_t log_size = 0; char* log_data; oskar_log_set_value_width(h->log, 25); record_timing(h); oskar_log_section(h->log, 'M', "Simulation complete"); oskar_log_message(h->log, 'M', 0, "Output(s):"); if (h->vis_name) oskar_log_value(h->log, 'M', 1, "OSKAR binary file", "%s", h->vis_name); if (h->ms_name) oskar_log_value(h->log, 'M', 1, "Measurement Set", "%s", h->ms_name); /* Write simulation log to the output files. */ log_data = oskar_log_file_data(h->log, &log_size); #ifndef OSKAR_NO_MS if (h->ms) oskar_ms_add_history(h->ms, "OSKAR_LOG", log_data, log_size); #endif if (h->vis) oskar_binary_write(h->vis, OSKAR_CHAR, OSKAR_TAG_GROUP_RUN, OSKAR_TAG_RUN_LOG, 0, log_size, log_data, status); free(log_data); } /* Finalise. */ oskar_simulator_finalise(h, status); }
void oskar_interferometer_run(oskar_Interferometer* h, int* status) { int i, num_threads; oskar_Thread** threads = 0; ThreadArgs* args = 0; if (*status || !h) return; /* Check the visibilities are going somewhere. */ if (!h->vis_name #ifndef OSKAR_NO_MS && !h->ms_name #endif ) { oskar_log_error(h->log, "No output file specified."); #ifdef OSKAR_NO_MS if (h->ms_name) oskar_log_error(h->log, "OSKAR was compiled without Measurement Set support."); #endif *status = OSKAR_ERR_FILE_IO; return; } /* Initialise if required. */ oskar_interferometer_check_init(h, status); /* Set up worker threads. */ num_threads = h->num_devices + 1; oskar_barrier_set_num_threads(h->barrier, num_threads); threads = (oskar_Thread**) calloc(num_threads, sizeof(oskar_Thread*)); args = (ThreadArgs*) calloc(num_threads, sizeof(ThreadArgs)); for (i = 0; i < num_threads; ++i) { args[i].h = h; args[i].num_threads = num_threads; args[i].thread_id = i; } /* Record memory usage. */ if (h->log && !*status) { oskar_log_section(h->log, 'M', "Initial memory usage"); #ifdef OSKAR_HAVE_CUDA for (i = 0; i < h->num_gpus; ++i) oskar_cuda_mem_log(h->log, 0, h->gpu_ids[i]); #endif system_mem_log(h->log); oskar_log_section(h->log, 'M', "Starting simulation..."); } /* Start simulation timer. */ oskar_timer_start(h->tmr_sim); /* Set status code. */ h->status = *status; /* Start the worker threads. */ oskar_interferometer_reset_work_unit_index(h); for (i = 0; i < num_threads; ++i) threads[i] = oskar_thread_create(run_blocks, (void*)&args[i], 0); /* Wait for worker threads to finish. */ for (i = 0; i < num_threads; ++i) { oskar_thread_join(threads[i]); oskar_thread_free(threads[i]); } free(threads); free(args); /* Get status code. */ *status = h->status; /* Record memory usage. */ if (h->log && !*status) { oskar_log_section(h->log, 'M', "Final memory usage"); #ifdef OSKAR_HAVE_CUDA for (i = 0; i < h->num_gpus; ++i) oskar_cuda_mem_log(h->log, 0, h->gpu_ids[i]); #endif system_mem_log(h->log); } /* If there are sources in the simulation and the station beam is not * normalised to 1.0 at the phase centre, the values of noise RMS * may give a very unexpected S/N ratio! * The alternative would be to scale the noise to match the station * beam gain but that would require knowledge of the station beam * amplitude at the phase centre for each time and channel. */ if (h->log && oskar_telescope_noise_enabled(h->tel) && !*status) { int have_sources, amp_calibrated; have_sources = (h->num_sky_chunks > 0 && oskar_sky_num_sources(h->sky_chunks[0]) > 0); amp_calibrated = oskar_station_normalise_final_beam( oskar_telescope_station_const(h->tel, 0)); if (have_sources && !amp_calibrated) { const char* a = "WARNING: System noise added to visibilities"; const char* b = "without station beam normalisation enabled."; const char* c = "This will give an invalid signal to noise ratio."; oskar_log_line(h->log, 'W', ' '); oskar_log_line(h->log, 'W', '*'); oskar_log_message(h->log, 'W', -1, a); oskar_log_message(h->log, 'W', -1, b); oskar_log_message(h->log, 'W', -1, c); oskar_log_line(h->log, 'W', '*'); oskar_log_line(h->log, 'W', ' '); } } /* Record times and summarise output files. */ if (h->log && !*status) { size_t log_size = 0; char* log_data; oskar_log_set_value_width(h->log, 25); record_timing(h); oskar_log_section(h->log, 'M', "Simulation complete"); oskar_log_message(h->log, 'M', 0, "Output(s):"); if (h->vis_name) oskar_log_value(h->log, 'M', 1, "OSKAR binary file", "%s", h->vis_name); if (h->ms_name) oskar_log_value(h->log, 'M', 1, "Measurement Set", "%s", h->ms_name); /* Write simulation log to the output files. */ log_data = oskar_log_file_data(h->log, &log_size); #ifndef OSKAR_NO_MS if (h->ms) oskar_ms_add_history(h->ms, "OSKAR_LOG", log_data, log_size); #endif if (h->vis) oskar_binary_write(h->vis, OSKAR_CHAR, OSKAR_TAG_GROUP_RUN, OSKAR_TAG_RUN_LOG, 0, log_size, log_data, status); free(log_data); } /* Finalise. */ oskar_interferometer_finalise(h, status); }
opt_oct_t* opt_oct_assign_linexpr_array(ap_manager_t* man, bool destructive, opt_oct_t* o, ap_dim_t* tdim, ap_linexpr0_t** lexpr, size_t size, opt_oct_t* dest) { if (size==1) return opt_oct_assign_linexpr(man,destructive,o,tdim[0],lexpr[0],dest); opt_oct_internal_t* pr = opt_oct_init_from_manager(man,AP_FUNID_ASSIGN_LINEXPR_ARRAY,2*(o->dim+size+5)); ap_dim_t* d = (ap_dim_t*) pr->tmp2; opt_oct_mat_t *src, *dst; size_t i; ap_dim_t p = o->dim; int inexact = 0; bool respect_closure = false; /* TODO */ int src_size = 2*(o->dim)*(o->dim+1); /* checks */ if(size<=0){ return NULL; } for (i=0;i<o->dim;i++) { d[i] = 0; } for (i=0;i<size;i++) { if(tdim[i] >= o->dim){ return NULL; } if(d[tdim[i]]){ return NULL; } /* tdim has duplicate */ d[tdim[i]] = 1; } if (dest && !dest->closed && !dest->m) /* definitively empty due to dest*/ return opt_oct_set_mat(pr,o,NULL,NULL,destructive); if (pr->funopt->algorithm>=0) opt_oct_cache_closure(pr,o); src = o->closed ? o->closed : o->m; if (!src) return opt_oct_set_mat(pr,o,NULL,NULL,destructive); /* empty */ /* add temporary dimensions to hold destination variables */ dst = opt_hmat_alloc_top(o->dim+size); #if defined(TIMING) start_timing(); #endif opt_hmat_set_array(dst->mat,src->mat,src_size); /* coefs in expr for temporary dimensions are set to 0 */ for (i=0;i<2*size;i++){ pr->tmp[2*o->dim+i+2] = 0; } /* perform assignments */ for (i=0;i<size;i++) { opt_uexpr u = opt_oct_uexpr_of_linexpr(pr,pr->tmp,lexpr[i],o->intdim,o->dim); if (u.type==OPT_EMPTY) { opt_hmat_free(dst); return opt_oct_set_mat(pr,o,NULL,NULL,destructive); } if (u.type==OPT_BINARY || u.type==OPT_OTHER) inexact = 1; opt_hmat_assign(pr,u,dst,o->dim+size,o->dim+i,&respect_closure); } #if defined(TIMING) record_timing(assign_linexpr_time); #endif /* now close & remove temporary variables */ if (pr->funopt->algorithm>=0) { if (opt_hmat_strong_closure(dst,o->dim+size)) { /* empty */ opt_hmat_free(dst); return opt_oct_set_mat(pr,o,NULL,NULL,destructive); } } else flag_algo; if (!destructive) src = opt_hmat_alloc(src_size); for (i=0;i<o->dim;i++) { d[i] = i; } for (i=0;i<size;i++) { d[o->dim+i] = tdim[i]; d[tdim[i]] = o->dim; } opt_hmat_permute(src,dst,o->dim,o->dim+size,d); opt_hmat_free(dst); /* intersect with dest */ if (dest) { opt_oct_mat_t * src2 = dest->closed ? dest->closed : dest->m; meet_half(src,src,src2,o->dim,true); } if (inexact || o->intdim) flag_incomplete; else if (!o->closed) flag_algo; else if (pr->conv) flag_conv; return opt_oct_set_mat(pr,o,src,NULL,destructive); }