xref: /petsc/src/mat/impls/aij/seq/aijmkl/aijmkl.c (revision db63039fb01984a1f226b30ca0b0b82e7facf18c)
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. */
174abfa3b3SRichard Tran Mills   PetscBool sparse_optimized; /* If PETSC_TRUE, then mkl_sparse_optimize() has been called. */
18b8cbc1fbSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
19df555b71SRichard Tran Mills   sparse_matrix_t csrA; /* "Handle" used by SpMV2 inspector-executor routines. */
20df555b71SRichard Tran Mills   struct matrix_descr descr;
21b8cbc1fbSRichard Tran Mills #endif
224a2a386eSRichard Tran Mills } Mat_SeqAIJMKL;
234a2a386eSRichard Tran Mills 
244a2a386eSRichard Tran Mills extern PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat,MatAssemblyType);
254a2a386eSRichard Tran Mills 
264a2a386eSRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJMKL_SeqAIJ(Mat A,MatType type,MatReuse reuse,Mat *newmat)
274a2a386eSRichard Tran Mills {
284a2a386eSRichard Tran Mills   /* This routine is only called to convert a MATAIJMKL to its base PETSc type, */
294a2a386eSRichard Tran Mills   /* so we will ignore 'MatType type'. */
304a2a386eSRichard Tran Mills   PetscErrorCode ierr;
314a2a386eSRichard Tran Mills   Mat            B       = *newmat;
324a2a386eSRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
334a2a386eSRichard Tran Mills 
344a2a386eSRichard Tran Mills   PetscFunctionBegin;
354a2a386eSRichard Tran Mills   if (reuse == MAT_INITIAL_MATRIX) {
364a2a386eSRichard Tran Mills     ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr);
37e9c94282SRichard Tran Mills     aijmkl = (Mat_SeqAIJMKL*)B->spptr;
384a2a386eSRichard Tran Mills   }
394a2a386eSRichard Tran Mills 
404a2a386eSRichard Tran Mills   /* Reset the original function pointers. */
4154871a98SRichard Tran Mills   B->ops->duplicate        = MatDuplicate_SeqAIJ;
424a2a386eSRichard Tran Mills   B->ops->assemblyend      = MatAssemblyEnd_SeqAIJ;
434a2a386eSRichard Tran Mills   B->ops->destroy          = MatDestroy_SeqAIJ;
4454871a98SRichard Tran Mills   B->ops->mult             = MatMult_SeqAIJ;
45ff03dc53SRichard Tran Mills   B->ops->multtranspose    = MatMultTranspose_SeqAIJ;
4654871a98SRichard Tran Mills   B->ops->multadd          = MatMultAdd_SeqAIJ;
47ff03dc53SRichard Tran Mills   B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJ;
484a2a386eSRichard Tran Mills 
49e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",NULL);CHKERRQ(ierr);
50e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr);
51e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr);
52e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr);
53e9c94282SRichard Tran Mills 
544abfa3b3SRichard Tran Mills   /* Free everything in the Mat_SeqAIJMKL data structure. Currently, this
55e9c94282SRichard Tran Mills    * simply involves destroying the MKL sparse matrix handle and then freeing
56e9c94282SRichard Tran Mills    * the spptr pointer. */
574abfa3b3SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
584abfa3b3SRichard Tran Mills   if (aijmkl->sparse_optimized) {
590632b357SRichard Tran Mills     sparse_status_t stat;
604abfa3b3SRichard Tran Mills     stat = mkl_sparse_destroy(aijmkl->csrA);
614abfa3b3SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) {
624abfa3b3SRichard Tran Mills       PetscFunctionReturn(PETSC_ERR_LIB);
634abfa3b3SRichard Tran Mills     }
644abfa3b3SRichard Tran Mills   }
654abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
66e9c94282SRichard Tran Mills   ierr = PetscFree(B->spptr);CHKERRQ(ierr);
674a2a386eSRichard Tran Mills 
684a2a386eSRichard Tran Mills   /* Change the type of B to MATSEQAIJ. */
694a2a386eSRichard Tran Mills   ierr = PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ);CHKERRQ(ierr);
704a2a386eSRichard Tran Mills 
714a2a386eSRichard Tran Mills   *newmat = B;
724a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
734a2a386eSRichard Tran Mills }
744a2a386eSRichard Tran Mills 
754a2a386eSRichard Tran Mills PetscErrorCode MatDestroy_SeqAIJMKL(Mat A)
764a2a386eSRichard Tran Mills {
774a2a386eSRichard Tran Mills   PetscErrorCode ierr;
784a2a386eSRichard Tran Mills   Mat_SeqAIJMKL *aijmkl = (Mat_SeqAIJMKL*) A->spptr;
794a2a386eSRichard Tran Mills 
804a2a386eSRichard Tran Mills   PetscFunctionBegin;
81e9c94282SRichard Tran Mills 
82e9c94282SRichard Tran Mills   /* If MatHeaderMerge() was used, then this SeqAIJMKL matrix will not have an
83e9c94282SRichard Tran Mills    * spptr pointer. */
84e9c94282SRichard Tran Mills   if (aijmkl) {
854a2a386eSRichard Tran Mills     /* Clean up everything in the Mat_SeqAIJMKL data structure, then free A->spptr. */
864abfa3b3SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
874abfa3b3SRichard Tran Mills     if (aijmkl->sparse_optimized) {
884abfa3b3SRichard Tran Mills       sparse_status_t stat = SPARSE_STATUS_SUCCESS;
894abfa3b3SRichard Tran Mills       stat = mkl_sparse_destroy(aijmkl->csrA);
904abfa3b3SRichard Tran Mills       if (stat != SPARSE_STATUS_SUCCESS) {
914abfa3b3SRichard Tran Mills         PetscFunctionReturn(PETSC_ERR_LIB);
924abfa3b3SRichard Tran Mills       }
934abfa3b3SRichard Tran Mills     }
944abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
954a2a386eSRichard Tran Mills     ierr = PetscFree(A->spptr);CHKERRQ(ierr);
96e9c94282SRichard Tran Mills   }
974a2a386eSRichard Tran Mills 
984a2a386eSRichard Tran Mills   /* Change the type of A back to SEQAIJ and use MatDestroy_SeqAIJ()
994a2a386eSRichard Tran Mills    * to destroy everything that remains. */
1004a2a386eSRichard Tran Mills   ierr = PetscObjectChangeTypeName((PetscObject)A, MATSEQAIJ);CHKERRQ(ierr);
1014a2a386eSRichard Tran Mills   /* Note that I don't call MatSetType().  I believe this is because that
1024a2a386eSRichard Tran Mills    * is only to be called when *building* a matrix.  I could be wrong, but
1034a2a386eSRichard Tran Mills    * that is how things work for the SuperLU matrix class. */
1044a2a386eSRichard Tran Mills   ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr);
1054a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
1064a2a386eSRichard Tran Mills }
1074a2a386eSRichard Tran Mills 
1086e369cd5SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_mkl_handle(Mat A)
1094a2a386eSRichard Tran Mills {
1104a2a386eSRichard Tran Mills   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
111df555b71SRichard Tran Mills   Mat_SeqAIJMKL   *aijmkl;
11258678438SRichard Tran Mills   PetscInt        m,n;
1136e369cd5SRichard Tran Mills   MatScalar       *aa;
114df555b71SRichard Tran Mills   PetscInt        *aj,*ai;
1154a2a386eSRichard Tran Mills 
1166e369cd5SRichard Tran Mills #ifndef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
1176e369cd5SRichard Tran Mills   /* If the MKL library does not have mkl_sparse_optimize(), then this routine
1186e369cd5SRichard Tran Mills    * does nothing. We make it callable anyway in this case because it cuts
1196e369cd5SRichard Tran Mills    * down on littering the code with #ifdefs. */
1206e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
1216e369cd5SRichard Tran Mills #else
1224a2a386eSRichard Tran Mills 
1236e369cd5SRichard Tran Mills   sparse_status_t stat;
1244a2a386eSRichard Tran Mills 
125df555b71SRichard Tran Mills   aijmkl = (Mat_SeqAIJMKL*) A->spptr;
1266e369cd5SRichard Tran Mills 
1276e369cd5SRichard Tran Mills   if (aijmkl->no_SpMV2) PetscFunctionReturn(0);
1286e369cd5SRichard Tran Mills 
1290632b357SRichard Tran Mills   if (aijmkl->sparse_optimized) {
1300632b357SRichard Tran Mills     /* Matrix has been previously assembled and optimized. Must destroy old
1310632b357SRichard Tran Mills      * matrix handle before running the optimization step again. */
1320632b357SRichard Tran Mills     stat = mkl_sparse_destroy(aijmkl->csrA);
1330632b357SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) {
1340632b357SRichard Tran Mills       PetscFunctionReturn(PETSC_ERR_LIB);
1350632b357SRichard Tran Mills     }
1360632b357SRichard Tran Mills   }
1376e369cd5SRichard Tran Mills 
138c9d46305SRichard Tran Mills   /* Now perform the SpMV2 setup and matrix optimization. */
139df555b71SRichard Tran Mills   aijmkl->descr.type        = SPARSE_MATRIX_TYPE_GENERAL;
140df555b71SRichard Tran Mills   aijmkl->descr.mode        = SPARSE_FILL_MODE_LOWER;
141df555b71SRichard Tran Mills   aijmkl->descr.diag        = SPARSE_DIAG_NON_UNIT;
14258678438SRichard Tran Mills   m = A->rmap->n;
14358678438SRichard Tran Mills   n = A->cmap->n;
144df555b71SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
145df555b71SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
146df555b71SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
147f36dfe3fSRichard Tran Mills   if (n>0) {
14858678438SRichard Tran Mills     stat = mkl_sparse_x_create_csr (&aijmkl->csrA,SPARSE_INDEX_BASE_ZERO,m,n,ai,ai+1,aj,aa);
149df555b71SRichard Tran Mills     stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000);
150df555b71SRichard Tran Mills     stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE);
151df555b71SRichard Tran Mills     stat = mkl_sparse_optimize(aijmkl->csrA);
152df555b71SRichard Tran Mills     if (stat != SPARSE_STATUS_SUCCESS) {
153f68ad4bdSRichard Tran Mills       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to create matrix handle/complete mkl_sparse_optimize");
154df555b71SRichard Tran Mills       PetscFunctionReturn(PETSC_ERR_LIB);
155df555b71SRichard Tran Mills     }
1564abfa3b3SRichard Tran Mills     aijmkl->sparse_optimized = PETSC_TRUE;
157c9d46305SRichard Tran Mills   }
1586e369cd5SRichard Tran Mills 
1596e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
160d995685eSRichard Tran Mills #endif
1616e369cd5SRichard Tran Mills }
1626e369cd5SRichard Tran Mills 
1636e369cd5SRichard Tran Mills PetscErrorCode MatDuplicate_SeqAIJMKL(Mat A, MatDuplicateOption op, Mat *M)
1646e369cd5SRichard Tran Mills {
1656e369cd5SRichard Tran Mills   PetscErrorCode ierr;
1666e369cd5SRichard Tran Mills   Mat_SeqAIJMKL *aijmkl;
1676e369cd5SRichard Tran Mills   Mat_SeqAIJMKL *aijmkl_dest;
1686e369cd5SRichard Tran Mills 
1696e369cd5SRichard Tran Mills   PetscFunctionBegin;
1706e369cd5SRichard Tran Mills   ierr = MatDuplicate_SeqAIJ(A,op,M);CHKERRQ(ierr);
1716e369cd5SRichard Tran Mills   aijmkl      = (Mat_SeqAIJMKL*) A->spptr;
1726e369cd5SRichard Tran Mills   aijmkl_dest = (Mat_SeqAIJMKL*) (*M)->spptr;
1736e369cd5SRichard Tran Mills   ierr = PetscMemcpy(aijmkl_dest,aijmkl,sizeof(Mat_SeqAIJMKL));CHKERRQ(ierr);
1746e369cd5SRichard Tran Mills   aijmkl_dest->sparse_optimized = PETSC_FALSE;
1756e369cd5SRichard Tran Mills   ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr);
1766e369cd5SRichard Tran Mills   PetscFunctionReturn(0);
1776e369cd5SRichard Tran Mills }
1786e369cd5SRichard Tran Mills 
1796e369cd5SRichard Tran Mills PetscErrorCode MatAssemblyEnd_SeqAIJMKL(Mat A, MatAssemblyType mode)
1806e369cd5SRichard Tran Mills {
1816e369cd5SRichard Tran Mills   PetscErrorCode  ierr;
1826e369cd5SRichard Tran Mills   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
1836e369cd5SRichard Tran Mills 
1846e369cd5SRichard Tran Mills   PetscFunctionBegin;
1856e369cd5SRichard Tran Mills   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
1866e369cd5SRichard Tran Mills 
1876e369cd5SRichard Tran Mills   /* Since a MATSEQAIJMKL matrix is really just a MATSEQAIJ with some
1886e369cd5SRichard Tran Mills    * extra information and some different methods, call the AssemblyEnd
1896e369cd5SRichard Tran Mills    * routine for a MATSEQAIJ.
1906e369cd5SRichard Tran Mills    * I'm not sure if this is the best way to do this, but it avoids
1916e369cd5SRichard Tran Mills    * a lot of code duplication.
1926e369cd5SRichard Tran Mills    * I also note that currently MATSEQAIJMKL doesn't know anything about
1936e369cd5SRichard Tran Mills    * the Mat_CompressedRow data structure that SeqAIJ now uses when there
1946e369cd5SRichard Tran Mills    * are many zero rows.  If the SeqAIJ assembly end routine decides to use
1956e369cd5SRichard Tran Mills    * this, this may break things.  (Don't know... haven't looked at it.
1966e369cd5SRichard Tran Mills    * Do I need to disable this somehow?) */
1976e369cd5SRichard Tran Mills   a->inode.use = PETSC_FALSE;  /* Must disable: otherwise the MKL routines won't get used. */
1986e369cd5SRichard Tran Mills   ierr         = MatAssemblyEnd_SeqAIJ(A, mode);CHKERRQ(ierr);
1996e369cd5SRichard Tran Mills 
2006e369cd5SRichard Tran Mills   /* Now create the MKL sparse handle (if needed; the function checks). */
2016e369cd5SRichard Tran Mills   ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr);
202df555b71SRichard Tran Mills 
2034a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
2044a2a386eSRichard Tran Mills }
2054a2a386eSRichard Tran Mills 
2064a2a386eSRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL(Mat A,Vec xx,Vec yy)
2074a2a386eSRichard Tran Mills {
2084a2a386eSRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2094a2a386eSRichard Tran Mills   const PetscScalar *x;
2104a2a386eSRichard Tran Mills   PetscScalar       *y;
2114a2a386eSRichard Tran Mills   const MatScalar   *aa;
2124a2a386eSRichard Tran Mills   PetscErrorCode    ierr;
2134a2a386eSRichard Tran Mills   PetscInt          m=A->rmap->n;
214*db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
215*db63039fSRichard Tran Mills   PetscScalar       alpha = 1.0;
216*db63039fSRichard Tran Mills   PetscScalar       beta = 0.0;
2174a2a386eSRichard Tran Mills   const PetscInt    *aj,*ai;
218*db63039fSRichard Tran Mills   char              matdescra[6];
219*db63039fSRichard Tran Mills 
2204a2a386eSRichard Tran Mills 
2214a2a386eSRichard Tran Mills   /* Variables not in MatMult_SeqAIJ. */
222ff03dc53SRichard Tran Mills   char transa = 'n';  /* Used to indicate to MKL that we are not computing the transpose product. */
223ff03dc53SRichard Tran Mills 
224ff03dc53SRichard Tran Mills   PetscFunctionBegin;
225*db63039fSRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
226*db63039fSRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
227ff03dc53SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
228ff03dc53SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
229ff03dc53SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
230ff03dc53SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
231ff03dc53SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
232ff03dc53SRichard Tran Mills 
233ff03dc53SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
234*db63039fSRichard Tran Mills   mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y);
235ff03dc53SRichard Tran Mills 
236ff03dc53SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
237ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
238ff03dc53SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
239ff03dc53SRichard Tran Mills   PetscFunctionReturn(0);
240ff03dc53SRichard Tran Mills }
241ff03dc53SRichard Tran Mills 
242d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
243df555b71SRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy)
244df555b71SRichard Tran Mills {
245df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
246df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
247df555b71SRichard Tran Mills   const PetscScalar *x;
248df555b71SRichard Tran Mills   PetscScalar       *y;
249df555b71SRichard Tran Mills   PetscErrorCode    ierr;
250df555b71SRichard Tran Mills   sparse_status_t stat = SPARSE_STATUS_SUCCESS;
251df555b71SRichard Tran Mills 
252df555b71SRichard Tran Mills   PetscFunctionBegin;
253df555b71SRichard Tran Mills 
254f36dfe3fSRichard Tran Mills   /* If there are no rows, this is a no-op: return immediately. */
255f36dfe3fSRichard Tran Mills   if(A->cmap->n < 1) PetscFunctionReturn(0);
256f36dfe3fSRichard Tran Mills 
257df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
258df555b71SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
259df555b71SRichard Tran Mills 
260df555b71SRichard Tran Mills   /* Call MKL SpMV2 executor routine to do the MatMult. */
261df555b71SRichard Tran Mills   stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y);
262df555b71SRichard Tran Mills 
263df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
264df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
265df555b71SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
266df555b71SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) {
267df555b71SRichard Tran Mills     PetscFunctionReturn(PETSC_ERR_LIB);
268df555b71SRichard Tran Mills   }
269df555b71SRichard Tran Mills   PetscFunctionReturn(0);
270df555b71SRichard Tran Mills }
271d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
272df555b71SRichard Tran Mills 
273ff03dc53SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL(Mat A,Vec xx,Vec yy)
274ff03dc53SRichard Tran Mills {
275ff03dc53SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
276ff03dc53SRichard Tran Mills   const PetscScalar *x;
277ff03dc53SRichard Tran Mills   PetscScalar       *y;
278ff03dc53SRichard Tran Mills   const MatScalar   *aa;
279ff03dc53SRichard Tran Mills   PetscErrorCode    ierr;
280ff03dc53SRichard Tran Mills   PetscInt          m=A->rmap->n;
281*db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
282*db63039fSRichard Tran Mills   PetscScalar       alpha = 1.0;
283*db63039fSRichard Tran Mills   PetscScalar       beta = 0.0;
284ff03dc53SRichard Tran Mills   const PetscInt    *aj,*ai;
285*db63039fSRichard Tran Mills   char              matdescra[6];
286ff03dc53SRichard Tran Mills 
287ff03dc53SRichard Tran Mills   /* Variables not in MatMultTranspose_SeqAIJ. */
288ff03dc53SRichard Tran Mills   char transa = 't';  /* Used to indicate to MKL that we are computing the transpose product. */
2894a2a386eSRichard Tran Mills 
2904a2a386eSRichard Tran Mills   PetscFunctionBegin;
2914a2a386eSRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
2924a2a386eSRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
2934a2a386eSRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
2944a2a386eSRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
2954a2a386eSRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
2964a2a386eSRichard Tran Mills 
2974a2a386eSRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
298*db63039fSRichard Tran Mills   mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y);
2994a2a386eSRichard Tran Mills 
3004a2a386eSRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
3014a2a386eSRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
3024a2a386eSRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
3034a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
3044a2a386eSRichard Tran Mills }
3054a2a386eSRichard Tran Mills 
306d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
307df555b71SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy)
308df555b71SRichard Tran Mills {
309df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
310df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
311df555b71SRichard Tran Mills   const PetscScalar *x;
312df555b71SRichard Tran Mills   PetscScalar       *y;
313df555b71SRichard Tran Mills   PetscErrorCode    ierr;
3140632b357SRichard Tran Mills   sparse_status_t   stat;
315df555b71SRichard Tran Mills 
316df555b71SRichard Tran Mills   PetscFunctionBegin;
317df555b71SRichard Tran Mills 
318f36dfe3fSRichard Tran Mills   /* If there are no rows, this is a no-op: return immediately. */
319f36dfe3fSRichard Tran Mills   if(A->cmap->n < 1) PetscFunctionReturn(0);
320f36dfe3fSRichard Tran Mills 
321df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
322df555b71SRichard Tran Mills   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
323df555b71SRichard Tran Mills 
324df555b71SRichard Tran Mills   /* Call MKL SpMV2 executor routine to do the MatMultTranspose. */
325df555b71SRichard Tran Mills   stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y);
326df555b71SRichard Tran Mills 
327df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
328df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
329df555b71SRichard Tran Mills   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
330df555b71SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) {
331df555b71SRichard Tran Mills     PetscFunctionReturn(PETSC_ERR_LIB);
332df555b71SRichard Tran Mills   }
333df555b71SRichard Tran Mills   PetscFunctionReturn(0);
334df555b71SRichard Tran Mills }
335d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
336df555b71SRichard Tran Mills 
3374a2a386eSRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz)
3384a2a386eSRichard Tran Mills {
3394a2a386eSRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
3404a2a386eSRichard Tran Mills   const PetscScalar *x;
3414a2a386eSRichard Tran Mills   PetscScalar       *y,*z;
3424a2a386eSRichard Tran Mills   const MatScalar   *aa;
3434a2a386eSRichard Tran Mills   PetscErrorCode    ierr;
3444a2a386eSRichard Tran Mills   PetscInt          m=A->rmap->n;
345*db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
3464a2a386eSRichard Tran Mills   const PetscInt    *aj,*ai;
3474a2a386eSRichard Tran Mills   PetscInt          i;
3484a2a386eSRichard Tran Mills 
349ff03dc53SRichard Tran Mills   /* Variables not in MatMultAdd_SeqAIJ. */
350ff03dc53SRichard Tran Mills   char transa = 'n';  /* Used to indicate to MKL that we are not computing the transpose product. */
351a84739b8SRichard Tran Mills   PetscScalar       alpha = 1.0;
352*db63039fSRichard Tran Mills   PetscScalar       beta;
353a84739b8SRichard Tran Mills   char              matdescra[6];
354ff03dc53SRichard Tran Mills 
355ff03dc53SRichard Tran Mills   PetscFunctionBegin;
356a84739b8SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
357a84739b8SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
358a84739b8SRichard Tran Mills 
359ff03dc53SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
360ff03dc53SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
361ff03dc53SRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
362ff03dc53SRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
363ff03dc53SRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
364ff03dc53SRichard Tran Mills 
365ff03dc53SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
366a84739b8SRichard Tran Mills   if (zz == yy) {
367a84739b8SRichard 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. */
368*db63039fSRichard Tran Mills     beta = 1.0;
369*db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
370a84739b8SRichard Tran Mills   } else {
371*db63039fSRichard Tran Mills     /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z.
372*db63039fSRichard Tran Mills      * MKL sparse BLAS does not have a MatMultAdd equivalent. */
373*db63039fSRichard Tran Mills     beta = 0.0;
374*db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
375ff03dc53SRichard Tran Mills     for (i=0; i<m; i++) {
376ff03dc53SRichard Tran Mills       z[i] += y[i];
377ff03dc53SRichard Tran Mills     }
378a84739b8SRichard Tran Mills   }
379ff03dc53SRichard Tran Mills 
380ff03dc53SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
381ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
382ff03dc53SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
383ff03dc53SRichard Tran Mills   PetscFunctionReturn(0);
384ff03dc53SRichard Tran Mills }
385ff03dc53SRichard Tran Mills 
386d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
387df555b71SRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz)
388df555b71SRichard Tran Mills {
389df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
390df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
391df555b71SRichard Tran Mills   const PetscScalar *x;
392df555b71SRichard Tran Mills   PetscScalar       *y,*z;
393df555b71SRichard Tran Mills   PetscErrorCode    ierr;
394df555b71SRichard Tran Mills   PetscInt          m=A->rmap->n;
395df555b71SRichard Tran Mills   PetscInt          i;
396df555b71SRichard Tran Mills 
397df555b71SRichard Tran Mills   /* Variables not in MatMultAdd_SeqAIJ. */
398df555b71SRichard Tran Mills   sparse_status_t stat = SPARSE_STATUS_SUCCESS;
399df555b71SRichard Tran Mills 
400df555b71SRichard Tran Mills   PetscFunctionBegin;
401df555b71SRichard Tran Mills 
402f36dfe3fSRichard Tran Mills   /* If there are no rows, this is a no-op: return immediately. */
403f36dfe3fSRichard Tran Mills   if(A->cmap->n < 1) PetscFunctionReturn(0);
404df555b71SRichard Tran Mills 
405df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
406df555b71SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
407df555b71SRichard Tran Mills 
408df555b71SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
409df555b71SRichard Tran Mills   if (zz == yy) {
410df555b71SRichard 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,
411df555b71SRichard Tran Mills      * with alpha and beta both set to 1.0. */
412*db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z);
413df555b71SRichard Tran Mills   } else {
414df555b71SRichard 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
415df555b71SRichard Tran Mills      * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */
416*db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z);
417df555b71SRichard Tran Mills     for (i=0; i<m; i++) {
418df555b71SRichard Tran Mills       z[i] += y[i];
419df555b71SRichard Tran Mills     }
420df555b71SRichard Tran Mills   }
421df555b71SRichard Tran Mills 
422df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
423df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
424df555b71SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
425df555b71SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) {
426df555b71SRichard Tran Mills     PetscFunctionReturn(PETSC_ERR_LIB);
427df555b71SRichard Tran Mills   }
428df555b71SRichard Tran Mills   PetscFunctionReturn(0);
429df555b71SRichard Tran Mills }
430d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
431df555b71SRichard Tran Mills 
432ff03dc53SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz)
433ff03dc53SRichard Tran Mills {
434ff03dc53SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
435ff03dc53SRichard Tran Mills   const PetscScalar *x;
436ff03dc53SRichard Tran Mills   PetscScalar       *y,*z;
437ff03dc53SRichard Tran Mills   const MatScalar   *aa;
438ff03dc53SRichard Tran Mills   PetscErrorCode    ierr;
439ff03dc53SRichard Tran Mills   PetscInt          m=A->rmap->n;
440*db63039fSRichard Tran Mills   PetscInt          n=A->cmap->n;
441ff03dc53SRichard Tran Mills   const PetscInt    *aj,*ai;
442ff03dc53SRichard Tran Mills   PetscInt          i;
443ff03dc53SRichard Tran Mills 
444ff03dc53SRichard Tran Mills   /* Variables not in MatMultTransposeAdd_SeqAIJ. */
445ff03dc53SRichard Tran Mills   char transa = 't';  /* Used to indicate to MKL that we are computing the transpose product. */
446a84739b8SRichard Tran Mills   PetscScalar       alpha = 1.0;
447*db63039fSRichard Tran Mills   PetscScalar       beta;
448a84739b8SRichard Tran Mills   char              matdescra[6];
4494a2a386eSRichard Tran Mills 
4504a2a386eSRichard Tran Mills   PetscFunctionBegin;
451a84739b8SRichard Tran Mills   matdescra[0] = 'g';  /* Indicates to MKL that we using a general CSR matrix. */
452a84739b8SRichard Tran Mills   matdescra[3] = 'c';  /* Indicates to MKL that we use C-style (0-based) indexing. */
453a84739b8SRichard Tran Mills 
4544a2a386eSRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
4554a2a386eSRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
4564a2a386eSRichard Tran Mills   aj   = a->j;  /* aj[k] gives column index for element aa[k]. */
4574a2a386eSRichard Tran Mills   aa   = a->a;  /* Nonzero elements stored row-by-row. */
4584a2a386eSRichard Tran Mills   ai   = a->i;  /* ai[k] is the position in aa and aj where row k starts. */
4594a2a386eSRichard Tran Mills 
4604a2a386eSRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
461a84739b8SRichard Tran Mills   if (zz == yy) {
462a84739b8SRichard 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. */
463*db63039fSRichard Tran Mills     beta = 1.0;
464*db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&m,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
465a84739b8SRichard Tran Mills   } else {
466*db63039fSRichard Tran Mills     /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z.
467*db63039fSRichard Tran Mills      * MKL sparse BLAS does not have a MatMultAdd equivalent. */
468*db63039fSRichard Tran Mills     beta = 0.0;
469*db63039fSRichard Tran Mills     mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z);
4704a2a386eSRichard Tran Mills     for (i=0; i<m; i++) {
4714a2a386eSRichard Tran Mills       z[i] += y[i];
4724a2a386eSRichard Tran Mills     }
473a84739b8SRichard Tran Mills   }
4744a2a386eSRichard Tran Mills 
4754a2a386eSRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
4764a2a386eSRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
4774a2a386eSRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
4784a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
4794a2a386eSRichard Tran Mills }
4804a2a386eSRichard Tran Mills 
481d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
482df555b71SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz)
483df555b71SRichard Tran Mills {
484df555b71SRichard Tran Mills   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
485df555b71SRichard Tran Mills   Mat_SeqAIJMKL     *aijmkl=(Mat_SeqAIJMKL*)A->spptr;
486df555b71SRichard Tran Mills   const PetscScalar *x;
487df555b71SRichard Tran Mills   PetscScalar       *y,*z;
488df555b71SRichard Tran Mills   PetscErrorCode    ierr;
489df555b71SRichard Tran Mills   PetscInt          m=A->rmap->n;
490df555b71SRichard Tran Mills   PetscInt          i;
491df555b71SRichard Tran Mills 
492df555b71SRichard Tran Mills   /* Variables not in MatMultTransposeAdd_SeqAIJ. */
493df555b71SRichard Tran Mills   sparse_status_t stat = SPARSE_STATUS_SUCCESS;
494df555b71SRichard Tran Mills 
495df555b71SRichard Tran Mills   PetscFunctionBegin;
496df555b71SRichard Tran Mills 
497f36dfe3fSRichard Tran Mills   /* If there are no rows, this is a no-op: return immediately. */
498f36dfe3fSRichard Tran Mills   if(A->cmap->n < 1) PetscFunctionReturn(0);
499f36dfe3fSRichard Tran Mills 
500df555b71SRichard Tran Mills   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
501df555b71SRichard Tran Mills   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
502df555b71SRichard Tran Mills 
503df555b71SRichard Tran Mills   /* Call MKL sparse BLAS routine to do the MatMult. */
504df555b71SRichard Tran Mills   if (zz == yy) {
505df555b71SRichard 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,
506df555b71SRichard Tran Mills      * with alpha and beta both set to 1.0. */
507*db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z);
508df555b71SRichard Tran Mills   } else {
509df555b71SRichard 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
510df555b71SRichard Tran Mills      * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */
511*db63039fSRichard Tran Mills     stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z);
512df555b71SRichard Tran Mills     for (i=0; i<m; i++) {
513df555b71SRichard Tran Mills       z[i] += y[i];
514df555b71SRichard Tran Mills     }
515df555b71SRichard Tran Mills   }
516df555b71SRichard Tran Mills 
517df555b71SRichard Tran Mills   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
518df555b71SRichard Tran Mills   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
519df555b71SRichard Tran Mills   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
520df555b71SRichard Tran Mills   if (stat != SPARSE_STATUS_SUCCESS) {
521df555b71SRichard Tran Mills     PetscFunctionReturn(PETSC_ERR_LIB);
522df555b71SRichard Tran Mills   }
523df555b71SRichard Tran Mills   PetscFunctionReturn(0);
524df555b71SRichard Tran Mills }
525d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */
526df555b71SRichard Tran Mills 
527*db63039fSRichard Tran Mills PETSC_INTERN PetscErrorCode MatScale_SeqAIJMKL(Mat inA,PetscScalar alpha)
528*db63039fSRichard Tran Mills {
529*db63039fSRichard Tran Mills   PetscErrorCode ierr;
530*db63039fSRichard Tran Mills 
531*db63039fSRichard Tran Mills   ierr = MatScale_SeqAIJ(inA,alpha);CHKERRQ(ierr);
532*db63039fSRichard Tran Mills   ierr = MatSeqAIJMKL_create_mkl_handle(inA);CHKERRQ(ierr);
533*db63039fSRichard Tran Mills   PetscFunctionReturn(0);
534*db63039fSRichard Tran Mills }
535df555b71SRichard Tran Mills 
5364a2a386eSRichard Tran Mills /* MatConvert_SeqAIJ_SeqAIJMKL converts a SeqAIJ matrix into a
5374a2a386eSRichard Tran Mills  * SeqAIJMKL matrix.  This routine is called by the MatCreate_SeqMKLAIJ()
5384a2a386eSRichard Tran Mills  * routine, but can also be used to convert an assembled SeqAIJ matrix
5394a2a386eSRichard Tran Mills  * into a SeqAIJMKL one. */
5404a2a386eSRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJMKL(Mat A,MatType type,MatReuse reuse,Mat *newmat)
5414a2a386eSRichard Tran Mills {
5424a2a386eSRichard Tran Mills   PetscErrorCode ierr;
5434a2a386eSRichard Tran Mills   Mat            B = *newmat;
5444a2a386eSRichard Tran Mills   Mat_SeqAIJMKL  *aijmkl;
545c9d46305SRichard Tran Mills   PetscBool      set;
546e9c94282SRichard Tran Mills   PetscBool      sametype;
5474a2a386eSRichard Tran Mills 
5484a2a386eSRichard Tran Mills   PetscFunctionBegin;
5494a2a386eSRichard Tran Mills   if (reuse == MAT_INITIAL_MATRIX) {
5504a2a386eSRichard Tran Mills     ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr);
5514a2a386eSRichard Tran Mills   }
5524a2a386eSRichard Tran Mills 
553e9c94282SRichard Tran Mills   ierr = PetscObjectTypeCompare((PetscObject)A,type,&sametype);CHKERRQ(ierr);
554e9c94282SRichard Tran Mills   if (sametype) PetscFunctionReturn(0);
555e9c94282SRichard Tran Mills 
5564a2a386eSRichard Tran Mills   ierr     = PetscNewLog(B,&aijmkl);CHKERRQ(ierr);
5574a2a386eSRichard Tran Mills   B->spptr = (void*) aijmkl;
5584a2a386eSRichard Tran Mills 
559df555b71SRichard Tran Mills   /* Set function pointers for methods that we inherit from AIJ but override.
560e9c94282SRichard Tran Mills    * We also parse some command line options below, since those determine some of the methods we point to.
561e9c94282SRichard Tran Mills    * Note: Currently the transposed operations are not being set because I encounter memory corruption
562df555b71SRichard Tran Mills    * when these are enabled.  Need to look at this with Valgrind or similar. --RTM */
5634a2a386eSRichard Tran Mills   B->ops->duplicate        = MatDuplicate_SeqAIJMKL;
5644a2a386eSRichard Tran Mills   B->ops->assemblyend      = MatAssemblyEnd_SeqAIJMKL;
5654a2a386eSRichard Tran Mills   B->ops->destroy          = MatDestroy_SeqAIJMKL;
566c9d46305SRichard Tran Mills 
5674abfa3b3SRichard Tran Mills   aijmkl->sparse_optimized = PETSC_FALSE;
568d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
569d995685eSRichard Tran Mills   aijmkl->no_SpMV2 = PETSC_FALSE;  /* Default to using the SpMV2 routines if our MKL supports them. */
570d995685eSRichard Tran Mills #elif
571d995685eSRichard Tran Mills   aijmkl->no_SpMV2 = PETSC_TRUE;
572d995685eSRichard Tran Mills #endif
5734abfa3b3SRichard Tran Mills 
5744abfa3b3SRichard Tran Mills   /* Parse command line options. */
575c9d46305SRichard Tran Mills   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"AIJMKL Options","Mat");CHKERRQ(ierr);
576c9d46305SRichard Tran Mills   ierr = PetscOptionsBool("-mat_aijmkl_no_spmv2","NoSPMV2","None",(PetscBool)aijmkl->no_SpMV2,(PetscBool*)&aijmkl->no_SpMV2,&set);CHKERRQ(ierr);
577c9d46305SRichard Tran Mills   ierr = PetscOptionsEnd();CHKERRQ(ierr);
578d995685eSRichard Tran Mills #ifndef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
579d995685eSRichard Tran Mills   if(!aijmkl->no_SpMV2) {
580d995685eSRichard 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");
581d995685eSRichard Tran Mills     aijmkl->no_SpMV2 = PETSC_TRUE;
582d995685eSRichard Tran Mills   }
583d995685eSRichard Tran Mills #endif
584c9d46305SRichard Tran Mills 
585c9d46305SRichard Tran Mills   if(!aijmkl->no_SpMV2) {
586d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE
587df555b71SRichard Tran Mills     B->ops->mult             = MatMult_SeqAIJMKL_SpMV2;
588df555b71SRichard Tran Mills     /* B->ops->multtranspose    = MatMultTranspose_SeqAIJMKL_SpMV2; */
589df555b71SRichard Tran Mills     B->ops->multadd          = MatMultAdd_SeqAIJMKL_SpMV2;
590df555b71SRichard Tran Mills     /* B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL_SpMV2; */
591d995685eSRichard Tran Mills #endif
592c9d46305SRichard Tran Mills   } else {
5934a2a386eSRichard Tran Mills     B->ops->mult             = MatMult_SeqAIJMKL;
594c9d46305SRichard Tran Mills     /* B->ops->multtranspose    = MatMultTranspose_SeqAIJMKL; */
5954a2a386eSRichard Tran Mills     B->ops->multadd          = MatMultAdd_SeqAIJMKL;
596c9d46305SRichard Tran Mills     /* B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL; */
597c9d46305SRichard Tran Mills   }
5984a2a386eSRichard Tran Mills 
599*db63039fSRichard Tran Mills   B->ops->scale = MatScale_SeqAIJMKL;
600*db63039fSRichard Tran Mills 
601*db63039fSRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatScale_SeqAIJMKL_C",MatScale_SeqAIJMKL);CHKERRQ(ierr);
6024a2a386eSRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",MatConvert_SeqAIJMKL_SeqAIJ);CHKERRQ(ierr);
603e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaijmkl_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
604e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaijmkl_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
605e9c94282SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaijmkl_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
6064a2a386eSRichard Tran Mills 
6074a2a386eSRichard Tran Mills   ierr    = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJMKL);CHKERRQ(ierr);
6084a2a386eSRichard Tran Mills   *newmat = B;
6094a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
6104a2a386eSRichard Tran Mills }
6114a2a386eSRichard Tran Mills 
6124a2a386eSRichard Tran Mills /*@C
6134a2a386eSRichard Tran Mills    MatCreateSeqAIJMKL - Creates a sparse matrix of type SEQAIJMKL.
6144a2a386eSRichard Tran Mills    This type inherits from AIJ and is largely identical, but uses sparse BLAS
6154a2a386eSRichard Tran Mills    routines from Intel MKL whenever possible.
6164a2a386eSRichard Tran Mills    Collective on MPI_Comm
6174a2a386eSRichard Tran Mills 
6184a2a386eSRichard Tran Mills    Input Parameters:
6194a2a386eSRichard Tran Mills +  comm - MPI communicator, set to PETSC_COMM_SELF
6204a2a386eSRichard Tran Mills .  m - number of rows
6214a2a386eSRichard Tran Mills .  n - number of columns
6224a2a386eSRichard Tran Mills .  nz - number of nonzeros per row (same for all rows)
6234a2a386eSRichard Tran Mills -  nnz - array containing the number of nonzeros in the various rows
6244a2a386eSRichard Tran Mills          (possibly different for each row) or NULL
6254a2a386eSRichard Tran Mills 
6264a2a386eSRichard Tran Mills    Output Parameter:
6274a2a386eSRichard Tran Mills .  A - the matrix
6284a2a386eSRichard Tran Mills 
6294a2a386eSRichard Tran Mills    Notes:
6304a2a386eSRichard Tran Mills    If nnz is given then nz is ignored
6314a2a386eSRichard Tran Mills 
6324a2a386eSRichard Tran Mills    Level: intermediate
6334a2a386eSRichard Tran Mills 
6344a2a386eSRichard Tran Mills .keywords: matrix, cray, sparse, parallel
6354a2a386eSRichard Tran Mills 
6364a2a386eSRichard Tran Mills .seealso: MatCreate(), MatCreateMPIAIJMKL(), MatSetValues()
6374a2a386eSRichard Tran Mills @*/
6384a2a386eSRichard Tran Mills PetscErrorCode  MatCreateSeqAIJMKL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
6394a2a386eSRichard Tran Mills {
6404a2a386eSRichard Tran Mills   PetscErrorCode ierr;
6414a2a386eSRichard Tran Mills 
6424a2a386eSRichard Tran Mills   PetscFunctionBegin;
6434a2a386eSRichard Tran Mills   ierr = MatCreate(comm,A);CHKERRQ(ierr);
6444a2a386eSRichard Tran Mills   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
6454a2a386eSRichard Tran Mills   ierr = MatSetType(*A,MATSEQAIJMKL);CHKERRQ(ierr);
6464a2a386eSRichard Tran Mills   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
6474a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
6484a2a386eSRichard Tran Mills }
6494a2a386eSRichard Tran Mills 
6504a2a386eSRichard Tran Mills PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJMKL(Mat A)
6514a2a386eSRichard Tran Mills {
6524a2a386eSRichard Tran Mills   PetscErrorCode ierr;
6534a2a386eSRichard Tran Mills 
6544a2a386eSRichard Tran Mills   PetscFunctionBegin;
6554a2a386eSRichard Tran Mills   ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr);
6564a2a386eSRichard Tran Mills   ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr);
6574a2a386eSRichard Tran Mills   PetscFunctionReturn(0);
6584a2a386eSRichard Tran Mills }
659