int main() { long start_time = getCurrentTime(); #if TEST_TYPE == COUNTER_T t = (counter_t*)malloc(sizeof(counter_t)); counter_init(t, 0); #elif TEST_TYPE == LIST_T t = (list_t*)malloc(sizeof(list_t)); list_init(t); #else t = (hash_t*)malloc(sizeof(hash_t)); hash_init(t, 30); #endif int number = 1000; int thread_no = 15; int i; p = (pthread_t*)malloc(sizeof(pthread_t) * thread_no); for (i = 0; i < thread_no; ++i) { pthread_create(p+i, NULL, (void*)test, &number); } for (i = 0; i < thread_no; ++i) { pthread_join(p[i], NULL); // printf("%d completed\n", i); } printf("%.3lf", (double)(getCurrentTime() - start_time) / 1000.0); check_and_release(); return 0; }
void freq_main(void) { cli(); counter_init(); gate_init(); stop(); reset(); ff_clr(); key_init(); setup_timers(); setup_interrupts(); adc_init(); sti(); /*clear counter*/ TCNT2= 0; TCNT0= 0; TCNT1= 0xFF00; T0_ovc = T1_ovc =0; start(); //fast clear screen... post_display(filter());//really result while(1) { key_process(); keep_live(); mode = read_adc_mode(); update_lcd_status(); if(is_stop()&&soft_stop){ calc_freq(); post_display(filter());//really result c_live() ; //mark succeufull .. if(loop>=(ST)){ //never clear reset(); } loop=0; start(); } } }
int main(int argc, char **argv) { int i; int sem_size; int final_counter; int final_target = NUM_CHILDREN*TARGET_COUNT_PER_CHILD; if (argc >= 2) sem_size = NUM_CHILDREN; else sem_size = 1; // Initialize semaphore to 1 if (sem_init(SEMAPHORE_NUM, sem_size) < 0) { printf(1, "main: error initializing semaphore %d\n", SEMAPHORE_NUM); exit(); } printf(1, "main: initialized semaphore %d to %d\n", SEMAPHORE_NUM, sem_size); // Initialize counter counter_init(COUNTER_FILE, 0); printf(1, "Running with %d processes...\n", NUM_CHILDREN); // Start all children for (i=0; i<NUM_CHILDREN; i++) { int pid = fork(); if (pid == 0) child(); } // Wait for all children for (i=0; i<NUM_CHILDREN; i++) { wait(); } // Check the result final_counter = counter_get(COUNTER_FILE); printf(1, "Final counter is %d, target is %d\n", final_counter, final_target); if (final_counter == final_target) printf(1, "TEST PASSED!\n"); else printf(1, "TEST FAILED!\n"); // Clean up semaphore sem_destroy(SEMAPHORE_NUM); // Exit exit(); }
void FreqCountClass::begin(uint16_t msec) { if (msec < 10) return; gate_index = 0; count_msw = 0; count_prev = 0; count_ready = 0; counter_init(); gate_length = timer_init(msec); uint8_t status = SREG; cli(); timer_start(); timer_isr_latency_delay(); counter_start(); SREG = status; }
static void init_all(void) { led_group(LED_ALL, false); logic_init(); flipflop_init(); counter_init(); shiftreg_init(); povmsg_init(); dice_init(); game_init(); select = modeDefault; mode = modeDefault; sparkle_delay = SPARKLE_DELAY_DFLT; sparkle_timer = 0; select_timer = 0; off_timer = 0; idle_timer = 0; }
#include "work.h" #include <stdio.h> void get_time(imagedata_t *img, const char *fn, logger_t *l) { /*{{{*/ // this is a hack that is targeted to a // specific small set of jpeg images, // not expected, or more so, expected to // not work with arbitrary files log(l, "trying to open \"%s\"\n", fn); FILE *fd = fopen(fn, "rb"); assert(fd); char dstr[20]; fseek(fd, 188, SEEK_SET); fread(dstr, sizeof(char), 19, fd); fclose(fd); dstr[19] = '\0'; // 2013:12:27 18:20:32 // 0123456789012345678 img->time.tm_year = atoi(dstr)-1900; img->time.tm_mon = atoi(dstr+5); img->time.tm_mday = atoi(dstr+8); img->time.tm_hour = atoi(dstr+11); img->time.tm_min = atoi(dstr+14); img->time.tm_sec = atoi(dstr+17); img->stamp = mktime(&img->time); log(l, "%s -> %s -> %04d-%02d-%02d %02d:%02d:%02d -> %Ld\n", fn, dstr, img->time.tm_year, img->time.tm_mon, img->time.tm_mday, img->time.tm_hour, img->time.tm_min, img->time.tm_sec, img->stamp ); } /*}}}*/ void scalepow2(bitmap_t *out, bitmap_t *in, unsigned int spow) { /*{{{*/ unsigned int limit = 1<<spow; for (size_t ty=0; ty<out->height; ++ty) { for (size_t tx=0; tx<out->width; ++tx) { size_t tp = tx + ty*out->width; unsigned int ys = 0; unsigned int cbs = 0; unsigned int crs = 0; size_t ctr = 0; for (size_t y=0; y<limit; ++y) { size_t sy = ty*limit + y; if (sy >= in->height) { break; } for (size_t x=0; x<limit; ++x) { size_t sx = tx*limit + x; if (sx >= in->width) { break; } size_t sp = sx + sy*in->width; ys += in->data[sp].x[0]; cbs += in->data[sp].x[1]; crs += in->data[sp].x[2]; ++ctr; } } ctr = ctr==0?1:ctr; out->data[tp].x[0] = ys/ctr; out->data[tp].x[1] = cbs/ctr; out->data[tp].x[2] = crs/ctr; } } } /*}}}*/ void init_threaddata(threaddata_t *td, const char *ofmt, const char *ifmt, int start, int stop) { /*{{{*/ log_init(&td->common.l, stdout); log(&td->common.l, "- logger created\n"); td->common.writerstr = calloc(strlen(ofmt)+32, sizeof(char)); assert(td->common.writerstr != NULL); sprintf(td->common.writerstr, "pnmtojpeg -quality=100 > %s", ofmt); log(&td->common.l, "- output shape is '%s'\n", td->common.writerstr); td->common.readerfmt = ifmt; td->common.readerstr = calloc(strlen(ifmt)+16, sizeof(char)); assert(td->common.readerstr != NULL); sprintf(td->common.readerstr, "jpegtopnm < %s", ifmt); log(&td->common.l, "- input shape is '%s'\n", td->common.readerstr); log(&td->common.l, "- initializing counter\n"); counter_init(&td->counter, start, stop); log(&td->common.l, "- initializing buffer\n"); buffer_init(&td->buffer, BUFFERSIZE, loader, unloader); //font_load_gz(&td->font, "data/sun12x22.psfu"); font_load_gz(&td->font, "/usr/share/kbd/consolefonts/sun12x22.psfu.gz"); } /*}}}*/
void run_kernel( Input * I, Source * S, Table * table) { // Enter Parallel Region #pragma omp parallel default(none) shared(I, S, table) { #ifdef OPENMP int thread = omp_get_thread_num(); #else int thread = 0; #endif // Create Thread Local Random Seed unsigned int seed = time(NULL) * (thread+1); // Allocate Thread Local SIMD Vectors (align if using intel compiler) #ifdef INTEL SIMD_Vectors simd_vecs = aligned_allocate_simd_vectors(I); float * state_flux = (float *) _mm_malloc( I->egroups * sizeof(float), 64); #else SIMD_Vectors simd_vecs = allocate_simd_vectors(I); float * state_flux = (float *) malloc( I->egroups * sizeof(float)); #endif // Allocate Thread Local Flux Vector for( int i = 0; i < I->egroups; i++ ) state_flux[i] = (float) rand_r(&seed) / RAND_MAX; // Initialize PAPI Counters (if enabled) #ifdef PAPI int eventset = PAPI_NULL; int num_papi_events; #pragma omp critical { counter_init(&eventset, &num_papi_events, I); } #endif // Enter OMP For Loop over Segments #pragma omp for schedule(dynamic,100) for( long i = 0; i < I->segments; i++ ) { // Pick Random QSR int QSR_id = rand_r(&seed) % I->source_3D_regions; // Pick Random Fine Axial Interval int FAI_id = rand_r(&seed) % I->fine_axial_intervals; // Attenuate Segment attenuate_segment( I, S, QSR_id, FAI_id, state_flux, &simd_vecs, table); } // Stop PAPI Counters #ifdef PAPI if( thread == 0 ) { printf("\n"); border_print(); center_print("PAPI COUNTER RESULTS", 79); border_print(); printf("Count \tSmybol \tDescription\n"); } { #pragma omp barrier } counter_stop(&eventset, num_papi_events, I); #endif } }
int main(void) { log_init(); leds_init(); timer_init(); counter_init(); buttons_init(); ble_stack_init(); gap_params_init(); conn_params_init(); gatt_init(); advertising_data_set(); server_init(); client_init(); // Default ATT MTU size and connection interval are set at compile time. gatt_mtu_set(m_test_params.att_mtu); // Data Length Extension (DLE) is on by default. // Enable the Connection Event Length Extension. conn_evt_len_ext_set(m_test_params.conn_evt_len_ext_enabled); NRF_LOG_INFO("ATT MTU example started.\r\n"); NRF_LOG_INFO("Press button 3 on the board connected to the PC.\r\n"); NRF_LOG_INFO("Press button 4 on other board.\r\n"); NRF_LOG_FLUSH(); board_role_select(); if (m_board_role == BOARD_TESTER) { m_print_menu = true; } if (m_board_role == BOARD_DUMMY) { advertising_start(); scan_start(); } // Enter main loop. NRF_LOG_DEBUG("Entering main loop.\r\n"); for (;;) { if (m_print_menu) { menu_print(); } if (is_test_ready()) { m_run_test = true; test_run(); } if (!NRF_LOG_PROCESS()) { wait_for_event(); } } }
static int pkg_create_from_dir(struct pkg *pkg, const char *root, struct packing *pkg_archive) { char fpath[MAXPATHLEN]; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; int ret; struct stat st; int64_t flatsize = 0; int64_t nfiles; const char *relocation; hardlinks_t *hardlinks; if (pkg_is_valid(pkg) != EPKG_OK) { pkg_emit_error("the package is not valid"); return (EPKG_FATAL); } relocation = pkg_kv_get(&pkg->annotations, "relocated"); if (relocation == NULL) relocation = ""; if (pkg_rootdir != NULL) relocation = pkg_rootdir; /* * Get / compute size / checksum if not provided in the manifest */ nfiles = kh_count(pkg->filehash); counter_init("file sizes/checksums", nfiles); hardlinks = kh_init_hardlinks(); while (pkg_files(pkg, &file) == EPKG_OK) { snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", relocation, file->path); if (lstat(fpath, &st) == -1) { pkg_emit_error("file '%s' is missing", fpath); return (EPKG_FATAL); } if (file->size == 0) file->size = (int64_t)st.st_size; if (st.st_nlink == 1 || !check_for_hardlink(hardlinks, &st)) { flatsize += file->size; } file->sum = pkg_checksum_generate_file(fpath, PKG_HASH_TYPE_SHA256_HEX); if (file->sum == NULL) return (EPKG_FATAL); counter_count(); } kh_destroy_hardlinks(hardlinks); counter_end(); pkg->flatsize = flatsize; if (pkg->type == PKG_OLD_FILE) { pkg_emit_error("Cannot create an old format package"); return (EPKG_FATAL); } else { /* * Register shared libraries used by the package if * SHLIBS enabled in conf. Deletes shlib info if not. */ struct sbuf *b = sbuf_new_auto(); pkg_analyse_files(NULL, pkg, root); pkg_emit_manifest_sbuf(pkg, b, PKG_MANIFEST_EMIT_COMPACT, NULL); packing_append_buffer(pkg_archive, sbuf_data(b), "+COMPACT_MANIFEST", sbuf_len(b)); sbuf_clear(b); pkg_emit_manifest_sbuf(pkg, b, 0, NULL); sbuf_finish(b); packing_append_buffer(pkg_archive, sbuf_data(b), "+MANIFEST", sbuf_len(b)); sbuf_delete(b); } counter_init("packing files", nfiles); while (pkg_files(pkg, &file) == EPKG_OK) { snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", relocation, file->path); ret = packing_append_file_attr(pkg_archive, fpath, file->path, file->uname, file->gname, file->perm, file->fflags); if (developer_mode && ret != EPKG_OK) return (ret); counter_count(); } counter_end(); nfiles = kh_count(pkg->dirhash); counter_init("packing directories", nfiles); while (pkg_dirs(pkg, &dir) == EPKG_OK) { snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", relocation, dir->path); ret = packing_append_file_attr(pkg_archive, fpath, dir->path, dir->uname, dir->gname, dir->perm, dir->fflags); if (developer_mode && ret != EPKG_OK) return (ret); counter_count(); } counter_end(); return (EPKG_OK); }
int main( int argc, char* argv[] ) { // ===================================================================== // Initialization & Command Line Read-In // ===================================================================== int version = 13; int mype = 0; int max_procs = omp_get_num_procs(); int i, thread, mat; unsigned long seed; double omp_start, omp_end, p_energy; unsigned long long vhash = 0; int nprocs; #ifdef MPI MPI_Status stat; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); MPI_Comm_rank(MPI_COMM_WORLD, &mype); #endif // rand() is only used in the serial initialization stages. // A custom RNG is used in parallel portions. #ifdef VERIFICATION srand(26); #else srand(time(NULL)); #endif // Process CLI Fields -- store in "Inputs" structure Inputs in = read_CLI( argc, argv ); // Set number of OpenMP Threads omp_set_num_threads(in.nthreads); // Print-out of Input Summary if( mype == 0 ) print_inputs( in, nprocs, version ); // ===================================================================== // Prepare Nuclide Energy Grids, Unionized Energy Grid, & Material Data // ===================================================================== // Allocate & fill energy grids #ifndef BINARY_READ if( mype == 0) printf("Generating Nuclide Energy Grids...\n"); #endif NuclideGridPoint ** nuclide_grids = gpmatrix(in.n_isotopes,in.n_gridpoints); #ifdef VERIFICATION generate_grids_v( nuclide_grids, in.n_isotopes, in.n_gridpoints ); #else generate_grids( nuclide_grids, in.n_isotopes, in.n_gridpoints ); #endif // Sort grids by energy #ifndef BINARY_READ if( mype == 0) printf("Sorting Nuclide Energy Grids...\n"); sort_nuclide_grids( nuclide_grids, in.n_isotopes, in.n_gridpoints ); #endif // Prepare Unionized Energy Grid Framework #ifndef BINARY_READ GridPoint * energy_grid = generate_energy_grid( in.n_isotopes, in.n_gridpoints, nuclide_grids ); #else GridPoint * energy_grid = (GridPoint *)malloc( in.n_isotopes * in.n_gridpoints * sizeof( GridPoint ) ); int * index_data = (int *) malloc( in.n_isotopes * in.n_gridpoints * in.n_isotopes * sizeof(int)); for( i = 0; i < in.n_isotopes*in.n_gridpoints; i++ ) energy_grid[i].xs_ptrs = &index_data[i*in.n_isotopes]; #endif // Double Indexing. Filling in energy_grid with pointers to the // nuclide_energy_grids. #ifndef BINARY_READ set_grid_ptrs( energy_grid, nuclide_grids, in.n_isotopes, in.n_gridpoints ); #endif #ifdef BINARY_READ if( mype == 0 ) printf("Reading data from \"XS_data.dat\" file...\n"); binary_read(in.n_isotopes, in.n_gridpoints, nuclide_grids, energy_grid); #endif // Get material data if( mype == 0 ) printf("Loading Mats...\n"); int *num_nucs = load_num_nucs(in.n_isotopes); int **mats = load_mats(num_nucs, in.n_isotopes); #ifdef VERIFICATION double **concs = load_concs_v(num_nucs); #else double **concs = load_concs(num_nucs); #endif #ifdef BINARY_DUMP if( mype == 0 ) printf("Dumping data to binary file...\n"); binary_dump(in.n_isotopes, in.n_gridpoints, nuclide_grids, energy_grid); if( mype == 0 ) printf("Binary file \"XS_data.dat\" written! Exiting...\n"); return 0; #endif // ===================================================================== // Cross Section (XS) Parallel Lookup Simulation Begins // ===================================================================== // Outer benchmark loop can loop through all possible # of threads #ifdef BENCHMARK for( int bench_n = 1; bench_n <=omp_get_num_procs(); bench_n++ ) { in.nthreads = bench_n; omp_set_num_threads(in.nthreads); #endif if( mype == 0 ) { printf("\n"); border_print(); center_print("SIMULATION", 79); border_print(); } omp_start = omp_get_wtime(); //initialize papi with one thread (master) here #ifdef PAPI if ( PAPI_library_init(PAPI_VER_CURRENT) != PAPI_VER_CURRENT){ fprintf(stderr, "PAPI library init error!\n"); exit(1); } #endif // OpenMP compiler directives - declaring variables as shared or private #pragma omp parallel default(none) \ private(i, thread, p_energy, mat, seed) \ shared( max_procs, in, energy_grid, nuclide_grids, \ mats, concs, num_nucs, mype, vhash) { // Initialize parallel PAPI counters #ifdef PAPI int eventset = PAPI_NULL; int num_papi_events; #pragma omp critical { counter_init(&eventset, &num_papi_events); } #endif double macro_xs_vector[5]; double * xs = (double *) calloc(5, sizeof(double)); // Initialize RNG seeds for threads thread = omp_get_thread_num(); seed = (thread+1)*19+17; // XS Lookup Loop #pragma omp for schedule(dynamic) for( i = 0; i < in.lookups; i++ ) { // Status text if( INFO && mype == 0 && thread == 0 && i % 1000 == 0 ) printf("\rCalculating XS's... (%.0lf%% completed)", (i / ( (double)in.lookups / (double) in.nthreads )) / (double) in.nthreads * 100.0); // Randomly pick an energy and material for the particle #ifdef VERIFICATION #pragma omp critical { p_energy = rn_v(); mat = pick_mat(&seed); } #else p_energy = rn(&seed); mat = pick_mat(&seed); #endif // debugging //printf("E = %lf mat = %d\n", p_energy, mat); // This returns the macro_xs_vector, but we're not going // to do anything with it in this program, so return value // is written over. calculate_macro_xs( p_energy, mat, in.n_isotopes, in.n_gridpoints, num_nucs, concs, energy_grid, nuclide_grids, mats, macro_xs_vector ); // Copy results from above function call onto heap // so that compiler cannot optimize function out // (only occurs if -flto flag is used) memcpy(xs, macro_xs_vector, 5*sizeof(double)); // Verification hash calculation // This method provides a consistent hash accross // architectures and compilers. #ifdef VERIFICATION char line[256]; sprintf(line, "%.5lf %d %.5lf %.5lf %.5lf %.5lf %.5lf", p_energy, mat, macro_xs_vector[0], macro_xs_vector[1], macro_xs_vector[2], macro_xs_vector[3], macro_xs_vector[4]); unsigned long long vhash_local = hash(line, 10000); #pragma omp atomic vhash += vhash_local; #endif } // Prints out thread local PAPI counters #ifdef PAPI if( mype == 0 && thread == 0 ) { printf("\n"); border_print(); center_print("PAPI COUNTER RESULTS", 79); border_print(); printf("Count \tSmybol \tDescription\n"); } { #pragma omp barrier } counter_stop(&eventset, num_papi_events); #endif } #ifndef PAPI if( mype == 0) { printf("\n" ); printf("Simulation complete.\n" ); } #endif omp_end = omp_get_wtime(); // Print / Save Results and Exit print_results( in, mype, omp_end-omp_start, nprocs, vhash ); #ifdef BENCHMARK } #endif #ifdef MPI MPI_Finalize(); #endif return 0; }
void * do_work_handler(void *arg) { struct config_instance_t * inst = (struct config_instance_t *)arg; pthread_t self_id = pthread_self(); int work_idx; int i; char send_dir[512] = ""; uint64_t file_id = inst->fileid; int source_file_fid; __u8 * msg_buff; signal(SIGPIPE,SIG_IGN); for(i = 0 ; i < MAX_THREAD_PER_INST ; i++){ if(inst->work_thread_t[i] == self_id){ work_idx = i; print_debug("my work idx is %d\n", work_idx); } } if(work_idx == 0){ counter_init(&send_success_cnt); } sprintf(send_dir, "%s/send/%llu/", sync_work_dir, file_id); if (access(send_dir, F_OK)) mkdir(send_dir, 0777); msg_buff = (__u8*)malloc(MB(16)); if (!msg_buff){ print_error("Alloc memory fails\n"); return NULL; } while(1){ struct dir_instance_t * dir_head; struct dir_instance_t * dir_curr; pthread_mutex_lock(&inst->work_thread_lock[work_idx]); while(inst->work_dir_head[work_idx] == NULL){ pthread_cond_wait(&inst->work_thread_wait[work_idx] , &inst->work_thread_lock[work_idx]); } dir_head = inst->work_dir_head[work_idx]; //inst->work_dir_head[work_idx] = NULL; pthread_mutex_unlock(&inst->work_thread_lock[work_idx]); source_file_fid = open(inst->filename, O_RDONLY | O_CREAT, 0666); if (source_file_fid < 0){ print_error("open %s error\n", inst->filename); release_dir(dir_head); usleep(0); continue; } for_each_entry_dir(dir_curr, dir_head){ vmsync_file_lock(inst->lock_fd[dir_curr->blockid % LOCK_HASH_SIZE]); do_file_sync(inst , work_idx , msg_buff , source_file_fid , send_dir , dir_curr); vmsync_file_unlock(inst->lock_fd[dir_curr->blockid % LOCK_HASH_SIZE]); } //print_debug("load all %d load sgl %d del all %d del sgl %d create sgl %d snd_msg %d\n" , // all_load_cnt , sgl_load_cnt , all_del_cnt , sgl_del_cnt , sgl_create_cnt , snd_msg_cnt); close(source_file_fid); release_dir(dir_head); pthread_mutex_lock(&inst->work_thread_lock[work_idx]); inst->work_dir_head[work_idx] = NULL; pthread_mutex_unlock(&inst->work_thread_lock[work_idx]); }
/********************************************************************** * Plugin starts here **********************************************************************/ enum plugin_status plugin_start(const void* parameter){ int button; int last_second = -1; bool redraw=true; struct time time; struct counter counter; bool exit_clock = false; (void)parameter; atexit(cleanup); #if LCD_DEPTH > 1 rb->lcd_set_backdrop(NULL); #endif load_settings(); /* init xlcd functions */ counter_init(&counter); clock_draw_set_colors(); while(!exit_clock){ clock_update_time(&time); if(!clock_settings.general.idle_poweroff) rb->reset_poweroff_timer(); /************************* * Scan for button presses ************************/ button = pluginlib_getaction(HZ/10, plugin_contexts, PLA_ARRAY_COUNT); redraw=true;/* we'll set it to false afterwards if there was no action */ switch (button){ case ACTION_COUNTER_TOGGLE: /* start/stop counter */ if(clock_settings.general.show_counter) counter_toggle(&counter); break; case ACTION_COUNTER_RESET: /* reset counter */ if(clock_settings.general.show_counter) counter_reset(&counter); break; case ACTION_MODE_NEXT_REPEAT: case ACTION_MODE_NEXT: clock_settings.mode++; if(clock_settings.mode >= NB_CLOCK_MODES) clock_settings.mode = 0; break; case ACTION_MODE_PREV_REPEAT: case ACTION_MODE_PREV: clock_settings.mode--; if(clock_settings.mode < 0) clock_settings.mode = NB_CLOCK_MODES-1; break; case ACTION_SKIN_PREV_REPEAT: case ACTION_SKIN_PREV: clock_settings_skin_next(&clock_settings); break; case ACTION_SKIN_NEXT_REPEAT: case ACTION_SKIN_NEXT: clock_settings_skin_previous(&clock_settings); break; case ACTION_MENU: clock_draw_restore_colors(); exit_clock=main_menu(); break; default: exit_on_usb(button); if(time.second != last_second){ last_second=time.second; redraw=true; }else redraw=false; break; } if(redraw){ clock_draw_set_colors(); FOR_NB_SCREENS(i) clock_draw(rb->screens[i], &time, &counter); redraw=false; } } return PLUGIN_OK; }
void run_history_based_simulation(Input input, CalcDataPtrs data, long * abrarov_result, long * alls_result, unsigned long * vhash_result ) { printf("Beginning history based simulation...\n"); long g_abrarov = 0; long g_alls = 0; unsigned long vhash = 0; #pragma omp parallel default(none) \ shared(input, data) \ reduction(+:g_abrarov, g_alls, vhash) { double * xs = (double *) calloc(4, sizeof(double)); int thread = omp_get_thread_num(); long abrarov = 0; long alls = 0; #ifdef PAPI int eventset = PAPI_NULL; int num_papi_events; #pragma omp critical { counter_init(&eventset, &num_papi_events); } #endif complex double * sigTfactors = (complex double *) malloc( input.numL * sizeof(complex double) ); // This loop is independent! // I.e., particle histories can be executed in any order #pragma omp for schedule(guided) for( int p = 0; p < input.particles; p++ ) { // Particles are seeded by their particle ID unsigned long seed = ((unsigned long) p+ (unsigned long)1)* (unsigned long) 13371337; // Randomly pick an energy and material for the particle double E = rn(&seed); int mat = pick_mat(&seed); #ifdef STATUS if( thread == 0 && p % 35 == 0 ) printf("\rCalculating XS's... (%.0lf%% completed)", (p / ( (double)input.particles / (double) input.nthreads )) / (double) input.nthreads * 100.0); #endif // This loop is dependent! // I.e., This loop must be executed sequentially, // as each lookup depends on results from the previous lookup. for( int i = 0; i < input.lookups; i++ ) { double macro_xs[4] = {0}; calculate_macro_xs( macro_xs, mat, E, input, data, sigTfactors, &abrarov, &alls ); // Results are copied onto heap to avoid some compiler // flags (-flto) from optimizing out function call memcpy(xs, macro_xs, 4*sizeof(double)); // Verification hash calculation // This method provides a consistent hash accross // architectures and compilers. #ifdef VERIFICATION char line[256]; sprintf(line, "%.2le %d %.2le %.2le %.2le %.2le", E, mat, macro_xs[0], macro_xs[1], macro_xs[2], macro_xs[3]); unsigned long long vhash_local = hash(line, 10000); vhash += vhash_local; #endif // Randomly pick next energy and material for the particle // Also incorporates results from macro_xs lookup to // enforce loop dependency. // In a real MC app, this dependency is expressed in terms // of branching physics sampling, whereas here we are just // artificially enforcing this dependence based on altering // the seed for( int x = 0; x < 4; x++ ) { if( macro_xs[x] > 0 ) seed += 1337*p; else seed += 42; } E = rn(&seed); mat = pick_mat(&seed); } } free(sigTfactors); // Accumulate global counters g_abrarov = abrarov; g_alls = alls; #ifdef PAPI if( thread == 0 ) { printf("\n"); border_print(); center_print("PAPI COUNTER RESULTS", 79); border_print(); printf("Count \tSmybol \tDescription\n"); } { #pragma omp barrier } counter_stop(&eventset, num_papi_events); #endif } *abrarov_result = g_abrarov; *alls_result = g_alls; *vhash_result = vhash; }
void run_event_based_simulation(Input input, CalcDataPtrs data, long * abrarov_result, long * alls_result, unsigned long * vhash_result ) { printf("Beginning event based simulation...\n"); long g_abrarov = 0; long g_alls = 0; unsigned long vhash = 0; #pragma omp parallel default(none) \ shared(input, data) \ reduction(+:g_abrarov, g_alls, vhash) { double * xs = (double *) calloc(4, sizeof(double)); int thread = omp_get_thread_num(); long abrarov = 0; long alls = 0; #ifdef PAPI int eventset = PAPI_NULL; int num_papi_events; #pragma omp critical { counter_init(&eventset, &num_papi_events); } #endif complex double * sigTfactors = (complex double *) malloc( input.numL * sizeof(complex double) ); // This loop is independent! // I.e., macroscopic cross section lookups in the event based simulation // can be executed in any order. #pragma omp for schedule(guided) for( int i = 0; i < input.lookups; i++ ) { // Particles are seeded by their particle ID unsigned long seed = ((unsigned long) i+ (unsigned long)1)* (unsigned long) 13371337; // Randomly pick an energy and material for the particle double E = rn(&seed); int mat = pick_mat(&seed); #ifdef STATUS if( thread == 0 && i % 2000 == 0 ) printf("\rCalculating XS's... (%.0lf%% completed)", (i / ( (double)input.lookups / (double) input.nthreads )) / (double) input.nthreads * 100.0); #endif double macro_xs[4] = {0}; calculate_macro_xs( macro_xs, mat, E, input, data, sigTfactors, &abrarov, &alls ); // Results are copied onto heap to avoid some compiler // flags (-flto) from optimizing out function call memcpy(xs, macro_xs, 4*sizeof(double)); // Verification hash calculation // This method provides a consistent hash accross // architectures and compilers. #ifdef VERIFICATION char line[256]; sprintf(line, "%.2le %d %.2le %.2le %.2le %.2le", E, mat, macro_xs[0], macro_xs[1], macro_xs[2], macro_xs[3]); unsigned long long vhash_local = hash(line, 10000); vhash += vhash_local; #endif } free(sigTfactors); // Accumulate global counters g_abrarov = abrarov; g_alls = alls; #ifdef PAPI if( thread == 0 ) { printf("\n"); border_print(); center_print("PAPI COUNTER RESULTS", 79); border_print(); printf("Count \tSmybol \tDescription\n"); } { #pragma omp barrier } counter_stop(&eventset, num_papi_events); #endif } *abrarov_result = g_abrarov; *alls_result = g_alls; *vhash_result = vhash; }