File: pvmgsu_aux.c

package info (click to toggle)
pvm 3.4.6-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,236 kB
  • sloc: ansic: 72,074; makefile: 1,197; fortran: 631; sh: 512; csh: 74; asm: 37
file content (1108 lines) | stat: -rw-r--r-- 34,013 bytes parent folder | download | duplicates (4)
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108

static char rcsid[] =
	"$Id: pvmgsu_aux.c,v 1.10 2007/04/19 23:15:33 pvmsrc Exp $";

/*
 *         PVM version 3.4:  Parallel Virtual Machine System
 *               University of Tennessee, Knoxville TN.
 *           Oak Ridge National Laboratory, Oak Ridge TN.
 *                   Emory University, Atlanta GA.
 *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
 *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
 *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
 *                   (C) 1997 All Rights Reserved
 *
 *                              NOTICE
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted
 * provided that the above copyright notice appear in all copies and
 * that both the copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * Neither the Institutions (Emory University, Oak Ridge National
 * Laboratory, and University of Tennessee) nor the Authors make any
 * representations about the suitability of this software for any
 * purpose.  This software is provided ``as is'' without express or
 * implied warranty.
 *
 * PVM version 3 was funded in part by the U.S. Department of Energy,
 * the National Science Foundation and the State of Tennessee.
 */

/*
 *	pvmgsu_aux.c - auxiliary group library routines, gather and scatter  
 *       6 Jun 1995     Native mode reduce for Paragon. Donato
 *       8 Mar 1994     Added reduce & assoc routines. Donato & P.Papadopoulos
 *      24 Apr 1994     Added scatter, gather, gs_get_datasize routines. Donato
 *
 */

#ifdef HASSTDLIB
#include <stdlib.h>
#endif
#include <stdio.h>
#include <pvm3.h>
#include "pvmalloc.h"
#include "bfunc.h"
#include "lpvm.h"
#include <pvmtev.h>
#include "tevmac.h"
#include "global.h"
#include "pvmmimd.h"
#include "pvmgsd.h"
#include "pvmproto.h"

#ifdef  SYSVSTR
#include <string.h>
#else
#include <strings.h>
#endif

extern int pvm_errno;
extern int pvmmytid;
extern int pvmtoplvl;
extern struct Pvmtracer pvmtrc;



/* ==========                                         ================ 
 * ========== Declarations & Routines for the Paragon ================
 * ==========                                         ================ 
 * Note:  int and long are the same on the paragon 
 */

#if defined(IMA_PGON)

extern int pvmpgonpartsize;           /* from lpvmmimd.c */

/* ================ NativeFunction() for the PGON ====================     */
/* This routine compares the address of the user provided function
   to the built-in pvm functions to determine and then call the
   appropriate native mode function on the Paragon.
*/
int
NativeFunction(user_func, datatype, x, y, num)
#ifdef IMA_SCO
  void (*user_func)(int*, void*, void*, int*, int*);
#else
  void (*user_func)();
#endif
void *x, *y;
int  datatype, num;
{
    if (user_func==PvmSum)
    {
        switch(datatype)
        {
            case (PVM_INT):
            case (PVM_LONG):
                _gisum((int *) x, num, (int *) y);
                break;
            case (PVM_FLOAT):
                _gssum((float *) x, num, (float *) y);
                break;
            case (PVM_DOUBLE):
                _gdsum((double *) x, num, (double *) y);
                break;
            default:
                return(PvmNotImpl);
        }   /* end switch */
    }
    else
    if (user_func==PvmMax)
    {
        switch(datatype)
        {
            case (PVM_INT):
            case (PVM_LONG):
                _gihigh((int *) x, num, (int *) y);
                break;
            case (PVM_FLOAT):
                _gshigh((float *) x, num, (float *) y);
                break;
            case (PVM_DOUBLE):
                _gdhigh((double *) x, num, (double *) y);
                break;
            default:
                return(PvmNotImpl);
        }   /* end switch */
    }
    else
    if (user_func==PvmMin)
    {
        switch(datatype)
        {
            case (PVM_INT):
            case (PVM_LONG):
                _gilow((int *) x, num, (int *) y);
                break;
            case (PVM_FLOAT):
                _gslow((float *) x, num, (float *) y);
                break;
            case (PVM_DOUBLE):
                _gdlow((double *) x, num, (double *) y);
                break;
            default:
                return(PvmNotImpl);
        }   /* end switch */
    }
    else
    if (user_func==PvmProduct)
    {
        switch(datatype)
        {
            case (PVM_INT):
            case (PVM_LONG):
                _giprod((int *) x, num, (int *) y);
                break;
            case (PVM_FLOAT):
                _gsprod((float *) x, num, (float *) y);
                break;
            case (PVM_DOUBLE):
                _gdprod((double *) x, num, (double *) y);
                break;
            default:
                return(PvmNotImpl);
        }  /* end switch */
    }
    else
        return(PvmNotImpl);

    return(PvmOk);

}  /* end NativeFunction() */


