Exemplo n.º 1
0
/* ============================================================================
Function:     =        Mpi_Bitonic_sort
Purpose:      =        The root itteration logic for the bitonic sequence sort.
==============================================================================
Input arg:  =        1. my_rank: The size of the processors array.
            =        2. p: The size of the input array.
            =        3. my_list[]: A pointer to an array.
            =        4. neighbors_list[]: A pointer to an array.
            =        5. list_size: The size of the processors array.
            =        6. comm: The mpi communicator channel.
=========================================================================== */
void Mpi_Bitonic_sort(int my_rank, int p, int my_list[], int neighbors_list[],
                      int list_size, MPI_Comm comm)
{
    int partner_size, phase = 1;
    unsigned and_bit;

    for(partner_size = 2, and_bit = 2; partner_size <= p;
        partner_size = partner_size * 2, and_bit = and_bit << 1)
    {
        if((my_rank & and_bit) == 0)
        {
            #ifdef DEBUG
                Debugger(1, my_rank, phase, partner_size, my_list, p*list_size,
                            list_size);
            #endif
            Bitonic_sort_incr(my_rank, my_list, neighbors_list, list_size,
                                partner_size, comm);
        }
        else
        {
            #ifdef DEBUG
            Debugger(2, my_rank, phase, partner_size, my_list, p*list_size,
                        list_size);
            #endif
            Bitonic_sort_decr(my_rank, my_list, neighbors_list, list_size,
                                partner_size, comm);
        }
        #ifdef DEBUG
        Debugger(3, my_rank, phase, partner_size, my_list, p*list_size,
                list_size);
        #endif
        phase++;
    }

    if(my_rank == 0) printf("The list is now sorted. \n");
    Gather(my_list, p*list_size, my_rank, list_size);
}
Exemplo n.º 2
0
/*-------------------------------------------------------------------
 * Function:        Bitonic_sort
 * Purpose:         Implement bitonic sort of a list of ints
 * In arg:          rank
 * In globals:      barrier, thread_count, n (list size), list1
 * Out global:      l_a
 * Scratch globals: list2, l_b
 * Return val:      Ignored
 */
void *Bitonic_sort(void* rank) {
   long tmp = (long) rank;
   int my_rank = (int) tmp; 
   int local_n = n/thread_count;
   int my_first = my_rank*local_n;
// int my_last = my_first + local_n - 1;
   unsigned th_count, and_bit, dim;

   /* Sort my sublist */
   qsort(list1 + my_first, local_n, sizeof(int), Compare);  
   Barrier();
#  ifdef DEBUG
   if (my_rank == 0) Print_list("List after qsort", list1, n);
#  endif
   for (th_count = 2, and_bit = 2, dim = 1; th_count <= thread_count; 
         th_count <<= 1, and_bit <<= 1, dim++) {
      if ((my_rank & and_bit) == 0)
         Bitonic_sort_incr(th_count, dim, my_first, local_n, my_rank);
      else
         Bitonic_sort_decr(th_count, dim, my_first, local_n, my_rank);
   }

   return NULL;
}  /* Bitonic_sort */