1 /*  testIVL_Bcast.c  */
2 
3 #include "../spoolesMPI.h"
4 #include "../../Drand.h"
5 #include "../../timings.h"
6 
7 /*--------------------------------------------------------------------*/
8 int
main(int argc,char * argv[])9 main ( int argc, char *argv[] )
10 /*
11    ------------------------------------------------------------------
12    this program tests the IVL_MPI_Bcast() method
13 
14    (1) process root generates a random IVL object
15        and computes its checksum
16    (2) process root broadcasts the IVL object to the other processors
17    (3) each process computes the checksum of its IVL object
18    (4) the checksums are compared on root
19 
20    created -- 98sep10, cca
21    ------------------------------------------------------------------
22 */
23 {
24 char         *buffer ;
25 double       chksum, t1, t2 ;
26 double       *sums ;
27 Drand        drand ;
28 int          ilist, iproc, length, loc, maxlistsize, msglvl, myid,
29              nlist, nproc, root, seed, size ;
30 int          *list, *vec ;
31 IVL          *ivl ;
32 FILE         *msgFile ;
33 /*
34    ---------------------------------------------------------------
35    find out the identity of this process and the number of process
36    ---------------------------------------------------------------
37 */
38 MPI_Init(&argc, &argv) ;
39 MPI_Comm_rank(MPI_COMM_WORLD, &myid) ;
40 MPI_Comm_size(MPI_COMM_WORLD, &nproc) ;
41 fprintf(stdout, "\n process %d of %d, argc = %d", myid, nproc, argc) ;
42 fflush(stdout) ;
43 if ( argc != 7 ) {
44    fprintf(stdout,
45            "\n\n usage : %s msglvl msgFile nlist maxlistsize seed "
46            "\n    msglvl      -- message level"
47            "\n    msgFile     -- message file"
48            "\n    nlist       -- number of lists in the IVL object"
49            "\n    maxlistsize -- maximum size of a list"
50            "\n    root        -- root processor for broadcast"
51            "\n    seed        -- random number seed"
52            "\n", argv[0]) ;
53    return(0) ;
54 }
55 msglvl = atoi(argv[1]) ;
56 if ( strcmp(argv[2], "stdout") == 0 ) {
57    msgFile = stdout ;
58 } else {
59    length = strlen(argv[2]) + 1 + 4 ;
60    buffer = CVinit(length, '\0') ;
61    sprintf(buffer, "%s.%d", argv[2], myid) ;
62    if ( (msgFile = fopen(buffer, "w")) == NULL ) {
63       fprintf(stderr, "\n fatal error in %s"
64               "\n unable to open file %s\n",
65               argv[0], argv[2]) ;
66       return(-1) ;
67    }
68    CVfree(buffer) ;
69 }
70 nlist       = atoi(argv[3]) ;
71 maxlistsize = atoi(argv[4]) ;
72 root        = atoi(argv[5]) ;
73 seed        = atoi(argv[6]) ;
74 fprintf(msgFile,
75         "\n %s "
76         "\n msglvl      -- %d"
77         "\n msgFile     -- %s"
78         "\n nlist       -- %d"
79         "\n maxlistsize -- %d"
80         "\n root        -- %d"
81         "\n seed        -- %d"
82         "\n",
83         argv[0], msglvl, argv[2], nlist, maxlistsize, root, seed) ;
84 fflush(msgFile) ;
85 /*
86    --------------------------------------------
87    set up the IVL object and fill owned entries
88    --------------------------------------------
89 */
90 MARKTIME(t1) ;
91 ivl = IVL_new() ;
92 if ( myid == root ) {
93    IVL_init1(ivl, IVL_CHUNKED, nlist) ;
94    vec = IVinit(maxlistsize, -1) ;
95    Drand_setDefaultFields(&drand) ;
96    Drand_setSeed(&drand, seed) ;
97    Drand_setUniform(&drand, 0, maxlistsize) ;
98    for ( ilist = 0 ; ilist < nlist ; ilist++ ) {
99       size = (int) Drand_value(&drand) ;
100       Drand_fillIvector(&drand, size, vec) ;
101       IVL_setList(ivl, ilist, size, vec) ;
102    }
103    IVfree(vec) ;
104 }
105 MARKTIME(t2) ;
106 fprintf(msgFile, "\n CPU %8.3f : initialize the IVL object", t2 - t1) ;
107 fflush(msgFile) ;
108 if ( msglvl > 2 ) {
109    IVL_writeForHumanEye(ivl, msgFile) ;
110 } else {
111    IVL_writeStats(ivl, msgFile) ;
112 }
113 fflush(msgFile) ;
114 if ( myid == root ) {
115 /*
116    --------------------------------------
117    compute the checksum of the ivl object
118    --------------------------------------
119 */
120    for ( ilist = 0, chksum = 0.0 ; ilist < nlist ; ilist++ ) {
121          IVL_listAndSize(ivl, ilist, &size, &list) ;
122          chksum += 1 + ilist + size + IVsum(size, list) ;
123    }
124    fprintf(msgFile, "\n\n local chksum = %12.4e", chksum) ;
125    fflush(msgFile) ;
126 }
127 /*
128    ------------------------
129    broadcast the IVL object
130    ------------------------
131 */
132 MARKTIME(t1) ;
133 ivl = IVL_MPI_Bcast(ivl, root, msglvl, msgFile, MPI_COMM_WORLD) ;
134 MARKTIME(t2) ;
135 fprintf(msgFile, "\n CPU %8.3f : broadcast the IVL object", t2 - t1) ;
136 /*
137    --------------------------------------
138    compute the checksum of the ivl object
139    --------------------------------------
140 */
141 for ( ilist = 0, chksum = 0.0 ; ilist < nlist ; ilist++ ) {
142       IVL_listAndSize(ivl, ilist, &size, &list) ;
143       chksum += 1 + ilist + size + IVsum(size, list) ;
144 }
145 fprintf(msgFile, "\n\n local chksum = %12.4e", chksum) ;
146 fflush(msgFile) ;
147 /*
148    ---------------------------------------
149    gather the checksums from the processes
150    ---------------------------------------
151 */
152 sums = DVinit(nproc, 0.0) ;
153 MPI_Gather((void *) &chksum, 1, MPI_DOUBLE,
154            (void *) sums, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD) ;
155 if ( myid == 0 ) {
156    fprintf(msgFile, "\n\n sums") ;
157    DVfprintf(msgFile, nproc, sums) ;
158    for ( iproc = 0 ; iproc < nproc ; iproc++ ) {
159       sums[iproc] -= chksum ;
160    }
161    fprintf(msgFile, "\n\n errors") ;
162    DVfprintf(msgFile, nproc, sums) ;
163    fprintf(msgFile, "\n\n maxerror = %12.4e", DVmax(nproc, sums, &loc));
164 }
165 /*
166    ----------------
167    free the objects
168    ----------------
169 */
170 DVfree(sums) ;
171 IVL_free(ivl) ;
172 /*
173    ------------------------
174    exit the MPI environment
175    ------------------------
176 */
177 MPI_Finalize() ;
178 
179 fprintf(msgFile, "\n") ;
180 fclose(msgFile) ;
181 
182 return(0) ; }
183 
184 /*--------------------------------------------------------------------*/
185