File: temp_multivector.c

package info (click to toggle)
hypre 2.8.0b-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 147,420 kB
  • sloc: ansic: 1,781,700; f90: 251,025; cpp: 218,266; fortran: 51,552; sh: 17,720; java: 12,225; makefile: 5,371; python: 1,096; awk: 147; pascal: 92; perl: 56; lisp: 28; csh: 11
file content (536 lines) | stat: -rw-r--r-- 11,982 bytes parent folder | download | duplicates (2)
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*BHEADER**********************************************************************
 * Copyright (c) 2008,  Lawrence Livermore National Security, LLC.
 * Produced at the Lawrence Livermore National Laboratory.
 * This file is part of HYPRE.  See file COPYRIGHT for details.
 *
 * HYPRE is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License (as published by the Free
 * Software Foundation) version 2.1 dated February 1999.
 *
 * $Revision: 1.11 $
 ***********************************************************************EHEADER*/




#include <assert.h>
#include <math.h>
#include <stdlib.h>

#include "temp_multivector.h"
#include "interpreter.h"
#include "_hypre_utilities.h"

static void
mv_collectVectorPtr( HYPRE_Int* mask, mv_TempMultiVector* x, void** px ) {

  HYPRE_Int ix, jx;

  if ( mask != NULL ) {
    for ( ix = 0, jx = 0; ix < x->numVectors; ix++ )
      if ( mask[ix] )
	px[jx++] = x->vector[ix];
  }
  else
    for ( ix = 0; ix < x->numVectors; ix++ )
      px[ix] = x->vector[ix];

}

static HYPRE_Int
aux_maskCount( HYPRE_Int n, HYPRE_Int* mask ) {

  HYPRE_Int i, m;

  if ( mask == NULL )
    return n;

  for ( i = m = 0; i < n; i++ )
    if ( mask[i] )
      m++;

  return m;
}

static void
aux_indexFromMask( HYPRE_Int n, HYPRE_Int* mask, HYPRE_Int* index ) {

  HYPRE_Int i, j;
  
  if ( mask != NULL ) {
    for ( i = 0, j = 0; i < n; i++ )
      if ( mask[i] )
	index[j++] = i + 1;
  }
  else
    for ( i = 0; i < n; i++ )
      index[i] = i + 1;

}

/* ------- here goes simple random number generator --------- */

static hypre_ulongint next = 1;

/* RAND_MAX assumed to be 32767 */
static HYPRE_Int myrand(void) {
   next = next * 1103515245 + 12345;
   return((unsigned)(next/65536) % 32768);
}

static void mysrand(unsigned seed) {
   next = seed;
}


void*
mv_TempMultiVectorCreateFromSampleVector( void* ii_, HYPRE_Int n, void* sample ) { 

  HYPRE_Int i;
  mv_TempMultiVector* x;
  mv_InterfaceInterpreter* ii = (mv_InterfaceInterpreter*)ii_;

  x = (mv_TempMultiVector*) malloc(sizeof(mv_TempMultiVector));
  hypre_assert( x != NULL );
  
  x->interpreter = ii;
  x->numVectors = n;
  
  x->vector = (void**) calloc( n, sizeof(void*) );
  hypre_assert( x->vector != NULL );

  x->ownsVectors = 1;
  x->mask = NULL;
  x->ownsMask = 0;

  for ( i = 0; i < n; i++ )
    x->vector[i] = (ii->CreateVector)(sample);

  return x;

}

void*
mv_TempMultiVectorCreateCopy( void* src_, HYPRE_Int copyValues ) {

  HYPRE_Int i, n;

  mv_TempMultiVector* src;
  mv_TempMultiVector* dest;

  src = (mv_TempMultiVector*)src_;
  hypre_assert( src != NULL );

  n = src->numVectors;

  dest = mv_TempMultiVectorCreateFromSampleVector( src->interpreter, 
						      n, src->vector[0] );
  if ( copyValues )
    for ( i = 0; i < n; i++ ) {
      (dest->interpreter->CopyVector)(src->vector[i],dest->vector[i]);
  }

  return dest;
}

