File: xmpi_matrix.c

package info (click to toggle)
xmpi 2.2-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,232 kB
  • ctags: 1,656
  • sloc: ansic: 13,738; sh: 1,799; makefile: 233
file content (648 lines) | stat: -rw-r--r-- 14,303 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
 * Copyright 1998-1999, University of Notre Dame.
 * Authors: Brian W. Barrett, Arun F. Rodrigues, Jeffrey M. Squyres,
 * 	 and Andrew Lumsdaine
 *
 * This file is part of XMPI
 *
 * You should have received a copy of the License Agreement for XMPI 
 * along with the software; see the file LICENSE.  If not, contact 
 * Office of Research, University of Notre Dame, Notre Dame, IN 46556.
 *
 * Permission to modify the code and to distribute modified code is
 * granted, provided the text of this NOTICE is retained, a notice that
 * the code was modified is included with the above COPYRIGHT NOTICE and
 * with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE
 * file is distributed with the modified code.
 *
 * LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.
 * By way of example, but not limitation, Licensor MAKES NO
 * REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
 * PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS
 * OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS
 * OR OTHER RIGHTS.
 *
 * Additional copyrights may follow.

 *
 *	$Id: xmpi_matrix.c,v 1.4 1999/11/10 21:32:13 arodrig6 Exp $
 * 
 *	Function:	- message matrix subwindow management functions
 */

#include <stdlib.h>
#include <string.h>

#define _NO_PROTO

#include <Xm/DialogS.h>
#include <Xm/DrawingA.h>
#include <Xm/Form.h>
#include <Xm/ScrolledW.h>

#include "xmpi.h"
#include "Bitmaps/src1.xbm"
#include "Bitmaps/src2.xbm"
#include "Bitmaps/src3.xbm"
#include "Bitmaps/src4.xbm"
#include "Bitmaps/src5.xbm"
#include "Bitmaps/src6.xbm"
#include "Bitmaps/src7.xbm"
#include "Bitmaps/srcx.xbm"

/*
 * global functions
 */
void			xmpi_mat_popup();
void			xmpi_mat_update();
void			xmpi_mat_busy();
void			xmpi_mat_unbusy();
void			xmpi_mat_destroy();

/*
 * external variables
 */
extern struct _gps	*xmpi_app_procs;	/* appl. GPS array */

/*
 * local functions
 */
static void		destroy_cb();
static void		expose_cb();
static void		gcinit();
static void		mat_clear();
static void		mat_create();
static void		mat_plot_grid();
static void		popdown_cb();
static void		popup_cb();

/*
 * local variables
 */
static Widget		draw_w;			/* matrix drawing area */
static Widget		matrix_w = 0;		/* matrix window */
static Widget		mgr_w;			/* matrix manager */
static Widget		scroll_w;		/* scrolled window */
static Pixmap		pixmap;			/* matrix pixmap */
static Pixmap		src_pix[XMPI_MTCNTMAX + 1];
						/* msg source count pixmaps */
static Dimension	bordersize;		/* space around grid */
static Dimension	drawsize;		/* drawing area size */
static GC		lblpen = 0;		/* labelling pen */
static GC		bgpen = 0;		/* background pen */
static GC		gridpen = 0;		/* grid line pen */
static Display		*disp;			/* drawing area display */
static Drawable		root;			/* root window */
static unsigned int	depth;			/* screen depth */
static int		fl_up = 0;		/* matrix popped up flag */
static int		mat_nprocs = 0;		/* # processes */
static int		*mat_src;		/* message source count */

static char		*src_colours[XMPI_MTCNTMAX + 1] = {
				XMPI_RUNCOLOUR,
				XMPI_SYSCOLOUR,
				XMPI_BLKCOLOUR,
				XMPI_RCOMCOLOUR,
				XMPI_LCOMCOLOUR,
				XMPI_SYSCOLOUR,
				XMPI_RCOMCOLOUR,
				XMPI_LBLCOLOUR
			};

