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); 54e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr); 55e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaijmkl_C",NULL);CHKERRQ(ierr); 56*4222ddf1SHong Zhang 57ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 584a940b00SSatish Balay if (!aijmkl->no_SpMV2) { 598a369200SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE) 60e8be1fc7SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqaijmkl_seqaijmkl_C",NULL);CHKERRQ(ierr); 61e8be1fc7SRichard Tran Mills #endif 6245fbe478SRichard Tran Mills } 63e9c94282SRichard Tran Mills 644abfa3b3SRichard Tran Mills /* Free everything in the Mat_SeqAIJMKL data structure. Currently, this 65e9c94282SRichard Tran Mills * simply involves destroying the MKL sparse matrix handle and then freeing 66e9c94282SRichard Tran Mills * the spptr pointer. */ 67a8327b06SKarl Rupp if (reuse == MAT_INITIAL_MATRIX) aijmkl = (Mat_SeqAIJMKL*)B->spptr; 68a8327b06SKarl Rupp 694abfa3b3SRichard Tran Mills if (aijmkl->sparse_optimized) { 700632b357SRichard Tran Mills sparse_status_t stat; 714abfa3b3SRichard Tran Mills stat = mkl_sparse_destroy(aijmkl->csrA); 729c46acdfSRichard 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"); 734abfa3b3SRichard Tran Mills } 744abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 75e9c94282SRichard Tran Mills ierr = PetscFree(B->spptr);CHKERRQ(ierr); 764a2a386eSRichard Tran Mills 774a2a386eSRichard Tran Mills /* Change the type of B to MATSEQAIJ. */ 784a2a386eSRichard Tran Mills ierr = PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ);CHKERRQ(ierr); 794a2a386eSRichard Tran Mills 804a2a386eSRichard Tran Mills *newmat = B; 814a2a386eSRichard Tran Mills PetscFunctionReturn(0); 824a2a386eSRichard Tran Mills } 834a2a386eSRichard Tran Mills 844a2a386eSRichard Tran Mills PetscErrorCode MatDestroy_SeqAIJMKL(Mat A) 854a2a386eSRichard Tran Mills { 864a2a386eSRichard Tran Mills PetscErrorCode ierr; 874a2a386eSRichard Tran Mills Mat_SeqAIJMKL *aijmkl = (Mat_SeqAIJMKL*) A->spptr; 884a2a386eSRichard Tran Mills 894a2a386eSRichard Tran Mills PetscFunctionBegin; 90e9c94282SRichard Tran Mills 91e9c94282SRichard Tran Mills /* If MatHeaderMerge() was used, then this SeqAIJMKL matrix will not have an 92e9c94282SRichard Tran Mills * spptr pointer. */ 93e9c94282SRichard Tran Mills if (aijmkl) { 944a2a386eSRichard Tran Mills /* Clean up everything in the Mat_SeqAIJMKL data structure, then free A->spptr. */ 95ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 964abfa3b3SRichard Tran Mills if (aijmkl->sparse_optimized) { 974abfa3b3SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 984abfa3b3SRichard Tran Mills stat = mkl_sparse_destroy(aijmkl->csrA); 999c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy"); 1004abfa3b3SRichard Tran Mills } 1014abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 1024a2a386eSRichard Tran Mills ierr = PetscFree(A->spptr);CHKERRQ(ierr); 103e9c94282SRichard Tran Mills } 1044a2a386eSRichard Tran Mills 1054a2a386eSRichard Tran Mills /* Change the type of A back to SEQAIJ and use MatDestroy_SeqAIJ() 1064a2a386eSRichard Tran Mills * to destroy everything that remains. */ 1074a2a386eSRichard Tran Mills ierr = PetscObjectChangeTypeName((PetscObject)A, MATSEQAIJ);CHKERRQ(ierr); 1084a2a386eSRichard Tran Mills /* Note that I don't call MatSetType(). I believe this is because that 1094a2a386eSRichard Tran Mills * is only to be called when *building* a matrix. I could be wrong, but 1104a2a386eSRichard Tran Mills * that is how things work for the SuperLU matrix class. */ 1114a2a386eSRichard Tran Mills ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr); 1124a2a386eSRichard Tran Mills PetscFunctionReturn(0); 1134a2a386eSRichard Tran Mills } 1144a2a386eSRichard Tran Mills 1155b49642aSRichard Tran Mills /* MatSeqAIJKL_create_mkl_handle(), if called with an AIJMKL matrix that has not had mkl_sparse_optimize() called for it, 1165b49642aSRichard Tran Mills * creates an MKL sparse matrix handle from the AIJ arrays and calls mkl_sparse_optimize(). 1175b49642aSRichard Tran Mills * If called with an AIJMKL matrix for which aijmkl->sparse_optimized == PETSC_TRUE, then it destroys the old matrix 1185b49642aSRichard Tran Mills * handle, creates a new one, and then calls mkl_sparse_optimize(). 1195b49642aSRichard Tran Mills * Although in normal MKL usage it is possible to have a valid matrix handle on which mkl_sparse_optimize() has not been 1205b49642aSRichard Tran Mills * called, for AIJMKL the handle creation and optimization step always occur together, so we don't handle the case of 1215b49642aSRichard Tran Mills * an unoptimized matrix handle here. */ 1226e369cd5SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_mkl_handle(Mat A) 1234a2a386eSRichard Tran Mills { 124ffcab697SRichard Tran Mills #if !defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 1256e369cd5SRichard Tran Mills /* If the MKL library does not have mkl_sparse_optimize(), then this routine 1266e369cd5SRichard Tran Mills * does nothing. We make it callable anyway in this case because it cuts 1276e369cd5SRichard Tran Mills * down on littering the code with #ifdefs. */ 12845fbe478SRichard Tran Mills PetscFunctionBegin; 1296e369cd5SRichard Tran Mills PetscFunctionReturn(0); 1306e369cd5SRichard Tran Mills #else 131a8327b06SKarl Rupp Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 132a8327b06SKarl Rupp Mat_SeqAIJMKL *aijmkl = (Mat_SeqAIJMKL*)A->spptr; 133a8327b06SKarl Rupp PetscInt m,n; 134a8327b06SKarl Rupp MatScalar *aa; 135a8327b06SKarl Rupp PetscInt *aj,*ai; 1366e369cd5SRichard Tran Mills sparse_status_t stat; 137551aa5c8SRichard Tran Mills PetscErrorCode ierr; 1384a2a386eSRichard Tran Mills 139a8327b06SKarl Rupp PetscFunctionBegin; 140e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 141e626a176SRichard Tran Mills /* For MKL versions that still support the old, non-inspector-executor interfaces versions, we simply exit here if the no_SpMV2 142e626a176SRichard Tran Mills * option has been specified. For versions that have deprecated the old interfaces (version 18, update 2 and later), we must 143e626a176SRichard Tran Mills * use the new inspector-executor interfaces, but we can still use the old, non-inspector-executor code by not calling 144e626a176SRichard Tran Mills * mkl_sparse_optimize() later. */ 1456e369cd5SRichard Tran Mills if (aijmkl->no_SpMV2) PetscFunctionReturn(0); 1464d51fa23SRichard Tran Mills #endif 1476e369cd5SRichard Tran Mills 1480632b357SRichard Tran Mills if (aijmkl->sparse_optimized) { 1490632b357SRichard Tran Mills /* Matrix has been previously assembled and optimized. Must destroy old 1500632b357SRichard Tran Mills * matrix handle before running the optimization step again. */ 1510632b357SRichard Tran Mills stat = mkl_sparse_destroy(aijmkl->csrA); 1529c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy"); 1530632b357SRichard Tran Mills } 1548d3fe1b0SRichard Tran Mills aijmkl->sparse_optimized = PETSC_FALSE; 1556e369cd5SRichard Tran Mills 156c9d46305SRichard Tran Mills /* Now perform the SpMV2 setup and matrix optimization. */ 157df555b71SRichard Tran Mills aijmkl->descr.type = SPARSE_MATRIX_TYPE_GENERAL; 158df555b71SRichard Tran Mills aijmkl->descr.mode = SPARSE_FILL_MODE_LOWER; 159df555b71SRichard Tran Mills aijmkl->descr.diag = SPARSE_DIAG_NON_UNIT; 16058678438SRichard Tran Mills m = A->rmap->n; 16158678438SRichard Tran Mills n = A->cmap->n; 162df555b71SRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 163df555b71SRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 164df555b71SRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 16580095d54SIrina Sokolova if ((a->nz!=0) & !(A->structure_only)) { 1668d3fe1b0SRichard Tran Mills /* Create a new, optimized sparse matrix handle only if the matrix has nonzero entries. 1678d3fe1b0SRichard Tran Mills * The MKL sparse-inspector executor routines don't like being passed an empty matrix. */ 16858678438SRichard Tran Mills stat = mkl_sparse_x_create_csr(&aijmkl->csrA,SPARSE_INDEX_BASE_ZERO,m,n,ai,ai+1,aj,aa); 169e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to create matrix handle"); 170df555b71SRichard Tran Mills stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000); 171e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set mv_hint"); 172df555b71SRichard Tran Mills stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE); 173e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set memory_hint"); 1741950a7e7SRichard Tran Mills if (!aijmkl->no_SpMV2) { 175df555b71SRichard Tran Mills stat = mkl_sparse_optimize(aijmkl->csrA); 176e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_optimize"); 1771950a7e7SRichard Tran Mills } 1784abfa3b3SRichard Tran Mills aijmkl->sparse_optimized = PETSC_TRUE; 179e995cf24SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&(aijmkl->state));CHKERRQ(ierr); 180c9d46305SRichard Tran Mills } 1816e369cd5SRichard Tran Mills 1826e369cd5SRichard Tran Mills PetscFunctionReturn(0); 183d995685eSRichard Tran Mills #endif 1846e369cd5SRichard Tran Mills } 1856e369cd5SRichard Tran Mills 18619afcda9SRichard Tran Mills /* MatSeqAIJMKL_create_from_mkl_handle() creates a sequential AIJMKL matrix from an MKL sparse matrix handle. 18719afcda9SRichard Tran Mills * We need this to implement MatMatMult() using the MKL inspector-executor routines, which return an (unoptimized) 1886c87cf42SRichard Tran Mills * matrix handle. 189aab60f1bSRichard Tran Mills * Note: This routine simply destroys and replaces the original matrix if MAT_REUSE_MATRIX has been specified, as 190aab60f1bSRichard Tran Mills * there is no good alternative. */ 191ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 1926c87cf42SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_from_mkl_handle(MPI_Comm comm,sparse_matrix_t csrA,MatReuse reuse,Mat *mat) 19319afcda9SRichard Tran Mills { 19419afcda9SRichard Tran Mills PetscErrorCode ierr; 19519afcda9SRichard Tran Mills sparse_status_t stat; 19619afcda9SRichard Tran Mills sparse_index_base_t indexing; 19719afcda9SRichard Tran Mills PetscInt nrows, ncols; 19845fbe478SRichard Tran Mills PetscInt *aj,*ai,*dummy; 19919afcda9SRichard Tran Mills MatScalar *aa; 20019afcda9SRichard Tran Mills Mat A; 20119afcda9SRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 20219afcda9SRichard Tran Mills 20345fbe478SRichard Tran Mills /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */ 20445fbe478SRichard Tran Mills stat = mkl_sparse_x_export_csr(csrA,&indexing,&nrows,&ncols,&ai,&dummy,&aj,&aa); 2059c46acdfSRichard 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()"); 2066c87cf42SRichard Tran Mills 207aab60f1bSRichard Tran Mills if (reuse == MAT_REUSE_MATRIX) { 208aab60f1bSRichard Tran Mills ierr = MatDestroy(mat);CHKERRQ(ierr); 209aab60f1bSRichard Tran Mills } 21019afcda9SRichard Tran Mills ierr = MatCreate(comm,&A);CHKERRQ(ierr); 21119afcda9SRichard Tran Mills ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr); 21245fbe478SRichard Tran Mills ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,nrows,ncols);CHKERRQ(ierr); 213aab60f1bSRichard Tran Mills /* We use MatSeqAIJSetPreallocationCSR() instead of MatCreateSeqAIJWithArrays() because we must copy the arrays exported 214aab60f1bSRichard Tran Mills * from MKL; MKL developers tell us that modifying the arrays may cause unexpected results when using the MKL handle, and 215aab60f1bSRichard Tran Mills * they will be destroyed when the MKL handle is destroyed. 216aab60f1bSRichard Tran Mills * (In the interest of reducing memory consumption in future, can we figure out good ways to deal with this?) */ 21719afcda9SRichard Tran Mills ierr = MatSeqAIJSetPreallocationCSR(A,ai,aj,aa);CHKERRQ(ierr); 21819afcda9SRichard Tran Mills 21919afcda9SRichard Tran Mills /* We now have an assembled sequential AIJ matrix created from copies of the exported arrays from the MKL matrix handle. 22019afcda9SRichard Tran Mills * Now turn it into a MATSEQAIJMKL. */ 22119afcda9SRichard Tran Mills ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr); 2226c87cf42SRichard Tran Mills 22319afcda9SRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 22419afcda9SRichard Tran Mills aijmkl->csrA = csrA; 2256c87cf42SRichard Tran Mills 22619afcda9SRichard Tran Mills /* The below code duplicates much of what is in MatSeqAIJKL_create_mkl_handle(). I dislike this code duplication, but 22719afcda9SRichard Tran Mills * MatSeqAIJMKL_create_mkl_handle() cannot be used because we don't need to create a handle -- we've already got one, 22819afcda9SRichard Tran Mills * and just need to be able to run the MKL optimization step. */ 229f3fd1758SRichard Tran Mills aijmkl->descr.type = SPARSE_MATRIX_TYPE_GENERAL; 230f3fd1758SRichard Tran Mills aijmkl->descr.mode = SPARSE_FILL_MODE_LOWER; 231f3fd1758SRichard Tran Mills aijmkl->descr.diag = SPARSE_DIAG_NON_UNIT; 23219afcda9SRichard Tran Mills stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000); 23351539a68SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set mv_hint"); 23419afcda9SRichard Tran Mills stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE); 23551539a68SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set memory_hint"); 2361950a7e7SRichard Tran Mills if (!aijmkl->no_SpMV2) { 23719afcda9SRichard Tran Mills stat = mkl_sparse_optimize(aijmkl->csrA); 23851539a68SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_optimize"); 2391950a7e7SRichard Tran Mills } 24019afcda9SRichard Tran Mills aijmkl->sparse_optimized = PETSC_TRUE; 241e995cf24SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&(aijmkl->state));CHKERRQ(ierr); 24219afcda9SRichard Tran Mills 24319afcda9SRichard Tran Mills *mat = A; 24419afcda9SRichard Tran Mills PetscFunctionReturn(0); 24519afcda9SRichard Tran Mills } 24619afcda9SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 24719afcda9SRichard Tran Mills 248e8be1fc7SRichard Tran Mills /* MatSeqAIJMKL_update_from_mkl_handle() updates the matrix values array from the contents of the associated MKL sparse matrix handle. 249e8be1fc7SRichard 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 250e8be1fc7SRichard Tran Mills * MatMatMultNumeric(). */ 251ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 252e8be1fc7SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_update_from_mkl_handle(Mat A) 253e8be1fc7SRichard Tran Mills { 254e8be1fc7SRichard Tran Mills PetscInt i; 255e8be1fc7SRichard Tran Mills PetscInt nrows,ncols; 256e8be1fc7SRichard Tran Mills PetscInt nz; 257e8be1fc7SRichard Tran Mills PetscInt *ai,*aj,*dummy; 258e8be1fc7SRichard Tran Mills PetscScalar *aa; 259e8be1fc7SRichard Tran Mills PetscErrorCode ierr; 260e8be1fc7SRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 261e8be1fc7SRichard Tran Mills sparse_status_t stat; 262e8be1fc7SRichard Tran Mills sparse_index_base_t indexing; 263e8be1fc7SRichard Tran Mills 264e8be1fc7SRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 265e8be1fc7SRichard Tran Mills 266e8be1fc7SRichard Tran Mills /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */ 267e8be1fc7SRichard Tran Mills stat = mkl_sparse_x_export_csr(aijmkl->csrA,&indexing,&nrows,&ncols,&ai,&dummy,&aj,&aa); 268e8be1fc7SRichard 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()"); 269e8be1fc7SRichard Tran Mills 270e8be1fc7SRichard 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 271e8be1fc7SRichard Tran Mills * representations differ in small ways (e.g., more explicit nonzeros per row due to preallocation). */ 272e8be1fc7SRichard Tran Mills for (i=0; i<nrows; i++) { 273e8be1fc7SRichard Tran Mills nz = ai[i+1] - ai[i]; 274e8be1fc7SRichard Tran Mills ierr = MatSetValues_SeqAIJ(A, 1, &i, nz, aj+ai[i], aa+ai[i], INSERT_VALUES);CHKERRQ(ierr); 275e8be1fc7SRichard Tran Mills } 276e8be1fc7SRichard Tran Mills 277e8be1fc7SRichard Tran Mills ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 278e8be1fc7SRichard Tran Mills ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 279e8be1fc7SRichard Tran Mills 280e995cf24SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&(aijmkl->state));CHKERRQ(ierr); 281e995cf24SRichard Tran Mills /* We mark our matrix as having a valid, optimized MKL handle. 282e995cf24SRichard Tran Mills * TODO: It is valid, but I am not sure if it is optimized. Need to ask MKL developers. */ 283e995cf24SRichard Tran Mills aijmkl->sparse_optimized = PETSC_TRUE; 284e995cf24SRichard Tran Mills 285e8be1fc7SRichard Tran Mills PetscFunctionReturn(0); 286e8be1fc7SRichard Tran Mills } 287e8be1fc7SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 288e8be1fc7SRichard Tran Mills 2896e369cd5SRichard Tran Mills PetscErrorCode MatDuplicate_SeqAIJMKL(Mat A, MatDuplicateOption op, Mat *M) 2906e369cd5SRichard Tran Mills { 2916e369cd5SRichard Tran Mills PetscErrorCode ierr; 2926e369cd5SRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 2936e369cd5SRichard Tran Mills Mat_SeqAIJMKL *aijmkl_dest; 2946e369cd5SRichard Tran Mills 2956e369cd5SRichard Tran Mills PetscFunctionBegin; 2966e369cd5SRichard Tran Mills ierr = MatDuplicate_SeqAIJ(A,op,M);CHKERRQ(ierr); 2976e369cd5SRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 2986e369cd5SRichard Tran Mills aijmkl_dest = (Mat_SeqAIJMKL*) (*M)->spptr; 299580bdb30SBarry Smith ierr = PetscArraycpy(aijmkl_dest,aijmkl,1);CHKERRQ(ierr); 3006e369cd5SRichard Tran Mills aijmkl_dest->sparse_optimized = PETSC_FALSE; 3015b49642aSRichard Tran Mills if (aijmkl->eager_inspection) { 3026e369cd5SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr); 3035b49642aSRichard Tran Mills } 3046e369cd5SRichard Tran Mills PetscFunctionReturn(0); 3056e369cd5SRichard Tran Mills } 3066e369cd5SRichard Tran Mills 3076e369cd5SRichard Tran Mills PetscErrorCode MatAssemblyEnd_SeqAIJMKL(Mat A, MatAssemblyType mode) 3086e369cd5SRichard Tran Mills { 3096e369cd5SRichard Tran Mills PetscErrorCode ierr; 3106e369cd5SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3115b49642aSRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 3126e369cd5SRichard Tran Mills 3136e369cd5SRichard Tran Mills PetscFunctionBegin; 3146e369cd5SRichard Tran Mills if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 3156e369cd5SRichard Tran Mills 3166e369cd5SRichard Tran Mills /* Since a MATSEQAIJMKL matrix is really just a MATSEQAIJ with some 3176e369cd5SRichard Tran Mills * extra information and some different methods, call the AssemblyEnd 3186e369cd5SRichard Tran Mills * routine for a MATSEQAIJ. 3196e369cd5SRichard Tran Mills * I'm not sure if this is the best way to do this, but it avoids 320d96e85feSRichard Tran Mills * a lot of code duplication. */ 3216e369cd5SRichard Tran Mills a->inode.use = PETSC_FALSE; /* Must disable: otherwise the MKL routines won't get used. */ 3226e369cd5SRichard Tran Mills ierr = MatAssemblyEnd_SeqAIJ(A, mode);CHKERRQ(ierr); 3236e369cd5SRichard Tran Mills 3245b49642aSRichard Tran Mills /* If the user has requested "eager" inspection, create the optimized MKL sparse handle (if needed; the function checks). 3255b49642aSRichard Tran Mills * (The default is to do "lazy" inspection, deferring this until something like MatMult() is called.) */ 3265b49642aSRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 3275b49642aSRichard Tran Mills if (aijmkl->eager_inspection) { 3286e369cd5SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr); 3295b49642aSRichard Tran Mills } 330df555b71SRichard Tran Mills 3314a2a386eSRichard Tran Mills PetscFunctionReturn(0); 3324a2a386eSRichard Tran Mills } 3334a2a386eSRichard Tran Mills 334e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 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 } 3701950a7e7SRichard Tran Mills #endif 371ff03dc53SRichard Tran Mills 372ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 373df555b71SRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy) 374df555b71SRichard Tran Mills { 375df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 376df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 377df555b71SRichard Tran Mills const PetscScalar *x; 378df555b71SRichard Tran Mills PetscScalar *y; 379df555b71SRichard Tran Mills PetscErrorCode ierr; 380df555b71SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 381551aa5c8SRichard Tran Mills PetscObjectState state; 382df555b71SRichard Tran Mills 383df555b71SRichard Tran Mills PetscFunctionBegin; 384df555b71SRichard Tran Mills 38538987b35SRichard Tran Mills /* If there are no nonzero entries, zero yy and return immediately. */ 38638987b35SRichard Tran Mills if(!a->nz) { 38738987b35SRichard Tran Mills PetscInt i; 38838987b35SRichard Tran Mills PetscInt m=A->rmap->n; 38938987b35SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 39038987b35SRichard Tran Mills for (i=0; i<m; i++) { 39138987b35SRichard Tran Mills y[i] = 0.0; 39238987b35SRichard Tran Mills } 39338987b35SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 39438987b35SRichard Tran Mills PetscFunctionReturn(0); 39538987b35SRichard Tran Mills } 396f36dfe3fSRichard Tran Mills 397df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 398df555b71SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 399df555b71SRichard Tran Mills 4003fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 4013fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 4023fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 403551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 404551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 4053fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 4063fa15762SRichard Tran Mills } 4073fa15762SRichard Tran Mills 408df555b71SRichard Tran Mills /* Call MKL SpMV2 executor routine to do the MatMult. */ 409df555b71SRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y); 4109c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 411df555b71SRichard Tran Mills 412df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 413df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 414df555b71SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 415df555b71SRichard Tran Mills PetscFunctionReturn(0); 416df555b71SRichard Tran Mills } 417d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 418df555b71SRichard Tran Mills 419e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 420ff03dc53SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL(Mat A,Vec xx,Vec yy) 421ff03dc53SRichard Tran Mills { 422ff03dc53SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 423ff03dc53SRichard Tran Mills const PetscScalar *x; 424ff03dc53SRichard Tran Mills PetscScalar *y; 425ff03dc53SRichard Tran Mills const MatScalar *aa; 426ff03dc53SRichard Tran Mills PetscErrorCode ierr; 427ff03dc53SRichard Tran Mills PetscInt m=A->rmap->n; 428db63039fSRichard Tran Mills PetscInt n=A->cmap->n; 429db63039fSRichard Tran Mills PetscScalar alpha = 1.0; 430db63039fSRichard Tran Mills PetscScalar beta = 0.0; 431ff03dc53SRichard Tran Mills const PetscInt *aj,*ai; 432db63039fSRichard Tran Mills char matdescra[6]; 433ff03dc53SRichard Tran Mills 434ff03dc53SRichard Tran Mills /* Variables not in MatMultTranspose_SeqAIJ. */ 435ff03dc53SRichard Tran Mills char transa = 't'; /* Used to indicate to MKL that we are computing the transpose product. */ 4364a2a386eSRichard Tran Mills 4374a2a386eSRichard Tran Mills PetscFunctionBegin; 438969800c5SRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 439969800c5SRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 4404a2a386eSRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 4414a2a386eSRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 4424a2a386eSRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 4434a2a386eSRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 4444a2a386eSRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 4454a2a386eSRichard Tran Mills 4464a2a386eSRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 447db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y); 4484a2a386eSRichard Tran Mills 4494a2a386eSRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 4504a2a386eSRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 4514a2a386eSRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 4524a2a386eSRichard Tran Mills PetscFunctionReturn(0); 4534a2a386eSRichard Tran Mills } 4541950a7e7SRichard Tran Mills #endif 4554a2a386eSRichard Tran Mills 456ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 457df555b71SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy) 458df555b71SRichard Tran Mills { 459df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 460df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 461df555b71SRichard Tran Mills const PetscScalar *x; 462df555b71SRichard Tran Mills PetscScalar *y; 463df555b71SRichard Tran Mills PetscErrorCode ierr; 4640632b357SRichard Tran Mills sparse_status_t stat; 465551aa5c8SRichard Tran Mills PetscObjectState state; 466df555b71SRichard Tran Mills 467df555b71SRichard Tran Mills PetscFunctionBegin; 468df555b71SRichard Tran Mills 46938987b35SRichard Tran Mills /* If there are no nonzero entries, zero yy and return immediately. */ 47038987b35SRichard Tran Mills if(!a->nz) { 47138987b35SRichard Tran Mills PetscInt i; 47238987b35SRichard Tran Mills PetscInt n=A->cmap->n; 47338987b35SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 47438987b35SRichard Tran Mills for (i=0; i<n; i++) { 47538987b35SRichard Tran Mills y[i] = 0.0; 47638987b35SRichard Tran Mills } 47738987b35SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 47838987b35SRichard Tran Mills PetscFunctionReturn(0); 47938987b35SRichard Tran Mills } 480f36dfe3fSRichard Tran Mills 481df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 482df555b71SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 483df555b71SRichard Tran Mills 4843fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 4853fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 4863fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 487551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 488551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 4893fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 4903fa15762SRichard Tran Mills } 4913fa15762SRichard Tran Mills 492df555b71SRichard Tran Mills /* Call MKL SpMV2 executor routine to do the MatMultTranspose. */ 493df555b71SRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y); 4949c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 495df555b71SRichard Tran Mills 496df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 497df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 498df555b71SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 499df555b71SRichard Tran Mills PetscFunctionReturn(0); 500df555b71SRichard Tran Mills } 501d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 502df555b71SRichard Tran Mills 503e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 5044a2a386eSRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz) 5054a2a386eSRichard Tran Mills { 5064a2a386eSRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 5074a2a386eSRichard Tran Mills const PetscScalar *x; 5084a2a386eSRichard Tran Mills PetscScalar *y,*z; 5094a2a386eSRichard Tran Mills const MatScalar *aa; 5104a2a386eSRichard Tran Mills PetscErrorCode ierr; 5114a2a386eSRichard Tran Mills PetscInt m=A->rmap->n; 512db63039fSRichard Tran Mills PetscInt n=A->cmap->n; 5134a2a386eSRichard Tran Mills const PetscInt *aj,*ai; 5144a2a386eSRichard Tran Mills PetscInt i; 5154a2a386eSRichard Tran Mills 516ff03dc53SRichard Tran Mills /* Variables not in MatMultAdd_SeqAIJ. */ 517ff03dc53SRichard Tran Mills char transa = 'n'; /* Used to indicate to MKL that we are not computing the transpose product. */ 518a84739b8SRichard Tran Mills PetscScalar alpha = 1.0; 519db63039fSRichard Tran Mills PetscScalar beta; 520a84739b8SRichard Tran Mills char matdescra[6]; 521ff03dc53SRichard Tran Mills 522ff03dc53SRichard Tran Mills PetscFunctionBegin; 523a84739b8SRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 524a84739b8SRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 525a84739b8SRichard Tran Mills 526ff03dc53SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 527ff03dc53SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 528ff03dc53SRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 529ff03dc53SRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 530ff03dc53SRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 531ff03dc53SRichard Tran Mills 532ff03dc53SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 533a84739b8SRichard Tran Mills if (zz == yy) { 534a84739b8SRichard 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. */ 535db63039fSRichard Tran Mills beta = 1.0; 536db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 537a84739b8SRichard Tran Mills } else { 538db63039fSRichard Tran Mills /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z. 539db63039fSRichard Tran Mills * MKL sparse BLAS does not have a MatMultAdd equivalent. */ 540db63039fSRichard Tran Mills beta = 0.0; 541db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 542ff03dc53SRichard Tran Mills for (i=0; i<m; i++) { 543ff03dc53SRichard Tran Mills z[i] += y[i]; 544ff03dc53SRichard Tran Mills } 545a84739b8SRichard Tran Mills } 546ff03dc53SRichard Tran Mills 547ff03dc53SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 548ff03dc53SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 549ff03dc53SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 550ff03dc53SRichard Tran Mills PetscFunctionReturn(0); 551ff03dc53SRichard Tran Mills } 5521950a7e7SRichard Tran Mills #endif 553ff03dc53SRichard Tran Mills 554ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 555df555b71SRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz) 556df555b71SRichard Tran Mills { 557df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 558df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 559df555b71SRichard Tran Mills const PetscScalar *x; 560df555b71SRichard Tran Mills PetscScalar *y,*z; 561df555b71SRichard Tran Mills PetscErrorCode ierr; 562df555b71SRichard Tran Mills PetscInt m=A->rmap->n; 563df555b71SRichard Tran Mills PetscInt i; 564df555b71SRichard Tran Mills 565df555b71SRichard Tran Mills /* Variables not in MatMultAdd_SeqAIJ. */ 566df555b71SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 567551aa5c8SRichard Tran Mills PetscObjectState state; 568df555b71SRichard Tran Mills 569df555b71SRichard Tran Mills PetscFunctionBegin; 570df555b71SRichard Tran Mills 57138987b35SRichard Tran Mills /* If there are no nonzero entries, set zz = yy and return immediately. */ 57238987b35SRichard Tran Mills if(!a->nz) { 57338987b35SRichard Tran Mills PetscInt i; 57438987b35SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 57538987b35SRichard Tran Mills for (i=0; i<m; i++) { 57638987b35SRichard Tran Mills z[i] = y[i]; 57738987b35SRichard Tran Mills } 57838987b35SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 57938987b35SRichard Tran Mills PetscFunctionReturn(0); 58038987b35SRichard Tran Mills } 581df555b71SRichard Tran Mills 582df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 583df555b71SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 584df555b71SRichard Tran Mills 5853fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 5863fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 5873fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 588551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 589551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 5903fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 5913fa15762SRichard Tran Mills } 5923fa15762SRichard Tran Mills 593df555b71SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 594df555b71SRichard Tran Mills if (zz == yy) { 595df555b71SRichard 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, 596df555b71SRichard Tran Mills * with alpha and beta both set to 1.0. */ 597db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.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 } else { 600df555b71SRichard 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 601df555b71SRichard Tran Mills * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */ 602db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z); 6039c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 604df555b71SRichard Tran Mills for (i=0; i<m; i++) { 605df555b71SRichard Tran Mills z[i] += y[i]; 606df555b71SRichard Tran Mills } 607df555b71SRichard Tran Mills } 608df555b71SRichard Tran Mills 609df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 610df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 611df555b71SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 612df555b71SRichard Tran Mills PetscFunctionReturn(0); 613df555b71SRichard Tran Mills } 614d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 615df555b71SRichard Tran Mills 616e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 617ff03dc53SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz) 618ff03dc53SRichard Tran Mills { 619ff03dc53SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 620ff03dc53SRichard Tran Mills const PetscScalar *x; 621ff03dc53SRichard Tran Mills PetscScalar *y,*z; 622ff03dc53SRichard Tran Mills const MatScalar *aa; 623ff03dc53SRichard Tran Mills PetscErrorCode ierr; 624ff03dc53SRichard Tran Mills PetscInt m=A->rmap->n; 625db63039fSRichard Tran Mills PetscInt n=A->cmap->n; 626ff03dc53SRichard Tran Mills const PetscInt *aj,*ai; 627ff03dc53SRichard Tran Mills PetscInt i; 628ff03dc53SRichard Tran Mills 629ff03dc53SRichard Tran Mills /* Variables not in MatMultTransposeAdd_SeqAIJ. */ 630ff03dc53SRichard Tran Mills char transa = 't'; /* Used to indicate to MKL that we are computing the transpose product. */ 631a84739b8SRichard Tran Mills PetscScalar alpha = 1.0; 632db63039fSRichard Tran Mills PetscScalar beta; 633a84739b8SRichard Tran Mills char matdescra[6]; 6344a2a386eSRichard Tran Mills 6354a2a386eSRichard Tran Mills PetscFunctionBegin; 636a84739b8SRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 637a84739b8SRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 638a84739b8SRichard Tran Mills 6394a2a386eSRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 6404a2a386eSRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 6414a2a386eSRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 6424a2a386eSRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 6434a2a386eSRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 6444a2a386eSRichard Tran Mills 6454a2a386eSRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 646a84739b8SRichard Tran Mills if (zz == yy) { 647a84739b8SRichard 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. */ 648db63039fSRichard Tran Mills beta = 1.0; 649969800c5SRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 650a84739b8SRichard Tran Mills } else { 651db63039fSRichard Tran Mills /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z. 652db63039fSRichard Tran Mills * MKL sparse BLAS does not have a MatMultAdd equivalent. */ 653db63039fSRichard Tran Mills beta = 0.0; 654db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 655969800c5SRichard Tran Mills for (i=0; i<n; i++) { 6564a2a386eSRichard Tran Mills z[i] += y[i]; 6574a2a386eSRichard Tran Mills } 658a84739b8SRichard Tran Mills } 6594a2a386eSRichard Tran Mills 6604a2a386eSRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 6614a2a386eSRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 6624a2a386eSRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 6634a2a386eSRichard Tran Mills PetscFunctionReturn(0); 6644a2a386eSRichard Tran Mills } 6651950a7e7SRichard Tran Mills #endif 6664a2a386eSRichard Tran Mills 667ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 668df555b71SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz) 669df555b71SRichard Tran Mills { 670df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 671df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl=(Mat_SeqAIJMKL*)A->spptr; 672df555b71SRichard Tran Mills const PetscScalar *x; 673df555b71SRichard Tran Mills PetscScalar *y,*z; 674df555b71SRichard Tran Mills PetscErrorCode ierr; 675969800c5SRichard Tran Mills PetscInt n=A->cmap->n; 676df555b71SRichard Tran Mills PetscInt i; 677551aa5c8SRichard Tran Mills PetscObjectState state; 678df555b71SRichard Tran Mills 679df555b71SRichard Tran Mills /* Variables not in MatMultTransposeAdd_SeqAIJ. */ 680df555b71SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 681df555b71SRichard Tran Mills 682df555b71SRichard Tran Mills PetscFunctionBegin; 683df555b71SRichard Tran Mills 68438987b35SRichard Tran Mills /* If there are no nonzero entries, set zz = yy and return immediately. */ 68538987b35SRichard Tran Mills if(!a->nz) { 68638987b35SRichard Tran Mills PetscInt i; 68738987b35SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 68838987b35SRichard Tran Mills for (i=0; i<n; i++) { 68938987b35SRichard Tran Mills z[i] = y[i]; 69038987b35SRichard Tran Mills } 69138987b35SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 69238987b35SRichard Tran Mills PetscFunctionReturn(0); 69338987b35SRichard Tran Mills } 694f36dfe3fSRichard Tran Mills 695df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 696df555b71SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 697df555b71SRichard Tran Mills 6983fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 6993fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 7003fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 701551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 702551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 7033fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 7043fa15762SRichard Tran Mills } 7053fa15762SRichard Tran Mills 706df555b71SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 707df555b71SRichard Tran Mills if (zz == yy) { 708df555b71SRichard 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, 709df555b71SRichard Tran Mills * with alpha and beta both set to 1.0. */ 710db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z); 7119c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 712df555b71SRichard Tran Mills } else { 713df555b71SRichard 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 714df555b71SRichard Tran Mills * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */ 715db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z); 7169c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 717969800c5SRichard Tran Mills for (i=0; i<n; i++) { 718df555b71SRichard Tran Mills z[i] += y[i]; 719df555b71SRichard Tran Mills } 720df555b71SRichard Tran Mills } 721df555b71SRichard Tran Mills 722df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 723df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 724df555b71SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 725df555b71SRichard Tran Mills PetscFunctionReturn(0); 726df555b71SRichard Tran Mills } 727d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 728df555b71SRichard Tran Mills 7298a369200SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE) 730e8be1fc7SRichard Tran Mills PetscErrorCode MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat B,Mat C) 731e8be1fc7SRichard Tran Mills { 732e8be1fc7SRichard Tran Mills Mat_SeqAIJMKL *a, *b, *c; 733e8be1fc7SRichard Tran Mills sparse_matrix_t csrA, csrB, csrC; 734e8be1fc7SRichard Tran Mills PetscErrorCode ierr; 735e8be1fc7SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 736e8be1fc7SRichard Tran Mills struct matrix_descr descr_type_gen; 737e8be1fc7SRichard Tran Mills PetscObjectState state; 738e8be1fc7SRichard Tran Mills 739e8be1fc7SRichard Tran Mills PetscFunctionBegin; 740e8be1fc7SRichard Tran Mills a = (Mat_SeqAIJMKL*)A->spptr; 741e8be1fc7SRichard Tran Mills b = (Mat_SeqAIJMKL*)B->spptr; 742e8be1fc7SRichard Tran Mills c = (Mat_SeqAIJMKL*)C->spptr; 743e8be1fc7SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 744e8be1fc7SRichard Tran Mills if (!a->sparse_optimized || a->state != state) { 745e8be1fc7SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 746e8be1fc7SRichard Tran Mills } 747e8be1fc7SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr); 748e8be1fc7SRichard Tran Mills if (!b->sparse_optimized || b->state != state) { 749e8be1fc7SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(B); 750e8be1fc7SRichard Tran Mills } 751e8be1fc7SRichard Tran Mills csrA = a->csrA; 752e8be1fc7SRichard Tran Mills csrB = b->csrA; 753e8be1fc7SRichard Tran Mills csrC = c->csrA; 754e8be1fc7SRichard Tran Mills descr_type_gen.type = SPARSE_MATRIX_TYPE_GENERAL; 755e8be1fc7SRichard Tran Mills 756e8be1fc7SRichard Tran Mills stat = mkl_sparse_sp2m(SPARSE_OPERATION_NON_TRANSPOSE,descr_type_gen,csrA, 757e8be1fc7SRichard Tran Mills SPARSE_OPERATION_NON_TRANSPOSE,descr_type_gen,csrB, 758e8be1fc7SRichard Tran Mills SPARSE_STAGE_FINALIZE_MULT,&csrC); 759e8be1fc7SRichard Tran Mills 760e8be1fc7SRichard 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"); 761e8be1fc7SRichard Tran Mills 762e8be1fc7SRichard Tran Mills /* Have to update the PETSc AIJ representation for matrix C from contents of MKL handle. */ 7634f53af40SRichard Tran Mills ierr = MatSeqAIJMKL_update_from_mkl_handle(C);CHKERRQ(ierr); 764e8be1fc7SRichard Tran Mills 765e8be1fc7SRichard Tran Mills PetscFunctionReturn(0); 766e8be1fc7SRichard Tran Mills } 7678a369200SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE */ 768e8be1fc7SRichard Tran Mills 7698a369200SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE) 7704f53af40SRichard Tran Mills PetscErrorCode MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2(Mat A,Mat P,Mat C) 7714f53af40SRichard Tran Mills { 7724f53af40SRichard Tran Mills Mat_SeqAIJMKL *a, *p, *c; 7734f53af40SRichard Tran Mills sparse_matrix_t csrA, csrP, csrC; 7744f53af40SRichard Tran Mills PetscBool set, flag; 7754f53af40SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 776b9e1dd46SRichard Tran Mills struct matrix_descr descr_type_sym; 7774f53af40SRichard Tran Mills PetscObjectState state; 7784f53af40SRichard Tran Mills PetscErrorCode ierr; 7794f53af40SRichard Tran Mills 7804f53af40SRichard Tran Mills PetscFunctionBegin; 7814f53af40SRichard Tran Mills ierr = MatIsSymmetricKnown(A,&set,&flag); 7824f53af40SRichard Tran Mills if (!set || (set && !flag)) { 7834f53af40SRichard Tran Mills ierr = MatPtAPNumeric_SeqAIJ_SeqAIJ(A,P,C);CHKERRQ(ierr); 7844f53af40SRichard Tran Mills PetscFunctionReturn(0); 7854f53af40SRichard Tran Mills } 7864f53af40SRichard Tran Mills 7874f53af40SRichard Tran Mills a = (Mat_SeqAIJMKL*)A->spptr; 7884f53af40SRichard Tran Mills p = (Mat_SeqAIJMKL*)P->spptr; 7894f53af40SRichard Tran Mills c = (Mat_SeqAIJMKL*)C->spptr; 7904f53af40SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 7914f53af40SRichard Tran Mills if (!a->sparse_optimized || a->state != state) { 7924f53af40SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 7934f53af40SRichard Tran Mills } 7944f53af40SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)P,&state);CHKERRQ(ierr); 7954f53af40SRichard Tran Mills if (!p->sparse_optimized || p->state != state) { 7964f53af40SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(P); 7974f53af40SRichard Tran Mills } 7984f53af40SRichard Tran Mills csrA = a->csrA; 7994f53af40SRichard Tran Mills csrP = p->csrA; 8004f53af40SRichard Tran Mills csrC = c->csrA; 801b9e1dd46SRichard Tran Mills descr_type_sym.type = SPARSE_MATRIX_TYPE_SYMMETRIC; 802b9e1dd46SRichard Tran Mills descr_type_sym.mode = SPARSE_FILL_MODE_LOWER; 803b9e1dd46SRichard Tran Mills descr_type_sym.diag = SPARSE_DIAG_NON_UNIT; 8044f53af40SRichard Tran Mills 805f8990b4aSRichard Tran Mills /* Note that the call below won't work for complex matrices. (We protect this when pointers are assigned in MatConvert.) */ 806b9e1dd46SRichard Tran Mills stat = mkl_sparse_sypr(SPARSE_OPERATION_TRANSPOSE,csrP,csrA,descr_type_sym,&csrC,SPARSE_STAGE_FINALIZE_MULT); 8074f53af40SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to finalize mkl_sparse_sypr"); 8084f53af40SRichard Tran Mills 8094f53af40SRichard Tran Mills /* Have to update the PETSc AIJ representation for matrix C from contents of MKL handle. */ 8104f53af40SRichard Tran Mills ierr = MatSeqAIJMKL_update_from_mkl_handle(C);CHKERRQ(ierr); 8114f53af40SRichard Tran Mills 8124f53af40SRichard Tran Mills PetscFunctionReturn(0); 8134f53af40SRichard Tran Mills } 8144f53af40SRichard Tran Mills #endif 8154f53af40SRichard Tran Mills 8164a2a386eSRichard Tran Mills /* MatConvert_SeqAIJ_SeqAIJMKL converts a SeqAIJ matrix into a 817510b72f4SRichard Tran Mills * SeqAIJMKL matrix. This routine is called by the MatCreate_SeqAIJMKL() 8184a2a386eSRichard Tran Mills * routine, but can also be used to convert an assembled SeqAIJ matrix 8194a2a386eSRichard Tran Mills * into a SeqAIJMKL one. */ 8204a2a386eSRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJMKL(Mat A,MatType type,MatReuse reuse,Mat *newmat) 8214a2a386eSRichard Tran Mills { 8224a2a386eSRichard Tran Mills PetscErrorCode ierr; 8234a2a386eSRichard Tran Mills Mat B = *newmat; 8244a2a386eSRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 825c9d46305SRichard Tran Mills PetscBool set; 826e9c94282SRichard Tran Mills PetscBool sametype; 8274a2a386eSRichard Tran Mills 8284a2a386eSRichard Tran Mills PetscFunctionBegin; 8294a2a386eSRichard Tran Mills if (reuse == MAT_INITIAL_MATRIX) { 8304a2a386eSRichard Tran Mills ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr); 8314a2a386eSRichard Tran Mills } 8324a2a386eSRichard Tran Mills 833e9c94282SRichard Tran Mills ierr = PetscObjectTypeCompare((PetscObject)A,type,&sametype);CHKERRQ(ierr); 834e9c94282SRichard Tran Mills if (sametype) PetscFunctionReturn(0); 835e9c94282SRichard Tran Mills 8364a2a386eSRichard Tran Mills ierr = PetscNewLog(B,&aijmkl);CHKERRQ(ierr); 8374a2a386eSRichard Tran Mills B->spptr = (void*) aijmkl; 8384a2a386eSRichard Tran Mills 839df555b71SRichard Tran Mills /* Set function pointers for methods that we inherit from AIJ but override. 840969800c5SRichard Tran Mills * We also parse some command line options below, since those determine some of the methods we point to. */ 8414a2a386eSRichard Tran Mills B->ops->duplicate = MatDuplicate_SeqAIJMKL; 8424a2a386eSRichard Tran Mills B->ops->assemblyend = MatAssemblyEnd_SeqAIJMKL; 8434a2a386eSRichard Tran Mills B->ops->destroy = MatDestroy_SeqAIJMKL; 844c9d46305SRichard Tran Mills 8454abfa3b3SRichard Tran Mills aijmkl->sparse_optimized = PETSC_FALSE; 846ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 847d995685eSRichard Tran Mills aijmkl->no_SpMV2 = PETSC_FALSE; /* Default to using the SpMV2 routines if our MKL supports them. */ 848a8327b06SKarl Rupp #else 849d995685eSRichard Tran Mills aijmkl->no_SpMV2 = PETSC_TRUE; 850d995685eSRichard Tran Mills #endif 8515b49642aSRichard Tran Mills aijmkl->eager_inspection = PETSC_FALSE; 8524abfa3b3SRichard Tran Mills 8534abfa3b3SRichard Tran Mills /* Parse command line options. */ 854c9d46305SRichard Tran Mills ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"AIJMKL Options","Mat");CHKERRQ(ierr); 85548292275SRichard 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); 85648292275SRichard 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); 857c9d46305SRichard Tran Mills ierr = PetscOptionsEnd();CHKERRQ(ierr); 858ffcab697SRichard Tran Mills #if !defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 859d995685eSRichard Tran Mills if(!aijmkl->no_SpMV2) { 860d995685eSRichard 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"); 861d995685eSRichard Tran Mills aijmkl->no_SpMV2 = PETSC_TRUE; 862d995685eSRichard Tran Mills } 863d995685eSRichard Tran Mills #endif 864c9d46305SRichard Tran Mills 865ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 866df555b71SRichard Tran Mills B->ops->mult = MatMult_SeqAIJMKL_SpMV2; 867969800c5SRichard Tran Mills B->ops->multtranspose = MatMultTranspose_SeqAIJMKL_SpMV2; 868df555b71SRichard Tran Mills B->ops->multadd = MatMultAdd_SeqAIJMKL_SpMV2; 869969800c5SRichard Tran Mills B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL_SpMV2; 8708a369200SRichard Tran Mills # if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE) 871e8be1fc7SRichard Tran Mills B->ops->matmultnumeric = MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2; 872ffcab697SRichard Tran Mills # if !defined(PETSC_USE_COMPLEX) 8734f53af40SRichard Tran Mills B->ops->ptapnumeric = MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2; 8744f53af40SRichard Tran Mills # endif 875e8be1fc7SRichard Tran Mills # endif 8761950a7e7SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 8771950a7e7SRichard Tran Mills 878213898a2SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 879213898a2SRichard 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 880213898a2SRichard 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 881213898a2SRichard Tran Mills * call mkl_sparse_optimize(), which results in the old numerical kernels (without the inspector-executor model) being used. For 882213898a2SRichard Tran Mills * versions in which the older interface has not been deprecated, we use the old interface. */ 8831950a7e7SRichard Tran Mills if (aijmkl->no_SpMV2) { 8844a2a386eSRichard Tran Mills B->ops->mult = MatMult_SeqAIJMKL; 885969800c5SRichard Tran Mills B->ops->multtranspose = MatMultTranspose_SeqAIJMKL; 8864a2a386eSRichard Tran Mills B->ops->multadd = MatMultAdd_SeqAIJMKL; 887969800c5SRichard Tran Mills B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL; 888c9d46305SRichard Tran Mills } 8891950a7e7SRichard Tran Mills #endif 8904a2a386eSRichard Tran Mills 8914a2a386eSRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",MatConvert_SeqAIJMKL_SeqAIJ);CHKERRQ(ierr); 892e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaijmkl_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr); 893e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaijmkl_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr); 894*4222ddf1SHong Zhang 89545fbe478SRichard Tran Mills if(!aijmkl->no_SpMV2) { 896ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 8978a369200SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE) 898e8be1fc7SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqaijmkl_seqaijmkl_C",MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_SpMV2);CHKERRQ(ierr); 899e8be1fc7SRichard Tran Mills #endif 90045fbe478SRichard Tran Mills #endif 90145fbe478SRichard Tran Mills } 9024a2a386eSRichard Tran Mills 9034a2a386eSRichard Tran Mills ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJMKL);CHKERRQ(ierr); 9044a2a386eSRichard Tran Mills *newmat = B; 9054a2a386eSRichard Tran Mills PetscFunctionReturn(0); 9064a2a386eSRichard Tran Mills } 9074a2a386eSRichard Tran Mills 9084a2a386eSRichard Tran Mills /*@C 9094a2a386eSRichard Tran Mills MatCreateSeqAIJMKL - Creates a sparse matrix of type SEQAIJMKL. 9104a2a386eSRichard Tran Mills This type inherits from AIJ and is largely identical, but uses sparse BLAS 9114a2a386eSRichard Tran Mills routines from Intel MKL whenever possible. 91290147e49SRichard Tran Mills If the installed version of MKL supports the "SpMV2" sparse 91390147e49SRichard Tran Mills inspector-executor routines, then those are used by default. 914597ee276SRichard Tran Mills MatMult, MatMultAdd, MatMultTranspose, MatMultTransposeAdd, MatMatMult, MatTransposeMatMult, and MatPtAP (for 915597ee276SRichard Tran Mills symmetric A) operations are currently supported. 916597ee276SRichard Tran Mills Note that MKL version 18, update 2 or later is required for MatPtAP/MatPtAPNumeric and MatMatMultNumeric. 91790147e49SRichard Tran Mills 918d083f849SBarry Smith Collective 9194a2a386eSRichard Tran Mills 9204a2a386eSRichard Tran Mills Input Parameters: 9214a2a386eSRichard Tran Mills + comm - MPI communicator, set to PETSC_COMM_SELF 9224a2a386eSRichard Tran Mills . m - number of rows 9234a2a386eSRichard Tran Mills . n - number of columns 9244a2a386eSRichard Tran Mills . nz - number of nonzeros per row (same for all rows) 9254a2a386eSRichard Tran Mills - nnz - array containing the number of nonzeros in the various rows 9264a2a386eSRichard Tran Mills (possibly different for each row) or NULL 9274a2a386eSRichard Tran Mills 9284a2a386eSRichard Tran Mills Output Parameter: 9294a2a386eSRichard Tran Mills . A - the matrix 9304a2a386eSRichard Tran Mills 93190147e49SRichard Tran Mills Options Database Keys: 93266b7eeb6SRichard Tran Mills + -mat_aijmkl_no_spmv2 - disable use of the SpMV2 inspector-executor routines 93366b7eeb6SRichard 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 93490147e49SRichard Tran Mills 9354a2a386eSRichard Tran Mills Notes: 9364a2a386eSRichard Tran Mills If nnz is given then nz is ignored 9374a2a386eSRichard Tran Mills 9384a2a386eSRichard Tran Mills Level: intermediate 9394a2a386eSRichard Tran Mills 9404a2a386eSRichard Tran Mills .seealso: MatCreate(), MatCreateMPIAIJMKL(), MatSetValues() 9414a2a386eSRichard Tran Mills @*/ 9424a2a386eSRichard Tran Mills PetscErrorCode MatCreateSeqAIJMKL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 9434a2a386eSRichard Tran Mills { 9444a2a386eSRichard Tran Mills PetscErrorCode ierr; 9454a2a386eSRichard Tran Mills 9464a2a386eSRichard Tran Mills PetscFunctionBegin; 9474a2a386eSRichard Tran Mills ierr = MatCreate(comm,A);CHKERRQ(ierr); 9484a2a386eSRichard Tran Mills ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 9494a2a386eSRichard Tran Mills ierr = MatSetType(*A,MATSEQAIJMKL);CHKERRQ(ierr); 9504a2a386eSRichard Tran Mills ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr); 9514a2a386eSRichard Tran Mills PetscFunctionReturn(0); 9524a2a386eSRichard Tran Mills } 9534a2a386eSRichard Tran Mills 9544a2a386eSRichard Tran Mills PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJMKL(Mat A) 9554a2a386eSRichard Tran Mills { 9564a2a386eSRichard Tran Mills PetscErrorCode ierr; 9574a2a386eSRichard Tran Mills 9584a2a386eSRichard Tran Mills PetscFunctionBegin; 9594a2a386eSRichard Tran Mills ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr); 9604a2a386eSRichard Tran Mills ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr); 9614a2a386eSRichard Tran Mills PetscFunctionReturn(0); 9624a2a386eSRichard Tran Mills } 963