xref: /petsc/src/mat/impls/aij/seq/aijmkl/aijmkl.c (revision e05b4f34952eae2eb10984e6c9ba9096749eac59)
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;
20ffcab697SRichard Tran Mills #if defined(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;
34ffcab697SRichard Tran Mills #if defined(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;
534f53af40SRichard Tran Mills   B->ops->ptap             = MatPtAP_SeqAIJ_SeqAIJ;
544f53af40SRichard Tran Mills   B->ops->ptapnumeric      = MatPtAPNumeric_SeqAIJ_SeqAIJ;
55372ec6bbSRichard Tran Mills   B->ops->transposematmult = MatTransposeMatMult_SeqAIJ_SeqAIJ;
564a2a386eSRichard Tran Mills 
57e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",NULL);CHKERRQ(ierr);
58e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr);
59e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr);
60e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr);
61*e05b4f34SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatPtAP_is_seqaijmkl_C",NULL);CHKERRQ(ierr);
62ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
634a940b00SSatish Balay   if(!aijmkl->no_SpMV2) {
6445fbe478SRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqaijmkl_seqaijmkl_C",NULL);CHKERRQ(ierr);
65ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M)
66e8be1fc7SRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqaijmkl_seqaijmkl_C",NULL);CHKERRQ(ierr);
67e8be1fc7SRichard Tran Mills #endif
68372ec6bbSRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatTransposeMatMult_seqaijmkl_seqaijmkl_C",NULL);CHKERRQ(ierr);
6945fbe478SRichard Tran Mills   }
70e9c94282SRichard Tran Mills 
714abfa3b3SRichard Tran Mills   /* Free everything in the Mat_SeqAIJMKL data structure. Currently, this
72e9c94282SRichard Tran Mills    * simply involves destroying the MKL sparse matrix handle and then freeing
73e9c94282SRichard Tran Mills    * the spptr pointer. */
74a8327b06SKarl Rupp   if (reuse == MAT_INITIAL_MATRIX) aijmkl = (Mat_SeqAIJMKL*)B->spptr;
75a8327b06SKarl Rupp 
764abfa3b3SRichard Tran Mills   if (aijmkl->sparse_optimized) {
770632b357SRichard Tran Mills     sparse_status_t stat;
784abfa3b3SRichard Tran Mills     stat = mkl_sparse_destroy(aijmkl->csrA);
799c46acdfSRichard 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");
804abfa3b3SRichard Tran Mills   }
814abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
82e9c94282SRichard Tran Mills   ierr = PetscFree(B->spptr);CHKERRQ(ierr);
834a2a386eSRichard Tran Mills 
844a2a386eSRichard Tran Mills   /* Change the type of B to MATSEQAIJ. */
854a2a386eSRichard Tran Mills   ierr = PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ);CHKERRQ(ierr);
864a2a386eSRichard Tran Mills 
874a2a386eSRichard Tran Mills   *newmat = B;
884a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
894a2a386eSRichard Tran Mills }
904a2a386eSRichard Tran Mills 
914a2a386eSRichard Tran Mills PetscErrorCode MatDestroy_SeqAIJMKL(Mat A)
924a2a386eSRichard Tran Mills {
934a2a386eSRichard Tran Mills   PetscErrorCode ierr;
944a2a386eSRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl = (Mat_SeqAIJMKL*) A->spptr;
954a2a386eSRichard Tran Mills 
964a2a386eSRichard Tran Mills   PetscFunctionBegin;
97e9c94282SRichard Tran Mills 
98e9c94282SRichard Tran Mills   /* If MatHeaderMerge() was used, then this SeqAIJMKL matrix will not have an
99e9c94282SRichard Tran Mills    * spptr pointer. */
100e9c94282SRichard Tran Mills   if (aijmkl) {
1014a2a386eSRichard Tran Mills     /* Clean up everything in the Mat_SeqAIJMKL data structure, then free A->spptr. */
102ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
1034abfa3b3SRichard Tran Mills     if (aijmkl->sparse_optimized) {
1044abfa3b3SRichard Tran Mills       sparse_status_t stat = SPARSE_STATUS_SUCCESS;
1054abfa3b3SRichard Tran Mills       stat = mkl_sparse_destroy(aijmkl->csrA);
1069c46acdfSRichard Tran Mills       if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy");
1074abfa3b3SRichard Tran Mills     }
1084abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
1094a2a386eSRichard Tran Mills     ierr = PetscFree(A->spptr);CHKERRQ(ierr);
110e9c94282SRichard Tran Mills   }
1114a2a386eSRichard Tran Mills 
1124a2a386eSRichard Tran Mills   /* Change the type of A back to SEQAIJ and use MatDestroy_SeqAIJ()
1134a2a386eSRichard Tran Mills    * to destroy everything that remains. */
1144a2a386eSRichard Tran Mills   ierr = PetscObjectChangeTypeName((PetscObject)A, MATSEQAIJ);CHKERRQ(ierr);
1154a2a386eSRichard Tran Mills   /* Note that I don't call MatSetType().  I believe this is because that
1164a2a386eSRichard Tran Mills    * is only to be called when *building* a matrix.  I could be wrong, but
1174a2a386eSRichard Tran Mills    * that is how things work for the SuperLU matrix class. */
1184a2a386eSRichard Tran Mills   ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr);
1194a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
1204a2a386eSRichard Tran Mills }
1214a2a386eSRichard Tran Mills 
1225b49642aSRichard Tran Mills /* MatSeqAIJKL_create_mkl_handle(), if called with an AIJMKL matrix that has not had mkl_sparse_optimize() called for it,
1235b49642aSRichard Tran Mills  * creates an MKL sparse matrix handle from the AIJ arrays and calls mkl_sparse_optimize().
1245b49642aSRichard Tran Mills  * If called with an AIJMKL matrix for which aijmkl->sparse_optimized == PETSC_TRUE, then it destroys the old matrix
1255b49642aSRichard Tran Mills  * handle, creates a new one, and then calls mkl_sparse_optimize().
1265b49642aSRichard Tran Mills  * Although in normal MKL usage it is possible to have a valid matrix handle on which mkl_sparse_optimize() has not been
1275b49642aSRichard Tran Mills  * called, for AIJMKL the handle creation and optimization step always occur together, so we don't handle the case of
1285b49642aSRichard Tran Mills  * an unoptimized matrix handle here. */
1296e369cd5SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_mkl_handle(Mat A)
1304a2a386eSRichard Tran Mills {
131ffcab697SRichard Tran Mills #if !defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
1326e369cd5SRichard Tran Mills   /* If the MKL library does not have mkl_sparse_optimize(), then this routine
1336e369cd5SRichard Tran Mills    * does nothing. We make it callable anyway in this case because it cuts
1346e369cd5SRichard Tran Mills    * down on littering the code with #ifdefs. */
13545fbe478SRichard Tran Mills   PetscFunctionBegin;
1366e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
1376e369cd5SRichard Tran Mills #else
138a8327b06SKarl Rupp   Mat_SeqAIJ       *a = (Mat_SeqAIJ*)A->data;
139a8327b06SKarl Rupp   Mat_SeqAIJMKL    *aijmkl = (Mat_SeqAIJMKL*)A->spptr;
140a8327b06SKarl Rupp   PetscInt         m,n;
141a8327b06SKarl Rupp   MatScalar        *aa;
142a8327b06SKarl Rupp   PetscInt         *aj,*ai;
1436e369cd5SRichard Tran Mills   sparse_status_t  stat;
144551aa5c8SRichard Tran Mills   PetscErrorCode   ierr;
1454a2a386eSRichard Tran Mills 
146a8327b06SKarl Rupp   PetscFunctionBegin;
1476e369cd5SRichard Tran Mills   if (aijmkl->no_SpMV2) PetscFunctionReturn(0);
1486e369cd5SRichard Tran Mills 
1490632b357SRichard Tran Mills   if (aijmkl->sparse_optimized) {
1500632b357SRichard Tran Mills     /* Matrix has been previously assembled and optimized. Must destroy old
1510632b357SRichard Tran Mills      * matrix handle before running the optimization step again. */
1520632b357SRichard Tran Mills     stat = mkl_sparse_destroy(aijmkl->csrA);
1539c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy");
1540632b357SRichard Tran Mills   }
1558d3fe1b0SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_FALSE;
1566e369cd5SRichard Tran Mills 
157c9d46305SRichard Tran Mills   /* Now perform the SpMV2 setup and matrix optimization. */
158df555b71SRichard Tran Mills   aijmkl->descr.type        = SPARSE_MATRIX_TYPE_GENERAL;
159df555b71SRichard Tran Mills   aijmkl->descr.mode        = SPARSE_FILL_MODE_LOWER;
160df555b71SRichard Tran Mills   aijmkl->descr.diag        = SPARSE_DIAG_NON_UNIT;
16158678438SRichard Tran Mills   m = A->rmap->n;
16258678438SRichard Tran Mills   n = A->cmap->n;
163df555b71SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
164df555b71SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
165df555b71SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
16680095d54SIrina Sokolova   if ((a->nz!=0) & !(A->structure_only)) {
1678d3fe1b0SRichard Tran Mills     /* Create a new, optimized sparse matrix handle only if the matrix has nonzero entries.
1688d3fe1b0SRichard Tran Mills      * The MKL sparse-inspector executor routines don't like being passed an empty matrix. */
16958678438SRichard Tran Mills     stat = mkl_sparse_x_create_csr(&aijmkl->csrA,SPARSE_INDEX_BASE_ZERO,m,n,ai,ai+1,aj,aa);
170e8be1fc7SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to create matrix handle");
171df555b71SRichard Tran Mills     stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000);
172e8be1fc7SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set mv_hint");
173df555b71SRichard Tran Mills     stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE);
174e8be1fc7SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set memory_hint");
1751950a7e7SRichard Tran Mills     if (!aijmkl->no_SpMV2) {
176df555b71SRichard Tran Mills       stat = mkl_sparse_optimize(aijmkl->csrA);
177e8be1fc7SRichard Tran Mills       if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_optimize");
1781950a7e7SRichard Tran Mills     }
1794abfa3b3SRichard Tran Mills     aijmkl->sparse_optimized = PETSC_TRUE;
180e995cf24SRichard Tran Mills     ierr = PetscObjectStateGet((PetscObject)A,&(aijmkl->state));CHKERRQ(ierr);
181c9d46305SRichard Tran Mills   }
1826e369cd5SRichard Tran Mills 
1836e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
184d995685eSRichard Tran Mills #endif
1856e369cd5SRichard Tran Mills }
1866e369cd5SRichard Tran Mills 
18719afcda9SRichard Tran Mills /* MatSeqAIJMKL_create_from_mkl_handle() creates a sequential AIJMKL matrix from an MKL sparse matrix handle.
18819afcda9SRichard Tran Mills  * We need this to implement MatMatMult() using the MKL inspector-executor routines, which return an (unoptimized)
1896c87cf42SRichard Tran Mills  * matrix handle.
190aab60f1bSRichard Tran Mills  * Note: This routine simply destroys and replaces the original matrix if MAT_REUSE_MATRIX has been specified, as
191aab60f1bSRichard Tran Mills  * there is no good alternative. */
192ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
1936c87cf42SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_from_mkl_handle(MPI_Comm comm,sparse_matrix_t csrA,MatReuse reuse,Mat *mat)
19419afcda9SRichard Tran Mills {
19519afcda9SRichard Tran Mills   PetscErrorCode      ierr;
19619afcda9SRichard Tran Mills   sparse_status_t     stat;
19719afcda9SRichard Tran Mills   sparse_index_base_t indexing;
19819afcda9SRichard Tran Mills   PetscInt            nrows, ncols;
19945fbe478SRichard Tran Mills   PetscInt            *aj,*ai,*dummy;
20019afcda9SRichard Tran Mills   MatScalar           *aa;
20119afcda9SRichard Tran Mills   Mat                 A;
20219afcda9SRichard Tran Mills   Mat_SeqAIJMKL       *aijmkl;
20319afcda9SRichard Tran Mills 
20445fbe478SRichard Tran Mills   /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */
20545fbe478SRichard Tran Mills   stat = mkl_sparse_x_export_csr(csrA,&indexing,&nrows,&ncols,&ai,&dummy,&aj,&aa);
2069c46acdfSRichard 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()");
2076c87cf42SRichard Tran Mills 
208aab60f1bSRichard Tran Mills   if (reuse == MAT_REUSE_MATRIX) {
209aab60f1bSRichard Tran Mills     ierr = MatDestroy(mat);CHKERRQ(ierr);
210aab60f1bSRichard Tran Mills   }
21119afcda9SRichard Tran Mills   ierr = MatCreate(comm,&A);CHKERRQ(ierr);
21219afcda9SRichard Tran Mills   ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr);
21345fbe478SRichard Tran Mills   ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,nrows,ncols);CHKERRQ(ierr);
214aab60f1bSRichard Tran Mills   /* We use MatSeqAIJSetPreallocationCSR() instead of MatCreateSeqAIJWithArrays() because we must copy the arrays exported
215aab60f1bSRichard Tran Mills    * from MKL; MKL developers tell us that modifying the arrays may cause unexpected results when using the MKL handle, and
216aab60f1bSRichard Tran Mills    * they will be destroyed when the MKL handle is destroyed.
217aab60f1bSRichard Tran Mills    * (In the interest of reducing memory consumption in future, can we figure out good ways to deal with this?) */
21819afcda9SRichard Tran Mills   ierr = MatSeqAIJSetPreallocationCSR(A,ai,aj,aa);CHKERRQ(ierr);
21919afcda9SRichard Tran Mills 
22019afcda9SRichard Tran Mills   /* We now have an assembled sequential AIJ matrix created from copies of the exported arrays from the MKL matrix handle.
22119afcda9SRichard Tran Mills    * Now turn it into a MATSEQAIJMKL. */
22219afcda9SRichard Tran Mills   ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr);
2236c87cf42SRichard Tran Mills 
22419afcda9SRichard Tran Mills   aijmkl = (Mat_SeqAIJMKL*) A->spptr;
22519afcda9SRichard Tran Mills   aijmkl->csrA = csrA;
2266c87cf42SRichard Tran Mills 
22719afcda9SRichard Tran Mills   /* The below code duplicates much of what is in MatSeqAIJKL_create_mkl_handle(). I dislike this code duplication, but
22819afcda9SRichard Tran Mills    * MatSeqAIJMKL_create_mkl_handle() cannot be used because we don't need to create a handle -- we've already got one,
22919afcda9SRichard Tran Mills    * and just need to be able to run the MKL optimization step. */
230f3fd1758SRichard Tran Mills   aijmkl->descr.type        = SPARSE_MATRIX_TYPE_GENERAL;
231f3fd1758SRichard Tran Mills   aijmkl->descr.mode        = SPARSE_FILL_MODE_LOWER;
232f3fd1758SRichard Tran Mills   aijmkl->descr.diag        = SPARSE_DIAG_NON_UNIT;
23319afcda9SRichard Tran Mills   stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000);
23451539a68SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set mv_hint");
23519afcda9SRichard Tran Mills   stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE);
23651539a68SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set memory_hint");
2371950a7e7SRichard Tran Mills   if (!aijmkl->no_SpMV2) {
23819afcda9SRichard Tran Mills     stat = mkl_sparse_optimize(aijmkl->csrA);
23951539a68SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_optimize");
2401950a7e7SRichard Tran Mills   }
24119afcda9SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_TRUE;
242e995cf24SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&(aijmkl->state));CHKERRQ(ierr);
24319afcda9SRichard Tran Mills 
24419afcda9SRichard Tran Mills   *mat = A;
24519afcda9SRichard Tran Mills   PetscFunctionReturn(0);
24619afcda9SRichard Tran Mills }
24719afcda9SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
24819afcda9SRichard Tran Mills 
249e8be1fc7SRichard Tran Mills /* MatSeqAIJMKL_update_from_mkl_handle() updates the matrix values array from the contents of the associated MKL sparse matrix handle.
250e8be1fc7SRichard 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
251e8be1fc7SRichard Tran Mills  * MatMatMultNumeric(). */
252ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
253e8be1fc7SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_update_from_mkl_handle(Mat A)
254e8be1fc7SRichard Tran Mills {
255e8be1fc7SRichard Tran Mills   PetscInt            i;
256e8be1fc7SRichard Tran Mills   PetscInt            nrows,ncols;
257e8be1fc7SRichard Tran Mills   PetscInt            nz;
258e8be1fc7SRichard Tran Mills   PetscInt            *ai,*aj,*dummy;
259e8be1fc7SRichard Tran Mills   PetscScalar         *aa;
260e8be1fc7SRichard Tran Mills   PetscErrorCode      ierr;
261e8be1fc7SRichard Tran Mills   Mat_SeqAIJMKL       *aijmkl;
262e8be1fc7SRichard Tran Mills   sparse_status_t     stat;
263e8be1fc7SRichard Tran Mills   sparse_index_base_t indexing;
264e8be1fc7SRichard Tran Mills 
265e8be1fc7SRichard Tran Mills   aijmkl = (Mat_SeqAIJMKL*) A->spptr;
266e8be1fc7SRichard Tran Mills 
267e8be1fc7SRichard Tran Mills   /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */
268e8be1fc7SRichard Tran Mills   stat = mkl_sparse_x_export_csr(aijmkl->csrA,&indexing,&nrows,&ncols,&ai,&dummy,&aj,&aa);
269e8be1fc7SRichard 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()");
270e8be1fc7SRichard Tran Mills 
271e8be1fc7SRichard 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
272e8be1fc7SRichard Tran Mills    * representations differ in small ways (e.g., more explicit nonzeros per row due to preallocation). */
273e8be1fc7SRichard Tran Mills   for (i=0; i<nrows; i++) {
274e8be1fc7SRichard Tran Mills     nz = ai[i+1] - ai[i];
275e8be1fc7SRichard Tran Mills     ierr = MatSetValues_SeqAIJ(A, 1, &i, nz, aj+ai[i], aa+ai[i], INSERT_VALUES);CHKERRQ(ierr);
276e8be1fc7SRichard Tran Mills   }
277e8be1fc7SRichard Tran Mills 
278e8be1fc7SRichard Tran Mills   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
279e8be1fc7SRichard Tran Mills   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
280e8be1fc7SRichard Tran Mills 
281e995cf24SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&(aijmkl->state));CHKERRQ(ierr);
282e995cf24SRichard Tran Mills   /* We mark our matrix as having a valid, optimized MKL handle.
283e995cf24SRichard Tran Mills    * TODO: It is valid, but I am not sure if it is optimized. Need to ask MKL developers. */
284e995cf24SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_TRUE;
285e995cf24SRichard Tran Mills 
286e8be1fc7SRichard Tran Mills   PetscFunctionReturn(0);
287e8be1fc7SRichard Tran Mills }
288e8be1fc7SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
289e8be1fc7SRichard Tran Mills 
2906e369cd5SRichard Tran Mills PetscErrorCode MatDuplicate_SeqAIJMKL(Mat A, MatDuplicateOption op, Mat *M)
2916e369cd5SRichard Tran Mills {
2926e369cd5SRichard Tran Mills   PetscErrorCode ierr;
2936e369cd5SRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl;
2946e369cd5SRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl_dest;
2956e369cd5SRichard Tran Mills 
2966e369cd5SRichard Tran Mills   PetscFunctionBegin;
2976e369cd5SRichard Tran Mills   ierr = MatDuplicate_SeqAIJ(A,op,M);CHKERRQ(ierr);
2986e369cd5SRichard Tran Mills   aijmkl      = (Mat_SeqAIJMKL*) A->spptr;
2996e369cd5SRichard Tran Mills   aijmkl_dest = (Mat_SeqAIJMKL*) (*M)->spptr;
3006e369cd5SRichard Tran Mills   ierr = PetscMemcpy(aijmkl_dest,aijmkl,sizeof(Mat_SeqAIJMKL));CHKERRQ(ierr);
3016e369cd5SRichard Tran Mills   aijmkl_dest->sparse_optimized = PETSC_FALSE;
3025b49642aSRichard Tran Mills   if (aijmkl->eager_inspection) {
3036e369cd5SRichard Tran Mills     ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr);
3045b49642aSRichard Tran Mills   }
3056e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
3066e369cd5SRichard Tran Mills }
3076e369cd5SRichard Tran Mills 
3086e369cd5SRichard Tran Mills PetscErrorCode MatAssemblyEnd_SeqAIJMKL(Mat A, MatAssemblyType mode)
3096e369cd5SRichard Tran Mills {
3106e369cd5SRichard Tran Mills   PetscErrorCode  ierr;
3116e369cd5SRichard Tran Mills   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3125b49642aSRichard Tran Mills   Mat_SeqAIJMKL   *aijmkl;
3136e369cd5SRichard Tran Mills 
3146e369cd5SRichard Tran Mills   PetscFunctionBegin;
3156e369cd5SRichard Tran Mills   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
3166e369cd5SRichard Tran Mills 
3176e369cd5SRichard Tran Mills   /* Since a MATSEQAIJMKL matrix is really just a MATSEQAIJ with some
3186e369cd5SRichard Tran Mills    * extra information and some different methods, call the AssemblyEnd
3196e369cd5SRichard Tran Mills    * routine for a MATSEQAIJ.
3206e369cd5SRichard Tran Mills    * I'm not sure if this is the best way to do this, but it avoids
321d96e85feSRichard Tran Mills    * a lot of code duplication. */
3226e369cd5SRichard Tran Mills   a->inode.use = PETSC_FALSE;  /* Must disable: otherwise the MKL routines won't get used. */
3236e369cd5SRichard Tran Mills   ierr = MatAssemblyEnd_SeqAIJ(A, mode);CHKERRQ(ierr);
3246e369cd5SRichard Tran Mills 
3255b49642aSRichard Tran Mills   /* If the user has requested "eager" inspection, create the optimized MKL sparse handle (if needed; the function checks).
3265b49642aSRichard Tran Mills    * (The default is to do "lazy" inspection, deferring this until something like MatMult() is called.) */
3275b49642aSRichard Tran Mills   aijmkl = (Mat_SeqAIJMKL*) A->spptr;
3285b49642aSRichard Tran Mills   if (aijmkl->eager_inspection) {
3296e369cd5SRichard Tran Mills     ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr);
3305b49642aSRichard Tran Mills   }
331df555b71SRichard Tran Mills 
3324a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
3334a2a386eSRichard Tran Mills }
3344a2a386eSRichard Tran Mills 
335ffcab697SRichard Tran Mills #if !defined(PETSC_HAVE_MKL_SPARSE_SP2M)
3364a2a386eSRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL(Mat A,Vec xx,Vec yy)
3374a2a386eSRichard Tran Mills {
3384a2a386eSRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
3394a2a386eSRichard Tran Mills   const PetscScalar *x;
3404a2a386eSRichard Tran Mills   PetscScalar       *y;
3414a2a386eSRichard Tran Mills   const MatScalar   *aa;
3424a2a386eSRichard Tran Mills   PetscErrorCode    ierr;
3434a2a386eSRichard Tran Mills   PetscInt          m=A->rmap->n;
344db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
345db63039fSRichard Tran Mills   PetscScalar       alpha = 1.0;
346db63039fSRichard Tran Mills   PetscScalar       beta = 0.0;
3474a2a386eSRichard Tran Mills   const PetscInt    *aj,*ai;
348db63039fSRichard Tran Mills   char              matdescra[6];
349db63039fSRichard Tran Mills 
3504a2a386eSRichard Tran Mills 
3514a2a386eSRichard Tran Mills   /* Variables not in MatMult_SeqAIJ. */
352ff03dc53SRichard Tran Mills   char transa = 'n';  /* Used to indicate to MKL that we are not computing the transpose product. */
353ff03dc53SRichard Tran Mills 
354ff03dc53SRichard Tran Mills   PetscFunctionBegin;
355db63039fSRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
356db63039fSRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
357ff03dc53SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
358ff03dc53SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
359ff03dc53SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
360ff03dc53SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
361ff03dc53SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
362ff03dc53SRichard Tran Mills 
363ff03dc53SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
364db63039fSRichard Tran Mills   mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y);
365ff03dc53SRichard Tran Mills 
366ff03dc53SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
367ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
368ff03dc53SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
369ff03dc53SRichard Tran Mills   PetscFunctionReturn(0);
370ff03dc53SRichard Tran Mills }
3711950a7e7SRichard Tran Mills #endif
372ff03dc53SRichard Tran Mills 
373ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
374df555b71SRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy)
375df555b71SRichard Tran Mills {
376df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
377df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
378df555b71SRichard Tran Mills   const PetscScalar *x;
379df555b71SRichard Tran Mills   PetscScalar       *y;
380df555b71SRichard Tran Mills   PetscErrorCode    ierr;
381df555b71SRichard Tran Mills   sparse_status_t   stat = SPARSE_STATUS_SUCCESS;
382551aa5c8SRichard Tran Mills   PetscObjectState  state;
383df555b71SRichard Tran Mills 
384df555b71SRichard Tran Mills   PetscFunctionBegin;
385df555b71SRichard Tran Mills 
38638987b35SRichard Tran Mills   /* If there are no nonzero entries, zero yy and return immediately. */
38738987b35SRichard Tran Mills   if(!a->nz) {
38838987b35SRichard Tran Mills     PetscInt i;
38938987b35SRichard Tran Mills     PetscInt m=A->rmap->n;
39038987b35SRichard Tran Mills     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
39138987b35SRichard Tran Mills     for (i=0; i<m; i++) {
39238987b35SRichard Tran Mills       y[i] = 0.0;
39338987b35SRichard Tran Mills     }
39438987b35SRichard Tran Mills     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
39538987b35SRichard Tran Mills     PetscFunctionReturn(0);
39638987b35SRichard Tran Mills   }
397f36dfe3fSRichard Tran Mills 
398df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
399df555b71SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
400df555b71SRichard Tran Mills 
4013fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
4023fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
4033fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
404551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
405551aa5c8SRichard Tran Mills   if (!aijmkl->sparse_optimized || aijmkl->state != state) {
4063fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
4073fa15762SRichard Tran Mills   }
4083fa15762SRichard Tran Mills 
409df555b71SRichard Tran Mills   /* Call MKL SpMV2 executor routine to do the MatMult. */
410df555b71SRichard Tran Mills   stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y);
4119c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
412df555b71SRichard Tran Mills 
413df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
414df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
415df555b71SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
416df555b71SRichard Tran Mills   PetscFunctionReturn(0);
417df555b71SRichard Tran Mills }
418d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
419df555b71SRichard Tran Mills 
420ffcab697SRichard Tran Mills #if !defined(PETSC_HAVE_MKL_SPARSE_SP2M)
421ff03dc53SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL(Mat A,Vec xx,Vec yy)
422ff03dc53SRichard Tran Mills {
423ff03dc53SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
424ff03dc53SRichard Tran Mills   const PetscScalar *x;
425ff03dc53SRichard Tran Mills   PetscScalar       *y;
426ff03dc53SRichard Tran Mills   const MatScalar   *aa;
427ff03dc53SRichard Tran Mills   PetscErrorCode    ierr;
428ff03dc53SRichard Tran Mills   PetscInt          m=A->rmap->n;
429db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
430db63039fSRichard Tran Mills   PetscScalar       alpha = 1.0;
431db63039fSRichard Tran Mills   PetscScalar       beta = 0.0;
432ff03dc53SRichard Tran Mills   const PetscInt    *aj,*ai;
433db63039fSRichard Tran Mills   char              matdescra[6];
434ff03dc53SRichard Tran Mills 
435ff03dc53SRichard Tran Mills   /* Variables not in MatMultTranspose_SeqAIJ. */
436ff03dc53SRichard Tran Mills   char transa = 't';  /* Used to indicate to MKL that we are computing the transpose product. */
4374a2a386eSRichard Tran Mills 
4384a2a386eSRichard Tran Mills   PetscFunctionBegin;
439969800c5SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
440969800c5SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
4414a2a386eSRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
4424a2a386eSRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
4434a2a386eSRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
4444a2a386eSRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
4454a2a386eSRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
4464a2a386eSRichard Tran Mills 
4474a2a386eSRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
448db63039fSRichard Tran Mills   mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y);
4494a2a386eSRichard Tran Mills 
4504a2a386eSRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
4514a2a386eSRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
4524a2a386eSRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
4534a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
4544a2a386eSRichard Tran Mills }
4551950a7e7SRichard Tran Mills #endif
4564a2a386eSRichard Tran Mills 
457ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
458df555b71SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy)
459df555b71SRichard Tran Mills {
460df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
461df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
462df555b71SRichard Tran Mills   const PetscScalar *x;
463df555b71SRichard Tran Mills   PetscScalar       *y;
464df555b71SRichard Tran Mills   PetscErrorCode    ierr;
4650632b357SRichard Tran Mills   sparse_status_t   stat;
466551aa5c8SRichard Tran Mills   PetscObjectState  state;
467df555b71SRichard Tran Mills 
468df555b71SRichard Tran Mills   PetscFunctionBegin;
469df555b71SRichard Tran Mills 
47038987b35SRichard Tran Mills   /* If there are no nonzero entries, zero yy and return immediately. */
47138987b35SRichard Tran Mills   if(!a->nz) {
47238987b35SRichard Tran Mills     PetscInt i;
47338987b35SRichard Tran Mills     PetscInt n=A->cmap->n;
47438987b35SRichard Tran Mills     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
47538987b35SRichard Tran Mills     for (i=0; i<n; i++) {
47638987b35SRichard Tran Mills       y[i] = 0.0;
47738987b35SRichard Tran Mills     }
47838987b35SRichard Tran Mills     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
47938987b35SRichard Tran Mills     PetscFunctionReturn(0);
48038987b35SRichard Tran Mills   }
481f36dfe3fSRichard Tran Mills 
482df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
483df555b71SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
484df555b71SRichard Tran Mills 
4853fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
4863fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
4873fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
488551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
489551aa5c8SRichard Tran Mills   if (!aijmkl->sparse_optimized || aijmkl->state != state) {
4903fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
4913fa15762SRichard Tran Mills   }
4923fa15762SRichard Tran Mills 
493df555b71SRichard Tran Mills   /* Call MKL SpMV2 executor routine to do the MatMultTranspose. */
494df555b71SRichard Tran Mills   stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y);
4959c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
496df555b71SRichard Tran Mills 
497df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
498df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
499df555b71SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
500df555b71SRichard Tran Mills   PetscFunctionReturn(0);
501df555b71SRichard Tran Mills }
502d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
503df555b71SRichard Tran Mills 
504ffcab697SRichard Tran Mills #if !defined(PETSC_HAVE_MKL_SPARSE_SP2M)
5054a2a386eSRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz)
5064a2a386eSRichard Tran Mills {
5074a2a386eSRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
5084a2a386eSRichard Tran Mills   const PetscScalar *x;
5094a2a386eSRichard Tran Mills   PetscScalar       *y,*z;
5104a2a386eSRichard Tran Mills   const MatScalar   *aa;
5114a2a386eSRichard Tran Mills   PetscErrorCode    ierr;
5124a2a386eSRichard Tran Mills   PetscInt          m=A->rmap->n;
513db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
5144a2a386eSRichard Tran Mills   const PetscInt    *aj,*ai;
5154a2a386eSRichard Tran Mills   PetscInt          i;
5164a2a386eSRichard Tran Mills 
517ff03dc53SRichard Tran Mills   /* Variables not in MatMultAdd_SeqAIJ. */
518ff03dc53SRichard Tran Mills   char              transa = 'n';  /* Used to indicate to MKL that we are not computing the transpose product. */
519a84739b8SRichard Tran Mills   PetscScalar       alpha = 1.0;
520db63039fSRichard Tran Mills   PetscScalar       beta;
521a84739b8SRichard Tran Mills   char              matdescra[6];
522ff03dc53SRichard Tran Mills 
523ff03dc53SRichard Tran Mills   PetscFunctionBegin;
524a84739b8SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
525a84739b8SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
526a84739b8SRichard Tran Mills 
527ff03dc53SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
528ff03dc53SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
529ff03dc53SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
530ff03dc53SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
531ff03dc53SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
532ff03dc53SRichard Tran Mills 
533ff03dc53SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
534a84739b8SRichard Tran Mills   if (zz == yy) {
535a84739b8SRichard 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. */
536db63039fSRichard Tran Mills     beta = 1.0;
537db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
538a84739b8SRichard Tran Mills   } else {
539db63039fSRichard Tran Mills     /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z.
540db63039fSRichard Tran Mills      * MKL sparse BLAS does not have a MatMultAdd equivalent. */
541db63039fSRichard Tran Mills     beta = 0.0;
542db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
543ff03dc53SRichard Tran Mills     for (i=0; i<m; i++) {
544ff03dc53SRichard Tran Mills       z[i] += y[i];
545ff03dc53SRichard Tran Mills     }
546a84739b8SRichard Tran Mills   }
547ff03dc53SRichard Tran Mills 
548ff03dc53SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
549ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
550ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
551ff03dc53SRichard Tran Mills   PetscFunctionReturn(0);
552ff03dc53SRichard Tran Mills }
5531950a7e7SRichard Tran Mills #endif
554ff03dc53SRichard Tran Mills 
555ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
556df555b71SRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz)
557df555b71SRichard Tran Mills {
558df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
559df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
560df555b71SRichard Tran Mills   const PetscScalar *x;
561df555b71SRichard Tran Mills   PetscScalar       *y,*z;
562df555b71SRichard Tran Mills   PetscErrorCode    ierr;
563df555b71SRichard Tran Mills   PetscInt          m=A->rmap->n;
564df555b71SRichard Tran Mills   PetscInt          i;
565df555b71SRichard Tran Mills 
566df555b71SRichard Tran Mills   /* Variables not in MatMultAdd_SeqAIJ. */
567df555b71SRichard Tran Mills   sparse_status_t   stat = SPARSE_STATUS_SUCCESS;
568551aa5c8SRichard Tran Mills   PetscObjectState  state;
569df555b71SRichard Tran Mills 
570df555b71SRichard Tran Mills   PetscFunctionBegin;
571df555b71SRichard Tran Mills 
57238987b35SRichard Tran Mills   /* If there are no nonzero entries, set zz = yy and return immediately. */
57338987b35SRichard Tran Mills   if(!a->nz) {
57438987b35SRichard Tran Mills     PetscInt i;
57538987b35SRichard Tran Mills     ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
57638987b35SRichard Tran Mills     for (i=0; i<m; i++) {
57738987b35SRichard Tran Mills       z[i] = y[i];
57838987b35SRichard Tran Mills     }
57938987b35SRichard Tran Mills     ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
58038987b35SRichard Tran Mills     PetscFunctionReturn(0);
58138987b35SRichard Tran Mills   }
582df555b71SRichard Tran Mills 
583df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
584df555b71SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
585df555b71SRichard Tran Mills 
5863fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
5873fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
5883fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
589551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
590551aa5c8SRichard Tran Mills   if (!aijmkl->sparse_optimized || aijmkl->state != state) {
5913fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
5923fa15762SRichard Tran Mills   }
5933fa15762SRichard Tran Mills 
594df555b71SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
595df555b71SRichard Tran Mills   if (zz == yy) {
596df555b71SRichard 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,
597df555b71SRichard Tran Mills      * with alpha and beta both set to 1.0. */
598db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z);
5999c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
600df555b71SRichard Tran Mills   } else {
601df555b71SRichard 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
602df555b71SRichard Tran Mills      * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */
603db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z);
6049c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
605df555b71SRichard Tran Mills     for (i=0; i<m; i++) {
606df555b71SRichard Tran Mills       z[i] += y[i];
607df555b71SRichard Tran Mills     }
608df555b71SRichard Tran Mills   }
609df555b71SRichard Tran Mills 
610df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
611df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
612df555b71SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
613df555b71SRichard Tran Mills   PetscFunctionReturn(0);
614df555b71SRichard Tran Mills }
615d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
616df555b71SRichard Tran Mills 
617ffcab697SRichard Tran Mills #if !defined(PETSC_HAVE_MKL_SPARSE_SP2M)
618ff03dc53SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz)
619ff03dc53SRichard Tran Mills {
620ff03dc53SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
621ff03dc53SRichard Tran Mills   const PetscScalar *x;
622ff03dc53SRichard Tran Mills   PetscScalar       *y,*z;
623ff03dc53SRichard Tran Mills   const MatScalar   *aa;
624ff03dc53SRichard Tran Mills   PetscErrorCode    ierr;
625ff03dc53SRichard Tran Mills   PetscInt          m=A->rmap->n;
626db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
627ff03dc53SRichard Tran Mills   const PetscInt    *aj,*ai;
628ff03dc53SRichard Tran Mills   PetscInt          i;
629ff03dc53SRichard Tran Mills 
630ff03dc53SRichard Tran Mills   /* Variables not in MatMultTransposeAdd_SeqAIJ. */
631ff03dc53SRichard Tran Mills   char transa = 't';  /* Used to indicate to MKL that we are computing the transpose product. */
632a84739b8SRichard Tran Mills   PetscScalar       alpha = 1.0;
633db63039fSRichard Tran Mills   PetscScalar       beta;
634a84739b8SRichard Tran Mills   char              matdescra[6];
6354a2a386eSRichard Tran Mills 
6364a2a386eSRichard Tran Mills   PetscFunctionBegin;
637a84739b8SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
638a84739b8SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
639a84739b8SRichard Tran Mills 
6404a2a386eSRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
6414a2a386eSRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
6424a2a386eSRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
6434a2a386eSRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
6444a2a386eSRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
6454a2a386eSRichard Tran Mills 
6464a2a386eSRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
647a84739b8SRichard Tran Mills   if (zz == yy) {
648a84739b8SRichard 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. */
649db63039fSRichard Tran Mills     beta = 1.0;
650969800c5SRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
651a84739b8SRichard Tran Mills   } else {
652db63039fSRichard Tran Mills     /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z.
653db63039fSRichard Tran Mills      * MKL sparse BLAS does not have a MatMultAdd equivalent. */
654db63039fSRichard Tran Mills     beta = 0.0;
655db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
656969800c5SRichard Tran Mills     for (i=0; i<n; i++) {
6574a2a386eSRichard Tran Mills       z[i] += y[i];
6584a2a386eSRichard Tran Mills     }
659a84739b8SRichard Tran Mills   }
6604a2a386eSRichard Tran Mills 
6614a2a386eSRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
6624a2a386eSRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
6634a2a386eSRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
6644a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
6654a2a386eSRichard Tran Mills }
6661950a7e7SRichard Tran Mills #endif
6674a2a386eSRichard Tran Mills 
668ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
669df555b71SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz)
670df555b71SRichard Tran Mills {
671df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
672df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
673df555b71SRichard Tran Mills   const PetscScalar *x;
674df555b71SRichard Tran Mills   PetscScalar       *y,*z;
675df555b71SRichard Tran Mills   PetscErrorCode    ierr;
676969800c5SRichard Tran Mills   PetscInt          n=A->cmap->n;
677df555b71SRichard Tran Mills   PetscInt          i;
678551aa5c8SRichard Tran Mills   PetscObjectState  state;
679df555b71SRichard Tran Mills 
680df555b71SRichard Tran Mills   /* Variables not in MatMultTransposeAdd_SeqAIJ. */
681df555b71SRichard Tran Mills   sparse_status_t stat = SPARSE_STATUS_SUCCESS;
682df555b71SRichard Tran Mills 
683df555b71SRichard Tran Mills   PetscFunctionBegin;
684df555b71SRichard Tran Mills 
68538987b35SRichard Tran Mills   /* If there are no nonzero entries, set zz = yy and return immediately. */
68638987b35SRichard Tran Mills   if(!a->nz) {
68738987b35SRichard Tran Mills     PetscInt i;
68838987b35SRichard Tran Mills     ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
68938987b35SRichard Tran Mills     for (i=0; i<n; i++) {
69038987b35SRichard Tran Mills       z[i] = y[i];
69138987b35SRichard Tran Mills     }
69238987b35SRichard Tran Mills     ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
69338987b35SRichard Tran Mills     PetscFunctionReturn(0);
69438987b35SRichard Tran Mills   }
695f36dfe3fSRichard Tran Mills 
696df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
697df555b71SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
698df555b71SRichard Tran Mills 
6993fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
7003fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
7013fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
702551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
703551aa5c8SRichard Tran Mills   if (!aijmkl->sparse_optimized || aijmkl->state != state) {
7043fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
7053fa15762SRichard Tran Mills   }
7063fa15762SRichard Tran Mills 
707df555b71SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
708df555b71SRichard Tran Mills   if (zz == yy) {
709df555b71SRichard 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,
710df555b71SRichard Tran Mills      * with alpha and beta both set to 1.0. */
711db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z);
7129c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
713df555b71SRichard Tran Mills   } else {
714df555b71SRichard 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
715df555b71SRichard Tran Mills      * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */
716db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z);
7179c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
718969800c5SRichard Tran Mills     for (i=0; i<n; i++) {
719df555b71SRichard Tran Mills       z[i] += y[i];
720df555b71SRichard Tran Mills     }
721df555b71SRichard Tran Mills   }
722df555b71SRichard Tran Mills 
723df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
724df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
725df555b71SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
726df555b71SRichard Tran Mills   PetscFunctionReturn(0);
727df555b71SRichard Tran Mills }
728d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
729df555b71SRichard Tran Mills 
730ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
731aab60f1bSRichard Tran Mills /* Note that this code currently doesn't actually get used when MatMatMult() is called with MAT_REUSE_MATRIX, because
732aab60f1bSRichard Tran Mills  * the MatMatMult() interface code calls MatMatMultNumeric() in this case.
7333ecbffd0SRichard Tran Mills  * For releases of MKL prior to version 18, update 2:
734aab60f1bSRichard Tran Mills  * MKL has no notion of separately callable symbolic vs. numeric phases of sparse matrix-matrix multiply, so in the
735aab60f1bSRichard Tran Mills  * MAT_REUSE_MATRIX case, the SeqAIJ routines end up being used. Even though this means that the (hopefully more
736aab60f1bSRichard Tran Mills  * optimized) MKL routines do not get used, this probably is best because the MKL routines would waste time re-computing
737aab60f1bSRichard Tran Mills  * the symbolic portion, whereas the native PETSc SeqAIJ routines will avoid this. */
73845fbe478SRichard Tran Mills PetscErrorCode MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat*C)
73945fbe478SRichard Tran Mills {
74045fbe478SRichard Tran Mills   Mat_SeqAIJMKL    *a, *b;
74145fbe478SRichard Tran Mills   sparse_matrix_t  csrA, csrB, csrC;
74245fbe478SRichard Tran Mills   PetscErrorCode   ierr;
74345fbe478SRichard Tran Mills   sparse_status_t  stat = SPARSE_STATUS_SUCCESS;
744551aa5c8SRichard Tran Mills   PetscObjectState state;
74545fbe478SRichard Tran Mills 
74645fbe478SRichard Tran Mills   PetscFunctionBegin;
74745fbe478SRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
74845fbe478SRichard Tran Mills   b = (Mat_SeqAIJMKL*)B->spptr;
749551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
750551aa5c8SRichard Tran Mills   if (!a->sparse_optimized || a->state != state) {
75145fbe478SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
75245fbe478SRichard Tran Mills   }
753551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr);
754551aa5c8SRichard Tran Mills   if (!b->sparse_optimized || b->state != state) {
75545fbe478SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(B);
75645fbe478SRichard Tran Mills   }
75745fbe478SRichard Tran Mills   csrA = a->csrA;
75845fbe478SRichard Tran Mills   csrB = b->csrA;
75945fbe478SRichard Tran Mills 
76045fbe478SRichard Tran Mills   stat = mkl_sparse_spmm(SPARSE_OPERATION_NON_TRANSPOSE,csrA,csrB,&csrC);
7619c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete sparse matrix-matrix multiply");
76245fbe478SRichard Tran Mills 
7636c87cf42SRichard Tran Mills   ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr);
76445fbe478SRichard Tran Mills 
76545fbe478SRichard Tran Mills   PetscFunctionReturn(0);
76645fbe478SRichard Tran Mills }
76745fbe478SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
76845fbe478SRichard Tran Mills 
769ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M)
770e8be1fc7SRichard Tran Mills PetscErrorCode MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,Mat C)
771e8be1fc7SRichard Tran Mills {
772e8be1fc7SRichard Tran Mills   Mat_SeqAIJMKL       *a, *b, *c;
773e8be1fc7SRichard Tran Mills   sparse_matrix_t     csrA, csrB, csrC;
774e8be1fc7SRichard Tran Mills   PetscErrorCode      ierr;
775e8be1fc7SRichard Tran Mills   sparse_status_t     stat = SPARSE_STATUS_SUCCESS;
776e8be1fc7SRichard Tran Mills   struct matrix_descr descr_type_gen;
777e8be1fc7SRichard Tran Mills   PetscObjectState    state;
778e8be1fc7SRichard Tran Mills 
779e8be1fc7SRichard Tran Mills   PetscFunctionBegin;
780e8be1fc7SRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
781e8be1fc7SRichard Tran Mills   b = (Mat_SeqAIJMKL*)B->spptr;
782e8be1fc7SRichard Tran Mills   c = (Mat_SeqAIJMKL*)C->spptr;
783e8be1fc7SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
784e8be1fc7SRichard Tran Mills   if (!a->sparse_optimized || a->state != state) {
785e8be1fc7SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
786e8be1fc7SRichard Tran Mills   }
787e8be1fc7SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr);
788e8be1fc7SRichard Tran Mills   if (!b->sparse_optimized || b->state != state) {
789e8be1fc7SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(B);
790e8be1fc7SRichard Tran Mills   }
791e8be1fc7SRichard Tran Mills   csrA = a->csrA;
792e8be1fc7SRichard Tran Mills   csrB = b->csrA;
793e8be1fc7SRichard Tran Mills   csrC = c->csrA;
794e8be1fc7SRichard Tran Mills   descr_type_gen.type = SPARSE_MATRIX_TYPE_GENERAL;
795e8be1fc7SRichard Tran Mills 
796e8be1fc7SRichard Tran Mills   stat = mkl_sparse_sp2m(SPARSE_OPERATION_NON_TRANSPOSE,descr_type_gen,csrA,
797e8be1fc7SRichard Tran Mills                          SPARSE_OPERATION_NON_TRANSPOSE,descr_type_gen,csrB,
798e8be1fc7SRichard Tran Mills                          SPARSE_STAGE_FINALIZE_MULT,&csrC);
799e8be1fc7SRichard Tran Mills 
800e8be1fc7SRichard 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");
801e8be1fc7SRichard Tran Mills 
802e8be1fc7SRichard Tran Mills   /* Have to update the PETSc AIJ representation for matrix C from contents of MKL handle. */
8034f53af40SRichard Tran Mills   ierr = MatSeqAIJMKL_update_from_mkl_handle(C);CHKERRQ(ierr);
804e8be1fc7SRichard Tran Mills 
805e8be1fc7SRichard Tran Mills   PetscFunctionReturn(0);
806e8be1fc7SRichard Tran Mills }
807e8be1fc7SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_SP2M */
808e8be1fc7SRichard Tran Mills 
809ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
810372ec6bbSRichard Tran Mills PetscErrorCode MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat*C)
811372ec6bbSRichard Tran Mills {
812372ec6bbSRichard Tran Mills   Mat_SeqAIJMKL    *a, *b;
813372ec6bbSRichard Tran Mills   sparse_matrix_t  csrA, csrB, csrC;
814372ec6bbSRichard Tran Mills   PetscErrorCode   ierr;
815372ec6bbSRichard Tran Mills   sparse_status_t  stat = SPARSE_STATUS_SUCCESS;
816551aa5c8SRichard Tran Mills   PetscObjectState state;
817372ec6bbSRichard Tran Mills 
818372ec6bbSRichard Tran Mills   PetscFunctionBegin;
819372ec6bbSRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
820372ec6bbSRichard Tran Mills   b = (Mat_SeqAIJMKL*)B->spptr;
821551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
822551aa5c8SRichard Tran Mills   if (!a->sparse_optimized || a->state != state) {
823372ec6bbSRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
824372ec6bbSRichard Tran Mills   }
825551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr);
826551aa5c8SRichard Tran Mills   if (!b->sparse_optimized || b->state != state) {
827372ec6bbSRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(B);
828372ec6bbSRichard Tran Mills   }
829372ec6bbSRichard Tran Mills   csrA = a->csrA;
830372ec6bbSRichard Tran Mills   csrB = b->csrA;
831372ec6bbSRichard Tran Mills 
832372ec6bbSRichard Tran Mills   stat = mkl_sparse_spmm(SPARSE_OPERATION_TRANSPOSE,csrA,csrB,&csrC);
8339c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete sparse matrix-matrix multiply");
834372ec6bbSRichard Tran Mills 
835372ec6bbSRichard Tran Mills   ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr);
836372ec6bbSRichard Tran Mills 
837372ec6bbSRichard Tran Mills   PetscFunctionReturn(0);
838372ec6bbSRichard Tran Mills }
839372ec6bbSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
840372ec6bbSRichard Tran Mills 
841ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M)
8424f53af40SRichard Tran Mills PetscErrorCode MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat P,Mat C)
8434f53af40SRichard Tran Mills {
8444f53af40SRichard Tran Mills   Mat_SeqAIJMKL       *a, *p, *c;
8454f53af40SRichard Tran Mills   sparse_matrix_t     csrA, csrP, csrC;
8464f53af40SRichard Tran Mills   PetscBool           set, flag;
8474f53af40SRichard Tran Mills   sparse_status_t     stat = SPARSE_STATUS_SUCCESS;
848b9e1dd46SRichard Tran Mills   struct matrix_descr descr_type_sym;
8494f53af40SRichard Tran Mills   PetscObjectState    state;
8504f53af40SRichard Tran Mills   PetscErrorCode      ierr;
8514f53af40SRichard Tran Mills 
8524f53af40SRichard Tran Mills   PetscFunctionBegin;
8534f53af40SRichard Tran Mills   ierr = MatIsSymmetricKnown(A,&set,&flag);
8544f53af40SRichard Tran Mills   if (!set || (set && !flag)) {
8554f53af40SRichard Tran Mills     ierr = MatPtAPNumeric_SeqAIJ_SeqAIJ(A,P,C);CHKERRQ(ierr);
8564f53af40SRichard Tran Mills     PetscFunctionReturn(0);
8574f53af40SRichard Tran Mills   }
8584f53af40SRichard Tran Mills 
8594f53af40SRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
8604f53af40SRichard Tran Mills   p = (Mat_SeqAIJMKL*)P->spptr;
8614f53af40SRichard Tran Mills   c = (Mat_SeqAIJMKL*)C->spptr;
8624f53af40SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
8634f53af40SRichard Tran Mills   if (!a->sparse_optimized || a->state != state) {
8644f53af40SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
8654f53af40SRichard Tran Mills   }
8664f53af40SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)P,&state);CHKERRQ(ierr);
8674f53af40SRichard Tran Mills   if (!p->sparse_optimized || p->state != state) {
8684f53af40SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(P);
8694f53af40SRichard Tran Mills   }
8704f53af40SRichard Tran Mills   csrA = a->csrA;
8714f53af40SRichard Tran Mills   csrP = p->csrA;
8724f53af40SRichard Tran Mills   csrC = c->csrA;
873b9e1dd46SRichard Tran Mills   descr_type_sym.type = SPARSE_MATRIX_TYPE_SYMMETRIC;
874b9e1dd46SRichard Tran Mills   descr_type_sym.mode = SPARSE_FILL_MODE_LOWER;
875b9e1dd46SRichard Tran Mills   descr_type_sym.diag = SPARSE_DIAG_NON_UNIT;
8764f53af40SRichard Tran Mills 
877f8990b4aSRichard Tran Mills   /* Note that the call below won't work for complex matrices. (We protect this when pointers are assigned in MatConvert.) */
878b9e1dd46SRichard Tran Mills   stat = mkl_sparse_sypr(SPARSE_OPERATION_TRANSPOSE,csrP,csrA,descr_type_sym,&csrC,SPARSE_STAGE_FINALIZE_MULT);
8794f53af40SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to finalize mkl_sparse_sypr");
8804f53af40SRichard Tran Mills 
8814f53af40SRichard Tran Mills   /* Have to update the PETSc AIJ representation for matrix C from contents of MKL handle. */
8824f53af40SRichard Tran Mills   ierr = MatSeqAIJMKL_update_from_mkl_handle(C);CHKERRQ(ierr);
8834f53af40SRichard Tran Mills 
8844f53af40SRichard Tran Mills   PetscFunctionReturn(0);
8854f53af40SRichard Tran Mills }
8864f53af40SRichard Tran Mills #endif
8874f53af40SRichard Tran Mills 
888ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M)
8894f53af40SRichard Tran Mills PetscErrorCode MatPtAP_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C)
8904f53af40SRichard Tran Mills {
8914f53af40SRichard Tran Mills   Mat_SeqAIJMKL       *a, *p;
8924f53af40SRichard Tran Mills   sparse_matrix_t     csrA, csrP, csrC;
8934f53af40SRichard Tran Mills   PetscBool           set, flag;
8944f53af40SRichard Tran Mills   sparse_status_t     stat = SPARSE_STATUS_SUCCESS;
895b9e1dd46SRichard Tran Mills   struct matrix_descr descr_type_sym;
8964f53af40SRichard Tran Mills   PetscObjectState    state;
8974f53af40SRichard Tran Mills   PetscErrorCode      ierr;
8984f53af40SRichard Tran Mills 
8994f53af40SRichard Tran Mills   PetscFunctionBegin;
9004f53af40SRichard Tran Mills   ierr = MatIsSymmetricKnown(A,&set,&flag);
9014f53af40SRichard Tran Mills   if (!set || (set && !flag)) {
9024f53af40SRichard Tran Mills     ierr = MatPtAP_SeqAIJ_SeqAIJ(A,P,scall,fill,C);CHKERRQ(ierr);
9034f53af40SRichard Tran Mills     PetscFunctionReturn(0);
9044f53af40SRichard Tran Mills   }
9054f53af40SRichard Tran Mills 
9064f53af40SRichard Tran Mills   if (scall == MAT_REUSE_MATRIX) {
9074f53af40SRichard Tran Mills     ierr = MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2(A,P,*C);CHKERRQ(ierr);
9084f53af40SRichard Tran Mills     PetscFunctionReturn(0);
9094f53af40SRichard Tran Mills   }
9104f53af40SRichard Tran Mills 
9114f53af40SRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
9124f53af40SRichard Tran Mills   p = (Mat_SeqAIJMKL*)P->spptr;
9134f53af40SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
9144f53af40SRichard Tran Mills   if (!a->sparse_optimized || a->state != state) {
9154f53af40SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
9164f53af40SRichard Tran Mills   }
9174f53af40SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)P,&state);CHKERRQ(ierr);
9184f53af40SRichard Tran Mills   if (!p->sparse_optimized || p->state != state) {
9194f53af40SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(P);
9204f53af40SRichard Tran Mills   }
9214f53af40SRichard Tran Mills   csrA = a->csrA;
9224f53af40SRichard Tran Mills   csrP = p->csrA;
923b9e1dd46SRichard Tran Mills   descr_type_sym.type = SPARSE_MATRIX_TYPE_SYMMETRIC;
924b9e1dd46SRichard Tran Mills   descr_type_sym.mode = SPARSE_FILL_MODE_LOWER;
925b9e1dd46SRichard Tran Mills   descr_type_sym.diag = SPARSE_DIAG_NON_UNIT;
9264f53af40SRichard Tran Mills 
927f8990b4aSRichard Tran Mills   /* Note that the call below won't work for complex matrices. (We protect this when pointers are assigned in MatConvert.) */
928b9e1dd46SRichard Tran Mills   stat = mkl_sparse_sypr(SPARSE_OPERATION_TRANSPOSE,csrP,csrA,descr_type_sym,&csrC,SPARSE_STAGE_FULL_MULT);
9294f53af40SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete full mkl_sparse_sypr");
9304f53af40SRichard Tran Mills 
9314f53af40SRichard Tran Mills   ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr);
9324f53af40SRichard Tran Mills   ierr = MatSetOption(*C,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
9334f53af40SRichard Tran Mills 
9344f53af40SRichard Tran Mills   PetscFunctionReturn(0);
9354f53af40SRichard Tran Mills }
9364f53af40SRichard Tran Mills #endif
9374f53af40SRichard Tran Mills 
938a0dc3a43SRichard Tran Mills /* This function prototype is needed in MatConvert_SeqAIJ_SeqAIJMKL(), below. */
939a0dc3a43SRichard Tran Mills PETSC_INTERN PetscErrorCode MatPtAP_IS_XAIJ(Mat,Mat,MatReuse,PetscReal,Mat*);
940a0dc3a43SRichard Tran Mills 
9414a2a386eSRichard Tran Mills /* MatConvert_SeqAIJ_SeqAIJMKL converts a SeqAIJ matrix into a
9424a2a386eSRichard Tran Mills  * SeqAIJMKL matrix.  This routine is called by the MatCreate_SeqMKLAIJ()
9434a2a386eSRichard Tran Mills  * routine, but can also be used to convert an assembled SeqAIJ matrix
9444a2a386eSRichard Tran Mills  * into a SeqAIJMKL one. */
9454a2a386eSRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJMKL(Mat A,MatType type,MatReuse reuse,Mat *newmat)
9464a2a386eSRichard Tran Mills {
9474a2a386eSRichard Tran Mills   PetscErrorCode ierr;
9484a2a386eSRichard Tran Mills   Mat            B = *newmat;
9494a2a386eSRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl;
950c9d46305SRichard Tran Mills   PetscBool      set;
951e9c94282SRichard Tran Mills   PetscBool      sametype;
9524a2a386eSRichard Tran Mills 
9534a2a386eSRichard Tran Mills   PetscFunctionBegin;
9544a2a386eSRichard Tran Mills   if (reuse == MAT_INITIAL_MATRIX) {
9554a2a386eSRichard Tran Mills     ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr);
9564a2a386eSRichard Tran Mills   }
9574a2a386eSRichard Tran Mills 
958e9c94282SRichard Tran Mills   ierr = PetscObjectTypeCompare((PetscObject)A,type,&sametype);CHKERRQ(ierr);
959e9c94282SRichard Tran Mills   if (sametype) PetscFunctionReturn(0);
960e9c94282SRichard Tran Mills 
9614a2a386eSRichard Tran Mills   ierr     = PetscNewLog(B,&aijmkl);CHKERRQ(ierr);
9624a2a386eSRichard Tran Mills   B->spptr = (void*) aijmkl;
9634a2a386eSRichard Tran Mills 
964df555b71SRichard Tran Mills   /* Set function pointers for methods that we inherit from AIJ but override.
965969800c5SRichard Tran Mills    * We also parse some command line options below, since those determine some of the methods we point to. */
9664a2a386eSRichard Tran Mills   B->ops->duplicate        = MatDuplicate_SeqAIJMKL;
9674a2a386eSRichard Tran Mills   B->ops->assemblyend      = MatAssemblyEnd_SeqAIJMKL;
9684a2a386eSRichard Tran Mills   B->ops->destroy          = MatDestroy_SeqAIJMKL;
969c9d46305SRichard Tran Mills 
9704abfa3b3SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_FALSE;
971ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
972d995685eSRichard Tran Mills   aijmkl->no_SpMV2 = PETSC_FALSE;  /* Default to using the SpMV2 routines if our MKL supports them. */
973a8327b06SKarl Rupp #else
974d995685eSRichard Tran Mills   aijmkl->no_SpMV2 = PETSC_TRUE;
975d995685eSRichard Tran Mills #endif
9765b49642aSRichard Tran Mills   aijmkl->eager_inspection = PETSC_FALSE;
9774abfa3b3SRichard Tran Mills 
9784abfa3b3SRichard Tran Mills   /* Parse command line options. */
979c9d46305SRichard Tran Mills   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"AIJMKL Options","Mat");CHKERRQ(ierr);
980c9d46305SRichard Tran Mills   ierr = PetscOptionsBool("-mat_aijmkl_no_spmv2","NoSPMV2","None",(PetscBool)aijmkl->no_SpMV2,(PetscBool*)&aijmkl->no_SpMV2,&set);CHKERRQ(ierr);
9815b49642aSRichard Tran Mills   ierr = PetscOptionsBool("-mat_aijmkl_eager_inspection","Eager Inspection","None",(PetscBool)aijmkl->eager_inspection,(PetscBool*)&aijmkl->eager_inspection,&set);CHKERRQ(ierr);
982c9d46305SRichard Tran Mills   ierr = PetscOptionsEnd();CHKERRQ(ierr);
983ffcab697SRichard Tran Mills #if !defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
984d995685eSRichard Tran Mills   if(!aijmkl->no_SpMV2) {
985d995685eSRichard 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");
986d995685eSRichard Tran Mills     aijmkl->no_SpMV2 = PETSC_TRUE;
987d995685eSRichard Tran Mills   }
988d995685eSRichard Tran Mills #endif
989c9d46305SRichard Tran Mills 
990ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
991df555b71SRichard Tran Mills   B->ops->mult             = MatMult_SeqAIJMKL_SpMV2;
992969800c5SRichard Tran Mills   B->ops->multtranspose    = MatMultTranspose_SeqAIJMKL_SpMV2;
993df555b71SRichard Tran Mills   B->ops->multadd          = MatMultAdd_SeqAIJMKL_SpMV2;
994969800c5SRichard Tran Mills   B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL_SpMV2;
99545fbe478SRichard Tran Mills   B->ops->matmult          = MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2;
996ffcab697SRichard Tran Mills # if defined(PETSC_HAVE_MKL_SPARSE_SP2M)
997e8be1fc7SRichard Tran Mills   B->ops->matmultnumeric   = MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2;
998ffcab697SRichard Tran Mills #   if !defined(PETSC_USE_COMPLEX)
9994f53af40SRichard Tran Mills   B->ops->ptap             = MatPtAP_SeqAIJMKL_SeqAIJMKL_SpMV2;
10004f53af40SRichard Tran Mills   B->ops->ptapnumeric      = MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2;
10014f53af40SRichard Tran Mills #   endif
1002e8be1fc7SRichard Tran Mills # endif
1003a557fde5SRichard Tran Mills   B->ops->transposematmult = MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2;
10041950a7e7SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
10051950a7e7SRichard Tran Mills 
1006ffcab697SRichard Tran Mills #if !defined(PETSC_HAVE_MKL_SPARSE_SP2M)
10071950a7e7SRichard Tran Mills   /* In the same release in which MKL introduced mkl_sparse_sp2m() (version 18, update 2), the old sparse BLAS interfaces were
10081950a7e7SRichard Tran Mills    * marked as deprecated. If "no_SpMV2" has been specified by the user and MKL 18u2 or later is being used, we use the new
10091950a7e7SRichard Tran Mills    * _SpMV2 routines (set above), but do not call mkl_sparse_optimize(), which results in the old numerical kernels (without the
10101950a7e7SRichard Tran Mills    * inspector-executor model) being used. For versions in which the older interface has not been deprecated, we use the old
10111950a7e7SRichard Tran Mills    * interface. */
10121950a7e7SRichard Tran Mills   if (aijmkl->no_SpMV2) {
10134a2a386eSRichard Tran Mills     B->ops->mult             = MatMult_SeqAIJMKL;
1014969800c5SRichard Tran Mills     B->ops->multtranspose    = MatMultTranspose_SeqAIJMKL;
10154a2a386eSRichard Tran Mills     B->ops->multadd          = MatMultAdd_SeqAIJMKL;
1016969800c5SRichard Tran Mills     B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL;
1017c9d46305SRichard Tran Mills   }
10181950a7e7SRichard Tran Mills #endif
10194a2a386eSRichard Tran Mills 
10204a2a386eSRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",MatConvert_SeqAIJMKL_SeqAIJ);CHKERRQ(ierr);
1021e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaijmkl_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
1022e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaijmkl_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
1023e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaijmkl_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
1024a0dc3a43SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatPtAP_is_seqaijmkl_C",MatPtAP_IS_XAIJ);CHKERRQ(ierr);
102545fbe478SRichard Tran Mills   if(!aijmkl->no_SpMV2) {
1026ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
102745fbe478SRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqaijmkl_seqaijmkl_C",MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr);
1028ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M)
1029e8be1fc7SRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqaijmkl_seqaijmkl_C",MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr);
1030e8be1fc7SRichard Tran Mills #endif
1031372ec6bbSRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatTransposeMatMult_seqaijmkl_seqaijmkl_C",MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr);
103245fbe478SRichard Tran Mills #endif
103345fbe478SRichard Tran Mills   }
10344a2a386eSRichard Tran Mills 
10354a2a386eSRichard Tran Mills   ierr    = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJMKL);CHKERRQ(ierr);
10364a2a386eSRichard Tran Mills   *newmat = B;
10374a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
10384a2a386eSRichard Tran Mills }
10394a2a386eSRichard Tran Mills 
10404a2a386eSRichard Tran Mills /*@C
10414a2a386eSRichard Tran Mills    MatCreateSeqAIJMKL - Creates a sparse matrix of type SEQAIJMKL.
10424a2a386eSRichard Tran Mills    This type inherits from AIJ and is largely identical, but uses sparse BLAS
10434a2a386eSRichard Tran Mills    routines from Intel MKL whenever possible.
104490147e49SRichard Tran Mills    If the installed version of MKL supports the "SpMV2" sparse
104590147e49SRichard Tran Mills    inspector-executor routines, then those are used by default.
1046597ee276SRichard Tran Mills    MatMult, MatMultAdd, MatMultTranspose, MatMultTransposeAdd, MatMatMult, MatTransposeMatMult, and MatPtAP (for
1047597ee276SRichard Tran Mills    symmetric A) operations are currently supported.
1048597ee276SRichard Tran Mills    Note that MKL version 18, update 2 or later is required for MatPtAP/MatPtAPNumeric and MatMatMultNumeric.
104990147e49SRichard Tran Mills 
10504a2a386eSRichard Tran Mills    Collective on MPI_Comm
10514a2a386eSRichard Tran Mills 
10524a2a386eSRichard Tran Mills    Input Parameters:
10534a2a386eSRichard Tran Mills +  comm - MPI communicator, set to PETSC_COMM_SELF
10544a2a386eSRichard Tran Mills .  m - number of rows
10554a2a386eSRichard Tran Mills .  n - number of columns
10564a2a386eSRichard Tran Mills .  nz - number of nonzeros per row (same for all rows)
10574a2a386eSRichard Tran Mills -  nnz - array containing the number of nonzeros in the various rows
10584a2a386eSRichard Tran Mills          (possibly different for each row) or NULL
10594a2a386eSRichard Tran Mills 
10604a2a386eSRichard Tran Mills    Output Parameter:
10614a2a386eSRichard Tran Mills .  A - the matrix
10624a2a386eSRichard Tran Mills 
106390147e49SRichard Tran Mills    Options Database Keys:
106466b7eeb6SRichard Tran Mills +  -mat_aijmkl_no_spmv2 - disable use of the SpMV2 inspector-executor routines
106566b7eeb6SRichard 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
106690147e49SRichard Tran Mills 
10674a2a386eSRichard Tran Mills    Notes:
10684a2a386eSRichard Tran Mills    If nnz is given then nz is ignored
10694a2a386eSRichard Tran Mills 
10704a2a386eSRichard Tran Mills    Level: intermediate
10714a2a386eSRichard Tran Mills 
107290147e49SRichard Tran Mills .keywords: matrix, MKL, sparse, parallel
10734a2a386eSRichard Tran Mills 
10744a2a386eSRichard Tran Mills .seealso: MatCreate(), MatCreateMPIAIJMKL(), MatSetValues()
10754a2a386eSRichard Tran Mills @*/
10764a2a386eSRichard Tran Mills PetscErrorCode  MatCreateSeqAIJMKL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
10774a2a386eSRichard Tran Mills {
10784a2a386eSRichard Tran Mills   PetscErrorCode ierr;
10794a2a386eSRichard Tran Mills 
10804a2a386eSRichard Tran Mills   PetscFunctionBegin;
10814a2a386eSRichard Tran Mills   ierr = MatCreate(comm,A);CHKERRQ(ierr);
10824a2a386eSRichard Tran Mills   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
10834a2a386eSRichard Tran Mills   ierr = MatSetType(*A,MATSEQAIJMKL);CHKERRQ(ierr);
10844a2a386eSRichard Tran Mills   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
10854a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
10864a2a386eSRichard Tran Mills }
10874a2a386eSRichard Tran Mills 
10884a2a386eSRichard Tran Mills PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJMKL(Mat A)
10894a2a386eSRichard Tran Mills {
10904a2a386eSRichard Tran Mills   PetscErrorCode ierr;
10914a2a386eSRichard Tran Mills 
10924a2a386eSRichard Tran Mills   PetscFunctionBegin;
10934a2a386eSRichard Tran Mills   ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr);
10944a2a386eSRichard Tran Mills   ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr);
10954a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
10964a2a386eSRichard Tran Mills }
1097