static PetscErrorCode ISToGeneral_Block(IS inis) { IS_Block *sub = (IS_Block*)inis->data; PetscInt bs,n; const PetscInt *idx; PetscErrorCode ierr; PetscFunctionBegin; ierr = ISGetBlockSize(inis,&bs); CHKERRQ(ierr); ierr = ISGetLocalSize(inis,&n); CHKERRQ(ierr); ierr = ISGetIndices(inis,&idx); CHKERRQ(ierr); if (bs == 1) { PetscCopyMode mode = sub->borrowed_indices ? PETSC_USE_POINTER : PETSC_OWN_POINTER; sub->borrowed_indices = PETSC_TRUE; /* prevent deallocation when changing the subtype*/ ierr = ISSetType(inis,ISGENERAL); CHKERRQ(ierr); ierr = ISGeneralSetIndices(inis,n,idx,mode); CHKERRQ(ierr); } else { ierr = ISSetType(inis,ISGENERAL); CHKERRQ(ierr); ierr = ISGeneralSetIndices(inis,n,idx,PETSC_OWN_POINTER); CHKERRQ(ierr); } PetscFunctionReturn(0); }
/* Compose an IS with an ISLocalToGlobalMapping to map from IS source indices to global indices */ static PetscErrorCode ISL2GCompose(IS is,ISLocalToGlobalMapping ltog,ISLocalToGlobalMapping *cltog) { PetscErrorCode ierr; const PetscInt *idx; PetscInt m,*idxm; PetscBool isblock; PetscFunctionBegin; PetscValidHeaderSpecific(is,IS_CLASSID,1); PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,2); PetscValidPointer(cltog,3); ierr = PetscObjectTypeCompare((PetscObject)is,ISBLOCK,&isblock);CHKERRQ(ierr); if (isblock) { PetscInt bs,lbs; ierr = ISGetBlockSize(is,&bs);CHKERRQ(ierr); ierr = ISLocalToGlobalMappingGetBlockSize(ltog,&lbs);CHKERRQ(ierr); if (bs == lbs) { ierr = ISGetLocalSize(is,&m);CHKERRQ(ierr); m = m/bs; ierr = ISBlockGetIndices(is,&idx);CHKERRQ(ierr); ierr = PetscMalloc1(m,&idxm);CHKERRQ(ierr); ierr = ISLocalToGlobalMappingApplyBlock(ltog,m,idx,idxm);CHKERRQ(ierr); ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)is),bs,m,idxm,PETSC_OWN_POINTER,cltog);CHKERRQ(ierr); ierr = ISBlockRestoreIndices(is,&idx);CHKERRQ(ierr); PetscFunctionReturn(0); } } ierr = ISGetLocalSize(is,&m);CHKERRQ(ierr); ierr = ISGetIndices(is,&idx);CHKERRQ(ierr); ierr = PetscMalloc1(m,&idxm);CHKERRQ(ierr); if (ltog) { ierr = ISLocalToGlobalMappingApply(ltog,m,idx,idxm);CHKERRQ(ierr); } else { ierr = PetscMemcpy(idxm,idx,m*sizeof(PetscInt));CHKERRQ(ierr); } ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)is),1,m,idxm,PETSC_OWN_POINTER,cltog);CHKERRQ(ierr); ierr = ISRestoreIndices(is,&idx);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode ISView_General_HDF5(IS is, PetscViewer viewer) { hid_t filespace; /* file dataspace identifier */ hid_t chunkspace; /* chunk dataset property identifier */ hid_t plist_id; /* property list identifier */ hid_t dset_id; /* dataset identifier */ hid_t memspace; /* memory dataspace identifier */ hid_t inttype; /* int type (H5T_NATIVE_INT or H5T_NATIVE_LLONG) */ hid_t file_id, group; herr_t status; hsize_t dim, maxDims[3], dims[3], chunkDims[3], count[3],offset[3]; PetscInt bs, N, n, timestep, low; const PetscInt *ind; const char *isname; PetscErrorCode ierr; PetscFunctionBegin; ierr = ISGetBlockSize(is,&bs);CHKERRQ(ierr); ierr = PetscViewerHDF5OpenGroup(viewer, &file_id, &group);CHKERRQ(ierr); ierr = PetscViewerHDF5GetTimestep(viewer, ×tep);CHKERRQ(ierr); /* Create the dataspace for the dataset. * * dims - holds the current dimensions of the dataset * * maxDims - holds the maximum dimensions of the dataset (unlimited * for the number of time steps with the current dimensions for the * other dimensions; so only additional time steps can be added). * * chunkDims - holds the size of a single time step (required to * permit extending dataset). */ dim = 0; if (timestep >= 0) { dims[dim] = timestep+1; maxDims[dim] = H5S_UNLIMITED; chunkDims[dim] = 1; ++dim; } ierr = ISGetSize(is, &N);CHKERRQ(ierr); ierr = ISGetLocalSize(is, &n);CHKERRQ(ierr); ierr = PetscHDF5IntCast(N/bs,dims + dim);CHKERRQ(ierr); maxDims[dim] = dims[dim]; chunkDims[dim] = dims[dim]; ++dim; if (bs >= 1) { dims[dim] = bs; maxDims[dim] = dims[dim]; chunkDims[dim] = dims[dim]; ++dim; } filespace = H5Screate_simple(dim, dims, maxDims); if (filespace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()"); #if defined(PETSC_USE_64BIT_INDICES) inttype = H5T_NATIVE_LLONG; #else inttype = H5T_NATIVE_INT; #endif /* Create the dataset with default properties and close filespace */ ierr = PetscObjectGetName((PetscObject) is, &isname);CHKERRQ(ierr); if (!H5Lexists(group, isname, H5P_DEFAULT)) { /* Create chunk */ chunkspace = H5Pcreate(H5P_DATASET_CREATE); if (chunkspace == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Cannot H5Pcreate()"); status = H5Pset_chunk(chunkspace, dim, chunkDims);CHKERRQ(status); #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800) dset_id = H5Dcreate2(group, isname, inttype, filespace, H5P_DEFAULT, chunkspace, H5P_DEFAULT); #else dset_id = H5Dcreate(group, isname, inttype, filespace, H5P_DEFAULT); #endif if (dset_id == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Cannot H5Dcreate2()"); status = H5Pclose(chunkspace);CHKERRQ(status); } else { dset_id = H5Dopen2(group, isname, H5P_DEFAULT); status = H5Dset_extent(dset_id, dims);CHKERRQ(status); } status = H5Sclose(filespace);CHKERRQ(status); /* Each process defines a dataset and writes it to the hyperslab in the file */ dim = 0; if (timestep >= 0) { count[dim] = 1; ++dim; } ierr = PetscHDF5IntCast(n/bs,count + dim);CHKERRQ(ierr); ++dim; if (bs >= 1) { count[dim] = bs; ++dim; } if (n > 0) { memspace = H5Screate_simple(dim, count, NULL); if (memspace == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Cannot H5Screate_simple()"); } else { /* Can't create dataspace with zero for any dimension, so create null dataspace. */ memspace = H5Screate(H5S_NULL); if (memspace == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Cannot H5Screate()"); } /* Select hyperslab in the file */ ierr = PetscLayoutGetRange(is->map, &low, NULL);CHKERRQ(ierr); dim = 0; if (timestep >= 0) { offset[dim] = timestep; ++dim; } ierr = PetscHDF5IntCast(low/bs,offset + dim);CHKERRQ(ierr); ++dim; if (bs >= 1) { offset[dim] = 0; ++dim; } if (n > 0) { filespace = H5Dget_space(dset_id); if (filespace == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Cannot H5Dget_space()"); status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);CHKERRQ(status); } else { /* Create null filespace to match null memspace. */ filespace = H5Screate(H5S_NULL); if (filespace == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Cannot H5Screate(H5S_NULL)"); } /* Create property list for collective dataset write */ plist_id = H5Pcreate(H5P_DATASET_XFER); if (plist_id == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Cannot H5Pcreate()"); #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO) status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);CHKERRQ(status); #endif /* To write dataset independently use H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT) */ ierr = ISGetIndices(is, &ind);CHKERRQ(ierr); status = H5Dwrite(dset_id, inttype, memspace, filespace, plist_id, ind);CHKERRQ(status); status = H5Fflush(file_id, H5F_SCOPE_GLOBAL);CHKERRQ(status); ierr = ISGetIndices(is, &ind);CHKERRQ(ierr); /* Close/release resources */ if (group != file_id) {status = H5Gclose(group);CHKERRQ(status);} status = H5Pclose(plist_id);CHKERRQ(status); status = H5Sclose(filespace);CHKERRQ(status); status = H5Sclose(memspace);CHKERRQ(status); status = H5Dclose(dset_id);CHKERRQ(status); ierr = PetscInfo1(is, "Wrote IS object with name %s\n", isname);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode private_ISView_Swarm_XDMF(IS is,PetscViewer viewer) { long int *bytes = NULL; PetscContainer container = NULL; const char *viewername; char datafile[PETSC_MAX_PATH_LEN]; PetscViewer fviewer; PetscInt N,bs; const char *vecname; char fieldname[PETSC_MAX_PATH_LEN]; PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscObjectQuery((PetscObject)viewer,"XDMFViewerContext",(PetscObject*)&container);CHKERRQ(ierr); if (container) { ierr = PetscContainerGetPointer(container,(void**)&bytes);CHKERRQ(ierr); } else SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Valid to find attached data XDMFViewerContext"); ierr = PetscViewerFileGetName(viewer,&viewername);CHKERRQ(ierr); ierr = private_CreateDataFileNameXDMF(viewername,datafile);CHKERRQ(ierr); /* re-open a sub-viewer for all data fields */ /* name is viewer.name + "_swarm_fields.pbin" */ ierr = PetscViewerCreate(PetscObjectComm((PetscObject)viewer),&fviewer);CHKERRQ(ierr); ierr = PetscViewerSetType(fviewer,PETSCVIEWERBINARY);CHKERRQ(ierr); ierr = PetscViewerBinarySetSkipHeader(fviewer,PETSC_TRUE);CHKERRQ(ierr); ierr = PetscViewerBinarySetSkipInfo(fviewer,PETSC_TRUE);CHKERRQ(ierr); ierr = PetscViewerFileSetMode(fviewer,FILE_MODE_APPEND);CHKERRQ(ierr); ierr = PetscViewerFileSetName(fviewer,datafile);CHKERRQ(ierr); ierr = ISGetSize(is,&N);CHKERRQ(ierr); ierr = ISGetBlockSize(is,&bs);CHKERRQ(ierr); N = N/bs; ierr = PetscObjectGetName((PetscObject)is,&vecname);CHKERRQ(ierr); if (!vecname) { ierr = PetscSNPrintf(fieldname,PETSC_MAX_PATH_LEN-1,"swarmfield_%D",((PetscObject)is)->tag);CHKERRQ(ierr); } else { ierr = PetscSNPrintf(fieldname,PETSC_MAX_PATH_LEN-1,"%s",vecname);CHKERRQ(ierr); } /* write data header */ ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"<Attribute Center=\"Node\" Name=\"%s\" Type=\"None\">\n",fieldname);CHKERRQ(ierr); ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); if (bs == 1) { ierr = PetscViewerASCIIPrintf(viewer,"<DataItem Format=\"Binary\" Endian=\"Big\" DataType=\"Int\" Precision=\"4\" Dimensions=\"%D\" Seek=\"%D\">\n",N,bytes[0]);CHKERRQ(ierr); } else { ierr = PetscViewerASCIIPrintf(viewer,"<DataItem Format=\"Binary\" Endian=\"Big\" DataType=\"Int\" Precision=\"4\" Dimensions=\"%D %D\" Seek=\"%D\">\n",N,bs,bytes[0]);CHKERRQ(ierr); } ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"%s\n",datafile);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"</DataItem>\n");CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"</Attribute>\n");CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); /* write data */ ierr = ISView(is,fviewer);CHKERRQ(ierr); bytes[0] += sizeof(PetscInt) * N * bs; ierr = PetscViewerDestroy(&fviewer);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ MatCreateLocalRef - Gets a logical reference to a local submatrix, for use in assembly Not Collective Input Arguments: + A - Full matrix, generally parallel . isrow - Local index set for the rows - iscol - Local index set for the columns Output Arguments: . newmat - New serial Mat Level: developer Notes: Most will use MatGetLocalSubMatrix() which returns a real matrix corresponding to the local block if it available, such as with matrix formats that store these blocks separately. The new matrix forwards MatSetValuesLocal() and MatSetValuesBlockedLocal() to the global system. In general, it does not define MatMult() or any other functions. Local submatrices can be nested. .seealso: MatSetValuesLocal(), MatSetValuesBlockedLocal(), MatGetLocalSubMatrix(), MatCreateSubMatrix() @*/ PetscErrorCode MatCreateLocalRef(Mat A,IS isrow,IS iscol,Mat *newmat) { PetscErrorCode ierr; Mat_LocalRef *lr; Mat B; PetscInt m,n; PetscBool islr; PetscFunctionBegin; PetscValidHeaderSpecific(A,MAT_CLASSID,1); PetscValidHeaderSpecific(isrow,IS_CLASSID,2); PetscValidHeaderSpecific(iscol,IS_CLASSID,3); PetscValidPointer(newmat,4); *newmat = 0; ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr); ierr = ISGetLocalSize(isrow,&m);CHKERRQ(ierr); ierr = ISGetLocalSize(iscol,&n);CHKERRQ(ierr); ierr = MatSetSizes(B,m,n,m,n);CHKERRQ(ierr); ierr = PetscObjectChangeTypeName((PetscObject)B,MATLOCALREF);CHKERRQ(ierr); ierr = MatSetUp(B);CHKERRQ(ierr); B->ops->destroy = MatDestroy_LocalRef; ierr = PetscNewLog(B,Mat_LocalRef,&lr);CHKERRQ(ierr); B->data = (void*)lr; ierr = PetscObjectTypeCompare((PetscObject)A,MATLOCALREF,&islr);CHKERRQ(ierr); if (islr) { Mat_LocalRef *alr = (Mat_LocalRef*)A->data; lr->Top = alr->Top; } else { /* This does not increase the reference count because MatLocalRef is not allowed to live longer than its parent */ lr->Top = A; } { ISLocalToGlobalMapping rltog,cltog; PetscInt abs,rbs,cbs; /* We will translate directly to global indices for the top level */ lr->SetValues = MatSetValues; lr->SetValuesBlocked = MatSetValuesBlocked; B->ops->setvalueslocal = MatSetValuesLocal_LocalRef_Scalar; ierr = ISL2GCompose(isrow,A->rmap->mapping,&rltog);CHKERRQ(ierr); if (isrow == iscol && A->rmap->mapping == A->cmap->mapping) { ierr = PetscObjectReference((PetscObject)rltog);CHKERRQ(ierr); cltog = rltog; } else { ierr = ISL2GCompose(iscol,A->cmap->mapping,&cltog);CHKERRQ(ierr); } ierr = MatSetLocalToGlobalMapping(B,rltog,cltog);CHKERRQ(ierr); ierr = ISLocalToGlobalMappingDestroy(&rltog);CHKERRQ(ierr); ierr = ISLocalToGlobalMappingDestroy(&cltog);CHKERRQ(ierr); ierr = MatGetBlockSize(A,&abs);CHKERRQ(ierr); ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr); ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr); if (rbs == cbs) { /* submatrix has block structure, so user can insert values with blocked interface */ ierr = PetscLayoutSetBlockSize(B->rmap,rbs);CHKERRQ(ierr); ierr = PetscLayoutSetBlockSize(B->cmap,cbs);CHKERRQ(ierr); if (abs != rbs || abs == 1) { /* Top-level matrix has different block size, so we have to call its scalar insertion interface */ B->ops->setvaluesblockedlocal = MatSetValuesBlockedLocal_LocalRef_Scalar; } else { /* Block sizes match so we can forward values to the top level using the block interface */ B->ops->setvaluesblockedlocal = MatSetValuesBlockedLocal_LocalRef_Block; ierr = ISL2GComposeBlock(isrow,A->rmap->bmapping,&rltog);CHKERRQ(ierr); if (isrow == iscol && A->rmap->bmapping == A->cmap->bmapping) { ierr = PetscObjectReference((PetscObject)rltog);CHKERRQ(ierr); cltog = rltog; } else { ierr = ISL2GComposeBlock(iscol,A->cmap->bmapping,&cltog);CHKERRQ(ierr); } ierr = MatSetLocalToGlobalMappingBlock(B,rltog,cltog);CHKERRQ(ierr); ierr = ISLocalToGlobalMappingDestroy(&rltog);CHKERRQ(ierr); ierr = ISLocalToGlobalMappingDestroy(&cltog);CHKERRQ(ierr); } } } *newmat = B; PetscFunctionReturn(0); }
void PETSC_STDCALL isgetblocksize_(IS is,PetscInt *size, int *__ierr ){ *__ierr = ISGetBlockSize( (IS)PetscToPointer((is) ),size); }