a
is not a 2d array, it is an array of pointers to separate 1d arrays. Passing *a
to LAPACKE_dgels
only gives it a pointer to the first row. It will have no way to know where all of the other rows were allocated since they were allocated independently. It wants the entire array to be in a single contiguous block of memory. a
must be of type double*
, not double**
, and you don't dereference it when passing it. You must flatten the 2d indexes into 1d indexes yourself, using either row or column major form (which you tell the function).
EDIT
The following code allocates a flat 1d array with room for m*n
double
s. It then fills the array by converting the 2d indices to 1d row-major indices using the formula row * n + col
. If we wanted column-major indices, we would use col * m + row
.
#include <stdio.h>
#include <lapacke.h>
#include <conio.h>
int main (int argc, const char * argv[])
{
double b[5][2] = {-10,-3,12,14,14,12,16,16,18,16};
lapack_int info,m,n,lda,ldb,nrhs;
int i,j;
double *a;
m = 5;
n = 3;
nrhs = 2;
lda = 3;
ldb = 2;
a = malloc(m * n * sizeof(double));
a[0 * n + 0] = 1;
a[0 * n + 1] = 1;
a[0 * n + 2] = 1;
a[1 * n + 0] = 2;
a[1 * n + 1] = 3;
a[1 * n + 2] = 4;
a[2 * n + 0] = 3;
a[2 * n + 1] = 5;
a[2 * n + 2] = 2;
a[3 * n + 0] = 4;
a[3 * n + 1] = 2;
a[3 * n + 2] = 5;
a[4 * n + 0] = 5;
a[4 * n + 1] = 4;
a[4 * n + 2] = 3;
info = LAPACKE_dgels(LAPACK_ROW_MAJOR,'N',m,n,nrhs,a,lda,*b,ldb);
for(i=0;i<n;i++)
{
for(j=0;j<nrhs;j++)
{
printf("%lf ",b[i][j]);
}
printf("\n");
}
getch();
return(info);
}