xref: /petsc/src/mat/impls/aij/seq/aijmkl/aijmkl.c (revision 8a3692001cdf2e79412b8c9b84b2becd28037b57)
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);
61e05b4f34SRichard 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);
65*8a369200SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE)
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;
147e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED)
148e626a176SRichard Tran Mills   /* For MKL versions that still support the old, non-inspector-executor interfaces versions, we simply exit here if the no_SpMV2
149e626a176SRichard Tran Mills    * option has been specified. For versions that have deprecated the old interfaces (version 18, update 2 and later), we must
150e626a176SRichard Tran Mills    * use the new inspector-executor interfaces, but we can still use the old, non-inspector-executor code by not calling
151e626a176SRichard Tran Mills    * mkl_sparse_optimize() later. */
1526e369cd5SRichard Tran Mills   if (aijmkl->no_SpMV2) PetscFunctionReturn(0);
1534d51fa23SRichard Tran Mills #endif
1546e369cd5SRichard Tran Mills 
1550632b357SRichard Tran Mills   if (aijmkl->sparse_optimized) {
1560632b357SRichard Tran Mills     /* Matrix has been previously assembled and optimized. Must destroy old
1570632b357SRichard Tran Mills      * matrix handle before running the optimization step again. */
1580632b357SRichard Tran Mills     stat = mkl_sparse_destroy(aijmkl->csrA);
1599c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy");
1600632b357SRichard Tran Mills   }
1618d3fe1b0SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_FALSE;
1626e369cd5SRichard Tran Mills 
163c9d46305SRichard Tran Mills   /* Now perform the SpMV2 setup and matrix optimization. */
164df555b71SRichard Tran Mills   aijmkl->descr.type        = SPARSE_MATRIX_TYPE_GENERAL;
165df555b71SRichard Tran Mills   aijmkl->descr.mode        = SPARSE_FILL_MODE_LOWER;
166df555b71SRichard Tran Mills   aijmkl->descr.diag        = SPARSE_DIAG_NON_UNIT;
16758678438SRichard Tran Mills   m = A->rmap->n;
16858678438SRichard Tran Mills   n = A->cmap->n;
169df555b71SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
170df555b71SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
171df555b71SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
17280095d54SIrina Sokolova   if ((a->nz!=0) & !(A->structure_only)) {
1738d3fe1b0SRichard Tran Mills     /* Create a new, optimized sparse matrix handle only if the matrix has nonzero entries.
1748d3fe1b0SRichard Tran Mills      * The MKL sparse-inspector executor routines don't like being passed an empty matrix. */
17558678438SRichard Tran Mills     stat = mkl_sparse_x_create_csr(&aijmkl->csrA,SPARSE_INDEX_BASE_ZERO,m,n,ai,ai+1,aj,aa);
176e8be1fc7SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to create matrix handle");
177df555b71SRichard Tran Mills     stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000);
178e8be1fc7SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set mv_hint");
179df555b71SRichard Tran Mills     stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE);
180e8be1fc7SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set memory_hint");
1811950a7e7SRichard Tran Mills     if (!aijmkl->no_SpMV2) {
182df555b71SRichard Tran Mills       stat = mkl_sparse_optimize(aijmkl->csrA);
183e8be1fc7SRichard Tran Mills       if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_optimize");
1841950a7e7SRichard Tran Mills     }
1854abfa3b3SRichard Tran Mills     aijmkl->sparse_optimized = PETSC_TRUE;
186e995cf24SRichard Tran Mills     ierr = PetscObjectStateGet((PetscObject)A,&(aijmkl->state));CHKERRQ(ierr);
187c9d46305SRichard Tran Mills   }
1886e369cd5SRichard Tran Mills 
1896e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
190d995685eSRichard Tran Mills #endif
1916e369cd5SRichard Tran Mills }
1926e369cd5SRichard Tran Mills 
19319afcda9SRichard Tran Mills /* MatSeqAIJMKL_create_from_mkl_handle() creates a sequential AIJMKL matrix from an MKL sparse matrix handle.
19419afcda9SRichard Tran Mills  * We need this to implement MatMatMult() using the MKL inspector-executor routines, which return an (unoptimized)
1956c87cf42SRichard Tran Mills  * matrix handle.
196aab60f1bSRichard Tran Mills  * Note: This routine simply destroys and replaces the original matrix if MAT_REUSE_MATRIX has been specified, as
197aab60f1bSRichard Tran Mills  * there is no good alternative. */
198ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
1996c87cf42SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_from_mkl_handle(MPI_Comm comm,sparse_matrix_t csrA,MatReuse reuse,Mat *mat)
20019afcda9SRichard Tran Mills {
20119afcda9SRichard Tran Mills   PetscErrorCode      ierr;
20219afcda9SRichard Tran Mills   sparse_status_t     stat;
20319afcda9SRichard Tran Mills   sparse_index_base_t indexing;
20419afcda9SRichard Tran Mills   PetscInt            nrows, ncols;
20545fbe478SRichard Tran Mills   PetscInt            *aj,*ai,*dummy;
20619afcda9SRichard Tran Mills   MatScalar           *aa;
20719afcda9SRichard Tran Mills   Mat                 A;
20819afcda9SRichard Tran Mills   Mat_SeqAIJMKL       *aijmkl;
20919afcda9SRichard Tran Mills 
21045fbe478SRichard Tran Mills   /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */
21145fbe478SRichard Tran Mills   stat = mkl_sparse_x_export_csr(csrA,&indexing,&nrows,&ncols,&ai,&dummy,&aj,&aa);
2129c46acdfSRichard 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()");
2136c87cf42SRichard Tran Mills 
214aab60f1bSRichard Tran Mills   if (reuse == MAT_REUSE_MATRIX) {
215aab60f1bSRichard Tran Mills     ierr = MatDestroy(mat);CHKERRQ(ierr);
216aab60f1bSRichard Tran Mills   }
21719afcda9SRichard Tran Mills   ierr = MatCreate(comm,&A);CHKERRQ(ierr);
21819afcda9SRichard Tran Mills   ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr);
21945fbe478SRichard Tran Mills   ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,nrows,ncols);CHKERRQ(ierr);
220aab60f1bSRichard Tran Mills   /* We use MatSeqAIJSetPreallocationCSR() instead of MatCreateSeqAIJWithArrays() because we must copy the arrays exported
221aab60f1bSRichard Tran Mills    * from MKL; MKL developers tell us that modifying the arrays may cause unexpected results when using the MKL handle, and
222aab60f1bSRichard Tran Mills    * they will be destroyed when the MKL handle is destroyed.
223aab60f1bSRichard Tran Mills    * (In the interest of reducing memory consumption in future, can we figure out good ways to deal with this?) */
22419afcda9SRichard Tran Mills   ierr = MatSeqAIJSetPreallocationCSR(A,ai,aj,aa);CHKERRQ(ierr);
22519afcda9SRichard Tran Mills 
22619afcda9SRichard Tran Mills   /* We now have an assembled sequential AIJ matrix created from copies of the exported arrays from the MKL matrix handle.
22719afcda9SRichard Tran Mills    * Now turn it into a MATSEQAIJMKL. */
22819afcda9SRichard Tran Mills   ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr);
2296c87cf42SRichard Tran Mills 
23019afcda9SRichard Tran Mills   aijmkl = (Mat_SeqAIJMKL*) A->spptr;
23119afcda9SRichard Tran Mills   aijmkl->csrA = csrA;
2326c87cf42SRichard Tran Mills 
23319afcda9SRichard Tran Mills   /* The below code duplicates much of what is in MatSeqAIJKL_create_mkl_handle(). I dislike this code duplication, but
23419afcda9SRichard Tran Mills    * MatSeqAIJMKL_create_mkl_handle() cannot be used because we don't need to create a handle -- we've already got one,
23519afcda9SRichard Tran Mills    * and just need to be able to run the MKL optimization step. */
236f3fd1758SRichard Tran Mills   aijmkl->descr.type        = SPARSE_MATRIX_TYPE_GENERAL;
237f3fd1758SRichard Tran Mills   aijmkl->descr.mode        = SPARSE_FILL_MODE_LOWER;
238f3fd1758SRichard Tran Mills   aijmkl->descr.diag        = SPARSE_DIAG_NON_UNIT;
23919afcda9SRichard Tran Mills   stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000);
24051539a68SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set mv_hint");
24119afcda9SRichard Tran Mills   stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE);
24251539a68SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set memory_hint");
2431950a7e7SRichard Tran Mills   if (!aijmkl->no_SpMV2) {
24419afcda9SRichard Tran Mills     stat = mkl_sparse_optimize(aijmkl->csrA);
24551539a68SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_optimize");
2461950a7e7SRichard Tran Mills   }
24719afcda9SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_TRUE;
248e995cf24SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&(aijmkl->state));CHKERRQ(ierr);
24919afcda9SRichard Tran Mills 
25019afcda9SRichard Tran Mills   *mat = A;
25119afcda9SRichard Tran Mills   PetscFunctionReturn(0);
25219afcda9SRichard Tran Mills }
25319afcda9SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
25419afcda9SRichard Tran Mills 
255e8be1fc7SRichard Tran Mills /* MatSeqAIJMKL_update_from_mkl_handle() updates the matrix values array from the contents of the associated MKL sparse matrix handle.
256e8be1fc7SRichard 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
257e8be1fc7SRichard Tran Mills  * MatMatMultNumeric(). */
258ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
259e8be1fc7SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_update_from_mkl_handle(Mat A)
260e8be1fc7SRichard Tran Mills {
261e8be1fc7SRichard Tran Mills   PetscInt            i;
262e8be1fc7SRichard Tran Mills   PetscInt            nrows,ncols;
263e8be1fc7SRichard Tran Mills   PetscInt            nz;
264e8be1fc7SRichard Tran Mills   PetscInt            *ai,*aj,*dummy;
265e8be1fc7SRichard Tran Mills   PetscScalar         *aa;
266e8be1fc7SRichard Tran Mills   PetscErrorCode      ierr;
267e8be1fc7SRichard Tran Mills   Mat_SeqAIJMKL       *aijmkl;
268e8be1fc7SRichard Tran Mills   sparse_status_t     stat;
269e8be1fc7SRichard Tran Mills   sparse_index_base_t indexing;
270e8be1fc7SRichard Tran Mills 
271e8be1fc7SRichard Tran Mills   aijmkl = (Mat_SeqAIJMKL*) A->spptr;
272e8be1fc7SRichard Tran Mills 
273e8be1fc7SRichard Tran Mills   /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */
274e8be1fc7SRichard Tran Mills   stat = mkl_sparse_x_export_csr(aijmkl->csrA,&indexing,&nrows,&ncols,&ai,&dummy,&aj,&aa);
275e8be1fc7SRichard 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()");
276e8be1fc7SRichard Tran Mills 
277e8be1fc7SRichard 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
278e8be1fc7SRichard Tran Mills    * representations differ in small ways (e.g., more explicit nonzeros per row due to preallocation). */
279e8be1fc7SRichard Tran Mills   for (i=0; i<nrows; i++) {
280e8be1fc7SRichard Tran Mills     nz = ai[i+1] - ai[i];
281e8be1fc7SRichard Tran Mills     ierr = MatSetValues_SeqAIJ(A, 1, &i, nz, aj+ai[i], aa+ai[i], INSERT_VALUES);CHKERRQ(ierr);
282e8be1fc7SRichard Tran Mills   }
283e8be1fc7SRichard Tran Mills 
284e8be1fc7SRichard Tran Mills   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
285e8be1fc7SRichard Tran Mills   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
286e8be1fc7SRichard Tran Mills 
287e995cf24SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&(aijmkl->state));CHKERRQ(ierr);
288e995cf24SRichard Tran Mills   /* We mark our matrix as having a valid, optimized MKL handle.
289e995cf24SRichard Tran Mills    * TODO: It is valid, but I am not sure if it is optimized. Need to ask MKL developers. */
290e995cf24SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_TRUE;
291e995cf24SRichard Tran Mills 
292e8be1fc7SRichard Tran Mills   PetscFunctionReturn(0);
293e8be1fc7SRichard Tran Mills }
294e8be1fc7SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
295e8be1fc7SRichard Tran Mills 
2966e369cd5SRichard Tran Mills PetscErrorCode MatDuplicate_SeqAIJMKL(Mat A, MatDuplicateOption op, Mat *M)
2976e369cd5SRichard Tran Mills {
2986e369cd5SRichard Tran Mills   PetscErrorCode ierr;
2996e369cd5SRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl;
3006e369cd5SRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl_dest;
3016e369cd5SRichard Tran Mills 
3026e369cd5SRichard Tran Mills   PetscFunctionBegin;
3036e369cd5SRichard Tran Mills   ierr = MatDuplicate_SeqAIJ(A,op,M);CHKERRQ(ierr);
3046e369cd5SRichard Tran Mills   aijmkl      = (Mat_SeqAIJMKL*) A->spptr;
3056e369cd5SRichard Tran Mills   aijmkl_dest = (Mat_SeqAIJMKL*) (*M)->spptr;
3066e369cd5SRichard Tran Mills   ierr = PetscMemcpy(aijmkl_dest,aijmkl,sizeof(Mat_SeqAIJMKL));CHKERRQ(ierr);
3076e369cd5SRichard Tran Mills   aijmkl_dest->sparse_optimized = PETSC_FALSE;
3085b49642aSRichard Tran Mills   if (aijmkl->eager_inspection) {
3096e369cd5SRichard Tran Mills     ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr);
3105b49642aSRichard Tran Mills   }
3116e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
3126e369cd5SRichard Tran Mills }
3136e369cd5SRichard Tran Mills 
3146e369cd5SRichard Tran Mills PetscErrorCode MatAssemblyEnd_SeqAIJMKL(Mat A, MatAssemblyType mode)
3156e369cd5SRichard Tran Mills {
3166e369cd5SRichard Tran Mills   PetscErrorCode  ierr;
3176e369cd5SRichard Tran Mills   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3185b49642aSRichard Tran Mills   Mat_SeqAIJMKL   *aijmkl;
3196e369cd5SRichard Tran Mills 
3206e369cd5SRichard Tran Mills   PetscFunctionBegin;
3216e369cd5SRichard Tran Mills   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
3226e369cd5SRichard Tran Mills 
3236e369cd5SRichard Tran Mills   /* Since a MATSEQAIJMKL matrix is really just a MATSEQAIJ with some
3246e369cd5SRichard Tran Mills    * extra information and some different methods, call the AssemblyEnd
3256e369cd5SRichard Tran Mills    * routine for a MATSEQAIJ.
3266e369cd5SRichard Tran Mills    * I'm not sure if this is the best way to do this, but it avoids
327d96e85feSRichard Tran Mills    * a lot of code duplication. */
3286e369cd5SRichard Tran Mills   a->inode.use = PETSC_FALSE;  /* Must disable: otherwise the MKL routines won't get used. */
3296e369cd5SRichard Tran Mills   ierr = MatAssemblyEnd_SeqAIJ(A, mode);CHKERRQ(ierr);
3306e369cd5SRichard Tran Mills 
3315b49642aSRichard Tran Mills   /* If the user has requested "eager" inspection, create the optimized MKL sparse handle (if needed; the function checks).
3325b49642aSRichard Tran Mills    * (The default is to do "lazy" inspection, deferring this until something like MatMult() is called.) */
3335b49642aSRichard Tran Mills   aijmkl = (Mat_SeqAIJMKL*) A->spptr;
3345b49642aSRichard Tran Mills   if (aijmkl->eager_inspection) {
3356e369cd5SRichard Tran Mills     ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr);
3365b49642aSRichard Tran Mills   }
337df555b71SRichard Tran Mills 
3384a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
3394a2a386eSRichard Tran Mills }
3404a2a386eSRichard Tran Mills 
341e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED)
3424a2a386eSRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL(Mat A,Vec xx,Vec yy)
3434a2a386eSRichard Tran Mills {
3444a2a386eSRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
3454a2a386eSRichard Tran Mills   const PetscScalar *x;
3464a2a386eSRichard Tran Mills   PetscScalar       *y;
3474a2a386eSRichard Tran Mills   const MatScalar   *aa;
3484a2a386eSRichard Tran Mills   PetscErrorCode    ierr;
3494a2a386eSRichard Tran Mills   PetscInt          m=A->rmap->n;
350db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
351db63039fSRichard Tran Mills   PetscScalar       alpha = 1.0;
352db63039fSRichard Tran Mills   PetscScalar       beta = 0.0;
3534a2a386eSRichard Tran Mills   const PetscInt    *aj,*ai;
354db63039fSRichard Tran Mills   char              matdescra[6];
355db63039fSRichard Tran Mills 
3564a2a386eSRichard Tran Mills 
3574a2a386eSRichard Tran Mills   /* Variables not in MatMult_SeqAIJ. */
358ff03dc53SRichard Tran Mills   char transa = 'n';  /* Used to indicate to MKL that we are not computing the transpose product. */
359ff03dc53SRichard Tran Mills 
360ff03dc53SRichard Tran Mills   PetscFunctionBegin;
361db63039fSRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
362db63039fSRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
363ff03dc53SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
364ff03dc53SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
365ff03dc53SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
366ff03dc53SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
367ff03dc53SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
368ff03dc53SRichard Tran Mills 
369ff03dc53SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
370db63039fSRichard Tran Mills   mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y);
371ff03dc53SRichard Tran Mills 
372ff03dc53SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
373ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
374ff03dc53SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
375ff03dc53SRichard Tran Mills   PetscFunctionReturn(0);
376ff03dc53SRichard Tran Mills }
3771950a7e7SRichard Tran Mills #endif
378ff03dc53SRichard Tran Mills 
379ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
380df555b71SRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy)
381df555b71SRichard Tran Mills {
382df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
383df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
384df555b71SRichard Tran Mills   const PetscScalar *x;
385df555b71SRichard Tran Mills   PetscScalar       *y;
386df555b71SRichard Tran Mills   PetscErrorCode    ierr;
387df555b71SRichard Tran Mills   sparse_status_t   stat = SPARSE_STATUS_SUCCESS;
388551aa5c8SRichard Tran Mills   PetscObjectState  state;
389df555b71SRichard Tran Mills 
390df555b71SRichard Tran Mills   PetscFunctionBegin;
391df555b71SRichard Tran Mills 
39238987b35SRichard Tran Mills   /* If there are no nonzero entries, zero yy and return immediately. */
39338987b35SRichard Tran Mills   if(!a->nz) {
39438987b35SRichard Tran Mills     PetscInt i;
39538987b35SRichard Tran Mills     PetscInt m=A->rmap->n;
39638987b35SRichard Tran Mills     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
39738987b35SRichard Tran Mills     for (i=0; i<m; i++) {
39838987b35SRichard Tran Mills       y[i] = 0.0;
39938987b35SRichard Tran Mills     }
40038987b35SRichard Tran Mills     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
40138987b35SRichard Tran Mills     PetscFunctionReturn(0);
40238987b35SRichard Tran Mills   }
403f36dfe3fSRichard Tran Mills 
404df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
405df555b71SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
406df555b71SRichard Tran Mills 
4073fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
4083fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
4093fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
410551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
411551aa5c8SRichard Tran Mills   if (!aijmkl->sparse_optimized || aijmkl->state != state) {
4123fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
4133fa15762SRichard Tran Mills   }
4143fa15762SRichard Tran Mills 
415df555b71SRichard Tran Mills   /* Call MKL SpMV2 executor routine to do the MatMult. */
416df555b71SRichard Tran Mills   stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y);
4179c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
418df555b71SRichard Tran Mills 
419df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
420df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
421df555b71SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
422df555b71SRichard Tran Mills   PetscFunctionReturn(0);
423df555b71SRichard Tran Mills }
424d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
425df555b71SRichard Tran Mills 
426e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED)
427ff03dc53SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL(Mat A,Vec xx,Vec yy)
428ff03dc53SRichard Tran Mills {
429ff03dc53SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
430ff03dc53SRichard Tran Mills   const PetscScalar *x;
431ff03dc53SRichard Tran Mills   PetscScalar       *y;
432ff03dc53SRichard Tran Mills   const MatScalar   *aa;
433ff03dc53SRichard Tran Mills   PetscErrorCode    ierr;
434ff03dc53SRichard Tran Mills   PetscInt          m=A->rmap->n;
435db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
436db63039fSRichard Tran Mills   PetscScalar       alpha = 1.0;
437db63039fSRichard Tran Mills   PetscScalar       beta = 0.0;
438ff03dc53SRichard Tran Mills   const PetscInt    *aj,*ai;
439db63039fSRichard Tran Mills   char              matdescra[6];
440ff03dc53SRichard Tran Mills 
441ff03dc53SRichard Tran Mills   /* Variables not in MatMultTranspose_SeqAIJ. */
442ff03dc53SRichard Tran Mills   char transa = 't';  /* Used to indicate to MKL that we are computing the transpose product. */
4434a2a386eSRichard Tran Mills 
4444a2a386eSRichard Tran Mills   PetscFunctionBegin;
445969800c5SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
446969800c5SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
4474a2a386eSRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
4484a2a386eSRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
4494a2a386eSRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
4504a2a386eSRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
4514a2a386eSRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
4524a2a386eSRichard Tran Mills 
4534a2a386eSRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
454db63039fSRichard Tran Mills   mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y);
4554a2a386eSRichard Tran Mills 
4564a2a386eSRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
4574a2a386eSRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
4584a2a386eSRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
4594a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
4604a2a386eSRichard Tran Mills }
4611950a7e7SRichard Tran Mills #endif
4624a2a386eSRichard Tran Mills 
463ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
464df555b71SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy)
465df555b71SRichard Tran Mills {
466df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
467df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
468df555b71SRichard Tran Mills   const PetscScalar *x;
469df555b71SRichard Tran Mills   PetscScalar       *y;
470df555b71SRichard Tran Mills   PetscErrorCode    ierr;
4710632b357SRichard Tran Mills   sparse_status_t   stat;
472551aa5c8SRichard Tran Mills   PetscObjectState  state;
473df555b71SRichard Tran Mills 
474df555b71SRichard Tran Mills   PetscFunctionBegin;
475df555b71SRichard Tran Mills 
47638987b35SRichard Tran Mills   /* If there are no nonzero entries, zero yy and return immediately. */
47738987b35SRichard Tran Mills   if(!a->nz) {
47838987b35SRichard Tran Mills     PetscInt i;
47938987b35SRichard Tran Mills     PetscInt n=A->cmap->n;
48038987b35SRichard Tran Mills     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
48138987b35SRichard Tran Mills     for (i=0; i<n; i++) {
48238987b35SRichard Tran Mills       y[i] = 0.0;
48338987b35SRichard Tran Mills     }
48438987b35SRichard Tran Mills     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
48538987b35SRichard Tran Mills     PetscFunctionReturn(0);
48638987b35SRichard Tran Mills   }
487f36dfe3fSRichard Tran Mills 
488df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
489df555b71SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
490df555b71SRichard Tran Mills 
4913fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
4923fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
4933fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
494551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
495551aa5c8SRichard Tran Mills   if (!aijmkl->sparse_optimized || aijmkl->state != state) {
4963fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
4973fa15762SRichard Tran Mills   }
4983fa15762SRichard Tran Mills 
499df555b71SRichard Tran Mills   /* Call MKL SpMV2 executor routine to do the MatMultTranspose. */
500df555b71SRichard Tran Mills   stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y);
5019c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
502df555b71SRichard Tran Mills 
503df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
504df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
505df555b71SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
506df555b71SRichard Tran Mills   PetscFunctionReturn(0);
507df555b71SRichard Tran Mills }
508d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
509df555b71SRichard Tran Mills 
510e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED)
5114a2a386eSRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz)
5124a2a386eSRichard Tran Mills {
5134a2a386eSRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
5144a2a386eSRichard Tran Mills   const PetscScalar *x;
5154a2a386eSRichard Tran Mills   PetscScalar       *y,*z;
5164a2a386eSRichard Tran Mills   const MatScalar   *aa;
5174a2a386eSRichard Tran Mills   PetscErrorCode    ierr;
5184a2a386eSRichard Tran Mills   PetscInt          m=A->rmap->n;
519db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
5204a2a386eSRichard Tran Mills   const PetscInt    *aj,*ai;
5214a2a386eSRichard Tran Mills   PetscInt          i;
5224a2a386eSRichard Tran Mills 
523ff03dc53SRichard Tran Mills   /* Variables not in MatMultAdd_SeqAIJ. */
524ff03dc53SRichard Tran Mills   char              transa = 'n';  /* Used to indicate to MKL that we are not computing the transpose product. */
525a84739b8SRichard Tran Mills   PetscScalar       alpha = 1.0;
526db63039fSRichard Tran Mills   PetscScalar       beta;
527a84739b8SRichard Tran Mills   char              matdescra[6];
528ff03dc53SRichard Tran Mills 
529ff03dc53SRichard Tran Mills   PetscFunctionBegin;
530a84739b8SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
531a84739b8SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
532a84739b8SRichard Tran Mills 
533ff03dc53SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
534ff03dc53SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
535ff03dc53SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
536ff03dc53SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
537ff03dc53SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
538ff03dc53SRichard Tran Mills 
539ff03dc53SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
540a84739b8SRichard Tran Mills   if (zz == yy) {
541a84739b8SRichard 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. */
542db63039fSRichard Tran Mills     beta = 1.0;
543db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
544a84739b8SRichard Tran Mills   } else {
545db63039fSRichard Tran Mills     /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z.
546db63039fSRichard Tran Mills      * MKL sparse BLAS does not have a MatMultAdd equivalent. */
547db63039fSRichard Tran Mills     beta = 0.0;
548db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
549ff03dc53SRichard Tran Mills     for (i=0; i<m; i++) {
550ff03dc53SRichard Tran Mills       z[i] += y[i];
551ff03dc53SRichard Tran Mills     }
552a84739b8SRichard Tran Mills   }
553ff03dc53SRichard Tran Mills 
554ff03dc53SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
555ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
556ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
557ff03dc53SRichard Tran Mills   PetscFunctionReturn(0);
558ff03dc53SRichard Tran Mills }
5591950a7e7SRichard Tran Mills #endif
560ff03dc53SRichard Tran Mills 
561ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
562df555b71SRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz)
563df555b71SRichard Tran Mills {
564df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
565df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
566df555b71SRichard Tran Mills   const PetscScalar *x;
567df555b71SRichard Tran Mills   PetscScalar       *y,*z;
568df555b71SRichard Tran Mills   PetscErrorCode    ierr;
569df555b71SRichard Tran Mills   PetscInt          m=A->rmap->n;
570df555b71SRichard Tran Mills   PetscInt          i;
571df555b71SRichard Tran Mills 
572df555b71SRichard Tran Mills   /* Variables not in MatMultAdd_SeqAIJ. */
573df555b71SRichard Tran Mills   sparse_status_t   stat = SPARSE_STATUS_SUCCESS;
574551aa5c8SRichard Tran Mills   PetscObjectState  state;
575df555b71SRichard Tran Mills 
576df555b71SRichard Tran Mills   PetscFunctionBegin;
577df555b71SRichard Tran Mills 
57838987b35SRichard Tran Mills   /* If there are no nonzero entries, set zz = yy and return immediately. */
57938987b35SRichard Tran Mills   if(!a->nz) {
58038987b35SRichard Tran Mills     PetscInt i;
58138987b35SRichard Tran Mills     ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
58238987b35SRichard Tran Mills     for (i=0; i<m; i++) {
58338987b35SRichard Tran Mills       z[i] = y[i];
58438987b35SRichard Tran Mills     }
58538987b35SRichard Tran Mills     ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
58638987b35SRichard Tran Mills     PetscFunctionReturn(0);
58738987b35SRichard Tran Mills   }
588df555b71SRichard Tran Mills 
589df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
590df555b71SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
591df555b71SRichard Tran Mills 
5923fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
5933fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
5943fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
595551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
596551aa5c8SRichard Tran Mills   if (!aijmkl->sparse_optimized || aijmkl->state != state) {
5973fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
5983fa15762SRichard Tran Mills   }
5993fa15762SRichard Tran Mills 
600df555b71SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
601df555b71SRichard Tran Mills   if (zz == yy) {
602df555b71SRichard 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,
603df555b71SRichard Tran Mills      * with alpha and beta both set to 1.0. */
604db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z);
6059c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
606df555b71SRichard Tran Mills   } else {
607df555b71SRichard 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
608df555b71SRichard Tran Mills      * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */
609db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z);
6109c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
611df555b71SRichard Tran Mills     for (i=0; i<m; i++) {
612df555b71SRichard Tran Mills       z[i] += y[i];
613df555b71SRichard Tran Mills     }
614df555b71SRichard Tran Mills   }
615df555b71SRichard Tran Mills 
616df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
617df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
618df555b71SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
619df555b71SRichard Tran Mills   PetscFunctionReturn(0);
620df555b71SRichard Tran Mills }
621d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
622df555b71SRichard Tran Mills 
623e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED)
624ff03dc53SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz)
625ff03dc53SRichard Tran Mills {
626ff03dc53SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
627ff03dc53SRichard Tran Mills   const PetscScalar *x;
628ff03dc53SRichard Tran Mills   PetscScalar       *y,*z;
629ff03dc53SRichard Tran Mills   const MatScalar   *aa;
630ff03dc53SRichard Tran Mills   PetscErrorCode    ierr;
631ff03dc53SRichard Tran Mills   PetscInt          m=A->rmap->n;
632db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
633ff03dc53SRichard Tran Mills   const PetscInt    *aj,*ai;
634ff03dc53SRichard Tran Mills   PetscInt          i;
635ff03dc53SRichard Tran Mills 
636ff03dc53SRichard Tran Mills   /* Variables not in MatMultTransposeAdd_SeqAIJ. */
637ff03dc53SRichard Tran Mills   char transa = 't';  /* Used to indicate to MKL that we are computing the transpose product. */
638a84739b8SRichard Tran Mills   PetscScalar       alpha = 1.0;
639db63039fSRichard Tran Mills   PetscScalar       beta;
640a84739b8SRichard Tran Mills   char              matdescra[6];
6414a2a386eSRichard Tran Mills 
6424a2a386eSRichard Tran Mills   PetscFunctionBegin;
643a84739b8SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
644a84739b8SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
645a84739b8SRichard Tran Mills 
6464a2a386eSRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
6474a2a386eSRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
6484a2a386eSRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
6494a2a386eSRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
6504a2a386eSRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
6514a2a386eSRichard Tran Mills 
6524a2a386eSRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
653a84739b8SRichard Tran Mills   if (zz == yy) {
654a84739b8SRichard 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. */
655db63039fSRichard Tran Mills     beta = 1.0;
656969800c5SRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
657a84739b8SRichard Tran Mills   } else {
658db63039fSRichard Tran Mills     /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z.
659db63039fSRichard Tran Mills      * MKL sparse BLAS does not have a MatMultAdd equivalent. */
660db63039fSRichard Tran Mills     beta = 0.0;
661db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
662969800c5SRichard Tran Mills     for (i=0; i<n; i++) {
6634a2a386eSRichard Tran Mills       z[i] += y[i];
6644a2a386eSRichard Tran Mills     }
665a84739b8SRichard Tran Mills   }
6664a2a386eSRichard Tran Mills 
6674a2a386eSRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
6684a2a386eSRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
6694a2a386eSRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
6704a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
6714a2a386eSRichard Tran Mills }
6721950a7e7SRichard Tran Mills #endif
6734a2a386eSRichard Tran Mills 
674ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
675df555b71SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz)
676df555b71SRichard Tran Mills {
677df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
678df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
679df555b71SRichard Tran Mills   const PetscScalar *x;
680df555b71SRichard Tran Mills   PetscScalar       *y,*z;
681df555b71SRichard Tran Mills   PetscErrorCode    ierr;
682969800c5SRichard Tran Mills   PetscInt          n=A->cmap->n;
683df555b71SRichard Tran Mills   PetscInt          i;
684551aa5c8SRichard Tran Mills   PetscObjectState  state;
685df555b71SRichard Tran Mills 
686df555b71SRichard Tran Mills   /* Variables not in MatMultTransposeAdd_SeqAIJ. */
687df555b71SRichard Tran Mills   sparse_status_t stat = SPARSE_STATUS_SUCCESS;
688df555b71SRichard Tran Mills 
689df555b71SRichard Tran Mills   PetscFunctionBegin;
690df555b71SRichard Tran Mills 
69138987b35SRichard Tran Mills   /* If there are no nonzero entries, set zz = yy and return immediately. */
69238987b35SRichard Tran Mills   if(!a->nz) {
69338987b35SRichard Tran Mills     PetscInt i;
69438987b35SRichard Tran Mills     ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
69538987b35SRichard Tran Mills     for (i=0; i<n; i++) {
69638987b35SRichard Tran Mills       z[i] = y[i];
69738987b35SRichard Tran Mills     }
69838987b35SRichard Tran Mills     ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
69938987b35SRichard Tran Mills     PetscFunctionReturn(0);
70038987b35SRichard Tran Mills   }
701f36dfe3fSRichard Tran Mills 
702df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
703df555b71SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
704df555b71SRichard Tran Mills 
7053fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
7063fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
7073fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
708551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
709551aa5c8SRichard Tran Mills   if (!aijmkl->sparse_optimized || aijmkl->state != state) {
7103fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
7113fa15762SRichard Tran Mills   }
7123fa15762SRichard Tran Mills 
713df555b71SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
714df555b71SRichard Tran Mills   if (zz == yy) {
715df555b71SRichard 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,
716df555b71SRichard Tran Mills      * with alpha and beta both set to 1.0. */
717db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z);
7189c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
719df555b71SRichard Tran Mills   } else {
720df555b71SRichard 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
721df555b71SRichard Tran Mills      * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */
722db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z);
7239c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
724969800c5SRichard Tran Mills     for (i=0; i<n; i++) {
725df555b71SRichard Tran Mills       z[i] += y[i];
726df555b71SRichard Tran Mills     }
727df555b71SRichard Tran Mills   }
728df555b71SRichard Tran Mills 
729df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
730df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
731df555b71SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
732df555b71SRichard Tran Mills   PetscFunctionReturn(0);
733df555b71SRichard Tran Mills }
734d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
735df555b71SRichard Tran Mills 
736ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
737aab60f1bSRichard Tran Mills /* Note that this code currently doesn't actually get used when MatMatMult() is called with MAT_REUSE_MATRIX, because
738aab60f1bSRichard Tran Mills  * the MatMatMult() interface code calls MatMatMultNumeric() in this case.
7393ecbffd0SRichard Tran Mills  * For releases of MKL prior to version 18, update 2:
740aab60f1bSRichard Tran Mills  * MKL has no notion of separately callable symbolic vs. numeric phases of sparse matrix-matrix multiply, so in the
741aab60f1bSRichard Tran Mills  * MAT_REUSE_MATRIX case, the SeqAIJ routines end up being used. Even though this means that the (hopefully more
742aab60f1bSRichard Tran Mills  * optimized) MKL routines do not get used, this probably is best because the MKL routines would waste time re-computing
743aab60f1bSRichard Tran Mills  * the symbolic portion, whereas the native PETSc SeqAIJ routines will avoid this. */
74445fbe478SRichard Tran Mills PetscErrorCode MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat*C)
74545fbe478SRichard Tran Mills {
74645fbe478SRichard Tran Mills   Mat_SeqAIJMKL    *a, *b;
74745fbe478SRichard Tran Mills   sparse_matrix_t  csrA, csrB, csrC;
74845fbe478SRichard Tran Mills   PetscErrorCode   ierr;
74945fbe478SRichard Tran Mills   sparse_status_t  stat = SPARSE_STATUS_SUCCESS;
750551aa5c8SRichard Tran Mills   PetscObjectState state;
75145fbe478SRichard Tran Mills 
75245fbe478SRichard Tran Mills   PetscFunctionBegin;
75345fbe478SRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
75445fbe478SRichard Tran Mills   b = (Mat_SeqAIJMKL*)B->spptr;
755551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
756551aa5c8SRichard Tran Mills   if (!a->sparse_optimized || a->state != state) {
75745fbe478SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
75845fbe478SRichard Tran Mills   }
759551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr);
760551aa5c8SRichard Tran Mills   if (!b->sparse_optimized || b->state != state) {
76145fbe478SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(B);
76245fbe478SRichard Tran Mills   }
76345fbe478SRichard Tran Mills   csrA = a->csrA;
76445fbe478SRichard Tran Mills   csrB = b->csrA;
76545fbe478SRichard Tran Mills 
76645fbe478SRichard Tran Mills   stat = mkl_sparse_spmm(SPARSE_OPERATION_NON_TRANSPOSE,csrA,csrB,&csrC);
7679c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete sparse matrix-matrix multiply");
76845fbe478SRichard Tran Mills 
7696c87cf42SRichard Tran Mills   ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr);
77045fbe478SRichard Tran Mills 
77145fbe478SRichard Tran Mills   PetscFunctionReturn(0);
77245fbe478SRichard Tran Mills }
77345fbe478SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
77445fbe478SRichard Tran Mills 
775*8a369200SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE)
776e8be1fc7SRichard Tran Mills PetscErrorCode MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,Mat C)
777e8be1fc7SRichard Tran Mills {
778e8be1fc7SRichard Tran Mills   Mat_SeqAIJMKL       *a, *b, *c;
779e8be1fc7SRichard Tran Mills   sparse_matrix_t     csrA, csrB, csrC;
780e8be1fc7SRichard Tran Mills   PetscErrorCode      ierr;
781e8be1fc7SRichard Tran Mills   sparse_status_t     stat = SPARSE_STATUS_SUCCESS;
782e8be1fc7SRichard Tran Mills   struct matrix_descr descr_type_gen;
783e8be1fc7SRichard Tran Mills   PetscObjectState    state;
784e8be1fc7SRichard Tran Mills 
785e8be1fc7SRichard Tran Mills   PetscFunctionBegin;
786e8be1fc7SRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
787e8be1fc7SRichard Tran Mills   b = (Mat_SeqAIJMKL*)B->spptr;
788e8be1fc7SRichard Tran Mills   c = (Mat_SeqAIJMKL*)C->spptr;
789e8be1fc7SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
790e8be1fc7SRichard Tran Mills   if (!a->sparse_optimized || a->state != state) {
791e8be1fc7SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
792e8be1fc7SRichard Tran Mills   }
793e8be1fc7SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr);
794e8be1fc7SRichard Tran Mills   if (!b->sparse_optimized || b->state != state) {
795e8be1fc7SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(B);
796e8be1fc7SRichard Tran Mills   }
797e8be1fc7SRichard Tran Mills   csrA = a->csrA;
798e8be1fc7SRichard Tran Mills   csrB = b->csrA;
799e8be1fc7SRichard Tran Mills   csrC = c->csrA;
800e8be1fc7SRichard Tran Mills   descr_type_gen.type = SPARSE_MATRIX_TYPE_GENERAL;
801e8be1fc7SRichard Tran Mills 
802e8be1fc7SRichard Tran Mills   stat = mkl_sparse_sp2m(SPARSE_OPERATION_NON_TRANSPOSE,descr_type_gen,csrA,
803e8be1fc7SRichard Tran Mills                          SPARSE_OPERATION_NON_TRANSPOSE,descr_type_gen,csrB,
804e8be1fc7SRichard Tran Mills                          SPARSE_STAGE_FINALIZE_MULT,&csrC);
805e8be1fc7SRichard Tran Mills 
806e8be1fc7SRichard 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");
807e8be1fc7SRichard Tran Mills 
808e8be1fc7SRichard Tran Mills   /* Have to update the PETSc AIJ representation for matrix C from contents of MKL handle. */
8094f53af40SRichard Tran Mills   ierr = MatSeqAIJMKL_update_from_mkl_handle(C);CHKERRQ(ierr);
810e8be1fc7SRichard Tran Mills 
811e8be1fc7SRichard Tran Mills   PetscFunctionReturn(0);
812e8be1fc7SRichard Tran Mills }
813*8a369200SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE */
814e8be1fc7SRichard Tran Mills 
815ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
816372ec6bbSRichard Tran Mills PetscErrorCode MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat*C)
817372ec6bbSRichard Tran Mills {
818372ec6bbSRichard Tran Mills   Mat_SeqAIJMKL    *a, *b;
819372ec6bbSRichard Tran Mills   sparse_matrix_t  csrA, csrB, csrC;
820372ec6bbSRichard Tran Mills   PetscErrorCode   ierr;
821372ec6bbSRichard Tran Mills   sparse_status_t  stat = SPARSE_STATUS_SUCCESS;
822551aa5c8SRichard Tran Mills   PetscObjectState state;
823372ec6bbSRichard Tran Mills 
824372ec6bbSRichard Tran Mills   PetscFunctionBegin;
825372ec6bbSRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
826372ec6bbSRichard Tran Mills   b = (Mat_SeqAIJMKL*)B->spptr;
827551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
828551aa5c8SRichard Tran Mills   if (!a->sparse_optimized || a->state != state) {
829372ec6bbSRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
830372ec6bbSRichard Tran Mills   }
831551aa5c8SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr);
832551aa5c8SRichard Tran Mills   if (!b->sparse_optimized || b->state != state) {
833372ec6bbSRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(B);
834372ec6bbSRichard Tran Mills   }
835372ec6bbSRichard Tran Mills   csrA = a->csrA;
836372ec6bbSRichard Tran Mills   csrB = b->csrA;
837372ec6bbSRichard Tran Mills 
838372ec6bbSRichard Tran Mills   stat = mkl_sparse_spmm(SPARSE_OPERATION_TRANSPOSE,csrA,csrB,&csrC);
8399c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete sparse matrix-matrix multiply");
840372ec6bbSRichard Tran Mills 
841372ec6bbSRichard Tran Mills   ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr);
842372ec6bbSRichard Tran Mills 
843372ec6bbSRichard Tran Mills   PetscFunctionReturn(0);
844372ec6bbSRichard Tran Mills }
845372ec6bbSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
846372ec6bbSRichard Tran Mills 
847*8a369200SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE)
8484f53af40SRichard Tran Mills PetscErrorCode MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat P,Mat C)
8494f53af40SRichard Tran Mills {
8504f53af40SRichard Tran Mills   Mat_SeqAIJMKL       *a, *p, *c;
8514f53af40SRichard Tran Mills   sparse_matrix_t     csrA, csrP, csrC;
8524f53af40SRichard Tran Mills   PetscBool           set, flag;
8534f53af40SRichard Tran Mills   sparse_status_t     stat = SPARSE_STATUS_SUCCESS;
854b9e1dd46SRichard Tran Mills   struct matrix_descr descr_type_sym;
8554f53af40SRichard Tran Mills   PetscObjectState    state;
8564f53af40SRichard Tran Mills   PetscErrorCode      ierr;
8574f53af40SRichard Tran Mills 
8584f53af40SRichard Tran Mills   PetscFunctionBegin;
8594f53af40SRichard Tran Mills   ierr = MatIsSymmetricKnown(A,&set,&flag);
8604f53af40SRichard Tran Mills   if (!set || (set && !flag)) {
8614f53af40SRichard Tran Mills     ierr = MatPtAPNumeric_SeqAIJ_SeqAIJ(A,P,C);CHKERRQ(ierr);
8624f53af40SRichard Tran Mills     PetscFunctionReturn(0);
8634f53af40SRichard Tran Mills   }
8644f53af40SRichard Tran Mills 
8654f53af40SRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
8664f53af40SRichard Tran Mills   p = (Mat_SeqAIJMKL*)P->spptr;
8674f53af40SRichard Tran Mills   c = (Mat_SeqAIJMKL*)C->spptr;
8684f53af40SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
8694f53af40SRichard Tran Mills   if (!a->sparse_optimized || a->state != state) {
8704f53af40SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
8714f53af40SRichard Tran Mills   }
8724f53af40SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)P,&state);CHKERRQ(ierr);
8734f53af40SRichard Tran Mills   if (!p->sparse_optimized || p->state != state) {
8744f53af40SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(P);
8754f53af40SRichard Tran Mills   }
8764f53af40SRichard Tran Mills   csrA = a->csrA;
8774f53af40SRichard Tran Mills   csrP = p->csrA;
8784f53af40SRichard Tran Mills   csrC = c->csrA;
879b9e1dd46SRichard Tran Mills   descr_type_sym.type = SPARSE_MATRIX_TYPE_SYMMETRIC;
880b9e1dd46SRichard Tran Mills   descr_type_sym.mode = SPARSE_FILL_MODE_LOWER;
881b9e1dd46SRichard Tran Mills   descr_type_sym.diag = SPARSE_DIAG_NON_UNIT;
8824f53af40SRichard Tran Mills 
883f8990b4aSRichard Tran Mills   /* Note that the call below won't work for complex matrices. (We protect this when pointers are assigned in MatConvert.) */
884b9e1dd46SRichard Tran Mills   stat = mkl_sparse_sypr(SPARSE_OPERATION_TRANSPOSE,csrP,csrA,descr_type_sym,&csrC,SPARSE_STAGE_FINALIZE_MULT);
8854f53af40SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to finalize mkl_sparse_sypr");
8864f53af40SRichard Tran Mills 
8874f53af40SRichard Tran Mills   /* Have to update the PETSc AIJ representation for matrix C from contents of MKL handle. */
8884f53af40SRichard Tran Mills   ierr = MatSeqAIJMKL_update_from_mkl_handle(C);CHKERRQ(ierr);
8894f53af40SRichard Tran Mills 
8904f53af40SRichard Tran Mills   PetscFunctionReturn(0);
8914f53af40SRichard Tran Mills }
8924f53af40SRichard Tran Mills #endif
8934f53af40SRichard Tran Mills 
894*8a369200SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE)
8954f53af40SRichard Tran Mills PetscErrorCode MatPtAP_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C)
8964f53af40SRichard Tran Mills {
8974f53af40SRichard Tran Mills   Mat_SeqAIJMKL       *a, *p;
8984f53af40SRichard Tran Mills   sparse_matrix_t     csrA, csrP, csrC;
8994f53af40SRichard Tran Mills   PetscBool           set, flag;
9004f53af40SRichard Tran Mills   sparse_status_t     stat = SPARSE_STATUS_SUCCESS;
901b9e1dd46SRichard Tran Mills   struct matrix_descr descr_type_sym;
9024f53af40SRichard Tran Mills   PetscObjectState    state;
9034f53af40SRichard Tran Mills   PetscErrorCode      ierr;
9044f53af40SRichard Tran Mills 
9054f53af40SRichard Tran Mills   PetscFunctionBegin;
9064f53af40SRichard Tran Mills   ierr = MatIsSymmetricKnown(A,&set,&flag);
9074f53af40SRichard Tran Mills   if (!set || (set && !flag)) {
9084f53af40SRichard Tran Mills     ierr = MatPtAP_SeqAIJ_SeqAIJ(A,P,scall,fill,C);CHKERRQ(ierr);
9094f53af40SRichard Tran Mills     PetscFunctionReturn(0);
9104f53af40SRichard Tran Mills   }
9114f53af40SRichard Tran Mills 
9124f53af40SRichard Tran Mills   if (scall == MAT_REUSE_MATRIX) {
9134f53af40SRichard Tran Mills     ierr = MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2(A,P,*C);CHKERRQ(ierr);
9144f53af40SRichard Tran Mills     PetscFunctionReturn(0);
9154f53af40SRichard Tran Mills   }
9164f53af40SRichard Tran Mills 
9174f53af40SRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
9184f53af40SRichard Tran Mills   p = (Mat_SeqAIJMKL*)P->spptr;
9194f53af40SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr);
9204f53af40SRichard Tran Mills   if (!a->sparse_optimized || a->state != state) {
9214f53af40SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
9224f53af40SRichard Tran Mills   }
9234f53af40SRichard Tran Mills   ierr = PetscObjectStateGet((PetscObject)P,&state);CHKERRQ(ierr);
9244f53af40SRichard Tran Mills   if (!p->sparse_optimized || p->state != state) {
9254f53af40SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(P);
9264f53af40SRichard Tran Mills   }
9274f53af40SRichard Tran Mills   csrA = a->csrA;
9284f53af40SRichard Tran Mills   csrP = p->csrA;
929b9e1dd46SRichard Tran Mills   descr_type_sym.type = SPARSE_MATRIX_TYPE_SYMMETRIC;
930b9e1dd46SRichard Tran Mills   descr_type_sym.mode = SPARSE_FILL_MODE_LOWER;
931b9e1dd46SRichard Tran Mills   descr_type_sym.diag = SPARSE_DIAG_NON_UNIT;
9324f53af40SRichard Tran Mills 
933f8990b4aSRichard Tran Mills   /* Note that the call below won't work for complex matrices. (We protect this when pointers are assigned in MatConvert.) */
934b9e1dd46SRichard Tran Mills   stat = mkl_sparse_sypr(SPARSE_OPERATION_TRANSPOSE,csrP,csrA,descr_type_sym,&csrC,SPARSE_STAGE_FULL_MULT);
9354f53af40SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete full mkl_sparse_sypr");
9364f53af40SRichard Tran Mills 
9374f53af40SRichard Tran Mills   ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr);
9384f53af40SRichard Tran Mills   ierr = MatSetOption(*C,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
9394f53af40SRichard Tran Mills 
9404f53af40SRichard Tran Mills   PetscFunctionReturn(0);
9414f53af40SRichard Tran Mills }
9424f53af40SRichard Tran Mills #endif
9434f53af40SRichard Tran Mills 
944a0dc3a43SRichard Tran Mills /* This function prototype is needed in MatConvert_SeqAIJ_SeqAIJMKL(), below. */
945a0dc3a43SRichard Tran Mills PETSC_INTERN PetscErrorCode MatPtAP_IS_XAIJ(Mat,Mat,MatReuse,PetscReal,Mat*);
946a0dc3a43SRichard Tran Mills 
9474a2a386eSRichard Tran Mills /* MatConvert_SeqAIJ_SeqAIJMKL converts a SeqAIJ matrix into a
948510b72f4SRichard Tran Mills  * SeqAIJMKL matrix.  This routine is called by the MatCreate_SeqAIJMKL()
9494a2a386eSRichard Tran Mills  * routine, but can also be used to convert an assembled SeqAIJ matrix
9504a2a386eSRichard Tran Mills  * into a SeqAIJMKL one. */
9514a2a386eSRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJMKL(Mat A,MatType type,MatReuse reuse,Mat *newmat)
9524a2a386eSRichard Tran Mills {
9534a2a386eSRichard Tran Mills   PetscErrorCode ierr;
9544a2a386eSRichard Tran Mills   Mat            B = *newmat;
9554a2a386eSRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl;
956c9d46305SRichard Tran Mills   PetscBool      set;
957e9c94282SRichard Tran Mills   PetscBool      sametype;
9584a2a386eSRichard Tran Mills 
9594a2a386eSRichard Tran Mills   PetscFunctionBegin;
9604a2a386eSRichard Tran Mills   if (reuse == MAT_INITIAL_MATRIX) {
9614a2a386eSRichard Tran Mills     ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr);
9624a2a386eSRichard Tran Mills   }
9634a2a386eSRichard Tran Mills 
964e9c94282SRichard Tran Mills   ierr = PetscObjectTypeCompare((PetscObject)A,type,&sametype);CHKERRQ(ierr);
965e9c94282SRichard Tran Mills   if (sametype) PetscFunctionReturn(0);
966e9c94282SRichard Tran Mills 
9674a2a386eSRichard Tran Mills   ierr     = PetscNewLog(B,&aijmkl);CHKERRQ(ierr);
9684a2a386eSRichard Tran Mills   B->spptr = (void*) aijmkl;
9694a2a386eSRichard Tran Mills 
970df555b71SRichard Tran Mills   /* Set function pointers for methods that we inherit from AIJ but override.
971969800c5SRichard Tran Mills    * We also parse some command line options below, since those determine some of the methods we point to. */
9724a2a386eSRichard Tran Mills   B->ops->duplicate        = MatDuplicate_SeqAIJMKL;
9734a2a386eSRichard Tran Mills   B->ops->assemblyend      = MatAssemblyEnd_SeqAIJMKL;
9744a2a386eSRichard Tran Mills   B->ops->destroy          = MatDestroy_SeqAIJMKL;
975c9d46305SRichard Tran Mills 
9764abfa3b3SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_FALSE;
977ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
978d995685eSRichard Tran Mills   aijmkl->no_SpMV2 = PETSC_FALSE;  /* Default to using the SpMV2 routines if our MKL supports them. */
979a8327b06SKarl Rupp #else
980d995685eSRichard Tran Mills   aijmkl->no_SpMV2 = PETSC_TRUE;
981d995685eSRichard Tran Mills #endif
9825b49642aSRichard Tran Mills   aijmkl->eager_inspection = PETSC_FALSE;
9834abfa3b3SRichard Tran Mills 
9844abfa3b3SRichard Tran Mills   /* Parse command line options. */
985c9d46305SRichard Tran Mills   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"AIJMKL Options","Mat");CHKERRQ(ierr);
986c9d46305SRichard Tran Mills   ierr = PetscOptionsBool("-mat_aijmkl_no_spmv2","NoSPMV2","None",(PetscBool)aijmkl->no_SpMV2,(PetscBool*)&aijmkl->no_SpMV2,&set);CHKERRQ(ierr);
9875b49642aSRichard Tran Mills   ierr = PetscOptionsBool("-mat_aijmkl_eager_inspection","Eager Inspection","None",(PetscBool)aijmkl->eager_inspection,(PetscBool*)&aijmkl->eager_inspection,&set);CHKERRQ(ierr);
988c9d46305SRichard Tran Mills   ierr = PetscOptionsEnd();CHKERRQ(ierr);
989ffcab697SRichard Tran Mills #if !defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
990d995685eSRichard Tran Mills   if(!aijmkl->no_SpMV2) {
991d995685eSRichard 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");
992d995685eSRichard Tran Mills     aijmkl->no_SpMV2 = PETSC_TRUE;
993d995685eSRichard Tran Mills   }
994d995685eSRichard Tran Mills #endif
995c9d46305SRichard Tran Mills 
996ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
997df555b71SRichard Tran Mills   B->ops->mult             = MatMult_SeqAIJMKL_SpMV2;
998969800c5SRichard Tran Mills   B->ops->multtranspose    = MatMultTranspose_SeqAIJMKL_SpMV2;
999df555b71SRichard Tran Mills   B->ops->multadd          = MatMultAdd_SeqAIJMKL_SpMV2;
1000969800c5SRichard Tran Mills   B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL_SpMV2;
100145fbe478SRichard Tran Mills   B->ops->matmult          = MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2;
1002*8a369200SRichard Tran Mills # if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE)
1003e8be1fc7SRichard Tran Mills   B->ops->matmultnumeric   = MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2;
1004ffcab697SRichard Tran Mills #   if !defined(PETSC_USE_COMPLEX)
10054f53af40SRichard Tran Mills   B->ops->ptap             = MatPtAP_SeqAIJMKL_SeqAIJMKL_SpMV2;
10064f53af40SRichard Tran Mills   B->ops->ptapnumeric      = MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2;
10074f53af40SRichard Tran Mills #   endif
1008e8be1fc7SRichard Tran Mills # endif
1009a557fde5SRichard Tran Mills   B->ops->transposematmult = MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2;
10101950a7e7SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
10111950a7e7SRichard Tran Mills 
1012213898a2SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED)
1013213898a2SRichard Tran Mills   /* In MKL version 18, update 2, the old sparse BLAS interfaces were marked as deprecated. If "no_SpMV2" has been specified by the
1014213898a2SRichard Tran Mills    * user and the old SpBLAS interfaces are deprecated in our MKL version, we use the new _SpMV2 routines (set above), but do not
1015213898a2SRichard Tran Mills    * call mkl_sparse_optimize(), which results in the old numerical kernels (without the inspector-executor model) being used. For
1016213898a2SRichard Tran Mills    * versions in which the older interface has not been deprecated, we use the old interface. */
10171950a7e7SRichard Tran Mills   if (aijmkl->no_SpMV2) {
10184a2a386eSRichard Tran Mills     B->ops->mult             = MatMult_SeqAIJMKL;
1019969800c5SRichard Tran Mills     B->ops->multtranspose    = MatMultTranspose_SeqAIJMKL;
10204a2a386eSRichard Tran Mills     B->ops->multadd          = MatMultAdd_SeqAIJMKL;
1021969800c5SRichard Tran Mills     B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL;
1022c9d46305SRichard Tran Mills   }
10231950a7e7SRichard Tran Mills #endif
10244a2a386eSRichard Tran Mills 
10254a2a386eSRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",MatConvert_SeqAIJMKL_SeqAIJ);CHKERRQ(ierr);
1026e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaijmkl_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
1027e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaijmkl_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
1028e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaijmkl_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
1029a0dc3a43SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatPtAP_is_seqaijmkl_C",MatPtAP_IS_XAIJ);CHKERRQ(ierr);
103045fbe478SRichard Tran Mills   if(!aijmkl->no_SpMV2) {
1031ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE)
103245fbe478SRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqaijmkl_seqaijmkl_C",MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr);
1033*8a369200SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE)
1034e8be1fc7SRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqaijmkl_seqaijmkl_C",MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr);
1035e8be1fc7SRichard Tran Mills #endif
1036372ec6bbSRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatTransposeMatMult_seqaijmkl_seqaijmkl_C",MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr);
103745fbe478SRichard Tran Mills #endif
103845fbe478SRichard Tran Mills   }
10394a2a386eSRichard Tran Mills 
10404a2a386eSRichard Tran Mills   ierr    = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJMKL);CHKERRQ(ierr);
10414a2a386eSRichard Tran Mills   *newmat = B;
10424a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
10434a2a386eSRichard Tran Mills }
10444a2a386eSRichard Tran Mills 
10454a2a386eSRichard Tran Mills /*@C
10464a2a386eSRichard Tran Mills    MatCreateSeqAIJMKL - Creates a sparse matrix of type SEQAIJMKL.
10474a2a386eSRichard Tran Mills    This type inherits from AIJ and is largely identical, but uses sparse BLAS
10484a2a386eSRichard Tran Mills    routines from Intel MKL whenever possible.
104990147e49SRichard Tran Mills    If the installed version of MKL supports the "SpMV2" sparse
105090147e49SRichard Tran Mills    inspector-executor routines, then those are used by default.
1051597ee276SRichard Tran Mills    MatMult, MatMultAdd, MatMultTranspose, MatMultTransposeAdd, MatMatMult, MatTransposeMatMult, and MatPtAP (for
1052597ee276SRichard Tran Mills    symmetric A) operations are currently supported.
1053597ee276SRichard Tran Mills    Note that MKL version 18, update 2 or later is required for MatPtAP/MatPtAPNumeric and MatMatMultNumeric.
105490147e49SRichard Tran Mills 
10554a2a386eSRichard Tran Mills    Collective on MPI_Comm
10564a2a386eSRichard Tran Mills 
10574a2a386eSRichard Tran Mills    Input Parameters:
10584a2a386eSRichard Tran Mills +  comm - MPI communicator, set to PETSC_COMM_SELF
10594a2a386eSRichard Tran Mills .  m - number of rows
10604a2a386eSRichard Tran Mills .  n - number of columns
10614a2a386eSRichard Tran Mills .  nz - number of nonzeros per row (same for all rows)
10624a2a386eSRichard Tran Mills -  nnz - array containing the number of nonzeros in the various rows
10634a2a386eSRichard Tran Mills          (possibly different for each row) or NULL
10644a2a386eSRichard Tran Mills 
10654a2a386eSRichard Tran Mills    Output Parameter:
10664a2a386eSRichard Tran Mills .  A - the matrix
10674a2a386eSRichard Tran Mills 
106890147e49SRichard Tran Mills    Options Database Keys:
106966b7eeb6SRichard Tran Mills +  -mat_aijmkl_no_spmv2 - disable use of the SpMV2 inspector-executor routines
107066b7eeb6SRichard 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
107190147e49SRichard Tran Mills 
10724a2a386eSRichard Tran Mills    Notes:
10734a2a386eSRichard Tran Mills    If nnz is given then nz is ignored
10744a2a386eSRichard Tran Mills 
10754a2a386eSRichard Tran Mills    Level: intermediate
10764a2a386eSRichard Tran Mills 
107790147e49SRichard Tran Mills .keywords: matrix, MKL, sparse, parallel
10784a2a386eSRichard Tran Mills 
10794a2a386eSRichard Tran Mills .seealso: MatCreate(), MatCreateMPIAIJMKL(), MatSetValues()
10804a2a386eSRichard Tran Mills @*/
10814a2a386eSRichard Tran Mills PetscErrorCode  MatCreateSeqAIJMKL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
10824a2a386eSRichard Tran Mills {
10834a2a386eSRichard Tran Mills   PetscErrorCode ierr;
10844a2a386eSRichard Tran Mills 
10854a2a386eSRichard Tran Mills   PetscFunctionBegin;
10864a2a386eSRichard Tran Mills   ierr = MatCreate(comm,A);CHKERRQ(ierr);
10874a2a386eSRichard Tran Mills   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
10884a2a386eSRichard Tran Mills   ierr = MatSetType(*A,MATSEQAIJMKL);CHKERRQ(ierr);
10894a2a386eSRichard Tran Mills   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
10904a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
10914a2a386eSRichard Tran Mills }
10924a2a386eSRichard Tran Mills 
10934a2a386eSRichard Tran Mills PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJMKL(Mat A)
10944a2a386eSRichard Tran Mills {
10954a2a386eSRichard Tran Mills   PetscErrorCode ierr;
10964a2a386eSRichard Tran Mills 
10974a2a386eSRichard Tran Mills   PetscFunctionBegin;
10984a2a386eSRichard Tran Mills   ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr);
10994a2a386eSRichard Tran Mills   ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr);
11004a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
11014a2a386eSRichard Tran Mills }
1102