45 #ifdef HAVE_TEUCHOS_MPI 47 #endif // HAVE_TEUCHOS_MPI 51 template<
class PacketType>
56 #ifdef HAVE_TEUCHOS_MPI 57 using Teuchos::MpiComm;
58 #endif // HAVE_TEUCHOS_MPI 59 using Teuchos::reduce;
62 typedef PacketType packet_type;
66 out <<
"Testing Teuchos::reduce<int, " << TypeNameTraits<packet_type>::name ()
67 <<
"> with reductType = REDUCE_SUM and root = " << root << endl;
72 packet_type sendBuf[10];
73 packet_type recvBuf[10];
74 for (
int i = 0; i < count; ++i) {
79 reduce<int, packet_type> (sendBuf, recvBuf, count, reductType, root, comm);
86 #ifdef HAVE_TEUCHOS_MPI 87 using Teuchos::MpiComm;
88 int err = MPI_SUCCESS;
90 const MpiComm<int>* mpiComm =
dynamic_cast<const MpiComm<int>*
> (&comm);
92 (mpiComm == NULL, std::logic_error,
"Building with MPI, but default " 93 "communicator is not a Teuchos::MpiComm!");
94 MPI_Comm rawMpiComm = * (mpiComm->getRawMpiComm ());
97 err = MPI_Barrier (rawMpiComm);
99 (err != MPI_SUCCESS, std::logic_error,
"MPI_Barrier failed!");
102 packet_type sendBuf2[10];
103 packet_type recvBuf2[10];
104 for (
int i = 0; i < count; ++i) {
109 MPI_Datatype rawMpiType;
110 if (
typeid (packet_type) ==
typeid (
short)) {
111 rawMpiType = MPI_SHORT;
112 }
else if (
typeid (packet_type) ==
typeid (
unsigned short)) {
113 rawMpiType = MPI_UNSIGNED_SHORT;
114 }
else if (
typeid (packet_type) ==
typeid (
int)) {
115 rawMpiType = MPI_INT;
116 }
else if (
typeid (packet_type) ==
typeid (
unsigned int)) {
117 rawMpiType = MPI_UNSIGNED;
118 }
else if (
typeid (packet_type) ==
typeid (
long)) {
119 rawMpiType = MPI_LONG;
120 }
else if (
typeid (packet_type) ==
typeid (
unsigned long)) {
121 rawMpiType = MPI_UNSIGNED_LONG;
122 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 123 }
else if (
typeid (packet_type) ==
typeid (
long long)) {
124 rawMpiType = MPI_LONG_LONG;
125 }
else if (
typeid (packet_type) ==
typeid (
unsigned long long)) {
126 rawMpiType = MPI_UNSIGNED_LONG_LONG;
127 #endif // HAVE_TEUCHOS_LONG_LONG_INT 128 }
else if (
typeid (packet_type) ==
typeid (
float)) {
129 rawMpiType = MPI_FLOAT;
130 }
else if (
typeid (packet_type) ==
typeid (
double)) {
131 rawMpiType = MPI_DOUBLE;
134 (
true, std::logic_error,
"Unimplemented conversion from PacketType = " 135 << TypeNameTraits<packet_type>::name () <<
" to MPI_Datatype.");
138 err = MPI_Allreduce (sendBuf2, recvBuf2, count, rawMpiType, MPI_SUM, rawMpiComm);
140 (err != MPI_SUCCESS, std::logic_error,
"MPI_Allreduce failed!");
142 for (
int i = 0; i < count; ++i) {
147 int lclSuccess = success ? 1 : 0;
148 int gblSuccess = lclSuccess;
149 err = MPI_Allreduce (&lclSuccess, &gblSuccess, 1, MPI_INT, MPI_MIN, rawMpiComm);
151 (err != MPI_SUCCESS, std::logic_error,
"MPI_Allreduce failed!");
152 success = gblSuccess == 1 ? true :
false;
154 #else // HAVE_TEUCHOS_MPI 156 (comm.
getSize () != 1, std::logic_error,
"Not building with MPI, but " 157 "communicator has size = " << comm.
getSize () <<
" != 1. We don't know " 158 "how to test this case.");
159 for (
int i = 0; i < count; ++i) {
163 #endif // HAVE_TEUCHOS_MPI 173 using Teuchos::reduce;
174 typedef PacketType packet_type;
177 const int numProcs = comm->getSize ();
181 for (
int root = 0; root < numProcs; ++root) {
182 const bool curSuccess = testReduceSum<packet_type> (success, out, root, *comm);
184 success = success && curSuccess;
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(Comm, ReduceSum, PacketType)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
Return the default global communicator.
virtual int getRank() const =0
Returns the rank of this process.
Implementation of Teuchos wrappers for MPI.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
Abstract interface for distributed-memory communication.
Default traits class that just returns typeid(T).name().
bool testReduceSum(bool &success, std::ostream &out, const int root, const Teuchos::Comm< int > &comm)
static T zero()
Returns representation of zero for this scalar type.
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT(TEST_GROUP, TEST_NAME, TYPE)
Instantiate a templated unit test with one template parameter.
Smart reference counting pointer class for automatic garbage collection.
static T one()
Returns representation of one for this scalar type.
virtual int getSize() const =0
Returns the number of processes that make up this communicator.