1b97cf49bSBarry Smith /* 2b97cf49bSBarry Smith Defines the basic matrix operations for the ADJ adjacency list matrix data-structure. 3b97cf49bSBarry Smith */ 43eda8832SBarry Smith #include "src/mat/impls/adj/mpi/mpiadj.h" 5325e03aeSBarry Smith #include "petscsys.h" 6b97cf49bSBarry Smith 74a2ae208SSatish Balay #undef __FUNCT__ 84a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAdj_ASCII" 9dfbe8321SBarry Smith PetscErrorCode MatView_MPIAdj_ASCII(Mat A,PetscViewer viewer) 10b97cf49bSBarry Smith { 113eda8832SBarry Smith Mat_MPIAdj *a = (Mat_MPIAdj*)A->data; 12dfbe8321SBarry Smith PetscErrorCode ierr; 13dfbe8321SBarry Smith int i,j,m = A->m; 14fb9695e5SSatish Balay char *name; 15ce1411ecSBarry Smith PetscViewerFormat format; 16b97cf49bSBarry Smith 17433994e6SBarry Smith PetscFunctionBegin; 183a7fca6bSBarry Smith ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr); 19b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 20fb9695e5SSatish Balay if (format == PETSC_VIEWER_ASCII_INFO) { 213a40ed3dSBarry Smith PetscFunctionReturn(0); 22fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_MATLAB) { 23ffac6cdbSBarry Smith SETERRQ(PETSC_ERR_SUP,"Matlab format not supported"); 240752156aSBarry Smith } else { 25b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 26b97cf49bSBarry Smith for (i=0; i<m; i++) { 27b0a32e0cSBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"row %d:",i+a->rstart);CHKERRQ(ierr); 28b97cf49bSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 29b0a32e0cSBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer," %d ",a->j[j]);CHKERRQ(ierr); 300752156aSBarry Smith } 31b0a32e0cSBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr); 32b97cf49bSBarry Smith } 33b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 34b97cf49bSBarry Smith } 35b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 363a40ed3dSBarry Smith PetscFunctionReturn(0); 37b97cf49bSBarry Smith } 38b97cf49bSBarry Smith 394a2ae208SSatish Balay #undef __FUNCT__ 404a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAdj" 41dfbe8321SBarry Smith PetscErrorCode MatView_MPIAdj(Mat A,PetscViewer viewer) 42b97cf49bSBarry Smith { 43dfbe8321SBarry Smith PetscErrorCode ierr; 4432077d6dSBarry Smith PetscTruth iascii; 45b97cf49bSBarry Smith 46433994e6SBarry Smith PetscFunctionBegin; 4732077d6dSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 4832077d6dSBarry Smith if (iascii) { 493eda8832SBarry Smith ierr = MatView_MPIAdj_ASCII(A,viewer);CHKERRQ(ierr); 505cd90555SBarry Smith } else { 5129bbc08cSBarry Smith SETERRQ1(1,"Viewer type %s not supported by MPIAdj",((PetscObject)viewer)->type_name); 52b97cf49bSBarry Smith } 533a40ed3dSBarry Smith PetscFunctionReturn(0); 54b97cf49bSBarry Smith } 55b97cf49bSBarry Smith 564a2ae208SSatish Balay #undef __FUNCT__ 574a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAdj" 58dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAdj(Mat mat) 59b97cf49bSBarry Smith { 603eda8832SBarry Smith Mat_MPIAdj *a = (Mat_MPIAdj*)mat->data; 61dfbe8321SBarry Smith PetscErrorCode ierr; 6294d884c6SBarry Smith 6394d884c6SBarry Smith PetscFunctionBegin; 64aa482453SBarry Smith #if defined(PETSC_USE_LOG) 65b0a32e0cSBarry Smith PetscLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d, NZ=%d",mat->m,mat->n,a->nz); 66b97cf49bSBarry Smith #endif 67606d414cSSatish Balay if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);} 680400557aSBarry Smith if (a->freeaij) { 69606d414cSSatish Balay ierr = PetscFree(a->i);CHKERRQ(ierr); 70606d414cSSatish Balay ierr = PetscFree(a->j);CHKERRQ(ierr); 711198db28SBarry Smith if (a->values) {ierr = PetscFree(a->values);CHKERRQ(ierr);} 720400557aSBarry Smith } 730400557aSBarry Smith ierr = PetscFree(a->rowners);CHKERRQ(ierr); 741198db28SBarry Smith ierr = PetscFree(a);CHKERRQ(ierr); 75901853e0SKris Buschelman 76901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAdjSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr); 773a40ed3dSBarry Smith PetscFunctionReturn(0); 78b97cf49bSBarry Smith } 79b97cf49bSBarry Smith 804a2ae208SSatish Balay #undef __FUNCT__ 814a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAdj" 82dfbe8321SBarry Smith PetscErrorCode MatSetOption_MPIAdj(Mat A,MatOption op) 83b97cf49bSBarry Smith { 843eda8832SBarry Smith Mat_MPIAdj *a = (Mat_MPIAdj*)A->data; 85b97cf49bSBarry Smith 86433994e6SBarry Smith PetscFunctionBegin; 8712c028f9SKris Buschelman switch (op) { 8877e54ba9SKris Buschelman case MAT_SYMMETRIC: 8912c028f9SKris Buschelman case MAT_STRUCTURALLY_SYMMETRIC: 909a4540c5SBarry Smith case MAT_HERMITIAN: 91b97cf49bSBarry Smith a->symmetric = PETSC_TRUE; 9212c028f9SKris Buschelman break; 939a4540c5SBarry Smith case MAT_NOT_SYMMETRIC: 949a4540c5SBarry Smith case MAT_NOT_STRUCTURALLY_SYMMETRIC: 959a4540c5SBarry Smith case MAT_NOT_HERMITIAN: 969a4540c5SBarry Smith a->symmetric = PETSC_FALSE; 979a4540c5SBarry Smith break; 989a4540c5SBarry Smith case MAT_SYMMETRY_ETERNAL: 999a4540c5SBarry Smith case MAT_NOT_SYMMETRY_ETERNAL: 1009a4540c5SBarry Smith break; 10112c028f9SKris Buschelman default: 102b0a32e0cSBarry Smith PetscLogInfo(A,"MatSetOption_MPIAdj:Option ignored\n"); 10312c028f9SKris Buschelman break; 104b97cf49bSBarry Smith } 1053a40ed3dSBarry Smith PetscFunctionReturn(0); 106b97cf49bSBarry Smith } 107b97cf49bSBarry Smith 108b97cf49bSBarry Smith 109b97cf49bSBarry Smith /* 110b97cf49bSBarry Smith Adds diagonal pointers to sparse matrix structure. 111b97cf49bSBarry Smith */ 112b97cf49bSBarry Smith 1134a2ae208SSatish Balay #undef __FUNCT__ 1144a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_MPIAdj" 115dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_MPIAdj(Mat A) 116b97cf49bSBarry Smith { 1173eda8832SBarry Smith Mat_MPIAdj *a = (Mat_MPIAdj*)A->data; 1186849ba73SBarry Smith PetscErrorCode ierr; 1196849ba73SBarry Smith int i,j,*diag,m = A->m; 120b97cf49bSBarry Smith 121433994e6SBarry Smith PetscFunctionBegin; 122b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&diag);CHKERRQ(ierr); 123b0a32e0cSBarry Smith PetscLogObjectMemory(A,(m+1)*sizeof(int)); 124273d9f13SBarry Smith for (i=0; i<A->m; i++) { 125b97cf49bSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 126b97cf49bSBarry Smith if (a->j[j] == i) { 127b97cf49bSBarry Smith diag[i] = j; 128b97cf49bSBarry Smith break; 129b97cf49bSBarry Smith } 130b97cf49bSBarry Smith } 131b97cf49bSBarry Smith } 132b97cf49bSBarry Smith a->diag = diag; 1333a40ed3dSBarry Smith PetscFunctionReturn(0); 134b97cf49bSBarry Smith } 135b97cf49bSBarry Smith 1364a2ae208SSatish Balay #undef __FUNCT__ 1374a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAdj" 138dfbe8321SBarry Smith PetscErrorCode MatGetRow_MPIAdj(Mat A,int row,int *nz,int **idx,PetscScalar **v) 139b97cf49bSBarry Smith { 1403eda8832SBarry Smith Mat_MPIAdj *a = (Mat_MPIAdj*)A->data; 141b97cf49bSBarry Smith int *itmp; 142b97cf49bSBarry Smith 143433994e6SBarry Smith PetscFunctionBegin; 1440752156aSBarry Smith row -= a->rstart; 1450752156aSBarry Smith 146273d9f13SBarry Smith if (row < 0 || row >= A->m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row out of range"); 147b97cf49bSBarry Smith 148b97cf49bSBarry Smith *nz = a->i[row+1] - a->i[row]; 149b97cf49bSBarry Smith if (v) *v = PETSC_NULL; 150b97cf49bSBarry Smith if (idx) { 151b97cf49bSBarry Smith itmp = a->j + a->i[row]; 152b97cf49bSBarry Smith if (*nz) { 153b97cf49bSBarry Smith *idx = itmp; 154b97cf49bSBarry Smith } 155b97cf49bSBarry Smith else *idx = 0; 156b97cf49bSBarry Smith } 1573a40ed3dSBarry Smith PetscFunctionReturn(0); 158b97cf49bSBarry Smith } 159b97cf49bSBarry Smith 1604a2ae208SSatish Balay #undef __FUNCT__ 1614a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAdj" 162dfbe8321SBarry Smith PetscErrorCode MatRestoreRow_MPIAdj(Mat A,int row,int *nz,int **idx,PetscScalar **v) 163b97cf49bSBarry Smith { 164433994e6SBarry Smith PetscFunctionBegin; 1653a40ed3dSBarry Smith PetscFunctionReturn(0); 166b97cf49bSBarry Smith } 167b97cf49bSBarry Smith 1684a2ae208SSatish Balay #undef __FUNCT__ 1694a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_MPIAdj" 170dfbe8321SBarry Smith PetscErrorCode MatGetBlockSize_MPIAdj(Mat A,int *bs) 171b97cf49bSBarry Smith { 172433994e6SBarry Smith PetscFunctionBegin; 173b97cf49bSBarry Smith *bs = 1; 1743a40ed3dSBarry Smith PetscFunctionReturn(0); 175b97cf49bSBarry Smith } 176b97cf49bSBarry Smith 177b97cf49bSBarry Smith 1784a2ae208SSatish Balay #undef __FUNCT__ 1794a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAdj" 180dfbe8321SBarry Smith PetscErrorCode MatEqual_MPIAdj(Mat A,Mat B,PetscTruth* flg) 181b97cf49bSBarry Smith { 1823eda8832SBarry Smith Mat_MPIAdj *a = (Mat_MPIAdj *)A->data,*b = (Mat_MPIAdj *)B->data; 183dfbe8321SBarry Smith PetscErrorCode ierr; 1840f5bd95cSBarry Smith PetscTruth flag; 185b97cf49bSBarry Smith 186433994e6SBarry Smith PetscFunctionBegin; 187b97cf49bSBarry Smith /* If the matrix dimensions are not equal,or no of nonzeros */ 188273d9f13SBarry Smith if ((A->m != B->m) ||(a->nz != b->nz)) { 1890f5bd95cSBarry Smith flag = PETSC_FALSE; 190b97cf49bSBarry Smith } 191b97cf49bSBarry Smith 192b97cf49bSBarry Smith /* if the a->i are the same */ 193273d9f13SBarry Smith ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(int),&flag);CHKERRQ(ierr); 194b97cf49bSBarry Smith 195b97cf49bSBarry Smith /* if a->j are the same */ 1960f5bd95cSBarry Smith ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(int),&flag);CHKERRQ(ierr); 197b97cf49bSBarry Smith 198ca161407SBarry Smith ierr = MPI_Allreduce(&flag,flg,1,MPI_INT,MPI_LAND,A->comm);CHKERRQ(ierr); 1993a40ed3dSBarry Smith PetscFunctionReturn(0); 200b97cf49bSBarry Smith } 201b97cf49bSBarry Smith 2024a2ae208SSatish Balay #undef __FUNCT__ 2034a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_MPIAdj" 204dfbe8321SBarry Smith PetscErrorCode MatGetRowIJ_MPIAdj(Mat A,int oshift,PetscTruth symmetric,int *m,int *ia[],int *ja[],PetscTruth *done) 2056cd91f64SBarry Smith { 206dfbe8321SBarry Smith PetscErrorCode ierr; 207dfbe8321SBarry Smith int size,i; 2086cd91f64SBarry Smith Mat_MPIAdj *a = (Mat_MPIAdj *)A->data; 2096cd91f64SBarry Smith 2106cd91f64SBarry Smith PetscFunctionBegin; 2116cd91f64SBarry Smith ierr = MPI_Comm_size(A->comm,&size);CHKERRQ(ierr); 2126cd91f64SBarry Smith if (size > 1) {*done = PETSC_FALSE; PetscFunctionReturn(0);} 2136cd91f64SBarry Smith *m = A->m; 2146cd91f64SBarry Smith *ia = a->i; 2156cd91f64SBarry Smith *ja = a->j; 2166cd91f64SBarry Smith *done = PETSC_TRUE; 217d42735a1SBarry Smith if (oshift) { 218d42735a1SBarry Smith for (i=0; i<(*ia)[*m]; i++) { 219d42735a1SBarry Smith (*ja)[i]++; 220d42735a1SBarry Smith } 221d42735a1SBarry Smith for (i=0; i<=(*m); i++) (*ia)[i]++; 222d42735a1SBarry Smith } 223d42735a1SBarry Smith PetscFunctionReturn(0); 224d42735a1SBarry Smith } 225d42735a1SBarry Smith 2264a2ae208SSatish Balay #undef __FUNCT__ 2274a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_MPIAdj" 228dfbe8321SBarry Smith PetscErrorCode MatRestoreRowIJ_MPIAdj(Mat A,int oshift,PetscTruth symmetric,int *m,int *ia[],int *ja[],PetscTruth *done) 229d42735a1SBarry Smith { 230d42735a1SBarry Smith int i; 231d42735a1SBarry Smith Mat_MPIAdj *a = (Mat_MPIAdj *)A->data; 232d42735a1SBarry Smith 233d42735a1SBarry Smith PetscFunctionBegin; 234*e005ede5SBarry Smith if (ia && a->i != *ia) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ia passed back is not one obtained with MatGetRowIJ()"); 235*e005ede5SBarry Smith if (ja && a->j != *ja) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ja passed back is not one obtained with MatGetRowIJ()"); 236d42735a1SBarry Smith if (oshift) { 237d42735a1SBarry Smith for (i=0; i<=(*m); i++) (*ia)[i]--; 238d42735a1SBarry Smith for (i=0; i<(*ia)[*m]; i++) { 239d42735a1SBarry Smith (*ja)[i]--; 240d42735a1SBarry Smith } 241d42735a1SBarry Smith } 2426cd91f64SBarry Smith PetscFunctionReturn(0); 2436cd91f64SBarry Smith } 244b97cf49bSBarry Smith 245b97cf49bSBarry Smith /* -------------------------------------------------------------------*/ 2460b3b1487SBarry Smith static struct _MatOps MatOps_Values = {0, 2473eda8832SBarry Smith MatGetRow_MPIAdj, 2483eda8832SBarry Smith MatRestoreRow_MPIAdj, 249b97cf49bSBarry Smith 0, 25097304618SKris Buschelman /* 4*/ 0, 251b97cf49bSBarry Smith 0, 252b97cf49bSBarry Smith 0, 253b97cf49bSBarry Smith 0, 2540b3b1487SBarry Smith 0, 2550b3b1487SBarry Smith 0, 25697304618SKris Buschelman /*10*/ 0, 2570b3b1487SBarry Smith 0, 2580b3b1487SBarry Smith 0, 2590b3b1487SBarry Smith 0, 2600b3b1487SBarry Smith 0, 26197304618SKris Buschelman /*15*/ 0, 2623eda8832SBarry Smith MatEqual_MPIAdj, 2630b3b1487SBarry Smith 0, 2640b3b1487SBarry Smith 0, 2650b3b1487SBarry Smith 0, 26697304618SKris Buschelman /*20*/ 0, 2670b3b1487SBarry Smith 0, 2680b3b1487SBarry Smith 0, 2693eda8832SBarry Smith MatSetOption_MPIAdj, 2700b3b1487SBarry Smith 0, 27197304618SKris Buschelman /*25*/ 0, 2720b3b1487SBarry Smith 0, 2730b3b1487SBarry Smith 0, 2740b3b1487SBarry Smith 0, 2750b3b1487SBarry Smith 0, 27697304618SKris Buschelman /*30*/ 0, 2770b3b1487SBarry Smith 0, 278273d9f13SBarry Smith 0, 279273d9f13SBarry Smith 0, 2800b3b1487SBarry Smith 0, 28197304618SKris Buschelman /*35*/ 0, 2820b3b1487SBarry Smith 0, 2830b3b1487SBarry Smith 0, 2840b3b1487SBarry Smith 0, 2850b3b1487SBarry Smith 0, 28697304618SKris Buschelman /*40*/ 0, 2870b3b1487SBarry Smith 0, 2880b3b1487SBarry Smith 0, 2890b3b1487SBarry Smith 0, 2900b3b1487SBarry Smith 0, 29197304618SKris Buschelman /*45*/ 0, 2920b3b1487SBarry Smith 0, 2930b3b1487SBarry Smith 0, 2940b3b1487SBarry Smith 0, 2950b3b1487SBarry Smith 0, 29697304618SKris Buschelman /*50*/ MatGetBlockSize_MPIAdj, 2976cd91f64SBarry Smith MatGetRowIJ_MPIAdj, 298d42735a1SBarry Smith MatRestoreRowIJ_MPIAdj, 299b97cf49bSBarry Smith 0, 300b97cf49bSBarry Smith 0, 30197304618SKris Buschelman /*55*/ 0, 302b97cf49bSBarry Smith 0, 3030752156aSBarry Smith 0, 3040752156aSBarry Smith 0, 3050b3b1487SBarry Smith 0, 30697304618SKris Buschelman /*60*/ 0, 307b9b97703SBarry Smith MatDestroy_MPIAdj, 308b9b97703SBarry Smith MatView_MPIAdj, 30997304618SKris Buschelman MatGetPetscMaps_Petsc, 31097304618SKris Buschelman 0, 31197304618SKris Buschelman /*65*/ 0, 31297304618SKris Buschelman 0, 31397304618SKris Buschelman 0, 31497304618SKris Buschelman 0, 31597304618SKris Buschelman 0, 31697304618SKris Buschelman /*70*/ 0, 31797304618SKris Buschelman 0, 31897304618SKris Buschelman 0, 31997304618SKris Buschelman 0, 32097304618SKris Buschelman 0, 32197304618SKris Buschelman /*75*/ 0, 32297304618SKris Buschelman 0, 32397304618SKris Buschelman 0, 32497304618SKris Buschelman 0, 32597304618SKris Buschelman 0, 32697304618SKris Buschelman /*80*/ 0, 32797304618SKris Buschelman 0, 32897304618SKris Buschelman 0, 32997304618SKris Buschelman 0, 330865e5f61SKris Buschelman 0, 331865e5f61SKris Buschelman /*85*/ 0, 332865e5f61SKris Buschelman 0, 333865e5f61SKris Buschelman 0, 334865e5f61SKris Buschelman 0, 335865e5f61SKris Buschelman 0, 336865e5f61SKris Buschelman /*90*/ 0, 337865e5f61SKris Buschelman 0, 338865e5f61SKris Buschelman 0, 339865e5f61SKris Buschelman 0, 340865e5f61SKris Buschelman 0, 341865e5f61SKris Buschelman /*95*/ 0, 342865e5f61SKris Buschelman 0, 343865e5f61SKris Buschelman 0, 344865e5f61SKris Buschelman 0}; 345b97cf49bSBarry Smith 346a23d5eceSKris Buschelman EXTERN_C_BEGIN 347a23d5eceSKris Buschelman #undef __FUNCT__ 348a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAdjSetPreallocation_MPIAdj" 349dfbe8321SBarry Smith PetscErrorCode MatMPIAdjSetPreallocation_MPIAdj(Mat B,int *i,int *j,int *values) 350a23d5eceSKris Buschelman { 351a23d5eceSKris Buschelman Mat_MPIAdj *b = (Mat_MPIAdj *)B->data; 352dfbe8321SBarry Smith PetscErrorCode ierr; 353a23d5eceSKris Buschelman #if defined(PETSC_USE_BOPT_g) 354a23d5eceSKris Buschelman int ii; 355a23d5eceSKris Buschelman #endif 356a23d5eceSKris Buschelman 357a23d5eceSKris Buschelman PetscFunctionBegin; 358a23d5eceSKris Buschelman B->preallocated = PETSC_TRUE; 359a23d5eceSKris Buschelman #if defined(PETSC_USE_BOPT_g) 360a23d5eceSKris Buschelman if (i[0] != 0) SETERRQ1(1,"First i[] index must be zero, instead it is %d\n",i[0]); 361a23d5eceSKris Buschelman for (ii=1; ii<B->m; ii++) { 362a23d5eceSKris Buschelman if (i[ii] < 0 || i[ii] < i[ii-1]) { 363a23d5eceSKris Buschelman SETERRQ4(1,"i[%d]=%d index is out of range: i[%d]=%d",ii,i[ii],ii-1,i[ii-1]); 364a23d5eceSKris Buschelman } 365a23d5eceSKris Buschelman } 366a23d5eceSKris Buschelman for (ii=0; ii<i[B->m]; ii++) { 367a23d5eceSKris Buschelman if (j[ii] < 0 || j[ii] >= B->N) { 368a23d5eceSKris Buschelman SETERRQ2(1,"Column index %d out of range %d\n",ii,j[ii]); 369a23d5eceSKris Buschelman } 370a23d5eceSKris Buschelman } 371a23d5eceSKris Buschelman #endif 372a23d5eceSKris Buschelman 373a23d5eceSKris Buschelman b->j = j; 374a23d5eceSKris Buschelman b->i = i; 375a23d5eceSKris Buschelman b->values = values; 376a23d5eceSKris Buschelman 377a23d5eceSKris Buschelman b->nz = i[B->m]; 378a23d5eceSKris Buschelman b->diag = 0; 379a23d5eceSKris Buschelman b->symmetric = PETSC_FALSE; 380a23d5eceSKris Buschelman b->freeaij = PETSC_TRUE; 381a23d5eceSKris Buschelman 382a23d5eceSKris Buschelman ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 383a23d5eceSKris Buschelman ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 384a23d5eceSKris Buschelman PetscFunctionReturn(0); 385a23d5eceSKris Buschelman } 386a23d5eceSKris Buschelman EXTERN_C_END 387b97cf49bSBarry Smith 3880bad9183SKris Buschelman /*MC 389fafad747SKris Buschelman MATMPIADJ - MATMPIADJ = "mpiadj" - A matrix type to be used for distributed adjacency matrices, 3900bad9183SKris Buschelman intended for use constructing orderings and partitionings. 3910bad9183SKris Buschelman 3920bad9183SKris Buschelman Level: beginner 3930bad9183SKris Buschelman 3940bad9183SKris Buschelman .seealso: MatCreateMPIAdj 3950bad9183SKris Buschelman M*/ 3960bad9183SKris Buschelman 397273d9f13SBarry Smith EXTERN_C_BEGIN 3984a2ae208SSatish Balay #undef __FUNCT__ 3994a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIAdj" 400dfbe8321SBarry Smith PetscErrorCode MatCreate_MPIAdj(Mat B) 401273d9f13SBarry Smith { 402273d9f13SBarry Smith Mat_MPIAdj *b; 4036849ba73SBarry Smith PetscErrorCode ierr; 4046849ba73SBarry Smith int ii,size,rank; 405273d9f13SBarry Smith 406273d9f13SBarry Smith PetscFunctionBegin; 407273d9f13SBarry Smith ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr); 408273d9f13SBarry Smith ierr = MPI_Comm_rank(B->comm,&rank);CHKERRQ(ierr); 409273d9f13SBarry Smith 410b0a32e0cSBarry Smith ierr = PetscNew(Mat_MPIAdj,&b);CHKERRQ(ierr); 411b0a32e0cSBarry Smith B->data = (void*)b; 412273d9f13SBarry Smith ierr = PetscMemzero(b,sizeof(Mat_MPIAdj));CHKERRQ(ierr); 413273d9f13SBarry Smith ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 414273d9f13SBarry Smith B->factor = 0; 415273d9f13SBarry Smith B->lupivotthreshold = 1.0; 416273d9f13SBarry Smith B->mapping = 0; 417273d9f13SBarry Smith B->assembled = PETSC_FALSE; 418273d9f13SBarry Smith 419273d9f13SBarry Smith ierr = MPI_Allreduce(&B->m,&B->M,1,MPI_INT,MPI_SUM,B->comm);CHKERRQ(ierr); 420273d9f13SBarry Smith B->n = B->N; 421273d9f13SBarry Smith 422273d9f13SBarry Smith /* the information in the maps duplicates the information computed below, eventually 423273d9f13SBarry Smith we should remove the duplicate information that is not contained in the maps */ 4248a124369SBarry Smith ierr = PetscMapCreateMPI(B->comm,B->m,B->M,&B->rmap);CHKERRQ(ierr); 425273d9f13SBarry Smith /* we don't know the "local columns" so just use the row information :-(*/ 4268a124369SBarry Smith ierr = PetscMapCreateMPI(B->comm,B->m,B->M,&B->cmap);CHKERRQ(ierr); 427273d9f13SBarry Smith 428b0a32e0cSBarry Smith ierr = PetscMalloc((size+1)*sizeof(int),&b->rowners);CHKERRQ(ierr); 429b0a32e0cSBarry Smith PetscLogObjectMemory(B,(size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAdj)); 430273d9f13SBarry Smith ierr = MPI_Allgather(&B->m,1,MPI_INT,b->rowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr); 431273d9f13SBarry Smith b->rowners[0] = 0; 432273d9f13SBarry Smith for (ii=2; ii<=size; ii++) { 433273d9f13SBarry Smith b->rowners[ii] += b->rowners[ii-1]; 434273d9f13SBarry Smith } 435273d9f13SBarry Smith b->rstart = b->rowners[rank]; 436273d9f13SBarry Smith b->rend = b->rowners[rank+1]; 437a23d5eceSKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAdjSetPreallocation_C", 438a23d5eceSKris Buschelman "MatMPIAdjSetPreallocation_MPIAdj", 439a23d5eceSKris Buschelman MatMPIAdjSetPreallocation_MPIAdj);CHKERRQ(ierr); 440273d9f13SBarry Smith PetscFunctionReturn(0); 441273d9f13SBarry Smith } 442273d9f13SBarry Smith EXTERN_C_END 443273d9f13SBarry Smith 4444a2ae208SSatish Balay #undef __FUNCT__ 4454a2ae208SSatish Balay #define __FUNCT__ "MatMPIAdjSetPreallocation" 446a23d5eceSKris Buschelman /*@C 447a23d5eceSKris Buschelman MatMPIAdjSetPreallocation - Sets the array used for storing the matrix elements 448a23d5eceSKris Buschelman 449a23d5eceSKris Buschelman Collective on MPI_Comm 450a23d5eceSKris Buschelman 451a23d5eceSKris Buschelman Input Parameters: 452a23d5eceSKris Buschelman + A - the matrix 453a23d5eceSKris Buschelman . i - the indices into j for the start of each row 454a23d5eceSKris Buschelman . j - the column indices for each row (sorted for each row). 455a23d5eceSKris Buschelman The indices in i and j start with zero (NOT with one). 456a23d5eceSKris Buschelman - values - [optional] edge weights 457a23d5eceSKris Buschelman 458a23d5eceSKris Buschelman Level: intermediate 459a23d5eceSKris Buschelman 460a23d5eceSKris Buschelman .seealso: MatCreate(), MatCreateMPIAdj(), MatSetValues() 461a23d5eceSKris Buschelman @*/ 462dfbe8321SBarry Smith PetscErrorCode MatMPIAdjSetPreallocation(Mat B,int *i,int *j,int *values) 463273d9f13SBarry Smith { 464dfbe8321SBarry Smith PetscErrorCode ierr,(*f)(Mat,int*,int*,int*); 465273d9f13SBarry Smith 466273d9f13SBarry Smith PetscFunctionBegin; 467a23d5eceSKris Buschelman ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIAdjSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr); 468a23d5eceSKris Buschelman if (f) { 469a23d5eceSKris Buschelman ierr = (*f)(B,i,j,values);CHKERRQ(ierr); 470273d9f13SBarry Smith } 471273d9f13SBarry Smith PetscFunctionReturn(0); 472273d9f13SBarry Smith } 473273d9f13SBarry Smith 4744a2ae208SSatish Balay #undef __FUNCT__ 4754a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAdj" 476b97cf49bSBarry Smith /*@C 4773eda8832SBarry Smith MatCreateMPIAdj - Creates a sparse matrix representing an adjacency list. 478b97cf49bSBarry Smith The matrix does not have numerical values associated with it, but is 479b97cf49bSBarry Smith intended for ordering (to reduce bandwidth etc) and partitioning. 480b97cf49bSBarry Smith 481ef5ee4d1SLois Curfman McInnes Collective on MPI_Comm 482ef5ee4d1SLois Curfman McInnes 483b97cf49bSBarry Smith Input Parameters: 484c2e958feSBarry Smith + comm - MPI communicator 4850752156aSBarry Smith . m - number of local rows 486b97cf49bSBarry Smith . n - number of columns 487b97cf49bSBarry Smith . i - the indices into j for the start of each row 488329f5518SBarry Smith . j - the column indices for each row (sorted for each row). 489ef5ee4d1SLois Curfman McInnes The indices in i and j start with zero (NOT with one). 490329f5518SBarry Smith - values -[optional] edge weights 491b97cf49bSBarry Smith 492b97cf49bSBarry Smith Output Parameter: 493b97cf49bSBarry Smith . A - the matrix 494b97cf49bSBarry Smith 49515091d37SBarry Smith Level: intermediate 49615091d37SBarry Smith 4974bc6d8c0SBarry Smith Notes: This matrix object does not support most matrix operations, include 4984bc6d8c0SBarry Smith MatSetValues(). 499c2e958feSBarry Smith You must NOT free the ii, values and jj arrays yourself. PETSc will free them 500ededeb1aSvictorle when the matrix is destroyed; you must allocate them with PetscMalloc(). If you 5011198db28SBarry Smith call from Fortran you need not create the arrays with PetscMalloc(). 502327e1a73SBarry Smith Should not include the matrix diagonals. 503b97cf49bSBarry Smith 504e3f7e1d6Svictorle If you already have a matrix, you can create its adjacency matrix by a call 505ededeb1aSvictorle to MatConvert, specifying a type of MATMPIADJ. 506ededeb1aSvictorle 507ef5ee4d1SLois Curfman McInnes Possible values for MatSetOption() - MAT_STRUCTURALLY_SYMMETRIC 508b97cf49bSBarry Smith 509e3f7e1d6Svictorle .seealso: MatCreate(), MatConvert(), MatGetOrdering() 510b97cf49bSBarry Smith @*/ 511dfbe8321SBarry Smith PetscErrorCode MatCreateMPIAdj(MPI_Comm comm,int m,int n,int *i,int *j,int *values,Mat *A) 512b97cf49bSBarry Smith { 513dfbe8321SBarry Smith PetscErrorCode ierr; 514b97cf49bSBarry Smith 515433994e6SBarry Smith PetscFunctionBegin; 516273d9f13SBarry Smith ierr = MatCreate(comm,m,n,PETSC_DETERMINE,n,A);CHKERRQ(ierr); 517273d9f13SBarry Smith ierr = MatSetType(*A,MATMPIADJ);CHKERRQ(ierr); 518273d9f13SBarry Smith ierr = MatMPIAdjSetPreallocation(*A,i,j,values);CHKERRQ(ierr); 5193a40ed3dSBarry Smith PetscFunctionReturn(0); 520b97cf49bSBarry Smith } 521b97cf49bSBarry Smith 522273d9f13SBarry Smith EXTERN_C_BEGIN 5234a2ae208SSatish Balay #undef __FUNCT__ 5244a2ae208SSatish Balay #define __FUNCT__ "MatConvertTo_MPIAdj" 525dfbe8321SBarry Smith PetscErrorCode MatConvertTo_MPIAdj(Mat A,MatType type,Mat *newmat) 526b3cc6726SBarry Smith { 527676c34cdSKris Buschelman Mat B; 5286849ba73SBarry Smith PetscErrorCode ierr; 5296849ba73SBarry Smith int i,m,N,nzeros = 0,*ia,*ja,len,rstart,cnt,j,*a; 530b3cc6726SBarry Smith const int *rj; 531b3cc6726SBarry Smith const PetscScalar *ra; 532c2e958feSBarry Smith MPI_Comm comm; 533c2e958feSBarry Smith 534c2e958feSBarry Smith PetscFunctionBegin; 535c2e958feSBarry Smith ierr = MatGetSize(A,PETSC_NULL,&N);CHKERRQ(ierr); 536c2e958feSBarry Smith ierr = MatGetLocalSize(A,&m,PETSC_NULL);CHKERRQ(ierr); 537c2e958feSBarry Smith ierr = MatGetOwnershipRange(A,&rstart,PETSC_NULL);CHKERRQ(ierr); 538c2e958feSBarry Smith 539c2e958feSBarry Smith /* count the number of nonzeros per row */ 540c2e958feSBarry Smith for (i=0; i<m; i++) { 541c2e958feSBarry Smith ierr = MatGetRow(A,i+rstart,&len,&rj,PETSC_NULL);CHKERRQ(ierr); 542c2e958feSBarry Smith for (j=0; j<len; j++) { 543c2e958feSBarry Smith if (rj[j] == i+rstart) {len--; break;} /* don't count diagonal */ 544c2e958feSBarry Smith } 545c2e958feSBarry Smith ierr = MatRestoreRow(A,i+rstart,&len,&rj,PETSC_NULL);CHKERRQ(ierr); 546c2e958feSBarry Smith nzeros += len; 547c2e958feSBarry Smith } 548c2e958feSBarry Smith 549c2e958feSBarry Smith /* malloc space for nonzeros */ 550b0a32e0cSBarry Smith ierr = PetscMalloc((nzeros+1)*sizeof(int),&a);CHKERRQ(ierr); 551b0a32e0cSBarry Smith ierr = PetscMalloc((N+1)*sizeof(int),&ia);CHKERRQ(ierr); 552b0a32e0cSBarry Smith ierr = PetscMalloc((nzeros+1)*sizeof(int),&ja);CHKERRQ(ierr); 553c2e958feSBarry Smith 554c2e958feSBarry Smith nzeros = 0; 555c2e958feSBarry Smith ia[0] = 0; 556c2e958feSBarry Smith for (i=0; i<m; i++) { 557c2e958feSBarry Smith ierr = MatGetRow(A,i+rstart,&len,&rj,&ra);CHKERRQ(ierr); 558c2e958feSBarry Smith cnt = 0; 559c2e958feSBarry Smith for (j=0; j<len; j++) { 560c2e958feSBarry Smith if (rj[j] != i+rstart) { /* if not diagonal */ 5614c49b128SBarry Smith a[nzeros+cnt] = (int) PetscAbsScalar(ra[j]); 562c2e958feSBarry Smith ja[nzeros+cnt++] = rj[j]; 563c2e958feSBarry Smith } 564c2e958feSBarry Smith } 565c2e958feSBarry Smith ierr = MatRestoreRow(A,i+rstart,&len,&rj,&ra);CHKERRQ(ierr); 566c2e958feSBarry Smith nzeros += cnt; 567c2e958feSBarry Smith ia[i+1] = nzeros; 568c2e958feSBarry Smith } 569c2e958feSBarry Smith 570c2e958feSBarry Smith ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 571f204ca49SKris Buschelman ierr = MatCreate(comm,m,N,PETSC_DETERMINE,N,&B);CHKERRQ(ierr); 572f204ca49SKris Buschelman ierr = MatSetType(B,type);CHKERRQ(ierr); 573f204ca49SKris Buschelman ierr = MatMPIAdjSetPreallocation(B,ia,ja,a);CHKERRQ(ierr); 574c2e958feSBarry Smith 575676c34cdSKris Buschelman /* Fake support for "inplace" convert. */ 576676c34cdSKris Buschelman if (*newmat == A) { 577676c34cdSKris Buschelman ierr = MatDestroy(A);CHKERRQ(ierr); 578676c34cdSKris Buschelman } 579676c34cdSKris Buschelman *newmat = B; 580c2e958feSBarry Smith PetscFunctionReturn(0); 581c2e958feSBarry Smith } 582273d9f13SBarry Smith EXTERN_C_END 583433994e6SBarry Smith 584433994e6SBarry Smith 585433994e6SBarry Smith 586433994e6SBarry Smith 587433994e6SBarry Smith 588