#endif /* #ifdef PGON */



/* ================ pvm_reduce() =====================================     */
/*
  int info = pvm_reduce(void (*func)(), void *data, int count, int datatype,
                        int msgtag, char *gname, int rootinst)
  where
      void (*func)(int *datatype, void *data, void *work, int *num, int *info)

  Currently, this implementation uses a fan in algorithm to perform
  the reduce operation.

  Each group member sends their data to the coordinator on their host machine.
  The coordinator performs the specified function combining its own data and 
  the data from the group members on the same host.  
  
  On the Paragon the nx native global operations are utilized if all the
  nodes of the Paragon are part of the group.

  The coordinators then pass their results on to the specified root node 
  of the reduce operation.
*/

int pvm_reduce(func, data, count, datatype, msgtag, gname, rootinst)
#ifdef  IMA_SCO
    void (*func)(int*, void*, void*, int*, int*);
#else
    void (*func)();
#endif
    void *data;
    int count, datatype, msgtag, rootinst;
    char *gname; 
{
    int cnt, roottid, datasize, cc=PvmOk, rbuf, sbuf;
    int coordinator, nmembers_on_host, nhosts_in_group, mask=0;
    void *work = NULL;      /* work array to be allocated */

    int  (*packfunc)(), (*unpackfunc)();
    int x;
	int savectx;

	TEV_DECLS

    BGN_TRACE( TEV_REDUCE, gname, TEV_DID_MC, &msgtag );

    rbuf = pvm_setrbuf(0);            /* set receive buf */
    sbuf = pvm_mkbuf(PvmDataDefault);
    sbuf = pvm_setsbuf(sbuf);

	/* set context for dynamic groups */
	savectx = pvm_setcontext( SYSCTX_DG );

    if ( (data == NULL) || (count <= 0) ) /* check some parameters */
    {
      cc = PvmBadParam;
      goto done;
    }
  
    /* get instance number - caller must be in group, root must be in group */
    if ( (cc = pvm_getinst(gname, pvmmytid))            < PvmOk ) goto done;
    if ( (cc = roottid  = pvm_gettid (gname, rootinst)) < PvmOk ) goto done;
    if ( (cc = datasize = gs_get_datasize(datatype))    < PvmOk ) goto done;

    /* set up pointers to the appropriate pack and unpack routines */
    if ( (cc = gs_pack_unpack(datatype, &packfunc, &unpackfunc) ) < PvmOk)
        goto done;

    if ((work = (void *) PVM_ALLOC(count*datasize, "pvm_reduce")) == NULL)
    {
        cc = PvmNoMem;
        goto done;
    }

    pvm_grphostinfo(gname, gs_tidtohost(pvmmytid), &coordinator, 
                    &nmembers_on_host, &nhosts_in_group);

#if defined(IMA_PGON)
    /*  if all the nodes are participating,  
        then call the Native mode version, if one exists.
        The native function call is a side-effect of the call to NativeFunction.
        If the coordinator and roottid are PGON nodes, there is no
        need to differentiate (e.g. pass data from coordinator to roottid).
    */

    mask = TIDHOST | TIDPTYPE;

    if ( TIDISNODE(pvmmytid) && 
       ( pvmpgonpartsize == nmembers_on_host ) &&
       ( (cc = NativeFunction(func, datatype, data, work, count)) == PvmOk) )
    {
        if ( TIDISNODE(roottid) && (pvmmytid & mask) == (roottid & mask) )
            coordinator = roottid;
    }
    else
#endif    /* end if defined(IMA_PGON) */
    {
        if ( (pvmmytid==coordinator) && (nmembers_on_host>1) )
        {
            /* recv data from other group members on same host, perform func */
            for (cnt = nmembers_on_host-1; cnt>0; cnt--)
            {
                if ( (cc = pvm_recv(-1, msgtag) )          < PvmOk) goto done;
                if ( (cc = (*unpackfunc)( work, count, 1)) < PvmOk) goto done;
                (*func)( &datatype, data, work, &count, &cc );
                if (cc < PvmOk) goto done;           /* error flag from func */
            }  
        }
        else if (pvmmytid != coordinator)
        {
            /* send data to the data coordinator on this same host */
            pvm_initsend(PvmDataDefault);
            if ( (cc = (*packfunc)( data, count, 1) )  < PvmOk ) goto done;
            if ( (cc = pvm_send( coordinator, msgtag)) < PvmOk ) goto done;;
        }
    } 

    if ( (pvmmytid==coordinator) && (pvmmytid != roottid) )
    {
        /* send data to the roottid for the reduce operation */
        pvm_initsend(PvmDataDefault);
        if ( (cc = (*packfunc)( data, count, 1) )  < PvmOk ) goto done;
        if ( (cc = pvm_send( roottid, msgtag)) < PvmOk ) goto done;
    } 

    /* Root node of the reduce operation: 
       - get data from my host coordinator, if necessary
       - get work values from each of the other hosts
       - perform the specified functions on data from other hosts
    */
    if (pvmmytid == roottid) 
    {
        /* if root isn't the host coordinator, receive from coordinator 1st */
        if  (pvmmytid != coordinator) 
        {
            if ( (cc = pvm_recv(coordinator, msgtag) ) < PvmOk) goto done;
            if ( (cc = (*unpackfunc)( data, count, 1)) < PvmOk) goto done;
        }

        if (nhosts_in_group-- <=  0) goto done;

        /* recv data from other group members on diff host, perform func */
        for (cnt = nhosts_in_group; cnt>0; cnt--)
        {
            if ( (cc = pvm_recv(-1, msgtag) )          < PvmOk) goto done;
            if ( (cc = (*unpackfunc)( work, count, 1)) < PvmOk) goto done;
            (*func)( &datatype, data, work, &count, &cc );
            if (cc < PvmOk) goto done;               /* error flag from func */
        }   /* end for */
    }
    
    cc = PvmOk;
    
  done:
  
    /* restore user's buffers */
    pvm_freebuf(pvm_setrbuf(rbuf));
    pvm_freebuf(pvm_setsbuf(sbuf));

	/* restore user context */
	pvm_setcontext( savectx );

    if (work != NULL) PVM_FREE(work);         /* free work space  */

    if (cc < 0) lpvmerr("pvm_reduce",cc);
  
    END_TRACE( TEV_REDUCE, TEV_DID_CC, &cc );

    return(cc);

} /* end pvm_reduce() */



