Actual source code: networkmonitor.c

petsc-3.11.3 2019-06-26
Report Typos and Errors
  1:  #include <petscdmnetwork.h>
  2:  #include <petscdraw.h>

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

  7:   Collective on MPI_Comm

  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 DMNetworkMonitor

 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 DMNetworkMonitor

 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 DMNetworkMonitor

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: . ymin - ymin for viewer
109: . ymax - ymax for viewer
110: - hold - determines if plot limits should be held

112:   Level: intermediate

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

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

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

134:   MPI_Comm_rank(monitor->comm, &rank);
135:   MPI_Comm_size(monitor->comm, &size);

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

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

150:   PetscMalloc1(1, &node);

152:   /* Setup viewer. */
153:   PetscViewerDrawOpen(monitor->comm, NULL, titleBuffer, PETSC_DECIDE, PETSC_DECIDE, PETSC_DRAW_QUARTER_SIZE, PETSC_DRAW_QUARTER_SIZE, &(node->viewer));
154:   PetscViewerPushFormat(node->viewer, PETSC_VIEWER_DRAW_LG);
155:   PetscViewerDrawGetDrawLG(node->viewer, 0, &drawlg);
156:   PetscDrawLGGetAxis(drawlg, &axis);
157:   PetscDrawAxisSetLimits(axis, 0, nodes-1, ymin, ymax);
158:   PetscDrawAxisSetHoldLimits(axis, hold);

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

163:   node->element   = element;
164:   node->nodes     = nodes;
165:   node->start     = start;
166:   node->blocksize = blocksize;

168:   node->next         = monitor->firstnode;
169:   monitor->firstnode = node;
170:   return(0);
171: }

173: /*@
174:   DMNetworkMonitorView - Monitor function for TSMonitorSet.

176:   Collectiveon DMNetworkMonitor

178:   Input Parameters:
179: + monitor - DMNetworkMonitor object
180: - x - TS solution vector

182:   Level: intermediate

184: .seealso: DMNetworkMonitorCreate(), DMNetworkMonitorDestroy(), DMNetworkMonitorAdd()
185: @*/
186: PetscErrorCode DMNetworkMonitorView(DMNetworkMonitor monitor,Vec x)
187: {
188:   PetscErrorCode      ierr;
189:   PetscInt            varoffset,i,start;
190:   const PetscScalar   *xx;
191:   PetscScalar         *vv;
192:   DMNetworkMonitorList node;

195:   VecGetArrayRead(x, &xx);
196:   for (node = monitor->firstnode; node; node = node->next) {
197:     DMNetworkGetVariableGlobalOffset(monitor->network, node->element, &varoffset);
198:     VecGetArray(node->v, &vv);
199:     start = varoffset + node->start;
200:     for (i = 0; i < node->nodes; i++) {
201:       vv[i] = xx[start+i*node->blocksize];
202:     }
203:     VecRestoreArray(node->v, &vv);
204:     VecView(node->v, node->viewer);
205:   }
206:   VecRestoreArrayRead(x, &xx);
207:   return(0);
208: }