Skip to content

Commit

Permalink
switched pivot strategy 3 to allreduce
Browse files Browse the repository at this point in the history
  • Loading branch information
tureture committed May 10, 2023
1 parent d5d762a commit 46d1f0d
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions A3/quicksort.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,27 @@ int find_median(int *array, int n)

int select_pivot(int *array, int n, int pivot_strategy, MPI_Comm comm)
{
int size,rank,pivot;
int* pivots;
int size,rank, pivot;
MPI_Comm_rank(comm, &rank);
MPI_Comm_size(comm, &size);

if (pivot_strategy == 1) // Median of processor 0
{
int pivot;

if (rank == 0) // Select the median on rank 0
{
pivot = find_median(array, n);
}

// Broadcast pivot to all other processors in group
MPI_Bcast(&pivot, 1, MPI_INT, 0, comm);
}
else if (pivot_strategy == 2) // Median of medians
{
int pivot;
int *pivots;

// find median
pivot = find_median(array, n);

Expand All @@ -117,36 +124,38 @@ int select_pivot(int *array, int n, int pivot_strategy, MPI_Comm comm)
quicksort(pivots,size);
pivot = find_median(pivots, size);
}

// FREE MEMORY
if (rank==0 && (pivot_strategy==2 || pivot_strategy==3))
free(pivots);

// Broadcast pivot to all other processors in group
MPI_Bcast(&pivot, 1, MPI_INT, 0, comm);
}
else // Mean of medians Pivot strategy 3
{
// find median
pivot = find_median(array, n);

// Allocate memory for all pivots
if (rank == 0)
pivots = (int*)malloc(size * sizeof(int));

// gather pivot in pivots array on rank 0
MPI_Gather(&pivot, 1, MPI_INT, pivots, 1, MPI_INT, 0, comm); // Gather all pivots to processor 0
int pivots[2]; // actual pivot and if current list is 0
int sum_pivots[2];

// find mean of medians
if (rank == 0)
// find median and number of nonzero lists
if (n > 0)
{
int sum=0;
for (int i=0;i<size;i++)
sum+=pivots[i];
pivots[0] = find_median(array, n);
pivots[1] = 1;
}
else
{
pivots[0] = 0;
pivots[1] = 0;

pivot = sum/size;
}
}

// Broadcast pivot to all other processors in group
MPI_Bcast(&pivot, 1, MPI_INT, 0, comm);
MPI_Allreduce(&pivots, &sum_pivots, 1, MPI_INT, MPI_SUM, comm);

// FREE MEMORY
if (rank==0 && (pivot_strategy==2 || pivot_strategy==3))
free(pivots);
// find mean of medians
pivot = sum_pivots[0]/sum_pivots[1];

}

return pivot;
}
Expand Down

0 comments on commit 46d1f0d

Please sign in to comment.