Exemplo n.º 1
0
/*-----------------------------------------------------------------*/
int main(int argc, char* argv[]) {
   int  n;
   char g_i;
   int* a;
   double start, finish;

   Get_args(argc, argv, &n, &g_i);
   a = malloc(n*sizeof(int));
   if (g_i == 'g') {
      Generate_list(a, n);
#     ifdef DEBUG
      Print_list(a, n, "Before sort");
#     endif
   } else {
      Read_list(a, n);
   }

   start = omp_get_wtime();
   Odd_even(a, n);
   finish = omp_get_wtime();

#  ifdef DEBUG
   Print_list(a, n, "After sort");
#  endif
   
   printf("Elapsed time = %e seconds\n", finish - start);

   free(a);
   return 0;
}  /* main */
Exemplo n.º 2
0
/*-------------------------------------------------------------------
 * Function:      Bitonic_sort_decr
 * Purpose:       Use parallel bitonic sort to sort a list into
 *                   decreasing order.  This implements a butterfly
 *                   communication scheme among the threads
 * In args:       th_count:  the number of threads participating
 *                   in this sort
 *                dim:  base 2 log of th_count
 *                my_first:  the subscript of my first element in l_a
 *                local_n:  the number of elements assigned to each
 *                   thread
 *                 my_rank:  the calling thread's global rank
 * In/out global:  l_a pointer to current list.
 * Scratch global: l_b pointer to temporary list.
 */
void Bitonic_sort_decr(int th_count, int dim, int my_first, int local_n,
      int my_rank) {
   int stage;
   int partner;
   int* tmp;
   unsigned eor_bit = 1 << (dim - 1);

   for (stage = 0; stage < dim; stage++) {
      partner = my_rank ^ eor_bit;
      if (my_rank > partner)
         Merge_split_lo(my_rank, my_first, local_n, partner);
      else
         Merge_split_hi(my_rank, my_first, local_n, partner);
      eor_bit >>= 1;
      Barrier();
      if (my_rank == 0) {
#        ifdef DEBUG
         char title[1000];
#        endif
         tmp = l_a;
         l_a = l_b;
         l_b = tmp;
#        ifdef DEBUG
         sprintf(title, "Th_count = %d, stage = %d", th_count, stage);
         Print_list(title, l_a, n);
#        endif
      }
      Barrier();
   } 
       
}  /* Bitonic_sort_decr */
Exemplo n.º 3
0
/*-----------------------------------------------------------------
 * Function:     Odd_even
 * Purpose:      Sort list using odd-even transposition sort
 * In args:      n
 * In/out args:  a
 */
void Odd_even(int a[], int n) {
   int phase, i, tmp;
#  ifdef DEBUG
   char title[100];
#  endif

   for (phase = 0; phase < n; phase++) {
      if (phase % 2 == 0)
#        pragma omp parallel for num_threads(thread_count) \
            default(none) shared(a, n) private(i, tmp)
         for (i = 1; i < n; i += 2) {
            if (a[i-1] > a[i]) {
               tmp = a[i-1];
               a[i-1] = a[i];
               a[i] = tmp;
            }
         }
      else
#        pragma omp parallel for num_threads(thread_count) \
            default(none) shared(a, n) private(i, tmp)
         for (i = 1; i < n-1; i += 2) {
            if (a[i] > a[i+1]) {
               tmp = a[i+1];
               a[i+1] = a[i];
               a[i] = tmp;
            }
         }
#     ifdef DEBUG
      sprintf(title, "After phase %d", phase);
      Print_list(a, n, title);
#     endif
   }
}  /* Odd_even */
Exemplo n.º 4
0
/*--------------------------------------------------------------------*/
int main(int argc, char* argv[]) {
   long       thread;
   pthread_t* thread_handles; 
   double     start, finish;
   int        gen_list, output_list;

   Get_args(argc, argv, &gen_list, &output_list);

   thread_handles = malloc (thread_count*sizeof(pthread_t));
// pthread_barrier_init(&barrier, NULL, thread_count);
   pthread_mutex_init(&bar_mutex, NULL);
   pthread_cond_init(&bar_cond, NULL);
   list1 = malloc(n*sizeof(int));
   list2 = malloc(n*sizeof(int));
   l_a = list1;
   l_b = list2;

   if (gen_list)
      Gen_list(list1, n);
   else
      Read_list("Enter the list", list1, n);
   if (output_list)
      Print_list("The input list is", list1, n);

   GET_TIME(start);
   for (thread = 0; thread < thread_count; thread++)
      pthread_create(&thread_handles[thread], NULL,
          Bitonic_sort, (void*) thread);

   for (thread = 0; thread < thread_count; thread++) 
      pthread_join(thread_handles[thread], NULL);
   GET_TIME(finish);
   printf("Elapsed time = %e seconds\n", finish - start);

   if (output_list)
      Print_list("The sorted list is", l_a, n);

   free(list1);
   free(list2);
// pthread_barrier_destroy(&barrier);
   pthread_mutex_destroy(&bar_mutex);
   pthread_cond_destroy(&bar_cond);
   free(thread_handles);
   return 0;
}  /* main */
Exemplo n.º 5
0
/*-------------------------------------------------------------------
 * Function:   Print_local_lists
 * Purpose:    Print each process' current list contents
 * Input args: all
 * Notes:
 * 1.  Assumes all participating processes are contributing local_n 
 *     elements
 */
