From 8a7f360ec3160acdb545d05712435d8ca0334357 Mon Sep 17 00:00:00 2001 From: Everton Constantino Date: Tue, 19 May 2020 19:24:11 +0000 Subject: - Vectorizing MMA packing. - Optimizing MMA kernel. - Adding PacketBlock store to blas_data_mapper. --- test/blasutil.cpp | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 test/blasutil.cpp (limited to 'test/blasutil.cpp') diff --git a/test/blasutil.cpp b/test/blasutil.cpp new file mode 100644 index 000000000..cd8716351 --- /dev/null +++ b/test/blasutil.cpp @@ -0,0 +1,196 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2020 Everton Constantino +// +// This Source Code Form is subject to the terms of the Mozilla +// Public License v. 2.0. If a copy of the MPL was not distributed +// with this file, You can obtain one at http://mozilla.org/MPL/2.0/ + +#include "main.h" + +#include + +using namespace Eigen; + +#define GET(i,j) (StorageOrder == RowMajor ? (i)*stride + (j) : (i) + (j)*stride) +#define SCATTER(i,j,k) (StorageOrder == RowMajor ? ((i)+(k))*stride + (j) : (i) + ((j)+(k))*stride) + +template +void compare(const Packet& a, const Packet& b) +{ + int pktsz = internal::packet_traits::size; + Scalar *buffA = new Scalar[pktsz]; + Scalar *buffB = new Scalar[pktsz]; + + internal::pstoreu(buffA, a); + internal::pstoreu(buffB, b); + + for(int i = 0; i < pktsz; i++) + { + VERIFY_IS_EQUAL(buffA[i], buffB[i]); + } + + delete[] buffA; + delete[] buffB; +} + +template +struct PacketBlockSet +{ + typedef typename internal::packet_traits::type Packet; + + void setPacketBlock(internal::PacketBlock& block, Scalar value) + { + for(int idx = 0; idx < n; idx++) + { + block.packet[idx] = internal::pset1(value); + } + } + + void comparePacketBlock(Scalar *data, int i, int j, int stride, internal::PacketBlock& block) + { + for(int idx = 0; idx < n; idx++) + { + Packet line = internal::ploadu(data + SCATTER(i,j,idx)); + compare(block.packet[idx], line); + } + } +}; + +template +void run_bdmp_spec_1() +{ + typedef internal::blas_data_mapper BlasDataMapper; + int packetSize = internal::packet_traits::size; + int minSize = std::max(packetSize, BlockSize); + typedef typename internal::packet_traits::type Packet; + + int szm = internal::random(minSize,500), szn = internal::random(minSize,500); + int stride = StorageOrder == RowMajor ? szn : szm; + Scalar *d = new Scalar[szn*szm]; + + // Initializing with random entries + for(int i = 0; i < szm*szn; i++) + { + d[i] = internal::random(static_cast(3), static_cast(10)); + } + + BlasDataMapper bdm(d, stride); + + // Testing operator() + for(int i = 0; i < szm; i++) + { + for(int j = 0; j < szn; j++) + { + VERIFY_IS_EQUAL(d[GET(i,j)], bdm(i,j)); + } + } + + // Testing getSubMapper and getLinearMapper + int i0 = internal::random(0,szm-2); + int j0 = internal::random(0,szn-2); + for(int i = i0; i < szm; i++) + { + for(int j = j0; j < szn; j++) + { + const BlasDataMapper& bdmSM = bdm.getSubMapper(i0,j0); + const internal::BlasLinearMapper& bdmLM = bdm.getLinearMapper(i0,j0); + + Scalar v = bdmSM(i - i0, j - j0); + Scalar vd = d[GET(i,j)]; + VERIFY_IS_EQUAL(vd, v); + VERIFY_IS_EQUAL(vd, bdmLM(GET(i-i0, j-j0))); + } + } + + // Testing loadPacket + for(int i = 0; i < szm - minSize; i++) + { + for(int j = 0; j < szn - minSize; j++) + { + Packet pktBDM = bdm.template loadPacket(i,j); + Packet pktD = internal::ploadu(d + GET(i,j)); + + compare(pktBDM, pktD); + } + } + + // Testing gatherPacket + Scalar *buff = new Scalar[packetSize]; + for(int i = 0; i < szm - minSize; i++) + { + for(int j = 0; j < szn - minSize; j++) + { + Packet p = bdm.template gatherPacket(i,j); + internal::pstoreu(buff, p); + + for(int k = 0; k < packetSize; k++) + { + VERIFY_IS_EQUAL(d[SCATTER(i,j,k)], buff[k]); + } + + } + } + delete[] buff; + + // Testing scatterPacket + for(int i = 0; i < szm - minSize; i++) + { + for(int j = 0; j < szn - minSize; j++) + { + Packet p = internal::pset1(static_cast(1)); + bdm.template scatterPacket(i,j,p); + for(int k = 0; k < packetSize; k++) + { + VERIFY_IS_EQUAL(d[SCATTER(i,j,k)], static_cast(1)); + } + } + } + + //Testing storePacketBlock + internal::PacketBlock block; + + PacketBlockSet pbs; + pbs.setPacketBlock(block, static_cast(2)); + + for(int i = 0; i < szm - minSize; i++) + { + for(int j = 0; j < szn - minSize; j++) + { + bdm.template storePacketBlock(i, j, block); + + pbs.comparePacketBlock(d, i, j, stride, block); + } + } + + delete[] d; +} + +template +void run_test() +{ + run_bdmp_spec_1(); + run_bdmp_spec_1(); + run_bdmp_spec_1(); + run_bdmp_spec_1(); + run_bdmp_spec_1(); + run_bdmp_spec_1(); + run_bdmp_spec_1(); + run_bdmp_spec_1(); + run_bdmp_spec_1(); + run_bdmp_spec_1(); +} + +EIGEN_DECLARE_TEST(blasutil) +{ + for(int i = 0; i < g_repeat; i++) + { + CALL_SUBTEST_1(run_test()); + CALL_SUBTEST_2(run_test()); + CALL_SUBTEST_3(run_test()); + CALL_SUBTEST_4(run_test()); + CALL_SUBTEST_5(run_test()); + CALL_SUBTEST_6(run_test()); + } +} \ No newline at end of file -- cgit v1.2.3