void mg_mpi(int* data, const unsigned long n, const int rank, const int size, const MPI_Comm comm) { // root tells each process how much data to expect. unsigned long partition_size; if(rank == 0) { partition_size = n / size; // TODO handle invalid multiplicity (eg 100 / 8 = 12.5) } MPI_Bcast( &partition_size, 1, MPI_UNSIGNED_LONG, 0, comm ); log(LOG_DEBUG, "part size: %u\n", partition_size); // allocate a partition of the correct size to work in. int* partition = (int*)malloc( partition_size * sizeof(int) ); // scatter data to the partitions MPI_Scatter( data, partition_size, MPI_INT, partition, partition_size, MPI_INT, 0, comm ); // clear initial data, to avoid confusion. if(rank == 0) { memset(data, 0, n); } // sort inividual pieces using mergesort sort_partition(&partition, partition_size); // gather results up the tree, merging 2 pieces at each step, until results // end up in the root. they are stored in data. collect_results(&data, n, &partition, &partition_size, rank, size, comm); // partition buffer is no longer needed so free it. free( partition ); }
void OrbLB::qsort(int x, int p, int r) { if (p<r) { int q = sort_partition(x, p, r); //CmiPrintf("midpoint: %d %d %d\n", p,q,r); qsort(x, p, q-1); qsort(x, q+1, r); } }