2023-11-10 17:42:30 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
|
// for linear algebra.
|
|
|
|
|
//
|
2026-02-25 07:45:02 -08:00
|
|
|
// Copyright (C) 2023 Rasmus Munk Larsen <rmlarsen@gmail.com>
|
2023-11-10 17:42:30 +00:00
|
|
|
//
|
|
|
|
|
// 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/.
|
|
|
|
|
|
|
|
|
|
#define EIGEN_GEMM_THREADPOOL
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
|
|
void test_parallelize_gemm() {
|
|
|
|
|
constexpr int n = 1024;
|
|
|
|
|
constexpr int num_threads = 4;
|
2023-12-22 03:18:46 +00:00
|
|
|
MatrixXf a = MatrixXf::Random(n, n);
|
|
|
|
|
MatrixXf b = MatrixXf::Random(n, n);
|
|
|
|
|
MatrixXf c = MatrixXf::Random(n, n);
|
2023-11-10 17:42:30 +00:00
|
|
|
c.noalias() = a * b;
|
|
|
|
|
|
|
|
|
|
ThreadPool pool(num_threads);
|
2024-08-09 09:35:23 -07:00
|
|
|
Eigen::setGemmThreadPool(&pool);
|
2023-11-10 17:42:30 +00:00
|
|
|
MatrixXf c_threaded(n, n);
|
|
|
|
|
c_threaded.noalias() = a * b;
|
|
|
|
|
|
|
|
|
|
VERIFY_IS_APPROX(c, c_threaded);
|
2026-03-12 12:32:06 -07:00
|
|
|
Eigen::setGemmThreadPool(nullptr);
|
2023-11-10 17:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-12 12:32:06 -07:00
|
|
|
void test_parallelize_gemm_varied() {
|
|
|
|
|
constexpr int num_threads = 4;
|
|
|
|
|
ThreadPool pool(num_threads);
|
|
|
|
|
|
|
|
|
|
// Non-square float
|
|
|
|
|
{
|
|
|
|
|
MatrixXf a = MatrixXf::Random(512, 2048);
|
|
|
|
|
MatrixXf b = MatrixXf::Random(2048, 256);
|
|
|
|
|
MatrixXf c_serial(512, 256);
|
|
|
|
|
c_serial.noalias() = a * b;
|
|
|
|
|
Eigen::setGemmThreadPool(&pool);
|
|
|
|
|
MatrixXf c_threaded(512, 256);
|
|
|
|
|
c_threaded.noalias() = a * b;
|
|
|
|
|
Eigen::setGemmThreadPool(nullptr);
|
|
|
|
|
VERIFY_IS_APPROX(c_serial, c_threaded);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Double
|
|
|
|
|
{
|
|
|
|
|
MatrixXd a = MatrixXd::Random(512, 512);
|
|
|
|
|
MatrixXd b = MatrixXd::Random(512, 512);
|
|
|
|
|
MatrixXd c_serial(512, 512);
|
|
|
|
|
c_serial.noalias() = a * b;
|
|
|
|
|
Eigen::setGemmThreadPool(&pool);
|
|
|
|
|
MatrixXd c_threaded(512, 512);
|
|
|
|
|
c_threaded.noalias() = a * b;
|
|
|
|
|
Eigen::setGemmThreadPool(nullptr);
|
|
|
|
|
VERIFY_IS_APPROX(c_serial, c_threaded);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Complex double
|
|
|
|
|
{
|
|
|
|
|
MatrixXcd a = MatrixXcd::Random(256, 256);
|
|
|
|
|
MatrixXcd b = MatrixXcd::Random(256, 256);
|
|
|
|
|
MatrixXcd c_serial(256, 256);
|
|
|
|
|
c_serial.noalias() = a * b;
|
|
|
|
|
Eigen::setGemmThreadPool(&pool);
|
|
|
|
|
MatrixXcd c_threaded(256, 256);
|
|
|
|
|
c_threaded.noalias() = a * b;
|
|
|
|
|
Eigen::setGemmThreadPool(nullptr);
|
|
|
|
|
VERIFY_IS_APPROX(c_serial, c_threaded);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EIGEN_DECLARE_TEST(product_threaded) {
|
|
|
|
|
CALL_SUBTEST_1(test_parallelize_gemm());
|
|
|
|
|
CALL_SUBTEST_2(test_parallelize_gemm_varied());
|
|
|
|
|
}
|