void parallel_merge(int *T, int p1, int r1, int p2, int r2, int *A, int p3)
{
	int q1, q2, q3;
	int n1 = r1 - p1 + 1;
	int n2 = r2 - p2 + 1;

	if (n1 < n2)
	{
		swap(&p1, &p2);
		swap(&r1, &r2);
		swap(&n1, &n2);
	}
	if (n1 == 0)
		return;
	else
	{
		q1 = (p1 + r1) / 2;
		q2 = binary_search(T[q1], T, p2, r2);
		q3 = p3 + (q1 - p1) + (q2 - p2);
		A[q3] = T[q1];
#pragma omp parallel sections shared(T, p1, q1, p2, q2, A, p3, q3)
		{
#pragma omp section
			parallel_merge(T, p1, q1 - 1, p2, q2 - 1, A, p3);
#pragma omp section
			parallel_merge(T, q1 + 1, r1, q2, r2, A, q3 + 1);
		}
		return;
	}
}
void parallel_merge_sort(int *A, int p, int r, int *B, int s)
{
	int n = r - p + 1;

	if (n == 1)
	{
		B[s] = A[p];
		return;
	}
	else
	{
		int *T = (int *) malloc(sizeof(int) * n);
		int q1 = (p + r) / 2;
		int q2 = q1 - p + 1;

		#pragma omp parallel sections shared(A, p, q1, T, r, q2)
		{
#pragma omp section
			parallel_merge_sort(A, p, q1, T, 0);
#pragma omp section
			parallel_merge_sort(A, q1 + 1, r, T, q2);
		}
		parallel_merge(T, 0, q2 - 1, q2, n - 1, B, s);
		free(T);
		return;
	}
}
Exemple #3
0
void parallel_sort(void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *))
{
  int i, ncpu_in_group, master, groupnr;
  size_t nmax, *nlist;

  int NTask;
  int ThisTask;

  MPI_Comm_rank(MPI_COMM_WORLD, &ThisTask);
  MPI_Comm_size(MPI_COMM_WORLD, &NTask);

  int PTask;

  for(PTask = 0; NTask > (1 << PTask); PTask++) continue;

  serial_sort((char *) base, nmemb, size, compar);

  nlist = (size_t *) malloc(NTask * sizeof(size_t));

  MPI_Allgather(&nmemb, sizeof(size_t), MPI_BYTE, nlist, sizeof(size_t), MPI_BYTE, MPI_COMM_WORLD);

  for(i = 0, nmax = 0; i < NTask; i++)
    if(nlist[i] > nmax)
      nmax = nlist[i];

  for(ncpu_in_group = 2; ncpu_in_group <= (1 << PTask); ncpu_in_group *= 2)
    {
      groupnr = ThisTask / ncpu_in_group;

      master = ncpu_in_group * groupnr;

      parallel_merge(master, ncpu_in_group, nlist, nmax, (char *) base, nmemb, size, compar);
    }

  free(nlist);
}
void
test_main (void) 
{
  parallel_merge ("child-sort", 123);
}
void
test_main (void) 
{
  parallel_merge ("child-qsort-mm", 80);
}
void
test_main (void)
{
  parallel_merge ("child-qsort", 72);
}