int main() { /* Initialize the RNG */ unsigned rng, i, trials = TRIALS / get_num_cores(), x, y, active; rng = 0; init_rng(&rng); barrier(&b0); /* Do the simulation */ for (i = 0; i < trials; ++i) { x = rand_mc(&rng) / ((1u<<31)/10000); y = rand_mc(&rng) / ((1u<<31)/10000); if (x * x + y * y <= 100000000) count[get_core_id()]++; } printf("core %u: %u\n", get_core_id(), count[get_core_id()]); barrier(&b1); if (get_core_id() == 0) barrier_init(&b0); barrier(&b2); if (get_core_id() == 0) barrier_init(&b1); /* Do the final reduction */ for (active = get_num_cores()/2; active > 0; active /= 2) { if (get_core_id() < active) { unsigned idx0 = get_core_id(), idx1 = get_core_id() + active; count[idx0] = count[idx0] + count[idx1]; printf("%u active cores, sum = %u\n", active, count[idx0]); } barrier(&b0); if (get_core_id() == 0) barrier_init(&b2); barrier(&b1); if (get_core_id() == 0) barrier_init(&b0); barrier(&b2); if (get_core_id() == 0) barrier_init(&b1); } if (get_core_id() == 0) { unsigned pi_whole = count[0]*4/TRIALS, pi_frac = count[0]*4%TRIALS/(TRIALS/100); printf("pi is approximately %u.%02u\n", pi_whole, pi_frac); } return 0; }
int main(int argc, char *argv[]){ #ifdef FORCE char buffer[8192], *p, *q; int length; #endif if (argc == 1) return 0; switch (argv[1][0]) { case '0' : /* for Makefile */ #ifdef FORCE printf("CORE=%s\n", CORENAME); #else #if defined(INTEL_AMD) || defined(POWER) || defined(__mips__) || defined(__arm__) || defined(__aarch64__) printf("CORE=%s\n", get_corename()); #endif #endif #ifdef FORCE printf("LIBCORE=%s\n", LIBNAME); #else printf("LIBCORE="); get_libname(); printf("\n"); #endif printf("NUM_CORES=%d\n", get_num_cores()); #if defined(__arm__) && !defined(FORCE) get_features(); #endif #ifdef INTEL_AMD #ifndef FORCE get_sse(); #else sprintf(buffer, "%s", ARCHCONFIG); p = &buffer[0]; while (*p) { if ((*p == '-') && (*(p + 1) == 'D')) { p += 2; while ((*p != ' ') && (*p != '\0')) { if (*p == '=') { printf("="); p ++; while ((*p != ' ') && (*p != '\0')) { printf("%c", *p); p ++; } } else { printf("%c", *p); p ++; if ((*p == ' ') || (*p =='\0')) printf("=1"); } } printf("\n"); } else p ++; } #endif #endif #ifdef MAKE_NB_JOBS printf("MAKE += -j %d\n", MAKE_NB_JOBS); #elif NO_PARALLEL_MAKE==1 printf("MAKE += -j 1\n"); #else #ifndef OS_WINDOWS printf("MAKE += -j %d\n", get_num_cores()); #endif #endif break; case '1' : /* For config.h */ #ifdef FORCE sprintf(buffer, "%s -DCORE_%s\n", ARCHCONFIG, CORENAME); p = &buffer[0]; while (*p) { if ((*p == '-') && (*(p + 1) == 'D')) { p += 2; printf("#define "); while ((*p != ' ') && (*p != '\0')) { if (*p == '=') { printf(" "); p ++; while ((*p != ' ') && (*p != '\0')) { printf("%c", *p); p ++; } } else { printf("%c", *p); p ++; } } printf("\n"); } else p ++; } #else get_cpuconfig(); #endif #ifdef FORCE printf("#define CHAR_CORENAME \"%s\"\n", CORENAME); #else #if defined(INTEL_AMD) || defined(POWER) || defined(__mips__) || defined(__arm__) || defined(__aarch64__) printf("#define CHAR_CORENAME \"%s\"\n", get_corename()); #endif #endif break; case '2' : /* SMP */ if (get_num_cores() > 1) printf("SMP=1\n"); break; } fflush(stdout); return 0; }
unsigned rand_mc(unsigned *p) { unsigned val; for (unsigned i = 0; i <= get_num_cores(); i++) val = rand_r(p); return val; }
/** * Initializes the library and sets the parallelism degree according to * the cost model obtained from the parameters that the user specifies. * If not specified otherwise after the initialization, the library will * consider all the protocols active. * * @param size_v4 Size of the array of pointers used to build the database * for v4 flows. * @param size_v6 Size of the array of pointers used to build the database * for v6 flows. * @param max_active_v4_flows The maximum number of IPv4 flows which can * be active at any time. After reaching this * threshold, new flows will not be created. * @param max_active_v6_flows The maximum number of IPv6 flows which can * be active at any time. After reaching this * threshold, new flows will not be created. * @param parallelism_details Details about the parallelism form. Must be * zeroed and then filled by the user. * @return A pointer to the state of the library. */ mc_dpi_library_state_t* mc_dpi_init_stateful( u_int32_t size_v4, u_int32_t size_v6, u_int32_t max_active_v4_flows, u_int32_t max_active_v6_flows, mc_dpi_parallelism_details_t parallelism_details){ mc_dpi_library_state_t* state; assert(posix_memalign((void**) &state, DPI_CACHE_LINE_SIZE, sizeof(mc_dpi_library_state_t)+DPI_CACHE_LINE_SIZE)==0); bzero(state, sizeof(mc_dpi_library_state_t)); u_int8_t parallelism_form=parallelism_details.parallelism_form; if(parallelism_details.available_processors){ state->available_processors = parallelism_details.available_processors; }else{ state->available_processors = get_num_cores(); } if(parallelism_form==MC_DPI_PARALLELISM_FORM_DOUBLE_FARM){ assert(state->available_processors >= 4+2); }else{ assert(state->available_processors >= 2+1); } state->mapping = new unsigned int[state->available_processors]; uint k; for(k=0; k<state->available_processors; k++){ if(parallelism_details.mapping==NULL){ state->mapping[k] = k; }else{ state->mapping[k] = parallelism_details.mapping[k]; } } state->terminating=0; u_int16_t hash_table_partitions; state->double_farm_L3_L4_active_workers = parallelism_details.double_farm_num_L3_workers; state->double_farm_L7_active_workers = parallelism_details.double_farm_num_L7_workers; state->single_farm_active_workers = state->available_processors-2; if(parallelism_form==MC_DPI_PARALLELISM_FORM_DOUBLE_FARM){ assert(state->double_farm_L3_L4_active_workers>0 && state->double_farm_L7_active_workers>0); debug_print("%s\n","[mc_dpi_api.cpp]: A pipeline of two " "farms will be activated."); hash_table_partitions=state->double_farm_L7_active_workers; }else{ assert(state->single_farm_active_workers>0); debug_print("%s\n","[mc_dpi_api.cpp]: Only one farm will " "be activated."); hash_table_partitions=state->single_farm_active_workers; } state->sequential_state=dpi_init_stateful_num_partitions( size_v4, size_v6, max_active_v4_flows, max_active_v6_flows, hash_table_partitions); /******************************/ /* Create the tasks pool. */ /******************************/ #if DPI_MULTICORE_USE_TASKS_POOL void* tmp; assert(posix_memalign((void**) &tmp, DPI_CACHE_LINE_SIZE, sizeof(ff::SWSR_Ptr_Buffer)+DPI_CACHE_LINE_SIZE)==0); state->tasks_pool=new (tmp) ff::SWSR_Ptr_Buffer( DPI_MULTICORE_TASKS_POOL_SIZE); state->tasks_pool->init(); #endif if(parallelism_form==MC_DPI_PARALLELISM_FORM_DOUBLE_FARM){ mc_dpi_create_double_farm(state, size_v4, size_v6); }else{ mc_dpi_create_single_farm(state, size_v4, size_v6); } state->is_running=0; state->stop_time.tv_sec=0; state->stop_time.tv_usec=0; return state; }