/* ================ gs_pack_unpack()==================================     */
/*  
    int info = gs_pack_unpack( int datatype, 
                               int (**packfunc)(), int (**unpackfunc)() )

    Sets up pointers to the appropriate pack and unpack function based
    on datatype specified.
*/

int 
gs_pack_unpack(datatype, packfunc, unpackfunc)
int datatype, (**packfunc)(), (**unpackfunc)();
{

    switch(datatype) 
    {
        case (PVM_STR):  
            *packfunc = pvm_pkstr; 
            *unpackfunc = pvm_upkstr; 
            break;
        case (PVM_BYTE):  
            *packfunc = pvm_pkbyte; 
            *unpackfunc = pvm_upkbyte; 
            break;
        case (PVM_SHORT):
            *packfunc = pvm_pkshort; 
            *unpackfunc = pvm_upkshort; 
            break;
        case (PVM_INT):
            *packfunc = pvm_pkint;
            *unpackfunc = pvm_upkint; 
            break;
        case (PVM_LONG):      
            *packfunc = pvm_pklong; 
            *unpackfunc = pvm_upklong; 
            break;
        case (PVM_FLOAT):
            *packfunc = pvm_pkfloat; 
            *unpackfunc = pvm_upkfloat; 
            break;
        case (PVM_DOUBLE):
            *packfunc = pvm_pkdouble; 
            *unpackfunc = pvm_upkdouble; 
            break;
        case (PVM_CPLX):
            *packfunc = pvm_pkcplx; 
            *unpackfunc = pvm_upkcplx; 
            break;
        case (PVM_DCPLX):
            *packfunc = pvm_pkdcplx; 
            *unpackfunc = pvm_upkdcplx; 
            break;
        default:
            return(PvmBadParam);
    }

    return(PvmOk);
}   
/* ================ PvmMax()==========================================     */
/* 
  void PvmMax(int *datatype, void *x, void *y, int *num, int *info)
  Assigns the elements of x the maximum value between the
  corresponding elements of x and y.  
  For complex values the maximum is determined by maximum modulus.
*/

