DEF_TEST(Sort, reporter) { /** An array of random numbers to be sorted. */ int randomArray[500]; /** The reference sort of the random numbers. */ int sortedArray[SK_ARRAY_COUNT(randomArray)]; /** The random numbers are copied into this array, sorted by an SkSort, then this array is compared against the reference sort. */ int workingArray[SK_ARRAY_COUNT(randomArray)]; SkRandom rand; for (int i = 0; i < 10000; i++) { int count = rand.nextRangeU(1, SK_ARRAY_COUNT(randomArray)); rand_array(rand, randomArray, count); // Use qsort as the reference sort. memcpy(sortedArray, randomArray, sizeof(randomArray)); qsort(sortedArray, count, sizeof(sortedArray[0]), compare_int); memcpy(workingArray, randomArray, sizeof(randomArray)); SkTHeapSort<int>(workingArray, count); check_sort(reporter, "Heap", workingArray, sortedArray, count); memcpy(workingArray, randomArray, sizeof(randomArray)); SkTQSort<int>(workingArray, workingArray + count - 1); check_sort(reporter, "Quick", workingArray, sortedArray, count); } }
int main(void) { scheduler_begin(); struct array * A = rand_array(1000000); seq_threshold = 100; printf("before sort: %s\n", check_sort(A)); par_mergesort(A); printf("after sort: %s\n", check_sort(A)); scheduler_end(); return 0; }
extern "C" void _start ( ) { int length = sizeof(global_arr) / sizeof(int); heapify(global_arr, length); sort(global_arr, length); for (int i = 0; i < TEST_ARRAY_SIZE; i++) { test_array[i] = TEST_ARRAY_SIZE - i; } heapify(test_array, TEST_ARRAY_SIZE); sort(test_array, TEST_ARRAY_SIZE); _exit(check_sort(global_arr, length) && check_sort(test_array, TEST_ARRAY_SIZE) ? 0 : -1); }
extern "C" void _start ( ) { int array[] = {499, -688, -758, -871, -30, 37, -844, 99, -637, -24, 1, -813, -289, 719, 477, 579, -212, -250, 790, -620, 298, -358, 296, 354, 165, 840, 85, -818, 435, 959, 637, 389, 441, 528, 172, -735, -605, -264, -863, 425, -451, 985, 248, 233, -581, 472, 761, 939, 41, -152, 739, 603, -313, -97, -863, -649, -866, 733, 767, -771, -271, -905, -778, 302, 431, -680, 824, -779, -590, 531, 248, -811, 118, 213, 140, -343, 626, -140, 183, -218, 879, -101, -463, 413, -677, 357, 444, -361, -271, 653, -695, 938, -582, -800, 752, -887, -584, 692, 561, 665, 120, -41, 894, 134, 767, -585, -2, -548, -827, -670, 420, -334, -465, 667, 398, -940, 240, 144, 918, -467, 504, -645, 831, 929, -661, 551, 949, 960, 340, 330, 678, 492, -295, 527, -879, 814, -503, 824, -886, 104, -665, 511, 535, -170, 798, 157, 347, -130, 418, -902, -428, 134, 619, 986, 263, -655, -504, 343, -948, -810, -282, 899, 361, 288, 241, 308, 688, 984, -345, 766, -51}; int* aux_array; int length = sizeof(array) / sizeof(int); aux_array = new int[((sizeof(array) / sizeof(int)) / 2 + 1)]; if (!aux_array) { _exit(-1); } sort(array, length, aux_array); _exit(check_sort(array, length) ? 0 : -1); }
void basic_sort(t_box *box) { while (box->size_a != 0) { box->locat = little_number(A, box->size_a, &box->val_min); if (A[0] > A[1] && A[1] < A[2]) swap_a(box); if (check_sort(A, box->size_a)) break ; if (box->locat < (box->size / 2)) while (A[0] != box->val_min) rot_a(box); else while (A[0] != box->val_min) revrot_a(box); push_b(box); } while (box->size_b != 0) push_a(box); }
void test_sort(t_box *box) { if (A[0] > A[1] && A[1] > A[2]) { rot_a(box); swap_a(box); } else if (A[0] > A[1] && A[1] < A[2]) swap_a(box); if (A[box->size - 2] > A[box->size - 1]) { revrot_a(box); revrot_a(box); swap_a(box); rot_a(box); rot_a(box); } else if (A[0] > A[1] && !(check_sort(A, box->size))) rot_a(box); }
int *ra(int *l_a, int k, int kb) { int tmp; int i; int j; tmp = l_a[0]; i = 1; j = 0; while (i < k) { l_a[j] = l_a[i]; i++; j++; } l_a[j] = tmp; my_printf("ra"); if (check_sort(l_a, k) != 1 || kb != 0) my_putchar(' '); return (l_a); }
void pre_sort(t_box *box) { int error; int locat; error = amount_error(box, &locat); if (error == 0) return ; if (A[0] > A[1] && locat == 0 && A[box->size - 2] > A[box->size - 1]) swap_a(box); else if (locat >= (box->size / 2) && error == 1) test_sort(box); else if (A[0] > A[1] && error == 1) test_sort(box); else if (box->size <= 3) test_sort(box); if (!(check_sort(A, box->size))) basic_sort(box); if (box->b_op == 1) print_nbop(box); }
void operator()(var * n) { if (!m_quantifiers) fail("logic does not support quantifiers"); check_sort(m.get_sort(n)); }
int main(int argc, char* argv[]) { if (argc != 3) { fprintf(stderr, "%s\n", usage); return 0; } /* Initialisation */ MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); MPI_Comm_size(MPI_COMM_WORLD, &nb_proc); MPI_Status status; int nb_elem = atoi(argv[1]); // nombre d'éléments total int sort_type = atoi(argv[2]); // type de l'algorithme de tri k = nb_elem / nb_proc; int *tab_sort = (int *)malloc(k*sizeof(int)); int *tab_tmp = (int *)malloc(k*sizeof(int)); if ((tab_tmp == NULL) || (tab_sort == NULL)) { fprintf(stderr, "Erreur allocation mémoire du tableau \n"); MPI_Finalize(); exit(1); } int left = my_rank-1; int right = my_rank+1; // initialise le tableau local P0 printf("Initialisation du tableau..."); switch (sort_type) { case 1: case 2: init_rand(tab_tmp, k); break; case 3: case 4: case 5: init_rand(tab_sort, k); break; } P0 printf(" OK!\n"); P0 printf("Calcul...\n"); // début du chronométrage double start, end; start = MPI_Wtime(); // choix de l'algorithme de tri switch (sort_type) { case 1: PRAM(tab_tmp, tab_sort); break; case 2: PRAM_omp(tab_tmp, tab_sort); break; case 3: quick_sort(tab_sort, k); break; case 4: quick_sort_omp(tab_sort, k); break; case 5: qsort(tab_sort, k, sizeof(int), compare); break; } // tri pair-impair int step; for (step = 0; step < nb_proc; step++) { if ((my_rank%2) - (step%2) == 0) { if (my_rank != nb_proc-1) { MPI_Recv(tab_tmp, k, MPI_INT, right, TAG_TAB, MPI_COMM_WORLD, &status); merge_sort(tab_sort, tab_tmp); MPI_Send(tab_tmp, k, MPI_INT, right, TAG_TAB, MPI_COMM_WORLD); } } else { if (my_rank != 0) { MPI_Send(tab_sort, k, MPI_INT, left, TAG_TAB, MPI_COMM_WORLD); MPI_Recv(tab_sort, k, MPI_INT, left, TAG_TAB, MPI_COMM_WORLD, &status); } } } // fin du chronométrage end = MPI_Wtime(); print_results(end - start); // écriture dans le fichier if (nb_elem <= NMAX) { P0 printf("Ecriture du fichier..."); MPI_File file; MPI_Offset my_offset; char filename[strlen("/Vrac/ppar_cassat_ducamain_sort")+1]; strcpy(filename, "/Vrac/ppar_cassat_ducamain_sort"); my_offset = my_rank * sizeof(int) * k; MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &file); MPI_File_write_at(file, my_offset, tab_sort, k, MPI_INT, &status); // printf("(%d) a écrit: ", my_rank); // print_tab(tab_sort, k); // attends que tous aient écrit MPI_Barrier(MPI_COMM_WORLD); MPI_File_read_ordered(file, tab_sort, k, MPI_INT, &status); MPI_File_close(&file); // printf("(%d) a lu: ", my_rank); // print_tab(tab_sort, k); P0 printf(" OK!\n"); } // Vérification du tri P0 printf("Vérification du tri...\n"); if (!check_sort(tab_sort, k) && (my_rank == 0)) printf("\tTri correct!\n"); #ifdef BENCH P0 fprintf(stderr, "\t%d\t%d\t%d", sort_type, nb_elem, nb_proc); #ifdef _OPENMP #pragma omp parallel { if ((my_rank == 0) && (omp_get_thread_num() == 0)) fprintf(stderr, "\t%d", omp_get_num_threads()); } #endif P0 fprintf(stderr, "\n"); #endif free(tab_sort); free(tab_tmp); /* Desactivation */ MPI_Finalize(); return 0; }