MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / topkTest

Function topkTest

test/topk.cpp:75–160  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

73
74template<typename T>
75void topkTest(const int ndims, const dim_t* dims, const unsigned k,
76 const int dim, const af_topk_function order) {
77 SUPPORTED_TYPE_CHECK(T);
78 af_dtype dtype = (af_dtype)dtype_traits<T>::af_type;
79
80 af_array input, output, outindex;
81
82 size_t ielems = 1;
83 size_t oelems = 1;
84
85 for (int i = 0; i < ndims; i++) {
86 ielems *= dims[i];
87 oelems *= (i == dim ? k : dims[i]);
88 }
89
90 size_t bCount = ielems / dims[dim];
91 size_t bSize = dims[dim];
92
93 vector<T> inData(ielems);
94 T val{std::numeric_limits<T>::lowest()};
95 generate(begin(inData), end(inData), [&]() {
96 increment_next(val);
97 return val;
98 });
99
100 random_device rnd_device;
101 mt19937 g(rnd_device());
102 shuffle(begin(inData), end(inData), g);
103
104 vector<T> outData(oelems);
105 vector<uint> outIdxs(oelems);
106
107 for (size_t b = 0; b < bCount; b++) {
108 using KeyValuePair = pair<T, uint>;
109
110 vector<KeyValuePair> kvPairs;
111 kvPairs.reserve(((b + 1) * bSize));
112
113 for (size_t i = b * bSize; i < ((b + 1) * bSize); ++i)
114 kvPairs.push_back(make_pair(inData[i], (i - b * bSize)));
115
116 if (order & AF_TOPK_MIN) {
117 stable_sort(kvPairs.begin(), kvPairs.end(),
118 [](const KeyValuePair& lhs, const KeyValuePair& rhs) {
119 return lhs.first < rhs.first;
120 });
121 } else {
122 stable_sort(kvPairs.begin(), kvPairs.end(),
123 [](const KeyValuePair& lhs, const KeyValuePair& rhs) {
124 return lhs.first > rhs.first;
125 });
126 }
127
128 auto it = kvPairs.begin();
129 for (size_t i = 0; i < k; ++it, ++i) {
130 outData[i + b * k] = it->first;
131 outIdxs[i + b * k] = it->second;
132 }

Callers

nothing calls this directly

Calls 7

beginFunction · 0.85
endFunction · 0.85
increment_nextFunction · 0.85
af_create_arrayFunction · 0.50
af_topkFunction · 0.50
af_get_data_ptrFunction · 0.50
af_release_arrayFunction · 0.50

Tested by

no test coverage detected