void 
PvmMax(datatype, x, y, num, info)
int *datatype;
void *x, *y; 
int *num, *info;
{
    char   *xchar,   *ychar;
    int    *xint,    *yint;
    short  *xshort,  *yshort;
    long   *xlong,   *ylong;
    float  *xfloat,  *yfloat;
    double *xdouble, *ydouble;
    float   xfreal, xfimag, yfreal, yfimag;
    double  xdreal, xdimag, ydreal, ydimag;
    float   xsqrfloat, ysqrfloat;
    double  xsqrdouble, ysqrdouble;
    
    int i, count;
  
    count = *num;
  
    switch(*datatype) 
    {
        case (PVM_BYTE):
            xchar = (char *) x;
            ychar = (char *) y;
            for (i=0; i<count; i++) xchar[i] = MAX(xchar[i], ychar[i]);
            break;
        case (PVM_SHORT):
            xshort = (short *) x;
            yshort = (short *) y;
            for (i=0; i<count; i++) xshort[i] = MAX(xshort[i], yshort[i]);
            break;
        case (PVM_INT):
            xint = (int *) x;
            yint = (int *) y;
            for (i=0; i<count; i++) xint[i] = MAX(xint[i], yint[i]);
            break;
        case (PVM_LONG):
            xlong = (long *) x;
            ylong = (long *) y;
            for (i=0; i<count; i++) xlong[i] = MAX(xlong[i], ylong[i]);
            break;
        case (PVM_FLOAT):
            xfloat = (float *) x;
            yfloat = (float *) y;
            for (i=0; i<count; i++) xfloat[i] = MAX(xfloat[i], yfloat[i]);
            break;
        case (PVM_DOUBLE):
            xdouble = (double *) x;
            ydouble = (double *) y;
            for (i=0; i<count; i++) xdouble[i] = MAX(xdouble[i], ydouble[i]);
            break;
        case (PVM_CPLX):
            /* complex - complex*8 in fortran - treated as two floats */
            /* returns the complex pair with the greatest magnitude */
            xfloat = (float *) x;
            yfloat = (float *) y;
            for (i=0; i<2*count; i+=2)
            {
                xfreal = xfloat[i];
                xfimag = xfloat[i+1];
                yfreal = yfloat[i];
                yfimag = yfloat[i+1];
                xsqrfloat = xfreal*xfreal + xfimag*xfimag;
                ysqrfloat = yfreal*yfreal + yfimag*yfimag;
                if (ysqrfloat > xsqrfloat)
                {
                    xfloat[i]   = yfreal;
                    xfloat[i+1] = yfimag;
                }
            }
            break;
        case (PVM_DCPLX):
            /* double complex - complex*16 in fortran - treated as 2 doubles */
            /* returns the complex pair with the greatest magnitude */
            xdouble = (double *) x;
            ydouble = (double *) y;
            for (i=0; i<2*count; i+=2)
            {
                xdreal = xdouble[i];
                xdimag = xdouble[i+1];
                ydreal = ydouble[i];
                ydimag = ydouble[i+1];
                xsqrdouble = xdreal*xdreal + xdimag*xdimag;
                ysqrdouble = ydreal*ydreal + ydimag*ydimag;
                if (ysqrdouble > xsqrdouble)
                {
                    xdouble[i]   = ydreal;
                    xdouble[i+1] = ydimag;
                }
            }
            break;
        default:
            *info = PvmBadParam;
            return;
    }  /* end switch */

    *info = PvmOk;
    return;

}  /* end of PvmMax() */



/* ================ PvmMin()==========================================     */
/* 
  void PvmMin(int *datatype, void *x, void *y, int *num, int *info)

  Assigns the elements of x the minimum value between the
  corresponding elements of x and y.
  For complex values the minimum is determined by minimum modulus.

*/

