Blame view

fvn_sparse/UMFPACK/Source/umf_triplet.c 9.52 KB
422234dc3   daniau   git-svn-id: https...
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
  /* ========================================================================== */
  /* === UMF_triplet ========================================================== */
  /* ========================================================================== */
  
  /* -------------------------------------------------------------------------- */
  /* UMFPACK Copyright (c) Timothy A. Davis, CISE,                              */
  /* Univ. of Florida.  All Rights Reserved.  See ../Doc/License for License.   */
  /* web: http://www.cise.ufl.edu/research/sparse/umfpack                       */
  /* -------------------------------------------------------------------------- */
  
  /*
      Not user callable.  Converts triplet input to column-oriented form.
      Duplicate entries may exist (they are summed in the output).  The columns
      of the column-oriented form are in sorted order.  The input is not modified.
      Returns 1 if OK, 0 if an error occurred.
  
      Compiled into four different routines for each version (di, dl, zi, zl),
      for a total of 16 different routines.
  */
  
  #include "umf_internal.h"
  
  #ifdef DO_MAP
  #ifdef DO_VALUES
  GLOBAL Int UMF_triplet_map_x
  #else
  GLOBAL Int UMF_triplet_map_nox
  #endif
  #else
  #ifdef DO_VALUES
  GLOBAL Int UMF_triplet_nomap_x
  #else
  GLOBAL Int UMF_triplet_nomap_nox
  #endif
  #endif
  (
      Int n_row,
      Int n_col,
      Int nz,
      const Int Ti [ ],		/* size nz */
      const Int Tj [ ],		/* size nz */
      Int Ap [ ],			/* size n_col + 1 */
      Int Ai [ ],			/* size nz */
      Int Rp [ ],			/* size n_row + 1 */
      Int Rj [ ],			/* size nz */
      Int W [ ],			/* size max (n_row, n_col) */
      Int RowCount [ ]		/* size n_row */
  #ifdef DO_VALUES
      , const double Tx [ ]	/* size nz */
      , double Ax [ ]		/* size nz */
      , double Rx [ ]		/* size nz */
  #ifdef COMPLEX
      , const double Tz [ ]	/* size nz */
      , double Az [ ]		/* size nz */
      , double Rz [ ]		/* size nz */
  #endif
  #endif
  #ifdef DO_MAP
      , Int Map [ ]		/* size nz */
      , Int Map2 [ ]		/* size nz */
  #endif
  )
  {
  
      /* ---------------------------------------------------------------------- */
      /* local variables */
      /* ---------------------------------------------------------------------- */
  
      Int i, j, k, p, cp, p1, p2, pdest, pj ;
  #ifdef DO_MAP
      Int duplicates ;
  #endif
  #ifdef DO_VALUES
  #ifdef COMPLEX
      Int split = SPLIT (Tz) && SPLIT (Az) && SPLIT (Rz) ;
  #endif
  #endif
  
      /* ---------------------------------------------------------------------- */
      /* count the entries in each row (also counting duplicates) */
      /* ---------------------------------------------------------------------- */
  
      /* use W as workspace for row counts (including duplicates) */
      for (i = 0 ; i < n_row ; i++)
      {
  	W [i] = 0 ;
      }
  
      for (k = 0 ; k < nz ; k++)
      {
  	i = Ti [k] ;
  	j = Tj [k] ;
  	if (i < 0 || i >= n_row || j < 0 || j >= n_col)
  	{
  	    return (UMFPACK_ERROR_invalid_matrix) ;
  	}
  	W [i]++ ;
  #ifndef NDEBUG
  	DEBUG1 ((ID " triplet: "ID" "ID" ", k, i, j)) ;
  #ifdef DO_VALUES
  	{
  	    Entry tt ;
  	    ASSIGN (tt, Tx, Tz, k, split) ;
  	    EDEBUG2 (tt) ;
  	    DEBUG1 (("
  ")) ;
  	}
  #endif
  #endif
      }
  
      /* ---------------------------------------------------------------------- */
      /* compute the row pointers */
      /* ---------------------------------------------------------------------- */
  
      Rp [0] = 0 ;
      for (i = 0 ; i < n_row ; i++)
      {
  	Rp [i+1] = Rp [i] + W [i] ;
  	W [i] = Rp [i] ;
      }
  
      /* W is now equal to the row pointers */
  
      /* ---------------------------------------------------------------------- */
      /* construct the row form */
      /* ---------------------------------------------------------------------- */
  
      for (k = 0 ; k < nz ; k++)
      {
  	p = W [Ti [k]]++ ;
  #ifdef DO_MAP
  	Map [k] = p ;
  #endif
  	Rj [p] = Tj [k] ;
  #ifdef DO_VALUES
  #ifdef COMPLEX
  	if (split)
  	{
  	    Rx [p] = Tx [k] ;
  	    Rz [p] = Tz [k] ;
  	}
  	else
  	{
  	    Rx [2*p  ] = Tx [2*k  ] ;
  	    Rx [2*p+1] = Tx [2*k+1] ;
  	}
  #else
  	Rx [p] = Tx [k] ;
  #endif
  #endif
      }
  
      /* Rp stays the same, but W [i] is advanced to the start of row i+1 */
  
  #ifndef NDEBUG
      for (i = 0 ; i < n_row ; i++)
      {
  	ASSERT (W [i] == Rp [i+1]) ;
      }
  #ifdef DO_MAP
      for (k = 0 ; k < nz ; k++)
      {
  	/* make sure that kth triplet is mapped correctly */
  	p = Map [k] ;
  	DEBUG1 (("First row map: Map ["ID"] = "ID"
  ", k, p)) ;
  	i = Ti [k] ;
  	j = Tj [k] ;
  	ASSERT (j == Rj [p]) ;
  	ASSERT (Rp [i] <= p && p < Rp [i+1]) ;
      }
  #endif
  #endif
  
      /* ---------------------------------------------------------------------- */
      /* sum up duplicates */
      /* ---------------------------------------------------------------------- */
  
      /* use W [j] to hold position in Ri/Rx/Rz of a_ij, for row i [ */
  
      for (j = 0 ; j < n_col ; j++)
      {
  	W [j] = EMPTY ;
      }
  
  #ifdef DO_MAP
      duplicates = FALSE ;
  #endif
  
      for (i = 0 ; i < n_row ; i++)
      {
  	p1 = Rp [i] ;
  	p2 = Rp [i+1] ;
  	pdest = p1 ;
  	/* At this point, W [j] < p1 holds true for all columns j, */
  	/* because Ri/Rx/Rz is stored in row oriented order. */
  #ifndef NDEBUG
  	if (UMF_debug >= -2)
  	{
  	    for (j = 0 ; j < n_col ; j++)
  	    {
  		ASSERT (W [j] < p1) ;
  	    }
  	}
  #endif
  	for (p = p1 ; p < p2 ; p++)
  	{
  	    j = Rj [p] ;
  	    ASSERT (j >= 0 && j < n_col) ;
  	    pj = W [j] ;
  	    if (pj >= p1)
  	    {
  		/* this column index, j, is already in row i, at position pj */
  		ASSERT (pj < p) ;
  		ASSERT (Rj [pj] == j) ;
  #ifdef DO_MAP
  		Map2 [p] = pj ;
  		duplicates = TRUE ;
  #endif
  #ifdef DO_VALUES
  		/* sum the entry */
  #ifdef COMPLEX
  		if (split)
  		{
  		    Rx [pj] += Rx [p] ;
  		    Rz [pj] += Rz [p] ;
  		}
  		else
  		{
  		    Rx[2*pj  ] += Rx[2*p  ] ;
  		    Rx[2*pj+1] += Rx[2*p+1] ;
  		}
  #else
  		Rx [pj] += Rx [p] ;
  #endif
  #endif
  	    }
  	    else
  	    {
  		/* keep the entry */
  		/* also keep track in W[j] of position of a_ij for case above */
  		W [j] = pdest ;
  #ifdef DO_MAP
  		Map2 [p] = pdest ;
  #endif
  		/* no need to move the entry if pdest is equal to p */
  		if (pdest != p)
  		{
  		    Rj [pdest] = j ;
  #ifdef DO_VALUES
  #ifdef COMPLEX
  		    if (split)
  		    {
  			Rx [pdest] = Rx [p] ;
  			Rz [pdest] = Rz [p] ;
  		    }
  		    else
  		    {
  			Rx [2*pdest  ] = Rx [2*p  ] ;
  			Rx [2*pdest+1] = Rx [2*p+1] ;
  		    }
  #else
  		    Rx [pdest] = Rx [p] ;
  #endif
  #endif
  		}
  		pdest++ ;
  	    }
  	}
  	RowCount [i] = pdest - p1 ;
      }
  
      /* done using W for position of a_ij ] */
  
      /* ---------------------------------------------------------------------- */
      /* merge Map and Map2 into a single Map */
      /* ---------------------------------------------------------------------- */
  
  #ifdef DO_MAP
      if (duplicates)
      {
  	for (k = 0 ; k < nz ; k++)
  	{
  	    Map [k] = Map2 [Map [k]] ;
  	}
      }
  #ifndef NDEBUG
      else
      {
  	/* no duplicates, so no need to recompute Map */
  	for (k = 0 ; k < nz ; k++)
  	{
  	    ASSERT (Map2 [k] == k) ;
  	}
      }
      for (k = 0 ; k < nz ; k++)
      {
  	/* make sure that kth triplet is mapped correctly */
  	p = Map [k] ;
  	DEBUG1 (("Second row map: Map ["ID"] = "ID"
  ", k, p)) ;
  	i = Ti [k] ;
  	j = Tj [k] ;
  	ASSERT (j == Rj [p]) ;
  	ASSERT (Rp [i] <= p && p < Rp [i+1]) ;
      }
  #endif
  #endif
  
      /* now the kth triplet maps to p = Map [k], and thus to Rj/Rx [p] */
  
      /* ---------------------------------------------------------------------- */
      /* count the entries in each column */
      /* ---------------------------------------------------------------------- */
  
      /* [ use W as work space for column counts of A */
      for (j = 0 ; j < n_col ; j++)
      {
  	W [j] = 0 ;
      }
  
      for (i = 0 ; i < n_row ; i++)
      {
  	for (p = Rp [i] ; p < Rp [i] + RowCount [i] ; p++)
  	{
  	    j = Rj [p] ;
  	    ASSERT (j >= 0 && j < n_col) ;
  	    W [j]++ ;
  	}
      }
  
      /* ---------------------------------------------------------------------- */
      /* create the column pointers */
      /* ---------------------------------------------------------------------- */
  
      Ap [0] = 0 ;
      for (j = 0 ; j < n_col ; j++)
      {
  	Ap [j+1] = Ap [j] + W [j] ;
      }
      /* done using W as workspace for column counts of A ] */
  
      for (j = 0 ; j < n_col ; j++)
      {
  	W [j] = Ap [j] ;
      }
  
      /* ---------------------------------------------------------------------- */
      /* construct the column form */
      /* ---------------------------------------------------------------------- */
  
      for (i = 0 ; i < n_row ; i++)
      {
  	for (p = Rp [i] ; p < Rp [i] + RowCount [i] ; p++)
  	{
  	    cp = W [Rj [p]]++ ;
  #ifdef DO_MAP
  	    Map2 [p] = cp ;
  #endif
  	    Ai [cp] = i ;
  #ifdef DO_VALUES
  #ifdef COMPLEX
  	    if (split)
  	    {
  		Ax [cp] = Rx [p] ;
  		Az [cp] = Rz [p] ;
  	    }
  	    else
  	    {
  		Ax [2*cp  ] = Rx [2*p  ] ;
  		Ax [2*cp+1] = Rx [2*p+1] ;
  	    }
  #else
  	    Ax [cp] = Rx [p] ;
  #endif
  #endif
  	}
      }
  
      /* ---------------------------------------------------------------------- */
      /* merge Map and Map2 into a single Map */
      /* ---------------------------------------------------------------------- */
  
  #ifdef DO_MAP
      for (k = 0 ; k < nz ; k++)
      {
  	Map [k] = Map2 [Map [k]] ;
      }
  #endif
  
      /* now the kth triplet maps to p = Map [k], and thus to Ai/Ax [p] */
  
  #ifndef NDEBUG
      for (j = 0 ; j < n_col ; j++)
      {
  	ASSERT (W [j] == Ap [j+1]) ;
      }
  
      UMF_dump_col_matrix (
  #ifdef DO_VALUES
  	Ax,
  #ifdef COMPLEX
  	Az,
  #endif
  #else
  	(double *) NULL,
  #ifdef COMPLEX
  	(double *) NULL,
  #endif
  #endif
  	Ai, Ap, n_row, n_col, nz) ;
  
  #ifdef DO_MAP
      for (k = 0 ; k < nz ; k++)
      {
  	/* make sure that kth triplet is mapped correctly */
  	p = Map [k] ;
  	DEBUG1 (("Col map: Map ["ID"] = "ID"\t", k, p)) ;
  	i = Ti [k] ;
  	j = Tj [k] ;
  	ASSERT (i == Ai [p]) ;
  	DEBUG1 (("   i "ID" j "ID" Ap[j] "ID" p "ID" Ap[j+1] "ID"
  ",
  		i, j, Ap [j], p, Ap [j+1])) ;
  	ASSERT (Ap [j] <= p && p < Ap [j+1]) ;
      }
  #endif
  #endif
  
      return (UMFPACK_OK) ;
  }