void 
mv_TempMultiVectorDestroy( void* x_ ) {

  HYPRE_Int i;
  mv_TempMultiVector* x = (mv_TempMultiVector*)x_;

  if ( x == NULL )
    return;

  if ( x->ownsVectors && x->vector != NULL ) {
    for ( i = 0; i < x->numVectors; i++ )
      (x->interpreter->DestroyVector)(x->vector[i]);
    free(x->vector);
  }
  if ( x->mask && x->ownsMask )
    free(x->mask);
  free(x);
}

HYPRE_Int
mv_TempMultiVectorWidth( void* x_ ) {

  mv_TempMultiVector* x = (mv_TempMultiVector*)x_;

  if ( x == NULL )
    return 0;

  return x->numVectors;
}

HYPRE_Int
mv_TempMultiVectorHeight( void* x_ ) {
 
  mv_TempMultiVector* x = (mv_TempMultiVector*)x_;
  
  if ( x == NULL )
   return 0; 
 
  return (x->interpreter->VectorSize)(x->vector[0]); 
}

/* this shallow copy of the mask is convenient but not safe;
   a proper copy should be considered */
void
mv_TempMultiVectorSetMask( void* x_, HYPRE_Int* mask ) {

  mv_TempMultiVector* x = (mv_TempMultiVector*)x_;

  hypre_assert( x != NULL );
  x->mask = mask;
  x->ownsMask = 0;
}

void
mv_TempMultiVectorClear( void* x_ ) {

  HYPRE_Int i;
  mv_TempMultiVector* x = (mv_TempMultiVector*)x_;

  hypre_assert( x != NULL );

  for ( i = 0; i < x->numVectors; i++ )
    if ( x->mask == NULL || (x->mask)[i] )
      (x->interpreter->ClearVector)(x->vector[i]);
}

void
mv_TempMultiVectorSetRandom( void* x_, HYPRE_Int seed ) {

  HYPRE_Int i;
  mv_TempMultiVector* x = (mv_TempMultiVector*)x_;

  hypre_assert( x != NULL );

  mysrand(seed);

  for ( i = 0; i < x->numVectors; i++ ) {
    if ( x->mask == NULL || (x->mask)[i] ) {
      seed=myrand();
      (x->interpreter->SetRandomValues)(x->vector[i], seed);
    }
  }
}



void 
mv_TempMultiVectorCopy( void* src_, void* dest_ ) {

  HYPRE_Int i, ms, md;
  void** ps;
  void** pd;
  mv_TempMultiVector* src = (mv_TempMultiVector*)src_;
  mv_TempMultiVector* dest = (mv_TempMultiVector*)dest_;

  hypre_assert( src != NULL && dest != NULL );

  ms = aux_maskCount( src->numVectors, src->mask );
  md = aux_maskCount( dest->numVectors, dest->mask );
  hypre_assert( ms == md );
	
  ps = (void**) calloc( ms, sizeof(void*) );
  hypre_assert( ps != NULL );
  pd = (void**) calloc( md, sizeof(void*) );
  hypre_assert( pd != NULL );

  mv_collectVectorPtr( src->mask, src, ps );
  mv_collectVectorPtr( dest->mask, dest, pd );

  for ( i = 0; i < ms; i++ )
    (src->interpreter->CopyVector)(ps[i],pd[i]);

  free(ps);
  free(pd);
}

void 
mv_TempMultiVectorAxpy( double a, void* x_, void* y_ ) { 
	
  HYPRE_Int i, mx, my;
  void** px;
  void** py;
  mv_TempMultiVector* x;
  mv_TempMultiVector* y;

  x = (mv_TempMultiVector*)x_;
  y = (mv_TempMultiVector*)y_;
  hypre_assert( x != NULL && y != NULL );

  mx = aux_maskCount( x->numVectors, x->mask );
  my = aux_maskCount( y->numVectors, y->mask );
  hypre_assert( mx == my );

  px = (void**) calloc( mx, sizeof(void*) );
  hypre_assert( px != NULL );
  py = (void**) calloc( my, sizeof(void*) );
  hypre_assert( py != NULL );

  mv_collectVectorPtr( x->mask, x, px );
  mv_collectVectorPtr( y->mask, y, py );

  for ( i = 0; i < mx; i++ )
    (x->interpreter->Axpy)(a,px[i],py[i]);

  free(px);
  free(py);
}

