1 /*  testIVLallgather.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_allgather() method
13 
14    (1) each process generates the same owners[n] map
15    (2) each process creates an IVL object
16        and fills its owned lists with random numbers
17    (3) the processes gather-all's the lists of ivl
18 
19    created -- 98apr03, cca
20    -------------------------------------------------
21 */
22 {
23 char         *buffer ;
24 double       chksum, globalsum, t1, t2 ;
25 Drand        drand ;
26 int          ilist, length, myid, msglvl, nlist,
27              nproc, rc, seed, size, tag ;
28 int          *list, *owners, *vec ;
29 int          stats[4], tstats[4] ;
30 IV           *ownersIV ;
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 != 5 ) {
44    fprintf(stdout,
45            "\n\n usage : %s msglvl msgFile n seed "
46            "\n    msglvl  -- message level"
47            "\n    msgFile -- message file"
48            "\n    nlist   -- number of lists in the IVL object"
49            "\n    seed    -- random number seed"
50            "\n", argv[0]) ;
51    return(0) ;
52 }
53 msglvl = atoi(argv[1]) ;
54 if ( strcmp(argv[2], "stdout") == 0 ) {
55    msgFile = stdout ;
56 } else {
57    length = strlen(argv[2]) + 1 + 4 ;
58    buffer = CVinit(length, '\0') ;
59    sprintf(buffer, "%s.%d", argv[2], myid) ;
60    if ( (msgFile = fopen(buffer, "w")) == NULL ) {
61       fprintf(stderr, "\n fatal error in %s"
62               "\n unable to open file %s\n",
63               argv[0], argv[2]) ;
64       return(-1) ;
65    }
66    CVfree(buffer) ;
67 }
68 nlist = atoi(argv[3]) ;
69 seed  = atoi(argv[4]) ;
70 fprintf(msgFile,
71         "\n %s "
72         "\n msglvl  -- %d"
73         "\n msgFile -- %s"
74         "\n nlist   -- %d"
75         "\n seed    -- %d"
76         "\n",
77         argv[0], msglvl, argv[2], nlist, seed) ;
78 fflush(msgFile) ;
79 /*
80    ----------------------------
81    generate the ownersIV object
82    ----------------------------
83 */
84 MARKTIME(t1) ;
85 ownersIV = IV_new() ;
86 IV_init(ownersIV, nlist, NULL) ;
87 owners = IV_entries(ownersIV) ;
88 Drand_setDefaultFields(&drand) ;
89 Drand_setSeed(&drand, seed) ;
90 Drand_setUniform(&drand, 0, nproc) ;
91 Drand_fillIvector(&drand, nlist, owners) ;
92 MARKTIME(t2) ;
93 fprintf(msgFile, "\n CPU %8.3f : initialize the ownersIV object",
94         t2 - t1) ;
95 fflush(msgFile) ;
96 fprintf(msgFile, "\n\n ownersIV generated") ;
97 if ( msglvl > 2 ) {
98    IV_writeForHumanEye(ownersIV, msgFile) ;
99 } else {
100    IV_writeStats(ownersIV, msgFile) ;
101 }
102 fflush(msgFile) ;
103 /*
104    --------------------------------------------
105    set up the IVL object and fill owned entries
106    --------------------------------------------
107 */
108 MARKTIME(t1) ;
109 ivl = IVL_new() ;
110 IVL_init1(ivl, IVL_CHUNKED, nlist) ;
111 vec = IVinit(nlist, -1) ;
112 Drand_setSeed(&drand, seed + myid) ;
113 Drand_setUniform(&drand, 0, nlist) ;
114 for ( ilist = 0 ; ilist < nlist ; ilist++ ) {
115    if ( owners[ilist] == myid ) {
116       size = (int) Drand_value(&drand) ;
117       Drand_fillIvector(&drand, size, vec) ;
118       IVL_setList(ivl, ilist, size, vec) ;
119    }
120 }
121 MARKTIME(t2) ;
122 fprintf(msgFile, "\n CPU %8.3f : initialize the IVL object",
123         t2 - t1) ;
124 fflush(msgFile) ;
125 if ( msglvl > 2 ) {
126    IVL_writeForHumanEye(ivl, msgFile) ;
127 } else {
128    IVL_writeStats(ivl, msgFile) ;
129 }
130 fflush(msgFile) ;
131 /*
132    --------------------------------------------
133    compute the local checksum of the ivl object
134    --------------------------------------------
135 */
136 for ( ilist = 0, chksum = 0.0 ; ilist < nlist ; ilist++ ) {
137    if ( owners[ilist] == myid ) {
138       IVL_listAndSize(ivl, ilist, &size, &list) ;
139       chksum += 1 + ilist + size + IVsum(size, list) ;
140    }
141 }
142 fprintf(msgFile, "\n\n local partial chksum = %12.4e", chksum) ;
143 fflush(msgFile) ;
144 /*
145    -----------------------
146    get the global checksum
147    -----------------------
148 */
149 rc = MPI_Allreduce((void *) &chksum, (void *) &globalsum,
150                    1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD) ;
151 /*
152    --------------------------------
153    execute the all-gather operation
154    --------------------------------
155 */
156 tag = 47 ;
157 IVzero(4, stats) ;
158 IVL_MPI_allgather(ivl, ownersIV,
159                   stats, msglvl, msgFile, tag, MPI_COMM_WORLD) ;
160 if ( msglvl > 0 ) {
161    fprintf(msgFile, "\n\n return from IVL_MPI_allgather()") ;
162    fprintf(msgFile,
163            "\n local send stats : %10d messages with %10d bytes"
164            "\n local recv stats : %10d messages with %10d bytes",
165            stats[0], stats[2], stats[1], stats[3]) ;
166    fflush(msgFile) ;
167 }
168 MPI_Reduce((void *) stats, (void *) tstats, 4, MPI_INT,
169           MPI_SUM, 0, MPI_COMM_WORLD) ;
170 if ( myid == 0 ) {
171    fprintf(msgFile,
172            "\n total send stats : %10d messages with %10d bytes"
173            "\n total recv stats : %10d messages with %10d bytes",
174            tstats[0], tstats[2], tstats[1], tstats[3]) ;
175    fflush(msgFile) ;
176 }
177 if ( msglvl > 2 ) {
178    fprintf(msgFile, "\n\n ivl") ;
179    IVL_writeForHumanEye(ivl, msgFile) ;
180    fflush(msgFile) ;
181 }
182 /*
183    -----------------------------------------
184    compute the checksum of the entire object
185    -----------------------------------------
186 */
187 for ( ilist = 0, chksum = 0.0 ; ilist < nlist ; ilist++ ) {
188    IVL_listAndSize(ivl, ilist, &size, &list) ;
189    chksum += 1 + ilist + size + IVsum(size, list) ;
190 }
191 fprintf(msgFile,
192         "\n globalsum = %12.4e, chksum = %12.4e, error = %12.4e",
193         globalsum, chksum, fabs(globalsum - chksum)) ;
194 fflush(msgFile) ;
195 /*
196    ----------------
197    free the objects
198    ----------------
199 */
200 IV_free(ownersIV) ;
201 IVL_free(ivl) ;
202 /*
203    ------------------------
204    exit the MPI environment
205    ------------------------
206 */
207 MPI_Finalize() ;
208 
209 fprintf(msgFile, "\n") ;
210 fclose(msgFile) ;
211 
212 return(0) ; }
213 
214 /*--------------------------------------------------------------------*/
215