1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/*
-- MAGMA (version 2.9.0) --
Univ. of Tennessee, Knoxville
Univ. of California, Berkeley
Univ. of Colorado, Denver
@date January 2025
@precisions normal z -> c d s
@author Mark Gates
*/
// includes, system
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
// includes, project
#include "flops.h"
#include "magma_v2.h"
#include "magma_lapack.h"
#include "testings.h"
// Initialize matrix to random.
// This ensures the same ISEED is always used,
// so we can re-generate the identical matrix.
void init_matrix(
magma_opts &opts,
magma_int_t m, magma_int_t n,
magmaDoubleComplex *A, magma_int_t lda )
{
magma_int_t iseed_save[4];
for (magma_int_t i = 0; i < 4; ++i) {
iseed_save[i] = opts.iseed[i];
}
magma_generate_matrix( opts, m, n, A, lda );
// restore iseed
for (magma_int_t i = 0; i < 4; ++i) {
opts.iseed[i] = iseed_save[i];
}
}
// On input, A and ipiv is LU factorization of A. On output, A is overwritten.
// Requires m == n.
// Uses init_matrix() to re-generate original A as needed.
// Generates random RHS b and solves Ax=b.
// Returns residual, |Ax - b| / (n |A| |x|).
double get_residual(
magma_opts &opts,
magma_int_t m, magma_int_t n,
magmaDoubleComplex *A, magma_int_t lda,
magma_int_t *ipiv )
{
if ( m != n ) {
printf( "\nERROR: residual check defined only for square matrices\n" );
return -1;
}
const magmaDoubleComplex c_one = MAGMA_Z_ONE;
const magmaDoubleComplex c_neg_one = MAGMA_Z_NEG_ONE;
const magma_int_t ione = 1;
// this seed should be DIFFERENT than used in init_matrix
// (else x is column of A, so residual can be exactly zero)
magma_int_t ISEED[4] = {0,0,0,1};
magma_int_t info = 0;
magmaDoubleComplex *x, *b;
// initialize RHS
TESTING_CHECK( magma_zmalloc_cpu( &x, n ));
TESTING_CHECK( magma_zmalloc_cpu( &b, n ));
lapackf77_zlarnv( &ione, ISEED, &n, b );
blasf77_zcopy( &n, b, &ione, x, &ione );
// solve Ax = b
lapackf77_zgetrs( "Notrans", &n, &ione, A, &lda, ipiv, x, &n, &info );
if (info != 0) {
printf("lapackf77_zgetrs returned error %lld: %s.\n",
(long long) info, magma_strerror( info ));
}
// reset to original A
init_matrix( opts, m, n, A, lda );
// compute r = Ax - b, saved in b
blasf77_zgemv( "Notrans", &m, &n, &c_one, A, &lda, x, &ione, &c_neg_one, b, &ione );
// compute residual |Ax - b| / (n*|A|*|x|)
double norm_x, norm_A, norm_r, work[1];
norm_A = lapackf77_zlange( "F", &m, &n, A, &lda, work );
norm_r = lapackf77_zlange( "F", &n, &ione, b, &n, work );
norm_x = lapackf77_zlange( "F", &n, &ione, x, &n, work );
//printf( "r=\n" ); magma_zprint( 1, n, b, 1 );
magma_free_cpu( x );
magma_free_cpu( b );
//printf( "r=%.2e, A=%.2e, x=%.2e, n=%lld\n", norm_r, norm_A, norm_x, (long long) n );
return norm_r / (n * norm_A * norm_x);
}
// On input, LU and ipiv is LU factorization of A. On output, LU is overwritten.
// Works for any m, n.
// Uses init_matrix() to re-generate original A as needed.
// Returns error in factorization, |PA - LU| / (n |A|)
// This allocates 3 more matrices to store A, L, and U.
double get_LU_error(
magma_opts &opts,
magma_int_t M, magma_int_t N,
magmaDoubleComplex *LU, magma_int_t lda,
magma_int_t *ipiv)
{
magma_int_t min_mn = min(M,N);
magma_int_t ione = 1;
magma_int_t i, j;
magmaDoubleComplex alpha = MAGMA_Z_ONE;
magmaDoubleComplex beta = MAGMA_Z_ZERO;
magmaDoubleComplex *A, *L, *U;
double work[1], matnorm, residual;
TESTING_CHECK( magma_zmalloc_cpu( &A, lda*N ));
TESTING_CHECK( magma_zmalloc_cpu( &L, M*min_mn ));
TESTING_CHECK( magma_zmalloc_cpu( &U, min_mn*N ));
memset( L, 0, M*min_mn*sizeof(magmaDoubleComplex) );
memset( U, 0, min_mn*N*sizeof(magmaDoubleComplex) );
// set to original A
init_matrix( opts, M, N, A, lda );
lapackf77_zlaswp( &N, A, &lda, &ione, &min_mn, ipiv, &ione);
// copy LU to L and U, and set diagonal to 1
lapackf77_zlacpy( MagmaLowerStr, &M, &min_mn, LU, &lda, L, &M );
lapackf77_zlacpy( MagmaUpperStr, &min_mn, &N, LU, &lda, U, &min_mn );
for (j=0; j < min_mn; j++)
L[j+j*M] = MAGMA_Z_MAKE( 1., 0. );
matnorm = lapackf77_zlange("f", &M, &N, A, &lda, work);
blasf77_zgemm("N", "N", &M, &N, &min_mn,
&alpha, L, &M, U, &min_mn, &beta, LU, &lda);
for( j = 0; j < N; j++ ) {
for( i = 0; i < M; i++ ) {
LU[i+j*lda] = MAGMA_Z_SUB( LU[i+j*lda], A[i+j*lda] );
}
}
residual = lapackf77_zlange("f", &M, &N, LU, &lda, work);
magma_free_cpu( A );
magma_free_cpu( L );
magma_free_cpu( U );
return residual / (matnorm * N);
}
/* ////////////////////////////////////////////////////////////////////////////
-- Testing zgetrf
*/
int main( int argc, char** argv)
{
TESTING_CHECK( magma_init() );
magma_print_environment();
real_Double_t gflops, gpu_perf, gpu_time, cpu_perf=0, cpu_time=0;
double error;
magmaDoubleComplex *h_A;
magma_int_t *ipiv;
magma_int_t M, N, n2, lda, info, min_mn;
int status = 0;
magma_opts opts;
opts.parse_opts( argc, argv );
double tol = opts.tolerance * lapackf77_dlamch("E");
printf("%% ngpu %lld, version %lld\n", (long long) opts.ngpu, (long long) opts.version );
if ( opts.check == 2 ) {
printf("%% M N CPU Gflop/s (sec) GPU Gflop/s (sec) |Ax-b|/(N*|A|*|x|)\n");
}
else {
printf("%% M N CPU Gflop/s (sec) GPU Gflop/s (sec) |PA-LU|/(N*|A|)\n");
}
printf("%%========================================================================\n");
for( int itest = 0; itest < opts.ntest; ++itest ) {
for( int iter = 0; iter < opts.niter; ++iter ) {
M = opts.msize[itest];
N = opts.nsize[itest];
min_mn = min(M, N);
lda = M;
n2 = lda*N;
gflops = FLOPS_ZGETRF( M, N ) / 1e9;
TESTING_CHECK( magma_imalloc_cpu( &ipiv, min_mn ));
TESTING_CHECK( magma_zmalloc_pinned( &h_A, n2 ));
/* =====================================================================
Performs operation using LAPACK
=================================================================== */
if ( opts.lapack ) {
init_matrix( opts, M, N, h_A, lda );
cpu_time = magma_wtime();
lapackf77_zgetrf( &M, &N, h_A, &lda, ipiv, &info );
cpu_time = magma_wtime() - cpu_time;
cpu_perf = gflops / cpu_time;
if (info != 0) {
printf("lapackf77_zgetrf returned error %lld: %s.\n",
(long long) info, magma_strerror( info ));
}
}
/* ====================================================================
Performs operation using MAGMA
=================================================================== */
init_matrix( opts, M, N, h_A, lda );
if ( opts.version == 2 || opts.version == 3 ) {
// no pivoting versions, so set ipiv to identity
for (magma_int_t i=0; i < min_mn; ++i ) {
ipiv[i] = i+1;
}
}
gpu_time = magma_wtime();
if ( opts.version == 1 ) {
magma_zgetrf( M, N, h_A, lda, ipiv, &info );
}
else if ( opts.version == 2 ) {
magma_zgetrf_nopiv( M, N, h_A, lda, &info );
}
else if ( opts.version == 3 ) {
magma_zgetf2_nopiv( M, N, h_A, lda, &info );
}
gpu_time = magma_wtime() - gpu_time;
gpu_perf = gflops / gpu_time;
if (info != 0) {
printf("magma_zgetrf returned error %lld: %s.\n",
(long long) info, magma_strerror( info ));
}
/* =====================================================================
Check the factorization
=================================================================== */
if ( opts.lapack ) {
printf("%5lld %5lld %7.2f (%7.2f) %7.2f (%7.2f)",
(long long) M, (long long) N, cpu_perf, cpu_time, gpu_perf, gpu_time );
}
else {
printf("%5lld %5lld --- ( --- ) %7.2f (%7.2f)",
(long long) M, (long long) N, gpu_perf, gpu_time );
}
if ( opts.check == 2 ) {
error = get_residual( opts, M, N, h_A, lda, ipiv );
printf(" %8.2e %s\n", error, (error < tol ? "ok" : "failed"));
status += ! (error < tol);
}
else if ( opts.check ) {
error = get_LU_error( opts, M, N, h_A, lda, ipiv );
printf(" %8.2e %s\n", error, (error < tol ? "ok" : "failed"));
status += ! (error < tol);
}
else {
printf(" --- \n");
}
magma_free_cpu( ipiv );
magma_free_pinned( h_A );
fflush( stdout );
}
if ( opts.niter > 1 ) {
printf( "\n" );
}
}
opts.cleanup();
TESTING_CHECK( magma_finalize() );
return status;
}
|