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; 334a2a386eSRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 344a2a386eSRichard Tran Mills 354a2a386eSRichard Tran Mills PetscFunctionBegin; 364a2a386eSRichard Tran Mills if (reuse == MAT_INITIAL_MATRIX) { 374a2a386eSRichard Tran Mills ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr); 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; 4845fbe478SRichard Tran Mills B->ops->matmult = MatMatMult_SeqAIJ_SeqAIJ; 49372ec6bbSRichard Tran Mills B->ops->transposematmult = MatTransposeMatMult_SeqAIJ_SeqAIJ; 5087c2a1d7SRichard Tran Mills B->ops->scale = MatScale_SeqAIJ; 5187c2a1d7SRichard Tran Mills B->ops->diagonalscale = MatDiagonalScale_SeqAIJ; 5287c2a1d7SRichard Tran Mills B->ops->diagonalset = MatDiagonalSet_SeqAIJ; 5387c2a1d7SRichard Tran Mills B->ops->axpy = MatAXPY_SeqAIJ; 544a2a386eSRichard Tran Mills 55e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",NULL);CHKERRQ(ierr); 56e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr); 57e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr); 58e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr); 5945fbe478SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 60*4a940b00SSatish Balay if(!aijmkl->no_SpMV2) { 6145fbe478SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqaijmkl_seqaijmkl_C",NULL);CHKERRQ(ierr); 62372ec6bbSRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatTransposeMatMult_seqaijmkl_seqaijmkl_C",NULL);CHKERRQ(ierr); 6345fbe478SRichard Tran Mills } 64*4a940b00SSatish Balay #endif 65e9c94282SRichard Tran Mills 664abfa3b3SRichard Tran Mills /* Free everything in the Mat_SeqAIJMKL data structure. Currently, this 67e9c94282SRichard Tran Mills * simply involves destroying the MKL sparse matrix handle and then freeing 68e9c94282SRichard Tran Mills * the spptr pointer. */ 694abfa3b3SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 70a8327b06SKarl Rupp if (reuse == MAT_INITIAL_MATRIX) aijmkl = (Mat_SeqAIJMKL*)B->spptr; 71a8327b06SKarl Rupp 724abfa3b3SRichard Tran Mills if (aijmkl->sparse_optimized) { 730632b357SRichard Tran Mills sparse_status_t stat; 744abfa3b3SRichard Tran Mills stat = mkl_sparse_destroy(aijmkl->csrA); 754abfa3b3SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) { 764abfa3b3SRichard Tran Mills PetscFunctionReturn(PETSC_ERR_LIB); 774abfa3b3SRichard Tran Mills } 784abfa3b3SRichard Tran Mills } 794abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 80e9c94282SRichard Tran Mills ierr = PetscFree(B->spptr);CHKERRQ(ierr); 814a2a386eSRichard Tran Mills 824a2a386eSRichard Tran Mills /* Change the type of B to MATSEQAIJ. */ 834a2a386eSRichard Tran Mills ierr = PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ);CHKERRQ(ierr); 844a2a386eSRichard Tran Mills 854a2a386eSRichard Tran Mills *newmat = B; 864a2a386eSRichard Tran Mills PetscFunctionReturn(0); 874a2a386eSRichard Tran Mills } 884a2a386eSRichard Tran Mills 894a2a386eSRichard Tran Mills PetscErrorCode MatDestroy_SeqAIJMKL(Mat A) 904a2a386eSRichard Tran Mills { 914a2a386eSRichard Tran Mills PetscErrorCode ierr; 924a2a386eSRichard Tran Mills Mat_SeqAIJMKL *aijmkl = (Mat_SeqAIJMKL*) A->spptr; 934a2a386eSRichard Tran Mills 944a2a386eSRichard Tran Mills PetscFunctionBegin; 95e9c94282SRichard Tran Mills 96e9c94282SRichard Tran Mills /* If MatHeaderMerge() was used, then this SeqAIJMKL matrix will not have an 97e9c94282SRichard Tran Mills * spptr pointer. */ 98e9c94282SRichard Tran Mills if (aijmkl) { 994a2a386eSRichard Tran Mills /* Clean up everything in the Mat_SeqAIJMKL data structure, then free A->spptr. */ 1004abfa3b3SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 1014abfa3b3SRichard Tran Mills if (aijmkl->sparse_optimized) { 1024abfa3b3SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 1034abfa3b3SRichard Tran Mills stat = mkl_sparse_destroy(aijmkl->csrA); 1044abfa3b3SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) { 1054abfa3b3SRichard Tran Mills PetscFunctionReturn(PETSC_ERR_LIB); 1064abfa3b3SRichard Tran Mills } 1074abfa3b3SRichard Tran Mills } 1084abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 1094a2a386eSRichard Tran Mills ierr = PetscFree(A->spptr);CHKERRQ(ierr); 110e9c94282SRichard Tran Mills } 1114a2a386eSRichard Tran Mills 1124a2a386eSRichard Tran Mills /* Change the type of A back to SEQAIJ and use MatDestroy_SeqAIJ() 1134a2a386eSRichard Tran Mills * to destroy everything that remains. */ 1144a2a386eSRichard Tran Mills ierr = PetscObjectChangeTypeName((PetscObject)A, MATSEQAIJ);CHKERRQ(ierr); 1154a2a386eSRichard Tran Mills /* Note that I don't call MatSetType(). I believe this is because that 1164a2a386eSRichard Tran Mills * is only to be called when *building* a matrix. I could be wrong, but 1174a2a386eSRichard Tran Mills * that is how things work for the SuperLU matrix class. */ 1184a2a386eSRichard Tran Mills ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr); 1194a2a386eSRichard Tran Mills PetscFunctionReturn(0); 1204a2a386eSRichard Tran Mills } 1214a2a386eSRichard Tran Mills 1225b49642aSRichard Tran Mills /* MatSeqAIJKL_create_mkl_handle(), if called with an AIJMKL matrix that has not had mkl_sparse_optimize() called for it, 1235b49642aSRichard Tran Mills * creates an MKL sparse matrix handle from the AIJ arrays and calls mkl_sparse_optimize(). 1245b49642aSRichard Tran Mills * If called with an AIJMKL matrix for which aijmkl->sparse_optimized == PETSC_TRUE, then it destroys the old matrix 1255b49642aSRichard Tran Mills * handle, creates a new one, and then calls mkl_sparse_optimize(). 1265b49642aSRichard Tran Mills * Although in normal MKL usage it is possible to have a valid matrix handle on which mkl_sparse_optimize() has not been 1275b49642aSRichard Tran Mills * called, for AIJMKL the handle creation and optimization step always occur together, so we don't handle the case of 1285b49642aSRichard Tran Mills * an unoptimized matrix handle here. */ 1296e369cd5SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_mkl_handle(Mat A) 1304a2a386eSRichard Tran Mills { 1316e369cd5SRichard Tran Mills #ifndef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 1326e369cd5SRichard Tran Mills /* If the MKL library does not have mkl_sparse_optimize(), then this routine 1336e369cd5SRichard Tran Mills * does nothing. We make it callable anyway in this case because it cuts 1346e369cd5SRichard Tran Mills * down on littering the code with #ifdefs. */ 13545fbe478SRichard Tran Mills PetscFunctionBegin; 1366e369cd5SRichard Tran Mills PetscFunctionReturn(0); 1376e369cd5SRichard Tran Mills #else 138a8327b06SKarl Rupp Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 139a8327b06SKarl Rupp Mat_SeqAIJMKL *aijmkl = (Mat_SeqAIJMKL*)A->spptr; 140a8327b06SKarl Rupp PetscInt m,n; 141a8327b06SKarl Rupp MatScalar *aa; 142a8327b06SKarl Rupp PetscInt *aj,*ai; 1436e369cd5SRichard Tran Mills sparse_status_t stat; 1444a2a386eSRichard Tran Mills 145a8327b06SKarl Rupp PetscFunctionBegin; 1466e369cd5SRichard Tran Mills if (aijmkl->no_SpMV2) PetscFunctionReturn(0); 1476e369cd5SRichard Tran Mills 1480632b357SRichard Tran Mills if (aijmkl->sparse_optimized) { 1490632b357SRichard Tran Mills /* Matrix has been previously assembled and optimized. Must destroy old 1500632b357SRichard Tran Mills * matrix handle before running the optimization step again. */ 1510632b357SRichard Tran Mills stat = mkl_sparse_destroy(aijmkl->csrA); 1520632b357SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) { 1530632b357SRichard Tran Mills PetscFunctionReturn(PETSC_ERR_LIB); 1540632b357SRichard Tran Mills } 1550632b357SRichard Tran Mills } 1568d3fe1b0SRichard Tran Mills aijmkl->sparse_optimized = PETSC_FALSE; 1576e369cd5SRichard Tran Mills 158c9d46305SRichard Tran Mills /* Now perform the SpMV2 setup and matrix optimization. */ 159df555b71SRichard Tran Mills aijmkl->descr.type = SPARSE_MATRIX_TYPE_GENERAL; 160df555b71SRichard Tran Mills aijmkl->descr.mode = SPARSE_FILL_MODE_LOWER; 161df555b71SRichard Tran Mills aijmkl->descr.diag = SPARSE_DIAG_NON_UNIT; 16258678438SRichard Tran Mills m = A->rmap->n; 16358678438SRichard Tran Mills n = A->cmap->n; 164df555b71SRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 165df555b71SRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 166df555b71SRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 16780095d54SIrina Sokolova if ((a->nz!=0) & !(A->structure_only)) { 1688d3fe1b0SRichard Tran Mills /* Create a new, optimized sparse matrix handle only if the matrix has nonzero entries. 1698d3fe1b0SRichard Tran Mills * The MKL sparse-inspector executor routines don't like being passed an empty matrix. */ 17058678438SRichard Tran Mills stat = mkl_sparse_x_create_csr(&aijmkl->csrA,SPARSE_INDEX_BASE_ZERO,m,n,ai,ai+1,aj,aa); 171df555b71SRichard Tran Mills stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000); 172df555b71SRichard Tran Mills stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE); 173df555b71SRichard Tran Mills stat = mkl_sparse_optimize(aijmkl->csrA); 174df555b71SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) { 175f68ad4bdSRichard Tran Mills SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to create matrix handle/complete mkl_sparse_optimize"); 176df555b71SRichard Tran Mills PetscFunctionReturn(PETSC_ERR_LIB); 177df555b71SRichard Tran Mills } 1784abfa3b3SRichard Tran Mills aijmkl->sparse_optimized = PETSC_TRUE; 179c9d46305SRichard Tran Mills } 1806e369cd5SRichard Tran Mills 1816e369cd5SRichard Tran Mills PetscFunctionReturn(0); 182d995685eSRichard Tran Mills #endif 1836e369cd5SRichard Tran Mills } 1846e369cd5SRichard Tran Mills 18519afcda9SRichard Tran Mills /* MatSeqAIJMKL_create_from_mkl_handle() creates a sequential AIJMKL matrix from an MKL sparse matrix handle. 18619afcda9SRichard Tran Mills * We need this to implement MatMatMult() using the MKL inspector-executor routines, which return an (unoptimized) 1876c87cf42SRichard Tran Mills * matrix handle. 188aab60f1bSRichard Tran Mills * Note: This routine simply destroys and replaces the original matrix if MAT_REUSE_MATRIX has been specified, as 189aab60f1bSRichard Tran Mills * there is no good alternative. */ 19019afcda9SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 1916c87cf42SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_from_mkl_handle(MPI_Comm comm,sparse_matrix_t csrA,MatReuse reuse,Mat *mat) 19219afcda9SRichard Tran Mills { 19319afcda9SRichard Tran Mills PetscErrorCode ierr; 19419afcda9SRichard Tran Mills sparse_status_t stat; 19519afcda9SRichard Tran Mills sparse_index_base_t indexing; 19619afcda9SRichard Tran Mills PetscInt nrows, ncols; 19745fbe478SRichard Tran Mills PetscInt *aj,*ai,*dummy; 19819afcda9SRichard Tran Mills MatScalar *aa; 19919afcda9SRichard Tran Mills Mat A; 2006c87cf42SRichard Tran Mills Mat_SeqAIJ *a; 20119afcda9SRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 20219afcda9SRichard Tran Mills 20345fbe478SRichard Tran Mills /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */ 20445fbe478SRichard Tran Mills stat = mkl_sparse_x_export_csr(csrA,&indexing,&nrows,&ncols,&ai,&dummy,&aj,&aa); 20519afcda9SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) { 20619afcda9SRichard Tran Mills SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_x_export_csr()"); 20719afcda9SRichard Tran Mills PetscFunctionReturn(PETSC_ERR_LIB); 20819afcda9SRichard Tran Mills } 2096c87cf42SRichard Tran Mills 210aab60f1bSRichard Tran Mills if (reuse == MAT_REUSE_MATRIX) { 211aab60f1bSRichard Tran Mills ierr = MatDestroy(mat);CHKERRQ(ierr); 212aab60f1bSRichard Tran Mills } 21319afcda9SRichard Tran Mills ierr = MatCreate(comm,&A);CHKERRQ(ierr); 21419afcda9SRichard Tran Mills ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr); 21545fbe478SRichard Tran Mills ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,nrows,ncols);CHKERRQ(ierr); 216aab60f1bSRichard Tran Mills /* We use MatSeqAIJSetPreallocationCSR() instead of MatCreateSeqAIJWithArrays() because we must copy the arrays exported 217aab60f1bSRichard Tran Mills * from MKL; MKL developers tell us that modifying the arrays may cause unexpected results when using the MKL handle, and 218aab60f1bSRichard Tran Mills * they will be destroyed when the MKL handle is destroyed. 219aab60f1bSRichard Tran Mills * (In the interest of reducing memory consumption in future, can we figure out good ways to deal with this?) */ 22019afcda9SRichard Tran Mills ierr = MatSeqAIJSetPreallocationCSR(A,ai,aj,aa);CHKERRQ(ierr); 22119afcda9SRichard Tran Mills 22219afcda9SRichard Tran Mills /* We now have an assembled sequential AIJ matrix created from copies of the exported arrays from the MKL matrix handle. 22319afcda9SRichard Tran Mills * Now turn it into a MATSEQAIJMKL. */ 22419afcda9SRichard Tran Mills ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr); 2256c87cf42SRichard Tran Mills 2266c87cf42SRichard Tran Mills a = (Mat_SeqAIJ*)A->data; 22719afcda9SRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 22819afcda9SRichard Tran Mills aijmkl->csrA = csrA; 2296c87cf42SRichard Tran Mills 23019afcda9SRichard Tran Mills /* The below code duplicates much of what is in MatSeqAIJKL_create_mkl_handle(). I dislike this code duplication, but 23119afcda9SRichard Tran Mills * MatSeqAIJMKL_create_mkl_handle() cannot be used because we don't need to create a handle -- we've already got one, 23219afcda9SRichard Tran Mills * and just need to be able to run the MKL optimization step. */ 233f3fd1758SRichard Tran Mills aijmkl->descr.type = SPARSE_MATRIX_TYPE_GENERAL; 234f3fd1758SRichard Tran Mills aijmkl->descr.mode = SPARSE_FILL_MODE_LOWER; 235f3fd1758SRichard Tran Mills aijmkl->descr.diag = SPARSE_DIAG_NON_UNIT; 23619afcda9SRichard Tran Mills stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000); 23719afcda9SRichard Tran Mills stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE); 23819afcda9SRichard Tran Mills stat = mkl_sparse_optimize(aijmkl->csrA); 23919afcda9SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) { 24019afcda9SRichard Tran Mills SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set hints/complete mkl_sparse_optimize"); 24119afcda9SRichard Tran Mills PetscFunctionReturn(PETSC_ERR_LIB); 24219afcda9SRichard Tran Mills } 24319afcda9SRichard Tran Mills aijmkl->sparse_optimized = PETSC_TRUE; 24419afcda9SRichard Tran Mills 24519afcda9SRichard Tran Mills *mat = A; 24619afcda9SRichard Tran Mills PetscFunctionReturn(0); 24719afcda9SRichard Tran Mills } 24819afcda9SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 24919afcda9SRichard Tran Mills 2506e369cd5SRichard Tran Mills PetscErrorCode MatDuplicate_SeqAIJMKL(Mat A, MatDuplicateOption op, Mat *M) 2516e369cd5SRichard Tran Mills { 2526e369cd5SRichard Tran Mills PetscErrorCode ierr; 2536e369cd5SRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 2546e369cd5SRichard Tran Mills Mat_SeqAIJMKL *aijmkl_dest; 2556e369cd5SRichard Tran Mills 2566e369cd5SRichard Tran Mills PetscFunctionBegin; 2576e369cd5SRichard Tran Mills ierr = MatDuplicate_SeqAIJ(A,op,M);CHKERRQ(ierr); 2586e369cd5SRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 2596e369cd5SRichard Tran Mills aijmkl_dest = (Mat_SeqAIJMKL*) (*M)->spptr; 2606e369cd5SRichard Tran Mills ierr = PetscMemcpy(aijmkl_dest,aijmkl,sizeof(Mat_SeqAIJMKL));CHKERRQ(ierr); 2616e369cd5SRichard Tran Mills aijmkl_dest->sparse_optimized = PETSC_FALSE; 2625b49642aSRichard Tran Mills if (aijmkl->eager_inspection) { 2636e369cd5SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr); 2645b49642aSRichard Tran Mills } 2656e369cd5SRichard Tran Mills PetscFunctionReturn(0); 2666e369cd5SRichard Tran Mills } 2676e369cd5SRichard Tran Mills 2686e369cd5SRichard Tran Mills PetscErrorCode MatAssemblyEnd_SeqAIJMKL(Mat A, MatAssemblyType mode) 2696e369cd5SRichard Tran Mills { 2706e369cd5SRichard Tran Mills PetscErrorCode ierr; 2716e369cd5SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2725b49642aSRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 2736e369cd5SRichard Tran Mills 2746e369cd5SRichard Tran Mills PetscFunctionBegin; 2756e369cd5SRichard Tran Mills if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 2766e369cd5SRichard Tran Mills 2776e369cd5SRichard Tran Mills /* Since a MATSEQAIJMKL matrix is really just a MATSEQAIJ with some 2786e369cd5SRichard Tran Mills * extra information and some different methods, call the AssemblyEnd 2796e369cd5SRichard Tran Mills * routine for a MATSEQAIJ. 2806e369cd5SRichard Tran Mills * I'm not sure if this is the best way to do this, but it avoids 281d96e85feSRichard Tran Mills * a lot of code duplication. */ 2826e369cd5SRichard Tran Mills a->inode.use = PETSC_FALSE; /* Must disable: otherwise the MKL routines won't get used. */ 2836e369cd5SRichard Tran Mills ierr = MatAssemblyEnd_SeqAIJ(A, mode);CHKERRQ(ierr); 2846e369cd5SRichard Tran Mills 2855b49642aSRichard Tran Mills /* If the user has requested "eager" inspection, create the optimized MKL sparse handle (if needed; the function checks). 2865b49642aSRichard Tran Mills * (The default is to do "lazy" inspection, deferring this until something like MatMult() is called.) */ 2875b49642aSRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 2885b49642aSRichard Tran Mills if (aijmkl->eager_inspection) { 2896e369cd5SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr); 290*4a940b00SSatish Balay #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 291886913bfSRichard Tran Mills } else if (aijmkl->sparse_optimized) { 292886913bfSRichard Tran Mills /* If doing lazy inspection and there is an optimized MKL handle, we need to destroy it, so that it will be 293886913bfSRichard Tran Mills * rebuilt later when needed. Otherwise, some SeqAIJ implementations that we depend on for some operations 294886913bfSRichard Tran Mills * (such as MatMatMultNumeric()) can modify the result matrix without the matrix handle being rebuilt. 2957225e97aSRichard Tran Mills * (The SeqAIJ version MatMatMultNumeric() knows nothing about matrix handles, but it *does* call MatAssemblyEnd().) */ 296886913bfSRichard Tran Mills sparse_status_t stat = mkl_sparse_destroy(aijmkl->csrA); 297886913bfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) { 298886913bfSRichard Tran Mills PetscFunctionReturn(PETSC_ERR_LIB); 299886913bfSRichard Tran Mills } 300886913bfSRichard Tran Mills aijmkl->sparse_optimized = PETSC_FALSE; 301*4a940b00SSatish Balay #endif 3025b49642aSRichard Tran Mills } 303df555b71SRichard Tran Mills 3044a2a386eSRichard Tran Mills PetscFunctionReturn(0); 3054a2a386eSRichard Tran Mills } 3064a2a386eSRichard Tran Mills 3074a2a386eSRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL(Mat A,Vec xx,Vec yy) 3084a2a386eSRichard Tran Mills { 3094a2a386eSRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3104a2a386eSRichard Tran Mills const PetscScalar *x; 3114a2a386eSRichard Tran Mills PetscScalar *y; 3124a2a386eSRichard Tran Mills const MatScalar *aa; 3134a2a386eSRichard Tran Mills PetscErrorCode ierr; 3144a2a386eSRichard Tran Mills PetscInt m=A->rmap->n; 315db63039fSRichard Tran Mills PetscInt n=A->cmap->n; 316db63039fSRichard Tran Mills PetscScalar alpha = 1.0; 317db63039fSRichard Tran Mills PetscScalar beta = 0.0; 3184a2a386eSRichard Tran Mills const PetscInt *aj,*ai; 319db63039fSRichard Tran Mills char matdescra[6]; 320db63039fSRichard Tran Mills 3214a2a386eSRichard Tran Mills 3224a2a386eSRichard Tran Mills /* Variables not in MatMult_SeqAIJ. */ 323ff03dc53SRichard Tran Mills char transa = 'n'; /* Used to indicate to MKL that we are not computing the transpose product. */ 324ff03dc53SRichard Tran Mills 325ff03dc53SRichard Tran Mills PetscFunctionBegin; 326db63039fSRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 327db63039fSRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 328ff03dc53SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 329ff03dc53SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 330ff03dc53SRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 331ff03dc53SRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 332ff03dc53SRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 333ff03dc53SRichard Tran Mills 334ff03dc53SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 335db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y); 336ff03dc53SRichard Tran Mills 337ff03dc53SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 338ff03dc53SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 339ff03dc53SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 340ff03dc53SRichard Tran Mills PetscFunctionReturn(0); 341ff03dc53SRichard Tran Mills } 342ff03dc53SRichard Tran Mills 343d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 344df555b71SRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy) 345df555b71SRichard Tran Mills { 346df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 347df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 348df555b71SRichard Tran Mills const PetscScalar *x; 349df555b71SRichard Tran Mills PetscScalar *y; 350df555b71SRichard Tran Mills PetscErrorCode ierr; 351df555b71SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 352df555b71SRichard Tran Mills 353df555b71SRichard Tran Mills PetscFunctionBegin; 354df555b71SRichard Tran Mills 35538987b35SRichard Tran Mills /* If there are no nonzero entries, zero yy and return immediately. */ 35638987b35SRichard Tran Mills if(!a->nz) { 35738987b35SRichard Tran Mills PetscInt i; 35838987b35SRichard Tran Mills PetscInt m=A->rmap->n; 35938987b35SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 36038987b35SRichard Tran Mills for (i=0; i<m; i++) { 36138987b35SRichard Tran Mills y[i] = 0.0; 36238987b35SRichard Tran Mills } 36338987b35SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 36438987b35SRichard Tran Mills PetscFunctionReturn(0); 36538987b35SRichard Tran Mills } 366f36dfe3fSRichard Tran Mills 367df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 368df555b71SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 369df555b71SRichard Tran Mills 3703fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 3713fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 3723fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 3733fa15762SRichard Tran Mills if (!aijmkl->sparse_optimized) { 3743fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 3753fa15762SRichard Tran Mills } 3763fa15762SRichard Tran Mills 377df555b71SRichard Tran Mills /* Call MKL SpMV2 executor routine to do the MatMult. */ 378df555b71SRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y); 379df555b71SRichard Tran Mills 380df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 381df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 382df555b71SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 383df555b71SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) { 384df555b71SRichard Tran Mills PetscFunctionReturn(PETSC_ERR_LIB); 385df555b71SRichard Tran Mills } 386df555b71SRichard Tran Mills PetscFunctionReturn(0); 387df555b71SRichard Tran Mills } 388d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 389df555b71SRichard Tran Mills 390ff03dc53SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL(Mat A,Vec xx,Vec yy) 391ff03dc53SRichard Tran Mills { 392ff03dc53SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 393ff03dc53SRichard Tran Mills const PetscScalar *x; 394ff03dc53SRichard Tran Mills PetscScalar *y; 395ff03dc53SRichard Tran Mills const MatScalar *aa; 396ff03dc53SRichard Tran Mills PetscErrorCode ierr; 397ff03dc53SRichard Tran Mills PetscInt m=A->rmap->n; 398db63039fSRichard Tran Mills PetscInt n=A->cmap->n; 399db63039fSRichard Tran Mills PetscScalar alpha = 1.0; 400db63039fSRichard Tran Mills PetscScalar beta = 0.0; 401ff03dc53SRichard Tran Mills const PetscInt *aj,*ai; 402db63039fSRichard Tran Mills char matdescra[6]; 403ff03dc53SRichard Tran Mills 404ff03dc53SRichard Tran Mills /* Variables not in MatMultTranspose_SeqAIJ. */ 405ff03dc53SRichard Tran Mills char transa = 't'; /* Used to indicate to MKL that we are computing the transpose product. */ 4064a2a386eSRichard Tran Mills 4074a2a386eSRichard Tran Mills PetscFunctionBegin; 408969800c5SRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 409969800c5SRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 4104a2a386eSRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 4114a2a386eSRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 4124a2a386eSRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 4134a2a386eSRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 4144a2a386eSRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 4154a2a386eSRichard Tran Mills 4164a2a386eSRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 417db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y); 4184a2a386eSRichard Tran Mills 4194a2a386eSRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 4204a2a386eSRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 4214a2a386eSRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 4224a2a386eSRichard Tran Mills PetscFunctionReturn(0); 4234a2a386eSRichard Tran Mills } 4244a2a386eSRichard Tran Mills 425d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 426df555b71SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy) 427df555b71SRichard Tran Mills { 428df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 429df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 430df555b71SRichard Tran Mills const PetscScalar *x; 431df555b71SRichard Tran Mills PetscScalar *y; 432df555b71SRichard Tran Mills PetscErrorCode ierr; 4330632b357SRichard Tran Mills sparse_status_t stat; 434df555b71SRichard Tran Mills 435df555b71SRichard Tran Mills PetscFunctionBegin; 436df555b71SRichard Tran Mills 43738987b35SRichard Tran Mills /* If there are no nonzero entries, zero yy and return immediately. */ 43838987b35SRichard Tran Mills if(!a->nz) { 43938987b35SRichard Tran Mills PetscInt i; 44038987b35SRichard Tran Mills PetscInt n=A->cmap->n; 44138987b35SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 44238987b35SRichard Tran Mills for (i=0; i<n; i++) { 44338987b35SRichard Tran Mills y[i] = 0.0; 44438987b35SRichard Tran Mills } 44538987b35SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 44638987b35SRichard Tran Mills PetscFunctionReturn(0); 44738987b35SRichard Tran Mills } 448f36dfe3fSRichard Tran Mills 449df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 450df555b71SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 451df555b71SRichard Tran Mills 4523fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 4533fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 4543fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 4553fa15762SRichard Tran Mills if (!aijmkl->sparse_optimized) { 4563fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 4573fa15762SRichard Tran Mills } 4583fa15762SRichard Tran Mills 459df555b71SRichard Tran Mills /* Call MKL SpMV2 executor routine to do the MatMultTranspose. */ 460df555b71SRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y); 461df555b71SRichard Tran Mills 462df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 463df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 464df555b71SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 465df555b71SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) { 466df555b71SRichard Tran Mills PetscFunctionReturn(PETSC_ERR_LIB); 467df555b71SRichard Tran Mills } 468df555b71SRichard Tran Mills PetscFunctionReturn(0); 469df555b71SRichard Tran Mills } 470d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 471df555b71SRichard Tran Mills 4724a2a386eSRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz) 4734a2a386eSRichard Tran Mills { 4744a2a386eSRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 4754a2a386eSRichard Tran Mills const PetscScalar *x; 4764a2a386eSRichard Tran Mills PetscScalar *y,*z; 4774a2a386eSRichard Tran Mills const MatScalar *aa; 4784a2a386eSRichard Tran Mills PetscErrorCode ierr; 4794a2a386eSRichard Tran Mills PetscInt m=A->rmap->n; 480db63039fSRichard Tran Mills PetscInt n=A->cmap->n; 4814a2a386eSRichard Tran Mills const PetscInt *aj,*ai; 4824a2a386eSRichard Tran Mills PetscInt i; 4834a2a386eSRichard Tran Mills 484ff03dc53SRichard Tran Mills /* Variables not in MatMultAdd_SeqAIJ. */ 485ff03dc53SRichard Tran Mills char transa = 'n'; /* Used to indicate to MKL that we are not computing the transpose product. */ 486a84739b8SRichard Tran Mills PetscScalar alpha = 1.0; 487db63039fSRichard Tran Mills PetscScalar beta; 488a84739b8SRichard Tran Mills char matdescra[6]; 489ff03dc53SRichard Tran Mills 490ff03dc53SRichard Tran Mills PetscFunctionBegin; 491a84739b8SRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 492a84739b8SRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 493a84739b8SRichard Tran Mills 494ff03dc53SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 495ff03dc53SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 496ff03dc53SRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 497ff03dc53SRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 498ff03dc53SRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 499ff03dc53SRichard Tran Mills 500ff03dc53SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 501a84739b8SRichard Tran Mills if (zz == yy) { 502a84739b8SRichard 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. */ 503db63039fSRichard Tran Mills beta = 1.0; 504db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 505a84739b8SRichard Tran Mills } else { 506db63039fSRichard Tran Mills /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z. 507db63039fSRichard Tran Mills * MKL sparse BLAS does not have a MatMultAdd equivalent. */ 508db63039fSRichard Tran Mills beta = 0.0; 509db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 510ff03dc53SRichard Tran Mills for (i=0; i<m; i++) { 511ff03dc53SRichard Tran Mills z[i] += y[i]; 512ff03dc53SRichard Tran Mills } 513a84739b8SRichard Tran Mills } 514ff03dc53SRichard Tran Mills 515ff03dc53SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 516ff03dc53SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 517ff03dc53SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 518ff03dc53SRichard Tran Mills PetscFunctionReturn(0); 519ff03dc53SRichard Tran Mills } 520ff03dc53SRichard Tran Mills 521d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 522df555b71SRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz) 523df555b71SRichard Tran Mills { 524df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 525df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 526df555b71SRichard Tran Mills const PetscScalar *x; 527df555b71SRichard Tran Mills PetscScalar *y,*z; 528df555b71SRichard Tran Mills PetscErrorCode ierr; 529df555b71SRichard Tran Mills PetscInt m=A->rmap->n; 530df555b71SRichard Tran Mills PetscInt i; 531df555b71SRichard Tran Mills 532df555b71SRichard Tran Mills /* Variables not in MatMultAdd_SeqAIJ. */ 533df555b71SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 534df555b71SRichard Tran Mills 535df555b71SRichard Tran Mills PetscFunctionBegin; 536df555b71SRichard Tran Mills 53738987b35SRichard Tran Mills /* If there are no nonzero entries, set zz = yy and return immediately. */ 53838987b35SRichard Tran Mills if(!a->nz) { 53938987b35SRichard Tran Mills PetscInt i; 54038987b35SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 54138987b35SRichard Tran Mills for (i=0; i<m; i++) { 54238987b35SRichard Tran Mills z[i] = y[i]; 54338987b35SRichard Tran Mills } 54438987b35SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 54538987b35SRichard Tran Mills PetscFunctionReturn(0); 54638987b35SRichard Tran Mills } 547df555b71SRichard Tran Mills 548df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 549df555b71SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 550df555b71SRichard Tran Mills 5513fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 5523fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 5533fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 5543fa15762SRichard Tran Mills if (!aijmkl->sparse_optimized) { 5553fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 5563fa15762SRichard Tran Mills } 5573fa15762SRichard Tran Mills 558df555b71SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 559df555b71SRichard Tran Mills if (zz == yy) { 560df555b71SRichard 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, 561df555b71SRichard Tran Mills * with alpha and beta both set to 1.0. */ 562db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z); 563df555b71SRichard Tran Mills } else { 564df555b71SRichard 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 565df555b71SRichard Tran Mills * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */ 566db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z); 567df555b71SRichard Tran Mills for (i=0; i<m; i++) { 568df555b71SRichard Tran Mills z[i] += y[i]; 569df555b71SRichard Tran Mills } 570df555b71SRichard Tran Mills } 571df555b71SRichard Tran Mills 572df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 573df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 574df555b71SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 575df555b71SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) { 576df555b71SRichard Tran Mills PetscFunctionReturn(PETSC_ERR_LIB); 577df555b71SRichard Tran Mills } 578df555b71SRichard Tran Mills PetscFunctionReturn(0); 579df555b71SRichard Tran Mills } 580d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 581df555b71SRichard Tran Mills 582ff03dc53SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz) 583ff03dc53SRichard Tran Mills { 584ff03dc53SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 585ff03dc53SRichard Tran Mills const PetscScalar *x; 586ff03dc53SRichard Tran Mills PetscScalar *y,*z; 587ff03dc53SRichard Tran Mills const MatScalar *aa; 588ff03dc53SRichard Tran Mills PetscErrorCode ierr; 589ff03dc53SRichard Tran Mills PetscInt m=A->rmap->n; 590db63039fSRichard Tran Mills PetscInt n=A->cmap->n; 591ff03dc53SRichard Tran Mills const PetscInt *aj,*ai; 592ff03dc53SRichard Tran Mills PetscInt i; 593ff03dc53SRichard Tran Mills 594ff03dc53SRichard Tran Mills /* Variables not in MatMultTransposeAdd_SeqAIJ. */ 595ff03dc53SRichard Tran Mills char transa = 't'; /* Used to indicate to MKL that we are computing the transpose product. */ 596a84739b8SRichard Tran Mills PetscScalar alpha = 1.0; 597db63039fSRichard Tran Mills PetscScalar beta; 598a84739b8SRichard Tran Mills char matdescra[6]; 5994a2a386eSRichard Tran Mills 6004a2a386eSRichard Tran Mills PetscFunctionBegin; 601a84739b8SRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 602a84739b8SRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 603a84739b8SRichard Tran Mills 6044a2a386eSRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 6054a2a386eSRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 6064a2a386eSRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 6074a2a386eSRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 6084a2a386eSRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 6094a2a386eSRichard Tran Mills 6104a2a386eSRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 611a84739b8SRichard Tran Mills if (zz == yy) { 612a84739b8SRichard 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. */ 613db63039fSRichard Tran Mills beta = 1.0; 614969800c5SRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 615a84739b8SRichard Tran Mills } else { 616db63039fSRichard Tran Mills /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z. 617db63039fSRichard Tran Mills * MKL sparse BLAS does not have a MatMultAdd equivalent. */ 618db63039fSRichard Tran Mills beta = 0.0; 619db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 620969800c5SRichard Tran Mills for (i=0; i<n; i++) { 6214a2a386eSRichard Tran Mills z[i] += y[i]; 6224a2a386eSRichard Tran Mills } 623a84739b8SRichard Tran Mills } 6244a2a386eSRichard Tran Mills 6254a2a386eSRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 6264a2a386eSRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 6274a2a386eSRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 6284a2a386eSRichard Tran Mills PetscFunctionReturn(0); 6294a2a386eSRichard Tran Mills } 6304a2a386eSRichard Tran Mills 631d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 632df555b71SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz) 633df555b71SRichard Tran Mills { 634df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 635df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 636df555b71SRichard Tran Mills const PetscScalar *x; 637df555b71SRichard Tran Mills PetscScalar *y,*z; 638df555b71SRichard Tran Mills PetscErrorCode ierr; 639969800c5SRichard Tran Mills PetscInt n=A->cmap->n; 640df555b71SRichard Tran Mills PetscInt i; 641df555b71SRichard Tran Mills 642df555b71SRichard Tran Mills /* Variables not in MatMultTransposeAdd_SeqAIJ. */ 643df555b71SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 644df555b71SRichard Tran Mills 645df555b71SRichard Tran Mills PetscFunctionBegin; 646df555b71SRichard Tran Mills 64738987b35SRichard Tran Mills /* If there are no nonzero entries, set zz = yy and return immediately. */ 64838987b35SRichard Tran Mills if(!a->nz) { 64938987b35SRichard Tran Mills PetscInt i; 65038987b35SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 65138987b35SRichard Tran Mills for (i=0; i<n; i++) { 65238987b35SRichard Tran Mills z[i] = y[i]; 65338987b35SRichard Tran Mills } 65438987b35SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 65538987b35SRichard Tran Mills PetscFunctionReturn(0); 65638987b35SRichard Tran Mills } 657f36dfe3fSRichard Tran Mills 658df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 659df555b71SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 660df555b71SRichard Tran Mills 6613fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 6623fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 6633fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 6643fa15762SRichard Tran Mills if (!aijmkl->sparse_optimized) { 6653fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 6663fa15762SRichard Tran Mills } 6673fa15762SRichard Tran Mills 668df555b71SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 669df555b71SRichard Tran Mills if (zz == yy) { 670df555b71SRichard 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, 671df555b71SRichard Tran Mills * with alpha and beta both set to 1.0. */ 672db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z); 673df555b71SRichard Tran Mills } else { 674df555b71SRichard 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 675df555b71SRichard Tran Mills * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */ 676db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z); 677969800c5SRichard Tran Mills for (i=0; i<n; i++) { 678df555b71SRichard Tran Mills z[i] += y[i]; 679df555b71SRichard Tran Mills } 680df555b71SRichard Tran Mills } 681df555b71SRichard Tran Mills 682df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 683df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 684df555b71SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 685df555b71SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) { 686df555b71SRichard Tran Mills PetscFunctionReturn(PETSC_ERR_LIB); 687df555b71SRichard Tran Mills } 688df555b71SRichard Tran Mills PetscFunctionReturn(0); 689df555b71SRichard Tran Mills } 690d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 691df555b71SRichard Tran Mills 69245fbe478SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 693aab60f1bSRichard Tran Mills /* Note that this code currently doesn't actually get used when MatMatMult() is called with MAT_REUSE_MATRIX, because 694aab60f1bSRichard Tran Mills * the MatMatMult() interface code calls MatMatMultNumeric() in this case. 695aab60f1bSRichard Tran Mills * MKL has no notion of separately callable symbolic vs. numeric phases of sparse matrix-matrix multiply, so in the 696aab60f1bSRichard Tran Mills * MAT_REUSE_MATRIX case, the SeqAIJ routines end up being used. Even though this means that the (hopefully more 697aab60f1bSRichard Tran Mills * optimized) MKL routines do not get used, this probably is best because the MKL routines would waste time re-computing 698aab60f1bSRichard Tran Mills * the symbolic portion, whereas the native PETSc SeqAIJ routines will avoid this. */ 69945fbe478SRichard Tran Mills PetscErrorCode MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat*C) 70045fbe478SRichard Tran Mills { 70145fbe478SRichard Tran Mills Mat_SeqAIJMKL *a, *b; 70245fbe478SRichard Tran Mills sparse_matrix_t csrA, csrB, csrC; 70345fbe478SRichard Tran Mills PetscErrorCode ierr; 70445fbe478SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 70545fbe478SRichard Tran Mills 70645fbe478SRichard Tran Mills PetscFunctionBegin; 70745fbe478SRichard Tran Mills a = (Mat_SeqAIJMKL*)A->spptr; 70845fbe478SRichard Tran Mills b = (Mat_SeqAIJMKL*)B->spptr; 70945fbe478SRichard Tran Mills if (!a->sparse_optimized) { 71045fbe478SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 71145fbe478SRichard Tran Mills } 71245fbe478SRichard Tran Mills if (!b->sparse_optimized) { 71345fbe478SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(B); 71445fbe478SRichard Tran Mills } 71545fbe478SRichard Tran Mills csrA = a->csrA; 71645fbe478SRichard Tran Mills csrB = b->csrA; 71745fbe478SRichard Tran Mills 71845fbe478SRichard Tran Mills stat = mkl_sparse_spmm(SPARSE_OPERATION_NON_TRANSPOSE,csrA,csrB,&csrC); 71945fbe478SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) { 72045fbe478SRichard Tran Mills SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete sparse matrix-matrix multiply"); 72145fbe478SRichard Tran Mills PetscFunctionReturn(PETSC_ERR_LIB); 72245fbe478SRichard Tran Mills } 72345fbe478SRichard Tran Mills 7246c87cf42SRichard Tran Mills ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr); 72545fbe478SRichard Tran Mills 72645fbe478SRichard Tran Mills PetscFunctionReturn(0); 72745fbe478SRichard Tran Mills } 72845fbe478SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 72945fbe478SRichard Tran Mills 730372ec6bbSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 731372ec6bbSRichard Tran Mills PetscErrorCode MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat*C) 732372ec6bbSRichard Tran Mills { 733372ec6bbSRichard Tran Mills Mat_SeqAIJMKL *a, *b; 734372ec6bbSRichard Tran Mills sparse_matrix_t csrA, csrB, csrC; 735372ec6bbSRichard Tran Mills PetscErrorCode ierr; 736372ec6bbSRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 737372ec6bbSRichard Tran Mills 738372ec6bbSRichard Tran Mills PetscFunctionBegin; 739372ec6bbSRichard Tran Mills a = (Mat_SeqAIJMKL*)A->spptr; 740372ec6bbSRichard Tran Mills b = (Mat_SeqAIJMKL*)B->spptr; 741372ec6bbSRichard Tran Mills if (!a->sparse_optimized) { 742372ec6bbSRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 743372ec6bbSRichard Tran Mills } 744372ec6bbSRichard Tran Mills if (!b->sparse_optimized) { 745372ec6bbSRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(B); 746372ec6bbSRichard Tran Mills } 747372ec6bbSRichard Tran Mills csrA = a->csrA; 748372ec6bbSRichard Tran Mills csrB = b->csrA; 749372ec6bbSRichard Tran Mills 750372ec6bbSRichard Tran Mills stat = mkl_sparse_spmm(SPARSE_OPERATION_TRANSPOSE,csrA,csrB,&csrC); 751372ec6bbSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) { 752372ec6bbSRichard Tran Mills SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete sparse matrix-matrix multiply"); 753372ec6bbSRichard Tran Mills PetscFunctionReturn(PETSC_ERR_LIB); 754372ec6bbSRichard Tran Mills } 755372ec6bbSRichard Tran Mills 756372ec6bbSRichard Tran Mills ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr); 757372ec6bbSRichard Tran Mills 758372ec6bbSRichard Tran Mills PetscFunctionReturn(0); 759372ec6bbSRichard Tran Mills } 760372ec6bbSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 761372ec6bbSRichard Tran Mills 76287c2a1d7SRichard Tran Mills PetscErrorCode MatScale_SeqAIJMKL(Mat inA,PetscScalar alpha) 763db63039fSRichard Tran Mills { 764db63039fSRichard Tran Mills PetscErrorCode ierr; 765db63039fSRichard Tran Mills 76687c2a1d7SRichard Tran Mills PetscFunctionBegin; 767db63039fSRichard Tran Mills ierr = MatScale_SeqAIJ(inA,alpha);CHKERRQ(ierr); 768db63039fSRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(inA);CHKERRQ(ierr); 769db63039fSRichard Tran Mills PetscFunctionReturn(0); 770db63039fSRichard Tran Mills } 771df555b71SRichard Tran Mills 77287c2a1d7SRichard Tran Mills PetscErrorCode MatDiagonalScale_SeqAIJMKL(Mat A,Vec ll,Vec rr) 77387c2a1d7SRichard Tran Mills { 77487c2a1d7SRichard Tran Mills PetscErrorCode ierr; 77587c2a1d7SRichard Tran Mills 77687c2a1d7SRichard Tran Mills PetscFunctionBegin; 77787c2a1d7SRichard Tran Mills ierr = MatDiagonalScale_SeqAIJ(A,ll,rr);CHKERRQ(ierr); 77887c2a1d7SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr); 77987c2a1d7SRichard Tran Mills PetscFunctionReturn(0); 78087c2a1d7SRichard Tran Mills } 78187c2a1d7SRichard Tran Mills 78287c2a1d7SRichard Tran Mills PetscErrorCode MatDiagonalSet_SeqAIJMKL(Mat Y,Vec D,InsertMode is) 78387c2a1d7SRichard Tran Mills { 78487c2a1d7SRichard Tran Mills PetscErrorCode ierr; 78587c2a1d7SRichard Tran Mills 78687c2a1d7SRichard Tran Mills PetscFunctionBegin; 78787c2a1d7SRichard Tran Mills ierr = MatDiagonalSet_SeqAIJ(Y,D,is);CHKERRQ(ierr); 78887c2a1d7SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(Y);CHKERRQ(ierr); 78987c2a1d7SRichard Tran Mills PetscFunctionReturn(0); 79087c2a1d7SRichard Tran Mills } 79187c2a1d7SRichard Tran Mills 79287c2a1d7SRichard Tran Mills PetscErrorCode MatAXPY_SeqAIJMKL(Mat Y,PetscScalar a,Mat X,MatStructure str) 79387c2a1d7SRichard Tran Mills { 79487c2a1d7SRichard Tran Mills PetscErrorCode ierr; 79587c2a1d7SRichard Tran Mills 79687c2a1d7SRichard Tran Mills PetscFunctionBegin; 79787c2a1d7SRichard Tran Mills ierr = MatAXPY_SeqAIJ(Y,a,X,str);CHKERRQ(ierr); 79887c2a1d7SRichard Tran Mills if (str == SAME_NONZERO_PATTERN) { 79987c2a1d7SRichard Tran Mills /* MatAssemblyEnd() is not called if SAME_NONZERO_PATTERN, so we need to force update of the MKL matrix handle. */ 80087c2a1d7SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(Y);CHKERRQ(ierr); 80187c2a1d7SRichard Tran Mills } 80287c2a1d7SRichard Tran Mills PetscFunctionReturn(0); 80387c2a1d7SRichard Tran Mills } 80487c2a1d7SRichard Tran Mills 8054a2a386eSRichard Tran Mills /* MatConvert_SeqAIJ_SeqAIJMKL converts a SeqAIJ matrix into a 8064a2a386eSRichard Tran Mills * SeqAIJMKL matrix. This routine is called by the MatCreate_SeqMKLAIJ() 8074a2a386eSRichard Tran Mills * routine, but can also be used to convert an assembled SeqAIJ matrix 8084a2a386eSRichard Tran Mills * into a SeqAIJMKL one. */ 8094a2a386eSRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJMKL(Mat A,MatType type,MatReuse reuse,Mat *newmat) 8104a2a386eSRichard Tran Mills { 8114a2a386eSRichard Tran Mills PetscErrorCode ierr; 8124a2a386eSRichard Tran Mills Mat B = *newmat; 8134a2a386eSRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 814c9d46305SRichard Tran Mills PetscBool set; 815e9c94282SRichard Tran Mills PetscBool sametype; 8164a2a386eSRichard Tran Mills 8174a2a386eSRichard Tran Mills PetscFunctionBegin; 8184a2a386eSRichard Tran Mills if (reuse == MAT_INITIAL_MATRIX) { 8194a2a386eSRichard Tran Mills ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr); 8204a2a386eSRichard Tran Mills } 8214a2a386eSRichard Tran Mills 822e9c94282SRichard Tran Mills ierr = PetscObjectTypeCompare((PetscObject)A,type,&sametype);CHKERRQ(ierr); 823e9c94282SRichard Tran Mills if (sametype) PetscFunctionReturn(0); 824e9c94282SRichard Tran Mills 8254a2a386eSRichard Tran Mills ierr = PetscNewLog(B,&aijmkl);CHKERRQ(ierr); 8264a2a386eSRichard Tran Mills B->spptr = (void*) aijmkl; 8274a2a386eSRichard Tran Mills 828df555b71SRichard Tran Mills /* Set function pointers for methods that we inherit from AIJ but override. 829969800c5SRichard Tran Mills * We also parse some command line options below, since those determine some of the methods we point to. */ 8304a2a386eSRichard Tran Mills B->ops->duplicate = MatDuplicate_SeqAIJMKL; 8314a2a386eSRichard Tran Mills B->ops->assemblyend = MatAssemblyEnd_SeqAIJMKL; 8324a2a386eSRichard Tran Mills B->ops->destroy = MatDestroy_SeqAIJMKL; 833c9d46305SRichard Tran Mills 8344abfa3b3SRichard Tran Mills aijmkl->sparse_optimized = PETSC_FALSE; 835d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 836d995685eSRichard Tran Mills aijmkl->no_SpMV2 = PETSC_FALSE; /* Default to using the SpMV2 routines if our MKL supports them. */ 837a8327b06SKarl Rupp #else 838d995685eSRichard Tran Mills aijmkl->no_SpMV2 = PETSC_TRUE; 839d995685eSRichard Tran Mills #endif 8405b49642aSRichard Tran Mills aijmkl->eager_inspection = PETSC_FALSE; 8414abfa3b3SRichard Tran Mills 8424abfa3b3SRichard Tran Mills /* Parse command line options. */ 843c9d46305SRichard Tran Mills ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"AIJMKL Options","Mat");CHKERRQ(ierr); 844c9d46305SRichard Tran Mills ierr = PetscOptionsBool("-mat_aijmkl_no_spmv2","NoSPMV2","None",(PetscBool)aijmkl->no_SpMV2,(PetscBool*)&aijmkl->no_SpMV2,&set);CHKERRQ(ierr); 8455b49642aSRichard Tran Mills ierr = PetscOptionsBool("-mat_aijmkl_eager_inspection","Eager Inspection","None",(PetscBool)aijmkl->eager_inspection,(PetscBool*)&aijmkl->eager_inspection,&set);CHKERRQ(ierr); 846c9d46305SRichard Tran Mills ierr = PetscOptionsEnd();CHKERRQ(ierr); 847d995685eSRichard Tran Mills #ifndef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 848d995685eSRichard Tran Mills if(!aijmkl->no_SpMV2) { 849d995685eSRichard 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"); 850d995685eSRichard Tran Mills aijmkl->no_SpMV2 = PETSC_TRUE; 851d995685eSRichard Tran Mills } 852d995685eSRichard Tran Mills #endif 853c9d46305SRichard Tran Mills 854c9d46305SRichard Tran Mills if(!aijmkl->no_SpMV2) { 855d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 856df555b71SRichard Tran Mills B->ops->mult = MatMult_SeqAIJMKL_SpMV2; 857969800c5SRichard Tran Mills B->ops->multtranspose = MatMultTranspose_SeqAIJMKL_SpMV2; 858df555b71SRichard Tran Mills B->ops->multadd = MatMultAdd_SeqAIJMKL_SpMV2; 859969800c5SRichard Tran Mills B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL_SpMV2; 86045fbe478SRichard Tran Mills B->ops->matmult = MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2; 861a557fde5SRichard Tran Mills B->ops->transposematmult = MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2; 862d995685eSRichard Tran Mills #endif 863c9d46305SRichard Tran Mills } else { 8644a2a386eSRichard Tran Mills B->ops->mult = MatMult_SeqAIJMKL; 865969800c5SRichard Tran Mills B->ops->multtranspose = MatMultTranspose_SeqAIJMKL; 8664a2a386eSRichard Tran Mills B->ops->multadd = MatMultAdd_SeqAIJMKL; 867969800c5SRichard Tran Mills B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL; 868c9d46305SRichard Tran Mills } 8694a2a386eSRichard Tran Mills 870db63039fSRichard Tran Mills B->ops->scale = MatScale_SeqAIJMKL; 87187c2a1d7SRichard Tran Mills B->ops->diagonalscale = MatDiagonalScale_SeqAIJMKL; 87287c2a1d7SRichard Tran Mills B->ops->diagonalset = MatDiagonalSet_SeqAIJMKL; 87387c2a1d7SRichard Tran Mills B->ops->axpy = MatAXPY_SeqAIJMKL; 874db63039fSRichard Tran Mills 875db63039fSRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatScale_SeqAIJMKL_C",MatScale_SeqAIJMKL);CHKERRQ(ierr); 8764a2a386eSRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",MatConvert_SeqAIJMKL_SeqAIJ);CHKERRQ(ierr); 877e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaijmkl_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr); 878e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaijmkl_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr); 879e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaijmkl_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr); 88045fbe478SRichard Tran Mills if(!aijmkl->no_SpMV2) { 88145fbe478SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 88245fbe478SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqaijmkl_seqaijmkl_C",MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr); 883372ec6bbSRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatTransposeMatMult_seqaijmkl_seqaijmkl_C",MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr); 88445fbe478SRichard Tran Mills #endif 88545fbe478SRichard Tran Mills } 8864a2a386eSRichard Tran Mills 8874a2a386eSRichard Tran Mills ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJMKL);CHKERRQ(ierr); 8884a2a386eSRichard Tran Mills *newmat = B; 8894a2a386eSRichard Tran Mills PetscFunctionReturn(0); 8904a2a386eSRichard Tran Mills } 8914a2a386eSRichard Tran Mills 8924a2a386eSRichard Tran Mills /*@C 8934a2a386eSRichard Tran Mills MatCreateSeqAIJMKL - Creates a sparse matrix of type SEQAIJMKL. 8944a2a386eSRichard Tran Mills This type inherits from AIJ and is largely identical, but uses sparse BLAS 8954a2a386eSRichard Tran Mills routines from Intel MKL whenever possible. 8963af10221SRichard Tran Mills MatMult, MatMultAdd, MatMultTranspose, MatMultTransposeAdd, MatMatMult, and MatTransposeMatMult 89790147e49SRichard Tran Mills operations are currently supported. 89890147e49SRichard Tran Mills If the installed version of MKL supports the "SpMV2" sparse 89990147e49SRichard Tran Mills inspector-executor routines, then those are used by default. 90090147e49SRichard Tran Mills 9014a2a386eSRichard Tran Mills Collective on MPI_Comm 9024a2a386eSRichard Tran Mills 9034a2a386eSRichard Tran Mills Input Parameters: 9044a2a386eSRichard Tran Mills + comm - MPI communicator, set to PETSC_COMM_SELF 9054a2a386eSRichard Tran Mills . m - number of rows 9064a2a386eSRichard Tran Mills . n - number of columns 9074a2a386eSRichard Tran Mills . nz - number of nonzeros per row (same for all rows) 9084a2a386eSRichard Tran Mills - nnz - array containing the number of nonzeros in the various rows 9094a2a386eSRichard Tran Mills (possibly different for each row) or NULL 9104a2a386eSRichard Tran Mills 9114a2a386eSRichard Tran Mills Output Parameter: 9124a2a386eSRichard Tran Mills . A - the matrix 9134a2a386eSRichard Tran Mills 91490147e49SRichard Tran Mills Options Database Keys: 91566b7eeb6SRichard Tran Mills + -mat_aijmkl_no_spmv2 - disable use of the SpMV2 inspector-executor routines 91666b7eeb6SRichard Tran Mills - -mat_aijmkl_eager_inspection - perform MKL "inspection" phase upon matrix assembly; default is to do "lazy" inspection, performing this step the first time the matrix is applied 91790147e49SRichard Tran Mills 9184a2a386eSRichard Tran Mills Notes: 9194a2a386eSRichard Tran Mills If nnz is given then nz is ignored 9204a2a386eSRichard Tran Mills 9214a2a386eSRichard Tran Mills Level: intermediate 9224a2a386eSRichard Tran Mills 92390147e49SRichard Tran Mills .keywords: matrix, MKL, sparse, parallel 9244a2a386eSRichard Tran Mills 9254a2a386eSRichard Tran Mills .seealso: MatCreate(), MatCreateMPIAIJMKL(), MatSetValues() 9264a2a386eSRichard Tran Mills @*/ 9274a2a386eSRichard Tran Mills PetscErrorCode MatCreateSeqAIJMKL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 9284a2a386eSRichard Tran Mills { 9294a2a386eSRichard Tran Mills PetscErrorCode ierr; 9304a2a386eSRichard Tran Mills 9314a2a386eSRichard Tran Mills PetscFunctionBegin; 9324a2a386eSRichard Tran Mills ierr = MatCreate(comm,A);CHKERRQ(ierr); 9334a2a386eSRichard Tran Mills ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 9344a2a386eSRichard Tran Mills ierr = MatSetType(*A,MATSEQAIJMKL);CHKERRQ(ierr); 9354a2a386eSRichard Tran Mills ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr); 9364a2a386eSRichard Tran Mills PetscFunctionReturn(0); 9374a2a386eSRichard Tran Mills } 9384a2a386eSRichard Tran Mills 9394a2a386eSRichard Tran Mills PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJMKL(Mat A) 9404a2a386eSRichard Tran Mills { 9414a2a386eSRichard Tran Mills PetscErrorCode ierr; 9424a2a386eSRichard Tran Mills 9434a2a386eSRichard Tran Mills PetscFunctionBegin; 9444a2a386eSRichard Tran Mills ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr); 9454a2a386eSRichard Tran Mills ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr); 9464a2a386eSRichard Tran Mills PetscFunctionReturn(0); 9474a2a386eSRichard Tran Mills } 948