xref: /petsc/src/mat/impls/aij/seq/aijmkl/aijmkl.c (revision 9c46acdfbc66b9187afcb21f11a82b85f21fce68)
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. */
19b8cbc1fbSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
20df555b71SRichard Tran Mills   sparse_matrix_t csrA; /* "Handle" used by SpMV2 inspector-executor routines. */
21df555b71SRichard Tran Mills   struct matrix_descr descr;
22b8cbc1fbSRichard Tran Mills #endif
234a2a386eSRichard Tran Mills } Mat_SeqAIJMKL;
244a2a386eSRichard Tran Mills 
254a2a386eSRichard Tran Mills extern PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat,MatAssemblyType);
264a2a386eSRichard Tran Mills 
274a2a386eSRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJMKL_SeqAIJ(Mat A,MatType type,MatReuse reuse,Mat *newmat)
284a2a386eSRichard Tran Mills {
294a2a386eSRichard Tran Mills   /* This routine is only called to convert a MATAIJMKL to its base PETSc type, */
304a2a386eSRichard Tran Mills   /* so we will ignore 'MatType type'. */
314a2a386eSRichard Tran Mills   PetscErrorCode ierr;
324a2a386eSRichard Tran Mills   Mat            B       = *newmat;
33c1d5218aSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
344a2a386eSRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
35c1d5218aSRichard Tran Mills #endif
364a2a386eSRichard Tran Mills 
374a2a386eSRichard Tran Mills   PetscFunctionBegin;
384a2a386eSRichard Tran Mills   if (reuse == MAT_INITIAL_MATRIX) {
394a2a386eSRichard Tran Mills     ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr);
404a2a386eSRichard Tran Mills   }
414a2a386eSRichard Tran Mills 
424a2a386eSRichard Tran Mills   /* Reset the original function pointers. */
4354871a98SRichard Tran Mills   B->ops->duplicate        = MatDuplicate_SeqAIJ;
444a2a386eSRichard Tran Mills   B->ops->assemblyend      = MatAssemblyEnd_SeqAIJ;
454a2a386eSRichard Tran Mills   B->ops->destroy          = MatDestroy_SeqAIJ;
4654871a98SRichard Tran Mills   B->ops->mult             = MatMult_SeqAIJ;
47ff03dc53SRichard Tran Mills   B->ops->multtranspose    = MatMultTranspose_SeqAIJ;
4854871a98SRichard Tran Mills   B->ops->multadd          = MatMultAdd_SeqAIJ;
49ff03dc53SRichard Tran Mills   B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJ;
5045fbe478SRichard Tran Mills   B->ops->matmult          = MatMatMult_SeqAIJ_SeqAIJ;
51372ec6bbSRichard Tran Mills   B->ops->transposematmult = MatTransposeMatMult_SeqAIJ_SeqAIJ;
5287c2a1d7SRichard Tran Mills   B->ops->scale            = MatScale_SeqAIJ;
5387c2a1d7SRichard Tran Mills   B->ops->diagonalscale    = MatDiagonalScale_SeqAIJ;
5487c2a1d7SRichard Tran Mills   B->ops->diagonalset      = MatDiagonalSet_SeqAIJ;
5587c2a1d7SRichard Tran Mills   B->ops->axpy             = MatAXPY_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);
6145fbe478SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
624a940b00SSatish Balay   if(!aijmkl->no_SpMV2) {
6345fbe478SRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqaijmkl_seqaijmkl_C",NULL);CHKERRQ(ierr);
64372ec6bbSRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatTransposeMatMult_seqaijmkl_seqaijmkl_C",NULL);CHKERRQ(ierr);
6545fbe478SRichard Tran Mills   }
66e9c94282SRichard Tran Mills 
674abfa3b3SRichard Tran Mills   /* Free everything in the Mat_SeqAIJMKL data structure. Currently, this
68e9c94282SRichard Tran Mills    * simply involves destroying the MKL sparse matrix handle and then freeing
69e9c94282SRichard Tran Mills    * the spptr pointer. */
70a8327b06SKarl Rupp   if (reuse == MAT_INITIAL_MATRIX) aijmkl = (Mat_SeqAIJMKL*)B->spptr;
71a8327b06SKarl Rupp 
724abfa3b3SRichard Tran Mills   if (aijmkl->sparse_optimized) {
730632b357SRichard Tran Mills     sparse_status_t stat;
744abfa3b3SRichard Tran Mills     stat = mkl_sparse_destroy(aijmkl->csrA);
75*9c46acdfSRichard 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");
764abfa3b3SRichard Tran Mills   }
774abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
78e9c94282SRichard Tran Mills   ierr = PetscFree(B->spptr);CHKERRQ(ierr);
794a2a386eSRichard Tran Mills 
804a2a386eSRichard Tran Mills   /* Change the type of B to MATSEQAIJ. */
814a2a386eSRichard Tran Mills   ierr = PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ);CHKERRQ(ierr);
824a2a386eSRichard Tran Mills 
834a2a386eSRichard Tran Mills   *newmat = B;
844a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
854a2a386eSRichard Tran Mills }
864a2a386eSRichard Tran Mills 
874a2a386eSRichard Tran Mills PetscErrorCode MatDestroy_SeqAIJMKL(Mat A)
884a2a386eSRichard Tran Mills {
894a2a386eSRichard Tran Mills   PetscErrorCode ierr;
904a2a386eSRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl = (Mat_SeqAIJMKL*) A->spptr;
914a2a386eSRichard Tran Mills 
924a2a386eSRichard Tran Mills   PetscFunctionBegin;
93e9c94282SRichard Tran Mills 
94e9c94282SRichard Tran Mills   /* If MatHeaderMerge() was used, then this SeqAIJMKL matrix will not have an
95e9c94282SRichard Tran Mills    * spptr pointer. */
96e9c94282SRichard Tran Mills   if (aijmkl) {
974a2a386eSRichard Tran Mills     /* Clean up everything in the Mat_SeqAIJMKL data structure, then free A->spptr. */
984abfa3b3SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
994abfa3b3SRichard Tran Mills     if (aijmkl->sparse_optimized) {
1004abfa3b3SRichard Tran Mills       sparse_status_t stat = SPARSE_STATUS_SUCCESS;
1014abfa3b3SRichard Tran Mills       stat = mkl_sparse_destroy(aijmkl->csrA);
102*9c46acdfSRichard Tran Mills       if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy");
1034abfa3b3SRichard Tran Mills     }
1044abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
1054a2a386eSRichard Tran Mills     ierr = PetscFree(A->spptr);CHKERRQ(ierr);
106e9c94282SRichard Tran Mills   }
1074a2a386eSRichard Tran Mills 
1084a2a386eSRichard Tran Mills   /* Change the type of A back to SEQAIJ and use MatDestroy_SeqAIJ()
1094a2a386eSRichard Tran Mills    * to destroy everything that remains. */
1104a2a386eSRichard Tran Mills   ierr = PetscObjectChangeTypeName((PetscObject)A, MATSEQAIJ);CHKERRQ(ierr);
1114a2a386eSRichard Tran Mills   /* Note that I don't call MatSetType().  I believe this is because that
1124a2a386eSRichard Tran Mills    * is only to be called when *building* a matrix.  I could be wrong, but
1134a2a386eSRichard Tran Mills    * that is how things work for the SuperLU matrix class. */
1144a2a386eSRichard Tran Mills   ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr);
1154a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
1164a2a386eSRichard Tran Mills }
1174a2a386eSRichard Tran Mills 
1185b49642aSRichard Tran Mills /* MatSeqAIJKL_create_mkl_handle(), if called with an AIJMKL matrix that has not had mkl_sparse_optimize() called for it,
1195b49642aSRichard Tran Mills  * creates an MKL sparse matrix handle from the AIJ arrays and calls mkl_sparse_optimize().
1205b49642aSRichard Tran Mills  * If called with an AIJMKL matrix for which aijmkl->sparse_optimized == PETSC_TRUE, then it destroys the old matrix
1215b49642aSRichard Tran Mills  * handle, creates a new one, and then calls mkl_sparse_optimize().
1225b49642aSRichard Tran Mills  * Although in normal MKL usage it is possible to have a valid matrix handle on which mkl_sparse_optimize() has not been
1235b49642aSRichard Tran Mills  * called, for AIJMKL the handle creation and optimization step always occur together, so we don't handle the case of
1245b49642aSRichard Tran Mills  * an unoptimized matrix handle here. */
1256e369cd5SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_mkl_handle(Mat A)
1264a2a386eSRichard Tran Mills {
1276e369cd5SRichard Tran Mills #ifndef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
1286e369cd5SRichard Tran Mills   /* If the MKL library does not have mkl_sparse_optimize(), then this routine
1296e369cd5SRichard Tran Mills    * does nothing. We make it callable anyway in this case because it cuts
1306e369cd5SRichard Tran Mills    * down on littering the code with #ifdefs. */
13145fbe478SRichard Tran Mills   PetscFunctionBegin;
1326e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
1336e369cd5SRichard Tran Mills #else
134a8327b06SKarl Rupp   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
135a8327b06SKarl Rupp   Mat_SeqAIJMKL   *aijmkl = (Mat_SeqAIJMKL*)A->spptr;
136a8327b06SKarl Rupp   PetscInt        m,n;
137a8327b06SKarl Rupp   MatScalar       *aa;
138a8327b06SKarl Rupp   PetscInt        *aj,*ai;
1396e369cd5SRichard Tran Mills   sparse_status_t stat;
1404a2a386eSRichard Tran Mills 
141a8327b06SKarl Rupp   PetscFunctionBegin;
1426e369cd5SRichard Tran Mills   if (aijmkl->no_SpMV2) PetscFunctionReturn(0);
1436e369cd5SRichard Tran Mills 
1440632b357SRichard Tran Mills   if (aijmkl->sparse_optimized) {
1450632b357SRichard Tran Mills     /* Matrix has been previously assembled and optimized. Must destroy old
1460632b357SRichard Tran Mills      * matrix handle before running the optimization step again. */
1470632b357SRichard Tran Mills     stat = mkl_sparse_destroy(aijmkl->csrA);
148*9c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy");
1490632b357SRichard Tran Mills   }
1508d3fe1b0SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_FALSE;
1516e369cd5SRichard Tran Mills 
152c9d46305SRichard Tran Mills   /* Now perform the SpMV2 setup and matrix optimization. */
153df555b71SRichard Tran Mills   aijmkl->descr.type        = SPARSE_MATRIX_TYPE_GENERAL;
154df555b71SRichard Tran Mills   aijmkl->descr.mode        = SPARSE_FILL_MODE_LOWER;
155df555b71SRichard Tran Mills   aijmkl->descr.diag        = SPARSE_DIAG_NON_UNIT;
15658678438SRichard Tran Mills   m = A->rmap->n;
15758678438SRichard Tran Mills   n = A->cmap->n;
158df555b71SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
159df555b71SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
160df555b71SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
16180095d54SIrina Sokolova   if ((a->nz!=0) & !(A->structure_only)) {
1628d3fe1b0SRichard Tran Mills     /* Create a new, optimized sparse matrix handle only if the matrix has nonzero entries.
1638d3fe1b0SRichard Tran Mills      * The MKL sparse-inspector executor routines don't like being passed an empty matrix. */
16458678438SRichard Tran Mills     stat = mkl_sparse_x_create_csr(&aijmkl->csrA,SPARSE_INDEX_BASE_ZERO,m,n,ai,ai+1,aj,aa);
165df555b71SRichard Tran Mills     stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000);
166df555b71SRichard Tran Mills     stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE);
167df555b71SRichard Tran Mills     stat = mkl_sparse_optimize(aijmkl->csrA);
168*9c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to create matrix handle/complete mkl_sparse_optimize");
1694abfa3b3SRichard Tran Mills     aijmkl->sparse_optimized = PETSC_TRUE;
170c9d46305SRichard Tran Mills   }
1716e369cd5SRichard Tran Mills 
1726e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
173d995685eSRichard Tran Mills #endif
1746e369cd5SRichard Tran Mills }
1756e369cd5SRichard Tran Mills 
17619afcda9SRichard Tran Mills /* MatSeqAIJMKL_create_from_mkl_handle() creates a sequential AIJMKL matrix from an MKL sparse matrix handle.
17719afcda9SRichard Tran Mills  * We need this to implement MatMatMult() using the MKL inspector-executor routines, which return an (unoptimized)
1786c87cf42SRichard Tran Mills  * matrix handle.
179aab60f1bSRichard Tran Mills  * Note: This routine simply destroys and replaces the original matrix if MAT_REUSE_MATRIX has been specified, as
180aab60f1bSRichard Tran Mills  * there is no good alternative. */
18119afcda9SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
1826c87cf42SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_from_mkl_handle(MPI_Comm comm,sparse_matrix_t csrA,MatReuse reuse,Mat *mat)
18319afcda9SRichard Tran Mills {
18419afcda9SRichard Tran Mills   PetscErrorCode      ierr;
18519afcda9SRichard Tran Mills   sparse_status_t     stat;
18619afcda9SRichard Tran Mills   sparse_index_base_t indexing;
18719afcda9SRichard Tran Mills   PetscInt            nrows, ncols;
18845fbe478SRichard Tran Mills   PetscInt            *aj,*ai,*dummy;
18919afcda9SRichard Tran Mills   MatScalar           *aa;
19019afcda9SRichard Tran Mills   Mat                 A;
19119afcda9SRichard Tran Mills   Mat_SeqAIJMKL       *aijmkl;
19219afcda9SRichard Tran Mills 
19345fbe478SRichard Tran Mills   /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */
19445fbe478SRichard Tran Mills   stat = mkl_sparse_x_export_csr(csrA,&indexing,&nrows,&ncols,&ai,&dummy,&aj,&aa);
195*9c46acdfSRichard 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()");
1966c87cf42SRichard Tran Mills 
197aab60f1bSRichard Tran Mills   if (reuse == MAT_REUSE_MATRIX) {
198aab60f1bSRichard Tran Mills     ierr = MatDestroy(mat);CHKERRQ(ierr);
199aab60f1bSRichard Tran Mills   }
20019afcda9SRichard Tran Mills   ierr = MatCreate(comm,&A);CHKERRQ(ierr);
20119afcda9SRichard Tran Mills   ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr);
20245fbe478SRichard Tran Mills   ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,nrows,ncols);CHKERRQ(ierr);
203aab60f1bSRichard Tran Mills   /* We use MatSeqAIJSetPreallocationCSR() instead of MatCreateSeqAIJWithArrays() because we must copy the arrays exported
204aab60f1bSRichard Tran Mills    * from MKL; MKL developers tell us that modifying the arrays may cause unexpected results when using the MKL handle, and
205aab60f1bSRichard Tran Mills    * they will be destroyed when the MKL handle is destroyed.
206aab60f1bSRichard Tran Mills    * (In the interest of reducing memory consumption in future, can we figure out good ways to deal with this?) */
20719afcda9SRichard Tran Mills   ierr = MatSeqAIJSetPreallocationCSR(A,ai,aj,aa);CHKERRQ(ierr);
20819afcda9SRichard Tran Mills 
20919afcda9SRichard Tran Mills   /* We now have an assembled sequential AIJ matrix created from copies of the exported arrays from the MKL matrix handle.
21019afcda9SRichard Tran Mills    * Now turn it into a MATSEQAIJMKL. */
21119afcda9SRichard Tran Mills   ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr);
2126c87cf42SRichard Tran Mills 
21319afcda9SRichard Tran Mills   aijmkl = (Mat_SeqAIJMKL*) A->spptr;
21419afcda9SRichard Tran Mills   aijmkl->csrA = csrA;
2156c87cf42SRichard Tran Mills 
21619afcda9SRichard Tran Mills   /* The below code duplicates much of what is in MatSeqAIJKL_create_mkl_handle(). I dislike this code duplication, but
21719afcda9SRichard Tran Mills    * MatSeqAIJMKL_create_mkl_handle() cannot be used because we don't need to create a handle -- we've already got one,
21819afcda9SRichard Tran Mills    * and just need to be able to run the MKL optimization step. */
219f3fd1758SRichard Tran Mills   aijmkl->descr.type        = SPARSE_MATRIX_TYPE_GENERAL;
220f3fd1758SRichard Tran Mills   aijmkl->descr.mode        = SPARSE_FILL_MODE_LOWER;
221f3fd1758SRichard Tran Mills   aijmkl->descr.diag        = SPARSE_DIAG_NON_UNIT;
22219afcda9SRichard Tran Mills   stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000);
22319afcda9SRichard Tran Mills   stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE);
22419afcda9SRichard Tran Mills   stat = mkl_sparse_optimize(aijmkl->csrA);
225*9c46acdfSRichard 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");
22619afcda9SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_TRUE;
22719afcda9SRichard Tran Mills 
22819afcda9SRichard Tran Mills   *mat = A;
22919afcda9SRichard Tran Mills   PetscFunctionReturn(0);
23019afcda9SRichard Tran Mills }
23119afcda9SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
23219afcda9SRichard Tran Mills 
2336e369cd5SRichard Tran Mills PetscErrorCode MatDuplicate_SeqAIJMKL(Mat A, MatDuplicateOption op, Mat *M)
2346e369cd5SRichard Tran Mills {
2356e369cd5SRichard Tran Mills   PetscErrorCode ierr;
2366e369cd5SRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl;
2376e369cd5SRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl_dest;
2386e369cd5SRichard Tran Mills 
2396e369cd5SRichard Tran Mills   PetscFunctionBegin;
2406e369cd5SRichard Tran Mills   ierr = MatDuplicate_SeqAIJ(A,op,M);CHKERRQ(ierr);
2416e369cd5SRichard Tran Mills   aijmkl      = (Mat_SeqAIJMKL*) A->spptr;
2426e369cd5SRichard Tran Mills   aijmkl_dest = (Mat_SeqAIJMKL*) (*M)->spptr;
2436e369cd5SRichard Tran Mills   ierr = PetscMemcpy(aijmkl_dest,aijmkl,sizeof(Mat_SeqAIJMKL));CHKERRQ(ierr);
2446e369cd5SRichard Tran Mills   aijmkl_dest->sparse_optimized = PETSC_FALSE;
2455b49642aSRichard Tran Mills   if (aijmkl->eager_inspection) {
2466e369cd5SRichard Tran Mills     ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr);
2475b49642aSRichard Tran Mills   }
2486e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
2496e369cd5SRichard Tran Mills }
2506e369cd5SRichard Tran Mills 
2516e369cd5SRichard Tran Mills PetscErrorCode MatAssemblyEnd_SeqAIJMKL(Mat A, MatAssemblyType mode)
2526e369cd5SRichard Tran Mills {
2536e369cd5SRichard Tran Mills   PetscErrorCode  ierr;
2546e369cd5SRichard Tran Mills   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
2555b49642aSRichard Tran Mills   Mat_SeqAIJMKL   *aijmkl;
2566e369cd5SRichard Tran Mills 
2576e369cd5SRichard Tran Mills   PetscFunctionBegin;
2586e369cd5SRichard Tran Mills   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
2596e369cd5SRichard Tran Mills 
2606e369cd5SRichard Tran Mills   /* Since a MATSEQAIJMKL matrix is really just a MATSEQAIJ with some
2616e369cd5SRichard Tran Mills    * extra information and some different methods, call the AssemblyEnd
2626e369cd5SRichard Tran Mills    * routine for a MATSEQAIJ.
2636e369cd5SRichard Tran Mills    * I'm not sure if this is the best way to do this, but it avoids
264d96e85feSRichard Tran Mills    * a lot of code duplication. */
2656e369cd5SRichard Tran Mills   a->inode.use = PETSC_FALSE;  /* Must disable: otherwise the MKL routines won't get used. */
2666e369cd5SRichard Tran Mills   ierr = MatAssemblyEnd_SeqAIJ(A, mode);CHKERRQ(ierr);
2676e369cd5SRichard Tran Mills 
2685b49642aSRichard Tran Mills   /* If the user has requested "eager" inspection, create the optimized MKL sparse handle (if needed; the function checks).
2695b49642aSRichard Tran Mills    * (The default is to do "lazy" inspection, deferring this until something like MatMult() is called.) */
2705b49642aSRichard Tran Mills   aijmkl = (Mat_SeqAIJMKL*) A->spptr;
2715b49642aSRichard Tran Mills   if (aijmkl->eager_inspection) {
2726e369cd5SRichard Tran Mills     ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr);
2734a940b00SSatish Balay #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
274886913bfSRichard Tran Mills   } else if (aijmkl->sparse_optimized) {
275886913bfSRichard Tran Mills     /* If doing lazy inspection and there is an optimized MKL handle, we need to destroy it, so that it will be
276886913bfSRichard Tran Mills      * rebuilt later when needed. Otherwise, some SeqAIJ implementations that we depend on for some operations
277886913bfSRichard Tran Mills      * (such as MatMatMultNumeric()) can modify the result matrix without the matrix handle being rebuilt.
2787225e97aSRichard Tran Mills      * (The SeqAIJ version MatMatMultNumeric() knows nothing about matrix handles, but it *does* call MatAssemblyEnd().) */
279886913bfSRichard Tran Mills     sparse_status_t stat = mkl_sparse_destroy(aijmkl->csrA);
280*9c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy");
281886913bfSRichard Tran Mills     aijmkl->sparse_optimized = PETSC_FALSE;
2824a940b00SSatish Balay #endif
2835b49642aSRichard Tran Mills   }
284df555b71SRichard Tran Mills 
2854a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
2864a2a386eSRichard Tran Mills }
2874a2a386eSRichard Tran Mills 
2884a2a386eSRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL(Mat A,Vec xx,Vec yy)
2894a2a386eSRichard Tran Mills {
2904a2a386eSRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2914a2a386eSRichard Tran Mills   const PetscScalar *x;
2924a2a386eSRichard Tran Mills   PetscScalar       *y;
2934a2a386eSRichard Tran Mills   const MatScalar   *aa;
2944a2a386eSRichard Tran Mills   PetscErrorCode    ierr;
2954a2a386eSRichard Tran Mills   PetscInt          m=A->rmap->n;
296db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
297db63039fSRichard Tran Mills   PetscScalar       alpha = 1.0;
298db63039fSRichard Tran Mills   PetscScalar       beta = 0.0;
2994a2a386eSRichard Tran Mills   const PetscInt    *aj,*ai;
300db63039fSRichard Tran Mills   char              matdescra[6];
301db63039fSRichard Tran Mills 
3024a2a386eSRichard Tran Mills 
3034a2a386eSRichard Tran Mills   /* Variables not in MatMult_SeqAIJ. */
304ff03dc53SRichard Tran Mills   char transa = 'n';  /* Used to indicate to MKL that we are not computing the transpose product. */
305ff03dc53SRichard Tran Mills 
306ff03dc53SRichard Tran Mills   PetscFunctionBegin;
307db63039fSRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
308db63039fSRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
309ff03dc53SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
310ff03dc53SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
311ff03dc53SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
312ff03dc53SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
313ff03dc53SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
314ff03dc53SRichard Tran Mills 
315ff03dc53SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
316db63039fSRichard Tran Mills   mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y);
317ff03dc53SRichard Tran Mills 
318ff03dc53SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
319ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
320ff03dc53SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
321ff03dc53SRichard Tran Mills   PetscFunctionReturn(0);
322ff03dc53SRichard Tran Mills }
323ff03dc53SRichard Tran Mills 
324d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
325df555b71SRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy)
326df555b71SRichard Tran Mills {
327df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
328df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
329df555b71SRichard Tran Mills   const PetscScalar *x;
330df555b71SRichard Tran Mills   PetscScalar       *y;
331df555b71SRichard Tran Mills   PetscErrorCode    ierr;
332df555b71SRichard Tran Mills   sparse_status_t   stat = SPARSE_STATUS_SUCCESS;
333df555b71SRichard Tran Mills 
334df555b71SRichard Tran Mills   PetscFunctionBegin;
335df555b71SRichard Tran Mills 
33638987b35SRichard Tran Mills   /* If there are no nonzero entries, zero yy and return immediately. */
33738987b35SRichard Tran Mills   if(!a->nz) {
33838987b35SRichard Tran Mills     PetscInt i;
33938987b35SRichard Tran Mills     PetscInt m=A->rmap->n;
34038987b35SRichard Tran Mills     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
34138987b35SRichard Tran Mills     for (i=0; i<m; i++) {
34238987b35SRichard Tran Mills       y[i] = 0.0;
34338987b35SRichard Tran Mills     }
34438987b35SRichard Tran Mills     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
34538987b35SRichard Tran Mills     PetscFunctionReturn(0);
34638987b35SRichard Tran Mills   }
347f36dfe3fSRichard Tran Mills 
348df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
349df555b71SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
350df555b71SRichard Tran Mills 
3513fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
3523fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
3533fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
3543fa15762SRichard Tran Mills   if (!aijmkl->sparse_optimized) {
3553fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
3563fa15762SRichard Tran Mills   }
3573fa15762SRichard Tran Mills 
358df555b71SRichard Tran Mills   /* Call MKL SpMV2 executor routine to do the MatMult. */
359df555b71SRichard Tran Mills   stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y);
360*9c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
361df555b71SRichard Tran Mills 
362df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
363df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
364df555b71SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
365df555b71SRichard Tran Mills   PetscFunctionReturn(0);
366df555b71SRichard Tran Mills }
367d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
368df555b71SRichard Tran Mills 
369ff03dc53SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL(Mat A,Vec xx,Vec yy)
370ff03dc53SRichard Tran Mills {
371ff03dc53SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
372ff03dc53SRichard Tran Mills   const PetscScalar *x;
373ff03dc53SRichard Tran Mills   PetscScalar       *y;
374ff03dc53SRichard Tran Mills   const MatScalar   *aa;
375ff03dc53SRichard Tran Mills   PetscErrorCode    ierr;
376ff03dc53SRichard Tran Mills   PetscInt          m=A->rmap->n;
377db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
378db63039fSRichard Tran Mills   PetscScalar       alpha = 1.0;
379db63039fSRichard Tran Mills   PetscScalar       beta = 0.0;
380ff03dc53SRichard Tran Mills   const PetscInt    *aj,*ai;
381db63039fSRichard Tran Mills   char              matdescra[6];
382ff03dc53SRichard Tran Mills 
383ff03dc53SRichard Tran Mills   /* Variables not in MatMultTranspose_SeqAIJ. */
384ff03dc53SRichard Tran Mills   char transa = 't';  /* Used to indicate to MKL that we are computing the transpose product. */
3854a2a386eSRichard Tran Mills 
3864a2a386eSRichard Tran Mills   PetscFunctionBegin;
387969800c5SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
388969800c5SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
3894a2a386eSRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
3904a2a386eSRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
3914a2a386eSRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
3924a2a386eSRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
3934a2a386eSRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
3944a2a386eSRichard Tran Mills 
3954a2a386eSRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
396db63039fSRichard Tran Mills   mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y);
3974a2a386eSRichard Tran Mills 
3984a2a386eSRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
3994a2a386eSRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
4004a2a386eSRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
4014a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
4024a2a386eSRichard Tran Mills }
4034a2a386eSRichard Tran Mills 
404d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
405df555b71SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy)
406df555b71SRichard Tran Mills {
407df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
408df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
409df555b71SRichard Tran Mills   const PetscScalar *x;
410df555b71SRichard Tran Mills   PetscScalar       *y;
411df555b71SRichard Tran Mills   PetscErrorCode    ierr;
4120632b357SRichard Tran Mills   sparse_status_t   stat;
413df555b71SRichard Tran Mills 
414df555b71SRichard Tran Mills   PetscFunctionBegin;
415df555b71SRichard Tran Mills 
41638987b35SRichard Tran Mills   /* If there are no nonzero entries, zero yy and return immediately. */
41738987b35SRichard Tran Mills   if(!a->nz) {
41838987b35SRichard Tran Mills     PetscInt i;
41938987b35SRichard Tran Mills     PetscInt n=A->cmap->n;
42038987b35SRichard Tran Mills     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
42138987b35SRichard Tran Mills     for (i=0; i<n; i++) {
42238987b35SRichard Tran Mills       y[i] = 0.0;
42338987b35SRichard Tran Mills     }
42438987b35SRichard Tran Mills     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
42538987b35SRichard Tran Mills     PetscFunctionReturn(0);
42638987b35SRichard Tran Mills   }
427f36dfe3fSRichard Tran Mills 
428df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
429df555b71SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
430df555b71SRichard Tran Mills 
4313fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
4323fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
4333fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
4343fa15762SRichard Tran Mills   if (!aijmkl->sparse_optimized) {
4353fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
4363fa15762SRichard Tran Mills   }
4373fa15762SRichard Tran Mills 
438df555b71SRichard Tran Mills   /* Call MKL SpMV2 executor routine to do the MatMultTranspose. */
439df555b71SRichard Tran Mills   stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y);
440*9c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
441df555b71SRichard Tran Mills 
442df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
443df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
444df555b71SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
445df555b71SRichard Tran Mills   PetscFunctionReturn(0);
446df555b71SRichard Tran Mills }
447d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
448df555b71SRichard Tran Mills 
4494a2a386eSRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz)
4504a2a386eSRichard Tran Mills {
4514a2a386eSRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
4524a2a386eSRichard Tran Mills   const PetscScalar *x;
4534a2a386eSRichard Tran Mills   PetscScalar       *y,*z;
4544a2a386eSRichard Tran Mills   const MatScalar   *aa;
4554a2a386eSRichard Tran Mills   PetscErrorCode    ierr;
4564a2a386eSRichard Tran Mills   PetscInt          m=A->rmap->n;
457db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
4584a2a386eSRichard Tran Mills   const PetscInt    *aj,*ai;
4594a2a386eSRichard Tran Mills   PetscInt          i;
4604a2a386eSRichard Tran Mills 
461ff03dc53SRichard Tran Mills   /* Variables not in MatMultAdd_SeqAIJ. */
462ff03dc53SRichard Tran Mills   char              transa = 'n';  /* Used to indicate to MKL that we are not computing the transpose product. */
463a84739b8SRichard Tran Mills   PetscScalar       alpha = 1.0;
464db63039fSRichard Tran Mills   PetscScalar       beta;
465a84739b8SRichard Tran Mills   char              matdescra[6];
466ff03dc53SRichard Tran Mills 
467ff03dc53SRichard Tran Mills   PetscFunctionBegin;
468a84739b8SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
469a84739b8SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
470a84739b8SRichard Tran Mills 
471ff03dc53SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
472ff03dc53SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
473ff03dc53SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
474ff03dc53SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
475ff03dc53SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
476ff03dc53SRichard Tran Mills 
477ff03dc53SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
478a84739b8SRichard Tran Mills   if (zz == yy) {
479a84739b8SRichard 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. */
480db63039fSRichard Tran Mills     beta = 1.0;
481db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
482a84739b8SRichard Tran Mills   } else {
483db63039fSRichard Tran Mills     /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z.
484db63039fSRichard Tran Mills      * MKL sparse BLAS does not have a MatMultAdd equivalent. */
485db63039fSRichard Tran Mills     beta = 0.0;
486db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
487ff03dc53SRichard Tran Mills     for (i=0; i<m; i++) {
488ff03dc53SRichard Tran Mills       z[i] += y[i];
489ff03dc53SRichard Tran Mills     }
490a84739b8SRichard Tran Mills   }
491ff03dc53SRichard Tran Mills 
492ff03dc53SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
493ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
494ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
495ff03dc53SRichard Tran Mills   PetscFunctionReturn(0);
496ff03dc53SRichard Tran Mills }
497ff03dc53SRichard Tran Mills 
498d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
499df555b71SRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz)
500df555b71SRichard Tran Mills {
501df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
502df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
503df555b71SRichard Tran Mills   const PetscScalar *x;
504df555b71SRichard Tran Mills   PetscScalar       *y,*z;
505df555b71SRichard Tran Mills   PetscErrorCode    ierr;
506df555b71SRichard Tran Mills   PetscInt          m=A->rmap->n;
507df555b71SRichard Tran Mills   PetscInt          i;
508df555b71SRichard Tran Mills 
509df555b71SRichard Tran Mills   /* Variables not in MatMultAdd_SeqAIJ. */
510df555b71SRichard Tran Mills   sparse_status_t stat = SPARSE_STATUS_SUCCESS;
511df555b71SRichard Tran Mills 
512df555b71SRichard Tran Mills   PetscFunctionBegin;
513df555b71SRichard Tran Mills 
51438987b35SRichard Tran Mills   /* If there are no nonzero entries, set zz = yy and return immediately. */
51538987b35SRichard Tran Mills   if(!a->nz) {
51638987b35SRichard Tran Mills     PetscInt i;
51738987b35SRichard Tran Mills     ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
51838987b35SRichard Tran Mills     for (i=0; i<m; i++) {
51938987b35SRichard Tran Mills       z[i] = y[i];
52038987b35SRichard Tran Mills     }
52138987b35SRichard Tran Mills     ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
52238987b35SRichard Tran Mills     PetscFunctionReturn(0);
52338987b35SRichard Tran Mills   }
524df555b71SRichard Tran Mills 
525df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
526df555b71SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
527df555b71SRichard Tran Mills 
5283fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
5293fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
5303fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
5313fa15762SRichard Tran Mills   if (!aijmkl->sparse_optimized) {
5323fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
5333fa15762SRichard Tran Mills   }
5343fa15762SRichard Tran Mills 
535df555b71SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
536df555b71SRichard Tran Mills   if (zz == yy) {
537df555b71SRichard 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,
538df555b71SRichard Tran Mills      * with alpha and beta both set to 1.0. */
539db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z);
540*9c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
541df555b71SRichard Tran Mills   } else {
542df555b71SRichard 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
543df555b71SRichard Tran Mills      * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */
544db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z);
545*9c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
546df555b71SRichard Tran Mills     for (i=0; i<m; i++) {
547df555b71SRichard Tran Mills       z[i] += y[i];
548df555b71SRichard Tran Mills     }
549df555b71SRichard Tran Mills   }
550df555b71SRichard Tran Mills 
551df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
552df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
553df555b71SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
554df555b71SRichard Tran Mills   PetscFunctionReturn(0);
555df555b71SRichard Tran Mills }
556d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
557df555b71SRichard Tran Mills 
558ff03dc53SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz)
559ff03dc53SRichard Tran Mills {
560ff03dc53SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
561ff03dc53SRichard Tran Mills   const PetscScalar *x;
562ff03dc53SRichard Tran Mills   PetscScalar       *y,*z;
563ff03dc53SRichard Tran Mills   const MatScalar   *aa;
564ff03dc53SRichard Tran Mills   PetscErrorCode    ierr;
565ff03dc53SRichard Tran Mills   PetscInt          m=A->rmap->n;
566db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
567ff03dc53SRichard Tran Mills   const PetscInt    *aj,*ai;
568ff03dc53SRichard Tran Mills   PetscInt          i;
569ff03dc53SRichard Tran Mills 
570ff03dc53SRichard Tran Mills   /* Variables not in MatMultTransposeAdd_SeqAIJ. */
571ff03dc53SRichard Tran Mills   char transa = 't';  /* Used to indicate to MKL that we are computing the transpose product. */
572a84739b8SRichard Tran Mills   PetscScalar       alpha = 1.0;
573db63039fSRichard Tran Mills   PetscScalar       beta;
574a84739b8SRichard Tran Mills   char              matdescra[6];
5754a2a386eSRichard Tran Mills 
5764a2a386eSRichard Tran Mills   PetscFunctionBegin;
577a84739b8SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
578a84739b8SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
579a84739b8SRichard Tran Mills 
5804a2a386eSRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
5814a2a386eSRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
5824a2a386eSRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
5834a2a386eSRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
5844a2a386eSRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
5854a2a386eSRichard Tran Mills 
5864a2a386eSRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
587a84739b8SRichard Tran Mills   if (zz == yy) {
588a84739b8SRichard 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. */
589db63039fSRichard Tran Mills     beta = 1.0;
590969800c5SRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
591a84739b8SRichard Tran Mills   } else {
592db63039fSRichard Tran Mills     /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z.
593db63039fSRichard Tran Mills      * MKL sparse BLAS does not have a MatMultAdd equivalent. */
594db63039fSRichard Tran Mills     beta = 0.0;
595db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
596969800c5SRichard Tran Mills     for (i=0; i<n; i++) {
5974a2a386eSRichard Tran Mills       z[i] += y[i];
5984a2a386eSRichard Tran Mills     }
599a84739b8SRichard Tran Mills   }
6004a2a386eSRichard Tran Mills 
6014a2a386eSRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
6024a2a386eSRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
6034a2a386eSRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
6044a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
6054a2a386eSRichard Tran Mills }
6064a2a386eSRichard Tran Mills 
607d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
608df555b71SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz)
609df555b71SRichard Tran Mills {
610df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
611df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
612df555b71SRichard Tran Mills   const PetscScalar *x;
613df555b71SRichard Tran Mills   PetscScalar       *y,*z;
614df555b71SRichard Tran Mills   PetscErrorCode    ierr;
615969800c5SRichard Tran Mills   PetscInt          n=A->cmap->n;
616df555b71SRichard Tran Mills   PetscInt          i;
617df555b71SRichard Tran Mills 
618df555b71SRichard Tran Mills   /* Variables not in MatMultTransposeAdd_SeqAIJ. */
619df555b71SRichard Tran Mills   sparse_status_t stat = SPARSE_STATUS_SUCCESS;
620df555b71SRichard Tran Mills 
621df555b71SRichard Tran Mills   PetscFunctionBegin;
622df555b71SRichard Tran Mills 
62338987b35SRichard Tran Mills   /* If there are no nonzero entries, set zz = yy and return immediately. */
62438987b35SRichard Tran Mills   if(!a->nz) {
62538987b35SRichard Tran Mills     PetscInt i;
62638987b35SRichard Tran Mills     ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
62738987b35SRichard Tran Mills     for (i=0; i<n; i++) {
62838987b35SRichard Tran Mills       z[i] = y[i];
62938987b35SRichard Tran Mills     }
63038987b35SRichard Tran Mills     ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
63138987b35SRichard Tran Mills     PetscFunctionReturn(0);
63238987b35SRichard Tran Mills   }
633f36dfe3fSRichard Tran Mills 
634df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
635df555b71SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
636df555b71SRichard Tran Mills 
6373fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
6383fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
6393fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
6403fa15762SRichard Tran Mills   if (!aijmkl->sparse_optimized) {
6413fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
6423fa15762SRichard Tran Mills   }
6433fa15762SRichard Tran Mills 
644df555b71SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
645df555b71SRichard Tran Mills   if (zz == yy) {
646df555b71SRichard 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,
647df555b71SRichard Tran Mills      * with alpha and beta both set to 1.0. */
648db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z);
649*9c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
650df555b71SRichard Tran Mills   } else {
651df555b71SRichard 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
652df555b71SRichard Tran Mills      * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */
653db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z);
654*9c46acdfSRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv");
655969800c5SRichard Tran Mills     for (i=0; i<n; i++) {
656df555b71SRichard Tran Mills       z[i] += y[i];
657df555b71SRichard Tran Mills     }
658df555b71SRichard Tran Mills   }
659df555b71SRichard Tran Mills 
660df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
661df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
662df555b71SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
663df555b71SRichard Tran Mills   PetscFunctionReturn(0);
664df555b71SRichard Tran Mills }
665d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
666df555b71SRichard Tran Mills 
66745fbe478SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
668aab60f1bSRichard Tran Mills /* Note that this code currently doesn't actually get used when MatMatMult() is called with MAT_REUSE_MATRIX, because
669aab60f1bSRichard Tran Mills  * the MatMatMult() interface code calls MatMatMultNumeric() in this case.
670aab60f1bSRichard Tran Mills  * MKL has no notion of separately callable symbolic vs. numeric phases of sparse matrix-matrix multiply, so in the
671aab60f1bSRichard Tran Mills  * MAT_REUSE_MATRIX case, the SeqAIJ routines end up being used. Even though this means that the (hopefully more
672aab60f1bSRichard Tran Mills  * optimized) MKL routines do not get used, this probably is best because the MKL routines would waste time re-computing
673aab60f1bSRichard Tran Mills  * the symbolic portion, whereas the native PETSc SeqAIJ routines will avoid this. */
67445fbe478SRichard Tran Mills PetscErrorCode MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat*C)
67545fbe478SRichard Tran Mills {
67645fbe478SRichard Tran Mills   Mat_SeqAIJMKL   *a, *b;
67745fbe478SRichard Tran Mills   sparse_matrix_t csrA, csrB, csrC;
67845fbe478SRichard Tran Mills   PetscErrorCode  ierr;
67945fbe478SRichard Tran Mills   sparse_status_t stat = SPARSE_STATUS_SUCCESS;
68045fbe478SRichard Tran Mills 
68145fbe478SRichard Tran Mills   PetscFunctionBegin;
68245fbe478SRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
68345fbe478SRichard Tran Mills   b = (Mat_SeqAIJMKL*)B->spptr;
68445fbe478SRichard Tran Mills   if (!a->sparse_optimized) {
68545fbe478SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
68645fbe478SRichard Tran Mills   }
68745fbe478SRichard Tran Mills   if (!b->sparse_optimized) {
68845fbe478SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(B);
68945fbe478SRichard Tran Mills   }
69045fbe478SRichard Tran Mills   csrA = a->csrA;
69145fbe478SRichard Tran Mills   csrB = b->csrA;
69245fbe478SRichard Tran Mills 
69345fbe478SRichard Tran Mills   stat = mkl_sparse_spmm(SPARSE_OPERATION_NON_TRANSPOSE,csrA,csrB,&csrC);
694*9c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete sparse matrix-matrix multiply");
69545fbe478SRichard Tran Mills 
6966c87cf42SRichard Tran Mills   ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr);
69745fbe478SRichard Tran Mills 
69845fbe478SRichard Tran Mills   PetscFunctionReturn(0);
69945fbe478SRichard Tran Mills }
70045fbe478SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
70145fbe478SRichard Tran Mills 
702372ec6bbSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
703372ec6bbSRichard Tran Mills PetscErrorCode MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat*C)
704372ec6bbSRichard Tran Mills {
705372ec6bbSRichard Tran Mills   Mat_SeqAIJMKL   *a, *b;
706372ec6bbSRichard Tran Mills   sparse_matrix_t csrA, csrB, csrC;
707372ec6bbSRichard Tran Mills   PetscErrorCode  ierr;
708372ec6bbSRichard Tran Mills   sparse_status_t stat = SPARSE_STATUS_SUCCESS;
709372ec6bbSRichard Tran Mills 
710372ec6bbSRichard Tran Mills   PetscFunctionBegin;
711372ec6bbSRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
712372ec6bbSRichard Tran Mills   b = (Mat_SeqAIJMKL*)B->spptr;
713372ec6bbSRichard Tran Mills   if (!a->sparse_optimized) {
714372ec6bbSRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
715372ec6bbSRichard Tran Mills   }
716372ec6bbSRichard Tran Mills   if (!b->sparse_optimized) {
717372ec6bbSRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(B);
718372ec6bbSRichard Tran Mills   }
719372ec6bbSRichard Tran Mills   csrA = a->csrA;
720372ec6bbSRichard Tran Mills   csrB = b->csrA;
721372ec6bbSRichard Tran Mills 
722372ec6bbSRichard Tran Mills   stat = mkl_sparse_spmm(SPARSE_OPERATION_TRANSPOSE,csrA,csrB,&csrC);
723*9c46acdfSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete sparse matrix-matrix multiply");
724372ec6bbSRichard Tran Mills 
725372ec6bbSRichard Tran Mills   ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr);
726372ec6bbSRichard Tran Mills 
727372ec6bbSRichard Tran Mills   PetscFunctionReturn(0);
728372ec6bbSRichard Tran Mills }
729372ec6bbSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
730372ec6bbSRichard Tran Mills 
73187c2a1d7SRichard Tran Mills PetscErrorCode MatScale_SeqAIJMKL(Mat inA,PetscScalar alpha)
732db63039fSRichard Tran Mills {
733db63039fSRichard Tran Mills   PetscErrorCode ierr;
734db63039fSRichard Tran Mills 
73587c2a1d7SRichard Tran Mills   PetscFunctionBegin;
736db63039fSRichard Tran Mills   ierr = MatScale_SeqAIJ(inA,alpha);CHKERRQ(ierr);
737db63039fSRichard Tran Mills   ierr = MatSeqAIJMKL_create_mkl_handle(inA);CHKERRQ(ierr);
738db63039fSRichard Tran Mills   PetscFunctionReturn(0);
739db63039fSRichard Tran Mills }
740df555b71SRichard Tran Mills 
74187c2a1d7SRichard Tran Mills PetscErrorCode MatDiagonalScale_SeqAIJMKL(Mat A,Vec ll,Vec rr)
74287c2a1d7SRichard Tran Mills {
74387c2a1d7SRichard Tran Mills   PetscErrorCode ierr;
74487c2a1d7SRichard Tran Mills 
74587c2a1d7SRichard Tran Mills   PetscFunctionBegin;
74687c2a1d7SRichard Tran Mills   ierr = MatDiagonalScale_SeqAIJ(A,ll,rr);CHKERRQ(ierr);
74787c2a1d7SRichard Tran Mills   ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr);
74887c2a1d7SRichard Tran Mills   PetscFunctionReturn(0);
74987c2a1d7SRichard Tran Mills }
75087c2a1d7SRichard Tran Mills 
75187c2a1d7SRichard Tran Mills PetscErrorCode MatDiagonalSet_SeqAIJMKL(Mat Y,Vec D,InsertMode is)
75287c2a1d7SRichard Tran Mills {
75387c2a1d7SRichard Tran Mills   PetscErrorCode ierr;
75487c2a1d7SRichard Tran Mills 
75587c2a1d7SRichard Tran Mills   PetscFunctionBegin;
75687c2a1d7SRichard Tran Mills   ierr = MatDiagonalSet_SeqAIJ(Y,D,is);CHKERRQ(ierr);
75787c2a1d7SRichard Tran Mills   ierr = MatSeqAIJMKL_create_mkl_handle(Y);CHKERRQ(ierr);
75887c2a1d7SRichard Tran Mills   PetscFunctionReturn(0);
75987c2a1d7SRichard Tran Mills }
76087c2a1d7SRichard Tran Mills 
76187c2a1d7SRichard Tran Mills PetscErrorCode MatAXPY_SeqAIJMKL(Mat Y,PetscScalar a,Mat X,MatStructure str)
76287c2a1d7SRichard Tran Mills {
76387c2a1d7SRichard Tran Mills   PetscErrorCode ierr;
76487c2a1d7SRichard Tran Mills 
76587c2a1d7SRichard Tran Mills   PetscFunctionBegin;
76687c2a1d7SRichard Tran Mills   ierr = MatAXPY_SeqAIJ(Y,a,X,str);CHKERRQ(ierr);
76787c2a1d7SRichard Tran Mills   if (str == SAME_NONZERO_PATTERN) {
76887c2a1d7SRichard Tran Mills     /* MatAssemblyEnd() is not called if SAME_NONZERO_PATTERN, so we need to force update of the MKL matrix handle. */
76987c2a1d7SRichard Tran Mills     ierr = MatSeqAIJMKL_create_mkl_handle(Y);CHKERRQ(ierr);
77087c2a1d7SRichard Tran Mills   }
77187c2a1d7SRichard Tran Mills   PetscFunctionReturn(0);
77287c2a1d7SRichard Tran Mills }
77387c2a1d7SRichard Tran Mills 
7744a2a386eSRichard Tran Mills /* MatConvert_SeqAIJ_SeqAIJMKL converts a SeqAIJ matrix into a
7754a2a386eSRichard Tran Mills  * SeqAIJMKL matrix.  This routine is called by the MatCreate_SeqMKLAIJ()
7764a2a386eSRichard Tran Mills  * routine, but can also be used to convert an assembled SeqAIJ matrix
7774a2a386eSRichard Tran Mills  * into a SeqAIJMKL one. */
7784a2a386eSRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJMKL(Mat A,MatType type,MatReuse reuse,Mat *newmat)
7794a2a386eSRichard Tran Mills {
7804a2a386eSRichard Tran Mills   PetscErrorCode ierr;
7814a2a386eSRichard Tran Mills   Mat            B = *newmat;
7824a2a386eSRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl;
783c9d46305SRichard Tran Mills   PetscBool      set;
784e9c94282SRichard Tran Mills   PetscBool      sametype;
7854a2a386eSRichard Tran Mills 
7864a2a386eSRichard Tran Mills   PetscFunctionBegin;
7874a2a386eSRichard Tran Mills   if (reuse == MAT_INITIAL_MATRIX) {
7884a2a386eSRichard Tran Mills     ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr);
7894a2a386eSRichard Tran Mills   }
7904a2a386eSRichard Tran Mills 
791e9c94282SRichard Tran Mills   ierr = PetscObjectTypeCompare((PetscObject)A,type,&sametype);CHKERRQ(ierr);
792e9c94282SRichard Tran Mills   if (sametype) PetscFunctionReturn(0);
793e9c94282SRichard Tran Mills 
7944a2a386eSRichard Tran Mills   ierr     = PetscNewLog(B,&aijmkl);CHKERRQ(ierr);
7954a2a386eSRichard Tran Mills   B->spptr = (void*) aijmkl;
7964a2a386eSRichard Tran Mills 
797df555b71SRichard Tran Mills   /* Set function pointers for methods that we inherit from AIJ but override.
798969800c5SRichard Tran Mills    * We also parse some command line options below, since those determine some of the methods we point to. */
7994a2a386eSRichard Tran Mills   B->ops->duplicate        = MatDuplicate_SeqAIJMKL;
8004a2a386eSRichard Tran Mills   B->ops->assemblyend      = MatAssemblyEnd_SeqAIJMKL;
8014a2a386eSRichard Tran Mills   B->ops->destroy          = MatDestroy_SeqAIJMKL;
802c9d46305SRichard Tran Mills 
8034abfa3b3SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_FALSE;
804d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
805d995685eSRichard Tran Mills   aijmkl->no_SpMV2 = PETSC_FALSE;  /* Default to using the SpMV2 routines if our MKL supports them. */
806a8327b06SKarl Rupp #else
807d995685eSRichard Tran Mills   aijmkl->no_SpMV2 = PETSC_TRUE;
808d995685eSRichard Tran Mills #endif
8095b49642aSRichard Tran Mills   aijmkl->eager_inspection = PETSC_FALSE;
8104abfa3b3SRichard Tran Mills 
8114abfa3b3SRichard Tran Mills   /* Parse command line options. */
812c9d46305SRichard Tran Mills   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"AIJMKL Options","Mat");CHKERRQ(ierr);
813c9d46305SRichard Tran Mills   ierr = PetscOptionsBool("-mat_aijmkl_no_spmv2","NoSPMV2","None",(PetscBool)aijmkl->no_SpMV2,(PetscBool*)&aijmkl->no_SpMV2,&set);CHKERRQ(ierr);
8145b49642aSRichard Tran Mills   ierr = PetscOptionsBool("-mat_aijmkl_eager_inspection","Eager Inspection","None",(PetscBool)aijmkl->eager_inspection,(PetscBool*)&aijmkl->eager_inspection,&set);CHKERRQ(ierr);
815c9d46305SRichard Tran Mills   ierr = PetscOptionsEnd();CHKERRQ(ierr);
816d995685eSRichard Tran Mills #ifndef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
817d995685eSRichard Tran Mills   if(!aijmkl->no_SpMV2) {
818d995685eSRichard 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");
819d995685eSRichard Tran Mills     aijmkl->no_SpMV2 = PETSC_TRUE;
820d995685eSRichard Tran Mills   }
821d995685eSRichard Tran Mills #endif
822c9d46305SRichard Tran Mills 
823c9d46305SRichard Tran Mills   if(!aijmkl->no_SpMV2) {
824d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
825df555b71SRichard Tran Mills     B->ops->mult             = MatMult_SeqAIJMKL_SpMV2;
826969800c5SRichard Tran Mills     B->ops->multtranspose    = MatMultTranspose_SeqAIJMKL_SpMV2;
827df555b71SRichard Tran Mills     B->ops->multadd          = MatMultAdd_SeqAIJMKL_SpMV2;
828969800c5SRichard Tran Mills     B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL_SpMV2;
82945fbe478SRichard Tran Mills     B->ops->matmult          = MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2;
830a557fde5SRichard Tran Mills     B->ops->transposematmult = MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2;
831d995685eSRichard Tran Mills #endif
832c9d46305SRichard Tran Mills   } else {
8334a2a386eSRichard Tran Mills     B->ops->mult             = MatMult_SeqAIJMKL;
834969800c5SRichard Tran Mills     B->ops->multtranspose    = MatMultTranspose_SeqAIJMKL;
8354a2a386eSRichard Tran Mills     B->ops->multadd          = MatMultAdd_SeqAIJMKL;
836969800c5SRichard Tran Mills     B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL;
837c9d46305SRichard Tran Mills   }
8384a2a386eSRichard Tran Mills 
839db63039fSRichard Tran Mills   B->ops->scale              = MatScale_SeqAIJMKL;
84087c2a1d7SRichard Tran Mills   B->ops->diagonalscale      = MatDiagonalScale_SeqAIJMKL;
84187c2a1d7SRichard Tran Mills   B->ops->diagonalset        = MatDiagonalSet_SeqAIJMKL;
84287c2a1d7SRichard Tran Mills   B->ops->axpy               = MatAXPY_SeqAIJMKL;
843db63039fSRichard Tran Mills 
844db63039fSRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatScale_SeqAIJMKL_C",MatScale_SeqAIJMKL);CHKERRQ(ierr);
8454a2a386eSRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",MatConvert_SeqAIJMKL_SeqAIJ);CHKERRQ(ierr);
846e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaijmkl_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
847e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaijmkl_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
848e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaijmkl_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
84945fbe478SRichard Tran Mills   if(!aijmkl->no_SpMV2) {
85045fbe478SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
85145fbe478SRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqaijmkl_seqaijmkl_C",MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr);
852372ec6bbSRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatTransposeMatMult_seqaijmkl_seqaijmkl_C",MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr);
85345fbe478SRichard Tran Mills #endif
85445fbe478SRichard Tran Mills   }
8554a2a386eSRichard Tran Mills 
8564a2a386eSRichard Tran Mills   ierr    = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJMKL);CHKERRQ(ierr);
8574a2a386eSRichard Tran Mills   *newmat = B;
8584a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
8594a2a386eSRichard Tran Mills }
8604a2a386eSRichard Tran Mills 
8614a2a386eSRichard Tran Mills /*@C
8624a2a386eSRichard Tran Mills    MatCreateSeqAIJMKL - Creates a sparse matrix of type SEQAIJMKL.
8634a2a386eSRichard Tran Mills    This type inherits from AIJ and is largely identical, but uses sparse BLAS
8644a2a386eSRichard Tran Mills    routines from Intel MKL whenever possible.
8653af10221SRichard Tran Mills    MatMult, MatMultAdd, MatMultTranspose, MatMultTransposeAdd, MatMatMult, and MatTransposeMatMult
86690147e49SRichard Tran Mills    operations are currently supported.
86790147e49SRichard Tran Mills    If the installed version of MKL supports the "SpMV2" sparse
86890147e49SRichard Tran Mills    inspector-executor routines, then those are used by default.
86990147e49SRichard Tran Mills 
8704a2a386eSRichard Tran Mills    Collective on MPI_Comm
8714a2a386eSRichard Tran Mills 
8724a2a386eSRichard Tran Mills    Input Parameters:
8734a2a386eSRichard Tran Mills +  comm - MPI communicator, set to PETSC_COMM_SELF
8744a2a386eSRichard Tran Mills .  m - number of rows
8754a2a386eSRichard Tran Mills .  n - number of columns
8764a2a386eSRichard Tran Mills .  nz - number of nonzeros per row (same for all rows)
8774a2a386eSRichard Tran Mills -  nnz - array containing the number of nonzeros in the various rows
8784a2a386eSRichard Tran Mills          (possibly different for each row) or NULL
8794a2a386eSRichard Tran Mills 
8804a2a386eSRichard Tran Mills    Output Parameter:
8814a2a386eSRichard Tran Mills .  A - the matrix
8824a2a386eSRichard Tran Mills 
88390147e49SRichard Tran Mills    Options Database Keys:
88466b7eeb6SRichard Tran Mills +  -mat_aijmkl_no_spmv2 - disable use of the SpMV2 inspector-executor routines
88566b7eeb6SRichard 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
88690147e49SRichard Tran Mills 
8874a2a386eSRichard Tran Mills    Notes:
8884a2a386eSRichard Tran Mills    If nnz is given then nz is ignored
8894a2a386eSRichard Tran Mills 
8904a2a386eSRichard Tran Mills    Level: intermediate
8914a2a386eSRichard Tran Mills 
89290147e49SRichard Tran Mills .keywords: matrix, MKL, sparse, parallel
8934a2a386eSRichard Tran Mills 
8944a2a386eSRichard Tran Mills .seealso: MatCreate(), MatCreateMPIAIJMKL(), MatSetValues()
8954a2a386eSRichard Tran Mills @*/
8964a2a386eSRichard Tran Mills PetscErrorCode  MatCreateSeqAIJMKL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
8974a2a386eSRichard Tran Mills {
8984a2a386eSRichard Tran Mills   PetscErrorCode ierr;
8994a2a386eSRichard Tran Mills 
9004a2a386eSRichard Tran Mills   PetscFunctionBegin;
9014a2a386eSRichard Tran Mills   ierr = MatCreate(comm,A);CHKERRQ(ierr);
9024a2a386eSRichard Tran Mills   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
9034a2a386eSRichard Tran Mills   ierr = MatSetType(*A,MATSEQAIJMKL);CHKERRQ(ierr);
9044a2a386eSRichard Tran Mills   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
9054a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
9064a2a386eSRichard Tran Mills }
9074a2a386eSRichard Tran Mills 
9084a2a386eSRichard Tran Mills PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJMKL(Mat A)
9094a2a386eSRichard Tran Mills {
9104a2a386eSRichard Tran Mills   PetscErrorCode ierr;
9114a2a386eSRichard Tran Mills 
9124a2a386eSRichard Tran Mills   PetscFunctionBegin;
9134a2a386eSRichard Tran Mills   ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr);
9144a2a386eSRichard Tran Mills   ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr);
9154a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
9164a2a386eSRichard Tran Mills }
917