File: testScatterDenseMtx.c

package info (click to toggle)
spooles 2.2-11
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,656 kB
  • ctags: 3,690
  • sloc: ansic: 146,836; sh: 7,571; csh: 3,615; makefile: 1,968; perl: 74
file content (305 lines) | stat: -rw-r--r-- 9,382 bytes parent folder | download | duplicates (7)
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
/*  testScatterDenseMtx.c  */

#include "../spoolesMPI.h"
#include "../../Drand.h"
#include "../../timings.h"

/*--------------------------------------------------------------------*/

int
main ( int argc, char *argv[] )
/*
   ------------------------------------------------------------------
   this program tests the simple split and merge of a DenseMtx object

   (1) process root generates a nrow x ncol DenseMtx object
   (2) using a random map, scatter the rows into local matrices
   (3) gather the local matrices into a second global matrix
       and compare

   created -- 98sep26, cca
   ------------------------------------------------------------------
*/
{
char        *buffer ;
DenseMtx    *X, *Y, *Z ;
double      t1, t2 ;
double      checksums[3], Xchecksums[3], Ychecksums[3], Zchecksums[3] ;
Drand       drand ;
int         inc1, inc2, length, myid, msglvl, ncol, 
            nproc, nrow, root, seed, tag, type, v ;
int         *map ;
int         stats[4], tstats[4] ;
IV          *mapIV ;
FILE        *msgFile ;
/*
   ---------------------------------------------------------------
   find out the identity of this process and the number of process
   ---------------------------------------------------------------
*/
/*
fprintf(stdout, "\n stdout, MPI_COMM_WORLD = %p", MPI_COMM_WORLD) ;
fflush(stdout) ;
*/
MPI_Init(&argc, &argv) ;
MPI_Comm_rank(MPI_COMM_WORLD, &myid) ;
MPI_Comm_size(MPI_COMM_WORLD, &nproc) ;
/*
fprintf(stdout, "\n process %d of %d, argc = %d", myid, nproc, argc) ;
fflush(stdout) ;
*/
if ( argc != 10 ) {
   fprintf(stdout, 
"\n\n usage : %s msglvl msgFile type nrow ncol inc1 inc2 seed root"
"\n    msglvl  -- message level"
"\n    msgFile -- message file"
"\n    type    -- type of entries"
"\n      1 -- real"
"\n      2 -- complex"
"\n    nrow -- number of rows"
"\n    ncol -- number of columns"
"\n    inc1 -- row increment"
"\n    inc2 -- column increment"
"\n    seed -- random number seed"
"\n    root -- root processor"
"\n", argv[0]) ;
   return(0) ;
}
msglvl = atoi(argv[1]) ;
if ( strcmp(argv[2], "stdout") == 0 ) {
   msgFile = stdout ;
} else {
   length = strlen(argv[2]) + 1 + 4 ;
   buffer = CVinit(length, '\0') ;
   sprintf(buffer, "%s.%d", argv[2], myid) ;
   if ( (msgFile = fopen(buffer, "w")) == NULL ) {
      fprintf(stderr, "\n fatal error in %s"
              "\n unable to open file %s\n",
              argv[0], argv[2]) ;
      return(0) ;
   }
   CVfree(buffer) ;
}
type = atoi(argv[3]) ;
nrow = atoi(argv[4]) ;
ncol = atoi(argv[5]) ;
inc1 = atoi(argv[6]) ;
inc2 = atoi(argv[7]) ;
seed = atoi(argv[8]) ;
root = atoi(argv[9]) ;
fprintf(msgFile, 
        "\n %s "
        "\n msglvl     -- %d" 
        "\n msgFile    -- %s" 
        "\n type       -- %d" 
        "\n nrow       -- %d" 
        "\n ncol       -- %d" 
        "\n inc1       -- %d" 
        "\n inc2       -- %d" 
        "\n seed       -- %d" 
        "\n root       -- %d" 
        "\n",
        argv[0], msglvl, argv[2], type, nrow, ncol, 
        inc1, inc2, seed, root) ;
fflush(msgFile) ;
/*
   -----------------------------
   generate the DenseMtx object
   -----------------------------
*/
if ( myid == root ) {
   X = DenseMtx_new() ;
   fprintf(msgFile, "\n X = %p", X) ;
   fflush(msgFile) ;
   MARKTIME(t1) ;
   DenseMtx_init(X, type, 1, -1, nrow, ncol, inc1, inc2) ;
   Drand_setDefaultFields(&drand) ;
   Drand_setSeed(&drand, seed) ;
   Drand_setUniform(&drand, 0.0, 1.0) ;
   switch ( type ) {
   case SPOOLES_REAL :
      Drand_fillDvector(&drand, nrow*ncol, DenseMtx_entries(X)) ;
      break ;
   case SPOOLES_COMPLEX :
      Drand_fillDvector(&drand, 2*nrow*ncol, DenseMtx_entries(X)) ;
      break ;
   }
/*
   ------------------------------------------
   compute the serial checksums of the matrix
   ------------------------------------------
*/
   DenseMtx_checksums(X, Xchecksums) ;
   MARKTIME(t2) ;
   fprintf(msgFile, "\n CPU %8.3f : initialize the DenseMtx object",
           t2 - t1) ;
   fflush(msgFile) ;
   fprintf(msgFile, "\n\n DenseMtx generated") ;
   if ( msglvl > 2 ) {
      DenseMtx_writeForHumanEye(X, msgFile) ;
   } else {
      DenseMtx_writeStats(X, msgFile) ;
   }
   fflush(msgFile) ;
/*
   ---------------------
   generate a random map
   ---------------------
*/
   MARKTIME(t1) ;
   mapIV = IV_new() ;
   IV_init(mapIV, nrow, NULL) ;
   map = IV_entries(mapIV) ;
   Drand_setDefaultFields(&drand) ;
   Drand_setSeed(&drand, seed) ;
   Drand_setUniform(&drand, 0.0, (double) nrow) ;
   for ( v = 0 ; v < nrow ; v++ ) {
      map[v] = ((int) Drand_value(&drand)) % nproc ;
      if ( map[v] == root ) {
         map[v] = nproc - 1 ;
      }
   }
   MARKTIME(t2) ;
   fprintf(msgFile, "\n CPU %8.3f : generate random map", t2 - t1) ;
   fflush(msgFile) ;
   if ( msglvl > 2 ) {
      fprintf(msgFile, "\n\n map") ;
      IV_writeForHumanEye(mapIV, msgFile) ;
      fflush(msgFile) ;
   }
} else {
   X = NULL ;
   mapIV = NULL ;
}
/*
   ------------------------------------------
   now scatter the matrix with the random map
   ------------------------------------------
*/
stats[0]  = stats[1]  = stats[2]  = stats[3]  = 0 ;
tstats[0] = tstats[1] = tstats[2] = tstats[3] = 0 ;
MARKTIME(t1) ;
tag = 1 ;
Y = DenseMtx_MPI_splitFromGlobalByRows(X, NULL, mapIV, root, stats, 
                              msglvl, msgFile, tag, MPI_COMM_WORLD) ;
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU %8.3f : scatter matrix ", t2 - t1) ;
fprintf(msgFile, 
        "\n send stats : %d messages with %d bytes"
        "\n recv stats : %d messages with %d bytes",
        stats[0], stats[2], stats[1], stats[3]) ;
fflush(msgFile) ;
MPI_Reduce((void *) stats, (void *) tstats, 4, MPI_INT,
          MPI_SUM, root, MPI_COMM_WORLD) ;
if ( myid == root ) {
   fprintf(msgFile, 
           "\n total send stats : %d messages with %d bytes"
           "\n total recv stats : %d messages with %d bytes",
           tstats[0], tstats[2], tstats[1], tstats[3]) ;
   fflush(msgFile) ;
}
if ( Y != NULL ) {
   fprintf(msgFile, "\n\n local matrix") ;
   if ( msglvl > 2 ) {
      DenseMtx_writeForHumanEye(Y, msgFile) ;
   } else {
      DenseMtx_writeStats(Y, msgFile) ;
   }
} else {
   fprintf(msgFile, "\n\n local matrix is NULL") ;
}
fflush(msgFile) ;
/*
   -----------------------------------------------
   compute the checksums of the distributed matrix
   -----------------------------------------------
*/
if ( Y != NULL ) {
   DenseMtx_checksums(Y, checksums) ;
} else {
   checksums[0] = checksums[1] = checksums[2] = 0.0 ;
}
MPI_Reduce((void *) checksums, (void *) Ychecksums, 3, MPI_DOUBLE,
          MPI_SUM, root, MPI_COMM_WORLD) ;
if ( myid == root ) {
   fprintf(msgFile, 
           "\n\n checksums for original matrix    : %12.4e %12.4e"
           "\n checksums for distributed matrix : %12.4e %12.4e"
           "\n error in checksum                : %12.4e %12.4e",
         Xchecksums[0], Xchecksums[2], Ychecksums[0], Ychecksums[2],
         Ychecksums[0] - Xchecksums[0], Ychecksums[2] - Xchecksums[2]) ;
}
/*
   -----------------------------------------------------
   gather the local matrices into a second global matrix
   -----------------------------------------------------
*/
MARKTIME(t1) ;
stats[0] = stats[1] = stats[2] = stats[3] = 0 ;
if ( myid == root ) {
   Z = DenseMtx_new() ;
   fprintf(msgFile, "\n Z = %p", Z) ;
} else {
   Z = NULL ;
}
Z = DenseMtx_MPI_mergeToGlobalByRows(Z, Y, root, stats, msglvl,
                                     msgFile, tag, MPI_COMM_WORLD) ;
fprintf(msgFile, "\n Z = %p", Z) ;
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU %8.3f : merge into global matrix ", t2 - t1) ;
fprintf(msgFile, "\n send stats : %d messages with %d bytes"
                 "\n recv stats : %d messages with %d bytes",
                 stats[0], stats[2], stats[1], stats[3]) ;
fflush(msgFile) ;
MPI_Reduce((void *) stats, (void *) tstats, 4, MPI_INT,
          MPI_SUM, root, MPI_COMM_WORLD) ;
if ( myid == root ) {
   fprintf(msgFile, "\n total send stats : %d messages with %d bytes"
                    "\n total recv stats : %d messages with %d bytes",
                    tstats[0], tstats[2], tstats[1], tstats[3]) ;
   DenseMtx_sort(Z) ;
   fprintf(msgFile, "\n\n global Z matrix") ;
   if ( msglvl > 2 ) {
      DenseMtx_writeForHumanEye(Z, msgFile) ;
   } else {
      DenseMtx_writeStats(Z, msgFile) ;
   }
   fflush(msgFile) ;
   DenseMtx_checksums(Z, Zchecksums) ;
   fprintf(msgFile, 
           "\n\n checksums for original matrix    : %12.4e %12.4e"
           "\n checksums for distributed matrix : %12.4e %12.4e"
           "\n checksums for global matrix      : %12.4e %12.4e"
           "\n X x Y error in checksum          : %12.4e %12.4e"
           "\n X x Z error in checksum          : %12.4e %12.4e",
         Xchecksums[0], Xchecksums[2], 
         Ychecksums[0], Ychecksums[2],
         Zchecksums[0], Zchecksums[2],
         Xchecksums[0] - Ychecksums[0], Xchecksums[2] - Ychecksums[2],
         Xchecksums[0] - Zchecksums[0], Xchecksums[2] - Zchecksums[2]) ;
}
/*
   ----------------
   free the objects
   ----------------
*/
if ( myid == root ) {
   IV_free(mapIV) ;
}
if ( X != NULL ) {
   DenseMtx_free(X) ;
}
if ( Y != NULL ) {
   DenseMtx_free(Y) ;
}
if ( Z != NULL ) {
   DenseMtx_free(Z) ;
}
MPI_Finalize() ;

fprintf(msgFile, "\n") ;
fclose(msgFile) ;

return(0) ; }

/*--------------------------------------------------------------------*/