I want to solve the least square problem |Ax-b|->min with LAPACK's dgels_ in C, but I'm getting a segmentation fault error (I am aware that there was a similar problem, but the code is quite different and the answers do not apply to my problem). I already located the problem, it is right when dgels_ is executed.
Code:
#include <stdio.h>
#include <lapacke.h>
#define COL 3
#define ROW 4
int main()
{
char transa ='N';
lapack_int m, n, nrhs, lda, ldb, info;
m=ROW;
n=COL;
nrhs=1;
lda=ROW;
ldb=ROW;
double work[COL];
double A [COL*ROW] =
{ 1.1, 4.2, 1.7,
2.5, 2.1, 2.8,
3.4, 4.2, 8.5,
4.4, 5.2, 7.8 };
double b[ROW] =
{ 1.5,
2.1,
3.8,
3.4 };
printf("test 1 \n");
dgels_(&transa, &m, &n, &nrhs, A, &lda, b, &ldb, work, 0, &info);
printf("test 2 \n");
return 0;
}
The first "test 1" is printed then there is the segmentation fault error.
Edit: I tried to compile it before with values, i.e. using dgels_(transa, m, n, nrhs, A, lda, b, ldb, work, 0, info). However, then I get lots of error messages:
lapack_error.c: In function ‘main’:
lapack_error.c:32:13: warning: passing argument 1 of ‘dgels_’ makes pointer from integer without a cast [-Wint-conversion]
dgels_(transa, m, n, nrhs, A, lda, b, ldb, work, 0, info);
^~~~~~
In file included from /usr/include/lapacke.h:143:0,
from lapack_error.c:2:
/usr/include/lapacke.h:14793:6: note: expected ‘char *’ but argument is of type ‘char’
void LAPACK_dgels( char* trans, lapack_int* m, lapack_int* n, lapack_int* nrhs,
^
lapack_error.c:32:21: warning: passing argument 2 of ‘dgels_’ makes pointer from integer without a cast [-Wint-conversion]
dgels_(transa, m, n, nrhs, A, lda, b, ldb, work, 0, info);
^
In file included from /usr/include/lapacke.h:143:0,
from lapack_error.c:2:
/usr/include/lapacke.h:14793:6: note: expected ‘int *’ but argument is of type ‘int’
void LAPACK_dgels( char* trans, lapack_int* m, lapack_int* n, lapack_int* nrhs,
^
lapack_error.c:32:24: warning: passing argument 3 of ‘dgels_’ makes pointer from integer without a cast [-Wint-conversion]
dgels_(transa, m, n, nrhs, A, lda, b, ldb, work, 0, info);
^
In file included from /usr/include/lapacke.h:143:0,
from lapack_error.c:2:
/usr/include/lapacke.h:14793:6: note: expected ‘int *’ but argument is of type ‘int’
void LAPACK_dgels( char* trans, lapack_int* m, lapack_int* n, lapack_int* nrhs,
^
lapack_error.c:32:27: warning: passing argument 4 of ‘dgels_’ makes pointer from integer without a cast [-Wint-conversion]
dgels_(transa, m, n, nrhs, A, lda, b, ldb, work, 0, info);
^~~~
In file included from /usr/include/lapacke.h:143:0,
from lapack_error.c:2:
/usr/include/lapacke.h:14793:6: note: expected ‘int *’ but argument is of type ‘int’
void LAPACK_dgels( char* trans, lapack_int* m, lapack_int* n, lapack_int* nrhs,
^
lapack_error.c:32:36: warning: passing argument 6 of ‘dgels_’ makes pointer from integer without a cast [-Wint-conversion]
dgels_(transa, m, n, nrhs, A, lda, b, ldb, work, 0, info);
^~~
In file included from /usr/include/lapacke.h:143:0,
from lapack_error.c:2:
/usr/include/lapacke.h:14793:6: note: expected ‘int *’ but argument is of type ‘int’
void LAPACK_dgels( char* trans, lapack_int* m, lapack_int* n, lapack_int* nrhs,
^
lapack_error.c:32:44: warning: passing argument 8 of ‘dgels_’ makes pointer from integer without a cast [-Wint-conversion]
dgels_(transa, m, n, nrhs, A, lda, b, ldb, work, 0, info);
^~~
In file included from /usr/include/lapacke.h:143:0,
from lapack_error.c:2:
/usr/include/lapacke.h:14793:6: note: expected ‘int *’ but argument is of type ‘int’
void LAPACK_dgels( char* trans, lapack_int* m, lapack_int* n, lapack_int* nrhs,
^
lapack_error.c:32:58: warning: passing argument 11 of ‘dgels_’ makes pointer from integer without a cast [-Wint-conversion]
dgels_(transa, m, n, nrhs, A, lda, b, ldb, work, 0, info);
^~~~
In file included from /usr/include/lapacke.h:143:0,
from lapack_error.c:2:
/usr/include/lapacke.h:14793:6: note: expected ‘int *’ but argument is of type ‘int’
void LAPACK_dgels( char* trans, lapack_int* m, lapack_int* n, lapack_int* nrhs,
I then tried and look up a sample program and found this one (using sgesv but I figured it might be similar with dgels). And after rewriting dgels_(transa, m, n, nrhs, A, lda, b, ldb, work, 0, info) to dgels_(&transa, &m, &n, &nrhs, A, &lda, b, &ldb, work, 0, &info) there were no more error messages, so I thought this was the right way.
#define col 3rather use#define COL 3dgels_after the segfault happens.