45 #ifdef HAVE_TEUCHOS_MPI
47 #endif // HAVE_TEUCHOS_MPI
51 template<
class PacketType>
56 using Teuchos::reduce;
59 typedef PacketType packet_type;
63 out <<
"Testing Teuchos::reduce<int, " << TypeNameTraits<packet_type>::name ()
64 <<
"> with reductType = REDUCE_SUM and root = " << root << endl;
69 packet_type sendBuf[10];
70 packet_type recvBuf[10];
71 for (
int i = 0; i < count; ++i) {
76 reduce<int, packet_type> (sendBuf, recvBuf, count, reductType, root, comm);
83 #ifdef HAVE_TEUCHOS_MPI
84 using Teuchos::MpiComm;
85 int err = MPI_SUCCESS;
87 const MpiComm<int>* mpiComm =
dynamic_cast<const MpiComm<int>*
> (&comm);
89 (mpiComm == NULL, std::logic_error,
"Building with MPI, but default "
90 "communicator is not a Teuchos::MpiComm!");
91 MPI_Comm rawMpiComm = * (mpiComm->getRawMpiComm ());
94 err = MPI_Barrier (rawMpiComm);
96 (err != MPI_SUCCESS, std::logic_error,
"MPI_Barrier failed!");
99 packet_type sendBuf2[10];
100 packet_type recvBuf2[10];
101 for (
int i = 0; i < count; ++i) {
106 MPI_Datatype rawMpiType;
107 if (
typeid (packet_type) ==
typeid (
short)) {
108 rawMpiType = MPI_SHORT;
109 }
else if (
typeid (packet_type) ==
typeid (
unsigned short)) {
110 rawMpiType = MPI_UNSIGNED_SHORT;
111 }
else if (
typeid (packet_type) ==
typeid (
int)) {
112 rawMpiType = MPI_INT;
113 }
else if (
typeid (packet_type) ==
typeid (
unsigned int)) {
114 rawMpiType = MPI_UNSIGNED;
115 }
else if (
typeid (packet_type) ==
typeid (
long)) {
116 rawMpiType = MPI_LONG;
117 }
else if (
typeid (packet_type) ==
typeid (
unsigned long)) {
118 rawMpiType = MPI_UNSIGNED_LONG;
119 }
else if (
typeid (packet_type) ==
typeid (
long long)) {
120 rawMpiType = MPI_LONG_LONG;
121 }
else if (
typeid (packet_type) ==
typeid (
unsigned long long)) {
122 rawMpiType = MPI_UNSIGNED_LONG_LONG;
123 }
else if (
typeid (packet_type) ==
typeid (
float)) {
124 rawMpiType = MPI_FLOAT;
125 }
else if (
typeid (packet_type) ==
typeid (
double)) {
126 rawMpiType = MPI_DOUBLE;
129 (
true, std::logic_error,
"Unimplemented conversion from PacketType = "
130 << TypeNameTraits<packet_type>::name () <<
" to MPI_Datatype.");
133 err = MPI_Allreduce (sendBuf2, recvBuf2, count, rawMpiType, MPI_SUM, rawMpiComm);
135 (err != MPI_SUCCESS, std::logic_error,
"MPI_Allreduce failed!");
137 for (
int i = 0; i < count; ++i) {
142 int lclSuccess = success ? 1 : 0;
143 int gblSuccess = lclSuccess;
144 err = MPI_Allreduce (&lclSuccess, &gblSuccess, 1, MPI_INT, MPI_MIN, rawMpiComm);
146 (err != MPI_SUCCESS, std::logic_error,
"MPI_Allreduce failed!");
147 success = gblSuccess == 1 ?
true :
false;
149 #else // HAVE_TEUCHOS_MPI
151 (comm.
getSize () != 1, std::logic_error,
"Not building with MPI, but "
152 "communicator has size = " << comm.
getSize () <<
" != 1. We don't know "
153 "how to test this case.");
154 for (
int i = 0; i < count; ++i) {
158 #endif // HAVE_TEUCHOS_MPI
168 using Teuchos::reduce;
169 typedef PacketType packet_type;
172 const int numProcs = comm->getSize ();
176 for (
int root = 0; root < numProcs; ++root) {
177 const bool curSuccess = testReduceSum<packet_type> (success, out, root, *comm);
179 success = success && curSuccess;
virtual int getSize() const =0
Returns the number of processes that make up this communicator.
EReductionType
Predefined reduction operations that Teuchos::Comm understands.
virtual int getRank() const =0
Returns the rank of this process.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(TEST_GROUP, TEST_NAME, TYPE)
Macro for defining a templated unit test with one template parameter.
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.
Implementation of Teuchos wrappers for MPI.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
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.