PetscErrorCode PetscStackViewSAWs(void) { PetscStack* petscstackp; PetscMPIInt rank; PetscErrorCode ierr; ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); if (rank) return 0; petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack); PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/functions",petscstackp->function,20,SAWs_READ,SAWs_STRING)); PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/__current_size",&petscstackp->currentsize,1,SAWs_READ,SAWs_INT)); amsmemstack = PETSC_TRUE; return 0; }
PetscErrorCode PetscStackViewAMS(void) { AMS_Comm acomm; PetscErrorCode ierr; AMS_Memory mem; PetscStack* petscstackp; petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack); ierr = PetscViewerAMSGetAMSComm(PETSC_VIEWER_AMS_WORLD,&acomm);CHKERRQ(ierr); PetscStackCallAMS(AMS_Memory_create,(acomm,"Stack",&mem)); PetscStackCallAMS(AMS_Memory_take_access,(mem)); PetscStackCallAMS(AMS_Memory_add_field,(mem,"functions",petscstackp->function,10,AMS_STRING,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF)); PetscStackCallAMS(AMS_Memory_add_field,(mem,"current size",&petscstackp->currentsize,1,AMS_INT,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF)); PetscStackCallAMS(AMS_Memory_publish,(mem)); PetscStackCallAMS(AMS_Memory_grant_access,(mem)); amsmemstack = mem; return 0; }
PetscErrorCode PetscStackView(FILE *file) { int i; PetscStack *petscstackp; petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack); if (!file) file = PETSC_STDOUT; if (file == PETSC_STDOUT) { (*PetscErrorPrintf)("Note: The EXACT line numbers in the stack are not available,\n"); (*PetscErrorPrintf)(" INSTEAD the line number of the start of the function\n"); (*PetscErrorPrintf)(" is given.\n"); for (i=petscstackp->currentsize-1; i>=0; i--) (*PetscErrorPrintf)("[%d] %s line %d %s%s\n",PetscGlobalRank,petscstackp->function[i],petscstackp->line[i],petscstackp->directory[i],petscstackp->file[i]); } else { fprintf(file,"Note: The EXACT line numbers in the stack are not available,\n"); fprintf(file," INSTEAD the line number of the start of the function\n"); fprintf(file," is given.\n"); for (i=petscstackp->currentsize-1; i>=0; i--) fprintf(file,"[%d] %s line %d %s%s\n",PetscGlobalRank,petscstackp->function[i],petscstackp->line[i],petscstackp->directory[i],petscstackp->file[i]); } return 0; }
PetscErrorCode PetscStackView(PetscViewer viewer) { PetscErrorCode ierr; int i; FILE *file; PetscStack* petscstackp; petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack); if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; ierr = PetscViewerASCIIGetPointer(viewer,&file);CHKERRQ(ierr); if (file == PETSC_STDOUT) { (*PetscErrorPrintf)("Note: The EXACT line numbers in the stack are not available,\n"); (*PetscErrorPrintf)(" INSTEAD the line number of the start of the function\n"); (*PetscErrorPrintf)(" is given.\n"); for (i=petscstackp->currentsize-1; i>=0; i--) { (*PetscErrorPrintf)("[%d] %s line %d %s%s\n",PetscGlobalRank, petscstackp->function[i], petscstackp->line[i], petscstackp->directory[i], petscstackp->file[i]); } } else { fprintf(file,"Note: The EXACT line numbers in the stack are not available,\n"); fprintf(file," INSTEAD the line number of the start of the function\n"); fprintf(file," is given.\n"); for (i=petscstackp->currentsize-1; i>=0; i--) { fprintf(file,"[%d] %s line %d %s%s\n",PetscGlobalRank, petscstackp->function[i], petscstackp->line[i], petscstackp->directory[i], petscstackp->file[i]); } } return 0; }
/* PetscTrMallocDefault - Malloc with tracing. Input Parameters: + a - number of bytes to allocate . lineno - line number where used. Use __LINE__ for this . function - function calling routine. Use __FUNCT__ for this - filename - file name where used. Use __FILE__ for this Returns: double aligned pointer to requested storage, or null if not available. */ PetscErrorCode PetscTrMallocDefault(size_t a,int lineno,const char function[],const char filename[],void **result) { TRSPACE *head; char *inew; size_t nsize; PetscErrorCode ierr; PetscFunctionBegin; if (TRdebugLevel) { ierr = PetscMallocValidate(lineno,function,filename); if (ierr) PetscFunctionReturn(ierr); } nsize = (a + (PETSC_MEMALIGN-1)) & ~(PETSC_MEMALIGN-1); ierr = PetscMallocAlign(nsize+sizeof(TrSPACE)+sizeof(PetscClassId),lineno,function,filename,(void**)&inew);CHKERRQ(ierr); head = (TRSPACE*)inew; inew += sizeof(TrSPACE); if (TRhead) TRhead->prev = head; head->next = TRhead; TRhead = head; head->prev = 0; head->size = nsize; head->id = TRid; head->lineno = lineno; head->filename = filename; head->functionname = function; head->classid = CLASSID_VALUE; *(PetscClassId*)(inew + nsize) = CLASSID_VALUE; TRallocated += nsize; if (TRallocated > TRMaxMem) TRMaxMem = TRallocated; TRfrags++; #if defined(PETSC_USE_DEBUG) if (PetscStackActive()) { ierr = PetscStackCopy((PetscStack*)PetscThreadLocalGetValue(petscstack),&head->stack);CHKERRQ(ierr); /* fix the line number to where the malloc() was called, not the PetscFunctionBegin; */ head->stack.line[head->stack.currentsize-2] = lineno; } #endif /* Allow logging of all mallocs made */ if (PetscLogMalloc > -1 && PetscLogMalloc < PetscLogMallocMax && a >= PetscLogMallocThreshold) { if (!PetscLogMalloc) { PetscLogMallocLength = (size_t*)malloc(PetscLogMallocMax*sizeof(size_t)); if (!PetscLogMallocLength) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," "); PetscLogMallocFile = (const char**)malloc(PetscLogMallocMax*sizeof(char*)); if (!PetscLogMallocFile) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," "); PetscLogMallocFunction = (const char**)malloc(PetscLogMallocMax*sizeof(char*)); if (!PetscLogMallocFunction) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," "); } PetscLogMallocLength[PetscLogMalloc] = nsize; PetscLogMallocFile[PetscLogMalloc] = filename; PetscLogMallocFunction[PetscLogMalloc++] = function; } *result = (void*)inew; PetscFunctionReturn(0); }
/*@C PetscCheckPointer - Returns PETSC_TRUE if a pointer points to accessible data Not Collective Input Parameters: + ptr - the pointer - dtype - the type of data the pointer is suppose to point to Level: developer @*/ PetscBool PetscCheckPointer(const void *ptr,PetscDataType dtype) { struct sigaction sa,oldsa; PetscStack *stackp; if (PETSC_RUNNING_ON_VALGRIND) return PETSC_TRUE; if (!ptr) return PETSC_FALSE; if (petsc_checkpointer_intensity < 1) return PETSC_TRUE; /* Skip the verbose check if we are inside a hot function. */ stackp = (PetscStack*)PetscThreadLocalGetValue(petscstack); if (stackp && stackp->hotdepth > 0 && petsc_checkpointer_intensity < 2) return PETSC_TRUE; sigemptyset(&sa.sa_mask); sa.sa_sigaction = PetscSegv_sigaction; sa.sa_flags = SA_SIGINFO; sigaction(SIGSEGV, &sa, &oldsa); if (setjmp(PetscSegvJumpBuf)) { /* A segv was triggered in the code below hence we return with an error code */ sigaction(SIGSEGV, &oldsa, NULL);/* reset old signal hanlder */ return PETSC_FALSE; } else { switch (dtype) { case PETSC_INT:{ PETSC_UNUSED PetscInt x = (PetscInt)*(volatile PetscInt*)ptr; break; } #if defined(PETSC_USE_COMPLEX) case PETSC_SCALAR:{ /* C++ is seriously dysfunctional with volatile std::complex. */ PetscReal xreal = ((volatile PetscReal*)ptr)[0],ximag = ((volatile PetscReal*)ptr)[1]; PETSC_UNUSED volatile PetscScalar x = xreal + PETSC_i*ximag; break; } #endif case PETSC_REAL:{ PETSC_UNUSED PetscReal x = *(volatile PetscReal*)ptr; break; } case PETSC_BOOL:{ PETSC_UNUSED PetscBool x = *(volatile PetscBool*)ptr; break; } case PETSC_ENUM:{ PETSC_UNUSED PetscEnum x = *(volatile PetscEnum*)ptr; break; } case PETSC_CHAR:{ PETSC_UNUSED char x = *(volatile char*)ptr; break; } case PETSC_OBJECT:{ PETSC_UNUSED volatile PetscClassId classid = ((PetscObject)ptr)->classid; break; } default:; } } sigaction(SIGSEGV, &oldsa, NULL); /* reset old signal hanlder */ return PETSC_TRUE; }