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; 50*190ae7a4SRichard Tran Mills B->ops->productsetfromoptions = MatProductSetFromOptions_SeqAIJ; 51431879ecSRichard Tran Mills B->ops->matmultsymbolic = MatMatMultSymbolic_SeqAIJ_SeqAIJ; 52e8be1fc7SRichard Tran Mills B->ops->matmultnumeric = MatMatMultNumeric_SeqAIJ_SeqAIJ; 53*190ae7a4SRichard Tran Mills B->ops->mattransposemultnumeric = MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ; 54*190ae7a4SRichard Tran Mills B->ops->transposematmultnumeric = MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ; 554f53af40SRichard Tran Mills B->ops->ptapnumeric = MatPtAPNumeric_SeqAIJ_SeqAIJ; 564a2a386eSRichard Tran Mills 57e9c94282SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",NULL);CHKERRQ(ierr); 584222ddf1SHong Zhang 59ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 60*190ae7a4SRichard Tran Mills if (!aijmkl->no_SpMV2) { 61*190ae7a4SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE) 62*190ae7a4SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqaijmkl_seqaijmkl_C",NULL);CHKERRQ(ierr); 63*190ae7a4SRichard Tran Mills #endif 64*190ae7a4SRichard Tran Mills } 65*190ae7a4SRichard Tran Mills 664abfa3b3SRichard Tran Mills /* Free everything in the Mat_SeqAIJMKL data structure. Currently, this 67e9c94282SRichard Tran Mills * simply involves destroying the MKL sparse matrix handle and then freeing 68e9c94282SRichard Tran Mills * the spptr pointer. */ 69a8327b06SKarl Rupp if (reuse == MAT_INITIAL_MATRIX) aijmkl = (Mat_SeqAIJMKL*)B->spptr; 70a8327b06SKarl Rupp 714abfa3b3SRichard Tran Mills if (aijmkl->sparse_optimized) { 720632b357SRichard Tran Mills sparse_status_t stat; 734abfa3b3SRichard Tran Mills stat = mkl_sparse_destroy(aijmkl->csrA); 749c46acdfSRichard 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"); 754abfa3b3SRichard Tran Mills } 766718818eSStefano Zampini #endif 77e9c94282SRichard Tran Mills ierr = PetscFree(B->spptr);CHKERRQ(ierr); 784a2a386eSRichard Tran Mills 794a2a386eSRichard Tran Mills /* Change the type of B to MATSEQAIJ. */ 804a2a386eSRichard Tran Mills ierr = PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ);CHKERRQ(ierr); 814a2a386eSRichard Tran Mills 824a2a386eSRichard Tran Mills *newmat = B; 834a2a386eSRichard Tran Mills PetscFunctionReturn(0); 844a2a386eSRichard Tran Mills } 854a2a386eSRichard Tran Mills 864a2a386eSRichard Tran Mills PetscErrorCode MatDestroy_SeqAIJMKL(Mat A) 874a2a386eSRichard Tran Mills { 884a2a386eSRichard Tran Mills PetscErrorCode ierr; 894a2a386eSRichard Tran Mills Mat_SeqAIJMKL *aijmkl = (Mat_SeqAIJMKL*) A->spptr; 904a2a386eSRichard Tran Mills 914a2a386eSRichard Tran Mills PetscFunctionBegin; 92e9c94282SRichard Tran Mills 93e9c94282SRichard Tran Mills /* If MatHeaderMerge() was used, then this SeqAIJMKL matrix will not have an 94e9c94282SRichard Tran Mills * spptr pointer. */ 95e9c94282SRichard Tran Mills if (aijmkl) { 964a2a386eSRichard Tran Mills /* Clean up everything in the Mat_SeqAIJMKL data structure, then free A->spptr. */ 97ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 984abfa3b3SRichard Tran Mills if (aijmkl->sparse_optimized) { 994abfa3b3SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 1004abfa3b3SRichard Tran Mills stat = mkl_sparse_destroy(aijmkl->csrA); 1019c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy"); 1024abfa3b3SRichard Tran Mills } 1034abfa3b3SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 1044a2a386eSRichard Tran Mills ierr = PetscFree(A->spptr);CHKERRQ(ierr); 105e9c94282SRichard Tran Mills } 1064a2a386eSRichard Tran Mills 1074a2a386eSRichard Tran Mills /* Change the type of A back to SEQAIJ and use MatDestroy_SeqAIJ() 1084a2a386eSRichard Tran Mills * to destroy everything that remains. */ 1094a2a386eSRichard Tran Mills ierr = PetscObjectChangeTypeName((PetscObject)A, MATSEQAIJ);CHKERRQ(ierr); 1104a2a386eSRichard Tran Mills /* Note that I don't call MatSetType(). I believe this is because that 1114a2a386eSRichard Tran Mills * is only to be called when *building* a matrix. I could be wrong, but 1124a2a386eSRichard Tran Mills * that is how things work for the SuperLU matrix class. */ 1134a2a386eSRichard Tran Mills ierr = MatDestroy_SeqAIJ(A);CHKERRQ(ierr); 1144a2a386eSRichard Tran Mills PetscFunctionReturn(0); 1154a2a386eSRichard Tran Mills } 1164a2a386eSRichard Tran Mills 117*190ae7a4SRichard Tran Mills /* MatSeqAIJMKL_create_mkl_handle(), if called with an AIJMKL matrix that has not had mkl_sparse_optimize() called for it, 1185b49642aSRichard Tran Mills * creates an MKL sparse matrix handle from the AIJ arrays and calls mkl_sparse_optimize(). 1195b49642aSRichard Tran Mills * If called with an AIJMKL matrix for which aijmkl->sparse_optimized == PETSC_TRUE, then it destroys the old matrix 1205b49642aSRichard Tran Mills * handle, creates a new one, and then calls mkl_sparse_optimize(). 1215b49642aSRichard Tran Mills * Although in normal MKL usage it is possible to have a valid matrix handle on which mkl_sparse_optimize() has not been 1225b49642aSRichard Tran Mills * called, for AIJMKL the handle creation and optimization step always occur together, so we don't handle the case of 1235b49642aSRichard Tran Mills * an unoptimized matrix handle here. */ 1246e369cd5SRichard Tran Mills PETSC_INTERN PetscErrorCode MatSeqAIJMKL_create_mkl_handle(Mat A) 1254a2a386eSRichard Tran Mills { 126ffcab697SRichard Tran Mills #if !defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 1276e369cd5SRichard Tran Mills /* If the MKL library does not have mkl_sparse_optimize(), then this routine 1286e369cd5SRichard Tran Mills * does nothing. We make it callable anyway in this case because it cuts 1296e369cd5SRichard Tran Mills * down on littering the code with #ifdefs. */ 13045fbe478SRichard Tran Mills PetscFunctionBegin; 1316e369cd5SRichard Tran Mills PetscFunctionReturn(0); 1326e369cd5SRichard Tran Mills #else 133a8327b06SKarl Rupp Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 134a8327b06SKarl Rupp Mat_SeqAIJMKL *aijmkl = (Mat_SeqAIJMKL*)A->spptr; 135a8327b06SKarl Rupp PetscInt m,n; 136a8327b06SKarl Rupp MatScalar *aa; 137a8327b06SKarl Rupp PetscInt *aj,*ai; 1386e369cd5SRichard Tran Mills sparse_status_t stat; 139551aa5c8SRichard Tran Mills PetscErrorCode ierr; 1404a2a386eSRichard Tran Mills 141a8327b06SKarl Rupp PetscFunctionBegin; 142e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 143e626a176SRichard Tran Mills /* For MKL versions that still support the old, non-inspector-executor interfaces versions, we simply exit here if the no_SpMV2 144e626a176SRichard Tran Mills * option has been specified. For versions that have deprecated the old interfaces (version 18, update 2 and later), we must 145e626a176SRichard Tran Mills * use the new inspector-executor interfaces, but we can still use the old, non-inspector-executor code by not calling 146e626a176SRichard Tran Mills * mkl_sparse_optimize() later. */ 1476e369cd5SRichard Tran Mills if (aijmkl->no_SpMV2) PetscFunctionReturn(0); 1484d51fa23SRichard Tran Mills #endif 1496e369cd5SRichard Tran Mills 1500632b357SRichard Tran Mills if (aijmkl->sparse_optimized) { 1510632b357SRichard Tran Mills /* Matrix has been previously assembled and optimized. Must destroy old 1520632b357SRichard Tran Mills * matrix handle before running the optimization step again. */ 1530632b357SRichard Tran Mills stat = mkl_sparse_destroy(aijmkl->csrA); 1549c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_destroy"); 1550632b357SRichard Tran Mills } 1568d3fe1b0SRichard Tran Mills aijmkl->sparse_optimized = PETSC_FALSE; 1576e369cd5SRichard Tran Mills 158c9d46305SRichard Tran Mills /* Now perform the SpMV2 setup and matrix optimization. */ 159df555b71SRichard Tran Mills aijmkl->descr.type = SPARSE_MATRIX_TYPE_GENERAL; 160df555b71SRichard Tran Mills aijmkl->descr.mode = SPARSE_FILL_MODE_LOWER; 161df555b71SRichard Tran Mills aijmkl->descr.diag = SPARSE_DIAG_NON_UNIT; 16258678438SRichard Tran Mills m = A->rmap->n; 16358678438SRichard Tran Mills n = A->cmap->n; 164df555b71SRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 165df555b71SRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 166df555b71SRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 16746cdef40SRichard Tran Mills if ((a->nz!=0) && aa && !(A->structure_only)) { 1688d3fe1b0SRichard Tran Mills /* Create a new, optimized sparse matrix handle only if the matrix has nonzero entries. 1698d3fe1b0SRichard Tran Mills * The MKL sparse-inspector executor routines don't like being passed an empty matrix. */ 17058678438SRichard Tran Mills stat = mkl_sparse_x_create_csr(&aijmkl->csrA,SPARSE_INDEX_BASE_ZERO,m,n,ai,ai+1,aj,aa); 171e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to create matrix handle"); 172df555b71SRichard Tran Mills stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000); 173e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set mv_hint"); 174df555b71SRichard Tran Mills stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE); 175e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set memory_hint"); 1761950a7e7SRichard Tran Mills if (!aijmkl->no_SpMV2) { 177df555b71SRichard Tran Mills stat = mkl_sparse_optimize(aijmkl->csrA); 178e8be1fc7SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete mkl_sparse_optimize"); 1791950a7e7SRichard Tran Mills } 1804abfa3b3SRichard Tran Mills aijmkl->sparse_optimized = PETSC_TRUE; 181e995cf24SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&(aijmkl->state));CHKERRQ(ierr); 182*190ae7a4SRichard Tran Mills } else { 183*190ae7a4SRichard Tran Mills aijmkl->csrA = PETSC_NULL; 184c9d46305SRichard Tran Mills } 1856e369cd5SRichard Tran Mills 1866e369cd5SRichard Tran Mills PetscFunctionReturn(0); 187d995685eSRichard Tran Mills #endif 1886e369cd5SRichard Tran Mills } 1896e369cd5SRichard Tran Mills 190ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 191*190ae7a4SRichard Tran Mills /* Take an already created but empty matrix and set up the nonzero structure from an MKL sparse matrix handle. */ 192*190ae7a4SRichard Tran Mills static PetscErrorCode MatSeqAIJMKL_setup_structure_from_mkl_handle(MPI_Comm comm,sparse_matrix_t csrA,PetscInt nrows,PetscInt ncols,Mat A) 19319afcda9SRichard Tran Mills { 19419afcda9SRichard Tran Mills PetscErrorCode ierr; 19519afcda9SRichard Tran Mills sparse_status_t stat; 19619afcda9SRichard Tran Mills sparse_index_base_t indexing; 197*190ae7a4SRichard Tran Mills PetscInt m,n; 19845fbe478SRichard Tran Mills PetscInt *aj,*ai,*dummy; 19919afcda9SRichard Tran Mills MatScalar *aa; 20019afcda9SRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 20119afcda9SRichard Tran Mills 202*190ae7a4SRichard Tran Mills if (csrA) { 20345fbe478SRichard Tran Mills /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */ 204*190ae7a4SRichard Tran Mills stat = mkl_sparse_x_export_csr(csrA,&indexing,&m,&n,&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()"); 206*190ae7a4SRichard Tran Mills if ((m != nrows) || (n != ncols)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Number of rows/columns does not match those from mkl_sparse_x_export_csr()"); 207*190ae7a4SRichard Tran Mills } else { 208*190ae7a4SRichard Tran Mills aj = ai = PETSC_NULL; 209*190ae7a4SRichard Tran Mills aa = PETSC_NULL; 210aab60f1bSRichard Tran Mills } 211*190ae7a4SRichard Tran Mills 21219afcda9SRichard Tran Mills ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr); 21345fbe478SRichard Tran Mills ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,nrows,ncols);CHKERRQ(ierr); 214aab60f1bSRichard Tran Mills /* We use MatSeqAIJSetPreallocationCSR() instead of MatCreateSeqAIJWithArrays() because we must copy the arrays exported 215aab60f1bSRichard Tran Mills * from MKL; MKL developers tell us that modifying the arrays may cause unexpected results when using the MKL handle, and 216aab60f1bSRichard Tran Mills * they will be destroyed when the MKL handle is destroyed. 217aab60f1bSRichard Tran Mills * (In the interest of reducing memory consumption in future, can we figure out good ways to deal with this?) */ 218*190ae7a4SRichard Tran Mills if (csrA) { 219*190ae7a4SRichard Tran Mills ierr = MatSeqAIJSetPreallocationCSR(A,ai,aj,NULL);CHKERRQ(ierr); 220*190ae7a4SRichard Tran Mills } else { 221*190ae7a4SRichard Tran Mills /* Since MatSeqAIJSetPreallocationCSR does initial set up and assembly begin/end, we must do that ourselves here. */ 222*190ae7a4SRichard Tran Mills ierr = MatSetUp(A);CHKERRQ(ierr); 223*190ae7a4SRichard Tran Mills ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 224*190ae7a4SRichard Tran Mills ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 225*190ae7a4SRichard Tran Mills } 22619afcda9SRichard Tran Mills 22719afcda9SRichard Tran Mills /* We now have an assembled sequential AIJ matrix created from copies of the exported arrays from the MKL matrix handle. 22819afcda9SRichard Tran Mills * Now turn it into a MATSEQAIJMKL. */ 22919afcda9SRichard Tran Mills ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr); 2306c87cf42SRichard Tran Mills 23119afcda9SRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 23219afcda9SRichard Tran Mills aijmkl->csrA = csrA; 2336c87cf42SRichard Tran Mills 23419afcda9SRichard Tran Mills /* The below code duplicates much of what is in MatSeqAIJKL_create_mkl_handle(). I dislike this code duplication, but 23519afcda9SRichard Tran Mills * MatSeqAIJMKL_create_mkl_handle() cannot be used because we don't need to create a handle -- we've already got one, 23619afcda9SRichard Tran Mills * and just need to be able to run the MKL optimization step. */ 237f3fd1758SRichard Tran Mills aijmkl->descr.type = SPARSE_MATRIX_TYPE_GENERAL; 238f3fd1758SRichard Tran Mills aijmkl->descr.mode = SPARSE_FILL_MODE_LOWER; 239f3fd1758SRichard Tran Mills aijmkl->descr.diag = SPARSE_DIAG_NON_UNIT; 240*190ae7a4SRichard Tran Mills if (csrA) { 24119afcda9SRichard Tran Mills stat = mkl_sparse_set_mv_hint(aijmkl->csrA,SPARSE_OPERATION_NON_TRANSPOSE,aijmkl->descr,1000); 24251539a68SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set mv_hint"); 24319afcda9SRichard Tran Mills stat = mkl_sparse_set_memory_hint(aijmkl->csrA,SPARSE_MEMORY_AGGRESSIVE); 24451539a68SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to set memory_hint"); 2451950a7e7SRichard Tran Mills } 246e995cf24SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&(aijmkl->state));CHKERRQ(ierr); 24719afcda9SRichard Tran Mills 24819afcda9SRichard Tran Mills PetscFunctionReturn(0); 24919afcda9SRichard Tran Mills } 25019afcda9SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 25119afcda9SRichard Tran Mills 252*190ae7a4SRichard Tran Mills 253e8be1fc7SRichard Tran Mills /* MatSeqAIJMKL_update_from_mkl_handle() updates the matrix values array from the contents of the associated MKL sparse matrix handle. 254e8be1fc7SRichard 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 255e8be1fc7SRichard Tran Mills * MatMatMultNumeric(). */ 256ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 257*190ae7a4SRichard Tran Mills static PetscErrorCode MatSeqAIJMKL_update_from_mkl_handle(Mat A) 258e8be1fc7SRichard Tran Mills { 259e8be1fc7SRichard Tran Mills PetscInt i; 260e8be1fc7SRichard Tran Mills PetscInt nrows,ncols; 261e8be1fc7SRichard Tran Mills PetscInt nz; 262e8be1fc7SRichard Tran Mills PetscInt *ai,*aj,*dummy; 263e8be1fc7SRichard Tran Mills PetscScalar *aa; 264e8be1fc7SRichard Tran Mills PetscErrorCode ierr; 265e8be1fc7SRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 266e8be1fc7SRichard Tran Mills sparse_status_t stat; 267e8be1fc7SRichard Tran Mills sparse_index_base_t indexing; 268e8be1fc7SRichard Tran Mills 269e8be1fc7SRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 270e8be1fc7SRichard Tran Mills 271*190ae7a4SRichard Tran Mills /* Exit immediately in case of the MKL matrix handle being NULL; this will be the case for empty matrices (zero rows or columns). */ 272*190ae7a4SRichard Tran Mills if (!aijmkl->csrA) PetscFunctionReturn(0); 273*190ae7a4SRichard Tran Mills 274e8be1fc7SRichard Tran Mills /* Note: Must pass in &dummy below since MKL can't accept NULL for this output array we don't actually want. */ 275e8be1fc7SRichard Tran Mills stat = mkl_sparse_x_export_csr(aijmkl->csrA,&indexing,&nrows,&ncols,&ai,&dummy,&aj,&aa); 276e8be1fc7SRichard 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()"); 277e8be1fc7SRichard Tran Mills 278e8be1fc7SRichard 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 279e8be1fc7SRichard Tran Mills * representations differ in small ways (e.g., more explicit nonzeros per row due to preallocation). */ 280e8be1fc7SRichard Tran Mills for (i=0; i<nrows; i++) { 281e8be1fc7SRichard Tran Mills nz = ai[i+1] - ai[i]; 282e8be1fc7SRichard Tran Mills ierr = MatSetValues_SeqAIJ(A, 1, &i, nz, aj+ai[i], aa+ai[i], INSERT_VALUES);CHKERRQ(ierr); 283e8be1fc7SRichard Tran Mills } 284e8be1fc7SRichard Tran Mills 285e8be1fc7SRichard Tran Mills ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 286e8be1fc7SRichard Tran Mills ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 287e8be1fc7SRichard Tran Mills 288e995cf24SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&(aijmkl->state));CHKERRQ(ierr); 289e995cf24SRichard Tran Mills /* We mark our matrix as having a valid, optimized MKL handle. 290e995cf24SRichard Tran Mills * TODO: It is valid, but I am not sure if it is optimized. Need to ask MKL developers. */ 291e995cf24SRichard Tran Mills aijmkl->sparse_optimized = PETSC_TRUE; 292e995cf24SRichard Tran Mills 293e8be1fc7SRichard Tran Mills PetscFunctionReturn(0); 294e8be1fc7SRichard Tran Mills } 295e8be1fc7SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 296e8be1fc7SRichard Tran Mills 2976e369cd5SRichard Tran Mills PetscErrorCode MatDuplicate_SeqAIJMKL(Mat A, MatDuplicateOption op, Mat *M) 2986e369cd5SRichard Tran Mills { 2996e369cd5SRichard Tran Mills PetscErrorCode ierr; 3006e369cd5SRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 3016e369cd5SRichard Tran Mills Mat_SeqAIJMKL *aijmkl_dest; 3026e369cd5SRichard Tran Mills 3036e369cd5SRichard Tran Mills PetscFunctionBegin; 3046e369cd5SRichard Tran Mills ierr = MatDuplicate_SeqAIJ(A,op,M);CHKERRQ(ierr); 3056e369cd5SRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 3066e369cd5SRichard Tran Mills aijmkl_dest = (Mat_SeqAIJMKL*) (*M)->spptr; 307580bdb30SBarry Smith ierr = PetscArraycpy(aijmkl_dest,aijmkl,1);CHKERRQ(ierr); 3086e369cd5SRichard Tran Mills aijmkl_dest->sparse_optimized = PETSC_FALSE; 3095b49642aSRichard Tran Mills if (aijmkl->eager_inspection) { 3106e369cd5SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr); 3115b49642aSRichard Tran Mills } 3126e369cd5SRichard Tran Mills PetscFunctionReturn(0); 3136e369cd5SRichard Tran Mills } 3146e369cd5SRichard Tran Mills 3156e369cd5SRichard Tran Mills PetscErrorCode MatAssemblyEnd_SeqAIJMKL(Mat A, MatAssemblyType mode) 3166e369cd5SRichard Tran Mills { 3176e369cd5SRichard Tran Mills PetscErrorCode ierr; 3186e369cd5SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3195b49642aSRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 3206e369cd5SRichard Tran Mills 3216e369cd5SRichard Tran Mills PetscFunctionBegin; 3226e369cd5SRichard Tran Mills if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 3236e369cd5SRichard Tran Mills 3246e369cd5SRichard Tran Mills /* Since a MATSEQAIJMKL matrix is really just a MATSEQAIJ with some 3256e369cd5SRichard Tran Mills * extra information and some different methods, call the AssemblyEnd 3266e369cd5SRichard Tran Mills * routine for a MATSEQAIJ. 3276e369cd5SRichard Tran Mills * I'm not sure if this is the best way to do this, but it avoids 328d96e85feSRichard Tran Mills * a lot of code duplication. */ 3296e369cd5SRichard Tran Mills a->inode.use = PETSC_FALSE; /* Must disable: otherwise the MKL routines won't get used. */ 3306e369cd5SRichard Tran Mills ierr = MatAssemblyEnd_SeqAIJ(A, mode);CHKERRQ(ierr); 3316e369cd5SRichard Tran Mills 3325b49642aSRichard Tran Mills /* If the user has requested "eager" inspection, create the optimized MKL sparse handle (if needed; the function checks). 3335b49642aSRichard Tran Mills * (The default is to do "lazy" inspection, deferring this until something like MatMult() is called.) */ 3345b49642aSRichard Tran Mills aijmkl = (Mat_SeqAIJMKL*) A->spptr; 3355b49642aSRichard Tran Mills if (aijmkl->eager_inspection) { 3366e369cd5SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(A);CHKERRQ(ierr); 3375b49642aSRichard Tran Mills } 338df555b71SRichard Tran Mills 3394a2a386eSRichard Tran Mills PetscFunctionReturn(0); 3404a2a386eSRichard Tran Mills } 3414a2a386eSRichard Tran Mills 342e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 3434a2a386eSRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL(Mat A,Vec xx,Vec yy) 3444a2a386eSRichard Tran Mills { 3454a2a386eSRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3464a2a386eSRichard Tran Mills const PetscScalar *x; 3474a2a386eSRichard Tran Mills PetscScalar *y; 3484a2a386eSRichard Tran Mills const MatScalar *aa; 3494a2a386eSRichard Tran Mills PetscErrorCode ierr; 3504a2a386eSRichard Tran Mills PetscInt m = A->rmap->n; 351db63039fSRichard Tran Mills PetscInt n = A->cmap->n; 352db63039fSRichard Tran Mills PetscScalar alpha = 1.0; 353db63039fSRichard Tran Mills PetscScalar beta = 0.0; 3544a2a386eSRichard Tran Mills const PetscInt *aj,*ai; 355db63039fSRichard Tran Mills char matdescra[6]; 356db63039fSRichard Tran Mills 3574a2a386eSRichard Tran Mills 3584a2a386eSRichard Tran Mills /* Variables not in MatMult_SeqAIJ. */ 359ff03dc53SRichard Tran Mills char transa = 'n'; /* Used to indicate to MKL that we are not computing the transpose product. */ 360ff03dc53SRichard Tran Mills 361ff03dc53SRichard Tran Mills PetscFunctionBegin; 362db63039fSRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 363db63039fSRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 364ff03dc53SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 365ff03dc53SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 366ff03dc53SRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 367ff03dc53SRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 368ff03dc53SRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 369ff03dc53SRichard Tran Mills 370ff03dc53SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 371db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y); 372ff03dc53SRichard Tran Mills 373ff03dc53SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 374ff03dc53SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 375ff03dc53SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 376ff03dc53SRichard Tran Mills PetscFunctionReturn(0); 377ff03dc53SRichard Tran Mills } 3781950a7e7SRichard Tran Mills #endif 379ff03dc53SRichard Tran Mills 380ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 381df555b71SRichard Tran Mills PetscErrorCode MatMult_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy) 382df555b71SRichard Tran Mills { 383df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 384df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl = (Mat_SeqAIJMKL*)A->spptr; 385df555b71SRichard Tran Mills const PetscScalar *x; 386df555b71SRichard Tran Mills PetscScalar *y; 387df555b71SRichard Tran Mills PetscErrorCode ierr; 388df555b71SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 389551aa5c8SRichard Tran Mills PetscObjectState state; 390df555b71SRichard Tran Mills 391df555b71SRichard Tran Mills PetscFunctionBegin; 392df555b71SRichard Tran Mills 39338987b35SRichard Tran Mills /* If there are no nonzero entries, zero yy and return immediately. */ 39438987b35SRichard Tran Mills if(!a->nz) { 39538987b35SRichard Tran Mills PetscInt i; 39638987b35SRichard Tran Mills PetscInt m=A->rmap->n; 39738987b35SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 39838987b35SRichard Tran Mills for (i=0; i<m; i++) { 39938987b35SRichard Tran Mills y[i] = 0.0; 40038987b35SRichard Tran Mills } 40138987b35SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 40238987b35SRichard Tran Mills PetscFunctionReturn(0); 40338987b35SRichard Tran Mills } 404f36dfe3fSRichard Tran Mills 405df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 406df555b71SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 407df555b71SRichard Tran Mills 4083fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 4093fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 4103fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 411551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 412551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 4133fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 4143fa15762SRichard Tran Mills } 4153fa15762SRichard Tran Mills 416df555b71SRichard Tran Mills /* Call MKL SpMV2 executor routine to do the MatMult. */ 417df555b71SRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y); 4189c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 419df555b71SRichard Tran Mills 420df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 421df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 422df555b71SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 423df555b71SRichard Tran Mills PetscFunctionReturn(0); 424df555b71SRichard Tran Mills } 425d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 426df555b71SRichard Tran Mills 427e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 428ff03dc53SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL(Mat A,Vec xx,Vec yy) 429ff03dc53SRichard Tran Mills { 430ff03dc53SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 431ff03dc53SRichard Tran Mills const PetscScalar *x; 432ff03dc53SRichard Tran Mills PetscScalar *y; 433ff03dc53SRichard Tran Mills const MatScalar *aa; 434ff03dc53SRichard Tran Mills PetscErrorCode ierr; 435ff03dc53SRichard Tran Mills PetscInt m = A->rmap->n; 436db63039fSRichard Tran Mills PetscInt n = A->cmap->n; 437db63039fSRichard Tran Mills PetscScalar alpha = 1.0; 438db63039fSRichard Tran Mills PetscScalar beta = 0.0; 439ff03dc53SRichard Tran Mills const PetscInt *aj,*ai; 440db63039fSRichard Tran Mills char matdescra[6]; 441ff03dc53SRichard Tran Mills 442ff03dc53SRichard Tran Mills /* Variables not in MatMultTranspose_SeqAIJ. */ 443ff03dc53SRichard Tran Mills char transa = 't'; /* Used to indicate to MKL that we are computing the transpose product. */ 4444a2a386eSRichard Tran Mills 4454a2a386eSRichard Tran Mills PetscFunctionBegin; 446969800c5SRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 447969800c5SRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 4484a2a386eSRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 4494a2a386eSRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 4504a2a386eSRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 4514a2a386eSRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 4524a2a386eSRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 4534a2a386eSRichard Tran Mills 4544a2a386eSRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 455db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,y); 4564a2a386eSRichard Tran Mills 4574a2a386eSRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 4584a2a386eSRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 4594a2a386eSRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 4604a2a386eSRichard Tran Mills PetscFunctionReturn(0); 4614a2a386eSRichard Tran Mills } 4621950a7e7SRichard Tran Mills #endif 4634a2a386eSRichard Tran Mills 464ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 465df555b71SRichard Tran Mills PetscErrorCode MatMultTranspose_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy) 466df555b71SRichard Tran Mills { 467df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 468df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl = (Mat_SeqAIJMKL*)A->spptr; 469df555b71SRichard Tran Mills const PetscScalar *x; 470df555b71SRichard Tran Mills PetscScalar *y; 471df555b71SRichard Tran Mills PetscErrorCode ierr; 4720632b357SRichard Tran Mills sparse_status_t stat; 473551aa5c8SRichard Tran Mills PetscObjectState state; 474df555b71SRichard Tran Mills 475df555b71SRichard Tran Mills PetscFunctionBegin; 476df555b71SRichard Tran Mills 47738987b35SRichard Tran Mills /* If there are no nonzero entries, zero yy and return immediately. */ 47838987b35SRichard Tran Mills if(!a->nz) { 47938987b35SRichard Tran Mills PetscInt i; 48038987b35SRichard Tran Mills PetscInt n=A->cmap->n; 48138987b35SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 48238987b35SRichard Tran Mills for (i=0; i<n; i++) { 48338987b35SRichard Tran Mills y[i] = 0.0; 48438987b35SRichard Tran Mills } 48538987b35SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 48638987b35SRichard Tran Mills PetscFunctionReturn(0); 48738987b35SRichard Tran Mills } 488f36dfe3fSRichard Tran Mills 489df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 490df555b71SRichard Tran Mills ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 491df555b71SRichard Tran Mills 4923fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 4933fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 4943fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 495551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 496551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 4973fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 4983fa15762SRichard Tran Mills } 4993fa15762SRichard Tran Mills 500df555b71SRichard Tran Mills /* Call MKL SpMV2 executor routine to do the MatMultTranspose. */ 501df555b71SRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,y); 5029c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 503df555b71SRichard Tran Mills 504df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 505df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 506df555b71SRichard Tran Mills ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 507df555b71SRichard Tran Mills PetscFunctionReturn(0); 508df555b71SRichard Tran Mills } 509d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 510df555b71SRichard Tran Mills 511e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 5124a2a386eSRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz) 5134a2a386eSRichard Tran Mills { 5144a2a386eSRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 5154a2a386eSRichard Tran Mills const PetscScalar *x; 5164a2a386eSRichard Tran Mills PetscScalar *y,*z; 5174a2a386eSRichard Tran Mills const MatScalar *aa; 5184a2a386eSRichard Tran Mills PetscErrorCode ierr; 5194a2a386eSRichard Tran Mills PetscInt m = A->rmap->n; 520db63039fSRichard Tran Mills PetscInt n = A->cmap->n; 5214a2a386eSRichard Tran Mills const PetscInt *aj,*ai; 5224a2a386eSRichard Tran Mills PetscInt i; 5234a2a386eSRichard Tran Mills 524ff03dc53SRichard Tran Mills /* Variables not in MatMultAdd_SeqAIJ. */ 525ff03dc53SRichard Tran Mills char transa = 'n'; /* Used to indicate to MKL that we are not computing the transpose product. */ 526a84739b8SRichard Tran Mills PetscScalar alpha = 1.0; 527db63039fSRichard Tran Mills PetscScalar beta; 528a84739b8SRichard Tran Mills char matdescra[6]; 529ff03dc53SRichard Tran Mills 530ff03dc53SRichard Tran Mills PetscFunctionBegin; 531a84739b8SRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 532a84739b8SRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 533a84739b8SRichard Tran Mills 534ff03dc53SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 535ff03dc53SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 536ff03dc53SRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 537ff03dc53SRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 538ff03dc53SRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 539ff03dc53SRichard Tran Mills 540ff03dc53SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 541a84739b8SRichard Tran Mills if (zz == yy) { 542a84739b8SRichard 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. */ 543db63039fSRichard Tran Mills beta = 1.0; 544db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 545a84739b8SRichard Tran Mills } else { 546db63039fSRichard Tran Mills /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z. 547db63039fSRichard Tran Mills * MKL sparse BLAS does not have a MatMultAdd equivalent. */ 548db63039fSRichard Tran Mills beta = 0.0; 549db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 550ff03dc53SRichard Tran Mills for (i=0; i<m; i++) { 551ff03dc53SRichard Tran Mills z[i] += y[i]; 552ff03dc53SRichard Tran Mills } 553a84739b8SRichard Tran Mills } 554ff03dc53SRichard Tran Mills 555ff03dc53SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 556ff03dc53SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 557ff03dc53SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 558ff03dc53SRichard Tran Mills PetscFunctionReturn(0); 559ff03dc53SRichard Tran Mills } 5601950a7e7SRichard Tran Mills #endif 561ff03dc53SRichard Tran Mills 562ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 563df555b71SRichard Tran Mills PetscErrorCode MatMultAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz) 564df555b71SRichard Tran Mills { 565df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 566df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl = (Mat_SeqAIJMKL*)A->spptr; 567df555b71SRichard Tran Mills const PetscScalar *x; 568df555b71SRichard Tran Mills PetscScalar *y,*z; 569df555b71SRichard Tran Mills PetscErrorCode ierr; 570df555b71SRichard Tran Mills PetscInt m = A->rmap->n; 571df555b71SRichard Tran Mills PetscInt i; 572df555b71SRichard Tran Mills 573df555b71SRichard Tran Mills /* Variables not in MatMultAdd_SeqAIJ. */ 574df555b71SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 575551aa5c8SRichard Tran Mills PetscObjectState state; 576df555b71SRichard Tran Mills 577df555b71SRichard Tran Mills PetscFunctionBegin; 578df555b71SRichard Tran Mills 57938987b35SRichard Tran Mills /* If there are no nonzero entries, set zz = yy and return immediately. */ 58038987b35SRichard Tran Mills if(!a->nz) { 58138987b35SRichard Tran Mills PetscInt i; 58238987b35SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 58338987b35SRichard Tran Mills for (i=0; i<m; i++) { 58438987b35SRichard Tran Mills z[i] = y[i]; 58538987b35SRichard Tran Mills } 58638987b35SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 58738987b35SRichard Tran Mills PetscFunctionReturn(0); 58838987b35SRichard Tran Mills } 589df555b71SRichard Tran Mills 590df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 591df555b71SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 592df555b71SRichard Tran Mills 5933fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 5943fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 5953fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 596551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 597551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 5983fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 5993fa15762SRichard Tran Mills } 6003fa15762SRichard Tran Mills 601df555b71SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 602df555b71SRichard Tran Mills if (zz == yy) { 603df555b71SRichard 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, 604df555b71SRichard Tran Mills * with alpha and beta both set to 1.0. */ 605db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z); 6069c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 607df555b71SRichard Tran Mills } else { 608df555b71SRichard 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 609df555b71SRichard Tran Mills * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */ 610db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_NON_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z); 6119c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 612df555b71SRichard Tran Mills for (i=0; i<m; i++) { 613df555b71SRichard Tran Mills z[i] += y[i]; 614df555b71SRichard Tran Mills } 615df555b71SRichard Tran Mills } 616df555b71SRichard Tran Mills 617df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 618df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 619df555b71SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 620df555b71SRichard Tran Mills PetscFunctionReturn(0); 621df555b71SRichard Tran Mills } 622d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 623df555b71SRichard Tran Mills 624e626a176SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 625ff03dc53SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL(Mat A,Vec xx,Vec yy,Vec zz) 626ff03dc53SRichard Tran Mills { 627ff03dc53SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 628ff03dc53SRichard Tran Mills const PetscScalar *x; 629ff03dc53SRichard Tran Mills PetscScalar *y,*z; 630ff03dc53SRichard Tran Mills const MatScalar *aa; 631ff03dc53SRichard Tran Mills PetscErrorCode ierr; 632ff03dc53SRichard Tran Mills PetscInt m = A->rmap->n; 633db63039fSRichard Tran Mills PetscInt n = A->cmap->n; 634ff03dc53SRichard Tran Mills const PetscInt *aj,*ai; 635ff03dc53SRichard Tran Mills PetscInt i; 636ff03dc53SRichard Tran Mills 637ff03dc53SRichard Tran Mills /* Variables not in MatMultTransposeAdd_SeqAIJ. */ 638ff03dc53SRichard Tran Mills char transa = 't'; /* Used to indicate to MKL that we are computing the transpose product. */ 639a84739b8SRichard Tran Mills PetscScalar alpha = 1.0; 640db63039fSRichard Tran Mills PetscScalar beta; 641a84739b8SRichard Tran Mills char matdescra[6]; 6424a2a386eSRichard Tran Mills 6434a2a386eSRichard Tran Mills PetscFunctionBegin; 644a84739b8SRichard Tran Mills matdescra[0] = 'g'; /* Indicates to MKL that we using a general CSR matrix. */ 645a84739b8SRichard Tran Mills matdescra[3] = 'c'; /* Indicates to MKL that we use C-style (0-based) indexing. */ 646a84739b8SRichard Tran Mills 6474a2a386eSRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 6484a2a386eSRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 6494a2a386eSRichard Tran Mills aj = a->j; /* aj[k] gives column index for element aa[k]. */ 6504a2a386eSRichard Tran Mills aa = a->a; /* Nonzero elements stored row-by-row. */ 6514a2a386eSRichard Tran Mills ai = a->i; /* ai[k] is the position in aa and aj where row k starts. */ 6524a2a386eSRichard Tran Mills 6534a2a386eSRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 654a84739b8SRichard Tran Mills if (zz == yy) { 655a84739b8SRichard 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. */ 656db63039fSRichard Tran Mills beta = 1.0; 657969800c5SRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 658a84739b8SRichard Tran Mills } else { 659db63039fSRichard Tran Mills /* zz and yy are different vectors, so call MKL's mkl_xcsrmv() with beta=0, then add the result to z. 660db63039fSRichard Tran Mills * MKL sparse BLAS does not have a MatMultAdd equivalent. */ 661db63039fSRichard Tran Mills beta = 0.0; 662db63039fSRichard Tran Mills mkl_xcsrmv(&transa,&m,&n,&alpha,matdescra,aa,aj,ai,ai+1,x,&beta,z); 663969800c5SRichard Tran Mills for (i=0; i<n; i++) { 6644a2a386eSRichard Tran Mills z[i] += y[i]; 6654a2a386eSRichard Tran Mills } 666a84739b8SRichard Tran Mills } 6674a2a386eSRichard Tran Mills 6684a2a386eSRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 6694a2a386eSRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 6704a2a386eSRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 6714a2a386eSRichard Tran Mills PetscFunctionReturn(0); 6724a2a386eSRichard Tran Mills } 6731950a7e7SRichard Tran Mills #endif 6744a2a386eSRichard Tran Mills 675ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 676df555b71SRichard Tran Mills PetscErrorCode MatMultTransposeAdd_SeqAIJMKL_SpMV2(Mat A,Vec xx,Vec yy,Vec zz) 677df555b71SRichard Tran Mills { 678df555b71SRichard Tran Mills Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 679df555b71SRichard Tran Mills Mat_SeqAIJMKL *aijmkl = (Mat_SeqAIJMKL*)A->spptr; 680df555b71SRichard Tran Mills const PetscScalar *x; 681df555b71SRichard Tran Mills PetscScalar *y,*z; 682df555b71SRichard Tran Mills PetscErrorCode ierr; 683969800c5SRichard Tran Mills PetscInt n = A->cmap->n; 684df555b71SRichard Tran Mills PetscInt i; 685551aa5c8SRichard Tran Mills PetscObjectState state; 686df555b71SRichard Tran Mills 687df555b71SRichard Tran Mills /* Variables not in MatMultTransposeAdd_SeqAIJ. */ 688df555b71SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 689df555b71SRichard Tran Mills 690df555b71SRichard Tran Mills PetscFunctionBegin; 691df555b71SRichard Tran Mills 69238987b35SRichard Tran Mills /* If there are no nonzero entries, set zz = yy and return immediately. */ 69338987b35SRichard Tran Mills if(!a->nz) { 69438987b35SRichard Tran Mills PetscInt i; 69538987b35SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 69638987b35SRichard Tran Mills for (i=0; i<n; i++) { 69738987b35SRichard Tran Mills z[i] = y[i]; 69838987b35SRichard Tran Mills } 69938987b35SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 70038987b35SRichard Tran Mills PetscFunctionReturn(0); 70138987b35SRichard Tran Mills } 702f36dfe3fSRichard Tran Mills 703df555b71SRichard Tran Mills ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 704df555b71SRichard Tran Mills ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 705df555b71SRichard Tran Mills 7063fa15762SRichard Tran Mills /* In some cases, we get to this point without mkl_sparse_optimize() having been called, so we check and then call 7073fa15762SRichard Tran Mills * it if needed. Eventually, when everything in PETSc is properly updating the matrix state, we should probably 7083fa15762SRichard Tran Mills * take a "lazy" approach to creation/updating of the MKL matrix handle and plan to always do it here (when needed). */ 709551aa5c8SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 710551aa5c8SRichard Tran Mills if (!aijmkl->sparse_optimized || aijmkl->state != state) { 7113fa15762SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 7123fa15762SRichard Tran Mills } 7133fa15762SRichard Tran Mills 714df555b71SRichard Tran Mills /* Call MKL sparse BLAS routine to do the MatMult. */ 715df555b71SRichard Tran Mills if (zz == yy) { 716df555b71SRichard 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, 717df555b71SRichard Tran Mills * with alpha and beta both set to 1.0. */ 718db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,1.0,z); 7199c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 720df555b71SRichard Tran Mills } else { 721df555b71SRichard 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 722df555b71SRichard Tran Mills * we add the contents of vector yy to the result; MKL sparse BLAS does not have a MatMultAdd equivalent. */ 723db63039fSRichard Tran Mills stat = mkl_sparse_x_mv(SPARSE_OPERATION_TRANSPOSE,1.0,aijmkl->csrA,aijmkl->descr,x,0.0,z); 7249c46acdfSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: error in mkl_sparse_x_mv"); 725969800c5SRichard Tran Mills for (i=0; i<n; i++) { 726df555b71SRichard Tran Mills z[i] += y[i]; 727df555b71SRichard Tran Mills } 728df555b71SRichard Tran Mills } 729df555b71SRichard Tran Mills 730df555b71SRichard Tran Mills ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 731df555b71SRichard Tran Mills ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 732df555b71SRichard Tran Mills ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 733df555b71SRichard Tran Mills PetscFunctionReturn(0); 734df555b71SRichard Tran Mills } 735d995685eSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 736df555b71SRichard Tran Mills 737*190ae7a4SRichard Tran Mills /* -------------------------- MatProduct code -------------------------- */ 7388a369200SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE) 739*190ae7a4SRichard Tran Mills static PetscErrorCode MatMatMultSymbolic_SeqAIJMKL_SeqAIJMKL_Private(Mat A,const sparse_operation_t transA,Mat B,const sparse_operation_t transB,Mat C) 740431879ecSRichard Tran Mills { 741*190ae7a4SRichard Tran Mills Mat_SeqAIJMKL *a, *b; 742431879ecSRichard Tran Mills sparse_matrix_t csrA, csrB, csrC; 743*190ae7a4SRichard Tran Mills PetscInt nrows,ncols; 744431879ecSRichard Tran Mills PetscErrorCode ierr; 745431879ecSRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 746431879ecSRichard Tran Mills struct matrix_descr descr_type_gen; 747431879ecSRichard Tran Mills PetscObjectState state; 748431879ecSRichard Tran Mills 749431879ecSRichard Tran Mills PetscFunctionBegin; 750*190ae7a4SRichard Tran Mills /* Determine the number of rows and columns that the result matrix C will have. We have to do this ourselves because MKL does 751*190ae7a4SRichard Tran Mills * not handle sparse matrices with zero rows or columns. */ 752*190ae7a4SRichard Tran Mills if (transA == SPARSE_OPERATION_NON_TRANSPOSE) nrows = A->rmap->N; 753*190ae7a4SRichard Tran Mills else nrows = A->cmap->N; 754*190ae7a4SRichard Tran Mills if (transB == SPARSE_OPERATION_NON_TRANSPOSE) ncols = B->cmap->N; 755*190ae7a4SRichard Tran Mills else ncols = B->rmap->N; 756*190ae7a4SRichard Tran Mills 757431879ecSRichard Tran Mills a = (Mat_SeqAIJMKL*)A->spptr; 758431879ecSRichard Tran Mills b = (Mat_SeqAIJMKL*)B->spptr; 759431879ecSRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 760431879ecSRichard Tran Mills if (!a->sparse_optimized || a->state != state) { 761431879ecSRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 762431879ecSRichard Tran Mills } 763431879ecSRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr); 764431879ecSRichard Tran Mills if (!b->sparse_optimized || b->state != state) { 765431879ecSRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(B); 766431879ecSRichard Tran Mills } 767431879ecSRichard Tran Mills csrA = a->csrA; 768431879ecSRichard Tran Mills csrB = b->csrA; 769431879ecSRichard Tran Mills descr_type_gen.type = SPARSE_MATRIX_TYPE_GENERAL; 770431879ecSRichard Tran Mills 771*190ae7a4SRichard Tran Mills if (csrA && csrB) { 772*190ae7a4SRichard Tran Mills stat = mkl_sparse_sp2m(transA,descr_type_gen,csrA,transB,descr_type_gen,csrB,SPARSE_STAGE_FULL_MULT_NO_VAL,&csrC); 773431879ecSRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to complete symbolic stage of sparse matrix-matrix multiply"); 774*190ae7a4SRichard Tran Mills } else { 775*190ae7a4SRichard Tran Mills csrC = PETSC_NULL; 776*190ae7a4SRichard Tran Mills } 777*190ae7a4SRichard Tran Mills 778*190ae7a4SRichard Tran Mills ierr = MatSeqAIJMKL_setup_structure_from_mkl_handle(PETSC_COMM_SELF,csrC,nrows,ncols,C);CHKERRQ(ierr); 779431879ecSRichard Tran Mills 780431879ecSRichard Tran Mills PetscFunctionReturn(0); 781431879ecSRichard Tran Mills } 782431879ecSRichard Tran Mills 783*190ae7a4SRichard Tran Mills PetscErrorCode MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_Private(Mat A,const sparse_operation_t transA,Mat B,const sparse_operation_t transB,Mat C) 784e8be1fc7SRichard Tran Mills { 785e8be1fc7SRichard Tran Mills Mat_SeqAIJMKL *a, *b, *c; 786e8be1fc7SRichard Tran Mills sparse_matrix_t csrA, csrB, csrC; 787e8be1fc7SRichard Tran Mills PetscErrorCode ierr; 788e8be1fc7SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 789e8be1fc7SRichard Tran Mills struct matrix_descr descr_type_gen; 790e8be1fc7SRichard Tran Mills PetscObjectState state; 791e8be1fc7SRichard Tran Mills 792e8be1fc7SRichard Tran Mills PetscFunctionBegin; 793e8be1fc7SRichard Tran Mills a = (Mat_SeqAIJMKL*)A->spptr; 794e8be1fc7SRichard Tran Mills b = (Mat_SeqAIJMKL*)B->spptr; 795e8be1fc7SRichard Tran Mills c = (Mat_SeqAIJMKL*)C->spptr; 796e8be1fc7SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 797e8be1fc7SRichard Tran Mills if (!a->sparse_optimized || a->state != state) { 798e8be1fc7SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 799e8be1fc7SRichard Tran Mills } 800e8be1fc7SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)B,&state);CHKERRQ(ierr); 801e8be1fc7SRichard Tran Mills if (!b->sparse_optimized || b->state != state) { 802e8be1fc7SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(B); 803e8be1fc7SRichard Tran Mills } 804e8be1fc7SRichard Tran Mills csrA = a->csrA; 805e8be1fc7SRichard Tran Mills csrB = b->csrA; 806e8be1fc7SRichard Tran Mills csrC = c->csrA; 807e8be1fc7SRichard Tran Mills descr_type_gen.type = SPARSE_MATRIX_TYPE_GENERAL; 808e8be1fc7SRichard Tran Mills 809*190ae7a4SRichard Tran Mills if (csrA && csrB) { 810*190ae7a4SRichard Tran Mills stat = mkl_sparse_sp2m(transA,descr_type_gen,csrA,transB,descr_type_gen,csrB,SPARSE_STAGE_FINALIZE_MULT,&csrC); 811e8be1fc7SRichard 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"); 812*190ae7a4SRichard Tran Mills } else { 813*190ae7a4SRichard Tran Mills csrC = PETSC_NULL; 814*190ae7a4SRichard Tran Mills } 815e8be1fc7SRichard Tran Mills 816e8be1fc7SRichard Tran Mills /* Have to update the PETSc AIJ representation for matrix C from contents of MKL handle. */ 8174f53af40SRichard Tran Mills ierr = MatSeqAIJMKL_update_from_mkl_handle(C);CHKERRQ(ierr); 818e8be1fc7SRichard Tran Mills 819e8be1fc7SRichard Tran Mills PetscFunctionReturn(0); 820e8be1fc7SRichard Tran Mills } 821e8be1fc7SRichard Tran Mills 822*190ae7a4SRichard Tran Mills PetscErrorCode MatMatMultSymbolic_SeqAIJMKL_SeqAIJMKL(Mat A,Mat B,PetscReal fill,Mat C) 8234f53af40SRichard Tran Mills { 824*190ae7a4SRichard Tran Mills PetscErrorCode ierr; 825*190ae7a4SRichard Tran Mills 826*190ae7a4SRichard Tran Mills PetscFunctionBegin; 827*190ae7a4SRichard Tran Mills ierr = MatMatMultSymbolic_SeqAIJMKL_SeqAIJMKL_Private(A,SPARSE_OPERATION_NON_TRANSPOSE,B,SPARSE_OPERATION_NON_TRANSPOSE,C);CHKERRQ(ierr); 828*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 829*190ae7a4SRichard Tran Mills } 830*190ae7a4SRichard Tran Mills 831*190ae7a4SRichard Tran Mills PetscErrorCode MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL(Mat A,Mat B,Mat C) 832*190ae7a4SRichard Tran Mills { 833*190ae7a4SRichard Tran Mills PetscErrorCode ierr; 834*190ae7a4SRichard Tran Mills 835*190ae7a4SRichard Tran Mills PetscFunctionBegin; 836*190ae7a4SRichard Tran Mills ierr = MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_Private(A,SPARSE_OPERATION_NON_TRANSPOSE,B,SPARSE_OPERATION_NON_TRANSPOSE,C);CHKERRQ(ierr); 837*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 838*190ae7a4SRichard Tran Mills } 839*190ae7a4SRichard Tran Mills 840*190ae7a4SRichard Tran Mills PetscErrorCode MatTransposeMatMultNumeric_SeqAIJMKL_SeqAIJMKL(Mat A,Mat B,Mat C) 841*190ae7a4SRichard Tran Mills { 842*190ae7a4SRichard Tran Mills PetscErrorCode ierr; 843*190ae7a4SRichard Tran Mills 844*190ae7a4SRichard Tran Mills PetscFunctionBegin; 845*190ae7a4SRichard Tran Mills ierr = MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_Private(A,SPARSE_OPERATION_TRANSPOSE,B,SPARSE_OPERATION_NON_TRANSPOSE,C);CHKERRQ(ierr); 846*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 847*190ae7a4SRichard Tran Mills } 848*190ae7a4SRichard Tran Mills 849*190ae7a4SRichard Tran Mills PetscErrorCode MatTransposeMatMultSymbolic_SeqAIJMKL_SeqAIJMKL(Mat A,Mat B,PetscReal fill,Mat C) 850*190ae7a4SRichard Tran Mills { 851*190ae7a4SRichard Tran Mills PetscErrorCode ierr; 852*190ae7a4SRichard Tran Mills 853*190ae7a4SRichard Tran Mills PetscFunctionBegin; 854*190ae7a4SRichard Tran Mills ierr = MatMatMultSymbolic_SeqAIJMKL_SeqAIJMKL_Private(A,SPARSE_OPERATION_TRANSPOSE,B,SPARSE_OPERATION_NON_TRANSPOSE,C);CHKERRQ(ierr); 855*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 856*190ae7a4SRichard Tran Mills } 857*190ae7a4SRichard Tran Mills 858*190ae7a4SRichard Tran Mills PetscErrorCode MatMatTransposeMultSymbolic_SeqAIJMKL_SeqAIJMKL(Mat A,Mat B,PetscReal fill,Mat C) 859*190ae7a4SRichard Tran Mills { 860*190ae7a4SRichard Tran Mills PetscErrorCode ierr; 861*190ae7a4SRichard Tran Mills 862*190ae7a4SRichard Tran Mills PetscFunctionBegin; 863*190ae7a4SRichard Tran Mills ierr = MatMatMultSymbolic_SeqAIJMKL_SeqAIJMKL_Private(A,SPARSE_OPERATION_NON_TRANSPOSE,B,SPARSE_OPERATION_TRANSPOSE,C);CHKERRQ(ierr); 864*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 865*190ae7a4SRichard Tran Mills } 866*190ae7a4SRichard Tran Mills 867*190ae7a4SRichard Tran Mills PetscErrorCode MatMatTransposeMultNumeric_SeqAIJMKL_SeqAIJMKL(Mat A,Mat B,Mat C) 868*190ae7a4SRichard Tran Mills { 869*190ae7a4SRichard Tran Mills PetscErrorCode ierr; 870*190ae7a4SRichard Tran Mills 871*190ae7a4SRichard Tran Mills PetscFunctionBegin; 872*190ae7a4SRichard Tran Mills ierr = MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL_Private(A,SPARSE_OPERATION_NON_TRANSPOSE,B,SPARSE_OPERATION_TRANSPOSE,C);CHKERRQ(ierr); 873*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 874*190ae7a4SRichard Tran Mills } 875*190ae7a4SRichard Tran Mills 876*190ae7a4SRichard Tran Mills static PetscErrorCode MatProductNumeric_AtB_SeqAIJMKL_SeqAIJMKL(Mat C) 877*190ae7a4SRichard Tran Mills { 878*190ae7a4SRichard Tran Mills PetscErrorCode ierr; 879*190ae7a4SRichard Tran Mills Mat_Product *product = C->product; 880*190ae7a4SRichard Tran Mills Mat A = product->A,B = product->B; 881*190ae7a4SRichard Tran Mills 882*190ae7a4SRichard Tran Mills PetscFunctionBegin; 883*190ae7a4SRichard Tran Mills ierr = MatTransposeMatMultNumeric_SeqAIJMKL_SeqAIJMKL(A,B,C);CHKERRQ(ierr); 884*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 885*190ae7a4SRichard Tran Mills } 886*190ae7a4SRichard Tran Mills 887*190ae7a4SRichard Tran Mills static PetscErrorCode MatProductSymbolic_AtB_SeqAIJMKL_SeqAIJMKL(Mat C) 888*190ae7a4SRichard Tran Mills { 889*190ae7a4SRichard Tran Mills PetscErrorCode ierr; 890*190ae7a4SRichard Tran Mills Mat_Product *product = C->product; 891*190ae7a4SRichard Tran Mills Mat A = product->A,B = product->B; 892*190ae7a4SRichard Tran Mills PetscReal fill = product->fill; 893*190ae7a4SRichard Tran Mills 894*190ae7a4SRichard Tran Mills PetscFunctionBegin; 895*190ae7a4SRichard Tran Mills ierr = MatTransposeMatMultSymbolic_SeqAIJMKL_SeqAIJMKL(A,B,fill,C);CHKERRQ(ierr); 896*190ae7a4SRichard Tran Mills 897*190ae7a4SRichard Tran Mills C->ops->productnumeric = MatProductNumeric_AtB_SeqAIJMKL_SeqAIJMKL; 898*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 899*190ae7a4SRichard Tran Mills } 900*190ae7a4SRichard Tran Mills 901*190ae7a4SRichard Tran Mills PetscErrorCode MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL(Mat A,Mat P,Mat C) 902*190ae7a4SRichard Tran Mills { 903*190ae7a4SRichard Tran Mills Mat Ct; 904*190ae7a4SRichard Tran Mills Vec zeros; 9054f53af40SRichard Tran Mills Mat_SeqAIJMKL *a, *p, *c; 9064f53af40SRichard Tran Mills sparse_matrix_t csrA, csrP, csrC; 9074f53af40SRichard Tran Mills PetscBool set, flag; 9084f53af40SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 909b9e1dd46SRichard Tran Mills struct matrix_descr descr_type_sym; 9104f53af40SRichard Tran Mills PetscObjectState state; 9114f53af40SRichard Tran Mills PetscErrorCode ierr; 9124f53af40SRichard Tran Mills 9134f53af40SRichard Tran Mills PetscFunctionBegin; 9144f53af40SRichard Tran Mills ierr = MatIsSymmetricKnown(A,&set,&flag); 915*190ae7a4SRichard Tran Mills if (!set || (set && !flag)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL() called on matrix A not marked as symmetric"); 9164f53af40SRichard Tran Mills 9174f53af40SRichard Tran Mills a = (Mat_SeqAIJMKL*)A->spptr; 9184f53af40SRichard Tran Mills p = (Mat_SeqAIJMKL*)P->spptr; 9194f53af40SRichard Tran Mills c = (Mat_SeqAIJMKL*)C->spptr; 9204f53af40SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 9214f53af40SRichard Tran Mills if (!a->sparse_optimized || a->state != state) { 9224f53af40SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 9234f53af40SRichard Tran Mills } 9244f53af40SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)P,&state);CHKERRQ(ierr); 9254f53af40SRichard Tran Mills if (!p->sparse_optimized || p->state != state) { 9264f53af40SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(P); 9274f53af40SRichard Tran Mills } 9284f53af40SRichard Tran Mills csrA = a->csrA; 9294f53af40SRichard Tran Mills csrP = p->csrA; 9304f53af40SRichard Tran Mills csrC = c->csrA; 931b9e1dd46SRichard Tran Mills descr_type_sym.type = SPARSE_MATRIX_TYPE_SYMMETRIC; 932*190ae7a4SRichard Tran Mills descr_type_sym.mode = SPARSE_FILL_MODE_UPPER; 933b9e1dd46SRichard Tran Mills descr_type_sym.diag = SPARSE_DIAG_NON_UNIT; 9344f53af40SRichard Tran Mills 935f8990b4aSRichard Tran Mills /* Note that the call below won't work for complex matrices. (We protect this when pointers are assigned in MatConvert.) */ 936b9e1dd46SRichard Tran Mills stat = mkl_sparse_sypr(SPARSE_OPERATION_TRANSPOSE,csrP,csrA,descr_type_sym,&csrC,SPARSE_STAGE_FINALIZE_MULT); 9374f53af40SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to finalize mkl_sparse_sypr"); 9384f53af40SRichard Tran Mills 939*190ae7a4SRichard Tran Mills /* Update the PETSc AIJ representation for matrix C from contents of MKL handle. 940*190ae7a4SRichard Tran Mills * This is more complicated than it should be: it turns out that, though mkl_sparse_sypr() will accept a full AIJ/CSR matrix, 941*190ae7a4SRichard Tran Mills * the output matrix only contains the upper or lower triangle (we arbitrarily have chosen upper) of the symmetric matrix. 942*190ae7a4SRichard Tran Mills * We have to fill in the missing portion, which we currently do below by forming the tranpose and performing at MatAXPY 943*190ae7a4SRichard Tran Mills * operation. This may kill any performance benefit of using the optimized mkl_sparse_sypr() routine. Performance might 944*190ae7a4SRichard Tran Mills * improve if we come up with a more efficient way to do this, or we can convince the MKL team to provide an option to output 945*190ae7a4SRichard Tran Mills * the full matrix. */ 9464f53af40SRichard Tran Mills ierr = MatSeqAIJMKL_update_from_mkl_handle(C);CHKERRQ(ierr); 947*190ae7a4SRichard Tran Mills ierr = MatTranspose(C,MAT_INITIAL_MATRIX,&Ct);CHKERRQ(ierr); 948*190ae7a4SRichard Tran Mills ierr = MatCreateVecs(C,&zeros,NULL);CHKERRQ(ierr); 949*190ae7a4SRichard Tran Mills ierr = VecSetFromOptions(zeros);CHKERRQ(ierr); 950*190ae7a4SRichard Tran Mills ierr = VecZeroEntries(zeros);CHKERRQ(ierr); 951*190ae7a4SRichard Tran Mills ierr = MatDiagonalSet(Ct,zeros,INSERT_VALUES);CHKERRQ(ierr); 952*190ae7a4SRichard Tran Mills ierr = MatAXPY(C,1.0,Ct,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); 953*190ae7a4SRichard Tran Mills /* Note: The MatAXPY() call destroys the MatProduct, so we must recreate it. */ 954*190ae7a4SRichard Tran Mills ierr = MatProductCreateWithMat(A,P,PETSC_NULL,C);CHKERRQ(ierr); 955*190ae7a4SRichard Tran Mills C->product->type = MATPRODUCT_PtAP; 956*190ae7a4SRichard Tran Mills ierr = MatSeqAIJMKL_create_mkl_handle(C);CHKERRQ(ierr); 957*190ae7a4SRichard Tran Mills ierr = VecDestroy(&zeros);CHKERRQ(ierr); 958*190ae7a4SRichard Tran Mills ierr = MatDestroy(&Ct);CHKERRQ(ierr); 9594f53af40SRichard Tran Mills 9604f53af40SRichard Tran Mills PetscFunctionReturn(0); 9614f53af40SRichard Tran Mills } 962*190ae7a4SRichard Tran Mills 963*190ae7a4SRichard Tran Mills PetscErrorCode MatProductSymbolic_PtAP_SeqAIJMKL_SeqAIJMKL_SymmetricReal(Mat C) 964*190ae7a4SRichard Tran Mills { 965*190ae7a4SRichard Tran Mills Mat_Product *product = C->product; 966*190ae7a4SRichard Tran Mills Mat A = product->A,P = product->B; 967*190ae7a4SRichard Tran Mills Mat_SeqAIJMKL *a,*p; 968*190ae7a4SRichard Tran Mills sparse_matrix_t csrA,csrP,csrC; 969*190ae7a4SRichard Tran Mills sparse_status_t stat = SPARSE_STATUS_SUCCESS; 970*190ae7a4SRichard Tran Mills struct matrix_descr descr_type_sym; 971*190ae7a4SRichard Tran Mills PetscObjectState state; 972*190ae7a4SRichard Tran Mills PetscErrorCode ierr; 973*190ae7a4SRichard Tran Mills 974*190ae7a4SRichard Tran Mills PetscFunctionBegin; 975*190ae7a4SRichard Tran Mills a = (Mat_SeqAIJMKL*)A->spptr; 976*190ae7a4SRichard Tran Mills p = (Mat_SeqAIJMKL*)P->spptr; 977*190ae7a4SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)A,&state);CHKERRQ(ierr); 978*190ae7a4SRichard Tran Mills if (!a->sparse_optimized || a->state != state) { 979*190ae7a4SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(A); 980*190ae7a4SRichard Tran Mills } 981*190ae7a4SRichard Tran Mills ierr = PetscObjectStateGet((PetscObject)P,&state);CHKERRQ(ierr); 982*190ae7a4SRichard Tran Mills if (!p->sparse_optimized || p->state != state) { 983*190ae7a4SRichard Tran Mills MatSeqAIJMKL_create_mkl_handle(P); 984*190ae7a4SRichard Tran Mills } 985*190ae7a4SRichard Tran Mills csrA = a->csrA; 986*190ae7a4SRichard Tran Mills csrP = p->csrA; 987*190ae7a4SRichard Tran Mills descr_type_sym.type = SPARSE_MATRIX_TYPE_SYMMETRIC; 988*190ae7a4SRichard Tran Mills descr_type_sym.mode = SPARSE_FILL_MODE_UPPER; 989*190ae7a4SRichard Tran Mills descr_type_sym.diag = SPARSE_DIAG_NON_UNIT; 990*190ae7a4SRichard Tran Mills 991*190ae7a4SRichard Tran Mills /* Note that the call below won't work for complex matrices. (We protect this when pointers are assigned in MatConvert.) */ 992*190ae7a4SRichard Tran Mills if (csrP && csrA) { 993*190ae7a4SRichard Tran Mills stat = mkl_sparse_sypr(SPARSE_OPERATION_TRANSPOSE,csrP,csrA,descr_type_sym,&csrC,SPARSE_STAGE_FULL_MULT_NO_VAL); 994*190ae7a4SRichard Tran Mills if (stat != SPARSE_STATUS_SUCCESS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Intel MKL error: unable to perform symbolic mkl_sparse_sypr"); 995*190ae7a4SRichard Tran Mills } else { 996*190ae7a4SRichard Tran Mills csrC = PETSC_NULL; 997*190ae7a4SRichard Tran Mills } 998*190ae7a4SRichard Tran Mills 999*190ae7a4SRichard Tran Mills /* Update the I and J arrays of the PETSc AIJ representation for matrix C from contents of MKL handle. 1000*190ae7a4SRichard Tran Mills * Note that, because mkl_sparse_sypr() only computes one triangle of the symmetric matrix, this representation will only contain 1001*190ae7a4SRichard Tran Mills * the upper triangle of the symmetric matrix. We fix this in MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL(). I believe that leaving things 1002*190ae7a4SRichard Tran Mills * in this incomplete state is OK because the numeric product should follow soon after, but am not certain if this is 1003*190ae7a4SRichard Tran Mills * guaranteed. */ 1004*190ae7a4SRichard Tran Mills ierr = MatSeqAIJMKL_setup_structure_from_mkl_handle(PETSC_COMM_SELF,csrC,P->cmap->N,P->cmap->N,C);CHKERRQ(ierr); 1005*190ae7a4SRichard Tran Mills 1006*190ae7a4SRichard Tran Mills C->ops->productnumeric = MatProductNumeric_PtAP; 1007*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 1008*190ae7a4SRichard Tran Mills } 1009*190ae7a4SRichard Tran Mills 1010*190ae7a4SRichard Tran Mills static PetscErrorCode MatProductSetFromOptions_SeqAIJMKL_AB(Mat C) 1011*190ae7a4SRichard Tran Mills { 1012*190ae7a4SRichard Tran Mills PetscFunctionBegin; 1013*190ae7a4SRichard Tran Mills C->ops->productsymbolic = MatProductSymbolic_AB; 1014*190ae7a4SRichard Tran Mills C->ops->matmultsymbolic = MatMatMultSymbolic_SeqAIJMKL_SeqAIJMKL; 1015*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 1016*190ae7a4SRichard Tran Mills } 1017*190ae7a4SRichard Tran Mills 1018*190ae7a4SRichard Tran Mills static PetscErrorCode MatProductSetFromOptions_SeqAIJMKL_AtB(Mat C) 1019*190ae7a4SRichard Tran Mills { 1020*190ae7a4SRichard Tran Mills PetscFunctionBegin; 1021*190ae7a4SRichard Tran Mills C->ops->productsymbolic = MatProductSymbolic_AtB_SeqAIJMKL_SeqAIJMKL; 1022*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 1023*190ae7a4SRichard Tran Mills } 1024*190ae7a4SRichard Tran Mills 1025*190ae7a4SRichard Tran Mills static PetscErrorCode MatProductSetFromOptions_SeqAIJMKL_ABt(Mat C) 1026*190ae7a4SRichard Tran Mills { 1027*190ae7a4SRichard Tran Mills PetscFunctionBegin; 1028*190ae7a4SRichard Tran Mills C->ops->mattransposemultsymbolic = MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ; 1029*190ae7a4SRichard Tran Mills C->ops->productsymbolic = MatProductSymbolic_ABt; 1030*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 1031*190ae7a4SRichard Tran Mills } 1032*190ae7a4SRichard Tran Mills 1033*190ae7a4SRichard Tran Mills static PetscErrorCode MatProductSetFromOptions_SeqAIJMKL_PtAP(Mat C) 1034*190ae7a4SRichard Tran Mills { 1035*190ae7a4SRichard Tran Mills PetscErrorCode ierr; 1036*190ae7a4SRichard Tran Mills Mat_Product *product = C->product; 1037*190ae7a4SRichard Tran Mills Mat A = product->A; 1038*190ae7a4SRichard Tran Mills PetscBool set, flag; 1039*190ae7a4SRichard Tran Mills 1040*190ae7a4SRichard Tran Mills PetscFunctionBegin; 1041*190ae7a4SRichard Tran Mills #if defined(PETSC_USE_COMPLEX) 1042*190ae7a4SRichard Tran Mills /* By setting C->ops->productsymbolic to NULL, we ensure that MatProductSymbolic_Basic() will be used. 1043*190ae7a4SRichard Tran Mills * We do this in several other locations in this file. This works for the time being, but the _Basic() 1044*190ae7a4SRichard Tran Mills * routines are considered unsafe and may be removed from the MatProduct code in the future. 1045*190ae7a4SRichard Tran Mills * TODO: Add proper MATSEQAIJMKL implementations, instead of relying on the _Basic() routines. */ 1046*190ae7a4SRichard Tran Mills C->ops->productsymbolic = NULL; 1047*190ae7a4SRichard Tran Mills #else 1048*190ae7a4SRichard Tran Mills /* AIJMKL only has an optimized routine for PtAP when A is symmetric and real. */ 1049*190ae7a4SRichard Tran Mills ierr = MatIsSymmetricKnown(A,&set,&flag);CHKERRQ(ierr); 1050*190ae7a4SRichard Tran Mills if (set && flag) { 1051*190ae7a4SRichard Tran Mills C->ops->productsymbolic = MatProductSymbolic_PtAP_SeqAIJMKL_SeqAIJMKL_SymmetricReal; 1052*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 1053*190ae7a4SRichard Tran Mills } else { 1054*190ae7a4SRichard Tran Mills C->ops->productsymbolic = NULL; /* MatProductSymbolic_Basic() will be used. */ 1055*190ae7a4SRichard Tran Mills } 1056*190ae7a4SRichard Tran Mills /* Note that we don't set C->ops->productnumeric here, as this has must happen in MatProductSymbolic_PtAP_SeqAIJMKL_SeqAIJMKL(), 1057*190ae7a4SRichard Tran Mills * depending on whether the algorithm for the general case vs. the real symmetric one is used. */ 1058*190ae7a4SRichard Tran Mills #endif 1059*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 1060*190ae7a4SRichard Tran Mills } 1061*190ae7a4SRichard Tran Mills 1062*190ae7a4SRichard Tran Mills static PetscErrorCode MatProductSetFromOptions_SeqAIJMKL_RARt(Mat C) 1063*190ae7a4SRichard Tran Mills { 1064*190ae7a4SRichard Tran Mills PetscFunctionBegin; 1065*190ae7a4SRichard Tran Mills C->ops->productsymbolic = NULL; /* MatProductSymbolic_Basic() will be used. */ 1066*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 1067*190ae7a4SRichard Tran Mills } 1068*190ae7a4SRichard Tran Mills 1069*190ae7a4SRichard Tran Mills static PetscErrorCode MatProductSetFromOptions_SeqAIJMKL_ABC(Mat C) 1070*190ae7a4SRichard Tran Mills { 1071*190ae7a4SRichard Tran Mills PetscFunctionBegin; 1072*190ae7a4SRichard Tran Mills C->ops->productsymbolic = NULL; /* MatProductSymbolic_Basic() will be used. */ 1073*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 1074*190ae7a4SRichard Tran Mills } 1075*190ae7a4SRichard Tran Mills 1076*190ae7a4SRichard Tran Mills PetscErrorCode MatProductSetFromOptions_SeqAIJMKL(Mat C) 1077*190ae7a4SRichard Tran Mills { 1078*190ae7a4SRichard Tran Mills PetscErrorCode ierr; 1079*190ae7a4SRichard Tran Mills Mat_Product *product = C->product; 1080*190ae7a4SRichard Tran Mills 1081*190ae7a4SRichard Tran Mills PetscFunctionBegin; 1082*190ae7a4SRichard Tran Mills switch (product->type) { 1083*190ae7a4SRichard Tran Mills case MATPRODUCT_AB: 1084*190ae7a4SRichard Tran Mills ierr = MatProductSetFromOptions_SeqAIJMKL_AB(C);CHKERRQ(ierr); 1085*190ae7a4SRichard Tran Mills break; 1086*190ae7a4SRichard Tran Mills case MATPRODUCT_AtB: 1087*190ae7a4SRichard Tran Mills ierr = MatProductSetFromOptions_SeqAIJMKL_AtB(C);CHKERRQ(ierr); 1088*190ae7a4SRichard Tran Mills break; 1089*190ae7a4SRichard Tran Mills case MATPRODUCT_ABt: 1090*190ae7a4SRichard Tran Mills ierr = MatProductSetFromOptions_SeqAIJMKL_ABt(C);CHKERRQ(ierr); 1091*190ae7a4SRichard Tran Mills break; 1092*190ae7a4SRichard Tran Mills case MATPRODUCT_PtAP: 1093*190ae7a4SRichard Tran Mills ierr = MatProductSetFromOptions_SeqAIJMKL_PtAP(C);CHKERRQ(ierr); 1094*190ae7a4SRichard Tran Mills break; 1095*190ae7a4SRichard Tran Mills case MATPRODUCT_RARt: 1096*190ae7a4SRichard Tran Mills ierr = MatProductSetFromOptions_SeqAIJMKL_RARt(C);CHKERRQ(ierr); 1097*190ae7a4SRichard Tran Mills break; 1098*190ae7a4SRichard Tran Mills case MATPRODUCT_ABC: 1099*190ae7a4SRichard Tran Mills ierr = MatProductSetFromOptions_SeqAIJMKL_ABC(C);CHKERRQ(ierr); 1100*190ae7a4SRichard Tran Mills break; 1101*190ae7a4SRichard Tran Mills default: 1102*190ae7a4SRichard Tran Mills break; 1103*190ae7a4SRichard Tran Mills } 1104*190ae7a4SRichard Tran Mills PetscFunctionReturn(0); 1105*190ae7a4SRichard Tran Mills } 1106431879ecSRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE */ 1107*190ae7a4SRichard Tran Mills /* ------------------------ End MatProduct code ------------------------ */ 11084f53af40SRichard Tran Mills 11094a2a386eSRichard Tran Mills /* MatConvert_SeqAIJ_SeqAIJMKL converts a SeqAIJ matrix into a 1110510b72f4SRichard Tran Mills * SeqAIJMKL matrix. This routine is called by the MatCreate_SeqAIJMKL() 11114a2a386eSRichard Tran Mills * routine, but can also be used to convert an assembled SeqAIJ matrix 11124a2a386eSRichard Tran Mills * into a SeqAIJMKL one. */ 11134a2a386eSRichard Tran Mills PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJMKL(Mat A,MatType type,MatReuse reuse,Mat *newmat) 11144a2a386eSRichard Tran Mills { 11154a2a386eSRichard Tran Mills PetscErrorCode ierr; 11164a2a386eSRichard Tran Mills Mat B = *newmat; 11174a2a386eSRichard Tran Mills Mat_SeqAIJMKL *aijmkl; 1118c9d46305SRichard Tran Mills PetscBool set; 1119e9c94282SRichard Tran Mills PetscBool sametype; 11204a2a386eSRichard Tran Mills 11214a2a386eSRichard Tran Mills PetscFunctionBegin; 11224a2a386eSRichard Tran Mills if (reuse == MAT_INITIAL_MATRIX) { 11234a2a386eSRichard Tran Mills ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr); 11244a2a386eSRichard Tran Mills } 11254a2a386eSRichard Tran Mills 1126e9c94282SRichard Tran Mills ierr = PetscObjectTypeCompare((PetscObject)A,type,&sametype);CHKERRQ(ierr); 1127e9c94282SRichard Tran Mills if (sametype) PetscFunctionReturn(0); 1128e9c94282SRichard Tran Mills 11294a2a386eSRichard Tran Mills ierr = PetscNewLog(B,&aijmkl);CHKERRQ(ierr); 11304a2a386eSRichard Tran Mills B->spptr = (void*) aijmkl; 11314a2a386eSRichard Tran Mills 1132df555b71SRichard Tran Mills /* Set function pointers for methods that we inherit from AIJ but override. 1133969800c5SRichard Tran Mills * We also parse some command line options below, since those determine some of the methods we point to. */ 11344a2a386eSRichard Tran Mills B->ops->duplicate = MatDuplicate_SeqAIJMKL; 11354a2a386eSRichard Tran Mills B->ops->assemblyend = MatAssemblyEnd_SeqAIJMKL; 11364a2a386eSRichard Tran Mills B->ops->destroy = MatDestroy_SeqAIJMKL; 1137c9d46305SRichard Tran Mills 11384abfa3b3SRichard Tran Mills aijmkl->sparse_optimized = PETSC_FALSE; 1139ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 1140d995685eSRichard Tran Mills aijmkl->no_SpMV2 = PETSC_FALSE; /* Default to using the SpMV2 routines if our MKL supports them. */ 1141a8327b06SKarl Rupp #else 1142d995685eSRichard Tran Mills aijmkl->no_SpMV2 = PETSC_TRUE; 1143d995685eSRichard Tran Mills #endif 11445b49642aSRichard Tran Mills aijmkl->eager_inspection = PETSC_FALSE; 11454abfa3b3SRichard Tran Mills 11464abfa3b3SRichard Tran Mills /* Parse command line options. */ 1147c9d46305SRichard Tran Mills ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"AIJMKL Options","Mat");CHKERRQ(ierr); 114848292275SRichard 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); 114948292275SRichard 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); 1150c9d46305SRichard Tran Mills ierr = PetscOptionsEnd();CHKERRQ(ierr); 1151ffcab697SRichard Tran Mills #if !defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 1152d995685eSRichard Tran Mills if(!aijmkl->no_SpMV2) { 1153d995685eSRichard 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"); 1154d995685eSRichard Tran Mills aijmkl->no_SpMV2 = PETSC_TRUE; 1155d995685eSRichard Tran Mills } 1156d995685eSRichard Tran Mills #endif 1157c9d46305SRichard Tran Mills 1158ffcab697SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 1159df555b71SRichard Tran Mills B->ops->mult = MatMult_SeqAIJMKL_SpMV2; 1160969800c5SRichard Tran Mills B->ops->multtranspose = MatMultTranspose_SeqAIJMKL_SpMV2; 1161df555b71SRichard Tran Mills B->ops->multadd = MatMultAdd_SeqAIJMKL_SpMV2; 1162969800c5SRichard Tran Mills B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL_SpMV2; 11638a369200SRichard Tran Mills # if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE) 1164*190ae7a4SRichard Tran Mills B->ops->productsetfromoptions = MatProductSetFromOptions_SeqAIJMKL; 1165*190ae7a4SRichard Tran Mills B->ops->matmultsymbolic = MatMatMultSymbolic_SeqAIJMKL_SeqAIJMKL; 1166*190ae7a4SRichard Tran Mills B->ops->matmultnumeric = MatMatMultNumeric_SeqAIJMKL_SeqAIJMKL; 1167*190ae7a4SRichard Tran Mills B->ops->mattransposemultnumeric = MatMatTransposeMultNumeric_SeqAIJMKL_SeqAIJMKL; 1168*190ae7a4SRichard Tran Mills B->ops->transposematmultnumeric = MatTransposeMatMultNumeric_SeqAIJMKL_SeqAIJMKL; 1169ffcab697SRichard Tran Mills # if !defined(PETSC_USE_COMPLEX) 1170*190ae7a4SRichard Tran Mills B->ops->ptapnumeric = MatPtAPNumeric_SeqAIJMKL_SeqAIJMKL; 1171*190ae7a4SRichard Tran Mills # else 1172*190ae7a4SRichard Tran Mills B->ops->ptapnumeric = NULL; 11734f53af40SRichard Tran Mills # endif 1174e8be1fc7SRichard Tran Mills # endif 11751950a7e7SRichard Tran Mills #endif /* PETSC_HAVE_MKL_SPARSE_OPTIMIZE */ 11761950a7e7SRichard Tran Mills 1177213898a2SRichard Tran Mills #if !defined(PETSC_MKL_SPBLAS_DEPRECATED) 1178213898a2SRichard 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 1179213898a2SRichard 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 1180213898a2SRichard Tran Mills * call mkl_sparse_optimize(), which results in the old numerical kernels (without the inspector-executor model) being used. For 1181213898a2SRichard Tran Mills * versions in which the older interface has not been deprecated, we use the old interface. */ 11821950a7e7SRichard Tran Mills if (aijmkl->no_SpMV2) { 11834a2a386eSRichard Tran Mills B->ops->mult = MatMult_SeqAIJMKL; 1184969800c5SRichard Tran Mills B->ops->multtranspose = MatMultTranspose_SeqAIJMKL; 11854a2a386eSRichard Tran Mills B->ops->multadd = MatMultAdd_SeqAIJMKL; 1186969800c5SRichard Tran Mills B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJMKL; 1187c9d46305SRichard Tran Mills } 11881950a7e7SRichard Tran Mills #endif 11894a2a386eSRichard Tran Mills 11904a2a386eSRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijmkl_seqaij_C",MatConvert_SeqAIJMKL_SeqAIJ);CHKERRQ(ierr); 11914a2a386eSRichard Tran Mills 1192*190ae7a4SRichard Tran Mills if(!aijmkl->no_SpMV2) { 1193*190ae7a4SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_OPTIMIZE) 1194*190ae7a4SRichard Tran Mills #if defined(PETSC_HAVE_MKL_SPARSE_SP2M_FEATURE) 1195*190ae7a4SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqaijmkl_seqaijmkl_C",MatProductSetFromOptions_SeqAIJMKL);CHKERRQ(ierr); 1196*190ae7a4SRichard Tran Mills #endif 1197*190ae7a4SRichard Tran Mills #endif 1198*190ae7a4SRichard Tran Mills } 1199*190ae7a4SRichard Tran Mills 12004a2a386eSRichard Tran Mills ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJMKL);CHKERRQ(ierr); 12014a2a386eSRichard Tran Mills *newmat = B; 12024a2a386eSRichard Tran Mills PetscFunctionReturn(0); 12034a2a386eSRichard Tran Mills } 12044a2a386eSRichard Tran Mills 12054a2a386eSRichard Tran Mills /*@C 12064a2a386eSRichard Tran Mills MatCreateSeqAIJMKL - Creates a sparse matrix of type SEQAIJMKL. 12074a2a386eSRichard Tran Mills This type inherits from AIJ and is largely identical, but uses sparse BLAS 12084a2a386eSRichard Tran Mills routines from Intel MKL whenever possible. 120990147e49SRichard Tran Mills If the installed version of MKL supports the "SpMV2" sparse 121090147e49SRichard Tran Mills inspector-executor routines, then those are used by default. 1211597ee276SRichard Tran Mills MatMult, MatMultAdd, MatMultTranspose, MatMultTransposeAdd, MatMatMult, MatTransposeMatMult, and MatPtAP (for 1212597ee276SRichard Tran Mills symmetric A) operations are currently supported. 1213597ee276SRichard Tran Mills Note that MKL version 18, update 2 or later is required for MatPtAP/MatPtAPNumeric and MatMatMultNumeric. 121490147e49SRichard Tran Mills 1215d083f849SBarry Smith Collective 12164a2a386eSRichard Tran Mills 12174a2a386eSRichard Tran Mills Input Parameters: 12184a2a386eSRichard Tran Mills + comm - MPI communicator, set to PETSC_COMM_SELF 12194a2a386eSRichard Tran Mills . m - number of rows 12204a2a386eSRichard Tran Mills . n - number of columns 12214a2a386eSRichard Tran Mills . nz - number of nonzeros per row (same for all rows) 12224a2a386eSRichard Tran Mills - nnz - array containing the number of nonzeros in the various rows 12234a2a386eSRichard Tran Mills (possibly different for each row) or NULL 12244a2a386eSRichard Tran Mills 12254a2a386eSRichard Tran Mills Output Parameter: 12264a2a386eSRichard Tran Mills . A - the matrix 12274a2a386eSRichard Tran Mills 122890147e49SRichard Tran Mills Options Database Keys: 122966b7eeb6SRichard Tran Mills + -mat_aijmkl_no_spmv2 - disable use of the SpMV2 inspector-executor routines 123066b7eeb6SRichard 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 123190147e49SRichard Tran Mills 12324a2a386eSRichard Tran Mills Notes: 12334a2a386eSRichard Tran Mills If nnz is given then nz is ignored 12344a2a386eSRichard Tran Mills 12354a2a386eSRichard Tran Mills Level: intermediate 12364a2a386eSRichard Tran Mills 12374a2a386eSRichard Tran Mills .seealso: MatCreate(), MatCreateMPIAIJMKL(), MatSetValues() 12384a2a386eSRichard Tran Mills @*/ 12394a2a386eSRichard Tran Mills PetscErrorCode MatCreateSeqAIJMKL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 12404a2a386eSRichard Tran Mills { 12414a2a386eSRichard Tran Mills PetscErrorCode ierr; 12424a2a386eSRichard Tran Mills 12434a2a386eSRichard Tran Mills PetscFunctionBegin; 12444a2a386eSRichard Tran Mills ierr = MatCreate(comm,A);CHKERRQ(ierr); 12454a2a386eSRichard Tran Mills ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 12464a2a386eSRichard Tran Mills ierr = MatSetType(*A,MATSEQAIJMKL);CHKERRQ(ierr); 12474a2a386eSRichard Tran Mills ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr); 12484a2a386eSRichard Tran Mills PetscFunctionReturn(0); 12494a2a386eSRichard Tran Mills } 12504a2a386eSRichard Tran Mills 12514a2a386eSRichard Tran Mills PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJMKL(Mat A) 12524a2a386eSRichard Tran Mills { 12534a2a386eSRichard Tran Mills PetscErrorCode ierr; 12544a2a386eSRichard Tran Mills 12554a2a386eSRichard Tran Mills PetscFunctionBegin; 12564a2a386eSRichard Tran Mills ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr); 12574a2a386eSRichard Tran Mills ierr = MatConvert_SeqAIJ_SeqAIJMKL(A,MATSEQAIJMKL,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr); 12584a2a386eSRichard Tran Mills PetscFunctionReturn(0); 12594a2a386eSRichard Tran Mills } 1260