14a2a386eSRichard Tran Mills /* 24a2a386eSRichard Tran Mills Defines basic operations for the MATSEQAIJMKL matrix class. 34a2a386eSRichard Tran Mills This class is derived from the MATSEQAIJ class and retains the 44a2a386eSRichard Tran Mills compressed row storage (aka Yale sparse matrix format) but uses 54a2a386eSRichard Tran Mills sparse BLAS operations from the Intel Math Kernel Library (MKL) 64a2a386eSRichard Tran Mills wherever possible. 74a2a386eSRichard Tran Mills */ 84a2a386eSRichard Tran Mills 94a2a386eSRichard Tran Mills #include <../src/mat/impls/aij/seq/aij.h> 104a2a386eSRichard Tran Mills #include <../src/mat/impls/aij/seq/aijmkl/aijmkl.h> 114a2a386eSRichard Tran Mills 124a2a386eSRichard Tran Mills /* MKL include files. */ 134a2a386eSRichard Tran Mills #include <mkl_spblas.h> /* Sparse BLAS */ 144a2a386eSRichard Tran Mills 154a2a386eSRichard Tran Mills typedef struct { 16c9d46305SRichard Tran Mills PetscBool no_SpMV2; /* If PETSC_TRUE, then don't use the MKL SpMV2 inspector-executor routines. */ 175b49642aSRichard Tran Mills PetscBool eager_inspection; /* If PETSC_TRUE, then call mkl_sparse_optimize() in MatDuplicate()/MatAssemblyEnd(). */ 184abfa3b3SRichard Tran Mills PetscBool sparse_optimized; /* If PETSC_TRUE, then mkl_sparse_optimize() has been called. */ 19551aa5c8SRichard Tran Mills PetscObjectState state; 20b8cbc1fbSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 21df555b71SRichard Tran Mills sparse_matrix_t csrA; /* "Handle" used by SpMV2 inspector-executor routines. */ 22df555b71SRichard Tran Mills struct matrix_descr descr; 23b8cbc1fbSRichard Tran Mills #endif 244a2a386eSRichard Tran Mills } Mat_SeqAIJMKL; 254a2a386eSRichard Tran Mills 264a2a386eSRichard Tran Mills extern PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat,MatAssemblyType); 274a2a386eSRichard Tran Mills 284a2a386eSRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJMKL_SeqAIJ(Mat A,MatType type,MatReuse reuse,Mat *newmat) 294a2a386eSRichard Tran Mills { 304a2a386eSRichard Tran Mills /* This routine is only called to convert a MATAIJMKL to its base PETSc type, */ 314a2a386eSRichard Tran Mills /* so we will ignore 'MatType type'. */ 324a2a386eSRichard Tran Mills PetscErrorCode ierr; 334a2a386eSRichard Tran Mills Mat B = *newmat; 34c1d5218aSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 354a2a386eSRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 36c1d5218aSRichard Tran Mills #endif 374a2a386eSRichard Tran Mills 384a2a386eSRichard Tran Mills PetscFunctionBegin; 394a2a386eSRichard Tran Mills if (reuse == MAT_INITIAL_MATRIX) { 404a2a386eSRichard Tran Mills ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr); 414a2a386eSRichard Tran Mills } 424a2a386eSRichard Tran Mills 434a2a386eSRichard Tran Mills /* Reset the original function pointers. */ 4454871a98SRichard Tran Mills B->ops->duplicate = MatDuplicate_SeqAIJ; 454a2a386eSRichard Tran Mills B->ops->assemblyend = MatAssemblyEnd_SeqAIJ; 464a2a386eSRichard Tran Mills B->ops->destroy = MatDestroy_SeqAIJ; 4754871a98SRichard Tran Mills B->ops->mult = MatMult_SeqAIJ; 48ff03dc53SRichard Tran Mills B->ops->multtranspose = MatMultTranspose_SeqAIJ; 4954871a98SRichard Tran Mills B->ops->multadd = MatMultAdd_SeqAIJ; 50ff03dc53SRichard Tran Mills B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJ; 5145fbe478SRichard Tran Mills B->ops->matmult = MatMatMult_SeqAIJ_SeqAIJ; 52e8be1fc7SRichard Tran Mills B->ops->matmultnumeric = MatMatMultNumeric_SeqAIJ_SeqAIJ; 53372ec6bbSRichard Tran Mills B->ops->transposematmult = MatTransposeMatMult_SeqAIJ_SeqAIJ; 5487c2a1d7SRichard Tran Mills B->ops->scale = MatScale_SeqAIJ; 5587c2a1d7SRichard Tran Mills B->ops->diagonalscale = MatDiagonalScale_SeqAIJ; 5687c2a1d7SRichard Tran Mills B->ops->diagonalset = MatDiagonalSet_SeqAIJ; 5787c2a1d7SRichard Tran Mills B->ops->axpy = MatAXPY_SeqAIJ; 584a2a386eSRichard Tran Mills 59e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",NULL);CHKERRQ(ierr); 60e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr); 61e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr); 62e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr); 6345fbe478SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 644a940b00SSatish Balay if(!aijmkl->no_SpMV2) { 6545fbe478SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqaijmkl_seqaijmkl_C",NULL);CHKERRQ(ierr); 66e8be1fc7SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_SP2M 67e8be1fc7SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqaijmkl_seqaijmkl_C",NULL);CHKERRQ(ierr); 68e8be1fc7SRichard Tran Mills #endif 69372ec6bbSRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatTransposeMatMult_seqaijmkl_seqaijmkl_C",NULL);CHKERRQ(ierr); 7045fbe478SRichard Tran Mills } 71e9c94282SRichard Tran Mills 724abfa3b3SRichard Tran Mills /* Free everything in the Mat_SeqAIJMKL data structure. Currently, this 73e9c94282SRichard Tran Mills * simply involves destroying the MKL sparse matrix handle and then freeing 74e9c94282SRichard Tran Mills * the spptr pointer. */ 75a8327b06SKarl Rupp if (reuse == MAT_INITIAL_MATRIX) aijmkl = (Mat_SeqAIJMKL*)B->spptr; 76a8327b06SKarl Rupp 774abfa3b3SRichard Tran Mills if (aijmkl->sparse_optimized) { 780632b357SRichard Tran Mills sparse_status_t stat; 794abfa3b3SRichard Tran Mills stat = mkl_sparse_destroy(aijmkl->csrA); 809c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set hints/complete mkl_sparse_optimize"); 814abfa3b3SRichard Tran Mills } 824abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 83e9c94282SRichard Tran Mills ierr = PetscFree(B->spptr);CHKERRQ(ierr); 844a2a386eSRichard Tran Mills 854a2a386eSRichard Tran Mills /* Change the type of B to MATSEQAIJ. */ 864a2a386eSRichard Tran Mills ierr = PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ);CHKERRQ(ierr); 874a2a386eSRichard Tran Mills 884a2a386eSRichard Tran Mills *newmat = B; 894a2a386eSRichard Tran Mills PetscFunctionReturn(0); 904a2a386eSRichard Tran Mills } 914a2a386eSRichard Tran Mills 924a2a386eSRichard Tran Mills PetscErrorCode MatDestroy_SeqAIJMKL(Mat A) 934a2a386eSRichard Tran Mills { 944a2a386eSRichard Tran Mills PetscErrorCode ierr; 954a2a386eSRichard Tran Mills Mat_SeqAIJMKL *aijmkl = (Mat_SeqAIJMKL*) A->spptr; 964a2a386eSRichard Tran Mills 974a2a386eSRichard Tran Mills PetscFunctionBegin; 98e9c94282SRichard Tran Mills 99e9c94282SRichard Tran Mills /* If MatHeaderMerge() was used, then this SeqAIJMKL matrix will not have an 100e9c94282SRichard Tran Mills * spptr pointer. */ 101e9c94282SRichard Tran Mills if (aijmkl) { 1024a2a386eSRichard Tran Mills /* Clean up everything in the Mat_SeqAIJMKL data structure, then free A->spptr. */ 1034abfa3b3SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 1044abfa3b3SRichard Tran Mills if (aijmkl->sparse_optimized) { 1054abfa3b3SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 1064abfa3b3SRichard Tran Mills stat = mkl_sparse_destroy(aijmkl->csrA); 1079c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy"); 1084abfa3b3SRichard Tran Mills } 1094abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 1104a2a386eSRichard Tran Mills ierr = PetscFree(A->spptr);CHKERRQ(ierr); 111e9c94282SRichard Tran Mills } 1124a2a386eSRichard Tran Mills 1134a2a386eSRichard Tran Mills /* Change the type of A back to SEQAIJ and use MatDestroy_SeqAIJ() 1144a2a386eSRichard Tran Mills * to destroy everything that remains. */ 1154a2a386eSRichard Tran Mills ierr = PetscObjectChangeTypeName((PetscObject)A, MATSEQAIJ);CHKERRQ(ierr); 1164a2a386eSRichard Tran Mills /* Note that I don't call MatSetType(). I believe this is because that 1174a2a386eSRichard Tran Mills * is only to be called when *building* a matrix. I could be wrong, but 1184a2a386eSRichard Tran Mills * that is how things work for the SuperLU matrix class. */ 1194a2a386eSRichard Tran Mills ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr); 1204a2a386eSRichard Tran Mills PetscFunctionReturn(0); 1214a2a386eSRichard Tran Mills } 1224a2a386eSRichard Tran Mills 1235b49642aSRichard Tran Mills /* MatSeqAIJKL_create_mkl_handle(), if called with an AIJMKL matrix that has not had mkl_sparse_optimize() called for it, 1245b49642aSRichard Tran Mills * creates an MKL sparse matrix handle from the AIJ arrays and calls mkl_sparse_optimize(). 1255b49642aSRichard Tran Mills * If called with an AIJMKL matrix for which aijmkl->sparse_optimized == PETSC_TRUE, then it destroys the old matrix 1265b49642aSRichard Tran Mills * handle, creates a new one, and then calls mkl_sparse_optimize(). 1275b49642aSRichard Tran Mills * Although in normal MKL usage it is possible to have a valid matrix handle on which mkl_sparse_optimize() has not been 1285b49642aSRichard Tran Mills * called, for AIJMKL the handle creation and optimization step always occur together, so we don't handle the case of 1295b49642aSRichard Tran Mills * an unoptimized matrix handle here. */ 1306e369cd5SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_mkl_handle(Mat A) 1314a2a386eSRichard Tran Mills { 1326e369cd5SRichard Tran Mills #ifndef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 1336e369cd5SRichard Tran Mills /* If the MKL library does not have mkl_sparse_optimize(), then this routine 1346e369cd5SRichard Tran Mills * does nothing. We make it callable anyway in this case because it cuts 1356e369cd5SRichard Tran Mills * down on littering the code with #ifdefs. */ 13645fbe478SRichard Tran Mills PetscFunctionBegin; 1376e369cd5SRichard Tran Mills PetscFunctionReturn(0); 1386e369cd5SRichard Tran Mills #else 139a8327b06SKarl Rupp Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 140a8327b06SKarl Rupp Mat_SeqAIJMKL *aijmkl = (Mat_SeqAIJMKL*)A->spptr; 141a8327b06SKarl Rupp PetscInt m,n; 142a8327b06SKarl Rupp MatScalar *aa; 143a8327b06SKarl Rupp PetscInt *aj,*ai; 1446e369cd5SRichard Tran Mills sparse_status_t stat; 145551aa5c8SRichard Tran Mills PetscErrorCode ierr; 146551aa5c8SRichard Tran Mills PetscObjectState state; 1474a2a386eSRichard Tran Mills 148a8327b06SKarl Rupp PetscFunctionBegin; 1496e369cd5SRichard Tran Mills if (aijmkl->no_SpMV2) PetscFunctionReturn(0); 1506e369cd5SRichard Tran Mills 1510632b357SRichard Tran Mills if (aijmkl->sparse_optimized) { 1520632b357SRichard Tran Mills /* Matrix has been previously assembled and optimized. Must destroy old 1530632b357SRichard Tran Mills * matrix handle before running the optimization step again. */ 1540632b357SRichard Tran Mills stat = mkl_sparse_destroy(aijmkl->csrA); 1559c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy"); 1560632b357SRichard Tran Mills } 1578d3fe1b0SRichard Tran Mills aijmkl->sparse_optimized = PETSC_FALSE; 1586e369cd5SRichard Tran Mills 159c9d46305SRichard Tran Mills /* Now perform the SpMV2 setup and matrix optimization. */ 160df555b71SRichard Tran Mills aijmkl->descr.type = SPARSE_MATRIX_TYPE_GENERAL; 161df555b71SRichard Tran Mills aijmkl->descr.mode = SPARSE_FILL_MODE_LOWER; 162df555b71SRichard Tran Mills aijmkl->descr.diag = SPARSE_DIAG_NON_UNIT; 16358678438SRichard Tran Mills m = A->rmap->n; 16458678438SRichard Tran Mills n = A->cmap->n; 165df555b71SRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 166df555b71SRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 167df555b71SRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 16880095d54SIrina Sokolova if ((a->nz!=0) & !(A->structure_only)) { 1698d3fe1b0SRichard Tran Mills /* Create a new, optimized sparse matrix handle only if the matrix has nonzero entries. 1708d3fe1b0SRichard Tran Mills * The MKL sparse-inspector executor routines don't like being passed an empty matrix. */ 17158678438SRichard Tran Mills stat = mkl_sparse_x_create_csr(&aijmkl->csrA,SPARSE_INDEX_BASE_ZERO,m,n,ai,ai+1,aj,aa); 172e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to create matrix handle"); 173df555b71SRichard Tran Mills stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000); 174e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set mv_hint"); 175df555b71SRichard Tran Mills stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE); 176e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set memory_hint"); 177df555b71SRichard Tran Mills stat = mkl_sparse_optimize(aijmkl->csrA); 178e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_optimize"); 1794abfa3b3SRichard Tran Mills aijmkl->sparse_optimized = PETSC_TRUE; 180c9d46305SRichard Tran Mills } 181551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 1826e369cd5SRichard Tran Mills 1836e369cd5SRichard Tran Mills PetscFunctionReturn(0); 184d995685eSRichard Tran Mills #endif 1856e369cd5SRichard Tran Mills } 1866e369cd5SRichard Tran Mills 18719afcda9SRichard Tran Mills /* MatSeqAIJMKL_create_from_mkl_handle() creates a sequential AIJMKL matrix from an MKL sparse matrix handle. 18819afcda9SRichard Tran Mills * We need this to implement MatMatMult() using the MKL inspector-executor routines, which return an (unoptimized) 1896c87cf42SRichard Tran Mills * matrix handle. 190aab60f1bSRichard Tran Mills * Note: This routine simply destroys and replaces the original matrix if MAT_REUSE_MATRIX has been specified, as 191aab60f1bSRichard Tran Mills * there is no good alternative. */ 19219afcda9SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 1936c87cf42SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_from_mkl_handle(MPI_Comm comm,sparse_matrix_t csrA,MatReuse reuse,Mat *mat) 19419afcda9SRichard Tran Mills { 19519afcda9SRichard Tran Mills PetscErrorCode ierr; 19619afcda9SRichard Tran Mills sparse_status_t stat; 19719afcda9SRichard Tran Mills sparse_index_base_t indexing; 19819afcda9SRichard Tran Mills PetscInt nrows, ncols; 19945fbe478SRichard Tran Mills PetscInt *aj,*ai,*dummy; 20019afcda9SRichard Tran Mills MatScalar *aa; 20119afcda9SRichard Tran Mills Mat A; 20219afcda9SRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 20319afcda9SRichard Tran Mills 20445fbe478SRichard Tran Mills /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */ 20545fbe478SRichard Tran Mills stat = mkl_sparse_x_export_csr(csrA,&indexing,&nrows,&ncols,&ai,&dummy,&aj,&aa); 2069c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_x_export_csr()"); 2076c87cf42SRichard Tran Mills 208aab60f1bSRichard Tran Mills if (reuse == MAT_REUSE_MATRIX) { 209aab60f1bSRichard Tran Mills ierr = MatDestroy(mat);CHKERRQ(ierr); 210aab60f1bSRichard Tran Mills } 21119afcda9SRichard Tran Mills ierr = MatCreate(comm,&A);CHKERRQ(ierr); 21219afcda9SRichard Tran Mills ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr); 21345fbe478SRichard Tran Mills ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,nrows,ncols);CHKERRQ(ierr); 214aab60f1bSRichard Tran Mills /* We use MatSeqAIJSetPreallocationCSR() instead of MatCreateSeqAIJWithArrays() because we must copy the arrays exported 215aab60f1bSRichard Tran Mills * from MKL; MKL developers tell us that modifying the arrays may cause unexpected results when using the MKL handle, and 216aab60f1bSRichard Tran Mills * they will be destroyed when the MKL handle is destroyed. 217aab60f1bSRichard Tran Mills * (In the interest of reducing memory consumption in future, can we figure out good ways to deal with this?) */ 21819afcda9SRichard Tran Mills ierr = MatSeqAIJSetPreallocationCSR(A,ai,aj,aa);CHKERRQ(ierr); 21919afcda9SRichard Tran Mills 22019afcda9SRichard Tran Mills /* We now have an assembled sequential AIJ matrix created from copies of the exported arrays from the MKL matrix handle. 22119afcda9SRichard Tran Mills * Now turn it into a MATSEQAIJMKL. */ 22219afcda9SRichard Tran Mills ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr); 2236c87cf42SRichard Tran Mills 22419afcda9SRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 22519afcda9SRichard Tran Mills aijmkl->csrA = csrA; 2266c87cf42SRichard Tran Mills 22719afcda9SRichard Tran Mills /* The below code duplicates much of what is in MatSeqAIJKL_create_mkl_handle(). I dislike this code duplication, but 22819afcda9SRichard Tran Mills * MatSeqAIJMKL_create_mkl_handle() cannot be used because we don't need to create a handle -- we've already got one, 22919afcda9SRichard Tran Mills * and just need to be able to run the MKL optimization step. */ 230f3fd1758SRichard Tran Mills aijmkl->descr.type = SPARSE_MATRIX_TYPE_GENERAL; 231f3fd1758SRichard Tran Mills aijmkl->descr.mode = SPARSE_FILL_MODE_LOWER; 232f3fd1758SRichard Tran Mills aijmkl->descr.diag = SPARSE_DIAG_NON_UNIT; 23319afcda9SRichard Tran Mills stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000); 23419afcda9SRichard Tran Mills stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE); 23519afcda9SRichard Tran Mills stat = mkl_sparse_optimize(aijmkl->csrA); 2369c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set hints/complete mkl_sparse_optimize"); 23719afcda9SRichard Tran Mills aijmkl->sparse_optimized = PETSC_TRUE; 23819afcda9SRichard Tran Mills 23919afcda9SRichard Tran Mills *mat = A; 24019afcda9SRichard Tran Mills PetscFunctionReturn(0); 24119afcda9SRichard Tran Mills } 24219afcda9SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 24319afcda9SRichard Tran Mills 244e8be1fc7SRichard Tran Mills /* MatSeqAIJMKL_update_from_mkl_handle() updates the matrix values array from the contents of the associated MKL sparse matrix handle. 245e8be1fc7SRichard Tran Mills * This is needed after mkl_sparse_sp2m() with SPARSE_STAGE_FINALIZE_MULT has been used to compute new values of the matrix in 246e8be1fc7SRichard Tran Mills * MatMatMultNumeric(). */ 247e8be1fc7SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 248e8be1fc7SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_update_from_mkl_handle(Mat A) 249e8be1fc7SRichard Tran Mills { 250e8be1fc7SRichard Tran Mills PetscInt i; 251e8be1fc7SRichard Tran Mills PetscInt nrows,ncols; 252e8be1fc7SRichard Tran Mills PetscInt nz; 253e8be1fc7SRichard Tran Mills PetscInt *ai,*aj,*dummy; 254e8be1fc7SRichard Tran Mills PetscScalar *aa; 255e8be1fc7SRichard Tran Mills PetscErrorCode ierr; 256e8be1fc7SRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 257e8be1fc7SRichard Tran Mills sparse_status_t stat; 258e8be1fc7SRichard Tran Mills sparse_index_base_t indexing; 259e8be1fc7SRichard Tran Mills 260e8be1fc7SRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 261e8be1fc7SRichard Tran Mills 262e8be1fc7SRichard Tran Mills /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */ 263e8be1fc7SRichard Tran Mills stat = mkl_sparse_x_export_csr(aijmkl->csrA,&indexing,&nrows,&ncols,&ai,&dummy,&aj,&aa); 264e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_x_export_csr()"); 265e8be1fc7SRichard Tran Mills 266e8be1fc7SRichard Tran Mills /* We can't just do a copy from the arrays exported by MKL to those used for the PETSc AIJ storage, because the MKL and PETSc 267e8be1fc7SRichard Tran Mills * representations differ in small ways (e.g., more explicit nonzeros per row due to preallocation). */ 268e8be1fc7SRichard Tran Mills for (i=0; i<nrows; i++) { 269e8be1fc7SRichard Tran Mills nz = ai[i+1] - ai[i]; 270e8be1fc7SRichard Tran Mills ierr = MatSetValues_SeqAIJ(A, 1, &i, nz, aj+ai[i], aa+ai[i], INSERT_VALUES);CHKERRQ(ierr); 271e8be1fc7SRichard Tran Mills } 272e8be1fc7SRichard Tran Mills 273e8be1fc7SRichard Tran Mills ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 274e8be1fc7SRichard Tran Mills ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 275e8be1fc7SRichard Tran Mills 276e8be1fc7SRichard Tran Mills PetscFunctionReturn(0); 277e8be1fc7SRichard Tran Mills } 278e8be1fc7SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 279e8be1fc7SRichard Tran Mills 2806e369cd5SRichard Tran Mills PetscErrorCode MatDuplicate_SeqAIJMKL(Mat A, MatDuplicateOption op, Mat *M) 2816e369cd5SRichard Tran Mills { 2826e369cd5SRichard Tran Mills PetscErrorCode ierr; 2836e369cd5SRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 2846e369cd5SRichard Tran Mills Mat_SeqAIJMKL *aijmkl_dest; 2856e369cd5SRichard Tran Mills 2866e369cd5SRichard Tran Mills PetscFunctionBegin; 2876e369cd5SRichard Tran Mills ierr = MatDuplicate_SeqAIJ(A,op,M);CHKERRQ(ierr); 2886e369cd5SRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 2896e369cd5SRichard Tran Mills aijmkl_dest = (Mat_SeqAIJMKL*) (*M)->spptr; 2906e369cd5SRichard Tran Mills ierr = PetscMemcpy(aijmkl_dest,aijmkl,sizeof(Mat_SeqAIJMKL));CHKERRQ(ierr); 2916e369cd5SRichard Tran Mills aijmkl_dest->sparse_optimized = PETSC_FALSE; 2925b49642aSRichard Tran Mills if (aijmkl->eager_inspection) { 2936e369cd5SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr); 2945b49642aSRichard Tran Mills } 2956e369cd5SRichard Tran Mills PetscFunctionReturn(0); 2966e369cd5SRichard Tran Mills } 2976e369cd5SRichard Tran Mills 2986e369cd5SRichard Tran Mills PetscErrorCode MatAssemblyEnd_SeqAIJMKL(Mat A, MatAssemblyType mode) 2996e369cd5SRichard Tran Mills { 3006e369cd5SRichard Tran Mills PetscErrorCode ierr; 3016e369cd5SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3025b49642aSRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 3036e369cd5SRichard Tran Mills 3046e369cd5SRichard Tran Mills PetscFunctionBegin; 3056e369cd5SRichard Tran Mills if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 3066e369cd5SRichard Tran Mills 3076e369cd5SRichard Tran Mills /* Since a MATSEQAIJMKL matrix is really just a MATSEQAIJ with some 3086e369cd5SRichard Tran Mills * extra information and some different methods, call the AssemblyEnd 3096e369cd5SRichard Tran Mills * routine for a MATSEQAIJ. 3106e369cd5SRichard Tran Mills * I'm not sure if this is the best way to do this, but it avoids 311d96e85feSRichard Tran Mills * a lot of code duplication. */ 3126e369cd5SRichard Tran Mills a->inode.use = PETSC_FALSE; /* Must disable: otherwise the MKL routines won't get used. */ 3136e369cd5SRichard Tran Mills ierr = MatAssemblyEnd_SeqAIJ(A, mode);CHKERRQ(ierr); 3146e369cd5SRichard Tran Mills 3155b49642aSRichard Tran Mills /* If the user has requested "eager" inspection, create the optimized MKL sparse handle (if needed; the function checks). 3165b49642aSRichard Tran Mills * (The default is to do "lazy" inspection, deferring this until something like MatMult() is called.) */ 3175b49642aSRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 3185b49642aSRichard Tran Mills if (aijmkl->eager_inspection) { 3196e369cd5SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr); 3204a940b00SSatish Balay #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 321886913bfSRichard Tran Mills } else if (aijmkl->sparse_optimized) { 322886913bfSRichard Tran Mills /* If doing lazy inspection and there is an optimized MKL handle, we need to destroy it, so that it will be 323886913bfSRichard Tran Mills * rebuilt later when needed. Otherwise, some SeqAIJ implementations that we depend on for some operations 324886913bfSRichard Tran Mills * (such as MatMatMultNumeric()) can modify the result matrix without the matrix handle being rebuilt. 3257225e97aSRichard Tran Mills * (The SeqAIJ version MatMatMultNumeric() knows nothing about matrix handles, but it *does* call MatAssemblyEnd().) */ 326886913bfSRichard Tran Mills sparse_status_t stat = mkl_sparse_destroy(aijmkl->csrA); 3279c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy"); 328886913bfSRichard Tran Mills aijmkl->sparse_optimized = PETSC_FALSE; 3294a940b00SSatish Balay #endif 3305b49642aSRichard Tran Mills } 331df555b71SRichard Tran Mills 3324a2a386eSRichard Tran Mills PetscFunctionReturn(0); 3334a2a386eSRichard Tran Mills } 3344a2a386eSRichard Tran Mills 3354a2a386eSRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL(Mat A,Vec xx,Vec yy) 3364a2a386eSRichard Tran Mills { 3374a2a386eSRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3384a2a386eSRichard Tran Mills const PetscScalar *x; 3394a2a386eSRichard Tran Mills PetscScalar *y; 3404a2a386eSRichard Tran Mills const MatScalar *aa; 3414a2a386eSRichard Tran Mills PetscErrorCode ierr; 3424a2a386eSRichard Tran Mills PetscInt m=A->rmap->n; 343db63039fSRichard Tran Mills PetscInt n=A->cmap->n; 344db63039fSRichard Tran Mills PetscScalar alpha = 1.0; 345db63039fSRichard Tran Mills PetscScalar beta = 0.0; 3464a2a386eSRichard Tran Mills const PetscInt *aj,*ai; 347db63039fSRichard Tran Mills char matdescra[6]; 348db63039fSRichard Tran Mills 3494a2a386eSRichard Tran Mills 3504a2a386eSRichard Tran Mills /* Variables not in MatMult_SeqAIJ. */ 351ff03dc53SRichard Tran Mills char transa = 'n'; /* Used to indicate to MKL that we are not computing the transpose product. */ 352ff03dc53SRichard Tran Mills 353ff03dc53SRichard Tran Mills PetscFunctionBegin; 354db63039fSRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 355db63039fSRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 356ff03dc53SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 357ff03dc53SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 358ff03dc53SRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 359ff03dc53SRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 360ff03dc53SRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 361ff03dc53SRichard Tran Mills 362ff03dc53SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 363db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y); 364ff03dc53SRichard Tran Mills 365ff03dc53SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 366ff03dc53SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 367ff03dc53SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 368ff03dc53SRichard Tran Mills PetscFunctionReturn(0); 369ff03dc53SRichard Tran Mills } 370ff03dc53SRichard Tran Mills 371d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 372df555b71SRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy) 373df555b71SRichard Tran Mills { 374df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 375df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 376df555b71SRichard Tran Mills const PetscScalar *x; 377df555b71SRichard Tran Mills PetscScalar *y; 378df555b71SRichard Tran Mills PetscErrorCode ierr; 379df555b71SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 380551aa5c8SRichard Tran Mills PetscObjectState state; 381df555b71SRichard Tran Mills 382df555b71SRichard Tran Mills PetscFunctionBegin; 383df555b71SRichard Tran Mills 38438987b35SRichard Tran Mills /* If there are no nonzero entries, zero yy and return immediately. */ 38538987b35SRichard Tran Mills if(!a->nz) { 38638987b35SRichard Tran Mills PetscInt i; 38738987b35SRichard Tran Mills PetscInt m=A->rmap->n; 38838987b35SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 38938987b35SRichard Tran Mills for (i=0; i<m; i++) { 39038987b35SRichard Tran Mills y[i] = 0.0; 39138987b35SRichard Tran Mills } 39238987b35SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 39338987b35SRichard Tran Mills PetscFunctionReturn(0); 39438987b35SRichard Tran Mills } 395f36dfe3fSRichard Tran Mills 396df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 397df555b71SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 398df555b71SRichard Tran Mills 3993fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 4003fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 4013fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 402551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 403551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 4043fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 4053fa15762SRichard Tran Mills } 4063fa15762SRichard Tran Mills 407df555b71SRichard Tran Mills /* Call MKL SpMV2 executor routine to do the MatMult. */ 408df555b71SRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y); 4099c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 410df555b71SRichard Tran Mills 411df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 412df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 413df555b71SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 414df555b71SRichard Tran Mills PetscFunctionReturn(0); 415df555b71SRichard Tran Mills } 416d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 417df555b71SRichard Tran Mills 418ff03dc53SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL(Mat A,Vec xx,Vec yy) 419ff03dc53SRichard Tran Mills { 420ff03dc53SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 421ff03dc53SRichard Tran Mills const PetscScalar *x; 422ff03dc53SRichard Tran Mills PetscScalar *y; 423ff03dc53SRichard Tran Mills const MatScalar *aa; 424ff03dc53SRichard Tran Mills PetscErrorCode ierr; 425ff03dc53SRichard Tran Mills PetscInt m=A->rmap->n; 426db63039fSRichard Tran Mills PetscInt n=A->cmap->n; 427db63039fSRichard Tran Mills PetscScalar alpha = 1.0; 428db63039fSRichard Tran Mills PetscScalar beta = 0.0; 429ff03dc53SRichard Tran Mills const PetscInt *aj,*ai; 430db63039fSRichard Tran Mills char matdescra[6]; 431ff03dc53SRichard Tran Mills 432ff03dc53SRichard Tran Mills /* Variables not in MatMultTranspose_SeqAIJ. */ 433ff03dc53SRichard Tran Mills char transa = 't'; /* Used to indicate to MKL that we are computing the transpose product. */ 4344a2a386eSRichard Tran Mills 4354a2a386eSRichard Tran Mills PetscFunctionBegin; 436969800c5SRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 437969800c5SRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 4384a2a386eSRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 4394a2a386eSRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 4404a2a386eSRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 4414a2a386eSRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 4424a2a386eSRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 4434a2a386eSRichard Tran Mills 4444a2a386eSRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 445db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y); 4464a2a386eSRichard Tran Mills 4474a2a386eSRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 4484a2a386eSRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 4494a2a386eSRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 4504a2a386eSRichard Tran Mills PetscFunctionReturn(0); 4514a2a386eSRichard Tran Mills } 4524a2a386eSRichard Tran Mills 453d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 454df555b71SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy) 455df555b71SRichard Tran Mills { 456df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 457df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 458df555b71SRichard Tran Mills const PetscScalar *x; 459df555b71SRichard Tran Mills PetscScalar *y; 460df555b71SRichard Tran Mills PetscErrorCode ierr; 4610632b357SRichard Tran Mills sparse_status_t stat; 462551aa5c8SRichard Tran Mills PetscObjectState state; 463df555b71SRichard Tran Mills 464df555b71SRichard Tran Mills PetscFunctionBegin; 465df555b71SRichard Tran Mills 46638987b35SRichard Tran Mills /* If there are no nonzero entries, zero yy and return immediately. */ 46738987b35SRichard Tran Mills if(!a->nz) { 46838987b35SRichard Tran Mills PetscInt i; 46938987b35SRichard Tran Mills PetscInt n=A->cmap->n; 47038987b35SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 47138987b35SRichard Tran Mills for (i=0; i<n; i++) { 47238987b35SRichard Tran Mills y[i] = 0.0; 47338987b35SRichard Tran Mills } 47438987b35SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 47538987b35SRichard Tran Mills PetscFunctionReturn(0); 47638987b35SRichard Tran Mills } 477f36dfe3fSRichard Tran Mills 478df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 479df555b71SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 480df555b71SRichard Tran Mills 4813fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 4823fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 4833fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 484551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 485551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 4863fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 4873fa15762SRichard Tran Mills } 4883fa15762SRichard Tran Mills 489df555b71SRichard Tran Mills /* Call MKL SpMV2 executor routine to do the MatMultTranspose. */ 490df555b71SRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y); 4919c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 492df555b71SRichard Tran Mills 493df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 494df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 495df555b71SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 496df555b71SRichard Tran Mills PetscFunctionReturn(0); 497df555b71SRichard Tran Mills } 498d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 499df555b71SRichard Tran Mills 5004a2a386eSRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz) 5014a2a386eSRichard Tran Mills { 5024a2a386eSRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 5034a2a386eSRichard Tran Mills const PetscScalar *x; 5044a2a386eSRichard Tran Mills PetscScalar *y,*z; 5054a2a386eSRichard Tran Mills const MatScalar *aa; 5064a2a386eSRichard Tran Mills PetscErrorCode ierr; 5074a2a386eSRichard Tran Mills PetscInt m=A->rmap->n; 508db63039fSRichard Tran Mills PetscInt n=A->cmap->n; 5094a2a386eSRichard Tran Mills const PetscInt *aj,*ai; 5104a2a386eSRichard Tran Mills PetscInt i; 5114a2a386eSRichard Tran Mills 512ff03dc53SRichard Tran Mills /* Variables not in MatMultAdd_SeqAIJ. */ 513ff03dc53SRichard Tran Mills char transa = 'n'; /* Used to indicate to MKL that we are not computing the transpose product. */ 514a84739b8SRichard Tran Mills PetscScalar alpha = 1.0; 515db63039fSRichard Tran Mills PetscScalar beta; 516a84739b8SRichard Tran Mills char matdescra[6]; 517ff03dc53SRichard Tran Mills 518ff03dc53SRichard Tran Mills PetscFunctionBegin; 519a84739b8SRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 520a84739b8SRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 521a84739b8SRichard Tran Mills 522ff03dc53SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 523ff03dc53SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 524ff03dc53SRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 525ff03dc53SRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 526ff03dc53SRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 527ff03dc53SRichard Tran Mills 528ff03dc53SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 529a84739b8SRichard Tran Mills if (zz == yy) { 530a84739b8SRichard 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. */ 531db63039fSRichard Tran Mills beta = 1.0; 532db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 533a84739b8SRichard Tran Mills } else { 534db63039fSRichard Tran Mills /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z. 535db63039fSRichard Tran Mills * MKL sparse BLAS does not have a MatMultAdd equivalent. */ 536db63039fSRichard Tran Mills beta = 0.0; 537db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 538ff03dc53SRichard Tran Mills for (i=0; i<m; i++) { 539ff03dc53SRichard Tran Mills z[i] += y[i]; 540ff03dc53SRichard Tran Mills } 541a84739b8SRichard Tran Mills } 542ff03dc53SRichard Tran Mills 543ff03dc53SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 544ff03dc53SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 545ff03dc53SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 546ff03dc53SRichard Tran Mills PetscFunctionReturn(0); 547ff03dc53SRichard Tran Mills } 548ff03dc53SRichard Tran Mills 549d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 550df555b71SRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz) 551df555b71SRichard Tran Mills { 552df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 553df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 554df555b71SRichard Tran Mills const PetscScalar *x; 555df555b71SRichard Tran Mills PetscScalar *y,*z; 556df555b71SRichard Tran Mills PetscErrorCode ierr; 557df555b71SRichard Tran Mills PetscInt m=A->rmap->n; 558df555b71SRichard Tran Mills PetscInt i; 559df555b71SRichard Tran Mills 560df555b71SRichard Tran Mills /* Variables not in MatMultAdd_SeqAIJ. */ 561df555b71SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 562551aa5c8SRichard Tran Mills PetscObjectState state; 563df555b71SRichard Tran Mills 564df555b71SRichard Tran Mills PetscFunctionBegin; 565df555b71SRichard Tran Mills 56638987b35SRichard Tran Mills /* If there are no nonzero entries, set zz = yy and return immediately. */ 56738987b35SRichard Tran Mills if(!a->nz) { 56838987b35SRichard Tran Mills PetscInt i; 56938987b35SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 57038987b35SRichard Tran Mills for (i=0; i<m; i++) { 57138987b35SRichard Tran Mills z[i] = y[i]; 57238987b35SRichard Tran Mills } 57338987b35SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 57438987b35SRichard Tran Mills PetscFunctionReturn(0); 57538987b35SRichard Tran Mills } 576df555b71SRichard Tran Mills 577df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 578df555b71SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 579df555b71SRichard Tran Mills 5803fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 5813fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 5823fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 583551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 584551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 5853fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 5863fa15762SRichard Tran Mills } 5873fa15762SRichard Tran Mills 588df555b71SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 589df555b71SRichard Tran Mills if (zz == yy) { 590df555b71SRichard 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, 591df555b71SRichard Tran Mills * with alpha and beta both set to 1.0. */ 592db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z); 5939c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 594df555b71SRichard Tran Mills } else { 595df555b71SRichard 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 596df555b71SRichard Tran Mills * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */ 597db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z); 5989c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 599df555b71SRichard Tran Mills for (i=0; i<m; i++) { 600df555b71SRichard Tran Mills z[i] += y[i]; 601df555b71SRichard Tran Mills } 602df555b71SRichard Tran Mills } 603df555b71SRichard Tran Mills 604df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 605df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 606df555b71SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 607df555b71SRichard Tran Mills PetscFunctionReturn(0); 608df555b71SRichard Tran Mills } 609d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 610df555b71SRichard Tran Mills 611ff03dc53SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz) 612ff03dc53SRichard Tran Mills { 613ff03dc53SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 614ff03dc53SRichard Tran Mills const PetscScalar *x; 615ff03dc53SRichard Tran Mills PetscScalar *y,*z; 616ff03dc53SRichard Tran Mills const MatScalar *aa; 617ff03dc53SRichard Tran Mills PetscErrorCode ierr; 618ff03dc53SRichard Tran Mills PetscInt m=A->rmap->n; 619db63039fSRichard Tran Mills PetscInt n=A->cmap->n; 620ff03dc53SRichard Tran Mills const PetscInt *aj,*ai; 621ff03dc53SRichard Tran Mills PetscInt i; 622ff03dc53SRichard Tran Mills 623ff03dc53SRichard Tran Mills /* Variables not in MatMultTransposeAdd_SeqAIJ. */ 624ff03dc53SRichard Tran Mills char transa = 't'; /* Used to indicate to MKL that we are computing the transpose product. */ 625a84739b8SRichard Tran Mills PetscScalar alpha = 1.0; 626db63039fSRichard Tran Mills PetscScalar beta; 627a84739b8SRichard Tran Mills char matdescra[6]; 6284a2a386eSRichard Tran Mills 6294a2a386eSRichard Tran Mills PetscFunctionBegin; 630a84739b8SRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 631a84739b8SRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 632a84739b8SRichard Tran Mills 6334a2a386eSRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 6344a2a386eSRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 6354a2a386eSRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 6364a2a386eSRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 6374a2a386eSRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 6384a2a386eSRichard Tran Mills 6394a2a386eSRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 640a84739b8SRichard Tran Mills if (zz == yy) { 641a84739b8SRichard 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. */ 642db63039fSRichard Tran Mills beta = 1.0; 643969800c5SRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 644a84739b8SRichard Tran Mills } else { 645db63039fSRichard Tran Mills /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z. 646db63039fSRichard Tran Mills * MKL sparse BLAS does not have a MatMultAdd equivalent. */ 647db63039fSRichard Tran Mills beta = 0.0; 648db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 649969800c5SRichard Tran Mills for (i=0; i<n; i++) { 6504a2a386eSRichard Tran Mills z[i] += y[i]; 6514a2a386eSRichard Tran Mills } 652a84739b8SRichard Tran Mills } 6534a2a386eSRichard Tran Mills 6544a2a386eSRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 6554a2a386eSRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 6564a2a386eSRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 6574a2a386eSRichard Tran Mills PetscFunctionReturn(0); 6584a2a386eSRichard Tran Mills } 6594a2a386eSRichard Tran Mills 660d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 661df555b71SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz) 662df555b71SRichard Tran Mills { 663df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 664df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 665df555b71SRichard Tran Mills const PetscScalar *x; 666df555b71SRichard Tran Mills PetscScalar *y,*z; 667df555b71SRichard Tran Mills PetscErrorCode ierr; 668969800c5SRichard Tran Mills PetscInt n=A->cmap->n; 669df555b71SRichard Tran Mills PetscInt i; 670551aa5c8SRichard Tran Mills PetscObjectState state; 671df555b71SRichard Tran Mills 672df555b71SRichard Tran Mills /* Variables not in MatMultTransposeAdd_SeqAIJ. */ 673df555b71SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 674df555b71SRichard Tran Mills 675df555b71SRichard Tran Mills PetscFunctionBegin; 676df555b71SRichard Tran Mills 67738987b35SRichard Tran Mills /* If there are no nonzero entries, set zz = yy and return immediately. */ 67838987b35SRichard Tran Mills if(!a->nz) { 67938987b35SRichard Tran Mills PetscInt i; 68038987b35SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 68138987b35SRichard Tran Mills for (i=0; i<n; i++) { 68238987b35SRichard Tran Mills z[i] = y[i]; 68338987b35SRichard Tran Mills } 68438987b35SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 68538987b35SRichard Tran Mills PetscFunctionReturn(0); 68638987b35SRichard Tran Mills } 687f36dfe3fSRichard Tran Mills 688df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 689df555b71SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 690df555b71SRichard Tran Mills 6913fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 6923fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 6933fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 694551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 695551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 6963fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 6973fa15762SRichard Tran Mills } 6983fa15762SRichard Tran Mills 699df555b71SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 700df555b71SRichard Tran Mills if (zz == yy) { 701df555b71SRichard 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, 702df555b71SRichard Tran Mills * with alpha and beta both set to 1.0. */ 703db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z); 7049c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 705df555b71SRichard Tran Mills } else { 706df555b71SRichard 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 707df555b71SRichard Tran Mills * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */ 708db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z); 7099c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 710969800c5SRichard Tran Mills for (i=0; i<n; i++) { 711df555b71SRichard Tran Mills z[i] += y[i]; 712df555b71SRichard Tran Mills } 713df555b71SRichard Tran Mills } 714df555b71SRichard Tran Mills 715df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 716df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 717df555b71SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 718df555b71SRichard Tran Mills PetscFunctionReturn(0); 719df555b71SRichard Tran Mills } 720d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 721df555b71SRichard Tran Mills 72245fbe478SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 723aab60f1bSRichard Tran Mills /* Note that this code currently doesn't actually get used when MatMatMult() is called with MAT_REUSE_MATRIX, because 724aab60f1bSRichard Tran Mills * the MatMatMult() interface code calls MatMatMultNumeric() in this case. 725*3ecbffd0SRichard Tran Mills * For releases of MKL prior to version 18, update 2: 726aab60f1bSRichard Tran Mills * MKL has no notion of separately callable symbolic vs. numeric phases of sparse matrix-matrix multiply, so in the 727aab60f1bSRichard Tran Mills * MAT_REUSE_MATRIX case, the SeqAIJ routines end up being used. Even though this means that the (hopefully more 728aab60f1bSRichard Tran Mills * optimized) MKL routines do not get used, this probably is best because the MKL routines would waste time re-computing 729aab60f1bSRichard Tran Mills * the symbolic portion, whereas the native PETSc SeqAIJ routines will avoid this. */ 73045fbe478SRichard Tran Mills PetscErrorCode MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat*C) 73145fbe478SRichard Tran Mills { 73245fbe478SRichard Tran Mills Mat_SeqAIJMKL *a, *b; 73345fbe478SRichard Tran Mills sparse_matrix_t csrA, csrB, csrC; 73445fbe478SRichard Tran Mills PetscErrorCode ierr; 73545fbe478SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 736551aa5c8SRichard Tran Mills PetscObjectState state; 73745fbe478SRichard Tran Mills 73845fbe478SRichard Tran Mills PetscFunctionBegin; 73945fbe478SRichard Tran Mills a = (Mat_SeqAIJMKL*)A->spptr; 74045fbe478SRichard Tran Mills b = (Mat_SeqAIJMKL*)B->spptr; 741551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 742551aa5c8SRichard Tran Mills if (!a->sparse_optimized || a->state != state) { 74345fbe478SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 74445fbe478SRichard Tran Mills } 745551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr); 746551aa5c8SRichard Tran Mills if (!b->sparse_optimized || b->state != state) { 74745fbe478SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(B); 74845fbe478SRichard Tran Mills } 74945fbe478SRichard Tran Mills csrA = a->csrA; 75045fbe478SRichard Tran Mills csrB = b->csrA; 75145fbe478SRichard Tran Mills 75245fbe478SRichard Tran Mills stat = mkl_sparse_spmm(SPARSE_OPERATION_NON_TRANSPOSE,csrA,csrB,&csrC); 7539c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete sparse matrix-matrix multiply"); 75445fbe478SRichard Tran Mills 7556c87cf42SRichard Tran Mills ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr); 75645fbe478SRichard Tran Mills 75745fbe478SRichard Tran Mills PetscFunctionReturn(0); 75845fbe478SRichard Tran Mills } 75945fbe478SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 76045fbe478SRichard Tran Mills 761e8be1fc7SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_SP2M 762e8be1fc7SRichard Tran Mills PetscErrorCode MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,Mat C) 763e8be1fc7SRichard Tran Mills { 764e8be1fc7SRichard Tran Mills Mat_SeqAIJMKL *a, *b, *c; 765e8be1fc7SRichard Tran Mills sparse_matrix_t csrA, csrB, csrC; 766e8be1fc7SRichard Tran Mills PetscErrorCode ierr; 767e8be1fc7SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 768e8be1fc7SRichard Tran Mills struct matrix_descr descr_type_gen; 769e8be1fc7SRichard Tran Mills PetscObjectState state; 770e8be1fc7SRichard Tran Mills 771e8be1fc7SRichard Tran Mills PetscFunctionBegin; 772e8be1fc7SRichard Tran Mills a = (Mat_SeqAIJMKL*)A->spptr; 773e8be1fc7SRichard Tran Mills b = (Mat_SeqAIJMKL*)B->spptr; 774e8be1fc7SRichard Tran Mills c = (Mat_SeqAIJMKL*)C->spptr; 775e8be1fc7SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 776e8be1fc7SRichard Tran Mills if (!a->sparse_optimized || a->state != state) { 777e8be1fc7SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 778e8be1fc7SRichard Tran Mills } 779e8be1fc7SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr); 780e8be1fc7SRichard Tran Mills if (!b->sparse_optimized || b->state != state) { 781e8be1fc7SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(B); 782e8be1fc7SRichard Tran Mills } 783e8be1fc7SRichard Tran Mills csrA = a->csrA; 784e8be1fc7SRichard Tran Mills csrB = b->csrA; 785e8be1fc7SRichard Tran Mills csrC = c->csrA; 786e8be1fc7SRichard Tran Mills descr_type_gen.type = SPARSE_MATRIX_TYPE_GENERAL; 787e8be1fc7SRichard Tran Mills 788e8be1fc7SRichard Tran Mills stat = mkl_sparse_sp2m(SPARSE_OPERATION_NON_TRANSPOSE,descr_type_gen,csrA, 789e8be1fc7SRichard Tran Mills SPARSE_OPERATION_NON_TRANSPOSE,descr_type_gen,csrB, 790e8be1fc7SRichard Tran Mills SPARSE_STAGE_FINALIZE_MULT,&csrC); 791e8be1fc7SRichard Tran Mills 792e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete numerical stage of sparse matrix-matrix multiply"); 793e8be1fc7SRichard Tran Mills 794e8be1fc7SRichard Tran Mills /* Have to update the PETSc AIJ representation for matrix C from contents of MKL handle. */ 795e8be1fc7SRichard Tran Mills ierr = MatSeqAIJMKL_update_from_mkl_handle(A);CHKERRQ(ierr); 796e8be1fc7SRichard Tran Mills 797e8be1fc7SRichard Tran Mills PetscFunctionReturn(0); 798e8be1fc7SRichard Tran Mills } 799e8be1fc7SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_SP2M */ 800e8be1fc7SRichard Tran Mills 801372ec6bbSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 802372ec6bbSRichard Tran Mills PetscErrorCode MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat*C) 803372ec6bbSRichard Tran Mills { 804372ec6bbSRichard Tran Mills Mat_SeqAIJMKL *a, *b; 805372ec6bbSRichard Tran Mills sparse_matrix_t csrA, csrB, csrC; 806372ec6bbSRichard Tran Mills PetscErrorCode ierr; 807372ec6bbSRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 808551aa5c8SRichard Tran Mills PetscObjectState state; 809372ec6bbSRichard Tran Mills 810372ec6bbSRichard Tran Mills PetscFunctionBegin; 811372ec6bbSRichard Tran Mills a = (Mat_SeqAIJMKL*)A->spptr; 812372ec6bbSRichard Tran Mills b = (Mat_SeqAIJMKL*)B->spptr; 813551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 814551aa5c8SRichard Tran Mills if (!a->sparse_optimized || a->state != state) { 815372ec6bbSRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 816372ec6bbSRichard Tran Mills } 817551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr); 818551aa5c8SRichard Tran Mills if (!b->sparse_optimized || b->state != state) { 819372ec6bbSRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(B); 820372ec6bbSRichard Tran Mills } 821372ec6bbSRichard Tran Mills csrA = a->csrA; 822372ec6bbSRichard Tran Mills csrB = b->csrA; 823372ec6bbSRichard Tran Mills 824372ec6bbSRichard Tran Mills stat = mkl_sparse_spmm(SPARSE_OPERATION_TRANSPOSE,csrA,csrB,&csrC); 8259c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete sparse matrix-matrix multiply"); 826372ec6bbSRichard Tran Mills 827372ec6bbSRichard Tran Mills ierr = MatSeqAIJMKL_create_from_mkl_handle(PETSC_COMM_SELF,csrC,scall,C);CHKERRQ(ierr); 828372ec6bbSRichard Tran Mills 829372ec6bbSRichard Tran Mills PetscFunctionReturn(0); 830372ec6bbSRichard Tran Mills } 831372ec6bbSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 832372ec6bbSRichard Tran Mills 83387c2a1d7SRichard Tran Mills PetscErrorCode MatScale_SeqAIJMKL(Mat inA,PetscScalar alpha) 834db63039fSRichard Tran Mills { 835db63039fSRichard Tran Mills PetscErrorCode ierr; 836db63039fSRichard Tran Mills 83787c2a1d7SRichard Tran Mills PetscFunctionBegin; 838db63039fSRichard Tran Mills ierr = MatScale_SeqAIJ(inA,alpha);CHKERRQ(ierr); 839db63039fSRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(inA);CHKERRQ(ierr); 840db63039fSRichard Tran Mills PetscFunctionReturn(0); 841db63039fSRichard Tran Mills } 842df555b71SRichard Tran Mills 84387c2a1d7SRichard Tran Mills PetscErrorCode MatDiagonalScale_SeqAIJMKL(Mat A,Vec ll,Vec rr) 84487c2a1d7SRichard Tran Mills { 84587c2a1d7SRichard Tran Mills PetscErrorCode ierr; 84687c2a1d7SRichard Tran Mills 84787c2a1d7SRichard Tran Mills PetscFunctionBegin; 84887c2a1d7SRichard Tran Mills ierr = MatDiagonalScale_SeqAIJ(A,ll,rr);CHKERRQ(ierr); 84987c2a1d7SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr); 85087c2a1d7SRichard Tran Mills PetscFunctionReturn(0); 85187c2a1d7SRichard Tran Mills } 85287c2a1d7SRichard Tran Mills 85387c2a1d7SRichard Tran Mills PetscErrorCode MatDiagonalSet_SeqAIJMKL(Mat Y,Vec D,InsertMode is) 85487c2a1d7SRichard Tran Mills { 85587c2a1d7SRichard Tran Mills PetscErrorCode ierr; 85687c2a1d7SRichard Tran Mills 85787c2a1d7SRichard Tran Mills PetscFunctionBegin; 85887c2a1d7SRichard Tran Mills ierr = MatDiagonalSet_SeqAIJ(Y,D,is);CHKERRQ(ierr); 85987c2a1d7SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(Y);CHKERRQ(ierr); 86087c2a1d7SRichard Tran Mills PetscFunctionReturn(0); 86187c2a1d7SRichard Tran Mills } 86287c2a1d7SRichard Tran Mills 86387c2a1d7SRichard Tran Mills PetscErrorCode MatAXPY_SeqAIJMKL(Mat Y,PetscScalar a,Mat X,MatStructure str) 86487c2a1d7SRichard Tran Mills { 86587c2a1d7SRichard Tran Mills PetscErrorCode ierr; 86687c2a1d7SRichard Tran Mills 86787c2a1d7SRichard Tran Mills PetscFunctionBegin; 86887c2a1d7SRichard Tran Mills ierr = MatAXPY_SeqAIJ(Y,a,X,str);CHKERRQ(ierr); 86987c2a1d7SRichard Tran Mills if (str == SAME_NONZERO_PATTERN) { 87087c2a1d7SRichard Tran Mills /* MatAssemblyEnd() is not called if SAME_NONZERO_PATTERN, so we need to force update of the MKL matrix handle. */ 87187c2a1d7SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(Y);CHKERRQ(ierr); 87287c2a1d7SRichard Tran Mills } 87387c2a1d7SRichard Tran Mills PetscFunctionReturn(0); 87487c2a1d7SRichard Tran Mills } 87587c2a1d7SRichard Tran Mills 8764a2a386eSRichard Tran Mills /* MatConvert_SeqAIJ_SeqAIJMKL converts a SeqAIJ matrix into a 8774a2a386eSRichard Tran Mills * SeqAIJMKL matrix. This routine is called by the MatCreate_SeqMKLAIJ() 8784a2a386eSRichard Tran Mills * routine, but can also be used to convert an assembled SeqAIJ matrix 8794a2a386eSRichard Tran Mills * into a SeqAIJMKL one. */ 8804a2a386eSRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJMKL(Mat A,MatType type,MatReuse reuse,Mat *newmat) 8814a2a386eSRichard Tran Mills { 8824a2a386eSRichard Tran Mills PetscErrorCode ierr; 8834a2a386eSRichard Tran Mills Mat B = *newmat; 8844a2a386eSRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 885c9d46305SRichard Tran Mills PetscBool set; 886e9c94282SRichard Tran Mills PetscBool sametype; 8874a2a386eSRichard Tran Mills 8884a2a386eSRichard Tran Mills PetscFunctionBegin; 8894a2a386eSRichard Tran Mills if (reuse == MAT_INITIAL_MATRIX) { 8904a2a386eSRichard Tran Mills ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr); 8914a2a386eSRichard Tran Mills } 8924a2a386eSRichard Tran Mills 893e9c94282SRichard Tran Mills ierr = PetscObjectTypeCompare((PetscObject)A,type,&sametype);CHKERRQ(ierr); 894e9c94282SRichard Tran Mills if (sametype) PetscFunctionReturn(0); 895e9c94282SRichard Tran Mills 8964a2a386eSRichard Tran Mills ierr = PetscNewLog(B,&aijmkl);CHKERRQ(ierr); 8974a2a386eSRichard Tran Mills B->spptr = (void*) aijmkl; 8984a2a386eSRichard Tran Mills 899df555b71SRichard Tran Mills /* Set function pointers for methods that we inherit from AIJ but override. 900969800c5SRichard Tran Mills * We also parse some command line options below, since those determine some of the methods we point to. */ 9014a2a386eSRichard Tran Mills B->ops->duplicate = MatDuplicate_SeqAIJMKL; 9024a2a386eSRichard Tran Mills B->ops->assemblyend = MatAssemblyEnd_SeqAIJMKL; 9034a2a386eSRichard Tran Mills B->ops->destroy = MatDestroy_SeqAIJMKL; 904c9d46305SRichard Tran Mills 9054abfa3b3SRichard Tran Mills aijmkl->sparse_optimized = PETSC_FALSE; 906d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 907d995685eSRichard Tran Mills aijmkl->no_SpMV2 = PETSC_FALSE; /* Default to using the SpMV2 routines if our MKL supports them. */ 908a8327b06SKarl Rupp #else 909d995685eSRichard Tran Mills aijmkl->no_SpMV2 = PETSC_TRUE; 910d995685eSRichard Tran Mills #endif 9115b49642aSRichard Tran Mills aijmkl->eager_inspection = PETSC_FALSE; 9124abfa3b3SRichard Tran Mills 9134abfa3b3SRichard Tran Mills /* Parse command line options. */ 914c9d46305SRichard Tran Mills ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"AIJMKL Options","Mat");CHKERRQ(ierr); 915c9d46305SRichard Tran Mills ierr = PetscOptionsBool("-mat_aijmkl_no_spmv2","NoSPMV2","None",(PetscBool)aijmkl->no_SpMV2,(PetscBool*)&aijmkl->no_SpMV2,&set);CHKERRQ(ierr); 9165b49642aSRichard Tran Mills ierr = PetscOptionsBool("-mat_aijmkl_eager_inspection","Eager Inspection","None",(PetscBool)aijmkl->eager_inspection,(PetscBool*)&aijmkl->eager_inspection,&set);CHKERRQ(ierr); 917c9d46305SRichard Tran Mills ierr = PetscOptionsEnd();CHKERRQ(ierr); 918d995685eSRichard Tran Mills #ifndef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 919d995685eSRichard Tran Mills if(!aijmkl->no_SpMV2) { 920d995685eSRichard 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"); 921d995685eSRichard Tran Mills aijmkl->no_SpMV2 = PETSC_TRUE; 922d995685eSRichard Tran Mills } 923d995685eSRichard Tran Mills #endif 924c9d46305SRichard Tran Mills 925c9d46305SRichard Tran Mills if(!aijmkl->no_SpMV2) { 926d995685eSRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 927df555b71SRichard Tran Mills B->ops->mult = MatMult_SeqAIJMKL_SpMV2; 928969800c5SRichard Tran Mills B->ops->multtranspose = MatMultTranspose_SeqAIJMKL_SpMV2; 929df555b71SRichard Tran Mills B->ops->multadd = MatMultAdd_SeqAIJMKL_SpMV2; 930969800c5SRichard Tran Mills B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL_SpMV2; 93145fbe478SRichard Tran Mills B->ops->matmult = MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2; 932e8be1fc7SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_SP2M 933e8be1fc7SRichard Tran Mills B->ops->matmultnumeric = MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2; 934e8be1fc7SRichard Tran Mills #endif 935a557fde5SRichard Tran Mills B->ops->transposematmult = MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2; 936d995685eSRichard Tran Mills #endif 937c9d46305SRichard Tran Mills } else { 9384a2a386eSRichard Tran Mills B->ops->mult = MatMult_SeqAIJMKL; 939969800c5SRichard Tran Mills B->ops->multtranspose = MatMultTranspose_SeqAIJMKL; 9404a2a386eSRichard Tran Mills B->ops->multadd = MatMultAdd_SeqAIJMKL; 941969800c5SRichard Tran Mills B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL; 942c9d46305SRichard Tran Mills } 9434a2a386eSRichard Tran Mills 944db63039fSRichard Tran Mills B->ops->scale = MatScale_SeqAIJMKL; 94587c2a1d7SRichard Tran Mills B->ops->diagonalscale = MatDiagonalScale_SeqAIJMKL; 94687c2a1d7SRichard Tran Mills B->ops->diagonalset = MatDiagonalSet_SeqAIJMKL; 94787c2a1d7SRichard Tran Mills B->ops->axpy = MatAXPY_SeqAIJMKL; 948db63039fSRichard Tran Mills 949db63039fSRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatScale_SeqAIJMKL_C",MatScale_SeqAIJMKL);CHKERRQ(ierr); 9504a2a386eSRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",MatConvert_SeqAIJMKL_SeqAIJ);CHKERRQ(ierr); 951e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaijmkl_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr); 952e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaijmkl_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr); 953e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaijmkl_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr); 95445fbe478SRichard Tran Mills if(!aijmkl->no_SpMV2) { 95545fbe478SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_OPTIMIZE 95645fbe478SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqaijmkl_seqaijmkl_C",MatMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr); 957e8be1fc7SRichard Tran Mills #ifdef PETSC_HAVE_MKL_SPARSE_SP2M 958e8be1fc7SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqaijmkl_seqaijmkl_C",MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr); 959e8be1fc7SRichard Tran Mills #endif 960372ec6bbSRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatTransposeMatMult_seqaijmkl_seqaijmkl_C",MatTransposeMatMult_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr); 96145fbe478SRichard Tran Mills #endif 96245fbe478SRichard Tran Mills } 9634a2a386eSRichard Tran Mills 9644a2a386eSRichard Tran Mills ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJMKL);CHKERRQ(ierr); 9654a2a386eSRichard Tran Mills *newmat = B; 9664a2a386eSRichard Tran Mills PetscFunctionReturn(0); 9674a2a386eSRichard Tran Mills } 9684a2a386eSRichard Tran Mills 9694a2a386eSRichard Tran Mills /*@C 9704a2a386eSRichard Tran Mills MatCreateSeqAIJMKL - Creates a sparse matrix of type SEQAIJMKL. 9714a2a386eSRichard Tran Mills This type inherits from AIJ and is largely identical, but uses sparse BLAS 9724a2a386eSRichard Tran Mills routines from Intel MKL whenever possible. 9733af10221SRichard Tran Mills MatMult, MatMultAdd, MatMultTranspose, MatMultTransposeAdd, MatMatMult, and MatTransposeMatMult 97490147e49SRichard Tran Mills operations are currently supported. 97590147e49SRichard Tran Mills If the installed version of MKL supports the "SpMV2" sparse 97690147e49SRichard Tran Mills inspector-executor routines, then those are used by default. 97790147e49SRichard Tran Mills 9784a2a386eSRichard Tran Mills Collective on MPI_Comm 9794a2a386eSRichard Tran Mills 9804a2a386eSRichard Tran Mills Input Parameters: 9814a2a386eSRichard Tran Mills + comm - MPI communicator, set to PETSC_COMM_SELF 9824a2a386eSRichard Tran Mills . m - number of rows 9834a2a386eSRichard Tran Mills . n - number of columns 9844a2a386eSRichard Tran Mills . nz - number of nonzeros per row (same for all rows) 9854a2a386eSRichard Tran Mills - nnz - array containing the number of nonzeros in the various rows 9864a2a386eSRichard Tran Mills (possibly different for each row) or NULL 9874a2a386eSRichard Tran Mills 9884a2a386eSRichard Tran Mills Output Parameter: 9894a2a386eSRichard Tran Mills . A - the matrix 9904a2a386eSRichard Tran Mills 99190147e49SRichard Tran Mills Options Database Keys: 99266b7eeb6SRichard Tran Mills + -mat_aijmkl_no_spmv2 - disable use of the SpMV2 inspector-executor routines 99366b7eeb6SRichard 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 99490147e49SRichard Tran Mills 9954a2a386eSRichard Tran Mills Notes: 9964a2a386eSRichard Tran Mills If nnz is given then nz is ignored 9974a2a386eSRichard Tran Mills 9984a2a386eSRichard Tran Mills Level: intermediate 9994a2a386eSRichard Tran Mills 100090147e49SRichard Tran Mills .keywords: matrix, MKL, sparse, parallel 10014a2a386eSRichard Tran Mills 10024a2a386eSRichard Tran Mills .seealso: MatCreate(), MatCreateMPIAIJMKL(), MatSetValues() 10034a2a386eSRichard Tran Mills @*/ 10044a2a386eSRichard Tran Mills PetscErrorCode MatCreateSeqAIJMKL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 10054a2a386eSRichard Tran Mills { 10064a2a386eSRichard Tran Mills PetscErrorCode ierr; 10074a2a386eSRichard Tran Mills 10084a2a386eSRichard Tran Mills PetscFunctionBegin; 10094a2a386eSRichard Tran Mills ierr = MatCreate(comm,A);CHKERRQ(ierr); 10104a2a386eSRichard Tran Mills ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 10114a2a386eSRichard Tran Mills ierr = MatSetType(*A,MATSEQAIJMKL);CHKERRQ(ierr); 10124a2a386eSRichard Tran Mills ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr); 10134a2a386eSRichard Tran Mills PetscFunctionReturn(0); 10144a2a386eSRichard Tran Mills } 10154a2a386eSRichard Tran Mills 10164a2a386eSRichard Tran Mills PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJMKL(Mat A) 10174a2a386eSRichard Tran Mills { 10184a2a386eSRichard Tran Mills PetscErrorCode ierr; 10194a2a386eSRichard Tran Mills 10204a2a386eSRichard Tran Mills PetscFunctionBegin; 10214a2a386eSRichard Tran Mills ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr); 10224a2a386eSRichard Tran Mills ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr); 10234a2a386eSRichard Tran Mills PetscFunctionReturn(0); 10244a2a386eSRichard Tran Mills } 1025