Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "par_partition.h"
00037
00038 #include <iostream>
00039
00040 #include "mpi_utils.h"
00041 #include "partition.h"
00042 #include "mpi_bindings.h"
00043
00044
00045
00046 using namespace std;
00047
00048 namespace cluster {
00049
00050 par_partition::par_partition(MPI_Comm _comm) : comm(_comm) { }
00051
00052 par_partition::~par_partition() { }
00053
00054
00055 void par_partition::gather(partition& destination, int root) {
00056 int rank, size;
00057 CMPI_Comm_rank(comm, &rank);
00058 CMPI_Comm_size(comm, &size);
00059
00060 #ifdef DEBUG
00061 size_t count = medoid_ids.size();
00062 CMPI_Bcast(&count, 1, MPI_SIZE_T, root, comm);
00063 if (count != medoid_ids.size()) {
00064 cerr << "Error: incorrect number of medoids on " << rank << ": " << medoid_ids.size()
00065 << ", expected " << count << endl;
00066 exit(1);
00067 }
00068
00069 size_t object_count = cluster_ids.size();
00070 CMPI_Bcast(&object_count, 1, MPI_SIZE_T, root, comm);
00071 if (object_count != cluster_ids.size()) {
00072 cerr << "Error: incorrect number of objects on " << rank << ": " << cluster_ids.size()
00073 << ", expected " << object_count << endl;
00074 exit(1);
00075 }
00076
00077 std::vector<object_id> bcast_medoid_ids = medoid_ids;
00078 CMPI_Bcast(&bcast_medoid_ids[0], bcast_medoid_ids.size(), MPI_SIZE_T, root, comm);
00079 for (size_t i=0; i < medoid_ids.size(); i++) {
00080 if (medoid_ids[i] != bcast_medoid_ids[i]) {
00081 cerr << "Error: medoids do not match on " << rank << endl;
00082 exit(1);
00083 }
00084 }
00085 #endif // DEBUG
00086
00087 if (rank == root) {
00088 destination.medoid_ids = medoid_ids;
00089 destination.cluster_ids.resize(cluster_ids.size() * size);
00090 }
00091
00092 CMPI_Gather(&cluster_ids[0], cluster_ids.size(), MPI_SIZE_T,
00093 &destination.cluster_ids[0], cluster_ids.size(), MPI_SIZE_T,
00094 root, comm);
00095 }
00096
00097
00098 void par_partition::get_sizes(std::vector<size_t>& sizes) {
00099 vector<size_t> local_sizes(medoid_ids.size(), 0);
00100 for (size_t i=0; i < cluster_ids.size(); i++) {
00101 local_sizes[cluster_ids[i]]++;
00102 }
00103
00104 sizes.resize(medoid_ids.size());
00105 CMPI_Allreduce(&local_sizes[0], &sizes[0], medoid_ids.size(), MPI_SIZE_T, MPI_SUM, comm);
00106 }
00107
00108
00109
00110 std::ostream& operator<<(std::ostream& out, const par_partition& par) {
00111 cluster::partition p;
00112 p.medoid_ids = par.medoid_ids;
00113 p.cluster_ids = par.cluster_ids;
00114 out << p;
00115 return out;
00116 }
00117
00118
00119 }