void 
PvmMin(datatype, x, y, num, info)
int *datatype;
void *x, *y;
int  *num, *info;
{
    char   *xchar,   *ychar;
    short  *xshort,  *yshort;
    int    *xint,    *yint;
    long   *xlong,   *ylong;
    float  *xfloat,  *yfloat;
    double *xdouble, *ydouble;
    float   xfreal, xfimag, yfreal, yfimag;
    double  xdreal, xdimag, ydreal, ydimag;
    float   xsqrfloat, ysqrfloat;
    double  xsqrdouble, ysqrdouble;
  
    int i, count;
  
    count = *num;
  
    switch(*datatype) 
    {
        case (PVM_BYTE):
            xchar = (char *) x;
            ychar = (char *) y;
            for (i=0; i<count; i++) xchar[i] = MIN(xchar[i], ychar[i]);
            break;
        case (PVM_SHORT):
            xshort = (short *) x;
            yshort = (short *) y;
            for (i=0; i<count; i++) xshort[i] = MIN(xshort[i], yshort[i]);
            break;
        case (PVM_INT):
            xint = (int *) x;
            yint = (int *) y;
            for (i=0; i<count; i++) xint[i] = MIN(xint[i], yint[i]);
            break;
        case (PVM_LONG):
            xlong = (long *) x;
            ylong = (long *) y;
            for (i=0; i<count; i++) xlong[i] = MIN(xlong[i], ylong[i]);
            break;
        case (PVM_FLOAT):
            xfloat = (float *) x;
            yfloat = (float *) y;
            for (i=0; i<count; i++) xfloat[i] = MIN(xfloat[i], yfloat[i]);
            break;
        case (PVM_DOUBLE):
            xdouble = (double *) x;
            ydouble = (double *) y;
            for (i=0; i<count; i++) xdouble[i] = MIN(xdouble[i], ydouble[i]);
            break;
        case (PVM_CPLX):
            /* complex - complex*8 in fortran - treated as two floats */
            /* returns the complex pair with the smaller magnitude */
            xfloat = (float *) x;
            yfloat = (float *) y;
            for (i=0; i<2*count; i+=2)
            {
                xfreal = xfloat[i];
                xfimag = xfloat[i+1];
                yfreal = yfloat[i];
                yfimag = yfloat[i+1];
                xsqrfloat = xfreal*xfreal + xfimag*xfimag;
                ysqrfloat = yfreal*yfreal + yfimag*yfimag;
                if (ysqrfloat < xsqrfloat)
                {
                    xfloat[i]   = yfreal;
                    xfloat[i+1] = yfimag;
                }
            }
            break;
        case (PVM_DCPLX):
            /* double complex - complex*16 in fortran - treated as 2 doubles */
            /* returns the complex pair with the smaller magnitude */
            xdouble = (double *) x;
            ydouble = (double *) y;
            for (i=0; i<2*count; i+=2)
            {
                xdreal = xdouble[i];
                xdimag = xdouble[i+1];
                ydreal = ydouble[i];
                ydimag = ydouble[i+1];
                xsqrdouble = xdreal*xdreal + xdimag*xdimag;
                ysqrdouble = ydreal*ydreal + ydimag*ydimag;
                if (ysqrdouble < xsqrdouble)
                {
                    xdouble[i]   = ydreal;
                    xdouble[i+1] = ydimag;
                }
            }
            break;
        default:
            *info = PvmBadParam;
            return;
    }  /* end switch */
     
    *info = PvmOk;
    return;

}  /* end of PvmMin() */


/* ================ PvmSum()==========================================     */

/* 
  void PvmSum(int *datatype, void *x, void *y, *num, *info)

  Assigns the elements of x the sum of the corresponding elements of x and y.
*/

void 
PvmSum(datatype, x, y, num, info)
int *datatype;
void *x, *y;
int *num, *info;
{
    short  *xshort,  *yshort;
    int    *xint,    *yint;
    long   *xlong,   *ylong;
    float  *xfloat,  *yfloat;
    double *xdouble, *ydouble;
  
    int i, count;
  
    count = *num;
  
    switch(*datatype) 
    {
        case (PVM_SHORT):
            xshort = (short *) x;
            yshort = (short *) y;
            for (i=0; i<count; i++) xshort[i] += yshort[i];
            break;
        case (PVM_INT):
            xint = (int *) x;
            yint = (int *) y;
            for (i=0; i<count; i++) xint[i] += yint[i];
            break;
        case (PVM_LONG):
            xlong = (long *) x;
            ylong = (long *) y;
            for (i=0; i<count; i++) xlong[i] += ylong[i];
            break;
        case (PVM_FLOAT):
            xfloat = (float *) x;
            yfloat = (float *) y;
            for (i=0; i<count; i++) xfloat[i] += yfloat[i];
            break;
        case (PVM_DOUBLE):
            xdouble = (double *) x;
            ydouble = (double *) y;
            for (i=0; i<count; i++) xdouble[i] += ydouble[i];
            break;
        case (PVM_CPLX):
            /* complex - complex*8 in fortran - treated as two floats */
            /* returns the sum of the two complex pairs */
            xfloat = (float *) x;
            yfloat = (float *) y;
            for (i=0; i<2*count; i++) xfloat[i]  += yfloat[i];
            break;
        case (PVM_DCPLX):
            /* double complex - complex*16 in fortran - treated as 2 doubles */
            /* returns the sum of the two complex pairs */
            xdouble = (double *) x;
            ydouble = (double *) y;
            for (i=0; i<2*count; i++) xdouble[i]   += ydouble[i];
            break;
        default:
            *info = PvmBadParam;
            return;
    }  /* end switch */

    *info = PvmOk;
    return;

}  /* end of PvmSum() */


