long benchmark(int size) { int m = sqrt(size); long requestStart, requestEnd; // random matrices are full rank (and can always be inverted if square) // http://www.sciencedirect.com/science/article/pii/S0096300306009040 double* a = random_array(m * m); int bSize = m * m; double* b = calloc(bSize, sizeof(double)); int* p = calloc(m, sizeof(int)); int info = 0; requestStart = currentTimeNanos(); // calling raw fortran because OS X doesn't have LAPACKE dgetrf_( &m, &m, a, &m, p, &info ); dgetri_( &m, a, &m, p, b, &bSize, &info ); requestEnd = currentTimeNanos(); free(a); free(b); free(p); return (requestEnd - requestStart); }
// MAIN void main (void) { int vector[MAX], cont; bool valor; clrscr (); init_array (vector); random_array (vector, 10); for (cont = 1; cont < MAX; cont++) printf ("%2d ", vector[cont]); valor = EsMonticulo (vector); if (valor == NO) CreaMonticulo (vector); monty (vector); printf ("\n\n"); for (cont = 1; cont <= max; cont++) printf ("%2d ", vector[cont]); getch (); }
GoodType nofMetalworker::GetRandomTool() { // Je nach Werkzeugeinstellungen zufällig ein Werkzeug produzieren, je größer der Balken, // desto höher jeweils die Wahrscheinlichkeit unsigned short all_size = 0; for(unsigned int i = 0; i < TOOL_COUNT; ++i) all_size += gwg->GetPlayer(player).toolsSettings_[i]; // if they're all zero if(all_size == 0) { // do nothing if addon is enabled, otherwise produce random ware (orig S2 behaviour) if (GAMECLIENT.GetGGS().isEnabled(ADDON_METALWORKSBEHAVIORONZERO) && GAMECLIENT.GetGGS().getSelection(ADDON_METALWORKSBEHAVIORONZERO) == 1) return GD_NOTHING; else return TOOLS_SETTINGS_IDS[RANDOM.Rand(__FILE__, __LINE__, GetObjId(), 12)]; } // Ansonsten Array mit den Werkzeugtypen erstellen und davon dann eins zufällig zurückliefern, je höher Wahr- // scheinlichkeit (Balken), desto öfter im Array enthalten std::vector<unsigned char> random_array(all_size); unsigned curIdx = 0; for(unsigned int i = 0; i < TOOL_COUNT; ++i) { for(unsigned g = 0; g < gwg->GetPlayer(player).toolsSettings_[i]; ++g) random_array[curIdx++] = i; } GoodType tool = TOOLS_SETTINGS_IDS[random_array[RANDOM.Rand(__FILE__, __LINE__, GetObjId(), all_size)]]; return tool; }
double test_priority_queue() { vector<int> ary = random_array(); priority_queue<int> que; Timer timer; for (auto i: ary) que.push(i); while (!que.empty()) que.pop(); return timer.stop() / (array_len * 2); }
long benchmark(int size) { long requestStart, requestEnd; double* a = random_array(size); double* b = random_array(size); requestStart = currentTimeNanos(); cblas_ddot(size, a, 1, b, 1); requestEnd = currentTimeNanos(); free(a); free(b); return (requestEnd - requestStart); }
int main(void) { int a[20]; random_array(a, 20); print_array(a, 20); bucket_sort(a, 20); print_array(a, 20); return 0; }
double compare_1d(unsigned n, int distrib) { auto r = random_array(n, distrib); TH1I h("", "", 100, 0, 1); double t = clock(); for (auto it = r.get(), end = r.get() + n; it != end;) h.Fill(*it++); t = (clock() - t) / CLOCKS_PER_SEC / n * 1e9; assert(distrib != 0 || h.GetSumOfWeights() == n); return t; }
int main() { int a[13]; int n = sizeof(a)/sizeof(a[0]); random_array(a, n); print_array(a, n); heap_sort(a, n); print_array(a, n); return 0; }
int main(){ int * array = NULL; array = create_array(); random_array(array); print_array(array); insert_sort(array); print_array(array); free_array(array); return 0; }
int main(int argc, char **argv) { const int maxThreads = omp_get_max_threads(); if (argc < 4) { fprintf(stderr, "Usage: bench <csv file> <input size> <num threads> [<num threads> ...]\n"); return -1; } FILE *const csvFile = csv_open(argv[1]); if (csvFile == NULL) { return -1; } const int len = safe_strtol(argv[2]); if (len < 1) { fprintf(stderr, "Input size must be positive\n"); return -1; } TYPE *nrs = random_array(len, time(NULL)); if (nrs == NULL) { return -1; } for (int i = 3; i < argc; i++) { int threads = safe_strtol(argv[i]); if (threads < 1) { threads = maxThreads; } omp_set_num_threads(threads); printf("%s. omp_get_max_threads() == %d\n", algorithm_name, threads); /* Bench the parallel implementation. */ double start = omp_get_wtime(); if (prefix_sums(nrs, len, NULL) != 0) { return -1; } double par_time = omp_get_wtime() - start; printf("elements: %d; par time: %f\n\n", len, par_time); fprintf(csvFile, "%s,%d,%d,%f\n", algorithm_name, threads, len, par_time); } free(nrs); csv_close(csvFile); return 0; }
double compare_2d(unsigned n, int distrib) { auto r = random_array(n, distrib); TH2I h("", "", 100, 0, 1, 100, 0, 1); double t = clock(); for (auto it = r.get(), end = r.get() + n; it != end;) { const auto x = *it++; const auto y = *it++; h.Fill(x, y); } t = (double(clock()) - t) / CLOCKS_PER_SEC / n * 1e9; assert(distrib != 0 || h.GetSumOfWeights() == n / 2); return t; }
double compare_6d(unsigned n, int distrib) { auto r = random_array(n, distrib); int bin[] = {10, 10, 10, 10, 10, 10}; double min[] = {0, 0, 0, 0, 0, 0}; double max[] = {1, 1, 1, 1, 1, 1}; THnI h("", "", 6, bin, min, max); double t = clock(); for (auto it = r.get(), end = r.get() + n; it != end; it += 6) h.Fill(it); t = (double(clock()) - t) / CLOCKS_PER_SEC / n * 1e9; TH1D* h1 = h.Projection(0); assert(distrib != 0 || h1->GetSumOfWeights() == n / 6); delete h1; return t; }
long benchmark(int size) { long requestStart, requestEnd; int incx = 1, incy = 1, n = size; double *cuA, *cuB; cublasStatus status; double* a = random_array(size); double* b = random_array(size); status = cublasAlloc(n, sizeof(double),(void**)&cuA); checkStatus("A", status); status = cublasAlloc(n, sizeof(double),(void**)&cuB); checkStatus("B", status); status = cublasSetVector(n, sizeof(double), a, incx, cuA, incx); checkStatus("setA", status); status = cublasSetVector(n, sizeof(double), b, incy, cuB, incy); checkStatus("setB", status); requestStart = currentTimeNanos(); cublasDdot(n, cuA, incx, cuB, incy); requestEnd = currentTimeNanos(); status = cublasFree(cuA); checkStatus("freeA", status); status = cublasFree(cuB); checkStatus("freeB", status); free(a); free(b); return (requestEnd - requestStart); }
int test_quicksort(int argc, char *argv[]) { int len; if (argc > 1) { len = atoi(argv[1]); } else { len = 1000; } if (len > 0) { int *arr = random_array(len); qsort(arr, len, sizeof(int), int_cmp); } else { fprintf(stderr, "Can't sort 0 length array.\n"); } }
int main(void) { int a[ARRAY_SIZE]; random_array(a,ARRAY_SIZE,1000); puts("before sort:"); print_array(a,ARRAY_SIZE); merge_sort(a,ARRAY_SIZE); assert_assend_order(a,ARRAY_SIZE); puts(" after sort:"); print_array(a,ARRAY_SIZE); return 0; }
int test_msort(int argc, char *argv[]) { int len; if (argc > 1) { len = atoi(argv[1]); } else { len = 1000; } if (len > 0) { int *arr = random_array(len); int *sorted = msort(arr, arr + len - 1); print_array(sorted, len); free(sorted); } else { fprintf(stderr, "Can't sort 0 length array.\n"); } }
void test_create (int index, int ** input, int ** output) { int jj; if (0 == index) { *input = malloc (10 * sizeof(int)); if (NULL == output) { err (EXIT_FAILURE, __FILE__": %s: malloc", __func__); } for (jj = 0; jj < 10; ++jj) { (*input)[jj] = jj; } } else if (1 == index) { *input = malloc (10 * sizeof(int)); if (NULL == output) { err (EXIT_FAILURE, __FILE__": %s: malloc", __func__); } for (jj = 0; jj < 10; ++jj) { (*input)[jj] = 9 - jj; } } else if (2 == index) { *input = malloc (10 * sizeof(int)); if (NULL == output) { err (EXIT_FAILURE, __FILE__": %s: malloc", __func__); } for (jj = 0; jj < 10; ++jj) { (*input)[jj] = jj % 2; } } else { *input = random_array (0, 9, 10); } *output = malloc (10 * sizeof(int)); if (NULL == output) { err (EXIT_FAILURE, __FILE__": %s: malloc", __func__); } memcpy (*output, *input, 10 * sizeof(int)); }
/*测试某数据是否在有序数组里面*/ void bintest(int len){ item_t x; item_t *arr=malloc(sizeof(item_t)*len); /*随机生成数据,并进行排序*/ random_array(arr,len,1000000); qsort(arr,len,sizeof(item_t),item_cmp); x.key=5677; printf("Searching the key %d: ",x.key); stimer_start(); if(binsearch(&x,arr,len,sizeof(item_t),item_cmp)){ printf("\t Found! \n"); } else{ printf("\t Not Found! \n"); } stimer_stop(); printf("%5.1f\n",stimer_time_total()); free(arr); }
void compare_algo(int len,int tests){ item_t *arr=malloc(sizeof(item_t)*len); /*随机生成数据,并进行排序*/ random_array(arr,len,1000000); qsort(arr,len,sizeof(item_t),item_cmp); double total1=0.0; double total2=0.0; int i; for(i=0;i<len;i++){ total2 +=multitests("binsearch",arr,len,arr[i],tests); total1 +=multitests("bsearch",arr,len,arr[i],tests); } printf("bsearch = %.2f\n",total1/tests); printf("binsearch = %.2f\n",total2/tests); }
int main (int argc, char ** argv) { int dmin = 0; int dmax = 1000; int nstart = 20; int nmax = 30000; double nfac = 1.2; double nd; int * data; int * dup; /* Allocate one big random input data array. For varying the and making sure we always start with random input, we will use a duplicate (with just the first N entries) at each iteration. */ printf ("# generating input data (this can take a while...)\n"); fflush (stdout); data = random_array (dmin, dmax, nmax); dup = malloc (nmax * sizeof(int)); if (NULL == dup) { err (EXIT_FAILURE, __FILE__": %s: malloc", __func__); } printf ("################################################\n" "#\n" "# Cocktail sort runtime measurements\n" "#\n" "# column 1: N\n" "# column 2: Random array T [ms]\n" "# column 3: ascended array as input [ms]\n" "# column 4: Descended array as in put [ms]\n" "#\n"); /* Main benchmarking loop. Note that we use a floating point "length" so that we can easily create a geometric series for N. This spaces the sampled array sizes more densely for small N, and for big N (where things usually take much longer) we don't take so many samples (otherwise running the benchmark takes a really long time). */ //int tempnum; for (nd = nstart; nd <= nmax; nd *= nfac) { double temp0 = 0, temp1 = 0, temp2 = 0; int nn; double tstart2, tstop2, tstart1 , tstop1 , tstart0, tstop0; /* convert the floating point "length" to an integer */ nn = nd; // printf ("%8d", nn); fflush (stdout); /* use a fresh duplicate from the random input data */ memcpy (dup, data, nn * sizeof(int)); //=========== random input =================== tstart0 = clockms (); cocktail (dup, nn); tstop0 = clockms (); temp0 = tstop0 - tstart0; //========== Asc sort as input =============== bubbAsc (dup, nn); tstart1 = clockms (); cocktail (dup, nn); tstop1 = clockms (); temp1 = tstop1 - tstart1; //=========== Des sort as input ============== bubbDes (dup, nn); tstart2 = clockms (); cocktail (dup, nn); tstop2 = clockms (); temp2 = tstop2 - tstart2; // printf (" %8g", tstop - tstart); printf (" %8d %8g %8g %8g\n",nn,temp0, temp1 ,temp2); } /* End of the program. */ free (data); free (dup); return 0; }
int main() { int32_t *a; int32_t n, j; a = (int32_t *) safe_malloc(1000 * sizeof(int32_t)); constant_array(a, 20); printf("input: "); print_array(a, 20); insertion_sort(a, 20); printf("isort: "); print_array(a, 20); printf("\n"); increasing_array(a, 20); printf("input: "); print_array(a, 20); insertion_sort(a, 20); printf("isort: "); print_array(a, 20); printf("\n"); decreasing_array(a, 20); printf("input: "); print_array(a, 20); insertion_sort(a, 20); printf("isort: "); print_array(a, 20); printf("\n"); for (n=0; n<20; n++) { for (j=0; j<10; j++) { random_array(a, n); printf("input: "); print_array(a, n); insertion_sort(a, n); printf("isort: "); print_array(a, n); printf("\n"); check_sorted(a, n); } } total1 = 0.0; total2 = 0.0; total3 = 0.0; total4 = 0.0; total4var = 0.0; total4var2 = 0.0; itotal = 0.0; for (n=0; n<100; n ++) { time1 = 0.0; time2 = 0.0; time3 = 0.0; time4 = 0.0; time4var = 0.0; time4var2 = 0.0; itime = 0.0; printf("size %"PRId32"\n", n); fflush(stdout); for (j=0; j<100; j++) { random_array(a, n); compare(a, n); if (j % 10 == 9) { printf("."); fflush(stdout); } } printf("\nisort: %.3f s\n", itime); printf("sort1: %.3f s\n", time1); printf("sort2: %.3f s\n", time2); printf("sort3: %.3f s\n", time3); printf("sort4: %.3f s\n", time4); printf("sort4var: %.3f s\n", time4var); printf("sort4var2: %.3f s\n", time4var2); printf("\n"); total1 += time1; total2 += time2; total3 += time3; total4 += time4; total4var += time4var; total4var2 += time4var2; itotal += itime; } printf("Total time\n"); printf("isort: %.3f s\n", itotal); printf("sort1: %.3f s\n", total1); printf("sort2: %.3f s\n", total2); printf("sort3: %.3f s\n", total3); printf("sort4: %.3f s\n", total4); printf("sort4var: %.3f s\n", total4var); printf("sort4var2: %.3f s\n", total4var2); safe_free(a); return 0; }
int main(int argc, char **argv) { int array_size = atoi(argv[1]); int threads = atoi(argv[2]); int buckets_size = threads; float *array = random_array(array_size); struct bucket *buckets = malloc(sizeof(struct bucket) * buckets_size); int i,j; double start, end; for(i=0; i<buckets_size; ++i) { buckets[i].array = malloc(sizeof(float) * array_size); buckets[i].size = 0; } omp_set_num_threads(threads); start = omp_get_wtime(); #pragma omp parallel for default(none) shared(buckets_size, buckets, array_size, array) private(i,j) for(i=0; i<buckets_size; ++i) { struct bucket *current = &buckets[i]; float upper = (float) (i + 1) / (float) buckets_size; float lower = (float) i / (float) buckets_size; for(j=0; j< array_size; ++j) { float value = array[j]; if (lower <= value && value < upper) { current->array[current->size++] = array[j]; } } } #pragma omp parallel for default(none) shared(buckets_size, buckets) private(i) for(i=0; i<buckets_size; ++i) { struct bucket *current = &buckets[i]; qsort(current->array, current->size, sizeof(float), compare); } for(i=1; i<buckets_size; ++i) { struct bucket *first = &buckets[i-1]; struct bucket *next = &buckets[i]; next->size += first->size; } #pragma omp parallel for default(none) shared(buckets_size, buckets, array) private(i) for(i=0; i<buckets_size; ++i) { struct bucket *current = &buckets[i]; float *bucket_array = current->array; int upper = current->size; int lower = 0; int k = 0; if(i != 0) { lower = buckets[i-1].size; } while(lower < upper) { array[lower] = bucket_array[k]; ++k; ++lower; } } end = omp_get_wtime(); printf("%d %d %lf\n", array_size, threads, end - start); //check_array(array, array_size); //print_array(array,array_size); return 0; }
int main(int argc, char **argv) { int ret = 0; MPI_Init(&argc, &argv); if (argc < 4) { fprintf(stderr, "Usage: bench <csv file> <input size> <input upper bound>\n"); ret = -1; goto out; } const int size = safe_strtol(argv[2]); if (size < 1) { fprintf(stderr, "Input size must be greater than 0\n"); ret = -1; goto out; } const int upper_bound = safe_strtol(argv[3]); if (size < 1) { fprintf(stderr, "Input upper bound must be greater than 0\n"); ret = -1; goto out; } int processes; MPI_Comm_size(MPI_COMM_WORLD, &processes); int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* We need to generate the same array in each process. The master process * determines a seed and broadcasts it to all others. */ int seed = time(NULL); MPI_Bcast(&seed, 1, MPI_INT, MASTER, MPI_COMM_WORLD); TYPE *a = random_array(size, upper_bound, seed); DEBUG("%s. MPI_Comm_size %d, MPI_Comm_rank %d, seed %d\n", algorithm_name, processes, rank, seed); /* Everything is set up, start sorting and time how long it takes. */ MPI_Barrier(MPI_COMM_WORLD); double start = MPI_Wtime(); TYPE *c = bucket_sort(a, size, upper_bound); double end = MPI_Wtime(); double localElapsed = end - start; double totalElapsed; MPI_Reduce(&localElapsed, &totalElapsed, 1, MPI_DOUBLE, MPI_MAX, MASTER, MPI_COMM_WORLD); free(a); free(c); /* Only the master process (rank 0) outputs information. */ if (rank == MASTER) { printf("processes: %d, elements: %d; upper bound: %d; time: %f\n", processes, size, upper_bound, totalElapsed); /* Persist this run in our csv file. */ FILE *const csvFile = csv_open(argv[1]); if (csvFile == NULL) { return -1; } fprintf(csvFile, "%s,%d,%d,%f\n", algorithm_name, processes, size, totalElapsed); csv_close(csvFile); } out: MPI_Finalize(); return ret; }
/** * Returns a container of random numbers to be accessed as an array. * @param[in] size The size of the container. * @return Random numbers' container. * * @caveat This should be used as a global operation to keep the * the internal state of the context synchronized. */ skylark::utility::random_array_t allocate_random_array(size_t size) { skylark::utility::random_array_t random_array(_counter, size, _seed); _counter += size; return random_array; }