Skip to content

Commit

Permalink
Added min/max sanity tests and some cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhornung67 committed May 12, 2020
1 parent 246bfe6 commit d03013b
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Copyright (c) 2016-20, Lawrence Livermore National Security, LLC
// and RAJA project contributors. See the RAJA/COPYRIGHT file for details.
//
// SPDX-License-Identifier: (BSD-3-Clause)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

#ifndef __TEST_FORALL_REDUCEMAX_SANITY_HPP__
#define __TEST_FORALL_REDUCEMAX_SANITY_HPP__

#include "RAJA/RAJA.hpp"

#include "test-forall-reduce-sanity.hpp"

#include <cstdlib>
#include <numeric>

template <typename DATA_TYPE, typename WORKING_RES,
typename EXEC_POLICY, typename REDUCE_POLICY>
void ForallReduceMaxSanityTest(RAJA::Index_type first, RAJA::Index_type last)
{
RAJA::TypedRangeSegment<RAJA::Index_type> r1(first, last);

camp::resources::Resource working_res{WORKING_RES()};
DATA_TYPE* working_array;
DATA_TYPE* check_array;
DATA_TYPE* test_array;

allocateForallTestData<DATA_TYPE>(last,
working_res,
&working_array,
&check_array,
&test_array);

const int modval = 100;
const DATA_TYPE max_init = -1;
const DATA_TYPE big_max = modval + 1;

for (RAJA::Index_type i = 0; i < last; ++i) {
test_array[i] = static_cast<DATA_TYPE>( rand() % modval );
}

DATA_TYPE ref_max = max_init;
for (RAJA::Index_type i = first; i < last; ++i) {
ref_max = RAJA_MAX(test_array[i], ref_max);
}

working_res.memcpy(working_array, test_array, sizeof(DATA_TYPE) * last);

RAJA::ReduceMax<REDUCE_POLICY, DATA_TYPE> maxinit(big_max);
RAJA::ReduceMax<REDUCE_POLICY, DATA_TYPE> max(max_init);

RAJA::forall<EXEC_POLICY>(r1, [=] RAJA_HOST_DEVICE(RAJA::Index_type idx) {
maxinit.max( working_array[idx] );
max.max( working_array[idx] );
});

ASSERT_EQ(static_cast<DATA_TYPE>(maxinit.get()), big_max);
ASSERT_EQ(static_cast<DATA_TYPE>(max.get()), ref_max);

max.reset(max_init);

const int nloops = 2;

for (int j = nloops; j > 0; --j) {
RAJA::forall<EXEC_POLICY>(r1, [=] RAJA_HOST_DEVICE(RAJA::Index_type idx) {
max.max( working_array[idx] / j);
});
}

ASSERT_EQ(static_cast<DATA_TYPE>(max.get()), ref_max);


deallocateForallTestData<DATA_TYPE>(working_res,
working_array,
check_array,
test_array);
}


TYPED_TEST_P(ForallReduceSanityTest, ReduceMaxSanityForall)
{
using DATA_TYPE = typename camp::at<TypeParam, camp::num<0>>::type;
using WORKING_RES = typename camp::at<TypeParam, camp::num<1>>::type;
using EXEC_POLICY = typename camp::at<TypeParam, camp::num<2>>::type;
using REDUCE_POLICY = typename camp::at<TypeParam, camp::num<3>>::type;

ForallReduceMaxSanityTest<DATA_TYPE, WORKING_RES,
EXEC_POLICY, REDUCE_POLICY>(0, 587);
ForallReduceMaxSanityTest<DATA_TYPE, WORKING_RES,
EXEC_POLICY, REDUCE_POLICY>(3, 642);
}

#endif // __TEST_FORALL_REDUCEMAX_SANITY_HPP__
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Copyright (c) 2016-20, Lawrence Livermore National Security, LLC
// and RAJA project contributors. See the RAJA/COPYRIGHT file for details.
//
// SPDX-License-Identifier: (BSD-3-Clause)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

#ifndef __TEST_FORALL_REDUCEMIN_SANITY_HPP__
#define __TEST_FORALL_REDUCEMIN_SANITY_HPP__

#include "RAJA/RAJA.hpp"

#include "test-forall-reduce-sanity.hpp"

#include <cstdlib>
#include <numeric>

