// returns an array that is sorted up to a given number of swaps sparray almost_sorted_sparray(long s, long n, long nb_swaps) { sparray tmp = sparray(n); par::parallel_for(almost_sorted_sparray_contr, 0l, n, [&] (long i) { tmp[i] = (value_type)i; }); for (long i = 0; i < nb_swaps; i++) std::swap(tmp[random_index(2*i, n)], tmp[random_index(2*i+1, n)]); return tmp; }
// returns an array with exponential distribution of size n using seed s sparray exp_dist_sparray(long s, long n) { sparray tmp = sparray(n); int lg = log2_up(n)+1; par::parallel_for(exp_dist_sparray_contr, 0l, n, [&] (long i) { long range = (1 << (random_index(2*(i+s), lg))); tmp[i] = (value_type)hash64shift((long)(range+random_index(2*(i+s), range))); }); return tmp; }
random_graph_t() : m_graph(), m_fully_occupied_vertices_count(0) { memset(m_connections_per_vertex, 0, sizeof(m_connections_per_vertex)); while (m_fully_occupied_vertices_count < size) assign_edge(random_possible_edge()); /* * Now label edges */ for(size_t i = 0; i < size; ++i) { bool label_available[] = { true, true, true }; unsigned int labels_available_count = sizeof(label_available) / sizeof(bool); for(size_t j = 0; j < size; ++j) { for(typename connection_t::iterator it = m_graph[i][j].begin(); it != m_graph[i][j].end(); ++it) { bool *rit = random_index(label_available, label_available + sizeof(label_available) / sizeof(bool), labels_available_count, identity<bool>()); *rit = false; *it = connection_types[rit - label_available]; --labels_available_count; } } } }
// randomly pick an inactive polynomial in the act table // return -1 if all polys are active static int32_t random_inactive_poly(test_bench_t *bench) { active_poly_table_t *atbl; uint32_t n, k, max_iter; atbl = &bench->act; max_iter = 10; n = bench->ptable->npolys; k = random_index(n); while (atbl->active[k] && max_iter > 0) { max_iter --; k ++; if (k >= n) { k = 0; } } if (max_iter == 0) { return -1; } else { assert(!atbl->active[k]); return k; } }
SEXP random_index_R(SEXP max_index) { SEXP result; GetRNGstate(); PROTECT(result = allocVector(INTSXP, 1)); INTEGER(result)[0] = random_index(INTEGER(max_index)[0]); PutRNGstate(); UNPROTECT(1); return result; }
void bs2_benchmark() { Rng_t rng; Eng_t eng (0, 100); chrono_timer timer; boost::signals2::signal<void(Rng_t&)> signal_one; boost::signals2::signal<void(std::size_t, Rng_t&)> signal_two; // N = 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192 for (std::size_t N = 16; N <= 128; N *= 2) // magic numbers are globals { Width_t sum_previous = 1; Width_t sum_current = 2; Width_t sum_count = 0; // loop until wrap around occurs or until max count is reached while (sum_previous < sum_current && sum_count < 100) { // try to randomize access patterns std::vector<std::size_t> random_index(N); std::generate(random_index.begin(), random_index.end(), IncrementFill()); std::shuffle(random_index.begin(), random_index.end(), rng); // delay seems to increase timing accuracy sum_previous = sum_current; timer.delay(10); timer.reset(); { // time boost::signals2::trackable construction overhead std::vector<Bs2> obj(N); for (std::size_t index : random_index) { // what is the next operation? connect or emit if (eng(rng) > 50) { Bs2* ptr = &obj[index]; signal_one.connect(boost::bind(&Bs2::method_one, ptr, _1)); signal_two.connect(boost::bind(&Bs2::method_two, ptr, _1, _2)); } else { signal_one(rng); signal_two(index, rng); } } } sum_current += timer.elapsed_us(); ++sum_count; } // calculate milliseconds and output the relative performance double elapsed_ms = (sum_previous / (double)sum_count) * 0.001; std::cout << (N / elapsed_ms) << std::endl; } std::cout << Bs2::s_sum << std::endl; }
unsigned int random_available_vertex() const { const unsigned int *it = random_index(m_connections_per_vertex, m_connections_per_vertex + size, size - m_fully_occupied_vertices_count, std::bind2nd(std::not_equal_to<unsigned int>(), connections_per_vertex)); assert(it != m_connections_per_vertex + size); return (unsigned int) (it - m_connections_per_vertex); }
// pick a random active poly, then a random variable in this poly // return -1 if p is constant static int32_t var_of_poly(polynomial_t *p) { uint32_t i, n; int32_t x; n = p->nterms; if (n == 0 || (n == 1 && p->mono[0].var == const_idx)) { // p is constant x = -1; } else { if (p->mono[0].var == const_idx) { assert(n >= 2); i = 1 + random_index(n - 1); } else { i = random_index(n); } x = p->mono[i].var; assert(x != const_idx); } return x; }
SEXP random_function_symbol_of_arity(int arity, SEXP function_symbol_list, SEXP function_arities) { int matching_function_symbols[MAX_FUNCTION_SYMBOLS]; int number_of_matching_function_symbols = 0; for (int i = 0; i < length(function_arities); i++) { if (arity == INTEGER(function_arities)[i]) { matching_function_symbols[number_of_matching_function_symbols] = i; number_of_matching_function_symbols++; } } if (number_of_matching_function_symbols == 0) { error("random_function_symbol_of_arity: no function symbol of arity %d", arity); } int selected_function_symbol = matching_function_symbols[random_index(number_of_matching_function_symbols)]; return VECTOR_ELT(function_symbol_list, selected_function_symbol); }
// generate a set of n different ints between a and b static void fill_random_indices(int *idx, int n, int a, int b) { if (b-a==n) {for(int i=0;i<n;i++)idx[i]=a+i;} if (5*n > (b-a)) {fill_random_shuffle(idx, n, a, b);return;} // TODO fisher yates shuffle and traverse it by blocks of length nfit int safecount = 0; do { for (int i = 0; i < n; i++) idx[i] = random_index(a, b); safecount += 1; } while (safecount < 100 && !are_different(idx, n)); if (safecount == 100) fail("could not generate any model"); //fprintf(stderr, "fri"); //for (int i = 0; i < n; i++) // fprintf(stderr, "\t%d", idx[i]); //fprintf(stderr, "\n"); }
/* * Fill table with random polys: * - nvars = number of initial variables (must be positive) * poly[1 ... nvars] will all be variables * - n = total number of polynomials to build (including nvars) */ static void build_poly_table(poly_table_t *table, uint32_t nvars, uint32_t n) { poly_buffer_t buffer; polynomial_t *p; uint32_t i; for (i=0; i<nvars; i++) { add_poly(table, NULL); } init_poly_buffer(&buffer); while (i<n) { p = NULL; if (random_index(10) != 0) { p = random_poly(&buffer, table); } add_poly(table, p); i ++; } delete_poly_buffer(&buffer); }
static int32_t random_var_of_poly(test_bench_t *bench) { active_poly_table_t *atbl; polynomial_t *p; uint32_t i, n; int32_t id; atbl = &bench->act; n = atbl->npolys; id = -1; // in case n is 0 if (n > 0) { i = random_index(n); id = atbl->id[i]; assert(0 < id && id < bench->ptable->npolys); p = bench->ptable->poly[id]; // if p is NULL, then it represents the variable id // so we just return id. Otherwise, we pick a variable of p if (p != NULL) { id = var_of_poly(p); } } return id; }
char *pmasterkong_code(char* page, uuid_t u) { int i; char *result; result = (char *) malloc( strlen("33B8109DAD1100C04X") * sizeof(char)); char str[50], tmp[4]; snprintf(str, sizeof(str), "%s%8.8x", page, u.time_low); for (i = 3; i < 6; i++) { snprintf(tmp, 4, "%2.2x", u.node[i]); //printf("%s", tmp); strcat(str, tmp); //printf("%2.2x", u.node[i]); } int allowed_chars_max_idx = sizeof(allowed_chars) - 1; int allowed_chars2_max_idx = sizeof(allowed_chars2) - 1; for (i = 0; i < sizeof(str); i++) { str[i] = toupper(str[i]); // 不允许0、1、I、O出现 if (str[i] == '0') str[i] = allowed_chars[random_index(0, allowed_chars_max_idx)]; //'F'; if (str[i] == '1') str[i] = allowed_chars[random_index(0, allowed_chars_max_idx)]; //'T'; if (str[i] == 'I') str[i] = allowed_chars[random_index(0, allowed_chars_max_idx)]; //'L'; if (str[i] == 'O') str[i] = allowed_chars[random_index(0, allowed_chars_max_idx)]; //'K'; if (i > 0) { // 不允许N、M相邻出现或重复出现 if (str[i] == 'M' && str[i - 1] == 'M') str[i] = allowed_chars2[random_index(0, allowed_chars2_max_idx)]; //'P'; if (str[i] == 'M' && str[i - 1] == 'N') str[i] = allowed_chars2[random_index(0, allowed_chars2_max_idx)]; //'Q'; if (str[i] == 'N' && str[i - 1] == 'M') str[i] = allowed_chars2[random_index(0, allowed_chars2_max_idx)]; //'R'; if (str[i] == 'N' && str[i - 1] == 'N') str[i] = allowed_chars2[random_index(0, allowed_chars2_max_idx)]; //'S'; // 不允许V、W相邻出现或重复出现 if (str[i] == 'V' && str[i - 1] == 'V') str[i] = allowed_chars2[random_index(0, allowed_chars2_max_idx)]; //'X'; if (str[i] == 'V' && str[i - 1] == 'W') str[i] = allowed_chars2[random_index(0, allowed_chars2_max_idx)]; //'Y'; if (str[i] == 'W' && str[i - 1] == 'V') str[i] = allowed_chars2[random_index(0, allowed_chars2_max_idx)]; //'Z'; if (str[i] == 'W' && str[i - 1] == 'W') str[i] = allowed_chars2[random_index(0, allowed_chars2_max_idx)]; //'A'; // 不允许8、B相邻出现或重复出现 if (str[i] == '8' && str[i - 1] == 'B') str[i] = allowed_chars2[random_index(0, allowed_chars2_max_idx)]; //'C'; if (str[i] == 'B' && str[i - 1] == '8') str[i] = allowed_chars2[random_index(0, allowed_chars2_max_idx)]; //'D'; if (str[i] == '8' && str[i - 1] == '8') str[i] = allowed_chars2[random_index(0, allowed_chars2_max_idx)]; //'E'; if (str[i] == 'B' && str[i - 1] == 'B') str[i] = allowed_chars2[random_index(0, allowed_chars2_max_idx)]; //'F'; } } strcpy(result, str); return result; }
T sample(std::vector<T> const& v) { std::uniform_int_distribution<std::size_t> random_index(0, v.size()-1); return v[random_index(config::random_engine)]; }
/* * Random variable in table */ static inline int32_t random_var(poly_table_t *table) { return table->var[random_index(table->nvars)]; }
/* * Random test: give more weight to assert_eq and propagate * - if there's a conflict, try to resolve it first */ static void random_op(test_bench_t *bench) { uint32_t r; if (bench->mngr_conflict) { if (bench->decision_level > bench->base_level) { test_backtrack(bench); } else if (bench->base_level > 0) { test_pop(bench); } } else { r = random_index(15); switch (r) { case 0: case 1: case 2: case 3: case 4: random_assert_eq(bench); break; case 5: random_assert_eq(bench); case 6: random_assert_eq(bench); case 7: random_assert_eq(bench); case 8: test_propagate(bench); break; case 9: random_activate(bench, 1); break; case 10: case 11: // increase decision level: force propagate first test_propagate(bench); if (! bench->mngr_conflict) { test_increase_dlevel(bench); } break; case 12: // push if (bench->decision_level == bench->base_level) { test_push(bench); } break; case 13: // backtrack if (bench->decision_level > bench->base_level) { test_backtrack(bench); } break; case 14: // pop if (bench->base_level > 0) { test_pop(bench); } break; default: assert(false); break; } } }
static inline int32_t random_constant(void) { return constant[random_index(NCONST)]; }
static inline uint32_t random_nterms(void) { return nterms[random_index(NTERMS)]; }
static inline int32_t random_coeff(void) { return coeff[random_index(NCOEFF)]; }