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

Function sparseCastTester

test/sparse_common.hpp:207–255  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

205
206template<typename Ti, typename To>
207static void sparseCastTester(const int m, const int n, int factor) {
208 SUPPORTED_TYPE_CHECK(Ti);
209 SUPPORTED_TYPE_CHECK(To);
210
211 af::array A = cpu_randu<Ti>(af::dim4(m, n));
212
213 A = makeSparse<Ti>(A, factor);
214
215 af::array sTi = af::sparse(A, AF_STORAGE_CSR);
216
217 // Cast
218 af::array sTo = sTi.as((af::dtype)af::dtype_traits<To>::af_type);
219
220 // Verify nnZ
221 dim_t iNNZ = sparseGetNNZ(sTi);
222 dim_t oNNZ = sparseGetNNZ(sTo);
223
224 ASSERT_EQ(iNNZ, oNNZ);
225
226 // Verify Types
227 dim_t iSType = sparseGetStorage(sTi);
228 dim_t oSType = sparseGetStorage(sTo);
229
230 ASSERT_EQ(iSType, oSType);
231
232 // Get the individual arrays and verify equality
233 af::array iValues = sparseGetValues(sTi);
234 af::array iRowIdx = sparseGetRowIdx(sTi);
235 af::array iColIdx = sparseGetColIdx(sTi);
236
237 af::array oValues = sparseGetValues(sTo);
238 af::array oRowIdx = sparseGetRowIdx(sTo);
239 af::array oColIdx = sparseGetColIdx(sTo);
240
241 // Verify values
242 ASSERT_EQ(0, af::max<int>(af::abs(iRowIdx - oRowIdx)));
243 ASSERT_EQ(0, af::max<int>(af::abs(iColIdx - oColIdx)));
244
245 static const double eps = 1e-6;
246 if (iValues.iscomplex() && !oValues.iscomplex()) {
247 ASSERT_NEAR(0, af::max<double>(af::abs(af::abs(iValues) - oValues)),
248 eps);
249 } else if (!iValues.iscomplex() && oValues.iscomplex()) {
250 ASSERT_NEAR(0, af::max<double>(af::abs(iValues - af::abs(oValues))),
251 eps);
252 } else {
253 ASSERT_NEAR(0, af::max<double>(af::abs(iValues - oValues)), eps);
254 }
255}

Callers

nothing calls this directly

Calls 9

sparseFunction · 0.85
sparseGetNNZFunction · 0.85
sparseGetStorageFunction · 0.85
sparseGetValuesFunction · 0.85
sparseGetRowIdxFunction · 0.85
sparseGetColIdxFunction · 0.85
asMethod · 0.80
absFunction · 0.70
dim4Class · 0.50

Tested by

no test coverage detected