Skip to content

Commit dae4c5a

Browse files
committed
Add LAPACKE examples for extended _64 API
1 parent ba11da8 commit dae4c5a

7 files changed

+456
-4
lines changed

LAPACKE/example/CMakeLists.txt

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,29 @@ add_executable(xexample_DGESV_colmajor example_DGESV_colmajor.c lapacke_example_
33
add_executable(xexample_DGELS_rowmajor example_DGELS_rowmajor.c lapacke_example_aux.c lapacke_example_aux.h)
44
add_executable(xexample_DGELS_colmajor example_DGELS_colmajor.c lapacke_example_aux.c lapacke_example_aux.h)
55

6-
target_link_libraries(xexample_DGESV_rowmajor ${LAPACKELIB})
7-
target_link_libraries(xexample_DGESV_colmajor ${LAPACKELIB})
8-
target_link_libraries(xexample_DGELS_rowmajor ${LAPACKELIB})
9-
target_link_libraries(xexample_DGELS_colmajor ${LAPACKELIB})
6+
target_link_libraries(xexample_DGESV_rowmajor ${LAPACKELIB} ${BLAS_LIBRARIES})
7+
target_link_libraries(xexample_DGESV_colmajor ${LAPACKELIB} ${BLAS_LIBRARIES})
8+
target_link_libraries(xexample_DGELS_rowmajor ${LAPACKELIB} ${BLAS_LIBRARIES})
9+
target_link_libraries(xexample_DGELS_colmajor ${LAPACKELIB} ${BLAS_LIBRARIES})
1010

1111
add_test(example_DGESV_rowmajor ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/xexample_DGESV_rowmajor)
1212
add_test(example_DGESV_colmajor ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/xexample_DGESV_colmajor)
1313
add_test(example_DGELS_rowmajor ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/xexample_DGELS_rowmajor)
1414
add_test(example_DGELS_colmajor ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/xexample_DGELS_colmajor)
15+
16+
if(BUILD_INDEX64_EXT_API)
17+
add_executable(xexample_DGESV_rowmajor_64 example_DGESV_rowmajor_64.c lapacke_example_aux.c lapacke_example_aux.h)
18+
add_executable(xexample_DGESV_colmajor_64 example_DGESV_colmajor_64.c lapacke_example_aux.c lapacke_example_aux.h)
19+
add_executable(xexample_DGELS_rowmajor_64 example_DGELS_rowmajor_64.c lapacke_example_aux.c lapacke_example_aux.h)
20+
add_executable(xexample_DGELS_colmajor_64 example_DGELS_colmajor_64.c lapacke_example_aux.c lapacke_example_aux.h)
21+
22+
target_link_libraries(xexample_DGESV_rowmajor_64 ${LAPACKELIB} ${BLAS_LIBRARIES})
23+
target_link_libraries(xexample_DGESV_colmajor_64 ${LAPACKELIB} ${BLAS_LIBRARIES})
24+
target_link_libraries(xexample_DGELS_rowmajor_64 ${LAPACKELIB} ${BLAS_LIBRARIES})
25+
target_link_libraries(xexample_DGELS_colmajor_64 ${LAPACKELIB} ${BLAS_LIBRARIES})
26+
27+
add_test(example_DGESV_rowmajor_64 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/xexample_DGESV_rowmajor_64)
28+
add_test(example_DGESV_colmajor_64 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/xexample_DGESV_colmajor_64)
29+
add_test(example_DGELS_rowmajor_64 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/xexample_DGELS_rowmajor_64)
30+
add_test(example_DGELS_colmajor_64 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/xexample_DGELS_colmajor_64)
31+
endif()
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
LAPACKE Example : Calling DGELS using col-major layout
3+
=====================================================
4+
5+
The program computes the solution to the system of linear
6+
equations with a square matrix A and multiple
7+
right-hand sides B, where A is the coefficient matrix
8+
and b is the right-hand side matrix:
9+
10+
Description
11+
===========
12+
13+
In this example, we wish solve the least squares problem min_x || B - Ax ||
14+
for two right-hand sides using the LAPACK routine DGELS. For input we will
15+
use the 5-by-3 matrix
16+
17+
( 1 1 1 )
18+
( 2 3 4 )
19+
A = ( 3 5 2 )
20+
( 4 2 5 )
21+
( 5 4 3 )
22+
and the 5-by-2 matrix
23+
24+
( -10 -3 )
25+
( 12 14 )
26+
B = ( 14 12 )
27+
( 16 16 )
28+
( 18 16 )
29+
We will first store the input matrix as a static C two-dimensional array,
30+
which is stored in col-major layout, and let LAPACKE handle the work space
31+
array allocation. The LAPACK base name for this function is gels, and we
32+
will use double precision (d), so the LAPACKE function name is LAPACKE_dgels.
33+
34+
lda=5 and ldb=5. The output for each right hand side is stored in b as
35+
consecutive vectors of length 3. The correct answer for this problem is
36+
the 3-by-2 matrix
37+
38+
( 2 1 )
39+
( 1 1 )
40+
( 1 2 )
41+
42+
A complete C program for this example is given below. Note that when the arrays
43+
are passed to the LAPACK routine, they must be dereferenced, since LAPACK is
44+
expecting arrays of type double *, not double **.
45+
46+
47+
LAPACKE Interface
48+
=================
49+
50+
LAPACKE_dgels (col-major, high-level) Example Program Results
51+
52+
-- LAPACKE Example routine --
53+
-- LAPACK is a software package provided by Univ. of Tennessee, --
54+
-- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
55+
*/
56+
/* Calling DGELS using col-major layout */
57+
58+
/* Includes */
59+
#include <stdio.h>
60+
#include <lapacke_64.h>
61+
#include "lapacke_example_aux.h"
62+
63+
/* Main program */
64+
int main (int argc, const char * argv[])
65+
{
66+
/* Locals */
67+
double A[5][3] = {{1,2,3},{4,5,1},{3,5,2},{4,1,4},{2,5,3}};
68+
double b[5][2] = {{-10,12},{14,16},{18,-3},{14,12},{16,16}};
69+
int64_t info,m,n,lda,ldb,nrhs;
70+
71+
/* Initialization */
72+
m = 5;
73+
n = 3;
74+
nrhs = 2;
75+
lda = 5;
76+
ldb = 5;
77+
78+
/* Print Entry Matrix */
79+
print_matrix_colmajor_64( "Entry Matrix A", m, n, *A, lda );
80+
/* Print Right Rand Side */
81+
print_matrix_colmajor_64( "Right Hand Side b", n, nrhs, *b, ldb );
82+
printf( "\n" );
83+
84+
/* Executable statements */
85+
printf( "LAPACKE_dgels_64 (col-major, high-level) Example Program Results\n" );
86+
/* Solve least squares problem*/
87+
info = LAPACKE_dgels_64(LAPACK_COL_MAJOR,'N',m,n,nrhs,*A,lda,*b,ldb);
88+
89+
/* Print Solution */
90+
print_matrix_colmajor_64( "Solution", n, nrhs, *b, ldb );
91+
printf( "\n" );
92+
exit( info );
93+
} /* End of LAPACKE_dgels Example */
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
LAPACKE Example : Calling DGELS using row-major layout
3+
=====================================================
4+
5+
The program computes the solution to the system of linear
6+
equations with a square matrix A and multiple
7+
right-hand sides B, where A is the coefficient matrix
8+
and b is the right-hand side matrix:
9+
10+
Description
11+
===========
12+
13+
In this example, we wish solve the least squares problem min_x || B - Ax ||
14+
for two right-hand sides using the LAPACK routine DGELS. For input we will
15+
use the 5-by-3 matrix
16+
17+
( 1 1 1 )
18+
( 2 3 4 )
19+
A = ( 3 5 2 )
20+
( 4 2 5 )
21+
( 5 4 3 )
22+
and the 5-by-2 matrix
23+
24+
( -10 -3 )
25+
( 12 14 )
26+
B = ( 14 12 )
27+
( 16 16 )
28+
( 18 16 )
29+
We will first store the input matrix as a static C two-dimensional array,
30+
which is stored in row-major layout, and let LAPACKE handle the work space
31+
array allocation. The LAPACK base name for this function is gels, and we
32+
will use double precision (d), so the LAPACKE function name is LAPACKE_dgels.
33+
34+
thus lda=3 and ldb=2. The output for each right hand side is stored in b as
35+
consecutive vectors of length 3. The correct answer for this problem is
36+
the 3-by-2 matrix
37+
38+
( 2 1 )
39+
( 1 1 )
40+
( 1 2 )
41+
42+
A complete C program for this example is given below. Note that when the arrays
43+
are passed to the LAPACK routine, they must be dereferenced, since LAPACK is
44+
expecting arrays of type double *, not double **.
45+
46+
47+
LAPACKE Interface
48+
=================
49+
50+
LAPACKE_dgels (row-major, high-level) Example Program Results
51+
52+
-- LAPACKE Example routine --
53+
-- LAPACK is a software package provided by Univ. of Tennessee, --
54+
-- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
55+
*/
56+
/* Calling DGELS using row-major layout */
57+
58+
/* Includes */
59+
#include <stdio.h>
60+
#include <lapacke_64.h>
61+
#include "lapacke_example_aux.h"
62+
63+
/* Main program */
64+
int main (int argc, const char * argv[])
65+
{
66+
/* Locals */
67+
double A[5][3] = {{1,1,1},{2,3,4},{3,5,2},{4,2,5},{5,4,3}};
68+
double b[5][2] = {{-10,-3},{12,14},{14,12},{16,16},{18,16}};
69+
int64_t info,m,n,lda,ldb,nrhs;
70+
71+
/* Initialization */
72+
m = 5;
73+
n = 3;
74+
nrhs = 2;
75+
lda = 3;
76+
ldb = 2;
77+
78+
/* Print Entry Matrix */
79+
print_matrix_rowmajor_64( "Entry Matrix A", m, n, *A, lda );
80+
/* Print Right Rand Side */
81+
print_matrix_rowmajor_64( "Right Hand Side b", n, nrhs, *b, ldb );
82+
printf( "\n" );
83+
84+
/* Executable statements */
85+
printf( "LAPACKE_dgels_64 (row-major, high-level) Example Program Results\n" );
86+
/* Solve least squares problem*/
87+
info = LAPACKE_dgels_64(LAPACK_ROW_MAJOR,'N',m,n,nrhs,*A,lda,*b,ldb);
88+
89+
/* Print Solution */
90+
print_matrix_rowmajor_64( "Solution", n, nrhs, *b, ldb );
91+
printf( "\n" );
92+
exit( 0 );
93+
} /* End of LAPACKE_dgels Example */
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
LAPACKE_dgesv Example
3+
=====================
4+
5+
The program computes the solution to the system of linear
6+
equations with a square matrix A and multiple
7+
right-hand sides B, where A is the coefficient matrix
8+
and b is the right-hand side matrix:
9+
10+
Description
11+
===========
12+
13+
The routine solves for X the system of linear equations A*X = B,
14+
where A is an n-by-n matrix, the columns of matrix B are individual
15+
right-hand sides, and the columns of X are the corresponding
16+
solutions.
17+
18+
The LU decomposition with partial pivoting and row interchanges is
19+
used to factor A as A = P*L*U, where P is a permutation matrix, L
20+
is unit lower triangular, and U is upper triangular. The factored
21+
form of A is then used to solve the system of equations A*X = B.
22+
23+
LAPACKE Interface
24+
=================
25+
26+
LAPACKE_dgesv (col-major, high-level) Example Program Results
27+
28+
-- LAPACKE Example routine --
29+
-- LAPACK is a software package provided by Univ. of Tennessee, --
30+
-- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
31+
*/
32+
/* Includes */
33+
#include <stdlib.h>
34+
#include <stdio.h>
35+
#include <string.h>
36+
#include "lapacke_64.h"
37+
#include "lapacke_example_aux.h"
38+
39+
/* Main program */
40+
int main(int argc, char **argv) {
41+
42+
/* Locals */
43+
int64_t n, nrhs, lda, ldb, info;
44+
int i, j;
45+
/* Local arrays */
46+
double *A, *b;
47+
int64_t *ipiv;
48+
49+
/* Default Value */
50+
n = 5; nrhs = 1;
51+
52+
/* Arguments */
53+
for( i = 1; i < argc; i++ ) {
54+
if( strcmp( argv[i], "-n" ) == 0 ) {
55+
n = atoi(argv[i+1]);
56+
i++;
57+
}
58+
if( strcmp( argv[i], "-nrhs" ) == 0 ) {
59+
nrhs = atoi(argv[i+1]);
60+
i++;
61+
}
62+
}
63+
64+
/* Initialization */
65+
lda=n, ldb=n;
66+
A = (double *)malloc(n*n*sizeof(double)) ;
67+
if (A==NULL){ printf("error of memory allocation\n"); exit(0); }
68+
b = (double *)malloc(n*nrhs*sizeof(double)) ;
69+
if (b==NULL){ printf("error of memory allocation\n"); exit(0); }
70+
ipiv = (int64_t *)malloc(n*sizeof(int64_t)) ;
71+
if (ipiv==NULL){ printf("error of memory allocation\n"); exit(0); }
72+
73+
for( i = 0; i < n; i++ ) {
74+
for( j = 0; j < n; j++ ) A[i+j*lda] = ((double) rand()) / ((double) RAND_MAX) - 0.5;
75+
}
76+
77+
for(i=0;i<n*nrhs;i++)
78+
b[i] = ((double) rand()) / ((double) RAND_MAX) - 0.5;
79+
80+
/* Print Entry Matrix */
81+
print_matrix_colmajor_64( "Entry Matrix A", n, n, A, lda );
82+
/* Print Right Rand Side */
83+
print_matrix_colmajor_64( "Right Rand Side b", n, nrhs, b, ldb );
84+
printf( "\n" );
85+
86+
/* Executable statements */
87+
printf( "LAPACKE_dgesv_64 (row-major, high-level) Example Program Results\n" );
88+
/* Solve the equations A*X = B */
89+
info = LAPACKE_dgesv_64( LAPACK_COL_MAJOR, n, nrhs, A, lda, ipiv,
90+
b, ldb );
91+
92+
/* Check for the exact singularity */
93+
if( info > 0 ) {
94+
printf( "The diagonal element of the triangular factor of A,\n" );
95+
printf( "U(%" LAPACK_IFMT ",%" LAPACK_IFMT ") is zero, so that A is singular;\n", info, info );
96+
printf( "the solution could not be computed.\n" );
97+
exit( 1 );
98+
}
99+
if (info <0) exit( 1 );
100+
/* Print solution */
101+
print_matrix_colmajor_64( "Solution", n, nrhs, b, ldb );
102+
/* Print details of LU factorization */
103+
print_matrix_colmajor_64( "Details of LU factorization", n, n, A, lda );
104+
/* Print pivot indices */
105+
print_vector_64( "Pivot indices", n, ipiv );
106+
exit( 0 );
107+
} /* End of LAPACKE_dgesv Example */
108+

0 commit comments

Comments
 (0)