void Print_local_lists(int local_A[], int local_n, 
         int my_rank, int p, MPI_Comm comm) {
   int*       A;
   int        q;
   MPI_Status status;

   if (my_rank == 0) {
      A = (int*) malloc(local_n*sizeof(int));
      Print_list(local_A, local_n, my_rank);
      for (q = 1; q < p; q++) {
         MPI_Recv(A, local_n, MPI_INT, q, 0, comm, &status);
         Print_list(A, local_n, q);
      }
      free(A);
   } else {
      MPI_Send(local_A, local_n, MPI_INT, 0, 0, comm);
   }
}  /* Print_local_lists */
Exemplo n.º 6
0
/*-----------------------------------------------------------------*/
int main(int argc, char* argv[]) {
   long  n;
   char g_i;
   long* a;

   Get_args(argc, argv, &n, &g_i);
   a = malloc(n*sizeof(long));
   if (g_i == 'g') {
      Generate_list(a, n);
      Print_list(a, n, "Before sort");
   } else {
      Read_list(a, n);
   }

   Bubble_sort(a, n);

   Print_list(a, n, "After sort");
   
   free(a);
   return 0;
}  /* main */
Exemplo n.º 7
0
int main(int argc, char* argv[])
{
	//struct Player *player = Create_player("Hessu Hopo", 609);
	//Print_player(player);
	//free(player);
	struct Player_list *list = Create_list();
	Add_player("Hessu Hopo", 609, list);
	Add_player("Hessu 23", 333, list);
	Print_list(list);
	free(list);
	return 0;	
}
Exemplo n.º 8
0
/*-----------------------------------------------------------------*/
int main(int argc, char* argv[]) {
    int  n;
    char g_i;
    int* a;

    Get_args(argc, argv, &n, &g_i);
    a = (int*) malloc(n*sizeof(int));
    if (g_i == 'g') {
        Generate_list(a, n);
        Print_list(a, n, "Before sort");
    } else {
        Read_list(a, n);
    }

    Odd_even_sort(a, n);

    Print_list(a, n, "After sort");

    free(a);
    return 0;
}  /* main */
Exemplo n.º 9
0
main(int argc, char* argv[]) {
    LOCAL_LIST_T  local_keys;
    int           list_size;
    int           error;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &p);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    MPI_Comm_dup(MPI_COMM_WORLD, &io_comm);
    Cache_io_rank(MPI_COMM_WORLD, io_comm);

    list_size = Get_list_size();

    /* Return negative if Allocate failed */
    error = Allocate_list(list_size, &local_keys);

    Get_local_keys(&local_keys);
    Print_list(&local_keys);
    Redistribute_keys(&local_keys);
    Local_sort(&local_keys);
    Print_list(&local_keys);

    MPI_Finalize();
} /* main */
Exemplo n.º 10
0
main(int argc, char* argv[]) {
    int       list_size;         /* Local list size  */
    int       n;                 /* Global list size */
    KEY_T     local_list[MAX];
    int       proc_set_size;
    int       my_rank;
    int       p;
    unsigned  and_bit;
    MPI_Comm  io_comm;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &p);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    MPI_Comm_dup(MPI_COMM_WORLD, &io_comm);
    Cache_io_rank(MPI_COMM_WORLD, io_comm);

    Cscanf(io_comm,"Enter the global list size","%d",&n);
    list_size = n/p;

    Generate_local_list(list_size, local_list);
