arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-backend/src/cpu_spqlios/spqlios-arithmetic/test/spqlios_zn_vmp_test.cpp
poulpy-backend/src/cpu_spqlios/spqlios-arithmetic/test/spqlios_zn_vmp_test.cppRaw
 1#include "gtest/gtest.h"
 2#include "spqlios/arithmetic/zn_arithmetic_private.h"
 3#include "testlib/zn_layouts.h"
 4
 5static void test_zn_vmp_prepare(ZN32_VMP_PREPARE_CONTIGUOUS_F prepare_contiguous) {
 6  MOD_Z* module = new_z_module_info(DEFAULT);
 7  for (uint64_t nrows : {1, 2, 5, 15}) {
 8    for (uint64_t ncols : {1, 2, 32, 42, 67}) {
 9      std::vector<int32_t> src(nrows * ncols);
10      zn32_pmat_layout out(nrows, ncols);
11      for (int32_t& x : src) x = uniform_i64_bits(32);
12      prepare_contiguous(module, out.data, src.data(), nrows, ncols);
13      for (uint64_t i = 0; i < nrows; ++i) {
14        for (uint64_t j = 0; j < ncols; ++j) {
15          int32_t in = src[i * ncols + j];
16          int32_t actual = out.get(i, j);
17          ASSERT_EQ(actual, in);
18        }
19      }
20    }
21  }
22  delete_z_module_info(module);
23}
24
25TEST(zn, zn32_vmp_prepare_contiguous) { test_zn_vmp_prepare(zn32_vmp_prepare_contiguous); }
26TEST(zn, default_zn32_vmp_prepare_contiguous_ref) { test_zn_vmp_prepare(default_zn32_vmp_prepare_contiguous_ref); }
27
28static void test_zn_vmp_prepare(ZN32_VMP_PREPARE_DBLPTR_F prepare_dblptr) {
29  MOD_Z* module = new_z_module_info(DEFAULT);
30  for (uint64_t nrows : {1, 2, 5, 15}) {
31    for (uint64_t ncols : {1, 2, 32, 42, 67}) {
32      std::vector<int32_t> src(nrows * ncols);
33      zn32_pmat_layout out(nrows, ncols);
34      for (int32_t& x : src) x = uniform_i64_bits(32);
35      const int32_t** mat_dblptr = (const int32_t**)malloc(nrows * sizeof(int32_t*));
36      for (size_t row_i = 0; row_i < nrows; row_i++) {
37        mat_dblptr[row_i] = &src.data()[row_i * ncols];
38      };
39      prepare_dblptr(module, out.data, mat_dblptr, nrows, ncols);
40      for (uint64_t i = 0; i < nrows; ++i) {
41        for (uint64_t j = 0; j < ncols; ++j) {
42          int32_t in = src[i * ncols + j];
43          int32_t actual = out.get(i, j);
44          ASSERT_EQ(actual, in);
45        }
46      }
47    }
48  }
49  delete_z_module_info(module);
50}
51
52TEST(zn, zn32_vmp_prepare_dblptr) { test_zn_vmp_prepare(zn32_vmp_prepare_dblptr); }
53TEST(zn, default_zn32_vmp_prepare_dblptr_ref) { test_zn_vmp_prepare(default_zn32_vmp_prepare_dblptr_ref); }
54
55template <typename INTTYPE>
56static void test_zn_vmp_apply(void (*apply)(const MOD_Z*, int32_t*, uint64_t, const INTTYPE*, uint64_t,
57                                            const ZN32_VMP_PMAT*, uint64_t, uint64_t)) {
58  MOD_Z* module = new_z_module_info(DEFAULT);
59  for (uint64_t nrows : {1, 2, 5, 15}) {
60    for (uint64_t ncols : {1, 2, 32, 42, 67}) {
61      for (uint64_t a_size : {1, 2, 5, 15}) {
62        for (uint64_t res_size : {1, 2, 32, 42, 67}) {
63          std::vector<INTTYPE> a(a_size);
64          zn32_pmat_layout out(nrows, ncols);
65          std::vector<int32_t> res(res_size);
66          for (INTTYPE& x : a) x = uniform_i64_bits(32);
67          out.fill_random();
68          std::vector<int32_t> expect = vmp_product(a.data(), a_size, res_size, out);
69          apply(module, res.data(), res_size, a.data(), a_size, out.data, nrows, ncols);
70          for (uint64_t i = 0; i < res_size; ++i) {
71            int32_t exp = expect[i];
72            int32_t actual = res[i];
73            ASSERT_EQ(actual, exp);
74          }
75        }
76      }
77    }
78  }
79  delete_z_module_info(module);
80}
81
82TEST(zn, zn32_vmp_apply_i32) { test_zn_vmp_apply(zn32_vmp_apply_i32); }
83TEST(zn, zn32_vmp_apply_i16) { test_zn_vmp_apply(zn32_vmp_apply_i16); }
84TEST(zn, zn32_vmp_apply_i8) { test_zn_vmp_apply(zn32_vmp_apply_i8); }
85
86TEST(zn, default_zn32_vmp_apply_i32_ref) { test_zn_vmp_apply(default_zn32_vmp_apply_i32_ref); }
87TEST(zn, default_zn32_vmp_apply_i16_ref) { test_zn_vmp_apply(default_zn32_vmp_apply_i16_ref); }
88TEST(zn, default_zn32_vmp_apply_i8_ref) { test_zn_vmp_apply(default_zn32_vmp_apply_i8_ref); }
89
90#ifdef __x86_64__
91TEST(zn, default_zn32_vmp_apply_i32_avx) { test_zn_vmp_apply(default_zn32_vmp_apply_i32_avx); }
92TEST(zn, default_zn32_vmp_apply_i16_avx) { test_zn_vmp_apply(default_zn32_vmp_apply_i16_avx); }
93TEST(zn, default_zn32_vmp_apply_i8_avx) { test_zn_vmp_apply(default_zn32_vmp_apply_i8_avx); }
94#endif