1b9e7e5c1SBarry Smith 24a2a386eSRichard Tran Mills /* 34a2a386eSRichard Tran Mills Defines basic operations for the MATSEQAIJMKL matrix class. 44a2a386eSRichard Tran Mills This class is derived from the MATSEQAIJ class and retains the 54a2a386eSRichard Tran Mills compressed row storage (aka Yale sparse matrix format) but uses 64a2a386eSRichard Tran Mills sparse BLAS operations from the Intel Math Kernel Library (MKL) 74a2a386eSRichard Tran Mills wherever possible. 84a2a386eSRichard Tran Mills */ 94a2a386eSRichard Tran Mills 104a2a386eSRichard Tran Mills #include <../src/mat/impls/aij/seq/aij.h> 114a2a386eSRichard Tran Mills #include <../src/mat/impls/aij/seq/aijmkl/aijmkl.h> 12b9e7e5c1SBarry Smith #include <mkl_spblas.h> 134a2a386eSRichard Tran Mills 144a2a386eSRichard Tran Mills typedef struct { 15c9d46305SRichard Tran Mills PetscBool no_SpMV2; /* If PETSC_TRUE, then don't use the MKL SpMV2 inspector-executor routines. */ 165b49642aSRichard Tran Mills PetscBool eager_inspection; /* If PETSC_TRUE, then call mkl_sparse_optimize() in MatDuplicate()/MatAssemblyEnd(). */ 174abfa3b3SRichard Tran Mills PetscBool sparse_optimized; /* If PETSC_TRUE, then mkl_sparse_optimize() has been called. */ 18551aa5c8SRichard Tran Mills PetscObjectState state; 19ffcab697SRichard Tran Mills #if defined(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; 33ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 344a2a386eSRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 35c1d5218aSRichard Tran Mills #endif 364a2a386eSRichard Tran Mills 374a2a386eSRichard Tran Mills PetscFunctionBegin; 384a2a386eSRichard Tran Mills if (reuse == MAT_INITIAL_MATRIX) { 394a2a386eSRichard Tran Mills ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr); 404a2a386eSRichard Tran Mills } 414a2a386eSRichard Tran Mills 424a2a386eSRichard Tran Mills /* Reset the original function pointers. */ 4354871a98SRichard Tran Mills B->ops->duplicate = MatDuplicate_SeqAIJ; 444a2a386eSRichard Tran Mills B->ops->assemblyend = MatAssemblyEnd_SeqAIJ; 454a2a386eSRichard Tran Mills B->ops->destroy = MatDestroy_SeqAIJ; 4654871a98SRichard Tran Mills B->ops->mult = MatMult_SeqAIJ; 47ff03dc53SRichard Tran Mills B->ops->multtranspose = MatMultTranspose_SeqAIJ; 4854871a98SRichard Tran Mills B->ops->multadd = MatMultAdd_SeqAIJ; 49ff03dc53SRichard Tran Mills B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJ; 50e8be1fc7SRichard Tran Mills B->ops->matmultnumeric = MatMatMultNumeric_SeqAIJ_SeqAIJ; 514f53af40SRichard Tran Mills B->ops->ptapnumeric = MatPtAPNumeric_SeqAIJ_SeqAIJ; 524a2a386eSRichard Tran Mills 53e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",NULL);CHKERRQ(ierr); 544222ddf1SHong Zhang 55ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 564abfa3b3SRichard Tran Mills /* Free everything in the Mat_SeqAIJMKL data structure. Currently, this 57e9c94282SRichard Tran Mills * simply involves destroying the MKL sparse matrix handle and then freeing 58e9c94282SRichard Tran Mills * the spptr pointer. */ 59a8327b06SKarl Rupp if (reuse == MAT_INITIAL_MATRIX) aijmkl = (Mat_SeqAIJMKL*)B->spptr; 60a8327b06SKarl Rupp 614abfa3b3SRichard Tran Mills if (aijmkl->sparse_optimized) { 620632b357SRichard Tran Mills sparse_status_t stat; 634abfa3b3SRichard Tran Mills stat = mkl_sparse_destroy(aijmkl->csrA); 649c46acdfSRichard 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"); 654abfa3b3SRichard Tran Mills } 66*6718818eSStefano Zampini #endif 67e9c94282SRichard Tran Mills ierr = PetscFree(B->spptr);CHKERRQ(ierr); 684a2a386eSRichard Tran Mills 694a2a386eSRichard Tran Mills /* Change the type of B to MATSEQAIJ. */ 704a2a386eSRichard Tran Mills ierr = PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ);CHKERRQ(ierr); 714a2a386eSRichard Tran Mills 724a2a386eSRichard Tran Mills *newmat = B; 734a2a386eSRichard Tran Mills PetscFunctionReturn(0); 744a2a386eSRichard Tran Mills } 754a2a386eSRichard Tran Mills 764a2a386eSRichard Tran Mills PetscErrorCode MatDestroy_SeqAIJMKL(Mat A) 774a2a386eSRichard Tran Mills { 784a2a386eSRichard Tran Mills PetscErrorCode ierr; 794a2a386eSRichard Tran Mills Mat_SeqAIJMKL *aijmkl = (Mat_SeqAIJMKL*) A->spptr; 804a2a386eSRichard Tran Mills 814a2a386eSRichard Tran Mills PetscFunctionBegin; 82e9c94282SRichard Tran Mills 83e9c94282SRichard Tran Mills /* If MatHeaderMerge() was used, then this SeqAIJMKL matrix will not have an 84e9c94282SRichard Tran Mills * spptr pointer. */ 85e9c94282SRichard Tran Mills if (aijmkl) { 864a2a386eSRichard Tran Mills /* Clean up everything in the Mat_SeqAIJMKL data structure, then free A->spptr. */ 87ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 884abfa3b3SRichard Tran Mills if (aijmkl->sparse_optimized) { 894abfa3b3SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 904abfa3b3SRichard Tran Mills stat = mkl_sparse_destroy(aijmkl->csrA); 919c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy"); 924abfa3b3SRichard Tran Mills } 934abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 944a2a386eSRichard Tran Mills ierr = PetscFree(A->spptr);CHKERRQ(ierr); 95e9c94282SRichard Tran Mills } 964a2a386eSRichard Tran Mills 974a2a386eSRichard Tran Mills /* Change the type of A back to SEQAIJ and use MatDestroy_SeqAIJ() 984a2a386eSRichard Tran Mills * to destroy everything that remains. */ 994a2a386eSRichard Tran Mills ierr = PetscObjectChangeTypeName((PetscObject)A, MATSEQAIJ);CHKERRQ(ierr); 1004a2a386eSRichard Tran Mills /* Note that I don't call MatSetType(). I believe this is because that 1014a2a386eSRichard Tran Mills * is only to be called when *building* a matrix. I could be wrong, but 1024a2a386eSRichard Tran Mills * that is how things work for the SuperLU matrix class. */ 1034a2a386eSRichard Tran Mills ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr); 1044a2a386eSRichard Tran Mills PetscFunctionReturn(0); 1054a2a386eSRichard Tran Mills } 1064a2a386eSRichard Tran Mills 1075b49642aSRichard Tran Mills /* MatSeqAIJKL_create_mkl_handle(), if called with an AIJMKL matrix that has not had mkl_sparse_optimize() called for it, 1085b49642aSRichard Tran Mills * creates an MKL sparse matrix handle from the AIJ arrays and calls mkl_sparse_optimize(). 1095b49642aSRichard Tran Mills * If called with an AIJMKL matrix for which aijmkl->sparse_optimized == PETSC_TRUE, then it destroys the old matrix 1105b49642aSRichard Tran Mills * handle, creates a new one, and then calls mkl_sparse_optimize(). 1115b49642aSRichard Tran Mills * Although in normal MKL usage it is possible to have a valid matrix handle on which mkl_sparse_optimize() has not been 1125b49642aSRichard Tran Mills * called, for AIJMKL the handle creation and optimization step always occur together, so we don't handle the case of 1135b49642aSRichard Tran Mills * an unoptimized matrix handle here. */ 1146e369cd5SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_mkl_handle(Mat A) 1154a2a386eSRichard Tran Mills { 116ffcab697SRichard Tran Mills #if !defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 1176e369cd5SRichard Tran Mills /* If the MKL library does not have mkl_sparse_optimize(), then this routine 1186e369cd5SRichard Tran Mills * does nothing. We make it callable anyway in this case because it cuts 1196e369cd5SRichard Tran Mills * down on littering the code with #ifdefs. */ 12045fbe478SRichard Tran Mills PetscFunctionBegin; 1216e369cd5SRichard Tran Mills PetscFunctionReturn(0); 1226e369cd5SRichard Tran Mills #else 123a8327b06SKarl Rupp Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 124a8327b06SKarl Rupp Mat_SeqAIJMKL *aijmkl = (Mat_SeqAIJMKL*)A->spptr; 125a8327b06SKarl Rupp PetscInt m,n; 126a8327b06SKarl Rupp MatScalar *aa; 127a8327b06SKarl Rupp PetscInt *aj,*ai; 1286e369cd5SRichard Tran Mills sparse_status_t stat; 129551aa5c8SRichard Tran Mills PetscErrorCode ierr; 1304a2a386eSRichard Tran Mills 131a8327b06SKarl Rupp PetscFunctionBegin; 132e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 133e626a176SRichard Tran Mills /* For MKL versions that still support the old, non-inspector-executor interfaces versions, we simply exit here if the no_SpMV2 134e626a176SRichard Tran Mills * option has been specified. For versions that have deprecated the old interfaces (version 18, update 2 and later), we must 135e626a176SRichard Tran Mills * use the new inspector-executor interfaces, but we can still use the old, non-inspector-executor code by not calling 136e626a176SRichard Tran Mills * mkl_sparse_optimize() later. */ 1376e369cd5SRichard Tran Mills if (aijmkl->no_SpMV2) PetscFunctionReturn(0); 1384d51fa23SRichard Tran Mills #endif 1396e369cd5SRichard Tran Mills 1400632b357SRichard Tran Mills if (aijmkl->sparse_optimized) { 1410632b357SRichard Tran Mills /* Matrix has been previously assembled and optimized. Must destroy old 1420632b357SRichard Tran Mills * matrix handle before running the optimization step again. */ 1430632b357SRichard Tran Mills stat = mkl_sparse_destroy(aijmkl->csrA); 1449c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy"); 1450632b357SRichard Tran Mills } 1468d3fe1b0SRichard Tran Mills aijmkl->sparse_optimized = PETSC_FALSE; 1476e369cd5SRichard Tran Mills 148c9d46305SRichard Tran Mills /* Now perform the SpMV2 setup and matrix optimization. */ 149df555b71SRichard Tran Mills aijmkl->descr.type = SPARSE_MATRIX_TYPE_GENERAL; 150df555b71SRichard Tran Mills aijmkl->descr.mode = SPARSE_FILL_MODE_LOWER; 151df555b71SRichard Tran Mills aijmkl->descr.diag = SPARSE_DIAG_NON_UNIT; 15258678438SRichard Tran Mills m = A->rmap->n; 15358678438SRichard Tran Mills n = A->cmap->n; 154df555b71SRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 155df555b71SRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 156df555b71SRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 15780095d54SIrina Sokolova if ((a->nz!=0) & !(A->structure_only)) { 1588d3fe1b0SRichard Tran Mills /* Create a new, optimized sparse matrix handle only if the matrix has nonzero entries. 1598d3fe1b0SRichard Tran Mills * The MKL sparse-inspector executor routines don't like being passed an empty matrix. */ 16058678438SRichard Tran Mills stat = mkl_sparse_x_create_csr(&aijmkl->csrA,SPARSE_INDEX_BASE_ZERO,m,n,ai,ai+1,aj,aa); 161e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to create matrix handle"); 162df555b71SRichard Tran Mills stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000); 163e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set mv_hint"); 164df555b71SRichard Tran Mills stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE); 165e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set memory_hint"); 1661950a7e7SRichard Tran Mills if (!aijmkl->no_SpMV2) { 167df555b71SRichard Tran Mills stat = mkl_sparse_optimize(aijmkl->csrA); 168e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_optimize"); 1691950a7e7SRichard Tran Mills } 1704abfa3b3SRichard Tran Mills aijmkl->sparse_optimized = PETSC_TRUE; 171e995cf24SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&(aijmkl->state));CHKERRQ(ierr); 172c9d46305SRichard Tran Mills } 1736e369cd5SRichard Tran Mills 1746e369cd5SRichard Tran Mills PetscFunctionReturn(0); 175d995685eSRichard Tran Mills #endif 1766e369cd5SRichard Tran Mills } 1776e369cd5SRichard Tran Mills 17819afcda9SRichard Tran Mills /* MatSeqAIJMKL_create_from_mkl_handle() creates a sequential AIJMKL matrix from an MKL sparse matrix handle. 17919afcda9SRichard Tran Mills * We need this to implement MatMatMult() using the MKL inspector-executor routines, which return an (unoptimized) 1806c87cf42SRichard Tran Mills * matrix handle. 181aab60f1bSRichard Tran Mills * Note: This routine simply destroys and replaces the original matrix if MAT_REUSE_MATRIX has been specified, as 182aab60f1bSRichard Tran Mills * there is no good alternative. */ 183ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 1846c87cf42SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_from_mkl_handle(MPI_Comm comm,sparse_matrix_t csrA,MatReuse reuse,Mat *mat) 18519afcda9SRichard Tran Mills { 18619afcda9SRichard Tran Mills PetscErrorCode ierr; 18719afcda9SRichard Tran Mills sparse_status_t stat; 18819afcda9SRichard Tran Mills sparse_index_base_t indexing; 18919afcda9SRichard Tran Mills PetscInt nrows, ncols; 19045fbe478SRichard Tran Mills PetscInt *aj,*ai,*dummy; 19119afcda9SRichard Tran Mills MatScalar *aa; 19219afcda9SRichard Tran Mills Mat A; 19319afcda9SRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 19419afcda9SRichard Tran Mills 19545fbe478SRichard Tran Mills /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */ 19645fbe478SRichard Tran Mills stat = mkl_sparse_x_export_csr(csrA,&indexing,&nrows,&ncols,&ai,&dummy,&aj,&aa); 1979c46acdfSRichard 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()"); 1986c87cf42SRichard Tran Mills 199aab60f1bSRichard Tran Mills if (reuse == MAT_REUSE_MATRIX) { 200aab60f1bSRichard Tran Mills ierr = MatDestroy(mat);CHKERRQ(ierr); 201aab60f1bSRichard Tran Mills } 20219afcda9SRichard Tran Mills ierr = MatCreate(comm,&A);CHKERRQ(ierr); 20319afcda9SRichard Tran Mills ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr); 20445fbe478SRichard Tran Mills ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,nrows,ncols);CHKERRQ(ierr); 205aab60f1bSRichard Tran Mills /* We use MatSeqAIJSetPreallocationCSR() instead of MatCreateSeqAIJWithArrays() because we must copy the arrays exported 206aab60f1bSRichard Tran Mills * from MKL; MKL developers tell us that modifying the arrays may cause unexpected results when using the MKL handle, and 207aab60f1bSRichard Tran Mills * they will be destroyed when the MKL handle is destroyed. 208aab60f1bSRichard Tran Mills * (In the interest of reducing memory consumption in future, can we figure out good ways to deal with this?) */ 20919afcda9SRichard Tran Mills ierr = MatSeqAIJSetPreallocationCSR(A,ai,aj,aa);CHKERRQ(ierr); 21019afcda9SRichard Tran Mills 21119afcda9SRichard Tran Mills /* We now have an assembled sequential AIJ matrix created from copies of the exported arrays from the MKL matrix handle. 21219afcda9SRichard Tran Mills * Now turn it into a MATSEQAIJMKL. */ 21319afcda9SRichard Tran Mills ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr); 2146c87cf42SRichard Tran Mills 21519afcda9SRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 21619afcda9SRichard Tran Mills aijmkl->csrA = csrA; 2176c87cf42SRichard Tran Mills 21819afcda9SRichard Tran Mills /* The below code duplicates much of what is in MatSeqAIJKL_create_mkl_handle(). I dislike this code duplication, but 21919afcda9SRichard Tran Mills * MatSeqAIJMKL_create_mkl_handle() cannot be used because we don't need to create a handle -- we've already got one, 22019afcda9SRichard Tran Mills * and just need to be able to run the MKL optimization step. */ 221f3fd1758SRichard Tran Mills aijmkl->descr.type = SPARSE_MATRIX_TYPE_GENERAL; 222f3fd1758SRichard Tran Mills aijmkl->descr.mode = SPARSE_FILL_MODE_LOWER; 223f3fd1758SRichard Tran Mills aijmkl->descr.diag = SPARSE_DIAG_NON_UNIT; 22419afcda9SRichard Tran Mills stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000); 22551539a68SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set mv_hint"); 22619afcda9SRichard Tran Mills stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE); 22751539a68SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set memory_hint"); 2281950a7e7SRichard Tran Mills if (!aijmkl->no_SpMV2) { 22919afcda9SRichard Tran Mills stat = mkl_sparse_optimize(aijmkl->csrA); 23051539a68SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_optimize"); 2311950a7e7SRichard Tran Mills } 23219afcda9SRichard Tran Mills aijmkl->sparse_optimized = PETSC_TRUE; 233e995cf24SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&(aijmkl->state));CHKERRQ(ierr); 23419afcda9SRichard Tran Mills 23519afcda9SRichard Tran Mills *mat = A; 23619afcda9SRichard Tran Mills PetscFunctionReturn(0); 23719afcda9SRichard Tran Mills } 23819afcda9SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 23919afcda9SRichard Tran Mills 240e8be1fc7SRichard Tran Mills /* MatSeqAIJMKL_update_from_mkl_handle() updates the matrix values array from the contents of the associated MKL sparse matrix handle. 241e8be1fc7SRichard 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 242e8be1fc7SRichard Tran Mills * MatMatMultNumeric(). */ 243ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 244e8be1fc7SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_update_from_mkl_handle(Mat A) 245e8be1fc7SRichard Tran Mills { 246e8be1fc7SRichard Tran Mills PetscInt i; 247e8be1fc7SRichard Tran Mills PetscInt nrows,ncols; 248e8be1fc7SRichard Tran Mills PetscInt nz; 249e8be1fc7SRichard Tran Mills PetscInt *ai,*aj,*dummy; 250e8be1fc7SRichard Tran Mills PetscScalar *aa; 251e8be1fc7SRichard Tran Mills PetscErrorCode ierr; 252e8be1fc7SRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 253e8be1fc7SRichard Tran Mills sparse_status_t stat; 254e8be1fc7SRichard Tran Mills sparse_index_base_t indexing; 255e8be1fc7SRichard Tran Mills 256e8be1fc7SRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 257e8be1fc7SRichard Tran Mills 258e8be1fc7SRichard Tran Mills /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */ 259e8be1fc7SRichard Tran Mills stat = mkl_sparse_x_export_csr(aijmkl->csrA,&indexing,&nrows,&ncols,&ai,&dummy,&aj,&aa); 260e8be1fc7SRichard 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()"); 261e8be1fc7SRichard Tran Mills 262e8be1fc7SRichard 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 263e8be1fc7SRichard Tran Mills * representations differ in small ways (e.g., more explicit nonzeros per row due to preallocation). */ 264e8be1fc7SRichard Tran Mills for (i=0; i<nrows; i++) { 265e8be1fc7SRichard Tran Mills nz = ai[i+1] - ai[i]; 266e8be1fc7SRichard Tran Mills ierr = MatSetValues_SeqAIJ(A, 1, &i, nz, aj+ai[i], aa+ai[i], INSERT_VALUES);CHKERRQ(ierr); 267e8be1fc7SRichard Tran Mills } 268e8be1fc7SRichard Tran Mills 269e8be1fc7SRichard Tran Mills ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 270e8be1fc7SRichard Tran Mills ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 271e8be1fc7SRichard Tran Mills 272e995cf24SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&(aijmkl->state));CHKERRQ(ierr); 273e995cf24SRichard Tran Mills /* We mark our matrix as having a valid, optimized MKL handle. 274e995cf24SRichard Tran Mills * TODO: It is valid, but I am not sure if it is optimized. Need to ask MKL developers. */ 275e995cf24SRichard Tran Mills aijmkl->sparse_optimized = PETSC_TRUE; 276e995cf24SRichard Tran Mills 277e8be1fc7SRichard Tran Mills PetscFunctionReturn(0); 278e8be1fc7SRichard Tran Mills } 279e8be1fc7SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 280e8be1fc7SRichard Tran Mills 2816e369cd5SRichard Tran Mills PetscErrorCode MatDuplicate_SeqAIJMKL(Mat A, MatDuplicateOption op, Mat *M) 2826e369cd5SRichard Tran Mills { 2836e369cd5SRichard Tran Mills PetscErrorCode ierr; 2846e369cd5SRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 2856e369cd5SRichard Tran Mills Mat_SeqAIJMKL *aijmkl_dest; 2866e369cd5SRichard Tran Mills 2876e369cd5SRichard Tran Mills PetscFunctionBegin; 2886e369cd5SRichard Tran Mills ierr = MatDuplicate_SeqAIJ(A,op,M);CHKERRQ(ierr); 2896e369cd5SRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 2906e369cd5SRichard Tran Mills aijmkl_dest = (Mat_SeqAIJMKL*) (*M)->spptr; 291580bdb30SBarry Smith ierr = PetscArraycpy(aijmkl_dest,aijmkl,1);CHKERRQ(ierr); 2926e369cd5SRichard Tran Mills aijmkl_dest->sparse_optimized = PETSC_FALSE; 2935b49642aSRichard Tran Mills if (aijmkl->eager_inspection) { 2946e369cd5SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr); 2955b49642aSRichard Tran Mills } 2966e369cd5SRichard Tran Mills PetscFunctionReturn(0); 2976e369cd5SRichard Tran Mills } 2986e369cd5SRichard Tran Mills 2996e369cd5SRichard Tran Mills PetscErrorCode MatAssemblyEnd_SeqAIJMKL(Mat A, MatAssemblyType mode) 3006e369cd5SRichard Tran Mills { 3016e369cd5SRichard Tran Mills PetscErrorCode ierr; 3026e369cd5SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3035b49642aSRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 3046e369cd5SRichard Tran Mills 3056e369cd5SRichard Tran Mills PetscFunctionBegin; 3066e369cd5SRichard Tran Mills if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 3076e369cd5SRichard Tran Mills 3086e369cd5SRichard Tran Mills /* Since a MATSEQAIJMKL matrix is really just a MATSEQAIJ with some 3096e369cd5SRichard Tran Mills * extra information and some different methods, call the AssemblyEnd 3106e369cd5SRichard Tran Mills * routine for a MATSEQAIJ. 3116e369cd5SRichard Tran Mills * I'm not sure if this is the best way to do this, but it avoids 312d96e85feSRichard Tran Mills * a lot of code duplication. */ 3136e369cd5SRichard Tran Mills a->inode.use = PETSC_FALSE; /* Must disable: otherwise the MKL routines won't get used. */ 3146e369cd5SRichard Tran Mills ierr = MatAssemblyEnd_SeqAIJ(A, mode);CHKERRQ(ierr); 3156e369cd5SRichard Tran Mills 3165b49642aSRichard Tran Mills /* If the user has requested "eager" inspection, create the optimized MKL sparse handle (if needed; the function checks). 3175b49642aSRichard Tran Mills * (The default is to do "lazy" inspection, deferring this until something like MatMult() is called.) */ 3185b49642aSRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 3195b49642aSRichard Tran Mills if (aijmkl->eager_inspection) { 3206e369cd5SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr); 3215b49642aSRichard Tran Mills } 322df555b71SRichard Tran Mills 3234a2a386eSRichard Tran Mills PetscFunctionReturn(0); 3244a2a386eSRichard Tran Mills } 3254a2a386eSRichard Tran Mills 326e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 3274a2a386eSRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL(Mat A,Vec xx,Vec yy) 3284a2a386eSRichard Tran Mills { 3294a2a386eSRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3304a2a386eSRichard Tran Mills const PetscScalar *x; 3314a2a386eSRichard Tran Mills PetscScalar *y; 3324a2a386eSRichard Tran Mills const MatScalar *aa; 3334a2a386eSRichard Tran Mills PetscErrorCode ierr; 3344a2a386eSRichard Tran Mills PetscInt m=A->rmap->n; 335db63039fSRichard Tran Mills PetscInt n=A->cmap->n; 336db63039fSRichard Tran Mills PetscScalar alpha = 1.0; 337db63039fSRichard Tran Mills PetscScalar beta = 0.0; 3384a2a386eSRichard Tran Mills const PetscInt *aj,*ai; 339db63039fSRichard Tran Mills char matdescra[6]; 340db63039fSRichard Tran Mills 3414a2a386eSRichard Tran Mills 3424a2a386eSRichard Tran Mills /* Variables not in MatMult_SeqAIJ. */ 343ff03dc53SRichard Tran Mills char transa = 'n'; /* Used to indicate to MKL that we are not computing the transpose product. */ 344ff03dc53SRichard Tran Mills 345ff03dc53SRichard Tran Mills PetscFunctionBegin; 346db63039fSRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 347db63039fSRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 348ff03dc53SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 349ff03dc53SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 350ff03dc53SRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 351ff03dc53SRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 352ff03dc53SRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 353ff03dc53SRichard Tran Mills 354ff03dc53SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 355db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y); 356ff03dc53SRichard Tran Mills 357ff03dc53SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 358ff03dc53SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 359ff03dc53SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 360ff03dc53SRichard Tran Mills PetscFunctionReturn(0); 361ff03dc53SRichard Tran Mills } 3621950a7e7SRichard Tran Mills #endif 363ff03dc53SRichard Tran Mills 364ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 365df555b71SRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy) 366df555b71SRichard Tran Mills { 367df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 368df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 369df555b71SRichard Tran Mills const PetscScalar *x; 370df555b71SRichard Tran Mills PetscScalar *y; 371df555b71SRichard Tran Mills PetscErrorCode ierr; 372df555b71SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 373551aa5c8SRichard Tran Mills PetscObjectState state; 374df555b71SRichard Tran Mills 375df555b71SRichard Tran Mills PetscFunctionBegin; 376df555b71SRichard Tran Mills 37738987b35SRichard Tran Mills /* If there are no nonzero entries, zero yy and return immediately. */ 37838987b35SRichard Tran Mills if(!a->nz) { 37938987b35SRichard Tran Mills PetscInt i; 38038987b35SRichard Tran Mills PetscInt m=A->rmap->n; 38138987b35SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 38238987b35SRichard Tran Mills for (i=0; i<m; i++) { 38338987b35SRichard Tran Mills y[i] = 0.0; 38438987b35SRichard Tran Mills } 38538987b35SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 38638987b35SRichard Tran Mills PetscFunctionReturn(0); 38738987b35SRichard Tran Mills } 388f36dfe3fSRichard Tran Mills 389df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 390df555b71SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 391df555b71SRichard Tran Mills 3923fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 3933fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 3943fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 395551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 396551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 3973fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 3983fa15762SRichard Tran Mills } 3993fa15762SRichard Tran Mills 400df555b71SRichard Tran Mills /* Call MKL SpMV2 executor routine to do the MatMult. */ 401df555b71SRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y); 4029c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 403df555b71SRichard Tran Mills 404df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 405df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 406df555b71SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 407df555b71SRichard Tran Mills PetscFunctionReturn(0); 408df555b71SRichard Tran Mills } 409d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 410df555b71SRichard Tran Mills 411e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 412ff03dc53SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL(Mat A,Vec xx,Vec yy) 413ff03dc53SRichard Tran Mills { 414ff03dc53SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 415ff03dc53SRichard Tran Mills const PetscScalar *x; 416ff03dc53SRichard Tran Mills PetscScalar *y; 417ff03dc53SRichard Tran Mills const MatScalar *aa; 418ff03dc53SRichard Tran Mills PetscErrorCode ierr; 419ff03dc53SRichard Tran Mills PetscInt m=A->rmap->n; 420db63039fSRichard Tran Mills PetscInt n=A->cmap->n; 421db63039fSRichard Tran Mills PetscScalar alpha = 1.0; 422db63039fSRichard Tran Mills PetscScalar beta = 0.0; 423ff03dc53SRichard Tran Mills const PetscInt *aj,*ai; 424db63039fSRichard Tran Mills char matdescra[6]; 425ff03dc53SRichard Tran Mills 426ff03dc53SRichard Tran Mills /* Variables not in MatMultTranspose_SeqAIJ. */ 427ff03dc53SRichard Tran Mills char transa = 't'; /* Used to indicate to MKL that we are computing the transpose product. */ 4284a2a386eSRichard Tran Mills 4294a2a386eSRichard Tran Mills PetscFunctionBegin; 430969800c5SRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 431969800c5SRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 4324a2a386eSRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 4334a2a386eSRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 4344a2a386eSRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 4354a2a386eSRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 4364a2a386eSRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 4374a2a386eSRichard Tran Mills 4384a2a386eSRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 439db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y); 4404a2a386eSRichard Tran Mills 4414a2a386eSRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 4424a2a386eSRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 4434a2a386eSRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 4444a2a386eSRichard Tran Mills PetscFunctionReturn(0); 4454a2a386eSRichard Tran Mills } 4461950a7e7SRichard Tran Mills #endif 4474a2a386eSRichard Tran Mills 448ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 449df555b71SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy) 450df555b71SRichard Tran Mills { 451df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 452df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 453df555b71SRichard Tran Mills const PetscScalar *x; 454df555b71SRichard Tran Mills PetscScalar *y; 455df555b71SRichard Tran Mills PetscErrorCode ierr; 4560632b357SRichard Tran Mills sparse_status_t stat; 457551aa5c8SRichard Tran Mills PetscObjectState state; 458df555b71SRichard Tran Mills 459df555b71SRichard Tran Mills PetscFunctionBegin; 460df555b71SRichard Tran Mills 46138987b35SRichard Tran Mills /* If there are no nonzero entries, zero yy and return immediately. */ 46238987b35SRichard Tran Mills if(!a->nz) { 46338987b35SRichard Tran Mills PetscInt i; 46438987b35SRichard Tran Mills PetscInt n=A->cmap->n; 46538987b35SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 46638987b35SRichard Tran Mills for (i=0; i<n; i++) { 46738987b35SRichard Tran Mills y[i] = 0.0; 46838987b35SRichard Tran Mills } 46938987b35SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 47038987b35SRichard Tran Mills PetscFunctionReturn(0); 47138987b35SRichard Tran Mills } 472f36dfe3fSRichard Tran Mills 473df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 474df555b71SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 475df555b71SRichard Tran Mills 4763fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 4773fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 4783fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 479551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 480551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 4813fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 4823fa15762SRichard Tran Mills } 4833fa15762SRichard Tran Mills 484df555b71SRichard Tran Mills /* Call MKL SpMV2 executor routine to do the MatMultTranspose. */ 485df555b71SRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y); 4869c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 487df555b71SRichard Tran Mills 488df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 489df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 490df555b71SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 491df555b71SRichard Tran Mills PetscFunctionReturn(0); 492df555b71SRichard Tran Mills } 493d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 494df555b71SRichard Tran Mills 495e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 4964a2a386eSRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz) 4974a2a386eSRichard Tran Mills { 4984a2a386eSRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 4994a2a386eSRichard Tran Mills const PetscScalar *x; 5004a2a386eSRichard Tran Mills PetscScalar *y,*z; 5014a2a386eSRichard Tran Mills const MatScalar *aa; 5024a2a386eSRichard Tran Mills PetscErrorCode ierr; 5034a2a386eSRichard Tran Mills PetscInt m=A->rmap->n; 504db63039fSRichard Tran Mills PetscInt n=A->cmap->n; 5054a2a386eSRichard Tran Mills const PetscInt *aj,*ai; 5064a2a386eSRichard Tran Mills PetscInt i; 5074a2a386eSRichard Tran Mills 508ff03dc53SRichard Tran Mills /* Variables not in MatMultAdd_SeqAIJ. */ 509ff03dc53SRichard Tran Mills char transa = 'n'; /* Used to indicate to MKL that we are not computing the transpose product. */ 510a84739b8SRichard Tran Mills PetscScalar alpha = 1.0; 511db63039fSRichard Tran Mills PetscScalar beta; 512a84739b8SRichard Tran Mills char matdescra[6]; 513ff03dc53SRichard Tran Mills 514ff03dc53SRichard Tran Mills PetscFunctionBegin; 515a84739b8SRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 516a84739b8SRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 517a84739b8SRichard Tran Mills 518ff03dc53SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 519ff03dc53SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 520ff03dc53SRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 521ff03dc53SRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 522ff03dc53SRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 523ff03dc53SRichard Tran Mills 524ff03dc53SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 525a84739b8SRichard Tran Mills if (zz == yy) { 526a84739b8SRichard 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. */ 527db63039fSRichard Tran Mills beta = 1.0; 528db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 529a84739b8SRichard Tran Mills } else { 530db63039fSRichard Tran Mills /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z. 531db63039fSRichard Tran Mills * MKL sparse BLAS does not have a MatMultAdd equivalent. */ 532db63039fSRichard Tran Mills beta = 0.0; 533db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 534ff03dc53SRichard Tran Mills for (i=0; i<m; i++) { 535ff03dc53SRichard Tran Mills z[i] += y[i]; 536ff03dc53SRichard Tran Mills } 537a84739b8SRichard Tran Mills } 538ff03dc53SRichard Tran Mills 539ff03dc53SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 540ff03dc53SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 541ff03dc53SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 542ff03dc53SRichard Tran Mills PetscFunctionReturn(0); 543ff03dc53SRichard Tran Mills } 5441950a7e7SRichard Tran Mills #endif 545ff03dc53SRichard Tran Mills 546ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 547df555b71SRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz) 548df555b71SRichard Tran Mills { 549df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 550df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 551df555b71SRichard Tran Mills const PetscScalar *x; 552df555b71SRichard Tran Mills PetscScalar *y,*z; 553df555b71SRichard Tran Mills PetscErrorCode ierr; 554df555b71SRichard Tran Mills PetscInt m=A->rmap->n; 555df555b71SRichard Tran Mills PetscInt i; 556df555b71SRichard Tran Mills 557df555b71SRichard Tran Mills /* Variables not in MatMultAdd_SeqAIJ. */ 558df555b71SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 559551aa5c8SRichard Tran Mills PetscObjectState state; 560df555b71SRichard Tran Mills 561df555b71SRichard Tran Mills PetscFunctionBegin; 562df555b71SRichard Tran Mills 56338987b35SRichard Tran Mills /* If there are no nonzero entries, set zz = yy and return immediately. */ 56438987b35SRichard Tran Mills if(!a->nz) { 56538987b35SRichard Tran Mills PetscInt i; 56638987b35SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 56738987b35SRichard Tran Mills for (i=0; i<m; i++) { 56838987b35SRichard Tran Mills z[i] = y[i]; 56938987b35SRichard Tran Mills } 57038987b35SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 57138987b35SRichard Tran Mills PetscFunctionReturn(0); 57238987b35SRichard Tran Mills } 573df555b71SRichard Tran Mills 574df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 575df555b71SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 576df555b71SRichard Tran Mills 5773fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 5783fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 5793fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 580551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 581551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 5823fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 5833fa15762SRichard Tran Mills } 5843fa15762SRichard Tran Mills 585df555b71SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 586df555b71SRichard Tran Mills if (zz == yy) { 587df555b71SRichard 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, 588df555b71SRichard Tran Mills * with alpha and beta both set to 1.0. */ 589db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z); 5909c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 591df555b71SRichard Tran Mills } else { 592df555b71SRichard 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 593df555b71SRichard Tran Mills * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */ 594db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z); 5959c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 596df555b71SRichard Tran Mills for (i=0; i<m; i++) { 597df555b71SRichard Tran Mills z[i] += y[i]; 598df555b71SRichard Tran Mills } 599df555b71SRichard Tran Mills } 600df555b71SRichard Tran Mills 601df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 602df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 603df555b71SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 604df555b71SRichard Tran Mills PetscFunctionReturn(0); 605df555b71SRichard Tran Mills } 606d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 607df555b71SRichard Tran Mills 608e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 609ff03dc53SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz) 610ff03dc53SRichard Tran Mills { 611ff03dc53SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 612ff03dc53SRichard Tran Mills const PetscScalar *x; 613ff03dc53SRichard Tran Mills PetscScalar *y,*z; 614ff03dc53SRichard Tran Mills const MatScalar *aa; 615ff03dc53SRichard Tran Mills PetscErrorCode ierr; 616ff03dc53SRichard Tran Mills PetscInt m=A->rmap->n; 617db63039fSRichard Tran Mills PetscInt n=A->cmap->n; 618ff03dc53SRichard Tran Mills const PetscInt *aj,*ai; 619ff03dc53SRichard Tran Mills PetscInt i; 620ff03dc53SRichard Tran Mills 621ff03dc53SRichard Tran Mills /* Variables not in MatMultTransposeAdd_SeqAIJ. */ 622ff03dc53SRichard Tran Mills char transa = 't'; /* Used to indicate to MKL that we are computing the transpose product. */ 623a84739b8SRichard Tran Mills PetscScalar alpha = 1.0; 624db63039fSRichard Tran Mills PetscScalar beta; 625a84739b8SRichard Tran Mills char matdescra[6]; 6264a2a386eSRichard Tran Mills 6274a2a386eSRichard Tran Mills PetscFunctionBegin; 628a84739b8SRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 629a84739b8SRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 630a84739b8SRichard Tran Mills 6314a2a386eSRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 6324a2a386eSRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 6334a2a386eSRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 6344a2a386eSRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 6354a2a386eSRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 6364a2a386eSRichard Tran Mills 6374a2a386eSRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 638a84739b8SRichard Tran Mills if (zz == yy) { 639a84739b8SRichard 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. */ 640db63039fSRichard Tran Mills beta = 1.0; 641969800c5SRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 642a84739b8SRichard Tran Mills } else { 643db63039fSRichard Tran Mills /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z. 644db63039fSRichard Tran Mills * MKL sparse BLAS does not have a MatMultAdd equivalent. */ 645db63039fSRichard Tran Mills beta = 0.0; 646db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 647969800c5SRichard Tran Mills for (i=0; i<n; i++) { 6484a2a386eSRichard Tran Mills z[i] += y[i]; 6494a2a386eSRichard Tran Mills } 650a84739b8SRichard Tran Mills } 6514a2a386eSRichard Tran Mills 6524a2a386eSRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 6534a2a386eSRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 6544a2a386eSRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 6554a2a386eSRichard Tran Mills PetscFunctionReturn(0); 6564a2a386eSRichard Tran Mills } 6571950a7e7SRichard Tran Mills #endif 6584a2a386eSRichard Tran Mills 659ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 660df555b71SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz) 661df555b71SRichard Tran Mills { 662df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 663df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 664df555b71SRichard Tran Mills const PetscScalar *x; 665df555b71SRichard Tran Mills PetscScalar *y,*z; 666df555b71SRichard Tran Mills PetscErrorCode ierr; 667969800c5SRichard Tran Mills PetscInt n=A->cmap->n; 668df555b71SRichard Tran Mills PetscInt i; 669551aa5c8SRichard Tran Mills PetscObjectState state; 670df555b71SRichard Tran Mills 671df555b71SRichard Tran Mills /* Variables not in MatMultTransposeAdd_SeqAIJ. */ 672df555b71SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 673df555b71SRichard Tran Mills 674df555b71SRichard Tran Mills PetscFunctionBegin; 675df555b71SRichard Tran Mills 67638987b35SRichard Tran Mills /* If there are no nonzero entries, set zz = yy and return immediately. */ 67738987b35SRichard Tran Mills if(!a->nz) { 67838987b35SRichard Tran Mills PetscInt i; 67938987b35SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 68038987b35SRichard Tran Mills for (i=0; i<n; i++) { 68138987b35SRichard Tran Mills z[i] = y[i]; 68238987b35SRichard Tran Mills } 68338987b35SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 68438987b35SRichard Tran Mills PetscFunctionReturn(0); 68538987b35SRichard Tran Mills } 686f36dfe3fSRichard Tran Mills 687df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 688df555b71SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 689df555b71SRichard Tran Mills 6903fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 6913fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 6923fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 693551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 694551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 6953fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 6963fa15762SRichard Tran Mills } 6973fa15762SRichard Tran Mills 698df555b71SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 699df555b71SRichard Tran Mills if (zz == yy) { 700df555b71SRichard 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, 701df555b71SRichard Tran Mills * with alpha and beta both set to 1.0. */ 702db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z); 7039c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 704df555b71SRichard Tran Mills } else { 705df555b71SRichard 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 706df555b71SRichard Tran Mills * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */ 707db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z); 7089c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 709969800c5SRichard Tran Mills for (i=0; i<n; i++) { 710df555b71SRichard Tran Mills z[i] += y[i]; 711df555b71SRichard Tran Mills } 712df555b71SRichard Tran Mills } 713df555b71SRichard Tran Mills 714df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 715df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 716df555b71SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 717df555b71SRichard Tran Mills PetscFunctionReturn(0); 718df555b71SRichard Tran Mills } 719d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 720df555b71SRichard Tran Mills 7218a369200SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE) 722e8be1fc7SRichard Tran Mills PetscErrorCode MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,Mat C) 723e8be1fc7SRichard Tran Mills { 724e8be1fc7SRichard Tran Mills Mat_SeqAIJMKL *a, *b, *c; 725e8be1fc7SRichard Tran Mills sparse_matrix_t csrA, csrB, csrC; 726e8be1fc7SRichard Tran Mills PetscErrorCode ierr; 727e8be1fc7SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 728e8be1fc7SRichard Tran Mills struct matrix_descr descr_type_gen; 729e8be1fc7SRichard Tran Mills PetscObjectState state; 730e8be1fc7SRichard Tran Mills 731e8be1fc7SRichard Tran Mills PetscFunctionBegin; 732e8be1fc7SRichard Tran Mills a = (Mat_SeqAIJMKL*)A->spptr; 733e8be1fc7SRichard Tran Mills b = (Mat_SeqAIJMKL*)B->spptr; 734e8be1fc7SRichard Tran Mills c = (Mat_SeqAIJMKL*)C->spptr; 735e8be1fc7SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 736e8be1fc7SRichard Tran Mills if (!a->sparse_optimized || a->state != state) { 737e8be1fc7SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 738e8be1fc7SRichard Tran Mills } 739e8be1fc7SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr); 740e8be1fc7SRichard Tran Mills if (!b->sparse_optimized || b->state != state) { 741e8be1fc7SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(B); 742e8be1fc7SRichard Tran Mills } 743e8be1fc7SRichard Tran Mills csrA = a->csrA; 744e8be1fc7SRichard Tran Mills csrB = b->csrA; 745e8be1fc7SRichard Tran Mills csrC = c->csrA; 746e8be1fc7SRichard Tran Mills descr_type_gen.type = SPARSE_MATRIX_TYPE_GENERAL; 747e8be1fc7SRichard Tran Mills 748e8be1fc7SRichard Tran Mills stat = mkl_sparse_sp2m(SPARSE_OPERATION_NON_TRANSPOSE,descr_type_gen,csrA, 749e8be1fc7SRichard Tran Mills SPARSE_OPERATION_NON_TRANSPOSE,descr_type_gen,csrB, 750e8be1fc7SRichard Tran Mills SPARSE_STAGE_FINALIZE_MULT,&csrC); 751e8be1fc7SRichard Tran Mills 752e8be1fc7SRichard 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"); 753e8be1fc7SRichard Tran Mills 754e8be1fc7SRichard Tran Mills /* Have to update the PETSc AIJ representation for matrix C from contents of MKL handle. */ 7554f53af40SRichard Tran Mills ierr = MatSeqAIJMKL_update_from_mkl_handle(C);CHKERRQ(ierr); 756e8be1fc7SRichard Tran Mills 757e8be1fc7SRichard Tran Mills PetscFunctionReturn(0); 758e8be1fc7SRichard Tran Mills } 7598a369200SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE */ 760e8be1fc7SRichard Tran Mills 7618a369200SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE) 7624f53af40SRichard Tran Mills PetscErrorCode MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat P,Mat C) 7634f53af40SRichard Tran Mills { 7644f53af40SRichard Tran Mills Mat_SeqAIJMKL *a, *p, *c; 7654f53af40SRichard Tran Mills sparse_matrix_t csrA, csrP, csrC; 7664f53af40SRichard Tran Mills PetscBool set, flag; 7674f53af40SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 768b9e1dd46SRichard Tran Mills struct matrix_descr descr_type_sym; 7694f53af40SRichard Tran Mills PetscObjectState state; 7704f53af40SRichard Tran Mills PetscErrorCode ierr; 7714f53af40SRichard Tran Mills 7724f53af40SRichard Tran Mills PetscFunctionBegin; 7734f53af40SRichard Tran Mills ierr = MatIsSymmetricKnown(A,&set,&flag); 7744f53af40SRichard Tran Mills if (!set || (set && !flag)) { 7754f53af40SRichard Tran Mills ierr = MatPtAPNumeric_SeqAIJ_SeqAIJ(A,P,C);CHKERRQ(ierr); 7764f53af40SRichard Tran Mills PetscFunctionReturn(0); 7774f53af40SRichard Tran Mills } 7784f53af40SRichard Tran Mills 7794f53af40SRichard Tran Mills a = (Mat_SeqAIJMKL*)A->spptr; 7804f53af40SRichard Tran Mills p = (Mat_SeqAIJMKL*)P->spptr; 7814f53af40SRichard Tran Mills c = (Mat_SeqAIJMKL*)C->spptr; 7824f53af40SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 7834f53af40SRichard Tran Mills if (!a->sparse_optimized || a->state != state) { 7844f53af40SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 7854f53af40SRichard Tran Mills } 7864f53af40SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)P,&state);CHKERRQ(ierr); 7874f53af40SRichard Tran Mills if (!p->sparse_optimized || p->state != state) { 7884f53af40SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(P); 7894f53af40SRichard Tran Mills } 7904f53af40SRichard Tran Mills csrA = a->csrA; 7914f53af40SRichard Tran Mills csrP = p->csrA; 7924f53af40SRichard Tran Mills csrC = c->csrA; 793b9e1dd46SRichard Tran Mills descr_type_sym.type = SPARSE_MATRIX_TYPE_SYMMETRIC; 794b9e1dd46SRichard Tran Mills descr_type_sym.mode = SPARSE_FILL_MODE_LOWER; 795b9e1dd46SRichard Tran Mills descr_type_sym.diag = SPARSE_DIAG_NON_UNIT; 7964f53af40SRichard Tran Mills 797f8990b4aSRichard Tran Mills /* Note that the call below won't work for complex matrices. (We protect this when pointers are assigned in MatConvert.) */ 798b9e1dd46SRichard Tran Mills stat = mkl_sparse_sypr(SPARSE_OPERATION_TRANSPOSE,csrP,csrA,descr_type_sym,&csrC,SPARSE_STAGE_FINALIZE_MULT); 7994f53af40SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to finalize mkl_sparse_sypr"); 8004f53af40SRichard Tran Mills 8014f53af40SRichard Tran Mills /* Have to update the PETSc AIJ representation for matrix C from contents of MKL handle. */ 8024f53af40SRichard Tran Mills ierr = MatSeqAIJMKL_update_from_mkl_handle(C);CHKERRQ(ierr); 8034f53af40SRichard Tran Mills 8044f53af40SRichard Tran Mills PetscFunctionReturn(0); 8054f53af40SRichard Tran Mills } 8064f53af40SRichard Tran Mills #endif 8074f53af40SRichard Tran Mills 8084a2a386eSRichard Tran Mills /* MatConvert_SeqAIJ_SeqAIJMKL converts a SeqAIJ matrix into a 809510b72f4SRichard Tran Mills * SeqAIJMKL matrix. This routine is called by the MatCreate_SeqAIJMKL() 8104a2a386eSRichard Tran Mills * routine, but can also be used to convert an assembled SeqAIJ matrix 8114a2a386eSRichard Tran Mills * into a SeqAIJMKL one. */ 8124a2a386eSRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJMKL(Mat A,MatType type,MatReuse reuse,Mat *newmat) 8134a2a386eSRichard Tran Mills { 8144a2a386eSRichard Tran Mills PetscErrorCode ierr; 8154a2a386eSRichard Tran Mills Mat B = *newmat; 8164a2a386eSRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 817c9d46305SRichard Tran Mills PetscBool set; 818e9c94282SRichard Tran Mills PetscBool sametype; 8194a2a386eSRichard Tran Mills 8204a2a386eSRichard Tran Mills PetscFunctionBegin; 8214a2a386eSRichard Tran Mills if (reuse == MAT_INITIAL_MATRIX) { 8224a2a386eSRichard Tran Mills ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr); 8234a2a386eSRichard Tran Mills } 8244a2a386eSRichard Tran Mills 825e9c94282SRichard Tran Mills ierr = PetscObjectTypeCompare((PetscObject)A,type,&sametype);CHKERRQ(ierr); 826e9c94282SRichard Tran Mills if (sametype) PetscFunctionReturn(0); 827e9c94282SRichard Tran Mills 8284a2a386eSRichard Tran Mills ierr = PetscNewLog(B,&aijmkl);CHKERRQ(ierr); 8294a2a386eSRichard Tran Mills B->spptr = (void*) aijmkl; 8304a2a386eSRichard Tran Mills 831df555b71SRichard Tran Mills /* Set function pointers for methods that we inherit from AIJ but override. 832969800c5SRichard Tran Mills * We also parse some command line options below, since those determine some of the methods we point to. */ 8334a2a386eSRichard Tran Mills B->ops->duplicate = MatDuplicate_SeqAIJMKL; 8344a2a386eSRichard Tran Mills B->ops->assemblyend = MatAssemblyEnd_SeqAIJMKL; 8354a2a386eSRichard Tran Mills B->ops->destroy = MatDestroy_SeqAIJMKL; 836c9d46305SRichard Tran Mills 8374abfa3b3SRichard Tran Mills aijmkl->sparse_optimized = PETSC_FALSE; 838ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 839d995685eSRichard Tran Mills aijmkl->no_SpMV2 = PETSC_FALSE; /* Default to using the SpMV2 routines if our MKL supports them. */ 840a8327b06SKarl Rupp #else 841d995685eSRichard Tran Mills aijmkl->no_SpMV2 = PETSC_TRUE; 842d995685eSRichard Tran Mills #endif 8435b49642aSRichard Tran Mills aijmkl->eager_inspection = PETSC_FALSE; 8444abfa3b3SRichard Tran Mills 8454abfa3b3SRichard Tran Mills /* Parse command line options. */ 846c9d46305SRichard Tran Mills ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"AIJMKL Options","Mat");CHKERRQ(ierr); 84748292275SRichard Tran Mills ierr = PetscOptionsBool("-mat_aijmkl_no_spmv2","Disable use of inspector-executor (SpMV 2) routines","None",(PetscBool)aijmkl->no_SpMV2,(PetscBool*)&aijmkl->no_SpMV2,&set);CHKERRQ(ierr); 84848292275SRichard Tran Mills ierr = PetscOptionsBool("-mat_aijmkl_eager_inspection","Run inspection at matrix assembly time, instead of waiting until needed by an operation","None",(PetscBool)aijmkl->eager_inspection,(PetscBool*)&aijmkl->eager_inspection,&set);CHKERRQ(ierr); 849c9d46305SRichard Tran Mills ierr = PetscOptionsEnd();CHKERRQ(ierr); 850ffcab697SRichard Tran Mills #if !defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 851d995685eSRichard Tran Mills if(!aijmkl->no_SpMV2) { 852d995685eSRichard 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"); 853d995685eSRichard Tran Mills aijmkl->no_SpMV2 = PETSC_TRUE; 854d995685eSRichard Tran Mills } 855d995685eSRichard Tran Mills #endif 856c9d46305SRichard Tran Mills 857ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 858df555b71SRichard Tran Mills B->ops->mult = MatMult_SeqAIJMKL_SpMV2; 859969800c5SRichard Tran Mills B->ops->multtranspose = MatMultTranspose_SeqAIJMKL_SpMV2; 860df555b71SRichard Tran Mills B->ops->multadd = MatMultAdd_SeqAIJMKL_SpMV2; 861969800c5SRichard Tran Mills B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL_SpMV2; 8628a369200SRichard Tran Mills # if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE) 863e8be1fc7SRichard Tran Mills B->ops->matmultnumeric = MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2; 864ffcab697SRichard Tran Mills # if !defined(PETSC_USE_COMPLEX) 8654f53af40SRichard Tran Mills B->ops->ptapnumeric = MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2; 8664f53af40SRichard Tran Mills # endif 867e8be1fc7SRichard Tran Mills # endif 8681950a7e7SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 8691950a7e7SRichard Tran Mills 870213898a2SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 871213898a2SRichard Tran Mills /* In MKL version 18, update 2, the old sparse BLAS interfaces were marked as deprecated. If "no_SpMV2" has been specified by the 872213898a2SRichard Tran Mills * user and the old SpBLAS interfaces are deprecated in our MKL version, we use the new _SpMV2 routines (set above), but do not 873213898a2SRichard Tran Mills * call mkl_sparse_optimize(), which results in the old numerical kernels (without the inspector-executor model) being used. For 874213898a2SRichard Tran Mills * versions in which the older interface has not been deprecated, we use the old interface. */ 8751950a7e7SRichard Tran Mills if (aijmkl->no_SpMV2) { 8764a2a386eSRichard Tran Mills B->ops->mult = MatMult_SeqAIJMKL; 877969800c5SRichard Tran Mills B->ops->multtranspose = MatMultTranspose_SeqAIJMKL; 8784a2a386eSRichard Tran Mills B->ops->multadd = MatMultAdd_SeqAIJMKL; 879969800c5SRichard Tran Mills B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL; 880c9d46305SRichard Tran Mills } 8811950a7e7SRichard Tran Mills #endif 8824a2a386eSRichard Tran Mills 8834a2a386eSRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",MatConvert_SeqAIJMKL_SeqAIJ);CHKERRQ(ierr); 8844a2a386eSRichard Tran Mills 8854a2a386eSRichard Tran Mills ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJMKL);CHKERRQ(ierr); 8864a2a386eSRichard Tran Mills *newmat = B; 8874a2a386eSRichard Tran Mills PetscFunctionReturn(0); 8884a2a386eSRichard Tran Mills } 8894a2a386eSRichard Tran Mills 8904a2a386eSRichard Tran Mills /*@C 8914a2a386eSRichard Tran Mills MatCreateSeqAIJMKL - Creates a sparse matrix of type SEQAIJMKL. 8924a2a386eSRichard Tran Mills This type inherits from AIJ and is largely identical, but uses sparse BLAS 8934a2a386eSRichard Tran Mills routines from Intel MKL whenever possible. 89490147e49SRichard Tran Mills If the installed version of MKL supports the "SpMV2" sparse 89590147e49SRichard Tran Mills inspector-executor routines, then those are used by default. 896597ee276SRichard Tran Mills MatMult, MatMultAdd, MatMultTranspose, MatMultTransposeAdd, MatMatMult, MatTransposeMatMult, and MatPtAP (for 897597ee276SRichard Tran Mills symmetric A) operations are currently supported. 898597ee276SRichard Tran Mills Note that MKL version 18, update 2 or later is required for MatPtAP/MatPtAPNumeric and MatMatMultNumeric. 89990147e49SRichard Tran Mills 900d083f849SBarry Smith Collective 9014a2a386eSRichard Tran Mills 9024a2a386eSRichard Tran Mills Input Parameters: 9034a2a386eSRichard Tran Mills + comm - MPI communicator, set to PETSC_COMM_SELF 9044a2a386eSRichard Tran Mills . m - number of rows 9054a2a386eSRichard Tran Mills . n - number of columns 9064a2a386eSRichard Tran Mills . nz - number of nonzeros per row (same for all rows) 9074a2a386eSRichard Tran Mills - nnz - array containing the number of nonzeros in the various rows 9084a2a386eSRichard Tran Mills (possibly different for each row) or NULL 9094a2a386eSRichard Tran Mills 9104a2a386eSRichard Tran Mills Output Parameter: 9114a2a386eSRichard Tran Mills . A - the matrix 9124a2a386eSRichard Tran Mills 91390147e49SRichard Tran Mills Options Database Keys: 91466b7eeb6SRichard Tran Mills + -mat_aijmkl_no_spmv2 - disable use of the SpMV2 inspector-executor routines 91566b7eeb6SRichard 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 91690147e49SRichard Tran Mills 9174a2a386eSRichard Tran Mills Notes: 9184a2a386eSRichard Tran Mills If nnz is given then nz is ignored 9194a2a386eSRichard Tran Mills 9204a2a386eSRichard Tran Mills Level: intermediate 9214a2a386eSRichard Tran Mills 9224a2a386eSRichard Tran Mills .seealso: MatCreate(), MatCreateMPIAIJMKL(), MatSetValues() 9234a2a386eSRichard Tran Mills @*/ 9244a2a386eSRichard Tran Mills PetscErrorCode MatCreateSeqAIJMKL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 9254a2a386eSRichard Tran Mills { 9264a2a386eSRichard Tran Mills PetscErrorCode ierr; 9274a2a386eSRichard Tran Mills 9284a2a386eSRichard Tran Mills PetscFunctionBegin; 9294a2a386eSRichard Tran Mills ierr = MatCreate(comm,A);CHKERRQ(ierr); 9304a2a386eSRichard Tran Mills ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 9314a2a386eSRichard Tran Mills ierr = MatSetType(*A,MATSEQAIJMKL);CHKERRQ(ierr); 9324a2a386eSRichard Tran Mills ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr); 9334a2a386eSRichard Tran Mills PetscFunctionReturn(0); 9344a2a386eSRichard Tran Mills } 9354a2a386eSRichard Tran Mills 9364a2a386eSRichard Tran Mills PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJMKL(Mat A) 9374a2a386eSRichard Tran Mills { 9384a2a386eSRichard Tran Mills PetscErrorCode ierr; 9394a2a386eSRichard Tran Mills 9404a2a386eSRichard Tran Mills PetscFunctionBegin; 9414a2a386eSRichard Tran Mills ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr); 9424a2a386eSRichard Tran Mills ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr); 9434a2a386eSRichard Tran Mills PetscFunctionReturn(0); 9444a2a386eSRichard Tran Mills } 945