xref: /petsc/src/mat/impls/aij/seq/aijmkl/aijmkl.c (revision 4f53af40d4298c4fada2e656b4dbeb55b230a855)
14a2a386eSRichard Tran Mills /*
24a2a386eSRichard Tran Mills   Defines basic operations for the MATSEQAIJMKL matrix class.
34a2a386eSRichard Tran Mills   This class is derived from the MATSEQAIJ class and retains the
44a2a386eSRichard Tran Mills   compressed row storage (aka Yale sparse matrix format) but uses
54a2a386eSRichard Tran Mills   sparse BLAS operations from the Intel Math Kernel Library (MKL)
64a2a386eSRichard Tran Mills   wherever possible.
74a2a386eSRichard Tran Mills */
84a2a386eSRichard Tran Mills 
94a2a386eSRichard Tran Mills #include <../src/mat/impls/aij/seq/aij.h>
104a2a386eSRichard Tran Mills #include <../src/mat/impls/aij/seq/aijmkl/aijmkl.h>
114a2a386eSRichard Tran Mills 
124a2a386eSRichard Tran Mills /* MKL include files. */
134a2a386eSRichard Tran Mills #include <mkl_spblas.h>  /* Sparse BLAS */
144a2a386eSRichard Tran Mills 
154a2a386eSRichard Tran Mills typedef struct {
16c9d46305SRichard Tran Mills   PetscBool no_SpMV2;  /* If PETSC_TRUE, then don't use the MKL SpMV2 inspector-executor routines. */
175b49642aSRichard Tran Mills   PetscBool eager_inspection; /* If PETSC_TRUE, then call mkl_sparse_optimize() in MatDuplicate()/MatAssemblyEnd(). */
184abfa3b3SRichard Tran Mills   PetscBool sparse_optimized; /* If PETSC_TRUE, then mkl_sparse_optimize() has been called. */
19551aa5c8SRichard Tran Mills   PetscObjectState state;
20b8cbc1fbSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
21df555b71SRichard Tran Mills   sparse_matrix_t csrA; /* "Handle" used by SpMV2 inspector-executor routines. */
22df555b71SRichard Tran Mills   struct matrix_descr descr;
23b8cbc1fbSRichard Tran Mills #endif
244a2a386eSRichard Tran Mills } Mat_SeqAIJMKL;
254a2a386eSRichard Tran Mills 
264a2a386eSRichard Tran Mills extern PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat,MatAssemblyType);
274a2a386eSRichard Tran Mills 
284a2a386eSRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJMKL_SeqAIJ(Mat A,MatType type,MatReuse reuse,Mat *newmat)
294a2a386eSRichard Tran Mills {
304a2a386eSRichard Tran Mills   /* This routine is only called to convert a MATAIJMKL to its base PETSc type, */
314a2a386eSRichard Tran Mills   /* so we will ignore 'MatType type'. */
324a2a386eSRichard Tran Mills   PetscErrorCode ierr;
334a2a386eSRichard Tran Mills   Mat            B       = *newmat;
34c1d5218aSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
354a2a386eSRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
36c1d5218aSRichard Tran Mills #endif
374a2a386eSRichard Tran Mills 
384a2a386eSRichard Tran Mills   PetscFunctionBegin;
394a2a386eSRichard Tran Mills   if (reuse == MAT_INITIAL_MATRIX) {
404a2a386eSRichard Tran Mills     ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr);
414a2a386eSRichard Tran Mills   }
424a2a386eSRichard Tran Mills 
434a2a386eSRichard Tran Mills   /* Reset the original function pointers. */
4454871a98SRichard Tran Mills   B->ops->duplicate        = MatDuplicate_SeqAIJ;
454a2a386eSRichard Tran Mills   B->ops->assemblyend      = MatAssemblyEnd_SeqAIJ;
464a2a386eSRichard Tran Mills   B->ops->destroy          = MatDestroy_SeqAIJ;
4754871a98SRichard Tran Mills   B->ops->mult             = MatMult_SeqAIJ;
48ff03dc53SRichard Tran Mills   B->ops->multtranspose    = MatMultTranspose_SeqAIJ;
4954871a98SRichard Tran Mills   B->ops->multadd          = MatMultAdd_SeqAIJ;
50ff03dc53SRichard Tran Mills   B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJ;
5145fbe478SRichard Tran Mills   B->ops->matmult          = MatMatMult_SeqAIJ_SeqAIJ;
52e8be1fc7SRichard Tran Mills   B->ops->matmultnumeric   = MatMatMultNumeric_SeqAIJ_SeqAIJ;
53*4f53af40SRichard Tran Mills   B->ops->ptap             = MatPtAP_SeqAIJ_SeqAIJ;
54*4f53af40SRichard Tran Mills   B->ops->ptapnumeric      = MatPtAPNumeric_SeqAIJ_SeqAIJ;
55372ec6bbSRichard Tran Mills   B->ops->transposematmult = MatTransposeMatMult_SeqAIJ_SeqAIJ;
5687c2a1d7SRichard Tran Mills   B->ops->scale            = MatScale_SeqAIJ;
5787c2a1d7SRichard Tran Mills   B->ops->diagonalscale    = MatDiagonalScale_SeqAIJ;
5887c2a1d7SRichard Tran Mills   B->ops->diagonalset      = MatDiagonalSet_SeqAIJ;
5987c2a1d7SRichard Tran Mills   B->ops->axpy             = MatAXPY_SeqAIJ;
604a2a386eSRichard Tran Mills 
61e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",NULL);CHKERRQ(ierr);
62e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr);
63e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr);
64e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr);
6545fbe478SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
664a940b00SSatish Balay   if(!aijmkl->no_SpMV2) {
6745fbe478SRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqaijmkl_seqaijmkl_C",NULL);CHKERRQ(ierr);
68e8be1fc7SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_SP2M
69e8be1fc7SRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqaijmkl_seqaijmkl_C",NULL);CHKERRQ(ierr);
70e8be1fc7SRichard Tran Mills #endif
71372ec6bbSRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatTransposeMatMult_seqaijmkl_seqaijmkl_C",NULL);CHKERRQ(ierr);
7245fbe478SRichard Tran Mills   }
73e9c94282SRichard Tran Mills 
744abfa3b3SRichard Tran Mills   /* Free everything in the Mat_SeqAIJMKL data structure. Currently, this
75e9c94282SRichard Tran Mills    * simply involves destroying the MKL sparse matrix handle and then freeing
76e9c94282SRichard Tran Mills    * the spptr pointer. */
77a8327b06SKarl Rupp   if (reuse == MAT_INITIAL_MATRIX) aijmkl = (Mat_SeqAIJMKL*)B->spptr;
78a8327b06SKarl Rupp 
794abfa3b3SRichard Tran Mills   if (aijmkl->sparse_optimized) {
800632b357SRichard Tran Mills     sparse_status_t stat;
814abfa3b3SRichard Tran Mills     stat = mkl_sparse_destroy(aijmkl->csrA);
829c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set hints/complete mkl_sparse_optimize");
834abfa3b3SRichard Tran Mills   }
844abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
85e9c94282SRichard Tran Mills   ierr = PetscFree(B->spptr);CHKERRQ(ierr);
864a2a386eSRichard Tran Mills 
874a2a386eSRichard Tran Mills   /* Change the type of B to MATSEQAIJ. */
884a2a386eSRichard Tran Mills   ierr = PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ);CHKERRQ(ierr);
894a2a386eSRichard Tran Mills 
904a2a386eSRichard Tran Mills   *newmat = B;
914a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
924a2a386eSRichard Tran Mills }
934a2a386eSRichard Tran Mills 
944a2a386eSRichard Tran Mills PetscErrorCode MatDestroy_SeqAIJMKL(Mat A)
954a2a386eSRichard Tran Mills {
964a2a386eSRichard Tran Mills   PetscErrorCode ierr;
974a2a386eSRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl = (Mat_SeqAIJMKL*) A->spptr;
984a2a386eSRichard Tran Mills 
994a2a386eSRichard Tran Mills   PetscFunctionBegin;
100e9c94282SRichard Tran Mills 
101e9c94282SRichard Tran Mills   /* If MatHeaderMerge() was used, then this SeqAIJMKL matrix will not have an
102e9c94282SRichard Tran Mills    * spptr pointer. */
103e9c94282SRichard Tran Mills   if (aijmkl) {
1044a2a386eSRichard Tran Mills     /* Clean up everything in the Mat_SeqAIJMKL data structure, then free A->spptr. */
1054abfa3b3SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
1064abfa3b3SRichard Tran Mills     if (aijmkl->sparse_optimized) {
1074abfa3b3SRichard Tran Mills       sparse_status_t stat = SPARSE_STATUS_SUCCESS;
1084abfa3b3SRichard Tran Mills       stat = mkl_sparse_destroy(aijmkl->csrA);
1099c46acdfSRichard Tran Mills       if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy");
1104abfa3b3SRichard Tran Mills     }
1114abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
1124a2a386eSRichard Tran Mills     ierr = PetscFree(A->spptr);CHKERRQ(ierr);
113e9c94282SRichard Tran Mills   }
1144a2a386eSRichard Tran Mills 
1154a2a386eSRichard Tran Mills   /* Change the type of A back to SEQAIJ and use MatDestroy_SeqAIJ()
1164a2a386eSRichard Tran Mills    * to destroy everything that remains. */
1174a2a386eSRichard Tran Mills   ierr = PetscObjectChangeTypeName((PetscObject)A, MATSEQAIJ);CHKERRQ(ierr);
1184a2a386eSRichard Tran Mills   /* Note that I don't call MatSetType().  I believe this is because that
1194a2a386eSRichard Tran Mills    * is only to be called when *building* a matrix.  I could be wrong, but
1204a2a386eSRichard Tran Mills    * that is how things work for the SuperLU matrix class. */
1214a2a386eSRichard Tran Mills   ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr);
1224a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
1234a2a386eSRichard Tran Mills }
1244a2a386eSRichard Tran Mills 
1255b49642aSRichard Tran Mills /* MatSeqAIJKL_create_mkl_handle(), if called with an AIJMKL matrix that has not had mkl_sparse_optimize() called for it,
1265b49642aSRichard Tran Mills  * creates an MKL sparse matrix handle from the AIJ arrays and calls mkl_sparse_optimize().
1275b49642aSRichard Tran Mills  * If called with an AIJMKL matrix for which aijmkl->sparse_optimized == PETSC_TRUE, then it destroys the old matrix
1285b49642aSRichard Tran Mills  * handle, creates a new one, and then calls mkl_sparse_optimize().
1295b49642aSRichard Tran Mills  * Although in normal MKL usage it is possible to have a valid matrix handle on which mkl_sparse_optimize() has not been
1305b49642aSRichard Tran Mills  * called, for AIJMKL the handle creation and optimization step always occur together, so we don't handle the case of
1315b49642aSRichard Tran Mills  * an unoptimized matrix handle here. */
1326e369cd5SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_mkl_handle(Mat A)
1334a2a386eSRichard Tran Mills {
1346e369cd5SRichard Tran Mills #ifndef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
1356e369cd5SRichard Tran Mills   /* If the MKL library does not have mkl_sparse_optimize(), then this routine
1366e369cd5SRichard Tran Mills    * does nothing. We make it callable anyway in this case because it cuts
1376e369cd5SRichard Tran Mills    * down on littering the code with #ifdefs. */
13845fbe478SRichard Tran Mills   PetscFunctionBegin;
1396e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
1406e369cd5SRichard Tran Mills #else
141a8327b06SKarl Rupp   Mat_SeqAIJ       *a = (Mat_SeqAIJ*)A->data;
142a8327b06SKarl Rupp   Mat_SeqAIJMKL    *aijmkl = (Mat_SeqAIJMKL*)A->spptr;
143a8327b06SKarl Rupp   PetscInt         m,n;
144a8327b06SKarl Rupp   MatScalar        *aa;
145a8327b06SKarl Rupp   PetscInt         *aj,*ai;
1466e369cd5SRichard Tran Mills   sparse_status_t  stat;
147551aa5c8SRichard Tran Mills   PetscErrorCode   ierr;
148551aa5c8SRichard Tran Mills   PetscObjectState state;
1494a2a386eSRichard Tran Mills 
150a8327b06SKarl Rupp   PetscFunctionBegin;
1516e369cd5SRichard Tran Mills   if (aijmkl->no_SpMV2) PetscFunctionReturn(0);
1526e369cd5SRichard Tran Mills 
1530632b357SRichard Tran Mills   if (aijmkl->sparse_optimized) {
1540632b357SRichard Tran Mills     /* Matrix has been previously assembled and optimized. Must destroy old
1550632b357SRichard Tran Mills      * matrix handle before running the optimization step again. */
1560632b357SRichard Tran Mills     stat = mkl_sparse_destroy(aijmkl->csrA);
1579c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy");
1580632b357SRichard Tran Mills   }
1598d3fe1b0SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_FALSE;
1606e369cd5SRichard Tran Mills 
161c9d46305SRichard Tran Mills   /* Now perform the SpMV2 setup and matrix optimization. */
162df555b71SRichard Tran Mills   aijmkl->descr.type        = SPARSE_MATRIX_TYPE_GENERAL;
163df555b71SRichard Tran Mills   aijmkl->descr.mode        = SPARSE_FILL_MODE_LOWER;
164df555b71SRichard Tran Mills   aijmkl->descr.diag        = SPARSE_DIAG_NON_UNIT;
16558678438SRichard Tran Mills   m = A->rmap->n;
16658678438SRichard Tran Mills   n = A->cmap->n;
167df555b71SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
168df555b71SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
169df555b71SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
17080095d54SIrina Sokolova   if ((a->nz!=0) & !(A->structure_only)) {
1718d3fe1b0SRichard Tran Mills     /* Create a new, optimized sparse matrix handle only if the matrix has nonzero entries.
1728d3fe1b0SRichard Tran Mills      * The MKL sparse-inspector executor routines don't like being passed an empty matrix. */
17358678438SRichard Tran Mills     stat = mkl_sparse_x_create_csr(&aijmkl->csrA,SPARSE_INDEX_BASE_ZERO,m,n,ai,ai+1,aj,aa);
174e8be1fc7SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to create matrix handle");
175df555b71SRichard Tran Mills     stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000);
176e8be1fc7SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set mv_hint");
177df555b71SRichard Tran Mills     stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE);
178e8be1fc7SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set memory_hint");
179df555b71SRichard Tran Mills     stat = mkl_sparse_optimize(aijmkl->csrA);
180e8be1fc7SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_optimize");
1814abfa3b3SRichard Tran Mills     aijmkl->sparse_optimized = PETSC_TRUE;
182c9d46305SRichard Tran Mills   }
183551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
1846e369cd5SRichard Tran Mills 
1856e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
186d995685eSRichard Tran Mills #endif
1876e369cd5SRichard Tran Mills }
1886e369cd5SRichard Tran Mills 
18919afcda9SRichard Tran Mills /* MatSeqAIJMKL_create_from_mkl_handle() creates a sequential AIJMKL matrix from an MKL sparse matrix handle.
19019afcda9SRichard Tran Mills  * We need this to implement MatMatMult() using the MKL inspector-executor routines, which return an (unoptimized)
1916c87cf42SRichard Tran Mills  * matrix handle.
192aab60f1bSRichard Tran Mills  * Note: This routine simply destroys and replaces the original matrix if MAT_REUSE_MATRIX has been specified, as
193aab60f1bSRichard Tran Mills  * there is no good alternative. */
19419afcda9SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
1956c87cf42SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_from_mkl_handle(MPI_Comm comm,sparse_matrix_t csrA,MatReuse reuse,Mat *mat)
19619afcda9SRichard Tran Mills {
19719afcda9SRichard Tran Mills   PetscErrorCode      ierr;
19819afcda9SRichard Tran Mills   sparse_status_t     stat;
19919afcda9SRichard Tran Mills   sparse_index_base_t indexing;
20019afcda9SRichard Tran Mills   PetscInt            nrows, ncols;
20145fbe478SRichard Tran Mills   PetscInt            *aj,*ai,*dummy;
20219afcda9SRichard Tran Mills   MatScalar           *aa;
20319afcda9SRichard Tran Mills   Mat                 A;
20419afcda9SRichard Tran Mills   Mat_SeqAIJMKL       *aijmkl;
20519afcda9SRichard Tran Mills 
20645fbe478SRichard Tran Mills   /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */
20745fbe478SRichard Tran Mills   stat = mkl_sparse_x_export_csr(csrA,&indexing,&nrows,&ncols,&ai,&dummy,&aj,&aa);
2089c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_x_export_csr()");
2096c87cf42SRichard Tran Mills 
210aab60f1bSRichard Tran Mills   if (reuse == MAT_REUSE_MATRIX) {
211aab60f1bSRichard Tran Mills     ierr = MatDestroy(mat);CHKERRQ(ierr);
212aab60f1bSRichard Tran Mills   }
21319afcda9SRichard Tran Mills   ierr = MatCreate(comm,&A);CHKERRQ(ierr);
21419afcda9SRichard Tran Mills   ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr);
21545fbe478SRichard Tran Mills   ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,nrows,ncols);CHKERRQ(ierr);
216aab60f1bSRichard Tran Mills   /* We use MatSeqAIJSetPreallocationCSR() instead of MatCreateSeqAIJWithArrays() because we must copy the arrays exported
217aab60f1bSRichard Tran Mills    * from MKL; MKL developers tell us that modifying the arrays may cause unexpected results when using the MKL handle, and
218aab60f1bSRichard Tran Mills    * they will be destroyed when the MKL handle is destroyed.
219aab60f1bSRichard Tran Mills    * (In the interest of reducing memory consumption in future, can we figure out good ways to deal with this?) */
22019afcda9SRichard Tran Mills   ierr = MatSeqAIJSetPreallocationCSR(A,ai,aj,aa);CHKERRQ(ierr);
22119afcda9SRichard Tran Mills 
22219afcda9SRichard Tran Mills   /* We now have an assembled sequential AIJ matrix created from copies of the exported arrays from the MKL matrix handle.
22319afcda9SRichard Tran Mills    * Now turn it into a MATSEQAIJMKL. */
22419afcda9SRichard Tran Mills   ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr);
2256c87cf42SRichard Tran Mills 
22619afcda9SRichard Tran Mills   aijmkl = (Mat_SeqAIJMKL*) A->spptr;
22719afcda9SRichard Tran Mills   aijmkl->csrA = csrA;
2286c87cf42SRichard Tran Mills 
22919afcda9SRichard Tran Mills   /* The below code duplicates much of what is in MatSeqAIJKL_create_mkl_handle(). I dislike this code duplication, but
23019afcda9SRichard Tran Mills    * MatSeqAIJMKL_create_mkl_handle() cannot be used because we don't need to create a handle -- we've already got one,
23119afcda9SRichard Tran Mills    * and just need to be able to run the MKL optimization step. */
232f3fd1758SRichard Tran Mills   aijmkl->descr.type        = SPARSE_MATRIX_TYPE_GENERAL;
233f3fd1758SRichard Tran Mills   aijmkl->descr.mode        = SPARSE_FILL_MODE_LOWER;
234f3fd1758SRichard Tran Mills   aijmkl->descr.diag        = SPARSE_DIAG_NON_UNIT;
23519afcda9SRichard Tran Mills   stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000);
23619afcda9SRichard Tran Mills   stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE);
23719afcda9SRichard Tran Mills   stat = mkl_sparse_optimize(aijmkl->csrA);
2389c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set hints/complete mkl_sparse_optimize");
23919afcda9SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_TRUE;
24019afcda9SRichard Tran Mills 
24119afcda9SRichard Tran Mills   *mat = A;
24219afcda9SRichard Tran Mills   PetscFunctionReturn(0);
24319afcda9SRichard Tran Mills }
24419afcda9SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
24519afcda9SRichard Tran Mills 
246e8be1fc7SRichard Tran Mills /* MatSeqAIJMKL_update_from_mkl_handle() updates the matrix values array from the contents of the associated MKL sparse matrix handle.
247e8be1fc7SRichard Tran Mills  * This is needed after mkl_sparse_sp2m() with SPARSE_STAGE_FINALIZE_MULT has been used to compute new values of the matrix in
248e8be1fc7SRichard Tran Mills  * MatMatMultNumeric(). */
249e8be1fc7SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
250e8be1fc7SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_update_from_mkl_handle(Mat A)
251e8be1fc7SRichard Tran Mills {
252e8be1fc7SRichard Tran Mills   PetscInt            i;
253e8be1fc7SRichard Tran Mills   PetscInt            nrows,ncols;
254e8be1fc7SRichard Tran Mills   PetscInt            nz;
255e8be1fc7SRichard Tran Mills   PetscInt            *ai,*aj,*dummy;
256e8be1fc7SRichard Tran Mills   PetscScalar         *aa;
257e8be1fc7SRichard Tran Mills   PetscErrorCode      ierr;
258e8be1fc7SRichard Tran Mills   Mat_SeqAIJMKL       *aijmkl;
259e8be1fc7SRichard Tran Mills   sparse_status_t     stat;
260e8be1fc7SRichard Tran Mills   sparse_index_base_t indexing;
261e8be1fc7SRichard Tran Mills 
262e8be1fc7SRichard Tran Mills   aijmkl = (Mat_SeqAIJMKL*) A->spptr;
263e8be1fc7SRichard Tran Mills 
264e8be1fc7SRichard Tran Mills   /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */
265e8be1fc7SRichard Tran Mills   stat = mkl_sparse_x_export_csr(aijmkl->csrA,&indexing,&nrows,&ncols,&ai,&dummy,&aj,&aa);
266e8be1fc7SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_x_export_csr()");
267e8be1fc7SRichard Tran Mills 
268e8be1fc7SRichard Tran Mills   /* We can't just do a copy from the arrays exported by MKL to those used for the PETSc AIJ storage, because the MKL and PETSc
269e8be1fc7SRichard Tran Mills    * representations differ in small ways (e.g., more explicit nonzeros per row due to preallocation). */
270e8be1fc7SRichard Tran Mills   for (i=0; i<nrows; i++) {
271e8be1fc7SRichard Tran Mills     nz = ai[i+1] - ai[i];
272e8be1fc7SRichard Tran Mills     ierr = MatSetValues_SeqAIJ(A, 1, &i, nz, aj+ai[i], aa+ai[i], INSERT_VALUES);CHKERRQ(ierr);
273e8be1fc7SRichard Tran Mills   }
274e8be1fc7SRichard Tran Mills 
275e8be1fc7SRichard Tran Mills   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
276e8be1fc7SRichard Tran Mills   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
277e8be1fc7SRichard Tran Mills 
278e8be1fc7SRichard Tran Mills   PetscFunctionReturn(0);
279e8be1fc7SRichard Tran Mills }
280e8be1fc7SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
281e8be1fc7SRichard Tran Mills 
2826e369cd5SRichard Tran Mills PetscErrorCode MatDuplicate_SeqAIJMKL(Mat A, MatDuplicateOption op, Mat *M)
2836e369cd5SRichard Tran Mills {
2846e369cd5SRichard Tran Mills   PetscErrorCode ierr;
2856e369cd5SRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl;
2866e369cd5SRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl_dest;
2876e369cd5SRichard Tran Mills 
2886e369cd5SRichard Tran Mills   PetscFunctionBegin;
2896e369cd5SRichard Tran Mills   ierr = MatDuplicate_SeqAIJ(A,op,M);CHKERRQ(ierr);
2906e369cd5SRichard Tran Mills   aijmkl      = (Mat_SeqAIJMKL*) A->spptr;
2916e369cd5SRichard Tran Mills   aijmkl_dest = (Mat_SeqAIJMKL*) (*M)->spptr;
2926e369cd5SRichard Tran Mills   ierr = PetscMemcpy(aijmkl_dest,aijmkl,sizeof(Mat_SeqAIJMKL));CHKERRQ(ierr);
2936e369cd5SRichard Tran Mills   aijmkl_dest->sparse_optimized = PETSC_FALSE;
2945b49642aSRichard Tran Mills   if (aijmkl->eager_inspection) {
2956e369cd5SRichard Tran Mills     ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr);
2965b49642aSRichard Tran Mills   }
2976e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
2986e369cd5SRichard Tran Mills }
2996e369cd5SRichard Tran Mills 
3006e369cd5SRichard Tran Mills PetscErrorCode MatAssemblyEnd_SeqAIJMKL(Mat A, MatAssemblyType mode)
3016e369cd5SRichard Tran Mills {
3026e369cd5SRichard Tran Mills   PetscErrorCode  ierr;
3036e369cd5SRichard Tran Mills   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3045b49642aSRichard Tran Mills   Mat_SeqAIJMKL   *aijmkl;
3056e369cd5SRichard Tran Mills 
3066e369cd5SRichard Tran Mills   PetscFunctionBegin;
3076e369cd5SRichard Tran Mills   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
3086e369cd5SRichard Tran Mills 
3096e369cd5SRichard Tran Mills   /* Since a MATSEQAIJMKL matrix is really just a MATSEQAIJ with some
3106e369cd5SRichard Tran Mills    * extra information and some different methods, call the AssemblyEnd
3116e369cd5SRichard Tran Mills    * routine for a MATSEQAIJ.
3126e369cd5SRichard Tran Mills    * I'm not sure if this is the best way to do this, but it avoids
313d96e85feSRichard Tran Mills    * a lot of code duplication. */
3146e369cd5SRichard Tran Mills   a->inode.use = PETSC_FALSE;  /* Must disable: otherwise the MKL routines won't get used. */
3156e369cd5SRichard Tran Mills   ierr = MatAssemblyEnd_SeqAIJ(A, mode);CHKERRQ(ierr);
3166e369cd5SRichard Tran Mills 
3175b49642aSRichard Tran Mills   /* If the user has requested "eager" inspection, create the optimized MKL sparse handle (if needed; the function checks).
3185b49642aSRichard Tran Mills    * (The default is to do "lazy" inspection, deferring this until something like MatMult() is called.) */
3195b49642aSRichard Tran Mills   aijmkl = (Mat_SeqAIJMKL*) A->spptr;
3205b49642aSRichard Tran Mills   if (aijmkl->eager_inspection) {
3216e369cd5SRichard Tran Mills     ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr);
3225b49642aSRichard Tran Mills   }
323df555b71SRichard Tran Mills 
3244a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
3254a2a386eSRichard Tran Mills }
3264a2a386eSRichard Tran Mills 
3274a2a386eSRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL(Mat A,Vec xx,Vec yy)
3284a2a386eSRichard Tran Mills {
3294a2a386eSRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
3304a2a386eSRichard Tran Mills   const PetscScalar *x;
3314a2a386eSRichard Tran Mills   PetscScalar       *y;
3324a2a386eSRichard Tran Mills   const MatScalar   *aa;
3334a2a386eSRichard Tran Mills   PetscErrorCode    ierr;
3344a2a386eSRichard Tran Mills   PetscInt          m=A->rmap->n;
335db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
336db63039fSRichard Tran Mills   PetscScalar       alpha = 1.0;
337db63039fSRichard Tran Mills   PetscScalar       beta = 0.0;
3384a2a386eSRichard Tran Mills   const PetscInt    *aj,*ai;
339db63039fSRichard Tran Mills   char              matdescra[6];
340db63039fSRichard Tran Mills 
3414a2a386eSRichard Tran Mills 
3424a2a386eSRichard Tran Mills   /* Variables not in MatMult_SeqAIJ. */
343ff03dc53SRichard Tran Mills   char transa = 'n';  /* Used to indicate to MKL that we are not computing the transpose product. */
344ff03dc53SRichard Tran Mills 
345ff03dc53SRichard Tran Mills   PetscFunctionBegin;
346db63039fSRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
347db63039fSRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
348ff03dc53SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
349ff03dc53SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
350ff03dc53SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
351ff03dc53SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
352ff03dc53SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
353ff03dc53SRichard Tran Mills 
354ff03dc53SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
355db63039fSRichard Tran Mills   mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y);
356ff03dc53SRichard Tran Mills 
357ff03dc53SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
358ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
359ff03dc53SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
360ff03dc53SRichard Tran Mills   PetscFunctionReturn(0);
361ff03dc53SRichard Tran Mills }
362ff03dc53SRichard Tran Mills 
363d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
364df555b71SRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy)
365df555b71SRichard Tran Mills {
366df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
367df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
368df555b71SRichard Tran Mills   const PetscScalar *x;
369df555b71SRichard Tran Mills   PetscScalar       *y;
370df555b71SRichard Tran Mills   PetscErrorCode    ierr;
371df555b71SRichard Tran Mills   sparse_status_t   stat = SPARSE_STATUS_SUCCESS;
372551aa5c8SRichard Tran Mills   PetscObjectState  state;
373df555b71SRichard Tran Mills 
374df555b71SRichard Tran Mills   PetscFunctionBegin;
375df555b71SRichard Tran Mills 
37638987b35SRichard Tran Mills   /* If there are no nonzero entries, zero yy and return immediately. */
37738987b35SRichard Tran Mills   if(!a->nz) {
37838987b35SRichard Tran Mills     PetscInt i;
37938987b35SRichard Tran Mills     PetscInt m=A->rmap->n;
38038987b35SRichard Tran Mills     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
38138987b35SRichard Tran Mills     for (i=0; i<m; i++) {
38238987b35SRichard Tran Mills       y[i] = 0.0;
38338987b35SRichard Tran Mills     }
38438987b35SRichard Tran Mills     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
38538987b35SRichard Tran Mills     PetscFunctionReturn(0);
38638987b35SRichard Tran Mills   }
387f36dfe3fSRichard Tran Mills 
388df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
389df555b71SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
390df555b71SRichard Tran Mills 
3913fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
3923fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
3933fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
394551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
395551aa5c8SRichard Tran Mills   if (!aijmkl->sparse_optimized || aijmkl->state != state) {
3963fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
3973fa15762SRichard Tran Mills   }
3983fa15762SRichard Tran Mills 
399df555b71SRichard Tran Mills   /* Call MKL SpMV2 executor routine to do the MatMult. */
400df555b71SRichard Tran Mills   stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y);
4019c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
402df555b71SRichard Tran Mills 
403df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
404df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
405df555b71SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
406df555b71SRichard Tran Mills   PetscFunctionReturn(0);
407df555b71SRichard Tran Mills }
408d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
409df555b71SRichard Tran Mills 
410ff03dc53SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL(Mat A,Vec xx,Vec yy)
411ff03dc53SRichard Tran Mills {
412ff03dc53SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
413ff03dc53SRichard Tran Mills   const PetscScalar *x;
414ff03dc53SRichard Tran Mills   PetscScalar       *y;
415ff03dc53SRichard Tran Mills   const MatScalar   *aa;
416ff03dc53SRichard Tran Mills   PetscErrorCode    ierr;
417ff03dc53SRichard Tran Mills   PetscInt          m=A->rmap->n;
418db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
419db63039fSRichard Tran Mills   PetscScalar       alpha = 1.0;
420db63039fSRichard Tran Mills   PetscScalar       beta = 0.0;
421ff03dc53SRichard Tran Mills   const PetscInt    *aj,*ai;
422db63039fSRichard Tran Mills   char              matdescra[6];
423ff03dc53SRichard Tran Mills 
424ff03dc53SRichard Tran Mills   /* Variables not in MatMultTranspose_SeqAIJ. */
425ff03dc53SRichard Tran Mills   char transa = 't';  /* Used to indicate to MKL that we are computing the transpose product. */
4264a2a386eSRichard Tran Mills 
4274a2a386eSRichard Tran Mills   PetscFunctionBegin;
428969800c5SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
429969800c5SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
4304a2a386eSRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
4314a2a386eSRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
4324a2a386eSRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
4334a2a386eSRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
4344a2a386eSRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
4354a2a386eSRichard Tran Mills 
4364a2a386eSRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
437db63039fSRichard Tran Mills   mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y);
4384a2a386eSRichard Tran Mills 
4394a2a386eSRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
4404a2a386eSRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
4414a2a386eSRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
4424a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
4434a2a386eSRichard Tran Mills }
4444a2a386eSRichard Tran Mills 
445d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
446df555b71SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy)
447df555b71SRichard Tran Mills {
448df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
449df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
450df555b71SRichard Tran Mills   const PetscScalar *x;
451df555b71SRichard Tran Mills   PetscScalar       *y;
452df555b71SRichard Tran Mills   PetscErrorCode    ierr;
4530632b357SRichard Tran Mills   sparse_status_t   stat;
454551aa5c8SRichard Tran Mills   PetscObjectState  state;
455df555b71SRichard Tran Mills 
456df555b71SRichard Tran Mills   PetscFunctionBegin;
457df555b71SRichard Tran Mills 
45838987b35SRichard Tran Mills   /* If there are no nonzero entries, zero yy and return immediately. */
45938987b35SRichard Tran Mills   if(!a->nz) {
46038987b35SRichard Tran Mills     PetscInt i;
46138987b35SRichard Tran Mills     PetscInt n=A->cmap->n;
46238987b35SRichard Tran Mills     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
46338987b35SRichard Tran Mills     for (i=0; i<n; i++) {
46438987b35SRichard Tran Mills       y[i] = 0.0;
46538987b35SRichard Tran Mills     }
46638987b35SRichard Tran Mills     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
46738987b35SRichard Tran Mills     PetscFunctionReturn(0);
46838987b35SRichard Tran Mills   }
469f36dfe3fSRichard Tran Mills 
470df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
471df555b71SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
472df555b71SRichard Tran Mills 
4733fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
4743fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
4753fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
476551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
477551aa5c8SRichard Tran Mills   if (!aijmkl->sparse_optimized || aijmkl->state != state) {
4783fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
4793fa15762SRichard Tran Mills   }
4803fa15762SRichard Tran Mills 
481df555b71SRichard Tran Mills   /* Call MKL SpMV2 executor routine to do the MatMultTranspose. */
482df555b71SRichard Tran Mills   stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y);
4839c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
484df555b71SRichard Tran Mills 
485df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
486df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
487df555b71SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
488df555b71SRichard Tran Mills   PetscFunctionReturn(0);
489df555b71SRichard Tran Mills }
490d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
491df555b71SRichard Tran Mills 
4924a2a386eSRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz)
4934a2a386eSRichard Tran Mills {
4944a2a386eSRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
4954a2a386eSRichard Tran Mills   const PetscScalar *x;
4964a2a386eSRichard Tran Mills   PetscScalar       *y,*z;
4974a2a386eSRichard Tran Mills   const MatScalar   *aa;
4984a2a386eSRichard Tran Mills   PetscErrorCode    ierr;
4994a2a386eSRichard Tran Mills   PetscInt          m=A->rmap->n;
500db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
5014a2a386eSRichard Tran Mills   const PetscInt    *aj,*ai;
5024a2a386eSRichard Tran Mills   PetscInt          i;
5034a2a386eSRichard Tran Mills 
504ff03dc53SRichard Tran Mills   /* Variables not in MatMultAdd_SeqAIJ. */
505ff03dc53SRichard Tran Mills   char              transa = 'n';  /* Used to indicate to MKL that we are not computing the transpose product. */
506a84739b8SRichard Tran Mills   PetscScalar       alpha = 1.0;
507db63039fSRichard Tran Mills   PetscScalar       beta;
508a84739b8SRichard Tran Mills   char              matdescra[6];
509ff03dc53SRichard Tran Mills 
510ff03dc53SRichard Tran Mills   PetscFunctionBegin;
511a84739b8SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
512a84739b8SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
513a84739b8SRichard Tran Mills 
514ff03dc53SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
515ff03dc53SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
516ff03dc53SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
517ff03dc53SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
518ff03dc53SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
519ff03dc53SRichard Tran Mills 
520ff03dc53SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
521a84739b8SRichard Tran Mills   if (zz == yy) {
522a84739b8SRichard Tran Mills     /* If zz and yy are the same vector, we can use MKL's mkl_xcsrmv(), which calculates y = alpha*A*x + beta*y. */
523db63039fSRichard Tran Mills     beta = 1.0;
524db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
525a84739b8SRichard Tran Mills   } else {
526db63039fSRichard Tran Mills     /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z.
527db63039fSRichard Tran Mills      * MKL sparse BLAS does not have a MatMultAdd equivalent. */
528db63039fSRichard Tran Mills     beta = 0.0;
529db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
530ff03dc53SRichard Tran Mills     for (i=0; i<m; i++) {
531ff03dc53SRichard Tran Mills       z[i] += y[i];
532ff03dc53SRichard Tran Mills     }
533a84739b8SRichard Tran Mills   }
534ff03dc53SRichard Tran Mills 
535ff03dc53SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
536ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
537ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
538ff03dc53SRichard Tran Mills   PetscFunctionReturn(0);
539ff03dc53SRichard Tran Mills }
540ff03dc53SRichard Tran Mills 
541d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
542df555b71SRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz)
543df555b71SRichard Tran Mills {
544df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
545df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
546df555b71SRichard Tran Mills   const PetscScalar *x;
547df555b71SRichard Tran Mills   PetscScalar       *y,*z;
548df555b71SRichard Tran Mills   PetscErrorCode    ierr;
549df555b71SRichard Tran Mills   PetscInt          m=A->rmap->n;
550df555b71SRichard Tran Mills   PetscInt          i;
551df555b71SRichard Tran Mills 
552df555b71SRichard Tran Mills   /* Variables not in MatMultAdd_SeqAIJ. */
553df555b71SRichard Tran Mills   sparse_status_t   stat = SPARSE_STATUS_SUCCESS;
554551aa5c8SRichard Tran Mills   PetscObjectState  state;
555df555b71SRichard Tran Mills 
556df555b71SRichard Tran Mills   PetscFunctionBegin;
557df555b71SRichard Tran Mills 
55838987b35SRichard Tran Mills   /* If there are no nonzero entries, set zz = yy and return immediately. */
55938987b35SRichard Tran Mills   if(!a->nz) {
56038987b35SRichard Tran Mills     PetscInt i;
56138987b35SRichard Tran Mills     ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
56238987b35SRichard Tran Mills     for (i=0; i<m; i++) {
56338987b35SRichard Tran Mills       z[i] = y[i];
56438987b35SRichard Tran Mills     }
56538987b35SRichard Tran Mills     ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
56638987b35SRichard Tran Mills     PetscFunctionReturn(0);
56738987b35SRichard Tran Mills   }
568df555b71SRichard Tran Mills 
569df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
570df555b71SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
571df555b71SRichard Tran Mills 
5723fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
5733fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
5743fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
575551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
576551aa5c8SRichard Tran Mills   if (!aijmkl->sparse_optimized || aijmkl->state != state) {
5773fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
5783fa15762SRichard Tran Mills   }
5793fa15762SRichard Tran Mills 
580df555b71SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
581df555b71SRichard Tran Mills   if (zz == yy) {
582df555b71SRichard Tran Mills     /* If zz and yy are the same vector, we can use mkl_sparse_x_mv, which calculates y = alpha*A*x + beta*y,
583df555b71SRichard Tran Mills      * with alpha and beta both set to 1.0. */
584db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z);
5859c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
586df555b71SRichard Tran Mills   } else {
587df555b71SRichard Tran Mills     /* zz and yy are different vectors, so we call mkl_sparse_x_mv with alpha=1.0 and beta=0.0, and then
588df555b71SRichard Tran Mills      * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */
589db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z);
5909c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
591df555b71SRichard Tran Mills     for (i=0; i<m; i++) {
592df555b71SRichard Tran Mills       z[i] += y[i];
593df555b71SRichard Tran Mills     }
594df555b71SRichard Tran Mills   }
595df555b71SRichard Tran Mills 
596df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
597df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
598df555b71SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
599df555b71SRichard Tran Mills   PetscFunctionReturn(0);
600df555b71SRichard Tran Mills }
601d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
602df555b71SRichard Tran Mills 
603ff03dc53SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz)
604ff03dc53SRichard Tran Mills {
605ff03dc53SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
606ff03dc53SRichard Tran Mills   const PetscScalar *x;
607ff03dc53SRichard Tran Mills   PetscScalar       *y,*z;
608ff03dc53SRichard Tran Mills   const MatScalar   *aa;
609ff03dc53SRichard Tran Mills   PetscErrorCode    ierr;
610ff03dc53SRichard Tran Mills   PetscInt          m=A->rmap->n;
611db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
612ff03dc53SRichard Tran Mills   const PetscInt    *aj,*ai;
613ff03dc53SRichard Tran Mills   PetscInt          i;
614ff03dc53SRichard Tran Mills 
615ff03dc53SRichard Tran Mills   /* Variables not in MatMultTransposeAdd_SeqAIJ. */
616ff03dc53SRichard Tran Mills   char transa = 't';  /* Used to indicate to MKL that we are computing the transpose product. */
617a84739b8SRichard Tran Mills   PetscScalar       alpha = 1.0;
618db63039fSRichard Tran Mills   PetscScalar       beta;
619a84739b8SRichard Tran Mills   char              matdescra[6];
6204a2a386eSRichard Tran Mills 
6214a2a386eSRichard Tran Mills   PetscFunctionBegin;
622a84739b8SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
623a84739b8SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
624a84739b8SRichard Tran Mills 
6254a2a386eSRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
6264a2a386eSRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
6274a2a386eSRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
6284a2a386eSRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
6294a2a386eSRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
6304a2a386eSRichard Tran Mills 
6314a2a386eSRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
632a84739b8SRichard Tran Mills   if (zz == yy) {
633a84739b8SRichard Tran Mills     /* If zz and yy are the same vector, we can use MKL's mkl_xcsrmv(), which calculates y = alpha*A*x + beta*y. */
634db63039fSRichard Tran Mills     beta = 1.0;
635969800c5SRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
636a84739b8SRichard Tran Mills   } else {
637db63039fSRichard Tran Mills     /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z.
638db63039fSRichard Tran Mills      * MKL sparse BLAS does not have a MatMultAdd equivalent. */
639db63039fSRichard Tran Mills     beta = 0.0;
640db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
641969800c5SRichard Tran Mills     for (i=0; i<n; i++) {
6424a2a386eSRichard Tran Mills       z[i] += y[i];
6434a2a386eSRichard Tran Mills     }
644a84739b8SRichard Tran Mills   }
6454a2a386eSRichard Tran Mills 
6464a2a386eSRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
6474a2a386eSRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
6484a2a386eSRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
6494a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
6504a2a386eSRichard Tran Mills }
6514a2a386eSRichard Tran Mills 
652d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
653df555b71SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz)
654df555b71SRichard Tran Mills {
655df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
656df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
657df555b71SRichard Tran Mills   const PetscScalar *x;
658df555b71SRichard Tran Mills   PetscScalar       *y,*z;
659df555b71SRichard Tran Mills   PetscErrorCode    ierr;
660969800c5SRichard Tran Mills   PetscInt          n=A->cmap->n;
661df555b71SRichard Tran Mills   PetscInt          i;
662551aa5c8SRichard Tran Mills   PetscObjectState  state;
663df555b71SRichard Tran Mills 
664df555b71SRichard Tran Mills   /* Variables not in MatMultTransposeAdd_SeqAIJ. */
665df555b71SRichard Tran Mills   sparse_status_t stat = SPARSE_STATUS_SUCCESS;
666df555b71SRichard Tran Mills 
667df555b71SRichard Tran Mills   PetscFunctionBegin;
668df555b71SRichard Tran Mills 
66938987b35SRichard Tran Mills   /* If there are no nonzero entries, set zz = yy and return immediately. */
67038987b35SRichard Tran Mills   if(!a->nz) {
67138987b35SRichard Tran Mills     PetscInt i;
67238987b35SRichard Tran Mills     ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
67338987b35SRichard Tran Mills     for (i=0; i<n; i++) {
67438987b35SRichard Tran Mills       z[i] = y[i];
67538987b35SRichard Tran Mills     }
67638987b35SRichard Tran Mills     ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
67738987b35SRichard Tran Mills     PetscFunctionReturn(0);
67838987b35SRichard Tran Mills   }
679f36dfe3fSRichard Tran Mills 
680df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
681df555b71SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
682df555b71SRichard Tran Mills 
6833fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
6843fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
6853fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
686551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
687551aa5c8SRichard Tran Mills   if (!aijmkl->sparse_optimized || aijmkl->state != state) {
6883fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
6893fa15762SRichard Tran Mills   }
6903fa15762SRichard Tran Mills 
691df555b71SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
692df555b71SRichard Tran Mills   if (zz == yy) {
693df555b71SRichard Tran Mills     /* If zz and yy are the same vector, we can use mkl_sparse_x_mv, which calculates y = alpha*A*x + beta*y,
694df555b71SRichard Tran Mills      * with alpha and beta both set to 1.0. */
695db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z);
6969c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
697df555b71SRichard Tran Mills   } else {
698df555b71SRichard Tran Mills     /* zz and yy are different vectors, so we call mkl_sparse_x_mv with alpha=1.0 and beta=0.0, and then
699df555b71SRichard Tran Mills      * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */
700db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z);
7019c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
702969800c5SRichard Tran Mills     for (i=0; i<n; i++) {
703df555b71SRichard Tran Mills       z[i] += y[i];
704df555b71SRichard Tran Mills     }
705df555b71SRichard Tran Mills   }
706df555b71SRichard Tran Mills 
707df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
708df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
709df555b71SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
710df555b71SRichard Tran Mills   PetscFunctionReturn(0);
711df555b71SRichard Tran Mills }
712d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
713df555b71SRichard Tran Mills 
71445fbe478SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
715aab60f1bSRichard Tran Mills /* Note that this code currently doesn't actually get used when MatMatMult() is called with MAT_REUSE_MATRIX, because
716aab60f1bSRichard Tran Mills  * the MatMatMult() interface code calls MatMatMultNumeric() in this case.
7173ecbffd0SRichard Tran Mills  * For releases of MKL prior to version 18, update 2:
718aab60f1bSRichard Tran Mills  * MKL has no notion of separately callable symbolic vs. numeric phases of sparse matrix-matrix multiply, so in the
719aab60f1bSRichard Tran Mills  * MAT_REUSE_MATRIX case, the SeqAIJ routines end up being used. Even though this means that the (hopefully more
720aab60f1bSRichard Tran Mills  * optimized) MKL routines do not get used, this probably is best because the MKL routines would waste time re-computing
721aab60f1bSRichard Tran Mills  * the symbolic portion, whereas the native PETSc SeqAIJ routines will avoid this. */
72245fbe478SRichard Tran Mills PetscErrorCode MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat*C)
72345fbe478SRichard Tran Mills {
72445fbe478SRichard Tran Mills   Mat_SeqAIJMKL    *a, *b;
72545fbe478SRichard Tran Mills   sparse_matrix_t  csrA, csrB, csrC;
72645fbe478SRichard Tran Mills   PetscErrorCode   ierr;
72745fbe478SRichard Tran Mills   sparse_status_t  stat = SPARSE_STATUS_SUCCESS;
728551aa5c8SRichard Tran Mills   PetscObjectState state;
72945fbe478SRichard Tran Mills 
73045fbe478SRichard Tran Mills   PetscFunctionBegin;
73145fbe478SRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
73245fbe478SRichard Tran Mills   b = (Mat_SeqAIJMKL*)B->spptr;
733551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
734551aa5c8SRichard Tran Mills   if (!a->sparse_optimized || a->state != state) {
73545fbe478SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
73645fbe478SRichard Tran Mills   }
737551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr);
738551aa5c8SRichard Tran Mills   if (!b->sparse_optimized || b->state != state) {
73945fbe478SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(B);
74045fbe478SRichard Tran Mills   }
74145fbe478SRichard Tran Mills   csrA = a->csrA;
74245fbe478SRichard Tran Mills   csrB = b->csrA;
74345fbe478SRichard Tran Mills 
74445fbe478SRichard Tran Mills   stat = mkl_sparse_spmm(SPARSE_OPERATION_NON_TRANSPOSE,csrA,csrB,&csrC);
7459c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete sparse matrix-matrix multiply");
74645fbe478SRichard Tran Mills 
7476c87cf42SRichard Tran Mills   ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr);
74845fbe478SRichard Tran Mills 
74945fbe478SRichard Tran Mills   PetscFunctionReturn(0);
75045fbe478SRichard Tran Mills }
75145fbe478SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
75245fbe478SRichard Tran Mills 
753e8be1fc7SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_SP2M
754e8be1fc7SRichard Tran Mills PetscErrorCode MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,Mat C)
755e8be1fc7SRichard Tran Mills {
756e8be1fc7SRichard Tran Mills   Mat_SeqAIJMKL       *a, *b, *c;
757e8be1fc7SRichard Tran Mills   sparse_matrix_t     csrA, csrB, csrC;
758e8be1fc7SRichard Tran Mills   PetscErrorCode      ierr;
759e8be1fc7SRichard Tran Mills   sparse_status_t     stat = SPARSE_STATUS_SUCCESS;
760e8be1fc7SRichard Tran Mills   struct matrix_descr descr_type_gen;
761e8be1fc7SRichard Tran Mills   PetscObjectState    state;
762e8be1fc7SRichard Tran Mills 
763e8be1fc7SRichard Tran Mills   PetscFunctionBegin;
764e8be1fc7SRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
765e8be1fc7SRichard Tran Mills   b = (Mat_SeqAIJMKL*)B->spptr;
766e8be1fc7SRichard Tran Mills   c = (Mat_SeqAIJMKL*)C->spptr;
767e8be1fc7SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
768e8be1fc7SRichard Tran Mills   if (!a->sparse_optimized || a->state != state) {
769e8be1fc7SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
770e8be1fc7SRichard Tran Mills   }
771e8be1fc7SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr);
772e8be1fc7SRichard Tran Mills   if (!b->sparse_optimized || b->state != state) {
773e8be1fc7SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(B);
774e8be1fc7SRichard Tran Mills   }
775e8be1fc7SRichard Tran Mills   csrA = a->csrA;
776e8be1fc7SRichard Tran Mills   csrB = b->csrA;
777e8be1fc7SRichard Tran Mills   csrC = c->csrA;
778e8be1fc7SRichard Tran Mills   descr_type_gen.type = SPARSE_MATRIX_TYPE_GENERAL;
779e8be1fc7SRichard Tran Mills 
780e8be1fc7SRichard Tran Mills   stat = mkl_sparse_sp2m(SPARSE_OPERATION_NON_TRANSPOSE,descr_type_gen,csrA,
781e8be1fc7SRichard Tran Mills                          SPARSE_OPERATION_NON_TRANSPOSE,descr_type_gen,csrB,
782e8be1fc7SRichard Tran Mills                          SPARSE_STAGE_FINALIZE_MULT,&csrC);
783e8be1fc7SRichard Tran Mills 
784e8be1fc7SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete numerical stage of sparse matrix-matrix multiply");
785e8be1fc7SRichard Tran Mills 
786e8be1fc7SRichard Tran Mills   /* Have to update the PETSc AIJ representation for matrix C from contents of MKL handle. */
787*4f53af40SRichard Tran Mills   ierr = MatSeqAIJMKL_update_from_mkl_handle(C);CHKERRQ(ierr);
788e8be1fc7SRichard Tran Mills 
789e8be1fc7SRichard Tran Mills   PetscFunctionReturn(0);
790e8be1fc7SRichard Tran Mills }
791e8be1fc7SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_SP2M */
792e8be1fc7SRichard Tran Mills 
793372ec6bbSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
794372ec6bbSRichard Tran Mills PetscErrorCode MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat*C)
795372ec6bbSRichard Tran Mills {
796372ec6bbSRichard Tran Mills   Mat_SeqAIJMKL    *a, *b;
797372ec6bbSRichard Tran Mills   sparse_matrix_t  csrA, csrB, csrC;
798372ec6bbSRichard Tran Mills   PetscErrorCode   ierr;
799372ec6bbSRichard Tran Mills   sparse_status_t  stat = SPARSE_STATUS_SUCCESS;
800551aa5c8SRichard Tran Mills   PetscObjectState state;
801372ec6bbSRichard Tran Mills 
802372ec6bbSRichard Tran Mills   PetscFunctionBegin;
803372ec6bbSRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
804372ec6bbSRichard Tran Mills   b = (Mat_SeqAIJMKL*)B->spptr;
805551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
806551aa5c8SRichard Tran Mills   if (!a->sparse_optimized || a->state != state) {
807372ec6bbSRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
808372ec6bbSRichard Tran Mills   }
809551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr);
810551aa5c8SRichard Tran Mills   if (!b->sparse_optimized || b->state != state) {
811372ec6bbSRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(B);
812372ec6bbSRichard Tran Mills   }
813372ec6bbSRichard Tran Mills   csrA = a->csrA;
814372ec6bbSRichard Tran Mills   csrB = b->csrA;
815372ec6bbSRichard Tran Mills 
816372ec6bbSRichard Tran Mills   stat = mkl_sparse_spmm(SPARSE_OPERATION_TRANSPOSE,csrA,csrB,&csrC);
8179c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete sparse matrix-matrix multiply");
818372ec6bbSRichard Tran Mills 
819372ec6bbSRichard Tran Mills   ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr);
820372ec6bbSRichard Tran Mills 
821372ec6bbSRichard Tran Mills   PetscFunctionReturn(0);
822372ec6bbSRichard Tran Mills }
823372ec6bbSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
824372ec6bbSRichard Tran Mills 
825*4f53af40SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_SP2M
826*4f53af40SRichard Tran Mills PetscErrorCode MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat P,Mat C)
827*4f53af40SRichard Tran Mills {
828*4f53af40SRichard Tran Mills   Mat_SeqAIJMKL       *a, *p, *c;
829*4f53af40SRichard Tran Mills   sparse_matrix_t     csrA, csrP, csrC;
830*4f53af40SRichard Tran Mills   PetscBool           set, flag;
831*4f53af40SRichard Tran Mills   sparse_status_t     stat = SPARSE_STATUS_SUCCESS;
832*4f53af40SRichard Tran Mills   struct matrix_descr descr_type_gen;
833*4f53af40SRichard Tran Mills   PetscObjectState    state;
834*4f53af40SRichard Tran Mills   PetscErrorCode      ierr;
835*4f53af40SRichard Tran Mills 
836*4f53af40SRichard Tran Mills   PetscFunctionBegin;
837*4f53af40SRichard Tran Mills   ierr = MatIsSymmetricKnown(A,&set,&flag);
838*4f53af40SRichard Tran Mills   if (!set || (set && !flag)) {
839*4f53af40SRichard Tran Mills     ierr = MatPtAPNumeric_SeqAIJ_SeqAIJ(A,P,C);CHKERRQ(ierr);
840*4f53af40SRichard Tran Mills     PetscFunctionReturn(0);
841*4f53af40SRichard Tran Mills   }
842*4f53af40SRichard Tran Mills 
843*4f53af40SRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
844*4f53af40SRichard Tran Mills   p = (Mat_SeqAIJMKL*)P->spptr;
845*4f53af40SRichard Tran Mills   c = (Mat_SeqAIJMKL*)C->spptr;
846*4f53af40SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
847*4f53af40SRichard Tran Mills   if (!a->sparse_optimized || a->state != state) {
848*4f53af40SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
849*4f53af40SRichard Tran Mills   }
850*4f53af40SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)P,&state);CHKERRQ(ierr);
851*4f53af40SRichard Tran Mills   if (!p->sparse_optimized || p->state != state) {
852*4f53af40SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(P);
853*4f53af40SRichard Tran Mills   }
854*4f53af40SRichard Tran Mills   csrA = a->csrA;
855*4f53af40SRichard Tran Mills   csrP = p->csrA;
856*4f53af40SRichard Tran Mills   csrC = c->csrA;
857*4f53af40SRichard Tran Mills   descr_type_gen.type = SPARSE_MATRIX_TYPE_GENERAL;
858*4f53af40SRichard Tran Mills 
859*4f53af40SRichard Tran Mills   /* TODO: Below won't work for complex matrix. Protect this! Maybe where function pointers are assigned in MatConvert? */
860*4f53af40SRichard Tran Mills   stat = mkl_sparse_sypr(SPARSE_OPERATION_TRANSPOSE,csrP,csrA,descr_type_gen,&csrC,SPARSE_STAGE_FINALIZE_MULT);
861*4f53af40SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to finalize mkl_sparse_sypr");
862*4f53af40SRichard Tran Mills 
863*4f53af40SRichard Tran Mills   /* Have to update the PETSc AIJ representation for matrix C from contents of MKL handle. */
864*4f53af40SRichard Tran Mills   ierr = MatSeqAIJMKL_update_from_mkl_handle(C);CHKERRQ(ierr);
865*4f53af40SRichard Tran Mills 
866*4f53af40SRichard Tran Mills   PetscFunctionReturn(0);
867*4f53af40SRichard Tran Mills }
868*4f53af40SRichard Tran Mills #endif
869*4f53af40SRichard Tran Mills 
870*4f53af40SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_SP2M
871*4f53af40SRichard Tran Mills PetscErrorCode MatPtAP_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C)
872*4f53af40SRichard Tran Mills {
873*4f53af40SRichard Tran Mills   Mat_SeqAIJMKL       *a, *p;
874*4f53af40SRichard Tran Mills   sparse_matrix_t     csrA, csrP, csrC;
875*4f53af40SRichard Tran Mills   PetscBool           set, flag;
876*4f53af40SRichard Tran Mills   sparse_status_t     stat = SPARSE_STATUS_SUCCESS;
877*4f53af40SRichard Tran Mills   struct matrix_descr descr_type_gen;
878*4f53af40SRichard Tran Mills   PetscObjectState    state;
879*4f53af40SRichard Tran Mills   PetscErrorCode      ierr;
880*4f53af40SRichard Tran Mills 
881*4f53af40SRichard Tran Mills   PetscFunctionBegin;
882*4f53af40SRichard Tran Mills   ierr = MatIsSymmetricKnown(A,&set,&flag);
883*4f53af40SRichard Tran Mills   if (!set || (set && !flag)) {
884*4f53af40SRichard Tran Mills     ierr = MatPtAP_SeqAIJ_SeqAIJ(A,P,scall,fill,C);CHKERRQ(ierr);
885*4f53af40SRichard Tran Mills     PetscFunctionReturn(0);
886*4f53af40SRichard Tran Mills   }
887*4f53af40SRichard Tran Mills 
888*4f53af40SRichard Tran Mills   if (scall == MAT_REUSE_MATRIX) {
889*4f53af40SRichard Tran Mills     ierr = MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2(A,P,*C);CHKERRQ(ierr);
890*4f53af40SRichard Tran Mills     PetscFunctionReturn(0);
891*4f53af40SRichard Tran Mills   }
892*4f53af40SRichard Tran Mills 
893*4f53af40SRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
894*4f53af40SRichard Tran Mills   p = (Mat_SeqAIJMKL*)P->spptr;
895*4f53af40SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
896*4f53af40SRichard Tran Mills   if (!a->sparse_optimized || a->state != state) {
897*4f53af40SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
898*4f53af40SRichard Tran Mills   }
899*4f53af40SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)P,&state);CHKERRQ(ierr);
900*4f53af40SRichard Tran Mills   if (!p->sparse_optimized || p->state != state) {
901*4f53af40SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(P);
902*4f53af40SRichard Tran Mills   }
903*4f53af40SRichard Tran Mills   csrA = a->csrA;
904*4f53af40SRichard Tran Mills   csrP = p->csrA;
905*4f53af40SRichard Tran Mills   descr_type_gen.type = SPARSE_MATRIX_TYPE_GENERAL;
906*4f53af40SRichard Tran Mills 
907*4f53af40SRichard Tran Mills   /* TODO: Below won't work for complex matrix. Protect this! Maybe where function pointers are assigned in MatConvert? */
908*4f53af40SRichard Tran Mills   stat = mkl_sparse_sypr(SPARSE_OPERATION_TRANSPOSE,csrP,csrA,descr_type_gen,&csrC,SPARSE_STAGE_FULL_MULT);
909*4f53af40SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete full mkl_sparse_sypr");
910*4f53af40SRichard Tran Mills 
911*4f53af40SRichard Tran Mills   ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr);
912*4f53af40SRichard Tran Mills   ierr = MatSetOption(*C,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
913*4f53af40SRichard Tran Mills 
914*4f53af40SRichard Tran Mills   PetscFunctionReturn(0);
915*4f53af40SRichard Tran Mills }
916*4f53af40SRichard Tran Mills #endif
917*4f53af40SRichard Tran Mills 
91887c2a1d7SRichard Tran Mills PetscErrorCode MatScale_SeqAIJMKL(Mat inA,PetscScalar alpha)
919db63039fSRichard Tran Mills {
920db63039fSRichard Tran Mills   PetscErrorCode ierr;
921db63039fSRichard Tran Mills 
92287c2a1d7SRichard Tran Mills   PetscFunctionBegin;
923db63039fSRichard Tran Mills   ierr = MatScale_SeqAIJ(inA,alpha);CHKERRQ(ierr);
924db63039fSRichard Tran Mills   ierr = MatSeqAIJMKL_create_mkl_handle(inA);CHKERRQ(ierr);
925db63039fSRichard Tran Mills   PetscFunctionReturn(0);
926db63039fSRichard Tran Mills }
927df555b71SRichard Tran Mills 
92887c2a1d7SRichard Tran Mills PetscErrorCode MatDiagonalScale_SeqAIJMKL(Mat A,Vec ll,Vec rr)
92987c2a1d7SRichard Tran Mills {
93087c2a1d7SRichard Tran Mills   PetscErrorCode ierr;
93187c2a1d7SRichard Tran Mills 
93287c2a1d7SRichard Tran Mills   PetscFunctionBegin;
93387c2a1d7SRichard Tran Mills   ierr = MatDiagonalScale_SeqAIJ(A,ll,rr);CHKERRQ(ierr);
93487c2a1d7SRichard Tran Mills   ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr);
93587c2a1d7SRichard Tran Mills   PetscFunctionReturn(0);
93687c2a1d7SRichard Tran Mills }
93787c2a1d7SRichard Tran Mills 
93887c2a1d7SRichard Tran Mills PetscErrorCode MatDiagonalSet_SeqAIJMKL(Mat Y,Vec D,InsertMode is)
93987c2a1d7SRichard Tran Mills {
94087c2a1d7SRichard Tran Mills   PetscErrorCode ierr;
94187c2a1d7SRichard Tran Mills 
94287c2a1d7SRichard Tran Mills   PetscFunctionBegin;
94387c2a1d7SRichard Tran Mills   ierr = MatDiagonalSet_SeqAIJ(Y,D,is);CHKERRQ(ierr);
94487c2a1d7SRichard Tran Mills   ierr = MatSeqAIJMKL_create_mkl_handle(Y);CHKERRQ(ierr);
94587c2a1d7SRichard Tran Mills   PetscFunctionReturn(0);
94687c2a1d7SRichard Tran Mills }
94787c2a1d7SRichard Tran Mills 
94887c2a1d7SRichard Tran Mills PetscErrorCode MatAXPY_SeqAIJMKL(Mat Y,PetscScalar a,Mat X,MatStructure str)
94987c2a1d7SRichard Tran Mills {
95087c2a1d7SRichard Tran Mills   PetscErrorCode ierr;
95187c2a1d7SRichard Tran Mills 
95287c2a1d7SRichard Tran Mills   PetscFunctionBegin;
95387c2a1d7SRichard Tran Mills   ierr = MatAXPY_SeqAIJ(Y,a,X,str);CHKERRQ(ierr);
95487c2a1d7SRichard Tran Mills   if (str == SAME_NONZERO_PATTERN) {
95587c2a1d7SRichard Tran Mills     /* MatAssemblyEnd() is not called if SAME_NONZERO_PATTERN, so we need to force update of the MKL matrix handle. */
95687c2a1d7SRichard Tran Mills     ierr = MatSeqAIJMKL_create_mkl_handle(Y);CHKERRQ(ierr);
95787c2a1d7SRichard Tran Mills   }
95887c2a1d7SRichard Tran Mills   PetscFunctionReturn(0);
95987c2a1d7SRichard Tran Mills }
96087c2a1d7SRichard Tran Mills 
9614a2a386eSRichard Tran Mills /* MatConvert_SeqAIJ_SeqAIJMKL converts a SeqAIJ matrix into a
9624a2a386eSRichard Tran Mills  * SeqAIJMKL matrix.  This routine is called by the MatCreate_SeqMKLAIJ()
9634a2a386eSRichard Tran Mills  * routine, but can also be used to convert an assembled SeqAIJ matrix
9644a2a386eSRichard Tran Mills  * into a SeqAIJMKL one. */
9654a2a386eSRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJMKL(Mat A,MatType type,MatReuse reuse,Mat *newmat)
9664a2a386eSRichard Tran Mills {
9674a2a386eSRichard Tran Mills   PetscErrorCode ierr;
9684a2a386eSRichard Tran Mills   Mat            B = *newmat;
9694a2a386eSRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl;
970c9d46305SRichard Tran Mills   PetscBool      set;
971e9c94282SRichard Tran Mills   PetscBool      sametype;
9724a2a386eSRichard Tran Mills 
9734a2a386eSRichard Tran Mills   PetscFunctionBegin;
9744a2a386eSRichard Tran Mills   if (reuse == MAT_INITIAL_MATRIX) {
9754a2a386eSRichard Tran Mills     ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr);
9764a2a386eSRichard Tran Mills   }
9774a2a386eSRichard Tran Mills 
978e9c94282SRichard Tran Mills   ierr = PetscObjectTypeCompare((PetscObject)A,type,&sametype);CHKERRQ(ierr);
979e9c94282SRichard Tran Mills   if (sametype) PetscFunctionReturn(0);
980e9c94282SRichard Tran Mills 
9814a2a386eSRichard Tran Mills   ierr     = PetscNewLog(B,&aijmkl);CHKERRQ(ierr);
9824a2a386eSRichard Tran Mills   B->spptr = (void*) aijmkl;
9834a2a386eSRichard Tran Mills 
984df555b71SRichard Tran Mills   /* Set function pointers for methods that we inherit from AIJ but override.
985969800c5SRichard Tran Mills    * We also parse some command line options below, since those determine some of the methods we point to. */
9864a2a386eSRichard Tran Mills   B->ops->duplicate        = MatDuplicate_SeqAIJMKL;
9874a2a386eSRichard Tran Mills   B->ops->assemblyend      = MatAssemblyEnd_SeqAIJMKL;
9884a2a386eSRichard Tran Mills   B->ops->destroy          = MatDestroy_SeqAIJMKL;
989c9d46305SRichard Tran Mills 
9904abfa3b3SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_FALSE;
991d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
992d995685eSRichard Tran Mills   aijmkl->no_SpMV2 = PETSC_FALSE;  /* Default to using the SpMV2 routines if our MKL supports them. */
993a8327b06SKarl Rupp #else
994d995685eSRichard Tran Mills   aijmkl->no_SpMV2 = PETSC_TRUE;
995d995685eSRichard Tran Mills #endif
9965b49642aSRichard Tran Mills   aijmkl->eager_inspection = PETSC_FALSE;
9974abfa3b3SRichard Tran Mills 
9984abfa3b3SRichard Tran Mills   /* Parse command line options. */
999c9d46305SRichard Tran Mills   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"AIJMKL Options","Mat");CHKERRQ(ierr);
1000c9d46305SRichard Tran Mills   ierr = PetscOptionsBool("-mat_aijmkl_no_spmv2","NoSPMV2","None",(PetscBool)aijmkl->no_SpMV2,(PetscBool*)&aijmkl->no_SpMV2,&set);CHKERRQ(ierr);
10015b49642aSRichard Tran Mills   ierr = PetscOptionsBool("-mat_aijmkl_eager_inspection","Eager Inspection","None",(PetscBool)aijmkl->eager_inspection,(PetscBool*)&aijmkl->eager_inspection,&set);CHKERRQ(ierr);
1002c9d46305SRichard Tran Mills   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1003d995685eSRichard Tran Mills #ifndef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
1004d995685eSRichard Tran Mills   if(!aijmkl->no_SpMV2) {
1005d995685eSRichard Tran Mills     ierr = PetscInfo(B,"User requested use of MKL SpMV2 routines, but MKL version does not support mkl_sparse_optimize();  defaulting to non-SpMV2 routines.\n");
1006d995685eSRichard Tran Mills     aijmkl->no_SpMV2 = PETSC_TRUE;
1007d995685eSRichard Tran Mills   }
1008d995685eSRichard Tran Mills #endif
1009c9d46305SRichard Tran Mills 
1010c9d46305SRichard Tran Mills   if(!aijmkl->no_SpMV2) {
1011d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
1012df555b71SRichard Tran Mills     B->ops->mult             = MatMult_SeqAIJMKL_SpMV2;
1013969800c5SRichard Tran Mills     B->ops->multtranspose    = MatMultTranspose_SeqAIJMKL_SpMV2;
1014df555b71SRichard Tran Mills     B->ops->multadd          = MatMultAdd_SeqAIJMKL_SpMV2;
1015969800c5SRichard Tran Mills     B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL_SpMV2;
101645fbe478SRichard Tran Mills     B->ops->matmult          = MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2;
1017e8be1fc7SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_SP2M
1018e8be1fc7SRichard Tran Mills     B->ops->matmultnumeric   = MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2;
1019*4f53af40SRichard Tran Mills #ifndef PETSC_USE_COMPLEX
1020*4f53af40SRichard Tran Mills     B->ops->ptap             = MatPtAP_SeqAIJMKL_SeqAIJMKL_SpMV2;
1021*4f53af40SRichard Tran Mills     B->ops->ptapnumeric      = MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2;
1022*4f53af40SRichard Tran Mills #endif
1023e8be1fc7SRichard Tran Mills #endif
1024a557fde5SRichard Tran Mills     B->ops->transposematmult = MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2;
1025d995685eSRichard Tran Mills #endif
1026c9d46305SRichard Tran Mills   } else {
10274a2a386eSRichard Tran Mills     B->ops->mult             = MatMult_SeqAIJMKL;
1028969800c5SRichard Tran Mills     B->ops->multtranspose    = MatMultTranspose_SeqAIJMKL;
10294a2a386eSRichard Tran Mills     B->ops->multadd          = MatMultAdd_SeqAIJMKL;
1030969800c5SRichard Tran Mills     B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL;
1031c9d46305SRichard Tran Mills   }
10324a2a386eSRichard Tran Mills 
1033db63039fSRichard Tran Mills   B->ops->scale              = MatScale_SeqAIJMKL;
103487c2a1d7SRichard Tran Mills   B->ops->diagonalscale      = MatDiagonalScale_SeqAIJMKL;
103587c2a1d7SRichard Tran Mills   B->ops->diagonalset        = MatDiagonalSet_SeqAIJMKL;
103687c2a1d7SRichard Tran Mills   B->ops->axpy               = MatAXPY_SeqAIJMKL;
1037db63039fSRichard Tran Mills 
1038db63039fSRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatScale_SeqAIJMKL_C",MatScale_SeqAIJMKL);CHKERRQ(ierr);
10394a2a386eSRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",MatConvert_SeqAIJMKL_SeqAIJ);CHKERRQ(ierr);
1040e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaijmkl_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
1041e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaijmkl_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
1042e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaijmkl_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
104345fbe478SRichard Tran Mills   if(!aijmkl->no_SpMV2) {
104445fbe478SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
104545fbe478SRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqaijmkl_seqaijmkl_C",MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr);
1046e8be1fc7SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_SP2M
1047e8be1fc7SRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqaijmkl_seqaijmkl_C",MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr);
1048e8be1fc7SRichard Tran Mills #endif
1049372ec6bbSRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatTransposeMatMult_seqaijmkl_seqaijmkl_C",MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr);
105045fbe478SRichard Tran Mills #endif
105145fbe478SRichard Tran Mills   }
10524a2a386eSRichard Tran Mills 
10534a2a386eSRichard Tran Mills   ierr    = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJMKL);CHKERRQ(ierr);
10544a2a386eSRichard Tran Mills   *newmat = B;
10554a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
10564a2a386eSRichard Tran Mills }
10574a2a386eSRichard Tran Mills 
10584a2a386eSRichard Tran Mills /*@C
10594a2a386eSRichard Tran Mills    MatCreateSeqAIJMKL - Creates a sparse matrix of type SEQAIJMKL.
10604a2a386eSRichard Tran Mills    This type inherits from AIJ and is largely identical, but uses sparse BLAS
10614a2a386eSRichard Tran Mills    routines from Intel MKL whenever possible.
10623af10221SRichard Tran Mills    MatMult, MatMultAdd, MatMultTranspose, MatMultTransposeAdd, MatMatMult, and MatTransposeMatMult
106390147e49SRichard Tran Mills    operations are currently supported.
106490147e49SRichard Tran Mills    If the installed version of MKL supports the "SpMV2" sparse
106590147e49SRichard Tran Mills    inspector-executor routines, then those are used by default.
106690147e49SRichard Tran Mills 
10674a2a386eSRichard Tran Mills    Collective on MPI_Comm
10684a2a386eSRichard Tran Mills 
10694a2a386eSRichard Tran Mills    Input Parameters:
10704a2a386eSRichard Tran Mills +  comm - MPI communicator, set to PETSC_COMM_SELF
10714a2a386eSRichard Tran Mills .  m - number of rows
10724a2a386eSRichard Tran Mills .  n - number of columns
10734a2a386eSRichard Tran Mills .  nz - number of nonzeros per row (same for all rows)
10744a2a386eSRichard Tran Mills -  nnz - array containing the number of nonzeros in the various rows
10754a2a386eSRichard Tran Mills          (possibly different for each row) or NULL
10764a2a386eSRichard Tran Mills 
10774a2a386eSRichard Tran Mills    Output Parameter:
10784a2a386eSRichard Tran Mills .  A - the matrix
10794a2a386eSRichard Tran Mills 
108090147e49SRichard Tran Mills    Options Database Keys:
108166b7eeb6SRichard Tran Mills +  -mat_aijmkl_no_spmv2 - disable use of the SpMV2 inspector-executor routines
108266b7eeb6SRichard Tran Mills -  -mat_aijmkl_eager_inspection - perform MKL "inspection" phase upon matrix assembly; default is to do "lazy" inspection, performing this step the first time the matrix is applied
108390147e49SRichard Tran Mills 
10844a2a386eSRichard Tran Mills    Notes:
10854a2a386eSRichard Tran Mills    If nnz is given then nz is ignored
10864a2a386eSRichard Tran Mills 
10874a2a386eSRichard Tran Mills    Level: intermediate
10884a2a386eSRichard Tran Mills 
108990147e49SRichard Tran Mills .keywords: matrix, MKL, sparse, parallel
10904a2a386eSRichard Tran Mills 
10914a2a386eSRichard Tran Mills .seealso: MatCreate(), MatCreateMPIAIJMKL(), MatSetValues()
10924a2a386eSRichard Tran Mills @*/
10934a2a386eSRichard Tran Mills PetscErrorCode  MatCreateSeqAIJMKL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
10944a2a386eSRichard Tran Mills {
10954a2a386eSRichard Tran Mills   PetscErrorCode ierr;
10964a2a386eSRichard Tran Mills 
10974a2a386eSRichard Tran Mills   PetscFunctionBegin;
10984a2a386eSRichard Tran Mills   ierr = MatCreate(comm,A);CHKERRQ(ierr);
10994a2a386eSRichard Tran Mills   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
11004a2a386eSRichard Tran Mills   ierr = MatSetType(*A,MATSEQAIJMKL);CHKERRQ(ierr);
11014a2a386eSRichard Tran Mills   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
11024a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
11034a2a386eSRichard Tran Mills }
11044a2a386eSRichard Tran Mills 
11054a2a386eSRichard Tran Mills PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJMKL(Mat A)
11064a2a386eSRichard Tran Mills {
11074a2a386eSRichard Tran Mills   PetscErrorCode ierr;
11084a2a386eSRichard Tran Mills 
11094a2a386eSRichard Tran Mills   PetscFunctionBegin;
11104a2a386eSRichard Tran Mills   ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr);
11114a2a386eSRichard Tran Mills   ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr);
11124a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
11134a2a386eSRichard Tran Mills }
1114