static unsigned char	*src_bits[XMPI_MTCNTMAX + 1] = {
				src1_bits,
				src2_bits,
				src3_bits,
				src4_bits,
				src5_bits,
				src6_bits,
				src7_bits,
				srcx_bits
			};

/*
 *	xmpi_mat_popup
 *
 *	Function:	- popup the message matrix window
 *			- creates it if necessary
 */
void
xmpi_mat_popup()

{
	struct xmproc	*mat_procs;		/* process status array */
/*
 * If matrix already created pop it up else create it.
 */
	if (!matrix_w) {
		mat_nprocs = xmpi_vw_getnprocs();
		mat_procs = xmpi_vw_getprocs();

		if (mat_nprocs <= 0) return;

		mat_src = (int *) malloc((unsigned) (mat_nprocs *
				sizeof(int)));
		if (mat_src == 0) xmpi_fail("xmpi (malloc)");

		mat_create();
		fl_up = 1;
		xmpi_mat_update(mat_procs);
	}

	XtPopup(matrix_w, XtGrabNone);
}

/*
 *	mat_create
 *
 *	Function:	- creates the message matrix window
 */
static void
mat_create()

{
	Dimension	minscrollsize;		/* min scroller resize */
	Dimension	scrollsize;		/* initial scroller size */

	if (mat_nprocs < XMPI_MTMINPROC) {
		bordersize = (((XMPI_MTMINPROC - mat_nprocs) / 2.0) *
				XMPI_MTCELLSIZE) + XMPI_MTCELLSIZE;
	} else {
		bordersize = 2 * XMPI_MTCELLSIZE;
	}

	drawsize = (mat_nprocs * XMPI_MTCELLSIZE) + (2 * bordersize);
	minscrollsize = (XMPI_MTMINPROC * XMPI_MTCELLSIZE) +
			(2 * XMPI_MTCELLSIZE) + XMPI_VWXWIDTH;
	scrollsize = (((XMPI_MTMAXPROC < mat_nprocs) ?
			XMPI_MTMAXPROC : mat_nprocs) *
			XMPI_MTCELLSIZE) + (2 * bordersize) + XMPI_VWXWIDTH;
/*
 * Create the matrix window.
 */
	matrix_w = XtVaCreatePopupShell("matrix_pop",
			xmDialogShellWidgetClass, xmpi_shell,
			XmNtitle, "XMPI Message Matrix",
                	XmNmappedWhenManaged, False,
			XmNmaxWidth, drawsize + XMPI_VWXWIDTH,
			XmNmaxHeight, drawsize + XMPI_VWXHEIGHT,
			XmNminWidth, minscrollsize,
			XmNminHeight, minscrollsize,
			NULL);
/*
 * Set matrix window callbacks.
 */
	XtAddCallback(matrix_w, XmNpopupCallback, popup_cb, NULL);
	XtAddCallback(matrix_w, XmNpopdownCallback, popdown_cb, NULL);
        XtAddCallback(matrix_w, XmNdestroyCallback, destroy_cb, NULL);

	mgr_w = XtVaCreateWidget("matrix_mgr",
			xmFormWidgetClass, matrix_w,
			NULL);
/*
 * Build the scrolled window.
 */
	scroll_w = XtVaCreateManagedWidget("matrix_view",
			xmScrolledWindowWidgetClass, mgr_w,
			XmNscrollingPolicy, XmAUTOMATIC,
			XmNheight, scrollsize,
			XmNwidth, scrollsize,
			XmNspacing, 0,
			XmNrightAttachment, XmATTACH_FORM,
			XmNleftAttachment, XmATTACH_FORM,
			XmNtopAttachment, XmATTACH_FORM,
			XmNbottomAttachment, XmATTACH_FORM,
			NULL);
/*
 * Create the matrix drawing area.
 */
	draw_w = XtVaCreateManagedWidget("matrix_draw",
			xmDrawingAreaWidgetClass, scroll_w,
			XmNwidth, drawsize,
			XmNheight, drawsize,
			NULL);

	XtAddCallback(draw_w, XmNexposeCallback, expose_cb, NULL);

	XtManageChild(mgr_w);
/*
 * Initialize graphic contexts.
 */
	gcinit();
/*
 * Create the matrix pixmap.
 */
	pixmap = XCreatePixmap(disp, root, drawsize, drawsize, depth);
}

