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
|
/*
-- MAGMA (version 2.9.0) --
Univ. of Tennessee, Knoxville
Univ. of California, Berkeley
Univ. of Colorado, Denver
@date January 2025
@generated from testing/testing_zgetri_gpu.cpp, normal z -> s, Wed Jan 22 14:40:30 2025
@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"
/* ////////////////////////////////////////////////////////////////////////////
-- Testing sgetri
*/
int main( int argc, char** argv )
{
TESTING_CHECK( magma_init() );
magma_print_environment();
// constants
const float c_zero = MAGMA_S_ZERO;
const float c_one = MAGMA_S_ONE;
const float c_neg_one = MAGMA_S_NEG_ONE;
real_Double_t gflops, gpu_perf, gpu_time, cpu_perf, cpu_time;
float *h_A, *h_Ainv, *h_R, *work, unused[1];
magmaFloat_ptr d_A, dwork;
magma_int_t N, n2, lda, ldda, info, lwork, ldwork;
float tmp;
float error, rwork[1];
magma_int_t *ipiv, iunused[1];
int status = 0;
magma_opts opts;
opts.parse_opts( argc, argv );
float tol = opts.tolerance * lapackf77_slamch("E");
printf("%% N CPU Gflop/s (sec) GPU Gflop/s (sec) ||I - A*A^{-1}||_1 / (N*cond(A))\n");
printf("%%===============================================================================\n");
for( int itest = 0; itest < opts.ntest; ++itest ) {
for( int iter = 0; iter < opts.niter; ++iter ) {
N = opts.nsize[itest];
lda = N;
n2 = lda*N;
ldda = magma_roundup( N, opts.align ); // multiple of 32 by default
ldwork = N * magma_get_sgetri_nb( N );
gflops = FLOPS_SGETRI( N ) / 1e9;
// query for workspace size
lwork = -1;
lapackf77_sgetri( &N, unused, &lda, iunused, &tmp, &lwork, &info );
if (info != 0) {
printf("lapackf77_sgetri returned error %lld: %s.\n",
(long long) info, magma_strerror( info ));
}
lwork = magma_int_t( MAGMA_S_REAL( tmp ));
TESTING_CHECK( magma_imalloc_cpu( &ipiv, N ));
TESTING_CHECK( magma_smalloc_cpu( &work, lwork ));
TESTING_CHECK( magma_smalloc_cpu( &h_A, n2 ));
TESTING_CHECK( magma_smalloc_cpu( &h_Ainv, n2 ));
TESTING_CHECK( magma_smalloc_cpu( &h_R, n2 ));
TESTING_CHECK( magma_smalloc( &d_A, ldda*N ));
TESTING_CHECK( magma_smalloc( &dwork, ldwork ));
/* Initialize the matrix */
magma_generate_matrix( opts, N, N, h_A, lda );
/* Factor the matrix. Both MAGMA and LAPACK will use this factor. */
magma_ssetmatrix( N, N, h_A, lda, d_A, ldda, opts.queue );
magma_sgetrf_gpu( N, N, d_A, ldda, ipiv, &info );
magma_sgetmatrix( N, N, d_A, ldda, h_Ainv, lda, opts.queue );
if (info != 0) {
printf("magma_sgetrf_gpu returned error %lld: %s.\n",
(long long) info, magma_strerror( info ));
}
// check for exact singularity
//h_Ainv[ 10 + 10*lda ] = MAGMA_S_MAKE( 0.0, 0.0 );
//magma_ssetmatrix( N, N, h_Ainv, lda, d_A, ldda, opts.queue );
/* ====================================================================
Performs operation using MAGMA
=================================================================== */
if(opts.version == 1) {
gpu_time = magma_wtime();
magma_sgetri_gpu( N, d_A, ldda, ipiv, dwork, ldwork, &info );
gpu_time = magma_wtime() - gpu_time;
}
else{
// test expert api
magma_queue_t queues[2];
magma_device_t cdev;
magma_getdevice( &cdev );
magma_queue_create( cdev, &queues[0] );
magma_queue_create( cdev, &queues[1] );
// query workspace
void *host_work=NULL, *device_work=NULL;
magma_int_t lwork_host[1] = {-1};
magma_int_t lwork_device[1] = {-1};
magma_sgetri_expert_gpu_work(
N, NULL, ldda, NULL, &info, MagmaNative,
NULL, lwork_host, NULL, lwork_device, queues );
if(lwork_host[0] > 0) {
TESTING_CHECK( magma_malloc_pinned(&host_work, lwork_host[0]) );
}
if(lwork_device[0] > 0) {
TESTING_CHECK( magma_malloc(&device_work, lwork_device[0]) );
}
gpu_time = magma_sync_wtime( queues[0] );
magma_sgetri_expert_gpu_work(
N, d_A, ldda, ipiv, &info, MagmaNative,
host_work, lwork_host, device_work, lwork_device, queues );
gpu_time = magma_sync_wtime( queues[0] ) - gpu_time;
magma_queue_destroy( queues[0] );
magma_queue_destroy( queues[1] );
if(host_work != NULL) magma_free_pinned( host_work );
if(device_work != NULL) magma_free( device_work );
}
gpu_perf = gflops / gpu_time;
if (info != 0) {
printf("magma_sgetri_gpu returned error %lld: %s.\n",
(long long) info, magma_strerror( info ));
}
/* =====================================================================
Performs operation using LAPACK
=================================================================== */
if ( opts.lapack ) {
cpu_time = magma_wtime();
lapackf77_sgetri( &N, h_Ainv, &lda, ipiv, work, &lwork, &info );
cpu_time = magma_wtime() - cpu_time;
cpu_perf = gflops / cpu_time;
if (info != 0) {
printf("lapackf77_sgetri returned error %lld: %s.\n",
(long long) info, magma_strerror( info ));
}
printf( "%5lld %7.2f (%7.2f) %7.2f (%7.2f)",
(long long) N, cpu_perf, cpu_time, gpu_perf, gpu_time );
}
else {
printf( "%5lld --- ( --- ) %7.2f (%7.2f)",
(long long) N, gpu_perf, gpu_time );
}
/* =====================================================================
Check the result
=================================================================== */
if ( opts.check ) {
magma_sgetmatrix( N, N, d_A, ldda, h_Ainv, lda, opts.queue );
// compute 1-norm condition number estimate, following LAPACK's zget03
float normA, normAinv, rcond;
normA = lapackf77_slange( "1", &N, &N, h_A, &lda, rwork );
normAinv = lapackf77_slange( "1", &N, &N, h_Ainv, &lda, rwork );
if ( normA <= 0 || normAinv <= 0 ) {
rcond = 0;
error = 1 / (tol/opts.tolerance); // == 1/eps
}
else {
rcond = (1 / normA) / normAinv;
// R = I
// R -= A*A^{-1}
// err = ||I - A*A^{-1}|| / ( N ||A||*||A^{-1}|| ) = ||R|| * rcond / N, using 1-norm
lapackf77_slaset( "full", &N, &N, &c_zero, &c_one, h_R, &lda );
blasf77_sgemm( "no", "no", &N, &N, &N,
&c_neg_one, h_A, &lda,
h_Ainv, &lda,
&c_one, h_R, &lda );
error = lapackf77_slange( "1", &N, &N, h_R, &lda, rwork );
error = error * rcond / N;
}
bool okay = (error < tol);
status += ! okay;
printf( " %8.2e %s\n",
error, (okay ? "ok" : "failed"));
}
else {
printf( "\n" );
}
magma_free_cpu( ipiv );
magma_free_cpu( work );
magma_free_cpu( h_A );
magma_free_cpu( h_Ainv );
magma_free_cpu( h_R );
magma_free( d_A );
magma_free( dwork );
fflush( stdout );
}
if ( opts.niter > 1 ) {
printf( "\n" );
}
}
opts.cleanup();
TESTING_CHECK( magma_finalize() );
return status;
}
|