00001
00007 #include "dtypes.h"
00008 #include "parUtils.h"
00009 #include "seqUtils.h"
00010 #include <iostream>
00011
00012 namespace seq {
00013 namespace test {
00014
00015 template <typename T>
00016 bool isSorted(const std::vector<T > & nodes) {
00017 for (unsigned int i = 1; i < nodes.size(); i++) {
00018 if ( nodes[i] < nodes[i-1] ) {
00019 std::cout<<"\n Local Sort Check failed for: "<<nodes[i]<<" and "
00020 <<nodes[i-1]<<std::endl<<std::endl;
00021 return false;
00022 }
00023 }
00024 return true;
00025 }
00026
00027 template <typename T>
00028 bool isSorted(T* nodes, unsigned int sz) {
00029 for (unsigned int i = 1; i < sz; i++) {
00030 if ( nodes[i] < nodes[i-1] ) {
00031 std::cout<<"\n Local Sort Check failed for: "<<nodes[i]<<" and "
00032 <<nodes[i-1]<<std::endl<<std::endl;
00033 return false;
00034 }
00035 }
00036 return true;
00037 }
00038
00039 template <typename T>
00040 bool isUniqueAndSorted(const std::vector<T > & nodes) {
00041 for (unsigned int i = 1; i < nodes.size(); i++) {
00042 if ( nodes[i] <= nodes[i-1] ) {
00043 std::cout<<"\n Local Sort+Unique Check failed for: "<<nodes[i]<<" and "
00044 <<nodes[i-1]<<std::endl;
00045 return false;
00046 }
00047 }
00048 return true;
00049 }
00050
00051 template <typename T>
00052 bool isUniqueAndSorted(T* nodes, unsigned int sz) {
00053 for (unsigned int i = 1; i < sz; i++) {
00054 if ( nodes[i] <= nodes[i-1] ) {
00055 std::cout<<"\n Local Sort+Unique Check failed for: "<<nodes[i]<<" and "
00056 <<nodes[i-1]<<std::endl;
00057 return false;
00058 }
00059 }
00060 return true;
00061 }
00062
00063 }
00064 }
00065
00066 namespace par {
00067 namespace test {
00068
00069 template <typename T>
00070 bool isSorted(const std::vector<T > &nodes, MPI_Comm comm) {
00071 bool localPassed = par::test::isSorted<T>(nodes);
00072 bool allLocalsPassed;
00073
00074 par::Mpi_Allreduce<bool>(&localPassed, &allLocalsPassed, 1,
00075 par::Mpi_datatype<bool>::LAND(), comm);
00076
00077 if(allLocalsPassed) {
00078 bool failedParCheck = false;
00079
00080 MPI_Comm new_comm;
00081 par::splitComm2way(nodes.empty(), new_comm, comm);
00082
00083 if(!nodes.empty()) {
00084 int rank;
00085 int npes;
00086 MPI_Request request;
00087 MPI_Status status;
00088 MPI_Comm_rank(new_comm,&rank);
00089 MPI_Comm_size(new_comm,&npes);
00090
00091
00092 T end = nodes[nodes.size()-1];
00093 if(rank < (npes -1)) {
00094 par::Mpi_Issend<T>(&end, 1, rank+1, 0, new_comm, &request );
00095 }
00096
00097 T prev, me;
00098 me = nodes[0];
00099
00100
00101 if(rank) {
00102 par::Mpi_Recv<T>( &prev, 1, rank-1, 0, new_comm, &status );
00103 if(prev > me) {
00104 failedParCheck = true;
00105 }
00106 }
00107
00108 if(rank < (npes-1)) {
00109 MPI_Status statusWait;
00110 MPI_Wait(&request, &statusWait);
00111 }
00112 }
00113
00114 bool anyProcFailed;
00115 par::Mpi_Allreduce<bool>(&failedParCheck, &anyProcFailed, 1,
00116 par::Mpi_datatype<bool>::LOR(), comm);
00117
00118 return (!anyProcFailed);
00119 }
00120
00121 return allLocalsPassed;
00122 }
00123 template <typename T>
00124 bool isUniqueAndSorted(const std::vector<T > &nodes, MPI_Comm comm) {
00125 bool localPassed = seq::test::isUniqueAndSorted<T>(nodes);
00126 bool allLocalsPassed;
00127
00128 par::Mpi_Allreduce<bool>(&localPassed, &allLocalsPassed, 1,
00129 par::Mpi_datatype<bool>::LAND(), comm);
00130
00131 if(allLocalsPassed) {
00132 bool failedParCheck = false;
00133
00134 MPI_Comm new_comm;
00135 par::splitComm2way(nodes.empty(), &new_comm, comm);
00136
00137 if(!nodes.empty()) {
00138 int rank;
00139 int npes;
00140 MPI_Request request;
00141 MPI_Status status;
00142 MPI_Comm_rank(new_comm,&rank);
00143 MPI_Comm_size(new_comm,&npes);
00144
00145
00146 T end = nodes[nodes.size()-1];
00147 if(rank < (npes-1)) {
00148 par::Mpi_Issend<T>(&end, 1, rank+1, 0, new_comm, &request );
00149 }
00150
00151 T prev, me;
00152 me = nodes[0];
00153
00154
00155 if(rank) {
00156 par::Mpi_Recv<T>( &prev, 1, rank-1, 0, new_comm, &status );
00157 if(prev >= me) {
00158 failedParCheck = true;
00159 }
00160 }
00161
00162 if(rank < (npes-1)) {
00163 MPI_Status statusWait;
00164 MPI_Wait(&request, &statusWait);
00165 }
00166 }
00167
00168 bool anyProcFailed;
00169 par::Mpi_Allreduce<bool>(&failedParCheck, &anyProcFailed, 1,
00170 par::Mpi_datatype<bool>::LOR(), comm);
00171
00172 return (!anyProcFailed);
00173 }
00174
00175 return allLocalsPassed;
00176 }
00177
00178 }
00179 }
00180
00181