/*
    Print_list("Before local sort", list_size, local_list, io_comm);
 */       
    Local_sort(list_size, local_list);
/*
    Print_list("After local sort", list_size, local_list, io_comm);
 */
 
    /* and_bit is a bitmask that, when "anded" with  */
    /* my_rank, tells us whether we're working on an */
    /* increasing or decreasing list                 */
    for (proc_set_size = 2, and_bit = 2;
         proc_set_size <= p;
         proc_set_size = proc_set_size*2, 
         and_bit = and_bit << 1)
        if ((my_rank & and_bit) == 0)
            Par_bitonic_sort_incr(list_size, 
                      local_list, proc_set_size, MPI_COMM_WORLD);
        else
            Par_bitonic_sort_decr(list_size, 
                      local_list, proc_set_size, MPI_COMM_WORLD);

    Print_list("After sort", list_size, local_list, io_comm);

    MPI_Finalize();
}  /* main */
Exemplo n.º 11
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 */
Exemplo n.º 12
0
/*--------------------------------------------------------------------*/
int main(int argc, char* argv[]) {
  long thread;
  pthread_t* thread_handles; 
  double start, finish;

  suppress_output = 0;
  // for (int i = 0; i < argc; ++i){
  //   printf("Command line args === argv[%d]: %s\n", i, argv[i]);
  // }  

  if (argc == 5) { 
  } else if (argc == 6 && (strcmp(argv[5], "n") == 0)) {
	  // printf("==== %s\n", argv[5]);
	  suppress_output = 1;
  } else {
	Usage(argv[0]);
  }
  
  thread_count = strtol(argv[1], NULL, 10);
  sample_size = strtol(argv[2], NULL, 10);
  list_size = strtol(argv[3], NULL, 10);
  input_file = argv[4];

  // Allocate memory for variables
  thread_handles = malloc(thread_count*sizeof(pthread_t));
  list = malloc(list_size * sizeof(int));
  tmp_list = malloc(list_size * sizeof(int));
  sorted_list = malloc(list_size * sizeof(int));
  sample_keys = malloc(sample_size * sizeof(int));
  sorted_keys = malloc(sample_size * sizeof(int));
  splitters = malloc(thread_count * sizeof(int));
  
  // One dimensional distribution arrays
  raw_dist = malloc(thread_count * thread_count * sizeof(int));
  col_dist = malloc(thread_count * sizeof(int));
  prefix_dist = malloc(thread_count * thread_count * sizeof(int));
  prefix_col_dist = malloc(thread_count * sizeof(int));
  
	
  // pthread_mutex_init(&barrier_mutex, NULL);
  // pthread_cond_init(&ok_to_proceed, NULL);
  pthread_barrier_init(&barrier, NULL, thread_count);
  

  // Read list content from input file
  FILE *fp = fopen(input_file, "r+");
  for (i = 0; i < list_size; i++) {
  	  if (!fscanf(fp, "%d", &list[i])) {
    	  break;
      }
  }
  Print_list(list, list_size, "original list");
  
  GET_TIME(start);
  
  for (thread = 0; thread < thread_count; thread++)
     pthread_create(&thread_handles[thread], NULL,
         Thread_work, (void*) thread);

  for (thread = 0; thread < thread_count; thread++) 
     pthread_join(thread_handles[thread], NULL);
  
  GET_TIME(finish);
  
  // Print_list(sample_keys, sample_size, "Sample keys (unsorted)");
  Print_list(sorted_keys, sample_size, "Sample keys (sorted)");
  Print_list(splitters, thread_count, "Splitters");
  Print_list(raw_dist, thread_count * thread_count, "Raw dist");
  Print_list(prefix_dist, thread_count * thread_count, "Prefix dist");
  Print_list(col_dist, thread_count, "Colsum dist");
  Print_list(prefix_col_dist, thread_count, "Prefix colsum dist");
  Print_list(tmp_list, list_size, "Temp list");
  
  // Only print list data if not suppressed
  if (suppress_output == 0) {
	  Print_list(sorted_list, list_size, "Sorted list");
  }
  
  // Print elapsed time regardless
  printf("Elapsed time = %e seconds\n", finish - start);


  pthread_barrier_destroy(&barrier);
  // pthread_mutex_destroy(&barrier_mutex);
  // pthread_cond_destroy(&ok_to_proceed);

  free(thread_handles);
  
  return 0;
}  /* main */