/* ================ PvmProduct()======================================     */
/* 
  void PvmProduct(int *datatype, void *x, void *y, *num, *info)

  Assigns the elements of x the sum of the corresponding elements of x and y.
*/

void 
PvmProduct(datatype, x, y, num, info)
int *datatype;
void *x, *y;
int *num, *info;
{
    short  *xshort,  *yshort;
    int    *xint,    *yint;
    long   *xlong,   *ylong;
    float  *xfloat,  *yfloat, a,b,c,d;
    double *xdouble, *ydouble, da,db,dc,dd;
  
    int i, count;
  
    count = *num;
  
    switch(*datatype) 
    {
        case (PVM_SHORT):
            xshort = (short *) x;
            yshort = (short *) y;
            for (i=0; i<count; i++) xshort[i] *= yshort[i];
            break;
        case (PVM_INT):
            xint = (int *) x;
            yint = (int *) y;
            for (i=0; i<count; i++) xint[i] *= yint[i];
            break;
        case (PVM_LONG):
            xlong = (long *) x;
            ylong = (long *) y;
            for (i=0; i<count; i++) xlong[i] *= ylong[i];
            break;
        case (PVM_FLOAT):
            xfloat = (float *) x;
            yfloat = (float *) y;
            for (i=0; i<count; i++) xfloat[i] *= yfloat[i];
            break;
        case (PVM_DOUBLE):
            xdouble = (double *) x;
            ydouble = (double *) y;
            for (i=0; i<count; i++) xdouble[i] *= ydouble[i];
            break;
        case (PVM_CPLX):
            /* complex - complex*8 in fortran - treated as two floats */
            /* returns the product of the two complex pairs */
            xfloat = (float *) x;
            yfloat = (float *) y;
            for (i=0; i<2*count; i += 2) 
            {
                a = xfloat[i]; b = xfloat[i+1], c = yfloat[i]; d = yfloat[i+1];
                xfloat[i] = a*c - b*d;
                xfloat[i+1] = a*d + b*c;
            }
            break;
        case (PVM_DCPLX):
            /* double complex - complex*16 in fortran - treated as 2 doubles */
            /* returns the  product of the two complex pairs */
            xdouble = (double *) x;
            ydouble = (double *) y;
            for (i=0; i<2*count; i+= 2) 
            {
                da = xdouble[i]; db = xdouble[i+1], 
                dc = ydouble[i]; dd = ydouble[i+1];
                xdouble[i] = da*dc - db*dd;
                xdouble[i+1] = da*dd + db*dc;
            }
            break;
        default:
            *info = PvmBadParam;
            return;
    }  /* end switch */

    *info = PvmOk;
    return;

}  /* end of PvmProduct() */


/* ================ pvm_gather()======================================     */
/*    
  int info = pvm_gather(void *result, void *data, int count, int datatype, 
                        int msgtag,  char *gname, int rootinst)

  Performs a gather of messages from each member of the group
  to a specified member of the group.

  Each member of the group 'gname' sends a message 'data' 
  of type 'datatype' and length 'count' to the root member of the group.
  The root receives these messages into a single array 'result'
  which is of length, at least, (number of group members)*'count'.
  The values received from the ith member of the group are
  placed into the 'result' array starting at position i*'count'.
  The root member of the group is specified by its instance number,
  'rootginst', in that group.
*/
 
