Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
6982 Discussions

Question: 2d array in C language for Lapack fortran library.

kimjoonshikgmail_com
224 Views
can someone help me ?
I need to use lapack functions is C.

for example,

dgemv(trans, m, n, alpha, a, lda, x, incx, beta, y, incy) for matrix vector multiplication or
dgels(trans, m, n, nrhs, a, lda, b, ldb, work, lwork, info) for LS solution using QR factorization,

Can someone inform me
how to define the 2 D array and 1 D array correctly ?formatrix a and vector b in above functions ?

I was using one - d array instead of 2 -d like

a[100] instead of a[10][10] and put the elements in a[100] as column based format and used for above functions,
dgemv worked but dgels give me segmentation faults error.

Thank you.

Joon.
0 Kudos
1 Reply
Michael_C_Intel4
Employee
224 Views

Hello, Joon.

MKL will publish LAPACK C examples soon, there will be dgels example, illustrating 1-d arrays usage. For instance, if you needto solve the problem with the matrix 4-by-4:

1.44 -7.84 -4.39 4.53
-9.96 -0.28 -3.24 3.83
-7.55 3.24 6.27 -6.64
8.34 8.09 5.28 2.06

and right-hand side:

8.58
8.26
8.48
-5.28

you need to initialize the arrays as follows:

int n = 4, nrhs = 1, info, lwork = 100; // Use lwork no less than minimal recommended, or query it
double work[100];
double a[4*4] = {
1.44, -9.96, -7.55, 8.34,
-7.84, -0.28, 3.24, 8.09,
-4.39, -3.24, 6.27, 5.28,
4.53, 3.83, -6.64, 2.06
};
double b[4*1] = {
8.58, 8.26, 8.48, -5.28
};

to call dgels:

dgels( "No transpose", &n, &n, &nrhs, a, &n, b, &n, work, &lwork, &info );


You may want to put the data into 2-d array instead1-dwith absoluetely the same order of elements:

double a2d[4][4] = {
1.44, -9.96, -7.55, 8.34,
-7.84, -0.28, 3.24, 8.09,
-4.39, -3.24, 6.27, 5.28,
4.53, 3.83, -6.64, 2.06
};

but in this case you need to specify input data differently to pass the pointer to the first element of the array:

dgels( "No transpose", &n, &n, &nrhs, *a2d, &n, b, &n, work, &lwork, &info );

or

dgels( "No transpose", &n, &n, &nrhs, a2d[0], &n, b, &n, work, &lwork, &info );


Michael.
0 Kudos
Reply