/*
 *	xmpi_mat_destroy
 *
 *	Function:	- destroys matrix window
 */
void
xmpi_mat_destroy()

{
	Widget		w;

	if (matrix_w) {
		XtUnmapWidget(matrix_w);
		w = matrix_w;
		mat_clear();
		XtDestroyWidget(w);
		fl_up = 0;
	}
}

/*
 *	destroy_cb
 *
 *	Function:	- matrix window destroy callback
 */
static void
destroy_cb()

{
	mat_clear();
}

/*
 *	mat_clear
 *
 *	Function:	- cleans up resources associated with matrix window
 */
static void
mat_clear()

{
	int		i;

	if (!matrix_w) return;
/*
 * Flushing the dispay here eliminates a BadGC (invalid GC parameter)
 * error that occurs in some cases.  I am not quite sure what the exact
 * cause of the error is but I suspect it is some attempt to update the
 * matrix window after the GCs have been freed.  
 */
	XmUpdateDisplay(matrix_w);
	matrix_w = 0;
/*
 * Free information storage space.
 */
	free(mat_src);
/*
 * Destroy pixmaps and pens.
 */
	for (i = 0; i <= XMPI_MTCNTMAX; ++i) {
		XFreePixmap(disp, src_pix[i]);
	}

	XFreePixmap(disp, pixmap);
	XFreeGC(disp, lblpen);
	XFreeGC(disp, bgpen);
	XFreeGC(disp, gridpen);
}

/*
 *	popup_cb
 *
 *	Function:	- matrix popup callback
 *	Accepts:	- widget
 */
static void
popup_cb(w)

Widget			w;

{
	fl_up = 1;
	XtManageChild(mgr_w);
}

/*
 *	popdown_cb
 *
 *	Function:	- matrix popdown callback
 *	Accepts:	- widget
 */
static void
popdown_cb(w)

Widget			w;

{
	fl_up = 0;
}

/*
 *	expose_cb
 *
 *	Function:	- callback function for expose events
 *	Accepts:	- widget
 *			- client data
 *			- ptr callback struct
 */
static void
expose_cb(w, cdata, p_callback)

Widget			w;
XtPointer		cdata;
XmDrawingAreaCallbackStruct
			*p_callback;

{
	XEvent		*event;

	event = p_callback->event;

	XCopyArea(event->xany.display, pixmap, event->xany.window,
			lblpen, 0, 0, drawsize, drawsize, 0, 0);
}

/*
 *	gcinit
 *
 *	Function:	- allocate graphic contexts
 */
static void
gcinit()

