1 2 #include <petsc-private/petscimpl.h> /*I "petscsys.h" I*/ 3 #include <petscviewer.h> 4 5 #undef __FUNCT__ 6 #define __FUNCT__ "PetscObjectSetName" 7 /*@C 8 PetscObjectSetName - Sets a string name associated with a PETSc object. 9 10 Not Collective 11 12 Input Parameters: 13 + obj - the Petsc variable 14 Thus must be cast with a (PetscObject), for example, 15 PetscObjectSetName((PetscObject)mat,name); 16 - name - the name to give obj 17 18 Level: advanced 19 20 Concepts: object name^setting 21 22 .seealso: PetscObjectGetName() 23 @*/ 24 PetscErrorCode PetscObjectSetName(PetscObject obj,const char name[]) 25 { 26 PetscErrorCode ierr; 27 28 PetscFunctionBegin; 29 PetscValidHeader(obj,1); 30 ierr = PetscFree(obj->name);CHKERRQ(ierr); 31 ierr = PetscStrallocpy(name,&obj->name);CHKERRQ(ierr); 32 PetscFunctionReturn(0); 33 } 34 35 #undef __FUNCT__ 36 #define __FUNCT__ "PetscObjectPrintClassNamePrefixType" 37 /*@C 38 PetscObjectPrintTypeNamePrefix - used in the XXXView() methods to display information about the class, name, prefix and type of an object 39 40 Input Parameters: 41 + obj - the PETSc object 42 - viewer - ASCII viewer where the information is printed 43 44 Level: developer 45 46 .seealso: PetscObjectSetName(), PetscObjectName() 47 48 @*/ 49 PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject obj,PetscViewer viewer) 50 { 51 PetscErrorCode ierr; 52 MPI_Comm comm; 53 PetscMPIInt size; 54 55 PetscFunctionBegin; 56 ierr = PetscViewerASCIIPrintf(viewer,"%s Object:",obj->class_name);CHKERRQ(ierr); 57 if (obj->name) { 58 ierr = PetscViewerASCIIPrintf(viewer,"%s",obj->name);CHKERRQ(ierr); 59 } 60 if (obj->prefix) { 61 ierr = PetscViewerASCIIPrintf(viewer,"(%s)",obj->prefix);CHKERRQ(ierr); 62 } 63 ierr = PetscObjectGetComm(obj,&comm);CHKERRQ(ierr); 64 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 65 ierr = PetscViewerASCIIPrintf(viewer," %d MPI processes\n",size);CHKERRQ(ierr); 66 if (obj->type_name) { 67 ierr = PetscViewerASCIIPrintf(viewer," type: %s\n",obj->type_name);CHKERRQ(ierr); 68 } else { 69 ierr = PetscViewerASCIIPrintf(viewer," type not yet set\n");CHKERRQ(ierr); 70 } 71 PetscFunctionReturn(0); 72 } 73 74 #undef __FUNCT__ 75 #define __FUNCT__ "PetscObjectName" 76 /*@C 77 PetscObjectName - Gives an object a name if it does not have one 78 79 Collective 80 81 Input Parameters: 82 . obj - the Petsc variable 83 Thus must be cast with a (PetscObject), for example, 84 PetscObjectName((PetscObject)mat,name); 85 86 Level: developer 87 88 Concepts: object name^setting default 89 90 Notes: This is used in a small number of places when an object NEEDS a name, for example when it is saved to MATLAB with that variable name. 91 Use PetscObjectSetName() to set the name of an object to what you want. The AMS viewer requires that no two published objects 92 share the same name. 93 94 Developer Note: this needs to generate the exact same string on all ranks that share the object. The current algorithm may not always work. 95 96 97 98 .seealso: PetscObjectGetName(), PetscObjectSetName() 99 @*/ 100 PetscErrorCode PetscObjectName(PetscObject obj) 101 { 102 PetscErrorCode ierr; 103 PetscCommCounter *counter; 104 PetscMPIInt flg; 105 char name[64]; 106 107 PetscFunctionBegin; 108 PetscValidHeader(obj,1); 109 if (!obj->name) { 110 void *commp = 0; 111 ierr = MPI_Attr_get(obj->comm,Petsc_Counter_keyval,(void*)&counter,&flg);CHKERRQ(ierr); 112 if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Bad MPI communicator supplied; must be a PETSc communicator"); 113 ierr = PetscMemcpy(&commp,&obj->comm,PetscMin(sizeof(commp),sizeof(obj->comm)));CHKERRQ(ierr); 114 ierr = MPI_Bcast((PETSC_UINTPTR_T*)&commp,1,MPIU_SIZE_T,0,obj->comm);CHKERRQ(ierr); 115 ierr = PetscSNPrintf(name,64,"%s_%p_%D",obj->class_name,commp,counter->namecount++);CHKERRQ(ierr); 116 ierr = PetscStrallocpy(name,&obj->name);CHKERRQ(ierr); 117 } 118 PetscFunctionReturn(0); 119 } 120 121 122 123 #undef __FUNCT__ 124 #define __FUNCT__ "PetscObjectChangeTypeName" 125 PetscErrorCode PetscObjectChangeTypeName(PetscObject obj,const char type_name[]) 126 { 127 PetscErrorCode ierr; 128 129 PetscFunctionBegin; 130 PetscValidHeader(obj,1); 131 ierr = PetscFree(obj->type_name);CHKERRQ(ierr); 132 ierr = PetscStrallocpy(type_name,&obj->type_name);CHKERRQ(ierr); 133 /* Clear all the old subtype callbacks so they can't accidentally be called (shouldn't happen anyway) */ 134 ierr = PetscMemzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE],obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE]*sizeof(PetscFortranCallback));CHKERRQ(ierr); 135 PetscFunctionReturn(0); 136 } 137 138