int 
pvm_gather(result, data, count, datatype, msgtag, gname, rootinst)
void *result, *data;
int  count, datatype, msgtag, rootinst;
char *gname;
{
    int mytid, roottid, myginst, datasize, gsize, *tids = 0, i, cc;
    int sbuf, rbuf;
  
    int (*packfunc)(), (*unpackfunc)();  /* ptrs to pack and unpack functions */
    int x;
	int savectx;
  
	TEV_DECLS

    BGN_TRACE( TEV_GATHER, gname, TEV_DID_MC, &msgtag );

	/* set context for dynamic groups */
	savectx = pvm_setcontext( SYSCTX_DG );

    if ( (data == NULL) || (count <= 0) ) /* check some parameters */
    {
        cc = PvmBadParam;
        goto done;
    }
  
    /* set up pointers to the appropriate pack and unpack routines */
    if ( (cc = gs_pack_unpack(datatype, &packfunc, &unpackfunc) ) < 0)
        goto done;
  
    /* root must be member of the group */
    if ( (cc = roottid = pvm_gettid(gname,rootinst)) < 0 ) goto done;
  
    /* get instance number - caller must be in group */
    if ( (cc = myginst = pvm_getinst(gname, pvmmytid)) < 0 ) goto done;

    if (myginst == rootinst)     /* I am the root for the gather operation */
    {
        if ( result == NULL) /* check result parameter */
        {
            cc = PvmBadParam;
            goto done;
        }
  
        /* get the number of bytes per element of type datatype */
        if ( (cc = datasize = gs_get_datasize(datatype)) < 0  ) goto done;
  
        /* Get the list of tids.  These must be contiguous (no holes). */
        if ( (cc = gs_get_tidlist(gname, msgtag, &gsize, &tids, 1)) < 0)
            goto done;
  
        rbuf = pvm_setrbuf(0);
  
        /* Get the values, put them in the correct place in the result. 
           The instance numbers should be contiguous within the group.
        */
        for (i=0; i<gsize; i++)
        {
            /* The root copies its data into its result array */
            if (i == myginst) 
            {
                BCOPY((char *) data, (char *) result + i*datasize*count, 
                      datasize*count);
            }
            else
            {
                if ( (cc = pvm_recv( tids[i], msgtag )) < 0 )
                {
                    pvm_freebuf(pvm_setrbuf(rbuf)); /* restore user recv buf*/
                    goto done;
                }
                if ((cc=(*unpackfunc)((char *)result+i*datasize*count,count,1))<0)
                {
                    pvm_freebuf(pvm_setrbuf(rbuf)); /* restore user recv buf*/
                    goto done;
                }
  
            } /* end if (i == myginst) */
  
        } /* end for-loop */
  
        pvm_freebuf(pvm_setrbuf(rbuf)); /* restore user's receive buf */
    }
    else  
    {    /* everyone except the root sends data to the root */
        sbuf = pvm_mkbuf(PvmDataDefault);
        sbuf = pvm_setsbuf(sbuf);
     
        if ( (cc = (*packfunc)( data, count, 1)) < 0)
        {
          pvm_freebuf(pvm_setsbuf(sbuf)); /* restore user's send buf */
          goto done;
        }
        if ( (cc = pvm_send( roottid, msgtag)) < 0)
        {
          pvm_freebuf(pvm_setsbuf(sbuf)); /* restore user's send buf */
          goto done;
        }
        pvm_freebuf(pvm_setsbuf(sbuf)); /* restore user's send buf */
  
    }  /* end if-else */


  cc = PvmOk;

done:

	/* restore user context */
	pvm_setcontext( savectx );

    if (tids) free(tids);
    
    if (cc < 0) lpvmerr("pvm_gather",cc);
  
    END_TRACE( TEV_GATHER, TEV_DID_CC, &cc );

    return(cc);

}  /* end pvm_gather() */

/* ================ pvm_scatter()=====================================     */
/*    
  int info = pvm_scatter(void *result, int *data,  int count, int datatype, 
                         void msgtag, char *gname, int rootinst)

  Performs a scatter of messages from the specified root member of the
  group to each of the members of the group.

  Each member of the group 'gname' receives a message 'result' 
  of type 'datatype' and length 'count' from the root member of the group.
  The root sends these messages from a single array 'data'
  which is of length, at least, (number of group members)*'count'.
  The values sent to the ith member of the group are
  taken from the 'data' array starting at position i*'count'.
  The root member of the group is specified by its instance number,
  'rootginst', in that group.
*/
 