{
	Pixel		fg, bg;			/* fore/background colours */
	XGCValues	gcval;			/* graphic context info */
	XColor		col_def;		/* colour definition */
	XColor		dev_null;		/* unused functionality */
	Pixmap		bm;			/* source count bitmap */
	GC		pen;			/* src cnt bitmap GC */
	int		i;

	disp = XtDisplay(draw_w);
	root = RootWindowOfScreen(XtScreen(draw_w));
	depth = DefaultDepthOfScreen(XtScreen(draw_w));

	XtVaGetValues(draw_w, XmNforeground, &fg, XmNbackground, &bg, NULL);
/*
 * Create the label, grid and background pens.
 */
	gcval.line_width = XMPI_MTLNWIDTH;
	gcval.foreground = bg;
	gcval.background = fg;

	gridpen = XCreateGC(disp, root,
			GCLineWidth | GCForeground | GCBackground, &gcval);

	gcval.foreground = fg;
	bgpen = XCreateGC(disp, root, GCForeground, &gcval);

	XAllocNamedColor(disp, DefaultColormapOfScreen(XtScreen(draw_w)),
				XMPI_LBLCOLOUR, &col_def, &dev_null);

	gcval.foreground = col_def.pixel;
	gcval.background = fg;
	gcval.font = app_res.ap_rankfont->fid;
	lblpen = XCreateGC(disp, root,
			GCFont | GCForeground | GCBackground, &gcval);
/*
 * Create the source message count pixmaps.
 */
	for (i = 0; i <= XMPI_MTCNTMAX; ++i) {
		bm = XCreateBitmapFromData(disp, root,
				(const char*)src_bits[i], src1_width, src1_height);

		XAllocNamedColor(disp,
				DefaultColormapOfScreen(XtScreen(draw_w)),
				src_colours[i], &col_def, &dev_null);

		gcval.foreground = col_def.pixel;
		gcval.background = fg;
		gcval.clip_mask = bm;
		pen = XCreateGC(disp, root,
				GCForeground | GCBackground | GCClipMask,
				&gcval);

		src_pix[i] = XCreatePixmapFromBitmapData(disp, root,
				(char*)src_bits[i], src1_width, src1_height, bg, fg,
				depth);

		XCopyPlane(disp, bm, src_pix[i], pen, 0, 0,
				src1_width, src1_height, 0, 0,
				(unsigned long) 1);

		XFreePixmap(disp, bm);
		XFreeGC(disp, pen);
	}
}

/*
 *	xmpi_mat_update
 *
 *	Function:	- get matrix info from dbase and plot it
 *	Accepts:	- process status information
 */
void
xmpi_mat_update(mat_procs)

struct xmproc		*mat_procs;

{
	int		i, j;
	Dimension	x, y;			/* coordinates */
	Pixmap		pix;			/* source count pixmap */
	struct xmmsg	*p;			/* message aggregate */

	if (!fl_up) return;
/*
 * Clear the screen.
 */
	XFillRectangle(disp, pixmap, bgpen, 0, 0, drawsize, drawsize);
	mat_plot_grid();

	y = bordersize;

	for (i = 0; i < mat_nprocs; ++i) {

		if (!mat_procs[i].xmp_msgs) {
			y += XMPI_MTCELLSIZE;
			continue;
		}

		for (j = 0; j < mat_nprocs; ++j) {
			mat_src[j] = 0;
		}

		p = al_top(mat_procs[i].xmp_msgs);

		while (p) {

			if (p->xmm_nmsg > 0) {
				mat_src[p->xmm_gsrc] += p->xmm_nmsg;
			}

			p = al_next(mat_procs[i].xmp_msgs, p);
		}

		x = bordersize;

		for (j = 0; j < mat_nprocs; ++j) {

			if (mat_src[j] == 0) {
				x += XMPI_MTCELLSIZE;
				continue;
			}

			if (mat_src[j] > XMPI_MTCNTMAX) {
				pix = src_pix[XMPI_MTCNTMAX];
			}

			pix = src_pix[mat_src[j] - 1];

			XCopyArea(disp, pix, pixmap, bgpen,
					0, 0, src1_width, src1_height,
					x + XMPI_MTCNTINSET,
					y + XMPI_MTCNTINSET);

			x += XMPI_MTCELLSIZE;
		}

		y += XMPI_MTCELLSIZE;
	}

	XCopyArea(disp, pixmap, XtWindow(draw_w), bgpen,
			0, 0, drawsize, drawsize, 0, 0);
}

/*
 *	mat_plot_grid
 *
 *	Function:	- plots the matrix outline and labels
 */
static void
mat_plot_grid()

