OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
TestGSLBLAS.cpp
Go to the documentation of this file.
1
38#include <gtest/gtest.h>
39#include <cmath>
40#include "Utilities/GSLBLAS.h"
42#include "Utilities/GSLMatrix.h"
43
44class GSLBLASTest : public ::testing::Test {
45protected:
46 void SetUp() override {
47 // Test setup
48 }
49};
50
51TEST_F(GSLBLASTest, DGEMM_NoTrans) {
52 // Test C = A * B
56
57 // A = [1 2 3]
58 // [4 5 6]
59 gsl_matrix_set(A, 0, 0, 1.0);
60 gsl_matrix_set(A, 0, 1, 2.0);
61 gsl_matrix_set(A, 0, 2, 3.0);
62 gsl_matrix_set(A, 1, 0, 4.0);
63 gsl_matrix_set(A, 1, 1, 5.0);
64 gsl_matrix_set(A, 1, 2, 6.0);
65
66 // B = [1 2]
67 // [3 4]
68 // [5 6]
69 gsl_matrix_set(B, 0, 0, 1.0);
70 gsl_matrix_set(B, 0, 1, 2.0);
71 gsl_matrix_set(B, 1, 0, 3.0);
72 gsl_matrix_set(B, 1, 1, 4.0);
73 gsl_matrix_set(B, 2, 0, 5.0);
74 gsl_matrix_set(B, 2, 1, 6.0);
75
77 gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, A, B, 0.0, C);
78
79 // Expected: C = [22 28]
80 // [49 64]
81 EXPECT_NEAR(gsl_matrix_get(C, 0, 0), 22.0, 1e-10);
82 EXPECT_NEAR(gsl_matrix_get(C, 0, 1), 28.0, 1e-10);
83 EXPECT_NEAR(gsl_matrix_get(C, 1, 0), 49.0, 1e-10);
84 EXPECT_NEAR(gsl_matrix_get(C, 1, 1), 64.0, 1e-10);
85
89}
90
91TEST_F(GSLBLASTest, DGEMM_WithBeta) {
92 // Test C = alpha*A*B + beta*C
96
99 gsl_matrix_set_all(C, 1.0);
100
101 // C = 2.0 * I * I + 3.0 * C = 2*I + 3*C = 2*I + 3*ones = [5 3]
102 // [3 5]
103 gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 2.0, A, B, 3.0, C);
104
105 EXPECT_NEAR(gsl_matrix_get(C, 0, 0), 5.0, 1e-10);
106 EXPECT_NEAR(gsl_matrix_get(C, 0, 1), 3.0, 1e-10);
107 EXPECT_NEAR(gsl_matrix_get(C, 1, 0), 3.0, 1e-10);
108 EXPECT_NEAR(gsl_matrix_get(C, 1, 1), 5.0, 1e-10);
109
113}
114
115TEST_F(GSLBLASTest, DGEMM_Transpose) {
116 // Test C = A^T * B
117 gsl_matrix* A = gsl_matrix_alloc(3, 2);
118 gsl_matrix* B = gsl_matrix_alloc(3, 2);
119 gsl_matrix* C = gsl_matrix_alloc(2, 2);
120
123
124 gsl_blas_dgemm(CblasTrans, CblasNoTrans, 1.0, A, B, 0.0, C);
125
126 // A^T * B = I^T * I = I * I = I
127 EXPECT_NEAR(gsl_matrix_get(C, 0, 0), 1.0, 1e-10);
128 EXPECT_NEAR(gsl_matrix_get(C, 0, 1), 0.0, 1e-10);
129 EXPECT_NEAR(gsl_matrix_get(C, 1, 0), 0.0, 1e-10);
130 EXPECT_NEAR(gsl_matrix_get(C, 1, 1), 1.0, 1e-10);
131
135}
136
138 // Test y = alpha*A*x + beta*y
139 gsl_matrix* A = gsl_matrix_alloc(2, 3);
142
144 gsl_vector_set_all(x, 1.0);
145 gsl_vector_set_all(y, 2.0);
146
147 // y = 1.0 * A * x + 1.0 * y = [1, 1, 0]^T + [2, 2]^T = [3, 2]^T
148 // (Note: A is 2x3 identity-like, so first two elements of x are used)
149 gsl_matrix_set(A, 0, 0, 1.0);
150 gsl_matrix_set(A, 0, 1, 0.0);
151 gsl_matrix_set(A, 0, 2, 0.0);
152 gsl_matrix_set(A, 1, 0, 0.0);
153 gsl_matrix_set(A, 1, 1, 1.0);
154 gsl_matrix_set(A, 1, 2, 0.0);
155
156 gsl_blas_dgemv(CblasNoTrans, 1.0, A, x, 1.0, y);
157
158 EXPECT_NEAR(gsl_vector_get(y, 0), 3.0, 1e-10);
159 EXPECT_NEAR(gsl_vector_get(y, 1), 3.0, 1e-10);
160
164}
165
166TEST_F(GSLBLASTest, ZGEMM_Complex) {
170
171 gsl_complex z1 = gsl_complex_rect(1.0, 0.0);
172 gsl_complex z2 = gsl_complex_rect(0.0, 0.0);
173
174 gsl_matrix_complex_set(A, 0, 0, z1);
175 gsl_matrix_complex_set(A, 0, 1, z2);
176 gsl_matrix_complex_set(A, 1, 0, z2);
177 gsl_matrix_complex_set(A, 1, 1, z1);
178
179 gsl_matrix_complex_set(B, 0, 0, z1);
180 gsl_matrix_complex_set(B, 0, 1, z2);
181 gsl_matrix_complex_set(B, 1, 0, z2);
182 gsl_matrix_complex_set(B, 1, 1, z1);
183
185 gsl_blas_zgemm(CblasNoTrans, CblasNoTrans, z1, A, B, z2, C);
186
187 // I * I = I
188 gsl_complex result = gsl_matrix_complex_get(C, 0, 0);
189 EXPECT_NEAR(GSL_REAL(result), 1.0, 1e-10);
190 EXPECT_NEAR(GSL_IMAG(result), 0.0, 1e-10);
191
195}
void gsl_blas_zgemm(CBLAS_TRANSPOSE TransA, CBLAS_TRANSPOSE TransB, gsl_complex alpha, const gsl_matrix_complex *A, const gsl_matrix_complex *B, gsl_complex beta, gsl_matrix_complex *C)
Complex matrix-matrix multiply and accumulate.
Definition GSLBLAS.h:90
@ CblasNoTrans
Definition GSLBLAS.h:32
@ CblasTrans
Definition GSLBLAS.h:32
void gsl_blas_dgemv(CBLAS_TRANSPOSE TransA, double alpha, const gsl_matrix *A, const gsl_vector *x, double beta, gsl_vector *y)
Real matrix-vector multiply and accumulate.
Definition GSLBLAS.h:144
void gsl_blas_dgemm(CBLAS_TRANSPOSE TransA, CBLAS_TRANSPOSE TransB, double alpha, const gsl_matrix *A, const gsl_matrix *B, double beta, gsl_matrix *C)
Real matrix-matrix multiply and accumulate.
Definition GSLBLAS.h:44
#define GSL_REAL(z)
Definition GSLCompat.h:117
#define GSL_IMAG(z)
Definition GSLCompat.h:118
gsl_complex gsl_complex_rect(double x, double y)
Construct .
Definition GSLComplex.h:49
gsl_matrix * gsl_matrix_alloc(size_t n1, size_t n2)
Allocate a zero-initialized real matrix of size .
Definition GSLMatrix.h:94
void gsl_matrix_set(gsl_matrix *m, size_t i, size_t j, double x)
Set .
Definition GSLMatrix.h:453
double gsl_matrix_get(const gsl_matrix *m, size_t i, size_t j)
Get .
Definition GSLMatrix.h:463
gsl_complex gsl_matrix_complex_get(const gsl_matrix_complex *m, size_t i, size_t j)
Get complex entry .
Definition GSLMatrix.h:510
void gsl_matrix_free(gsl_matrix *m)
Free a matrix allocated by gsl_matrix_alloc.
Definition GSLMatrix.h:108
void gsl_vector_free(gsl_vector *v)
Free a vector allocated by gsl_vector_alloc.
Definition GSLMatrix.h:160
gsl_vector * gsl_vector_alloc(size_t n)
Allocate a zero-initialized real vector of length .
Definition GSLMatrix.h:147
void gsl_matrix_complex_free(gsl_matrix_complex *m)
Free a matrix allocated by gsl_matrix_complex_alloc.
Definition GSLMatrix.h:136
void gsl_matrix_complex_set(gsl_matrix_complex *m, size_t i, size_t j, gsl_complex x)
Set complex entry .
Definition GSLMatrix.h:500
void gsl_vector_set_all(gsl_vector *v, double x)
Set all entries to x.
Definition GSLMatrix.h:541
double gsl_vector_get(const gsl_vector *v, size_t i)
Get .
Definition GSLMatrix.h:535
void gsl_matrix_set_all(gsl_matrix *m, double x)
Set all entries to x.
Definition GSLMatrix.h:471
void gsl_matrix_set_identity(gsl_matrix *m)
Set to identity on the main diagonal.
Definition GSLMatrix.h:486
gsl_matrix_complex * gsl_matrix_complex_alloc(size_t n1, size_t n2)
Allocate a zero-initialized complex matrix of size .
Definition GSLMatrix.h:120
void gsl_matrix_complex_set_zero(gsl_matrix_complex *m)
Set all complex entries to zero.
Definition GSLMatrix.h:517
void gsl_matrix_set_zero(gsl_matrix *m)
Set all entries to zero.
Definition GSLMatrix.h:480
TEST_F(GSLBLASTest, DGEMM_NoTrans)
void SetUp() override
Complex number stored as .
Definition GSLComplex.h:27
Dense complex matrix in row-major storage.
Definition GSLMatrix.h:45
Dense real matrix in row-major storage.
Definition GSLMatrix.h:29
Dense real vector with stride.
Definition GSLMatrix.h:61