int 
pvm_scatter(result, data, count, datatype, msgtag, gname, rootinst)
void *result, *data;
int  count, datatype, msgtag, rootinst;
char *gname;
{
    int roottid, myginst, datasize, gsize, *tids = 0, i, cc;
    int sbuf, rbuf;

    int (*packfunc)(), (*unpackfunc)();  /* ptrs to pack and unpack functions */
    int x;
	int savectx;

	TEV_DECLS

    BGN_TRACE( TEV_SCATTER, gname, TEV_DID_MC, &msgtag );

	/* set context for dynamic groups */
	savectx = pvm_setcontext( SYSCTX_DG );

    if ( (result == NULL) || (count <= 0) ) /* check some parameters */
    {
        cc = PvmBadParam;
        goto done;
    }

    /* set up pointers to the appropriate pack and unpack routines */
    if ( (cc = gs_pack_unpack(datatype, &packfunc, &unpackfunc)) < 0)
        goto done;

    /* root must be member of the group */
    if ( (roottid = pvm_gettid(gname,rootinst)) < 0 )
    {
        cc = roottid;
        goto done;
    }

    /* get instance number - caller must be in group */
    if ( (cc = myginst = pvm_getinst(gname, pvmmytid)) < 0 ) goto done;

    /* I am the root node for the scatter operation */
    if (myginst == rootinst)
    {
        if ( data == NULL) /* check data parameter */
        {
            cc = PvmBadParam;
            goto done;
        }

        /* get the number of bytes per element of type datatype */
        if ( (cc = datasize = gs_get_datasize(datatype)) < 0  ) goto done;
  
        /* Get the list of tids.  These must be contiguous (no holes). */
        if ( (cc = gs_get_tidlist(gname, msgtag, &gsize, &tids, 1)) < 0)
            goto done;
  
        sbuf = pvm_mkbuf(PvmDataDefault);
  
        /* The root sends values to everyone, except itself, in the group.
           For itself, the root copies the data into its result array.
        */
        for (i=0; i<gsize; i++)
        {
            if (i == myginst)
              BCOPY((char *) data + i*datasize*count, (char *) result,
                    datasize*count);
            else
            {
                sbuf = pvm_initsend(PvmDataDefault);
                if ((cc=(*packfunc)((char *)data+i*datasize*count,count,1))<0)
                {
                pvm_freebuf(pvm_setsbuf(sbuf));   /* restore user's sendbuf */
                goto done;
                }
                if ( (cc = pvm_send( tids[i], msgtag)) < 0)
                {
                    pvm_freebuf(pvm_setsbuf(sbuf)); /* restore user's sendbuf */
                    goto done;
                }
            } /* end if-else */
        } /* end for-loop */
        pvm_freebuf(pvm_setsbuf(sbuf)); /* restore user's send buf */
    }
    else
    {
        /* everyone receives a result from the root, except the root */
        rbuf = pvm_setrbuf(0);
        if ( (cc = pvm_recv( roottid, msgtag )) < 0)
        {
            pvm_freebuf(pvm_setrbuf(rbuf)); /* restore user's receive buf */
            goto done;
        }
        if ( (cc = (*unpackfunc)( result, count, 1)) < 0)
        {
            pvm_freebuf(pvm_setrbuf(rbuf)); /* restore user's receive buf */
            goto done;
        }
        pvm_freebuf(pvm_setrbuf(rbuf)); /* restore user's receive buf */
    
    }  /* end if-else */
	
    cc = PvmOk;

done:

	/* restore user context */
	pvm_setcontext( savectx );

    if (tids) free(tids);
  
    if (cc < 0) lpvmerr("pvm_scatter",cc);

    END_TRACE( TEV_SCATTER, TEV_DID_CC, &cc);

    return(cc);

}  /* end pvm_scatter() */

/* ================ gs_get_datasize() ================================     */
/*    
  int info = gs_get_datasize(int datatype)

  Returns the size in bytes of a single element of type datatype.
*/

int 
gs_get_datasize(datatype)
int datatype;
{

    switch (datatype)
    {
        case (PVM_STR):
        case (PVM_BYTE):
            return(sizeof(char));
        case (PVM_SHORT):
            return(sizeof(short));
        case (PVM_INT):
            return(sizeof(int));
        case (PVM_LONG):
            return(sizeof(long));
        case (PVM_FLOAT):
            return(sizeof(float));
        case (PVM_DOUBLE):
            return(sizeof(double));
        case (PVM_CPLX):
            return(2*sizeof(float));
        case (PVM_DCPLX):
            return(2*sizeof(double));
        default:
            return(PvmBadParam);
    }  /* end switch (datatype) */

}  /* end gs_get_datasize() */