{
	int		i;
	int		x, y;			/* coordinates */
	int		w, h;			/* width and height */
	int		dir;			/* string direction */
	int		ascent;			/* font ascent */
	int		descent;		/* font descent */
	int		fmtlen;			/* string length */
	char		*destxt, *srctxt;	/* labels */
	char		fmtbuf[8];		/* formatting buffer */
	XCharStruct	fmtinfo;		/* string font info */
	XFontStruct	*finfo;			/* font information */
/*
 * Clear the screen.
 */
	XFillRectangle(disp, pixmap, bgpen, 0, 0, drawsize, drawsize);
/*
 * Plot the horizontal grid lines.
 */
	x = bordersize;
	y = bordersize;

	for (i = 0; i <= mat_nprocs; ++i) {
		XDrawLine(disp, pixmap, gridpen, x, y,
				x + (XMPI_MTCELLSIZE * mat_nprocs), y);
		y += XMPI_MTCELLSIZE;
	}
/*
 * Plot the vertical grid lines.
 */
	x = bordersize;
	y = bordersize;

	for (i = 0; i <= mat_nprocs; ++i) {
		XDrawLine(disp, pixmap, lblpen, x, y,
				x, y + (XMPI_MTCELLSIZE * mat_nprocs));

		x += XMPI_MTCELLSIZE;
	}
/*
 * Plot the process rank labels.
 */
	for (i = 0; i < mat_nprocs; ++i) {
                sprintf(fmtbuf, "%d", i);
                fmtlen = strlen(fmtbuf);
 
		XTextExtents(app_res.ap_rankfont, fmtbuf, fmtlen,
				&dir, &ascent, &descent, &fmtinfo);
 
		x = bordersize - ((XMPI_MTLNWIDTH / 2) + 1);
		y = bordersize + (XMPI_MTCELLSIZE * i) + (XMPI_MTCELLSIZE / 2);
		XDrawString(disp, pixmap, lblpen, x - fmtinfo.width,
				y + (fmtinfo.ascent / 2), fmtbuf, fmtlen);

		x = bordersize + (XMPI_MTCELLSIZE * i) + (XMPI_MTCELLSIZE / 2);
		y = bordersize - ((XMPI_MTLNWIDTH / 2) + 2);
        	XDrawString(disp, pixmap, lblpen, x - (fmtinfo.width / 2),
				y, fmtbuf, fmtlen);
	}

	if (mat_nprocs > 1) {
		if (mat_nprocs > 3) {
			srctxt = "source";
			destxt = "destination";
		} else {
			srctxt = "src";
			destxt = "des";
		}
/*
 * Plot the source ranks label.
 */
		finfo = XQueryFont(disp, app_res.ap_rankfont->fid);

		XTextExtents(app_res.ap_rankfont, srctxt, strlen(srctxt),
				&dir, &ascent, &descent, &fmtinfo);

		x = drawsize / 2 - (fmtinfo.width / 2);
		y = bordersize - ((XMPI_MTLNWIDTH / 2) + 4)
		    - 2*(finfo->max_bounds.descent + finfo->max_bounds.descent);

		XDrawString(disp, pixmap, lblpen, x, y, srctxt, strlen(srctxt));
/*
 * Plot the destination ranks label.
 */
		xmpi_vstr_extent(destxt, app_res.ap_rankfont, &w, &h);

		x = bordersize - ((XMPI_MTLNWIDTH / 2) + 2)
			- finfo->max_bounds.width - w;

		y = (drawsize - h) / 2;

		xmpi_vstr_draw(disp, pixmap, lblpen,
			x, y, destxt, app_res.ap_rankfont);

		XFreeFontInfo((char **) 0, finfo, 1);
	}
/*
 * Refresh the screen.
 */
	XCopyArea(disp, pixmap, XtWindow(draw_w), gridpen,
			0, 0, drawsize, drawsize, 0, 0);
}

/*
 *	xmpi_mat_busy
 *
 *	Function:	- set the busy cursor for the matrix window
 */
void
xmpi_mat_busy()

{
	if (matrix_w) xmpi_busy_widget(matrix_w);
}

/*
 *	xmpi_mat_unbusy
 *
 *	Function:	- set the normal cursor for the matrix window
 */
void
xmpi_mat_unbusy()

{
	if (matrix_w) xmpi_unbusy_widget(matrix_w);
}