xref: /petsc/src/mat/impls/aij/seq/aijmkl/aijmkl.c (revision f3fd175839bd4670c4525ba579f9e8a22bd71c7b)
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;
33a8327b06SKarl Rupp #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
344a2a386eSRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
35a8327b06SKarl Rupp #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   if(!aijmkl->no_SpMV2) {
6245fbe478SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
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 #endif
6645fbe478SRichard Tran Mills   }
67e9c94282SRichard Tran Mills 
684abfa3b3SRichard Tran Mills   /* Free everything in the Mat_SeqAIJMKL data structure. Currently, this
69e9c94282SRichard Tran Mills    * simply involves destroying the MKL sparse matrix handle and then freeing
70e9c94282SRichard Tran Mills    * the spptr pointer. */
714abfa3b3SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
72a8327b06SKarl Rupp   if (reuse == MAT_INITIAL_MATRIX) aijmkl = (Mat_SeqAIJMKL*)B->spptr;
73a8327b06SKarl Rupp 
744abfa3b3SRichard Tran Mills   if (aijmkl->sparse_optimized) {
750632b357SRichard Tran Mills     sparse_status_t stat;
764abfa3b3SRichard Tran Mills     stat = mkl_sparse_destroy(aijmkl->csrA);
774abfa3b3SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) {
784abfa3b3SRichard Tran Mills       PetscFunctionReturn(PETSC_ERR_LIB);
794abfa3b3SRichard Tran Mills     }
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. */
1024abfa3b3SRichard Tran Mills #ifdef 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);
1064abfa3b3SRichard Tran Mills       if (stat != SPARSE_STATUS_SUCCESS) {
1074abfa3b3SRichard Tran Mills         PetscFunctionReturn(PETSC_ERR_LIB);
1084abfa3b3SRichard Tran Mills       }
1094abfa3b3SRichard Tran Mills     }
1104abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
1114a2a386eSRichard Tran Mills     ierr = PetscFree(A->spptr);CHKERRQ(ierr);
112e9c94282SRichard Tran Mills   }
1134a2a386eSRichard Tran Mills 
1144a2a386eSRichard Tran Mills   /* Change the type of A back to SEQAIJ and use MatDestroy_SeqAIJ()
1154a2a386eSRichard Tran Mills    * to destroy everything that remains. */
1164a2a386eSRichard Tran Mills   ierr = PetscObjectChangeTypeName((PetscObject)A, MATSEQAIJ);CHKERRQ(ierr);
1174a2a386eSRichard Tran Mills   /* Note that I don't call MatSetType().  I believe this is because that
1184a2a386eSRichard Tran Mills    * is only to be called when *building* a matrix.  I could be wrong, but
1194a2a386eSRichard Tran Mills    * that is how things work for the SuperLU matrix class. */
1204a2a386eSRichard Tran Mills   ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr);
1214a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
1224a2a386eSRichard Tran Mills }
1234a2a386eSRichard Tran Mills 
1245b49642aSRichard Tran Mills /* MatSeqAIJKL_create_mkl_handle(), if called with an AIJMKL matrix that has not had mkl_sparse_optimize() called for it,
1255b49642aSRichard Tran Mills  * creates an MKL sparse matrix handle from the AIJ arrays and calls mkl_sparse_optimize().
1265b49642aSRichard Tran Mills  * If called with an AIJMKL matrix for which aijmkl->sparse_optimized == PETSC_TRUE, then it destroys the old matrix
1275b49642aSRichard Tran Mills  * handle, creates a new one, and then calls mkl_sparse_optimize().
1285b49642aSRichard Tran Mills  * Although in normal MKL usage it is possible to have a valid matrix handle on which mkl_sparse_optimize() has not been
1295b49642aSRichard Tran Mills  * called, for AIJMKL the handle creation and optimization step always occur together, so we don't handle the case of
1305b49642aSRichard Tran Mills  * an unoptimized matrix handle here. */
1316e369cd5SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_mkl_handle(Mat A)
1324a2a386eSRichard Tran Mills {
1336e369cd5SRichard Tran Mills #ifndef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
1346e369cd5SRichard Tran Mills   /* If the MKL library does not have mkl_sparse_optimize(), then this routine
1356e369cd5SRichard Tran Mills    * does nothing. We make it callable anyway in this case because it cuts
1366e369cd5SRichard Tran Mills    * down on littering the code with #ifdefs. */
13745fbe478SRichard Tran Mills   PetscFunctionBegin;
1386e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
1396e369cd5SRichard Tran Mills #else
140a8327b06SKarl Rupp   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
141a8327b06SKarl Rupp   Mat_SeqAIJMKL   *aijmkl = (Mat_SeqAIJMKL*)A->spptr;
142a8327b06SKarl Rupp   PetscInt        m,n;
143a8327b06SKarl Rupp   MatScalar       *aa;
144a8327b06SKarl Rupp   PetscInt        *aj,*ai;
1456e369cd5SRichard Tran Mills   sparse_status_t stat;
1464a2a386eSRichard Tran Mills 
147a8327b06SKarl Rupp   PetscFunctionBegin;
1486e369cd5SRichard Tran Mills   if (aijmkl->no_SpMV2) PetscFunctionReturn(0);
1496e369cd5SRichard Tran Mills 
1500632b357SRichard Tran Mills   if (aijmkl->sparse_optimized) {
1510632b357SRichard Tran Mills     /* Matrix has been previously assembled and optimized. Must destroy old
1520632b357SRichard Tran Mills      * matrix handle before running the optimization step again. */
1530632b357SRichard Tran Mills     stat = mkl_sparse_destroy(aijmkl->csrA);
1540632b357SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) {
1550632b357SRichard Tran Mills       PetscFunctionReturn(PETSC_ERR_LIB);
1560632b357SRichard Tran Mills     }
1570632b357SRichard Tran Mills   }
1588d3fe1b0SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_FALSE;
1596e369cd5SRichard Tran Mills 
160c9d46305SRichard Tran Mills   /* Now perform the SpMV2 setup and matrix optimization. */
161df555b71SRichard Tran Mills   aijmkl->descr.type        = SPARSE_MATRIX_TYPE_GENERAL;
162df555b71SRichard Tran Mills   aijmkl->descr.mode        = SPARSE_FILL_MODE_LOWER;
163df555b71SRichard Tran Mills   aijmkl->descr.diag        = SPARSE_DIAG_NON_UNIT;
16458678438SRichard Tran Mills   m = A->rmap->n;
16558678438SRichard Tran Mills   n = A->cmap->n;
166df555b71SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
167df555b71SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
168df555b71SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
16980095d54SIrina Sokolova   if ((a->nz!=0) & !(A->structure_only)) {
1708d3fe1b0SRichard Tran Mills     /* Create a new, optimized sparse matrix handle only if the matrix has nonzero entries.
1718d3fe1b0SRichard Tran Mills      * The MKL sparse-inspector executor routines don't like being passed an empty matrix. */
17258678438SRichard Tran Mills     stat = mkl_sparse_x_create_csr(&aijmkl->csrA,SPARSE_INDEX_BASE_ZERO,m,n,ai,ai+1,aj,aa);
173df555b71SRichard Tran Mills     stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000);
174df555b71SRichard Tran Mills     stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE);
175df555b71SRichard Tran Mills     stat = mkl_sparse_optimize(aijmkl->csrA);
176df555b71SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) {
177f68ad4bdSRichard Tran Mills       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to create matrix handle/complete mkl_sparse_optimize");
178df555b71SRichard Tran Mills       PetscFunctionReturn(PETSC_ERR_LIB);
179df555b71SRichard Tran Mills     }
1804abfa3b3SRichard Tran Mills     aijmkl->sparse_optimized = PETSC_TRUE;
181c9d46305SRichard Tran Mills   }
1826e369cd5SRichard Tran Mills 
1836e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
184d995685eSRichard Tran Mills #endif
1856e369cd5SRichard Tran Mills }
1866e369cd5SRichard Tran Mills 
18719afcda9SRichard Tran Mills /* MatSeqAIJMKL_create_from_mkl_handle() creates a sequential AIJMKL matrix from an MKL sparse matrix handle.
18819afcda9SRichard Tran Mills  * We need this to implement MatMatMult() using the MKL inspector-executor routines, which return an (unoptimized)
1896c87cf42SRichard Tran Mills  * matrix handle.
1906c87cf42SRichard Tran Mills  * Note: This routine supports replacing the numerical values in an existing matrix that has the same sparsity
1916c87cf42SRichard Tran Mills  * structure as in the MKL handle. However, this code currently doesn't actually get used when MatMatMult()
1926c87cf42SRichard Tran Mills  * is called with MAT_REUSE_MATRIX, because the MatMatMult() interface code calls MatMatMultNumeric() in this case.
1936c87cf42SRichard Tran Mills  * MKL has no notion of separately callable symbolic vs. numeric phases of sparse matrix-matrix multiply, so in the
1946c87cf42SRichard Tran Mills  * MAT_REUSE_MATRIX case, the SeqAIJ routines end up being used. Even though this means that the (hopefully more
1956c87cf42SRichard Tran Mills  * optimized) MKL routines do not get used, this probably is best because the MKL routines would waste time re-computing
1966c87cf42SRichard Tran Mills  * the symbolic portion, whereas the native PETSc SeqAIJ routines will avoid this. */
19719afcda9SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
1986c87cf42SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_from_mkl_handle(MPI_Comm comm,sparse_matrix_t csrA,MatReuse reuse,Mat *mat)
19919afcda9SRichard Tran Mills {
20019afcda9SRichard Tran Mills   PetscErrorCode ierr;
20119afcda9SRichard Tran Mills   sparse_status_t stat;
20219afcda9SRichard Tran Mills   sparse_index_base_t indexing;
20319afcda9SRichard Tran Mills   PetscInt nrows, ncols;
20445fbe478SRichard Tran Mills   PetscInt *aj,*ai,*dummy;
20519afcda9SRichard Tran Mills   MatScalar *aa;
20619afcda9SRichard Tran Mills   Mat A;
2076c87cf42SRichard Tran Mills   Mat_SeqAIJ *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);
21219afcda9SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) {
21319afcda9SRichard Tran Mills     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_x_export_csr()");
21419afcda9SRichard Tran Mills     PetscFunctionReturn(PETSC_ERR_LIB);
21519afcda9SRichard Tran Mills   }
2166c87cf42SRichard Tran Mills 
2176c87cf42SRichard Tran Mills   if (reuse == MAT_INITIAL_MATRIX) {
21819afcda9SRichard Tran Mills     ierr = MatCreate(comm,&A);CHKERRQ(ierr);
21919afcda9SRichard Tran Mills     ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr);
22045fbe478SRichard Tran Mills     ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,nrows,ncols);CHKERRQ(ierr);
22119afcda9SRichard Tran Mills     ierr = MatSeqAIJSetPreallocationCSR(A,ai,aj,aa);CHKERRQ(ierr);
22219afcda9SRichard Tran Mills 
22319afcda9SRichard Tran Mills     /* We now have an assembled sequential AIJ matrix created from copies of the exported arrays from the MKL matrix handle.
22419afcda9SRichard Tran Mills      * Now turn it into a MATSEQAIJMKL. */
22519afcda9SRichard Tran Mills     ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr);
2266c87cf42SRichard Tran Mills   } else {
2276c87cf42SRichard Tran Mills     A = *mat;
2286c87cf42SRichard Tran Mills   }
2296c87cf42SRichard Tran Mills 
2306c87cf42SRichard Tran Mills   a = (Mat_SeqAIJ*)A->data;
23119afcda9SRichard Tran Mills   aijmkl = (Mat_SeqAIJMKL*) A->spptr;
2326c87cf42SRichard Tran Mills 
2336c87cf42SRichard Tran Mills   if (reuse == MAT_REUSE_MATRIX) {
2346c87cf42SRichard Tran Mills     /* Need to destroy old MKL handle. */
2356c87cf42SRichard Tran Mills     stat = mkl_sparse_destroy(aijmkl->csrA);
2366c87cf42SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) {
2376c87cf42SRichard Tran Mills       PetscFunctionReturn(PETSC_ERR_LIB);
2386c87cf42SRichard Tran Mills     }
2396c87cf42SRichard Tran Mills 
2406c87cf42SRichard Tran Mills     /* The new matrix is supposed to have the same sparsity pattern, so copy only the array of numerical values. */
2416c87cf42SRichard Tran Mills     ierr = PetscMemcpy(a->a,aa,sizeof(MatScalar)*a->nz);CHKERRQ(ierr);
2426c87cf42SRichard Tran Mills   }
24319afcda9SRichard Tran Mills   aijmkl->csrA = csrA;
2446c87cf42SRichard Tran Mills 
24519afcda9SRichard Tran Mills   /* The below code duplicates much of what is in MatSeqAIJKL_create_mkl_handle(). I dislike this code duplication, but
24619afcda9SRichard Tran Mills    * MatSeqAIJMKL_create_mkl_handle() cannot be used because we don't need to create a handle -- we've already got one,
24719afcda9SRichard Tran Mills    * and just need to be able to run the MKL optimization step. */
248*f3fd1758SRichard Tran Mills   aijmkl->descr.type        = SPARSE_MATRIX_TYPE_GENERAL;
249*f3fd1758SRichard Tran Mills   aijmkl->descr.mode        = SPARSE_FILL_MODE_LOWER;
250*f3fd1758SRichard Tran Mills   aijmkl->descr.diag        = SPARSE_DIAG_NON_UNIT;
25119afcda9SRichard Tran Mills   stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000);
25219afcda9SRichard Tran Mills   stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE);
25319afcda9SRichard Tran Mills   stat = mkl_sparse_optimize(aijmkl->csrA);
25419afcda9SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) {
25519afcda9SRichard Tran Mills     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set hints/complete mkl_sparse_optimize");
25619afcda9SRichard Tran Mills     PetscFunctionReturn(PETSC_ERR_LIB);
25719afcda9SRichard Tran Mills   }
25819afcda9SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_TRUE;
25919afcda9SRichard Tran Mills 
26019afcda9SRichard Tran Mills   *mat = A;
26119afcda9SRichard Tran Mills   PetscFunctionReturn(0);
26219afcda9SRichard Tran Mills }
26319afcda9SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
26419afcda9SRichard Tran Mills 
2656e369cd5SRichard Tran Mills PetscErrorCode MatDuplicate_SeqAIJMKL(Mat A, MatDuplicateOption op, Mat *M)
2666e369cd5SRichard Tran Mills {
2676e369cd5SRichard Tran Mills   PetscErrorCode ierr;
2686e369cd5SRichard Tran Mills   Mat_SeqAIJMKL *aijmkl;
2696e369cd5SRichard Tran Mills   Mat_SeqAIJMKL *aijmkl_dest;
2706e369cd5SRichard Tran Mills 
2716e369cd5SRichard Tran Mills   PetscFunctionBegin;
2726e369cd5SRichard Tran Mills   ierr = MatDuplicate_SeqAIJ(A,op,M);CHKERRQ(ierr);
2736e369cd5SRichard Tran Mills   aijmkl      = (Mat_SeqAIJMKL*) A->spptr;
2746e369cd5SRichard Tran Mills   aijmkl_dest = (Mat_SeqAIJMKL*) (*M)->spptr;
2756e369cd5SRichard Tran Mills   ierr = PetscMemcpy(aijmkl_dest,aijmkl,sizeof(Mat_SeqAIJMKL));CHKERRQ(ierr);
2766e369cd5SRichard Tran Mills   aijmkl_dest->sparse_optimized = PETSC_FALSE;
2775b49642aSRichard Tran Mills   if (aijmkl->eager_inspection) {
2786e369cd5SRichard Tran Mills     ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr);
2795b49642aSRichard Tran Mills   }
2806e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
2816e369cd5SRichard Tran Mills }
2826e369cd5SRichard Tran Mills 
2836e369cd5SRichard Tran Mills PetscErrorCode MatAssemblyEnd_SeqAIJMKL(Mat A, MatAssemblyType mode)
2846e369cd5SRichard Tran Mills {
2856e369cd5SRichard Tran Mills   PetscErrorCode  ierr;
2866e369cd5SRichard Tran Mills   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
2875b49642aSRichard Tran Mills   Mat_SeqAIJMKL *aijmkl;
2886e369cd5SRichard Tran Mills 
2896e369cd5SRichard Tran Mills   PetscFunctionBegin;
2906e369cd5SRichard Tran Mills   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
2916e369cd5SRichard Tran Mills 
2926e369cd5SRichard Tran Mills   /* Since a MATSEQAIJMKL matrix is really just a MATSEQAIJ with some
2936e369cd5SRichard Tran Mills    * extra information and some different methods, call the AssemblyEnd
2946e369cd5SRichard Tran Mills    * routine for a MATSEQAIJ.
2956e369cd5SRichard Tran Mills    * I'm not sure if this is the best way to do this, but it avoids
296d96e85feSRichard Tran Mills    * a lot of code duplication. */
2976e369cd5SRichard Tran Mills   a->inode.use = PETSC_FALSE;  /* Must disable: otherwise the MKL routines won't get used. */
2986e369cd5SRichard Tran Mills   ierr = MatAssemblyEnd_SeqAIJ(A, mode);CHKERRQ(ierr);
2996e369cd5SRichard Tran Mills 
3005b49642aSRichard Tran Mills   /* If the user has requested "eager" inspection, create the optimized MKL sparse handle (if needed; the function checks).
3015b49642aSRichard Tran Mills    * (The default is to do "lazy" inspection, deferring this until something like MatMult() is called.) */
3025b49642aSRichard Tran Mills   aijmkl = (Mat_SeqAIJMKL*) A->spptr;
3035b49642aSRichard Tran Mills   if (aijmkl->eager_inspection) {
3046e369cd5SRichard Tran Mills     ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr);
3055b49642aSRichard Tran Mills   }
306df555b71SRichard Tran Mills 
3074a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
3084a2a386eSRichard Tran Mills }
3094a2a386eSRichard Tran Mills 
3104a2a386eSRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL(Mat A,Vec xx,Vec yy)
3114a2a386eSRichard Tran Mills {
3124a2a386eSRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
3134a2a386eSRichard Tran Mills   const PetscScalar *x;
3144a2a386eSRichard Tran Mills   PetscScalar       *y;
3154a2a386eSRichard Tran Mills   const MatScalar   *aa;
3164a2a386eSRichard Tran Mills   PetscErrorCode    ierr;
3174a2a386eSRichard Tran Mills   PetscInt          m=A->rmap->n;
318db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
319db63039fSRichard Tran Mills   PetscScalar       alpha = 1.0;
320db63039fSRichard Tran Mills   PetscScalar       beta = 0.0;
3214a2a386eSRichard Tran Mills   const PetscInt    *aj,*ai;
322db63039fSRichard Tran Mills   char              matdescra[6];
323db63039fSRichard Tran Mills 
3244a2a386eSRichard Tran Mills 
3254a2a386eSRichard Tran Mills   /* Variables not in MatMult_SeqAIJ. */
326ff03dc53SRichard Tran Mills   char transa = 'n';  /* Used to indicate to MKL that we are not computing the transpose product. */
327ff03dc53SRichard Tran Mills 
328ff03dc53SRichard Tran Mills   PetscFunctionBegin;
329db63039fSRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
330db63039fSRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
331ff03dc53SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
332ff03dc53SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
333ff03dc53SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
334ff03dc53SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
335ff03dc53SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
336ff03dc53SRichard Tran Mills 
337ff03dc53SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
338db63039fSRichard Tran Mills   mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y);
339ff03dc53SRichard Tran Mills 
340ff03dc53SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
341ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
342ff03dc53SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
343ff03dc53SRichard Tran Mills   PetscFunctionReturn(0);
344ff03dc53SRichard Tran Mills }
345ff03dc53SRichard Tran Mills 
346d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
347df555b71SRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy)
348df555b71SRichard Tran Mills {
349df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
350df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
351df555b71SRichard Tran Mills   const PetscScalar *x;
352df555b71SRichard Tran Mills   PetscScalar       *y;
353df555b71SRichard Tran Mills   PetscErrorCode    ierr;
354df555b71SRichard Tran Mills   sparse_status_t stat = SPARSE_STATUS_SUCCESS;
355df555b71SRichard Tran Mills 
356df555b71SRichard Tran Mills   PetscFunctionBegin;
357df555b71SRichard Tran Mills 
35838987b35SRichard Tran Mills   /* If there are no nonzero entries, zero yy and return immediately. */
35938987b35SRichard Tran Mills   if(!a->nz) {
36038987b35SRichard Tran Mills     PetscInt i;
36138987b35SRichard Tran Mills     PetscInt m=A->rmap->n;
36238987b35SRichard Tran Mills     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
36338987b35SRichard Tran Mills     for (i=0; i<m; i++) {
36438987b35SRichard Tran Mills       y[i] = 0.0;
36538987b35SRichard Tran Mills     }
36638987b35SRichard Tran Mills     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
36738987b35SRichard Tran Mills     PetscFunctionReturn(0);
36838987b35SRichard Tran Mills   }
369f36dfe3fSRichard Tran Mills 
370df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
371df555b71SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
372df555b71SRichard Tran Mills 
3733fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
3743fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
3753fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
3763fa15762SRichard Tran Mills   if (!aijmkl->sparse_optimized) {
3773fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
3783fa15762SRichard Tran Mills   }
3793fa15762SRichard Tran Mills 
380df555b71SRichard Tran Mills   /* Call MKL SpMV2 executor routine to do the MatMult. */
381df555b71SRichard Tran Mills   stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y);
382df555b71SRichard Tran Mills 
383df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
384df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
385df555b71SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
386df555b71SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) {
387df555b71SRichard Tran Mills     PetscFunctionReturn(PETSC_ERR_LIB);
388df555b71SRichard Tran Mills   }
389df555b71SRichard Tran Mills   PetscFunctionReturn(0);
390df555b71SRichard Tran Mills }
391d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
392df555b71SRichard Tran Mills 
393ff03dc53SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL(Mat A,Vec xx,Vec yy)
394ff03dc53SRichard Tran Mills {
395ff03dc53SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
396ff03dc53SRichard Tran Mills   const PetscScalar *x;
397ff03dc53SRichard Tran Mills   PetscScalar       *y;
398ff03dc53SRichard Tran Mills   const MatScalar   *aa;
399ff03dc53SRichard Tran Mills   PetscErrorCode    ierr;
400ff03dc53SRichard Tran Mills   PetscInt          m=A->rmap->n;
401db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
402db63039fSRichard Tran Mills   PetscScalar       alpha = 1.0;
403db63039fSRichard Tran Mills   PetscScalar       beta = 0.0;
404ff03dc53SRichard Tran Mills   const PetscInt    *aj,*ai;
405db63039fSRichard Tran Mills   char              matdescra[6];
406ff03dc53SRichard Tran Mills 
407ff03dc53SRichard Tran Mills   /* Variables not in MatMultTranspose_SeqAIJ. */
408ff03dc53SRichard Tran Mills   char transa = 't';  /* Used to indicate to MKL that we are computing the transpose product. */
4094a2a386eSRichard Tran Mills 
4104a2a386eSRichard Tran Mills   PetscFunctionBegin;
411969800c5SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
412969800c5SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
4134a2a386eSRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
4144a2a386eSRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
4154a2a386eSRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
4164a2a386eSRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
4174a2a386eSRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
4184a2a386eSRichard Tran Mills 
4194a2a386eSRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
420db63039fSRichard Tran Mills   mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y);
4214a2a386eSRichard Tran Mills 
4224a2a386eSRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
4234a2a386eSRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
4244a2a386eSRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
4254a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
4264a2a386eSRichard Tran Mills }
4274a2a386eSRichard Tran Mills 
428d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
429df555b71SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy)
430df555b71SRichard Tran Mills {
431df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
432df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
433df555b71SRichard Tran Mills   const PetscScalar *x;
434df555b71SRichard Tran Mills   PetscScalar       *y;
435df555b71SRichard Tran Mills   PetscErrorCode    ierr;
4360632b357SRichard Tran Mills   sparse_status_t   stat;
437df555b71SRichard Tran Mills 
438df555b71SRichard Tran Mills   PetscFunctionBegin;
439df555b71SRichard Tran Mills 
44038987b35SRichard Tran Mills   /* If there are no nonzero entries, zero yy and return immediately. */
44138987b35SRichard Tran Mills   if(!a->nz) {
44238987b35SRichard Tran Mills     PetscInt i;
44338987b35SRichard Tran Mills     PetscInt n=A->cmap->n;
44438987b35SRichard Tran Mills     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
44538987b35SRichard Tran Mills     for (i=0; i<n; i++) {
44638987b35SRichard Tran Mills       y[i] = 0.0;
44738987b35SRichard Tran Mills     }
44838987b35SRichard Tran Mills     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
44938987b35SRichard Tran Mills     PetscFunctionReturn(0);
45038987b35SRichard Tran Mills   }
451f36dfe3fSRichard Tran Mills 
452df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
453df555b71SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
454df555b71SRichard Tran Mills 
4553fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
4563fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
4573fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
4583fa15762SRichard Tran Mills   if (!aijmkl->sparse_optimized) {
4593fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
4603fa15762SRichard Tran Mills   }
4613fa15762SRichard Tran Mills 
462df555b71SRichard Tran Mills   /* Call MKL SpMV2 executor routine to do the MatMultTranspose. */
463df555b71SRichard Tran Mills   stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y);
464df555b71SRichard Tran Mills 
465df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
466df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
467df555b71SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
468df555b71SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) {
469df555b71SRichard Tran Mills     PetscFunctionReturn(PETSC_ERR_LIB);
470df555b71SRichard Tran Mills   }
471df555b71SRichard Tran Mills   PetscFunctionReturn(0);
472df555b71SRichard Tran Mills }
473d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
474df555b71SRichard Tran Mills 
4754a2a386eSRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz)
4764a2a386eSRichard Tran Mills {
4774a2a386eSRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
4784a2a386eSRichard Tran Mills   const PetscScalar *x;
4794a2a386eSRichard Tran Mills   PetscScalar       *y,*z;
4804a2a386eSRichard Tran Mills   const MatScalar   *aa;
4814a2a386eSRichard Tran Mills   PetscErrorCode    ierr;
4824a2a386eSRichard Tran Mills   PetscInt          m=A->rmap->n;
483db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
4844a2a386eSRichard Tran Mills   const PetscInt    *aj,*ai;
4854a2a386eSRichard Tran Mills   PetscInt          i;
4864a2a386eSRichard Tran Mills 
487ff03dc53SRichard Tran Mills   /* Variables not in MatMultAdd_SeqAIJ. */
488ff03dc53SRichard Tran Mills   char transa = 'n';  /* Used to indicate to MKL that we are not computing the transpose product. */
489a84739b8SRichard Tran Mills   PetscScalar       alpha = 1.0;
490db63039fSRichard Tran Mills   PetscScalar       beta;
491a84739b8SRichard Tran Mills   char              matdescra[6];
492ff03dc53SRichard Tran Mills 
493ff03dc53SRichard Tran Mills   PetscFunctionBegin;
494a84739b8SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
495a84739b8SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
496a84739b8SRichard Tran Mills 
497ff03dc53SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
498ff03dc53SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
499ff03dc53SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
500ff03dc53SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
501ff03dc53SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
502ff03dc53SRichard Tran Mills 
503ff03dc53SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
504a84739b8SRichard Tran Mills   if (zz == yy) {
505a84739b8SRichard 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. */
506db63039fSRichard Tran Mills     beta = 1.0;
507db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
508a84739b8SRichard Tran Mills   } else {
509db63039fSRichard Tran Mills     /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z.
510db63039fSRichard Tran Mills      * MKL sparse BLAS does not have a MatMultAdd equivalent. */
511db63039fSRichard Tran Mills     beta = 0.0;
512db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
513ff03dc53SRichard Tran Mills     for (i=0; i<m; i++) {
514ff03dc53SRichard Tran Mills       z[i] += y[i];
515ff03dc53SRichard Tran Mills     }
516a84739b8SRichard Tran Mills   }
517ff03dc53SRichard Tran Mills 
518ff03dc53SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
519ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
520ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
521ff03dc53SRichard Tran Mills   PetscFunctionReturn(0);
522ff03dc53SRichard Tran Mills }
523ff03dc53SRichard Tran Mills 
524d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
525df555b71SRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz)
526df555b71SRichard Tran Mills {
527df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
528df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
529df555b71SRichard Tran Mills   const PetscScalar *x;
530df555b71SRichard Tran Mills   PetscScalar       *y,*z;
531df555b71SRichard Tran Mills   PetscErrorCode    ierr;
532df555b71SRichard Tran Mills   PetscInt          m=A->rmap->n;
533df555b71SRichard Tran Mills   PetscInt          i;
534df555b71SRichard Tran Mills 
535df555b71SRichard Tran Mills   /* Variables not in MatMultAdd_SeqAIJ. */
536df555b71SRichard Tran Mills   sparse_status_t stat = SPARSE_STATUS_SUCCESS;
537df555b71SRichard Tran Mills 
538df555b71SRichard Tran Mills   PetscFunctionBegin;
539df555b71SRichard Tran Mills 
54038987b35SRichard Tran Mills   /* If there are no nonzero entries, set zz = yy and return immediately. */
54138987b35SRichard Tran Mills   if(!a->nz) {
54238987b35SRichard Tran Mills     PetscInt i;
54338987b35SRichard Tran Mills     ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
54438987b35SRichard Tran Mills     for (i=0; i<m; i++) {
54538987b35SRichard Tran Mills       z[i] = y[i];
54638987b35SRichard Tran Mills     }
54738987b35SRichard Tran Mills     ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
54838987b35SRichard Tran Mills     PetscFunctionReturn(0);
54938987b35SRichard Tran Mills   }
550df555b71SRichard Tran Mills 
551df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
552df555b71SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
553df555b71SRichard Tran Mills 
5543fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
5553fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
5563fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
5573fa15762SRichard Tran Mills   if (!aijmkl->sparse_optimized) {
5583fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
5593fa15762SRichard Tran Mills   }
5603fa15762SRichard Tran Mills 
561df555b71SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
562df555b71SRichard Tran Mills   if (zz == yy) {
563df555b71SRichard 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,
564df555b71SRichard Tran Mills      * with alpha and beta both set to 1.0. */
565db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z);
566df555b71SRichard Tran Mills   } else {
567df555b71SRichard 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
568df555b71SRichard Tran Mills      * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */
569db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z);
570df555b71SRichard Tran Mills     for (i=0; i<m; i++) {
571df555b71SRichard Tran Mills       z[i] += y[i];
572df555b71SRichard Tran Mills     }
573df555b71SRichard Tran Mills   }
574df555b71SRichard Tran Mills 
575df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
576df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
577df555b71SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
578df555b71SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) {
579df555b71SRichard Tran Mills     PetscFunctionReturn(PETSC_ERR_LIB);
580df555b71SRichard Tran Mills   }
581df555b71SRichard Tran Mills   PetscFunctionReturn(0);
582df555b71SRichard Tran Mills }
583d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
584df555b71SRichard Tran Mills 
585ff03dc53SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz)
586ff03dc53SRichard Tran Mills {
587ff03dc53SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
588ff03dc53SRichard Tran Mills   const PetscScalar *x;
589ff03dc53SRichard Tran Mills   PetscScalar       *y,*z;
590ff03dc53SRichard Tran Mills   const MatScalar   *aa;
591ff03dc53SRichard Tran Mills   PetscErrorCode    ierr;
592ff03dc53SRichard Tran Mills   PetscInt          m=A->rmap->n;
593db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
594ff03dc53SRichard Tran Mills   const PetscInt    *aj,*ai;
595ff03dc53SRichard Tran Mills   PetscInt          i;
596ff03dc53SRichard Tran Mills 
597ff03dc53SRichard Tran Mills   /* Variables not in MatMultTransposeAdd_SeqAIJ. */
598ff03dc53SRichard Tran Mills   char transa = 't';  /* Used to indicate to MKL that we are computing the transpose product. */
599a84739b8SRichard Tran Mills   PetscScalar       alpha = 1.0;
600db63039fSRichard Tran Mills   PetscScalar       beta;
601a84739b8SRichard Tran Mills   char              matdescra[6];
6024a2a386eSRichard Tran Mills 
6034a2a386eSRichard Tran Mills   PetscFunctionBegin;
604a84739b8SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
605a84739b8SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
606a84739b8SRichard Tran Mills 
6074a2a386eSRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
6084a2a386eSRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
6094a2a386eSRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
6104a2a386eSRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
6114a2a386eSRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
6124a2a386eSRichard Tran Mills 
6134a2a386eSRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
614a84739b8SRichard Tran Mills   if (zz == yy) {
615a84739b8SRichard 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. */
616db63039fSRichard Tran Mills     beta = 1.0;
617969800c5SRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
618a84739b8SRichard Tran Mills   } else {
619db63039fSRichard Tran Mills     /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z.
620db63039fSRichard Tran Mills      * MKL sparse BLAS does not have a MatMultAdd equivalent. */
621db63039fSRichard Tran Mills     beta = 0.0;
622db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
623969800c5SRichard Tran Mills     for (i=0; i<n; i++) {
6244a2a386eSRichard Tran Mills       z[i] += y[i];
6254a2a386eSRichard Tran Mills     }
626a84739b8SRichard Tran Mills   }
6274a2a386eSRichard Tran Mills 
6284a2a386eSRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
6294a2a386eSRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
6304a2a386eSRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
6314a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
6324a2a386eSRichard Tran Mills }
6334a2a386eSRichard Tran Mills 
634d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
635df555b71SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz)
636df555b71SRichard Tran Mills {
637df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
638df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
639df555b71SRichard Tran Mills   const PetscScalar *x;
640df555b71SRichard Tran Mills   PetscScalar       *y,*z;
641df555b71SRichard Tran Mills   PetscErrorCode    ierr;
642969800c5SRichard Tran Mills   PetscInt          n=A->cmap->n;
643df555b71SRichard Tran Mills   PetscInt          i;
644df555b71SRichard Tran Mills 
645df555b71SRichard Tran Mills   /* Variables not in MatMultTransposeAdd_SeqAIJ. */
646df555b71SRichard Tran Mills   sparse_status_t stat = SPARSE_STATUS_SUCCESS;
647df555b71SRichard Tran Mills 
648df555b71SRichard Tran Mills   PetscFunctionBegin;
649df555b71SRichard Tran Mills 
65038987b35SRichard Tran Mills   /* If there are no nonzero entries, set zz = yy and return immediately. */
65138987b35SRichard Tran Mills   if(!a->nz) {
65238987b35SRichard Tran Mills     PetscInt i;
65338987b35SRichard Tran Mills     ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
65438987b35SRichard Tran Mills     for (i=0; i<n; i++) {
65538987b35SRichard Tran Mills       z[i] = y[i];
65638987b35SRichard Tran Mills     }
65738987b35SRichard Tran Mills     ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
65838987b35SRichard Tran Mills     PetscFunctionReturn(0);
65938987b35SRichard Tran Mills   }
660f36dfe3fSRichard Tran Mills 
661df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
662df555b71SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
663df555b71SRichard Tran Mills 
6643fa15762SRichard Tran Mills   /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call
6653fa15762SRichard Tran Mills    * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably
6663fa15762SRichard Tran Mills    * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */
6673fa15762SRichard Tran Mills   if (!aijmkl->sparse_optimized) {
6683fa15762SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
6693fa15762SRichard Tran Mills   }
6703fa15762SRichard Tran Mills 
671df555b71SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
672df555b71SRichard Tran Mills   if (zz == yy) {
673df555b71SRichard 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,
674df555b71SRichard Tran Mills      * with alpha and beta both set to 1.0. */
675db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z);
676df555b71SRichard Tran Mills   } else {
677df555b71SRichard 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
678df555b71SRichard Tran Mills      * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */
679db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z);
680969800c5SRichard Tran Mills     for (i=0; i<n; i++) {
681df555b71SRichard Tran Mills       z[i] += y[i];
682df555b71SRichard Tran Mills     }
683df555b71SRichard Tran Mills   }
684df555b71SRichard Tran Mills 
685df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
686df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
687df555b71SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
688df555b71SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) {
689df555b71SRichard Tran Mills     PetscFunctionReturn(PETSC_ERR_LIB);
690df555b71SRichard Tran Mills   }
691df555b71SRichard Tran Mills   PetscFunctionReturn(0);
692df555b71SRichard Tran Mills }
693d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
694df555b71SRichard Tran Mills 
69545fbe478SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
69645fbe478SRichard Tran Mills PetscErrorCode MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat*C)
69745fbe478SRichard Tran Mills {
69845fbe478SRichard Tran Mills   Mat_SeqAIJMKL *a, *b;
69945fbe478SRichard Tran Mills   sparse_matrix_t csrA, csrB, csrC;
70045fbe478SRichard Tran Mills   PetscErrorCode ierr;
70145fbe478SRichard Tran Mills   sparse_status_t stat = SPARSE_STATUS_SUCCESS;
70245fbe478SRichard Tran Mills 
70345fbe478SRichard Tran Mills   PetscFunctionBegin;
70445fbe478SRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
70545fbe478SRichard Tran Mills   b = (Mat_SeqAIJMKL*)B->spptr;
70645fbe478SRichard Tran Mills   if (!a->sparse_optimized) {
70745fbe478SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
70845fbe478SRichard Tran Mills   }
70945fbe478SRichard Tran Mills   if (!b->sparse_optimized) {
71045fbe478SRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(B);
71145fbe478SRichard Tran Mills   }
71245fbe478SRichard Tran Mills   csrA = a->csrA;
71345fbe478SRichard Tran Mills   csrB = b->csrA;
71445fbe478SRichard Tran Mills 
71545fbe478SRichard Tran Mills   stat = mkl_sparse_spmm(SPARSE_OPERATION_NON_TRANSPOSE,csrA,csrB,&csrC);
71645fbe478SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) {
71745fbe478SRichard Tran Mills     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete sparse matrix-matrix multiply");
71845fbe478SRichard Tran Mills     PetscFunctionReturn(PETSC_ERR_LIB);
71945fbe478SRichard Tran Mills   }
72045fbe478SRichard Tran Mills 
7216c87cf42SRichard Tran Mills   ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr);
72245fbe478SRichard Tran Mills 
72345fbe478SRichard Tran Mills   PetscFunctionReturn(0);
72445fbe478SRichard Tran Mills }
72545fbe478SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
72645fbe478SRichard Tran Mills 
727372ec6bbSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
728372ec6bbSRichard Tran Mills PetscErrorCode MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat*C)
729372ec6bbSRichard Tran Mills {
730372ec6bbSRichard Tran Mills   Mat_SeqAIJMKL *a, *b;
731372ec6bbSRichard Tran Mills   sparse_matrix_t csrA, csrB, csrC;
732372ec6bbSRichard Tran Mills   PetscErrorCode ierr;
733372ec6bbSRichard Tran Mills   sparse_status_t stat = SPARSE_STATUS_SUCCESS;
734372ec6bbSRichard Tran Mills 
735372ec6bbSRichard Tran Mills   PetscFunctionBegin;
736372ec6bbSRichard Tran Mills   a = (Mat_SeqAIJMKL*)A->spptr;
737372ec6bbSRichard Tran Mills   b = (Mat_SeqAIJMKL*)B->spptr;
738372ec6bbSRichard Tran Mills   if (!a->sparse_optimized) {
739372ec6bbSRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(A);
740372ec6bbSRichard Tran Mills   }
741372ec6bbSRichard Tran Mills   if (!b->sparse_optimized) {
742372ec6bbSRichard Tran Mills     MatSeqAIJMKL_create_mkl_handle(B);
743372ec6bbSRichard Tran Mills   }
744372ec6bbSRichard Tran Mills   csrA = a->csrA;
745372ec6bbSRichard Tran Mills   csrB = b->csrA;
746372ec6bbSRichard Tran Mills 
747372ec6bbSRichard Tran Mills   stat = mkl_sparse_spmm(SPARSE_OPERATION_TRANSPOSE,csrA,csrB,&csrC);
748372ec6bbSRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) {
749372ec6bbSRichard Tran Mills     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete sparse matrix-matrix multiply");
750372ec6bbSRichard Tran Mills     PetscFunctionReturn(PETSC_ERR_LIB);
751372ec6bbSRichard Tran Mills   }
752372ec6bbSRichard Tran Mills 
753372ec6bbSRichard Tran Mills   ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr);
754372ec6bbSRichard Tran Mills 
755372ec6bbSRichard Tran Mills   PetscFunctionReturn(0);
756372ec6bbSRichard Tran Mills }
757372ec6bbSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
758372ec6bbSRichard Tran Mills 
75987c2a1d7SRichard Tran Mills PetscErrorCode MatScale_SeqAIJMKL(Mat inA,PetscScalar alpha)
760db63039fSRichard Tran Mills {
761db63039fSRichard Tran Mills   PetscErrorCode ierr;
762db63039fSRichard Tran Mills 
76387c2a1d7SRichard Tran Mills   PetscFunctionBegin;
764db63039fSRichard Tran Mills   ierr = MatScale_SeqAIJ(inA,alpha);CHKERRQ(ierr);
765db63039fSRichard Tran Mills   ierr = MatSeqAIJMKL_create_mkl_handle(inA);CHKERRQ(ierr);
766db63039fSRichard Tran Mills   PetscFunctionReturn(0);
767db63039fSRichard Tran Mills }
768df555b71SRichard Tran Mills 
76987c2a1d7SRichard Tran Mills PetscErrorCode MatDiagonalScale_SeqAIJMKL(Mat A,Vec ll,Vec rr)
77087c2a1d7SRichard Tran Mills {
77187c2a1d7SRichard Tran Mills   PetscErrorCode ierr;
77287c2a1d7SRichard Tran Mills 
77387c2a1d7SRichard Tran Mills   PetscFunctionBegin;
77487c2a1d7SRichard Tran Mills   ierr = MatDiagonalScale_SeqAIJ(A,ll,rr);CHKERRQ(ierr);
77587c2a1d7SRichard Tran Mills   ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr);
77687c2a1d7SRichard Tran Mills   PetscFunctionReturn(0);
77787c2a1d7SRichard Tran Mills }
77887c2a1d7SRichard Tran Mills 
77987c2a1d7SRichard Tran Mills PetscErrorCode MatDiagonalSet_SeqAIJMKL(Mat Y,Vec D,InsertMode is)
78087c2a1d7SRichard Tran Mills {
78187c2a1d7SRichard Tran Mills   PetscErrorCode ierr;
78287c2a1d7SRichard Tran Mills 
78387c2a1d7SRichard Tran Mills   PetscFunctionBegin;
78487c2a1d7SRichard Tran Mills   ierr = MatDiagonalSet_SeqAIJ(Y,D,is);CHKERRQ(ierr);
78587c2a1d7SRichard Tran Mills   ierr = MatSeqAIJMKL_create_mkl_handle(Y);CHKERRQ(ierr);
78687c2a1d7SRichard Tran Mills   PetscFunctionReturn(0);
78787c2a1d7SRichard Tran Mills }
78887c2a1d7SRichard Tran Mills 
78987c2a1d7SRichard Tran Mills PetscErrorCode MatAXPY_SeqAIJMKL(Mat Y,PetscScalar a,Mat X,MatStructure str)
79087c2a1d7SRichard Tran Mills {
79187c2a1d7SRichard Tran Mills   PetscErrorCode ierr;
79287c2a1d7SRichard Tran Mills 
79387c2a1d7SRichard Tran Mills   PetscFunctionBegin;
79487c2a1d7SRichard Tran Mills   ierr = MatAXPY_SeqAIJ(Y,a,X,str);CHKERRQ(ierr);
79587c2a1d7SRichard Tran Mills   if (str == SAME_NONZERO_PATTERN) {
79687c2a1d7SRichard Tran Mills     /* MatAssemblyEnd() is not called if SAME_NONZERO_PATTERN, so we need to force update of the MKL matrix handle. */
79787c2a1d7SRichard Tran Mills     ierr = MatSeqAIJMKL_create_mkl_handle(Y);CHKERRQ(ierr);
79887c2a1d7SRichard Tran Mills   }
79987c2a1d7SRichard Tran Mills   PetscFunctionReturn(0);
80087c2a1d7SRichard Tran Mills }
80187c2a1d7SRichard Tran Mills 
8024a2a386eSRichard Tran Mills /* MatConvert_SeqAIJ_SeqAIJMKL converts a SeqAIJ matrix into a
8034a2a386eSRichard Tran Mills  * SeqAIJMKL matrix.  This routine is called by the MatCreate_SeqMKLAIJ()
8044a2a386eSRichard Tran Mills  * routine, but can also be used to convert an assembled SeqAIJ matrix
8054a2a386eSRichard Tran Mills  * into a SeqAIJMKL one. */
8064a2a386eSRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJMKL(Mat A,MatType type,MatReuse reuse,Mat *newmat)
8074a2a386eSRichard Tran Mills {
8084a2a386eSRichard Tran Mills   PetscErrorCode ierr;
8094a2a386eSRichard Tran Mills   Mat            B = *newmat;
8104a2a386eSRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl;
811c9d46305SRichard Tran Mills   PetscBool      set;
812e9c94282SRichard Tran Mills   PetscBool      sametype;
8134a2a386eSRichard Tran Mills 
8144a2a386eSRichard Tran Mills   PetscFunctionBegin;
8154a2a386eSRichard Tran Mills   if (reuse == MAT_INITIAL_MATRIX) {
8164a2a386eSRichard Tran Mills     ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr);
8174a2a386eSRichard Tran Mills   }
8184a2a386eSRichard Tran Mills 
819e9c94282SRichard Tran Mills   ierr = PetscObjectTypeCompare((PetscObject)A,type,&sametype);CHKERRQ(ierr);
820e9c94282SRichard Tran Mills   if (sametype) PetscFunctionReturn(0);
821e9c94282SRichard Tran Mills 
8224a2a386eSRichard Tran Mills   ierr     = PetscNewLog(B,&aijmkl);CHKERRQ(ierr);
8234a2a386eSRichard Tran Mills   B->spptr = (void*) aijmkl;
8244a2a386eSRichard Tran Mills 
825df555b71SRichard Tran Mills   /* Set function pointers for methods that we inherit from AIJ but override.
826969800c5SRichard Tran Mills    * We also parse some command line options below, since those determine some of the methods we point to. */
8274a2a386eSRichard Tran Mills   B->ops->duplicate        = MatDuplicate_SeqAIJMKL;
8284a2a386eSRichard Tran Mills   B->ops->assemblyend      = MatAssemblyEnd_SeqAIJMKL;
8294a2a386eSRichard Tran Mills   B->ops->destroy          = MatDestroy_SeqAIJMKL;
830c9d46305SRichard Tran Mills 
8314abfa3b3SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_FALSE;
832d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
833d995685eSRichard Tran Mills   aijmkl->no_SpMV2 = PETSC_FALSE;  /* Default to using the SpMV2 routines if our MKL supports them. */
834a8327b06SKarl Rupp #else
835d995685eSRichard Tran Mills   aijmkl->no_SpMV2 = PETSC_TRUE;
836d995685eSRichard Tran Mills #endif
8375b49642aSRichard Tran Mills   aijmkl->eager_inspection = PETSC_FALSE;
8384abfa3b3SRichard Tran Mills 
8394abfa3b3SRichard Tran Mills   /* Parse command line options. */
840c9d46305SRichard Tran Mills   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"AIJMKL Options","Mat");CHKERRQ(ierr);
841c9d46305SRichard Tran Mills   ierr = PetscOptionsBool("-mat_aijmkl_no_spmv2","NoSPMV2","None",(PetscBool)aijmkl->no_SpMV2,(PetscBool*)&aijmkl->no_SpMV2,&set);CHKERRQ(ierr);
8425b49642aSRichard Tran Mills   ierr = PetscOptionsBool("-mat_aijmkl_eager_inspection","Eager Inspection","None",(PetscBool)aijmkl->eager_inspection,(PetscBool*)&aijmkl->eager_inspection,&set);CHKERRQ(ierr);
843c9d46305SRichard Tran Mills   ierr = PetscOptionsEnd();CHKERRQ(ierr);
844d995685eSRichard Tran Mills #ifndef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
845d995685eSRichard Tran Mills   if(!aijmkl->no_SpMV2) {
846d995685eSRichard 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");
847d995685eSRichard Tran Mills     aijmkl->no_SpMV2 = PETSC_TRUE;
848d995685eSRichard Tran Mills   }
849d995685eSRichard Tran Mills #endif
850c9d46305SRichard Tran Mills 
851c9d46305SRichard Tran Mills   if(!aijmkl->no_SpMV2) {
852d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
853df555b71SRichard Tran Mills     B->ops->mult             = MatMult_SeqAIJMKL_SpMV2;
854969800c5SRichard Tran Mills     B->ops->multtranspose    = MatMultTranspose_SeqAIJMKL_SpMV2;
855df555b71SRichard Tran Mills     B->ops->multadd          = MatMultAdd_SeqAIJMKL_SpMV2;
856969800c5SRichard Tran Mills     B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL_SpMV2;
85745fbe478SRichard Tran Mills     B->ops->matmult          = MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2;
858372ec6bbSRichard Tran Mills     B->ops->mattransposemult = MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2;
859d995685eSRichard Tran Mills #endif
860c9d46305SRichard Tran Mills   } else {
8614a2a386eSRichard Tran Mills     B->ops->mult             = MatMult_SeqAIJMKL;
862969800c5SRichard Tran Mills     B->ops->multtranspose    = MatMultTranspose_SeqAIJMKL;
8634a2a386eSRichard Tran Mills     B->ops->multadd          = MatMultAdd_SeqAIJMKL;
864969800c5SRichard Tran Mills     B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL;
865c9d46305SRichard Tran Mills   }
8664a2a386eSRichard Tran Mills 
867db63039fSRichard Tran Mills   B->ops->scale              = MatScale_SeqAIJMKL;
86887c2a1d7SRichard Tran Mills   B->ops->diagonalscale      = MatDiagonalScale_SeqAIJMKL;
86987c2a1d7SRichard Tran Mills   B->ops->diagonalset        = MatDiagonalSet_SeqAIJMKL;
87087c2a1d7SRichard Tran Mills   B->ops->axpy               = MatAXPY_SeqAIJMKL;
871db63039fSRichard Tran Mills 
872db63039fSRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatScale_SeqAIJMKL_C",MatScale_SeqAIJMKL);CHKERRQ(ierr);
8734a2a386eSRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",MatConvert_SeqAIJMKL_SeqAIJ);CHKERRQ(ierr);
874e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaijmkl_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
875e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaijmkl_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
876e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaijmkl_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
87745fbe478SRichard Tran Mills   if(!aijmkl->no_SpMV2) {
87845fbe478SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
87945fbe478SRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqaijmkl_seqaijmkl_C",MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr);
880372ec6bbSRichard Tran Mills     ierr = PetscObjectComposeFunction((PetscObject)B,"MatTransposeMatMult_seqaijmkl_seqaijmkl_C",MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr);
88145fbe478SRichard Tran Mills #endif
88245fbe478SRichard Tran Mills   }
8834a2a386eSRichard Tran Mills 
8844a2a386eSRichard Tran Mills   ierr    = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJMKL);CHKERRQ(ierr);
8854a2a386eSRichard Tran Mills   *newmat = B;
8864a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
8874a2a386eSRichard Tran Mills }
8884a2a386eSRichard Tran Mills 
8894a2a386eSRichard Tran Mills /*@C
8904a2a386eSRichard Tran Mills    MatCreateSeqAIJMKL - Creates a sparse matrix of type SEQAIJMKL.
8914a2a386eSRichard Tran Mills    This type inherits from AIJ and is largely identical, but uses sparse BLAS
8924a2a386eSRichard Tran Mills    routines from Intel MKL whenever possible.
89390147e49SRichard Tran Mills    MatMult, MatMultAdd, MatMultTranspose, and MatMultTransposeAdd
89490147e49SRichard Tran Mills    operations are currently supported.
89590147e49SRichard Tran Mills    If the installed version of MKL supports the "SpMV2" sparse
89690147e49SRichard Tran Mills    inspector-executor routines, then those are used by default.
89790147e49SRichard Tran Mills 
8984a2a386eSRichard Tran Mills    Collective on MPI_Comm
8994a2a386eSRichard Tran Mills 
9004a2a386eSRichard Tran Mills    Input Parameters:
9014a2a386eSRichard Tran Mills +  comm - MPI communicator, set to PETSC_COMM_SELF
9024a2a386eSRichard Tran Mills .  m - number of rows
9034a2a386eSRichard Tran Mills .  n - number of columns
9044a2a386eSRichard Tran Mills .  nz - number of nonzeros per row (same for all rows)
9054a2a386eSRichard Tran Mills -  nnz - array containing the number of nonzeros in the various rows
9064a2a386eSRichard Tran Mills          (possibly different for each row) or NULL
9074a2a386eSRichard Tran Mills 
9084a2a386eSRichard Tran Mills    Output Parameter:
9094a2a386eSRichard Tran Mills .  A - the matrix
9104a2a386eSRichard Tran Mills 
91190147e49SRichard Tran Mills    Options Database Keys:
91290147e49SRichard Tran Mills .  -mat_aijmkl_no_spmv2 - disables use of the SpMV2 inspector-executor routines
91390147e49SRichard Tran Mills 
9144a2a386eSRichard Tran Mills    Notes:
9154a2a386eSRichard Tran Mills    If nnz is given then nz is ignored
9164a2a386eSRichard Tran Mills 
9174a2a386eSRichard Tran Mills    Level: intermediate
9184a2a386eSRichard Tran Mills 
91990147e49SRichard Tran Mills .keywords: matrix, MKL, sparse, parallel
9204a2a386eSRichard Tran Mills 
9214a2a386eSRichard Tran Mills .seealso: MatCreate(), MatCreateMPIAIJMKL(), MatSetValues()
9224a2a386eSRichard Tran Mills @*/
9234a2a386eSRichard Tran Mills PetscErrorCode  MatCreateSeqAIJMKL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
9244a2a386eSRichard Tran Mills {
9254a2a386eSRichard Tran Mills   PetscErrorCode ierr;
9264a2a386eSRichard Tran Mills 
9274a2a386eSRichard Tran Mills   PetscFunctionBegin;
9284a2a386eSRichard Tran Mills   ierr = MatCreate(comm,A);CHKERRQ(ierr);
9294a2a386eSRichard Tran Mills   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
9304a2a386eSRichard Tran Mills   ierr = MatSetType(*A,MATSEQAIJMKL);CHKERRQ(ierr);
9314a2a386eSRichard Tran Mills   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
9324a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
9334a2a386eSRichard Tran Mills }
9344a2a386eSRichard Tran Mills 
9354a2a386eSRichard Tran Mills PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJMKL(Mat A)
9364a2a386eSRichard Tran Mills {
9374a2a386eSRichard Tran Mills   PetscErrorCode ierr;
9384a2a386eSRichard Tran Mills 
9394a2a386eSRichard Tran Mills   PetscFunctionBegin;
9404a2a386eSRichard Tran Mills   ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr);
9414a2a386eSRichard Tran Mills   ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr);
9424a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
9434a2a386eSRichard Tran Mills }
944