File: test_solveupd.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 (358 lines) | stat: -rw-r--r-- 10,428 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
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
/*  test_solveupd.c  */

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

/*--------------------------------------------------------------------*/
int
main ( int argc, char *argv[] )
/*
   ----------------------------------
   test the SubMtx_solveupd() method.

   created -- 98may02, cca
   ----------------------------------
*/
{
SubMtx   *mtxA, *mtxX, *mtxY ;
double   ops, t1, t2 ;
double   *entX, *entY ;
Drand    *drand ;
FILE     *msgFile ;
int      inc1, inc2, irowA, jcolX, mode, msglvl, ncolA, nentA, nrowA, 
         ncolX, nrowX, ncolY, nrowY, seed, type ;
int      *colindA, *ivec, *rowindA ;

if ( argc != 12 ) {
   fprintf(stdout, 
           "\n\n usage : %s msglvl msgFile type mode"
           "\n         nrowY ncolY nrowA ncolA nentA nrowX seed"
           "\n    msglvl  -- message level"
           "\n    msgFile -- message file"
           "\n    type    -- type of matrix A"
           "\n       1 -- real"
           "\n       2 -- complex"
           "\n    mode    -- mode of matrix A"
           "\n       0 -- dense stored by rows"
           "\n       1 -- dense stored by columns"
           "\n       2 -- sparse stored by rows"
           "\n       3 -- sparse stored by columns"
           "\n    nrowY   -- # of rows in vector Y"
           "\n    ncolY   -- # of columns in vector Y"
           "\n    nrowA   -- # of rows in matrix A"
           "\n    ncolA   -- # of columns in matrix A"
           "\n    nentA   -- # of entries in matrix A"
           "\n    nrowX   -- # of rows in matrix X"
           "\n    seed    -- random number seed"
           "\n", argv[0]) ;
   return(0) ;
}
if ( (msglvl = atoi(argv[1])) < 0 ) {
   fprintf(stderr, "\n message level must be positive\n") ;
   exit(-1) ;
}
if ( strcmp(argv[2], "stdout") == 0 ) {
   msgFile = stdout ;
} else if ( (msgFile = fopen(argv[2], "a")) == NULL ) {
   fprintf(stderr, "\n unable to open file %s\n", argv[2]) ;
   return(-1) ;
}
type  = atoi(argv[3]) ;
mode  = atoi(argv[4]) ;
nrowY = atoi(argv[5]) ;
ncolY = atoi(argv[6]) ;
nrowA = atoi(argv[7]) ;
ncolA = atoi(argv[8]) ;
nentA = atoi(argv[9]) ;
nrowX = atoi(argv[10]) ;
seed  = atoi(argv[11]) ;
fprintf(msgFile, "\n %% %s:"
        "\n %% msglvl  = %d"
        "\n %% msgFile = %s"
        "\n %% type    = %d"
        "\n %% mode    = %d"
        "\n %% nrowY   = %d"
        "\n %% ncolY   = %d"
        "\n %% nrowA   = %d"
        "\n %% ncolA   = %d"
        "\n %% nentA   = %d"
        "\n %% nrowX   = %d"
        "\n %% seed    = %d",
        argv[0], msglvl, argv[2], type, mode, nrowY, ncolY, 
        nrowA, ncolA, nentA, nrowX, seed) ;
ncolX = ncolY ;
/*
   -----------------------------
   check for errors in the input
   -----------------------------
*/
if (  nrowA <= 0 || nrowA > nrowY
   || ncolA <= 0 || ncolA > nrowX
   || nentA > nrowA*ncolA 
   || nrowX <= 0 ) {
   fprintf(stderr, "\n invalid input\n") ;
   exit(-1) ;
}
switch ( type ) {
case SPOOLES_REAL :
case SPOOLES_COMPLEX :
   break ;
default :
   fprintf(stderr, "\n invalid type %d\n", type) ;
   exit(-1) ;
}
switch ( mode ) {
case SUBMTX_DENSE_ROWS :
case SUBMTX_DENSE_COLUMNS :
case SUBMTX_SPARSE_ROWS :
case SUBMTX_SPARSE_COLUMNS :
   break ;
default :
   fprintf(stderr, "\n invalid mode %d\n", mode) ;
   exit(-1) ;
}
/*
   --------------------------------------
   initialize the random number generator
   --------------------------------------
*/
drand = Drand_new() ;
Drand_init(drand) ;
Drand_setSeed(drand, seed) ;
Drand_setNormal(drand, 0.0, 1.0) ;
/*
   ------------------------------
   initialize the X SubMtx object
   ------------------------------
*/
MARKTIME(t1) ;
mtxX = SubMtx_new() ;
SubMtx_init(mtxX, type, SUBMTX_DENSE_COLUMNS, 0, 0, 
            nrowX, ncolX, nrowX*ncolX) ;
SubMtx_denseInfo(mtxX, &nrowX, &ncolX, &inc1, &inc2, &entX) ;
if ( SUBMTX_IS_REAL(mtxX) ) {
   Drand_fillDvector(drand, nrowX*ncolX, entX) ;
} else if ( SUBMTX_IS_COMPLEX(mtxX) ) {
   Drand_fillDvector(drand, 2*nrowX*ncolX, entX) ;
}
MARKTIME(t2) ;
fprintf(msgFile, "\n %% CPU : %.3f to initialize X SubMtx object",
        t2 - t1) ;
/*
   ------------------------------
   initialize the Y SubMtx object
   ------------------------------
*/
MARKTIME(t1) ;
mtxY = SubMtx_new() ;
SubMtx_init(mtxY, type, SUBMTX_DENSE_COLUMNS, 0, 0, 
            nrowY, ncolY, nrowY*ncolY) ;
SubMtx_denseInfo(mtxY, &nrowY, &ncolY, &inc1, &inc2, &entY) ;
if ( SUBMTX_IS_REAL(mtxX) ) {
   Drand_fillDvector(drand, nrowY*ncolY, entY) ;
   DVzero(nrowY*ncolY, entY) ;
} else if ( SUBMTX_IS_COMPLEX(mtxX) ) {
   Drand_fillDvector(drand, 2*nrowY*ncolY, entY) ;
   DVzero(2*nrowY*ncolY, entY) ;
}
MARKTIME(t2) ;
fprintf(msgFile, "\n %% CPU : %.3f to initialize Y SubMtx object",
        t2 - t1) ;
/*
   -------------------------------------
   initialize the A matrix SubMtx object
   -------------------------------------
*/
mtxA  = SubMtx_new() ;
SubMtx_initRandom(mtxA, type, mode, 0, 0, nrowA, ncolA, nentA, seed) ;
/*
   -------------------------
   load the row indices of A
   -------------------------
*/
SubMtx_rowIndices(mtxA, &nrowA, &rowindA) ;
ivec = IVinit(nrowY, -1) ;
IVramp(nrowY, ivec, 0, 1) ;
IVshuffle(nrowY, ivec, seed+1) ;
IVcopy(nrowA, rowindA, ivec) ;
IVqsortUp(nrowA, rowindA) ;
IVfree(ivec) ;
if ( msglvl > 3 ) {
   fprintf(msgFile, "\n %% row indices of A") ;
   IVfprintf(msgFile, nrowA, rowindA) ;
   fflush(msgFile) ;
}
/*
   ----------------------------
   load the column indices of A
   ----------------------------
*/
SubMtx_columnIndices(mtxA, &ncolA, &colindA) ;
ivec = IVinit(nrowX, -1) ;
IVramp(nrowX, ivec, 0, 1) ;
IVshuffle(nrowX, ivec, seed+2) ;
IVcopy(ncolA, colindA, ivec) ;
IVqsortUp(ncolA, colindA) ;
IVfree(ivec) ;
if ( msglvl > 3 ) {
   fprintf(msgFile, "\n %% column indices of A") ;
   IVfprintf(msgFile, ncolA, colindA) ;
   fflush(msgFile) ;
}
/*
   ----------------------------------
   compute the matrix-matrix multiply
   ----------------------------------
*/
if ( type == SPOOLES_REAL ) {
   double   *colX, *pYij, *rowA ;
   double   sum ;
   DV       *colDV, *rowDV ;
   int      ii ;

   ops = 2*nrowA*ncolA*ncolX ;
   colDV = DV_new() ;
   DV_init(colDV, nrowX, NULL) ;
   colX = DV_entries(colDV) ;
   rowDV = DV_new() ;
   DV_init(rowDV, ncolA, NULL) ;
   rowA = DV_entries(rowDV) ;
   for ( jcolX = 0 ; jcolX < ncolX ; jcolX++ ) {
      SubMtx_fillColumnDV(mtxX, jcolX, colDV) ;
      for ( irowA = 0 ; irowA < nrowA ; irowA++ ) {
         SubMtx_fillRowDV(mtxA, irowA, rowDV) ;
         if ( ncolA == nrowX ) {
            for ( ii = 0, sum = 0.0 ; ii < ncolA ; ii++ ) {
               sum += rowA[ii] * colX[ii] ;
            }
         } else {
            for ( ii = 0, sum = 0.0 ; ii < ncolA ; ii++ ) {
               sum += rowA[ii] * colX[colindA[ii]] ;
            }
         }
         if ( nrowA == nrowY ) {
            SubMtx_locationOfRealEntry(mtxY, irowA, jcolX, &pYij) ;
         } else {
            SubMtx_locationOfRealEntry(mtxY, rowindA[irowA], jcolX,
                                       &pYij) ;
         }
         *pYij = sum ;
      }
   }
   DV_free(colDV) ;
   DV_free(rowDV) ;
} else if ( type == SPOOLES_COMPLEX ) {
   double   *colX, *pYIij, *pYRij, *rowA ;
   double   idot, rdot ;
   ZV       *colZV, *rowZV ;

   ops = 8*nrowA*ncolA*ncolX ;
   colZV = ZV_new() ;
   ZV_init(colZV, nrowX, NULL) ;
   colX = ZV_entries(colZV) ;
   rowZV = ZV_new() ;
   ZV_init(rowZV, ncolA, NULL) ;
   rowA = ZV_entries(rowZV) ;
   for ( jcolX = 0 ; jcolX < ncolX ; jcolX++ ) {
      SubMtx_fillColumnZV(mtxX, jcolX, colZV) ;
      for ( irowA = 0 ; irowA < nrowA ; irowA++ ) {
         SubMtx_fillRowZV(mtxA, irowA, rowZV) ;
         if ( ncolA == nrowX ) {
            ZVdotU(ncolA, colX, rowA, &rdot, &idot) ;
         } else {
            ZVdotiU(ncolA, colX, colindA, rowA, &rdot, &idot) ;
         }
         if ( nrowA == nrowY ) {
            SubMtx_locationOfComplexEntry(mtxY, 
                                 irowA, jcolX, &pYRij, &pYIij) ;
         } else {
            SubMtx_locationOfComplexEntry(mtxY, rowindA[irowA], jcolX,
                                 &pYRij, &pYIij) ;
         }
         *pYRij = rdot ;
         *pYIij = idot ;
      }
   }
   ZV_free(colZV) ;
   ZV_free(rowZV) ;
}
MARKTIME(t2) ;
fprintf(msgFile, "\n %% CPU : %.3f to compute m-m, %.3f mflops",
        t2 - t1, ops*1.e-6/(t2 - t1)) ;
if ( msglvl > 1 ) {
   fprintf(msgFile, "\n\n %% Z SubMtx object") ;
   fprintf(msgFile, "\n Z = zeros(%d,%d) ;", nrowY, ncolY) ;
   SubMtx_writeForMatlab(mtxY, "Z", msgFile) ;
   fflush(msgFile) ;
}
/*
   ----------------------
   print out the matrices
   ----------------------
*/
if ( msglvl > 1 ) {
   fprintf(msgFile, "\n\n %% Y SubMtx object") ;
   fprintf(msgFile, "\n Y = zeros(%d,%d) ;", nrowY, ncolY) ;
   SubMtx_writeForMatlab(mtxY, "Y", msgFile) ;
   fprintf(msgFile, "\n\n %% A SubMtx object") ;
   fprintf(msgFile, "\n A = zeros(%d,%d) ;", nrowY, nrowX) ;
   SubMtx_writeForMatlab(mtxA, "A", msgFile) ;
   fprintf(msgFile, "\n\n %% X SubMtx object") ;
   fprintf(msgFile, "\n X = zeros(%d,%d) ;", nrowX, ncolY) ;
   SubMtx_writeForMatlab(mtxX, "X", msgFile) ;
   fflush(msgFile) ;
}
/*
   -----------------
   check with matlab
   -----------------
*/
if ( msglvl > 1 ) {
   fprintf(msgFile,
           "\n\n emtx   = abs(Y - A*X) ;"
           "\n\n maxabs = max(max(emtx)) ") ;
   fflush(msgFile) ;
}
/*
   -------------------------------
   compute the update Y := Y - A*X
   (Y should now be zero)
   -------------------------------
*/
SubMtx_solveupd(mtxY, mtxA, mtxX) ;
/*
   ----------------------
   print out the Y matrix
   ----------------------
*/
if ( msglvl > 1 ) {
   fprintf(msgFile, "\n\n %% Z SubMtx object") ;
   fprintf(msgFile, "\n Z = zeros(%d,%d) ;", nrowY, ncolY) ;
   SubMtx_writeForMatlab(mtxY, "Z", msgFile) ;
   fflush(msgFile) ;
}
fprintf(msgFile, "\n RES %4d %4d %4d %4d %4d %4d %4d %4d %12.4e", 
        type, mode, nrowY, ncolY, nrowA, ncolA, nrowX, ncolX, 
        SubMtx_maxabs(mtxY)) ;
if ( msglvl > 1 ) {
   fprintf(msgFile,
           "\n\n emtx   = abs(Z) ;"
           "\n\n maxerr = max(max(emtx)) ") ;
   fflush(msgFile) ;
}
/*
   ------------------------
   free the working storage
   ------------------------
*/
SubMtx_free(mtxA) ;
SubMtx_free(mtxX) ;
SubMtx_free(mtxY) ;
Drand_free(drand) ;

fprintf(msgFile, "\n") ;

return(1) ; }

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