template <typename DATA_TYPE, typename WORKING_RES,
typename EXEC_POLICY, typename REDUCE_POLICY>
void ForallReduceMinSanityTest(RAJA::Index_type first, RAJA::Index_type last)
{
RAJA::TypedRangeSegment<RAJA::Index_type> r1(first, last);

camp::resources::Resource working_res{WORKING_RES()};
DATA_TYPE* working_array;
DATA_TYPE* check_array;
DATA_TYPE* test_array;

allocateForallTestData<DATA_TYPE>(last,
working_res,
&working_array,
&check_array,
&test_array);

const int modval = 100;
const DATA_TYPE min_init = modval+1;
const DATA_TYPE small_min = -modval;

for (RAJA::Index_type i = 0; i < last; ++i) {
test_array[i] = static_cast<DATA_TYPE>( rand() % modval );
}

DATA_TYPE ref_min = min_init;
for (RAJA::Index_type i = first; i < last; ++i) {
ref_min = RAJA_MIN(test_array[i], ref_min);
}

working_res.memcpy(working_array, test_array, sizeof(DATA_TYPE) * last);


RAJA::ReduceMin<REDUCE_POLICY, DATA_TYPE> mininit(small_min);
RAJA::ReduceMin<REDUCE_POLICY, DATA_TYPE> min(min_init);

RAJA::forall<EXEC_POLICY>(r1, [=] RAJA_HOST_DEVICE(RAJA::Index_type idx) {
mininit.min( working_array[idx] );
min.min( working_array[idx] );
});

ASSERT_EQ(static_cast<DATA_TYPE>(mininit.get()), small_min);
ASSERT_EQ(static_cast<DATA_TYPE>(min.get()), ref_min);

min.reset(min_init);

const int nloops = 2;

for (int j = nloops; j > 0; --j) {
RAJA::forall<EXEC_POLICY>(r1, [=] RAJA_HOST_DEVICE(RAJA::Index_type idx) {
min.min( working_array[idx] * j);
});
}

ASSERT_EQ(static_cast<DATA_TYPE>(min.get()), ref_min);


deallocateForallTestData<DATA_TYPE>(working_res,
working_array,
check_array,
test_array);
}


TYPED_TEST_P(ForallReduceSanityTest, ReduceMinSanityForall)
{
using DATA_TYPE = typename camp::at<TypeParam, camp::num<0>>::type;
using WORKING_RES = typename camp::at<TypeParam, camp::num<1>>::type;
using EXEC_POLICY = typename camp::at<TypeParam, camp::num<2>>::type;
using REDUCE_POLICY = typename camp::at<TypeParam, camp::num<3>>::type;

ForallReduceMinSanityTest<DATA_TYPE, WORKING_RES,
EXEC_POLICY, REDUCE_POLICY>(0, 587);
ForallReduceMinSanityTest<DATA_TYPE, WORKING_RES,
EXEC_POLICY, REDUCE_POLICY>(3, 642);
}

#endif // __TEST_FORALL_REDUCEMIN_SANITY_HPP__
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ void ForallReduceSumSanityTest(RAJA::Index_type first, RAJA::Index_type last)
&check_array,
&test_array);

const int modval = 100;

for (RAJA::Index_type i = 0; i < last; ++i) {
test_array[i] = static_cast<DATA_TYPE>( rand() % 100 );
test_array[i] = static_cast<DATA_TYPE>( rand() % modval );
}

DATA_TYPE ref_sum = 0;
Expand All @@ -45,12 +47,15 @@ void ForallReduceSumSanityTest(RAJA::Index_type first, RAJA::Index_type last)


RAJA::ReduceSum<REDUCE_POLICY, DATA_TYPE> sum(0);
RAJA::ReduceSum<REDUCE_POLICY, DATA_TYPE> sum2(2);

RAJA::forall<EXEC_POLICY>(r1, [=] RAJA_HOST_DEVICE(RAJA::Index_type idx) {
sum += working_array[idx];
sum += working_array[idx];
sum2 += working_array[idx];
});

ASSERT_EQ(static_cast<DATA_TYPE>(sum.get()), ref_sum);
ASSERT_EQ(static_cast<DATA_TYPE>(sum2.get()), ref_sum + 2);

sum.reset(0);

Expand All @@ -66,9 +71,9 @@ void ForallReduceSumSanityTest(RAJA::Index_type first, RAJA::Index_type last)


deallocateForallTestData<DATA_TYPE>(working_res,
working_array,
check_array,
test_array);
working_array,
check_array,
test_array);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ using ReduceSanityDataTypeList = camp::list<int,
float,
double>;

#include "test-forall-reducesum-sanity.hpp"
#include "test-forall-reduce-sanity-sum.hpp"
#include "test-forall-reduce-sanity-min.hpp"
#include "test-forall-reduce-sanity-max.hpp"
#if 0
#include "test-forall-reducemin-sanity.hpp"
#include "test-forall-reducemax-sanity.hpp"
#include "test-forall-reduceminloc-sanity.hpp"
#include "test-forall-reducemaxloc-sanity.hpp"
#endif

REGISTER_TYPED_TEST_SUITE_P(ForallReduceSanityTest,
ReduceSumSanityForall);
#if 0
ReduceSumSanityForall,
ReduceMinSanityForall,
ReduceMaxSanityForall,
ReduceMaxSanityForall);
#if 0
ReduceMinLocSanityForall,
ReduceMaxLocSanityForall);
#endif
Expand Down

0 comments on commit d03013b

Please sign in to comment.