Actual source code: networkmonitor.c

petsc-master 2020-02-23
Report Typos and Errors
  1:  #include <petscdmnetwork.h>
  2:  #include <petscdraw.h>

  4: /*@
  5:   DMNetworkMonitorCreate - Creates a network monitor context

  7:   Collective

  9:   Input Parameters:
 10: . network - network to monitor

 12:   Output Parameters:
 13: . Monitorptr - Location to put network monitor context

 15:   Level: intermediate

 17: .seealso: DMNetworkMonitorDestroy(), DMNetworkMonitorAdd()
 18: @*/
 19: PetscErrorCode DMNetworkMonitorCreate(DM network,DMNetworkMonitor *monitorptr)
 20: {
 21:   PetscErrorCode   ierr;
 22:   DMNetworkMonitor monitor;
 23:   MPI_Comm         comm;
 24:   PetscMPIInt      size;

 27:   PetscObjectGetComm((PetscObject)network,&comm);
 28:   MPI_Comm_size(comm, &size);
 29:   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Parallel DMNetworkMonitor is not supported yet");

 31:   PetscMalloc1(1,&monitor);
 32:   monitor->comm      = comm;
 33:   monitor->network   = network;
 34:   monitor->firstnode = NULL;

 36:   *monitorptr = monitor;
 37:   return(0);
 38: }

 40: /*@
 41:   DMNetworkMonitorDestroy - Destroys a network monitor and all associated viewers

 43:   Collective on monitor

 45:   Input Parameters:
 46: . monitor - monitor to destroy

 48:   Level: intermediate

 50: .seealso: DMNetworkMonitorCreate, DMNetworkMonitorAdd
 51: @*/
 52: PetscErrorCode DMNetworkMonitorDestroy(DMNetworkMonitor *monitor)
 53: {

 57:   while ((*monitor)->firstnode) {
 58:     DMNetworkMonitorPop(*monitor);
 59:   }

 61:   PetscFree(*monitor);
 62:   return(0);
 63: }

 65: /*@
 66:   DMNetworkMonitorPop - Removes the most recently added viewer

 68:   Collective on monitor

 70:   Input Parameters:
 71: . monitor - the monitor

 73:   Level: intermediate

 75: .seealso: DMNetworkMonitorCreate(), DMNetworkMonitorDestroy()
 76: @*/
 77: PetscErrorCode DMNetworkMonitorPop(DMNetworkMonitor monitor)
 78: {
 79:   PetscErrorCode       ierr;
 80:   DMNetworkMonitorList node;

 83:   if (monitor->firstnode) {
 84:     /* Update links */
 85:     node = monitor->firstnode;
 86:     monitor->firstnode = node->next;

 88:     /* Free list node */
 89:     PetscViewerDestroy(&(node->viewer));
 90:     VecDestroy(&(node->v));
 91:     PetscFree(node);
 92:   }
 93:   return(0);
 94: }

 96: /*@C
 97:   DMNetworkMonitorAdd - Adds a new viewer to monitor

 99:   Collective on monitor

101:   Input Parameters:
102: + monitor - the monitor
103: . name - name of viewer
104: . element - vertex / edge number
105: . nodes - number of nodes
106: . start - variable starting offset
107: . blocksize - variable blocksize
108: . xmin - xmin (or PETSC_DECIDE) for viewer
109: . xmax - xmax (or PETSC_DECIDE) for viewer
110: . ymin - ymin for viewer
111: . ymax - ymax for viewer
112: - hold - determines if plot limits should be held

114:   Level: intermediate

116:   Notes:
117:   This is written to be independent of the semantics associated to the variables
118:   at a given network vertex / edge.

120:   Precisely, the parameters nodes, start and blocksize allow you to select a general
121:   strided subarray of the variables to monitor.

123: .seealso: DMNetworkMonitorCreate(), DMNetworkMonitorDestroy()
124: @*/
125: PetscErrorCode DMNetworkMonitorAdd(DMNetworkMonitor monitor,const char *name,PetscInt element,PetscInt nodes,PetscInt start,PetscInt blocksize,PetscReal xmin,PetscReal xmax,PetscReal ymin,PetscReal ymax,PetscBool hold)
126: {
127:   PetscErrorCode       ierr;
128:   PetscDrawLG          drawlg;
129:   PetscDrawAxis        axis;
130:   PetscMPIInt          rank, size;
131:   DMNetworkMonitorList node;
132:   char                 titleBuffer[64];
133:   PetscInt             vStart,vEnd,eStart,eEnd;

136:   MPI_Comm_rank(monitor->comm, &rank);
137:   MPI_Comm_size(monitor->comm, &size);

139:   DMNetworkGetVertexRange(monitor->network, &vStart, &vEnd);
140:   DMNetworkGetEdgeRange(monitor->network, &eStart, &eEnd);

142:   /* Make window title */
143:   if (vStart <= element && element < vEnd) {
144:     PetscSNPrintf(titleBuffer, 64, "%s @ vertex %d [%d / %d]", name, element - vStart, rank, size-1);
145:   } else if (eStart <= element && element < eEnd) {
146:     PetscSNPrintf(titleBuffer, 64, "%s @ edge %d [%d / %d]", name, element - eStart, rank, size-1);
147:   } else {
148:     /* vertex / edge is not on local machine, so skip! */
149:     return(0);
150:   }

152:   PetscMalloc1(1, &node);

154:   /* Setup viewer. */
155:   PetscViewerDrawOpen(monitor->comm, NULL, titleBuffer, PETSC_DECIDE, PETSC_DECIDE, PETSC_DRAW_QUARTER_SIZE, PETSC_DRAW_QUARTER_SIZE, &(node->viewer));
156:   PetscViewerPushFormat(node->viewer, PETSC_VIEWER_DRAW_LG_XRANGE);
157:   PetscViewerDrawGetDrawLG(node->viewer, 0, &drawlg);
158:   PetscDrawLGGetAxis(drawlg, &axis);
159:   if (xmin != PETSC_DECIDE && xmax != PETSC_DECIDE) {
160:     PetscDrawAxisSetLimits(axis, xmin, xmax, ymin, ymax);
161:   } else {
162:     PetscDrawAxisSetLimits(axis, 0, nodes-1, ymin, ymax);
163:   }
164:   PetscDrawAxisSetHoldLimits(axis, hold);

166:   /* Setup vector storage for drawing. */
167:   VecCreateSeq(PETSC_COMM_SELF, nodes, &(node->v));

169:   node->element   = element;
170:   node->nodes     = nodes;
171:   node->start     = start;
172:   node->blocksize = blocksize;

174:   node->next         = monitor->firstnode;
175:   monitor->firstnode = node;
176:   return(0);
177: }