void 
mv_TempMultiVectorByMultiVector( void* x_, void* y_,
				     HYPRE_Int xyGHeight, HYPRE_Int xyHeight, 
				     HYPRE_Int xyWidth, double* xyVal ) { 
/* xy = x'*y */	

  HYPRE_Int ix, iy, mx, my, jxy;
  double* p;
  void** px;
  void** py;
  mv_TempMultiVector* x;
  mv_TempMultiVector* y;

  x = (mv_TempMultiVector*)x_;
  y = (mv_TempMultiVector*)y_;
  hypre_assert( x != NULL && y != NULL );

  mx = aux_maskCount( x->numVectors, x->mask );
  hypre_assert( mx == xyHeight );

  my = aux_maskCount( y->numVectors, y->mask );
  hypre_assert( my == xyWidth );

  px = (void**) calloc( mx, sizeof(void*) );
  hypre_assert( px != NULL );
  py = (void**) calloc( my, sizeof(void*) );
  hypre_assert( py != NULL );

  mv_collectVectorPtr( x->mask, x, px );
  mv_collectVectorPtr( y->mask, y, py );

  jxy = xyGHeight - xyHeight;
  for ( iy = 0, p = xyVal; iy < my; iy++ ) {
    for ( ix = 0; ix < mx; ix++, p++ )
      *p = (x->interpreter->InnerProd)(px[ix],py[iy]);
    p += jxy;
  }

  free(px);
  free(py);

}

void 
mv_TempMultiVectorByMultiVectorDiag( void* x_, void* y_,
					HYPRE_Int* mask, HYPRE_Int n, double* diag ) {
/* diag = diag(x'*y) */	

  HYPRE_Int i, mx, my, m;
  void** px;
  void** py;
  HYPRE_Int* index;
  mv_TempMultiVector* x;
  mv_TempMultiVector* y;

  x = (mv_TempMultiVector*)x_;
  y = (mv_TempMultiVector*)y_;
  hypre_assert( x != NULL && y != NULL );

  mx = aux_maskCount( x->numVectors, x->mask );
  my = aux_maskCount( y->numVectors, y->mask );
  m = aux_maskCount( n, mask );
  hypre_assert( mx == my && mx == m );

  px = (void**) calloc( mx, sizeof(void*) );
  hypre_assert( px != NULL );
  py = (void**) calloc( my, sizeof(void*) );
  hypre_assert( py != NULL );

  mv_collectVectorPtr( x->mask, x, px );
  mv_collectVectorPtr( y->mask, y, py );

  index = (HYPRE_Int*)calloc( m, sizeof(HYPRE_Int) );
  aux_indexFromMask( n, mask, index );

  for ( i = 0; i < m; i++ )
    *(diag+index[i]-1) = (x->interpreter->InnerProd)(px[i],py[i]);
  
  free(index);
  free(px);
  free(py);

}

void 
mv_TempMultiVectorByMatrix( void* x_, 
			       HYPRE_Int rGHeight, HYPRE_Int rHeight, 
			       HYPRE_Int rWidth, double* rVal,
			       void* y_ ) {

  HYPRE_Int i, j, jump;
  HYPRE_Int mx, my;
  double* p;
  void** px;
  void** py;
  mv_TempMultiVector* x;
  mv_TempMultiVector* y;

  x = (mv_TempMultiVector*)x_;
  y = (mv_TempMultiVector*)y_;
  hypre_assert( x != NULL && y != NULL );

  mx = aux_maskCount( x->numVectors, x->mask );
  my = aux_maskCount( y->numVectors, y->mask );

  hypre_assert( mx == rHeight && my == rWidth );
  
  px = (void**) calloc( mx, sizeof(void*) );
  hypre_assert( px != NULL );
  py = (void**) calloc( my, sizeof(void*) );
  hypre_assert( py != NULL );
  
  mv_collectVectorPtr( x->mask, x, px );
  mv_collectVectorPtr( y->mask, y, py );

  jump = rGHeight - rHeight;
  for ( j = 0, p = rVal; j < my; j++ ) {
    (x->interpreter->ClearVector)( py[j] );
    for ( i = 0; i < mx; i++, p++ )
      (x->interpreter->Axpy)(*p,px[i],py[j]);
    p += jump;
  }

  free(px);
  free(py);
}

