Skip to content

Commit 553c38d

Browse files
authored
fix fallthrough in reduce_by_key_common for u8 (arrayfire#3503)
This fixes minByKey/maxByKey for u8.
1 parent b1e85d3 commit 553c38d

2 files changed

Lines changed: 103 additions & 61 deletions

File tree

src/api/c/reduce.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ static af_err reduce_by_key_common(af_array *keys_out, af_array *vals_out,
280280
case u8:
281281
reduce_key<op, uchar, uchar>(keys_out, vals_out, keys, vals,
282282
dim);
283+
break;
283284
case f16:
284285
reduce_key<op, half, half>(keys_out, vals_out, keys, vals, dim);
285286
break;

test/reduce.cpp

Lines changed: 102 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ using std::vector;
3636
template<typename T>
3737
class Reduce : public ::testing::Test {};
3838

39+
template<typename T>
40+
class ReduceByKey : public ::testing::Test {};
41+
3942
typedef ::testing::Types<float, double, cfloat, cdouble, uint, int, intl, uintl,
4043
uchar, short, ushort>
4144
TestTypes;
4245
TYPED_TEST_SUITE(Reduce, TestTypes);
46+
TYPED_TEST_SUITE(ReduceByKey, TestTypes);
4347

4448
typedef af_err (*reduceFunc)(af_array *, const af_array, const int);
4549

@@ -154,6 +158,16 @@ struct promote_type<ushort, af_product> {
154158
typedef uint type;
155159
};
156160

161+
// float16 is promoted to float32 for sum and product
162+
template<>
163+
struct promote_type<half_float::half, af_sum> {
164+
typedef float type;
165+
};
166+
template<>
167+
struct promote_type<half_float::half, af_product> {
168+
typedef float type;
169+
};
170+
157171
#define REDUCE_TESTS(FN) \
158172
TYPED_TEST(Reduce, Test_##FN) { \
159173
reduceTest<TypeParam, typename promote_type<TypeParam, af_##FN>::type, \
@@ -598,12 +612,16 @@ TEST_P(ReduceByKeyP, SumDim2) {
598612
ASSERT_ARRAYS_NEAR(valsReducedGold, valsReduced, 1e-5);
599613
}
600614

601-
TEST(ReduceByKey, MultiBlockReduceSingleval) {
615+
TYPED_TEST(ReduceByKey, MultiBlockReduceSingleval) {
616+
SUPPORTED_TYPE_CHECK(TypeParam);
602617
array keys = constant(0, 1024 * 1024, s32);
603-
array vals = constant(1, 1024 * 1024, f32);
618+
array vals = constant(1, 1024 * 1024,
619+
(af_dtype)af::dtype_traits<TypeParam>::af_type);
604620

605621
array keyResGold = constant(0, 1);
606-
array valsReducedGold = constant(1024 * 1024, 1, f32);
622+
using promoted_t = typename promote_type<TypeParam, af_sum>::type;
623+
array valsReducedGold = constant(1024 * 1024, 1,
624+
(af_dtype)af::dtype_traits<promoted_t>::af_type);
607625

608626
array keyRes, valsReduced;
609627
sumByKey(keyRes, valsReduced, keys, vals);
@@ -701,10 +719,11 @@ TEST(ReduceByKey, MultiBlockReduceByKeyRandom500) {
701719
reduce_by_key_test(string(TEST_DIR "/reduce/test_random500_by_key.test"));
702720
}
703721

704-
TEST(ReduceByKey, productReduceByKey) {
705-
const static int testSz = 8;
706-
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
707-
const float testVals[testSz] = {0, 7, 1, 6, 2, 5, 3, 4};
722+
TYPED_TEST(ReduceByKey, productReduceByKey) {
723+
SUPPORTED_TYPE_CHECK(TypeParam);
724+
const static int testSz = 8;
725+
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
726+
const TypeParam testVals[testSz] = {0, 7, 1, 6, 2, 5, 3, 4};
708727

709728
array keys(testSz, testKeys);
710729
array vals(testSz, testVals);
@@ -713,15 +732,17 @@ TEST(ReduceByKey, productReduceByKey) {
713732
productByKey(reduced_keys, reduced_vals, keys, vals, 0, 1);
714733

715734
const int goldSz = 5;
716-
const vector<float> gold_reduce{0, 7, 6, 30, 4};
735+
using promoted_t = typename promote_type<TypeParam, af_product>::type;
736+
const vector<promoted_t> gold_reduce{0, 7, 6, 30, 4};
717737

718738
ASSERT_VEC_ARRAY_EQ(gold_reduce, goldSz, reduced_vals);
719739
}
720740

721-
TEST(ReduceByKey, minReduceByKey) {
722-
const static int testSz = 8;
723-
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
724-
const float testVals[testSz] = {0, 7, 1, 6, 2, 5, 3, 4};
741+
TYPED_TEST(ReduceByKey, minReduceByKey) {
742+
SUPPORTED_TYPE_CHECK(TypeParam);
743+
const static int testSz = 8;
744+
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
745+
const TypeParam testVals[testSz] = {0, 7, 1, 6, 2, 5, 3, 4};
725746

726747
array keys(testSz, testKeys);
727748
array vals(testSz, testVals);
@@ -730,14 +751,15 @@ TEST(ReduceByKey, minReduceByKey) {
730751
minByKey(reduced_keys, reduced_vals, keys, vals);
731752

732753
const int goldSz = 5;
733-
const vector<float> gold_reduce{0, 1, 6, 2, 4};
754+
const vector<TypeParam> gold_reduce{0, 1, 6, 2, 4};
734755
ASSERT_VEC_ARRAY_EQ(gold_reduce, goldSz, reduced_vals);
735756
}
736757

737-
TEST(ReduceByKey, maxReduceByKey) {
738-
const static int testSz = 8;
739-
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
740-
const float testVals[testSz] = {0, 7, 1, 6, 2, 5, 3, 4};
758+
TYPED_TEST(ReduceByKey, maxReduceByKey) {
759+
SUPPORTED_TYPE_CHECK(TypeParam);
760+
const static int testSz = 8;
761+
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
762+
const TypeParam testVals[testSz] = {0, 7, 1, 6, 2, 5, 3, 4};
741763

742764
array keys(testSz, testKeys);
743765
array vals(testSz, testVals);
@@ -746,14 +768,15 @@ TEST(ReduceByKey, maxReduceByKey) {
746768
maxByKey(reduced_keys, reduced_vals, keys, vals);
747769

748770
const int goldSz = 5;
749-
const vector<float> gold_reduce{0, 7, 6, 5, 4};
771+
const vector<TypeParam> gold_reduce{0, 7, 6, 5, 4};
750772
ASSERT_VEC_ARRAY_EQ(gold_reduce, goldSz, reduced_vals);
751773
}
752774

753-
TEST(ReduceByKey, allTrueReduceByKey) {
754-
const static int testSz = 8;
755-
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
756-
const float testVals[testSz] = {0, 1, 1, 1, 0, 1, 1, 1};
775+
TYPED_TEST(ReduceByKey, allTrueReduceByKey) {
776+
SUPPORTED_TYPE_CHECK(TypeParam);
777+
const static int testSz = 8;
778+
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
779+
const TypeParam testVals[testSz] = {0, 1, 1, 1, 0, 1, 1, 1};
757780

758781
array keys(testSz, testKeys);
759782
array vals(testSz, testVals);
@@ -766,10 +789,11 @@ TEST(ReduceByKey, allTrueReduceByKey) {
766789
ASSERT_VEC_ARRAY_EQ(gold_reduce, goldSz, reduced_vals);
767790
}
768791

769-
TEST(ReduceByKey, anyTrueReduceByKey) {
770-
const static int testSz = 8;
771-
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 8, 8};
772-
const float testVals[testSz] = {0, 1, 1, 1, 0, 1, 0, 0};
792+
TYPED_TEST(ReduceByKey, anyTrueReduceByKey) {
793+
SUPPORTED_TYPE_CHECK(TypeParam);
794+
const static int testSz = 8;
795+
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 8, 8};
796+
const TypeParam testVals[testSz] = {0, 1, 1, 1, 0, 1, 0, 0};
773797

774798
array keys(testSz, testKeys);
775799
array vals(testSz, testVals);
@@ -783,10 +807,11 @@ TEST(ReduceByKey, anyTrueReduceByKey) {
783807
ASSERT_VEC_ARRAY_EQ(gold_reduce, goldSz, reduced_vals);
784808
}
785809

786-
TEST(ReduceByKey, countReduceByKey) {
787-
const static int testSz = 8;
788-
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 5};
789-
const float testVals[testSz] = {0, 1, 1, 1, 0, 1, 1, 1};
810+
TYPED_TEST(ReduceByKey, countReduceByKey) {
811+
SUPPORTED_TYPE_CHECK(TypeParam);
812+
const static int testSz = 8;
813+
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 5};
814+
const TypeParam testVals[testSz] = {0, 1, 1, 1, 0, 1, 1, 1};
790815

791816
array keys(testSz, testKeys);
792817
array vals(testSz, testVals);
@@ -799,11 +824,18 @@ TEST(ReduceByKey, countReduceByKey) {
799824
ASSERT_VEC_ARRAY_EQ(gold_reduce, goldSz, reduced_vals);
800825
}
801826

802-
TEST(ReduceByKey, ReduceByKeyNans) {
827+
TYPED_TEST(ReduceByKey, ReduceByKeyNans) {
828+
if (!IsFloatingPoint<TypeParam>::value) {
829+
SUCCEED() << "Not a floating point type.";
830+
return;
831+
}
832+
803833
SKIP_IF_FAST_MATH_ENABLED();
804-
const static int testSz = 8;
805-
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
806-
const float testVals[testSz] = {0, 7, NAN, 6, 2, 5, 3, 4};
834+
SUPPORTED_TYPE_CHECK(TypeParam);
835+
const static int testSz = 8;
836+
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
837+
const TypeParam nan = std::numeric_limits<TypeParam>::quiet_NaN();
838+
const TypeParam testVals[testSz] = {0, 7, nan, 6, 2, 5, 3, 4};
807839

808840
array keys(testSz, testKeys);
809841
array vals(testSz, testVals);
@@ -812,14 +844,16 @@ TEST(ReduceByKey, ReduceByKeyNans) {
812844
productByKey(reduced_keys, reduced_vals, keys, vals, 0, 1);
813845

814846
const int goldSz = 5;
815-
const vector<float> gold_reduce{0, 7, 6, 30, 4};
847+
using promoted_t = typename promote_type<TypeParam, af_product>::type;
848+
const vector<promoted_t> gold_reduce{0, 7, 6, 30, 4};
816849
ASSERT_VEC_ARRAY_EQ(gold_reduce, goldSz, reduced_vals);
817850
}
818851

819-
TEST(ReduceByKey, nDim0ReduceByKey) {
820-
const static int testSz = 8;
821-
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
822-
const float testVals[testSz] = {0, 7, 1, 6, 2, 5, 3, 4};
852+
TYPED_TEST(ReduceByKey, nDim0ReduceByKey) {
853+
SUPPORTED_TYPE_CHECK(TypeParam);
854+
const static int testSz = 8;
855+
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
856+
const TypeParam testVals[testSz] = {0, 7, 1, 6, 2, 5, 3, 4};
823857

824858
array keys(testSz, testKeys);
825859
array vals(testSz, testVals);
@@ -833,20 +867,22 @@ TEST(ReduceByKey, nDim0ReduceByKey) {
833867
sumByKey(reduced_keys, reduced_vals, keys, vals, dim, nanval);
834868

835869
const dim4 goldSz(5, 2, 2, 2);
836-
const vector<float> gold_reduce{0, 8, 6, 10, 4, 0, 8, 6, 10, 4,
870+
using promoted_t = typename promote_type<TypeParam, af_sum>::type;
871+
const vector<promoted_t> gold_reduce{0, 8, 6, 10, 4, 0, 8, 6, 10, 4,
837872

838-
0, 8, 6, 10, 4, 0, 8, 6, 10, 4,
873+
0, 8, 6, 10, 4, 0, 8, 6, 10, 4,
839874

840-
0, 8, 6, 10, 4, 0, 8, 6, 10, 4,
875+
0, 8, 6, 10, 4, 0, 8, 6, 10, 4,
841876

842-
0, 8, 6, 10, 4, 0, 8, 6, 10, 4};
877+
0, 8, 6, 10, 4, 0, 8, 6, 10, 4};
843878
ASSERT_VEC_ARRAY_EQ(gold_reduce, goldSz, reduced_vals);
844879
}
845880

846-
TEST(ReduceByKey, nDim1ReduceByKey) {
847-
const static int testSz = 8;
848-
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
849-
const float testVals[testSz] = {0, 7, 1, 6, 2, 5, 3, 4};
881+
TYPED_TEST(ReduceByKey, nDim1ReduceByKey) {
882+
SUPPORTED_TYPE_CHECK(TypeParam);
883+
const static int testSz = 8;
884+
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
885+
const TypeParam testVals[testSz] = {0, 7, 1, 6, 2, 5, 3, 4};
850886

851887
array keys(testSz, testKeys);
852888
array vals(testSz, testVals);
@@ -861,19 +897,21 @@ TEST(ReduceByKey, nDim1ReduceByKey) {
861897
sumByKey(reduced_keys, reduced_vals, keys, vals, dim, nanval);
862898

863899
const int goldSz = 5;
864-
const float gold_reduce[goldSz] = {0, 8, 6, 10, 4};
865-
vector<float> hreduce(reduced_vals.elements());
900+
using promoted_t = typename promote_type<TypeParam, af_sum>::type;
901+
const promoted_t gold_reduce[goldSz] = {0, 8, 6, 10, 4};
902+
vector<promoted_t> hreduce(reduced_vals.elements());
866903
reduced_vals.host(hreduce.data());
867904

868905
for (int i = 0; i < goldSz * ntile; i++) {
869906
ASSERT_EQ(gold_reduce[i / ntile], hreduce[i]);
870907
}
871908
}
872909

873-
TEST(ReduceByKey, nDim2ReduceByKey) {
874-
const static int testSz = 8;
875-
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
876-
const float testVals[testSz] = {0, 7, 1, 6, 2, 5, 3, 4};
910+
TYPED_TEST(ReduceByKey, nDim2ReduceByKey) {
911+
SUPPORTED_TYPE_CHECK(TypeParam);
912+
const static int testSz = 8;
913+
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
914+
const TypeParam testVals[testSz] = {0, 7, 1, 6, 2, 5, 3, 4};
877915

878916
array keys(testSz, testKeys);
879917
array vals(testSz, testVals);
@@ -888,19 +926,21 @@ TEST(ReduceByKey, nDim2ReduceByKey) {
888926
sumByKey(reduced_keys, reduced_vals, keys, vals, dim, nanval);
889927

890928
const int goldSz = 5;
891-
const float gold_reduce[goldSz] = {0, 8, 6, 10, 4};
892-
vector<float> h_a(reduced_vals.elements());
929+
using promoted_t = typename promote_type<TypeParam, af_sum>::type;
930+
const promoted_t gold_reduce[goldSz] = {0, 8, 6, 10, 4};
931+
vector<promoted_t> h_a(reduced_vals.elements());
893932
reduced_vals.host(h_a.data());
894933

895934
for (int i = 0; i < goldSz * ntile; i++) {
896935
ASSERT_EQ(gold_reduce[i / ntile], h_a[i]);
897936
}
898937
}
899938

900-
TEST(ReduceByKey, nDim3ReduceByKey) {
901-
const static int testSz = 8;
902-
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
903-
const float testVals[testSz] = {0, 7, 1, 6, 2, 5, 3, 4};
939+
TYPED_TEST(ReduceByKey, nDim3ReduceByKey) {
940+
SUPPORTED_TYPE_CHECK(TypeParam);
941+
const static int testSz = 8;
942+
const int testKeys[testSz] = {0, 2, 2, 9, 5, 5, 5, 8};
943+
const TypeParam testVals[testSz] = {0, 7, 1, 6, 2, 5, 3, 4};
904944

905945
array keys(testSz, testKeys);
906946
array vals(testSz, testVals);
@@ -915,8 +955,9 @@ TEST(ReduceByKey, nDim3ReduceByKey) {
915955
sumByKey(reduced_keys, reduced_vals, keys, vals, dim, nanval);
916956

917957
const int goldSz = 5;
918-
const float gold_reduce[goldSz] = {0, 8, 6, 10, 4};
919-
vector<float> h_a(reduced_vals.elements());
958+
using promoted_t = typename promote_type<TypeParam, af_sum>::type;
959+
const promoted_t gold_reduce[goldSz] = {0, 8, 6, 10, 4};
960+
vector<promoted_t> h_a(reduced_vals.elements());
920961
reduced_vals.host(h_a.data());
921962

922963
for (int i = 0; i < goldSz * ntile; i++) {

0 commit comments

Comments
 (0)