179: /*@
180:   DMNetworkMonitorView - Monitor function for TSMonitorSet.

182:   Collectiveon DMNetworkMonitor

184:   Input Parameters:
185: + monitor - DMNetworkMonitor object
186: - x - TS solution vector

188:   Level: intermediate

190: .seealso: DMNetworkMonitorCreate(), DMNetworkMonitorDestroy(), DMNetworkMonitorAdd()
191: @*/

193: PetscErrorCode DMNetworkMonitorView(DMNetworkMonitor monitor,Vec x)
194: {
195:   PetscErrorCode      ierr;
196:   PetscInt            varoffset,i,start;
197:   const PetscScalar   *xx;
198:   PetscScalar         *vv;
199:   DMNetworkMonitorList node;

202:   VecGetArrayRead(x, &xx);
203:   for (node = monitor->firstnode; node; node = node->next) {
204:     DMNetworkGetVariableGlobalOffset(monitor->network, node->element, &varoffset);
205:     VecGetArray(node->v, &vv);
206:     start = varoffset + node->start;
207:     for (i = 0; i < node->nodes; i++) {
208:       vv[i] = xx[start+i*node->blocksize];
209:     }
210:     VecRestoreArray(node->v, &vv);
211:     VecView(node->v, node->viewer);
212:   }
213:   VecRestoreArrayRead(x, &xx);
214:   return(0);
215: }