Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
Announcements
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.

## Problem with mkl_ddnscsr Beginner
187 Views

Hello

I’ve been having some problems using the “mkl_ddnscsr” function. I’ve followed the example that comes with the library but it’s not working properly. I can retrieve the non-zero elements in the dense matrix but the row and column vectors are returned empty (all elements are zeros). Below you can find my code; it’s a simplified version of the example in “dconverters.c”. I also have another doubt, what would be the most efficient way to use this function when working with large matrices and the number of non-zero elements is unknown; one way could be to set the a really high number for the maximum number of non-zero elements but that could lead to pre-allocating large vectors. Any help would be much appreciated.

```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "mkl_types.h"
#include "mkl_spblas.h"

int main (void)
{

#define M      4
#define N      4
#define LDA    4
#define NZMAX  8
#define NNZ    8
#define MBLK   2
#define NN     2
#define INFO   0
#define MN     16
#define IBASE1 1
#define IBASE2 1
#define LOCAT  2
#define IDIAG 3
#define NDIAG 4
#define INDIA 12
int		m = M, n=N, lda=LDA, nzmax=NZMAX, nnz = NNZ, mblk=MBLK, nn=NN, info=INFO ,mn=MN;
int	    ibase1 = IBASE1, ibase2 = IBASE2, locat = LOCAT, idiag = IDIAG, ndiag = NDIAG;
double	Acsr[NZMAX];
int		AI[M+1];
int		AJ[NZMAX];
int		i, j;
int		job;

job=0;
job=0;
job=1;
job=2;
job=NZMAX;
job=3;

for ( j=0; j<n; j++)
for ( i=0; i<m; i++)

return 0;
}```

Regards

lggs

2 Replies Black Belt
187 Views

I added the following lines to your code after line-54, and obtained correct results with MKL 11.2.4 and 11.3.3.

```printf("Info from MKL call = %d\n",info);
printf("AI array : "); for(i=0; i<=n; i++)printf(" %2d",AI); printf("\n");
printf("AJ array : "); for(i=0; i<nnz; i++)printf(" %2d",AJ); printf("\n");
for(i=0; i<nnz; i++)printf(" %7.2f",Acsr); printf("\n");
```

Regarding your second question, note that there is provision for making a "dry run" or "storage query" call by setting job=0 with job uninitialized, to find the value of nnz. Following this, you can allocate the Acsr and AJ arrays to their correct sizes, set job=nnz, job=1, and call the routine again, for a "wet run". Here is a complete example:

```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "mkl_types.h"
#include "mkl_spblas.h"
#include "mkl_service.h"

int main (void){
int		m, n, lda, nzmax, nnz, info;
double	*Acsr=NULL;  // to be allocated
int		*AI;
int		*AJ=NULL;    // to be allocated
int		i, j;
int		job; char buf;

mkl_get_version_string(buf,200);
printf("%s\n",buf);

job=0;  // Dense to CSR
job=0;  // 0-based indexing for Adns -- not used, really
job=1;  // 1-based indexing for Acsr
job=2;  // whole matrix in A
job=0;  // first call will inquire for n_nz
job=0;  // first call will inquire for n_nz

m=n=lda=4;
for ( i=0; i<m*n; i++) Adns=0.0;

AI=(int *)malloc((n+1)*sizeof(int));
nnz= job ? AI-1 : AI;
printf("info = %d, nnz = %d\n",info,nnz);
for(i=0; i<=n; i++)printf(" %2d",AI); printf("\n");
AJ=(int *)malloc(nnz*sizeof(int));
Acsr=(double *)malloc(nnz*sizeof(double));
job=nnz; job=1;
printf("Info from second call = %d\n",info);
for(i=0; i<nnz; i++)printf(" %2d",AJ); printf("\n");
for(i=0; i<nnz; i++)printf(" %7.2f",Acsr); printf("\n");
return 0;
}
``` Beginner
187 Views

Thanks for your help! The code I was using to print the answers was wrong (I have very limited experience with C++). I assumed there was a problem because I was also trying to call the mkl_ddnscsr function from Matlab using the SDL and I keep getting all zeros (for the row and column vectors), so I assumed I was doing something wrong from the beginning; at least now I know the correct way to use the function. I'll have to keep struggling with Matlab.

Regards

Gerardo 