void 
mv_TempMultiVectorXapy( void* x_, 
			   HYPRE_Int rGHeight, HYPRE_Int rHeight, 
			   HYPRE_Int rWidth, double* rVal,
			   void* y_ ) {

  HYPRE_Int i, j, jump;
  HYPRE_Int mx, my;
  double* p;
  void** px;
  void** py;
  mv_TempMultiVector* x;
  mv_TempMultiVector* y;

  x = (mv_TempMultiVector*)x_;
  y = (mv_TempMultiVector*)y_;
  hypre_assert( x != NULL && y != NULL );

  mx = aux_maskCount( x->numVectors, x->mask );
  my = aux_maskCount( y->numVectors, y->mask );

  hypre_assert( mx == rHeight && my == rWidth );
  
  px = (void**) calloc( mx, sizeof(void*) );
  hypre_assert( px != NULL );
  py = (void**) calloc( my, sizeof(void*) );
  hypre_assert( py != NULL );
  
  mv_collectVectorPtr( x->mask, x, px );
  mv_collectVectorPtr( y->mask, y, py );

  jump = rGHeight - rHeight;
  for ( j = 0, p = rVal; j < my; j++ ) {
    for ( i = 0; i < mx; i++, p++ )
      (x->interpreter->Axpy)(*p,px[i],py[j]);
    p += jump;
  }

  free(px);
  free(py);
}

void 
mv_TempMultiVectorByDiagonal( void* x_, 
				HYPRE_Int* mask, HYPRE_Int n, double* diag,
				void* y_ ) {

  HYPRE_Int j;
  HYPRE_Int mx, my, m;
  void** px;
  void** py;
  HYPRE_Int* index;
  mv_TempMultiVector* x;
  mv_TempMultiVector* y;

  x = (mv_TempMultiVector*)x_;
  y = (mv_TempMultiVector*)y_;
  hypre_assert( x != NULL && y != NULL );

  mx = aux_maskCount( x->numVectors, x->mask );
  my = aux_maskCount( y->numVectors, y->mask );
  m = aux_maskCount( n, mask );
	
  hypre_assert( mx == m && my == m );

  if ( m < 1 )
    return;

  px = (void**) calloc( mx, sizeof(void*) );
  hypre_assert( px != NULL );
  py = (void**) calloc( my, sizeof(void*) );
  hypre_assert( py != NULL );

  index = (HYPRE_Int*)calloc( m, sizeof(HYPRE_Int) );
  aux_indexFromMask( n, mask, index );

  mv_collectVectorPtr( x->mask, x, px );
  mv_collectVectorPtr( y->mask, y, py );

  for ( j = 0; j < my; j++ ) {
    (x->interpreter->ClearVector)(py[j]);
    (x->interpreter->Axpy)(diag[index[j]-1],px[j],py[j]);
  }

  free(px);
  free(py);
  free( index );
}

void 
mv_TempMultiVectorEval( void (*f)( void*, void*, void* ), void* par,
			   void* x_, void* y_ ) {

  HYPRE_Int i, mx, my;
  void** px;
  void** py;
  mv_TempMultiVector* x;
  mv_TempMultiVector* y;

  x = (mv_TempMultiVector*)x_;
  y = (mv_TempMultiVector*)y_;
  hypre_assert( x != NULL && y != NULL );

  if ( f == NULL ) {
    mv_TempMultiVectorCopy( x, y );
    return;
  }

  mx = aux_maskCount( x->numVectors, x->mask );
  my = aux_maskCount( y->numVectors, y->mask );
  hypre_assert( mx == my );

  px = (void**) calloc( mx, sizeof(void*) );
  hypre_assert( px != NULL );
  py = (void**) calloc( my, sizeof(void*) );
  hypre_assert( py != NULL );

  mv_collectVectorPtr( x->mask, x, px );
  mv_collectVectorPtr( y->mask, y, py );

  for ( i = 0; i < mx; i++ )
    f( par, (void*)px[i], (void*)py[i] );

  free(px);
  free(py);
}