| [2aa6644] | 1 | /*BHEADER**********************************************************************
|
|---|
| 2 | * Copyright (c) 2008, Lawrence Livermore National Security, LLC.
|
|---|
| 3 | * Produced at the Lawrence Livermore National Laboratory.
|
|---|
| 4 | * This file is part of HYPRE. See file COPYRIGHT for details.
|
|---|
| 5 | *
|
|---|
| 6 | * HYPRE is free software; you can redistribute it and/or modify it under the
|
|---|
| 7 | * terms of the GNU Lesser General Public License (as published by the Free
|
|---|
| 8 | * Software Foundation) version 2.1 dated February 1999.
|
|---|
| 9 | *
|
|---|
| 10 | * $Revision: 2.4 $
|
|---|
| 11 | ***********************************************************************EHEADER*/
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 | /******************************************************************************
|
|---|
| 16 | *
|
|---|
| 17 | * Member functions for hypre_Vector class.
|
|---|
| 18 | *
|
|---|
| 19 | *****************************************************************************/
|
|---|
| 20 |
|
|---|
| 21 | #include "headers.h"
|
|---|
| 22 | #include <assert.h>
|
|---|
| 23 |
|
|---|
| 24 | /*--------------------------------------------------------------------------
|
|---|
| 25 | * hypre_SeqVectorCreate
|
|---|
| 26 | *--------------------------------------------------------------------------*/
|
|---|
| 27 |
|
|---|
| 28 | hypre_Vector *
|
|---|
| 29 | hypre_SeqVectorCreate( int size )
|
|---|
| 30 | {
|
|---|
| 31 | hypre_Vector *vector;
|
|---|
| 32 |
|
|---|
| 33 | vector = hypre_CTAlloc(hypre_Vector, 1);
|
|---|
| 34 |
|
|---|
| 35 | hypre_VectorData(vector) = NULL;
|
|---|
| 36 | hypre_VectorSize(vector) = size;
|
|---|
| 37 |
|
|---|
| 38 | hypre_VectorNumVectors(vector) = 1;
|
|---|
| 39 | hypre_VectorMultiVecStorageMethod(vector) = 0;
|
|---|
| 40 |
|
|---|
| 41 | /* set defaults */
|
|---|
| 42 | hypre_VectorOwnsData(vector) = 1;
|
|---|
| 43 |
|
|---|
| 44 | return vector;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | /*--------------------------------------------------------------------------
|
|---|
| 48 | * hypre_SeqMultiVectorCreate
|
|---|
| 49 | *--------------------------------------------------------------------------*/
|
|---|
| 50 |
|
|---|
| 51 | hypre_Vector *
|
|---|
| 52 | hypre_SeqMultiVectorCreate( int size, int num_vectors )
|
|---|
| 53 | {
|
|---|
| 54 | hypre_Vector *vector = hypre_SeqVectorCreate(size);
|
|---|
| 55 | hypre_VectorNumVectors(vector) = num_vectors;
|
|---|
| 56 | return vector;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /*--------------------------------------------------------------------------
|
|---|
| 60 | * hypre_SeqVectorDestroy
|
|---|
| 61 | *--------------------------------------------------------------------------*/
|
|---|
| 62 |
|
|---|
| 63 | int
|
|---|
| 64 | hypre_SeqVectorDestroy( hypre_Vector *vector )
|
|---|
| 65 | {
|
|---|
| 66 | int ierr=0;
|
|---|
| 67 |
|
|---|
| 68 | if (vector)
|
|---|
| 69 | {
|
|---|
| 70 | if ( hypre_VectorOwnsData(vector) )
|
|---|
| 71 | {
|
|---|
| 72 | hypre_TFree(hypre_VectorData(vector));
|
|---|
| 73 | }
|
|---|
| 74 | hypre_TFree(vector);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | return ierr;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /*--------------------------------------------------------------------------
|
|---|
| 81 | * hypre_SeqVectorInitialize
|
|---|
| 82 | *--------------------------------------------------------------------------*/
|
|---|
| 83 |
|
|---|
| 84 | int
|
|---|
| 85 | hypre_SeqVectorInitialize( hypre_Vector *vector )
|
|---|
| 86 | {
|
|---|
| 87 | int size = hypre_VectorSize(vector);
|
|---|
| 88 | int ierr = 0;
|
|---|
| 89 | int num_vectors = hypre_VectorNumVectors(vector);
|
|---|
| 90 | int multivec_storage_method = hypre_VectorMultiVecStorageMethod(vector);
|
|---|
| 91 |
|
|---|
| 92 | if ( ! hypre_VectorData(vector) )
|
|---|
| 93 | {
|
|---|
| 94 | hypre_VectorData(vector) = hypre_CTAlloc(double, num_vectors*size);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | if ( multivec_storage_method == 0 )
|
|---|
| 98 | {
|
|---|
| 99 | hypre_VectorVectorStride(vector) = size;
|
|---|
| 100 | hypre_VectorIndexStride(vector) = 1;
|
|---|
| 101 | }
|
|---|
| 102 | else if ( multivec_storage_method == 1 )
|
|---|
| 103 | {
|
|---|
| 104 | hypre_VectorVectorStride(vector) = 1;
|
|---|
| 105 | hypre_VectorIndexStride(vector) = num_vectors;
|
|---|
| 106 | }
|
|---|
| 107 | else
|
|---|
| 108 | ++ierr;
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 | return ierr;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /*--------------------------------------------------------------------------
|
|---|
| 115 | * hypre_SeqVectorSetDataOwner
|
|---|
| 116 | *--------------------------------------------------------------------------*/
|
|---|
| 117 |
|
|---|
| 118 | int
|
|---|
| 119 | hypre_SeqVectorSetDataOwner( hypre_Vector *vector,
|
|---|
| 120 | int owns_data )
|
|---|
| 121 | {
|
|---|
| 122 | int ierr=0;
|
|---|
| 123 |
|
|---|
| 124 | hypre_VectorOwnsData(vector) = owns_data;
|
|---|
| 125 |
|
|---|
| 126 | return ierr;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /*--------------------------------------------------------------------------
|
|---|
| 130 | * ReadVector
|
|---|
| 131 | *--------------------------------------------------------------------------*/
|
|---|
| 132 |
|
|---|
| 133 | hypre_Vector *
|
|---|
| 134 | hypre_SeqVectorRead( char *file_name )
|
|---|
| 135 | {
|
|---|
| 136 | hypre_Vector *vector;
|
|---|
| 137 |
|
|---|
| 138 | FILE *fp;
|
|---|
| 139 |
|
|---|
| 140 | double *data;
|
|---|
| 141 | int size;
|
|---|
| 142 |
|
|---|
| 143 | int j;
|
|---|
| 144 |
|
|---|
| 145 | /*----------------------------------------------------------
|
|---|
| 146 | * Read in the data
|
|---|
| 147 | *----------------------------------------------------------*/
|
|---|
| 148 |
|
|---|
| 149 | fp = fopen(file_name, "r");
|
|---|
| 150 |
|
|---|
| 151 | fscanf(fp, "%d", &size);
|
|---|
| 152 |
|
|---|
| 153 | vector = hypre_SeqVectorCreate(size);
|
|---|
| 154 | hypre_SeqVectorInitialize(vector);
|
|---|
| 155 |
|
|---|
| 156 | data = hypre_VectorData(vector);
|
|---|
| 157 | for (j = 0; j < size; j++)
|
|---|
| 158 | {
|
|---|
| 159 | fscanf(fp, "%le", &data[j]);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | fclose(fp);
|
|---|
| 163 |
|
|---|
| 164 | /* multivector code not written yet >>> */
|
|---|
| 165 | hypre_assert( hypre_VectorNumVectors(vector) == 1 );
|
|---|
| 166 |
|
|---|
| 167 | return vector;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /*--------------------------------------------------------------------------
|
|---|
| 171 | * hypre_SeqVectorPrint
|
|---|
| 172 | *--------------------------------------------------------------------------*/
|
|---|
| 173 |
|
|---|
| 174 | int
|
|---|
| 175 | hypre_SeqVectorPrint( hypre_Vector *vector,
|
|---|
| 176 | char *file_name )
|
|---|
| 177 | {
|
|---|
| 178 | FILE *fp;
|
|---|
| 179 |
|
|---|
| 180 | double *data;
|
|---|
| 181 | int size, num_vectors, vecstride, idxstride;
|
|---|
| 182 |
|
|---|
| 183 | int i, j;
|
|---|
| 184 |
|
|---|
| 185 | int ierr = 0;
|
|---|
| 186 |
|
|---|
| 187 | num_vectors = hypre_VectorNumVectors(vector);
|
|---|
| 188 | vecstride = hypre_VectorVectorStride(vector);
|
|---|
| 189 | idxstride = hypre_VectorIndexStride(vector);
|
|---|
| 190 |
|
|---|
| 191 | /*----------------------------------------------------------
|
|---|
| 192 | * Print in the data
|
|---|
| 193 | *----------------------------------------------------------*/
|
|---|
| 194 |
|
|---|
| 195 | data = hypre_VectorData(vector);
|
|---|
| 196 | size = hypre_VectorSize(vector);
|
|---|
| 197 |
|
|---|
| 198 | fp = fopen(file_name, "w");
|
|---|
| 199 |
|
|---|
| 200 | if ( hypre_VectorNumVectors(vector) == 1 )
|
|---|
| 201 | {
|
|---|
| 202 | fprintf(fp, "%d\n", size);
|
|---|
| 203 | }
|
|---|
| 204 | else
|
|---|
| 205 | {
|
|---|
| 206 | fprintf(fp, "%d vectors of size %d\n", num_vectors, size );
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | if ( num_vectors>1 )
|
|---|
| 210 | {
|
|---|
| 211 | for ( j=0; j<num_vectors; ++j )
|
|---|
| 212 | {
|
|---|
| 213 | fprintf(fp, "vector %d\n", j );
|
|---|
| 214 | for (i = 0; i < size; i++)
|
|---|
| 215 | {
|
|---|
| 216 | fprintf(fp, "%.14e\n", data[ j*vecstride + i*idxstride ] );
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 | else
|
|---|
| 221 | {
|
|---|
| 222 | for (i = 0; i < size; i++)
|
|---|
| 223 | {
|
|---|
| 224 | fprintf(fp, "%.14e\n", data[i]);
|
|---|
| 225 | }
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | fclose(fp);
|
|---|
| 229 |
|
|---|
| 230 | return ierr;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | /*--------------------------------------------------------------------------
|
|---|
| 234 | * hypre_SeqVectorSetConstantValues
|
|---|
| 235 | *--------------------------------------------------------------------------*/
|
|---|
| 236 |
|
|---|
| 237 | int
|
|---|
| 238 | hypre_SeqVectorSetConstantValues( hypre_Vector *v,
|
|---|
| 239 | double value )
|
|---|
| 240 | {
|
|---|
| 241 | double *vector_data = hypre_VectorData(v);
|
|---|
| 242 | int size = hypre_VectorSize(v);
|
|---|
| 243 |
|
|---|
| 244 | int i;
|
|---|
| 245 |
|
|---|
| 246 | int ierr = 0;
|
|---|
| 247 |
|
|---|
| 248 | size *=hypre_VectorNumVectors(v);
|
|---|
| 249 |
|
|---|
| 250 | #ifdef HYPRE_USING_OPENMP
|
|---|
| 251 | #pragma omp parallel for private(i) schedule(static)
|
|---|
| 252 | #endif
|
|---|
| 253 | for (i = 0; i < size; i++)
|
|---|
| 254 | vector_data[i] = value;
|
|---|
| 255 |
|
|---|
| 256 | return ierr;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | /*--------------------------------------------------------------------------
|
|---|
| 260 | * hypre_SeqVectorSetRandomValues
|
|---|
| 261 | *
|
|---|
| 262 | * returns vector of values randomly distributed between -1.0 and +1.0
|
|---|
| 263 | *--------------------------------------------------------------------------*/
|
|---|
| 264 |
|
|---|
| 265 | int
|
|---|
| 266 | hypre_SeqVectorSetRandomValues( hypre_Vector *v,
|
|---|
| 267 | int seed )
|
|---|
| 268 | {
|
|---|
| 269 | double *vector_data = hypre_VectorData(v);
|
|---|
| 270 | int size = hypre_VectorSize(v);
|
|---|
| 271 |
|
|---|
| 272 | int i;
|
|---|
| 273 |
|
|---|
| 274 | int ierr = 0;
|
|---|
| 275 | hypre_SeedRand(seed);
|
|---|
| 276 |
|
|---|
| 277 | size *=hypre_VectorNumVectors(v);
|
|---|
| 278 |
|
|---|
| 279 | /* RDF: threading this loop may cause problems because of hypre_Rand() */
|
|---|
| 280 | for (i = 0; i < size; i++)
|
|---|
| 281 | vector_data[i] = 2.0 * hypre_Rand() - 1.0;
|
|---|
| 282 |
|
|---|
| 283 | return ierr;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | /*--------------------------------------------------------------------------
|
|---|
| 287 | * hypre_SeqVectorCopy
|
|---|
| 288 | * copies data from x to y
|
|---|
| 289 | * y should have already been initialized at the same size as x
|
|---|
| 290 | *--------------------------------------------------------------------------*/
|
|---|
| 291 |
|
|---|
| 292 | int
|
|---|
| 293 | hypre_SeqVectorCopy( hypre_Vector *x,
|
|---|
| 294 | hypre_Vector *y )
|
|---|
| 295 | {
|
|---|
| 296 | double *x_data = hypre_VectorData(x);
|
|---|
| 297 | double *y_data = hypre_VectorData(y);
|
|---|
| 298 | int size = hypre_VectorSize(x);
|
|---|
| 299 |
|
|---|
| 300 | int i;
|
|---|
| 301 |
|
|---|
| 302 | int ierr = 0;
|
|---|
| 303 |
|
|---|
| 304 | size *=hypre_VectorNumVectors(x);
|
|---|
| 305 | #ifdef HYPRE_USING_OPENMP
|
|---|
| 306 | #pragma omp parallel for private(i) schedule(static)
|
|---|
| 307 | #endif
|
|---|
| 308 | for (i = 0; i < size; i++)
|
|---|
| 309 | y_data[i] = x_data[i];
|
|---|
| 310 |
|
|---|
| 311 | return ierr;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | /*--------------------------------------------------------------------------
|
|---|
| 315 | * hypre_SeqVectorCloneDeep
|
|---|
| 316 | * Returns a complete copy of x - a deep copy, with its own copy of the data.
|
|---|
| 317 | *--------------------------------------------------------------------------*/
|
|---|
| 318 |
|
|---|
| 319 | hypre_Vector *
|
|---|
| 320 | hypre_SeqVectorCloneDeep( hypre_Vector *x )
|
|---|
| 321 | {
|
|---|
| 322 | int size = hypre_VectorSize(x);
|
|---|
| 323 | int num_vectors = hypre_VectorNumVectors(x);
|
|---|
| 324 | hypre_Vector * y = hypre_SeqMultiVectorCreate( size, num_vectors );
|
|---|
| 325 |
|
|---|
| 326 | hypre_VectorMultiVecStorageMethod(y) = hypre_VectorMultiVecStorageMethod(x);
|
|---|
| 327 | hypre_VectorVectorStride(y) = hypre_VectorVectorStride(x);
|
|---|
| 328 | hypre_VectorIndexStride(y) = hypre_VectorIndexStride(x);
|
|---|
| 329 |
|
|---|
| 330 | hypre_SeqVectorInitialize(y);
|
|---|
| 331 | hypre_SeqVectorCopy( x, y );
|
|---|
| 332 |
|
|---|
| 333 | return y;
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | /*--------------------------------------------------------------------------
|
|---|
| 337 | * hypre_SeqVectorCloneShallow
|
|---|
| 338 | * Returns a complete copy of x - a shallow copy, pointing the data of x
|
|---|
| 339 | *--------------------------------------------------------------------------*/
|
|---|
| 340 |
|
|---|
| 341 | hypre_Vector *
|
|---|
| 342 | hypre_SeqVectorCloneShallow( hypre_Vector *x )
|
|---|
| 343 | {
|
|---|
| 344 | int size = hypre_VectorSize(x);
|
|---|
| 345 | int num_vectors = hypre_VectorNumVectors(x);
|
|---|
| 346 | hypre_Vector * y = hypre_SeqMultiVectorCreate( size, num_vectors );
|
|---|
| 347 |
|
|---|
| 348 | hypre_VectorMultiVecStorageMethod(y) = hypre_VectorMultiVecStorageMethod(x);
|
|---|
| 349 | hypre_VectorVectorStride(y) = hypre_VectorVectorStride(x);
|
|---|
| 350 | hypre_VectorIndexStride(y) = hypre_VectorIndexStride(x);
|
|---|
| 351 |
|
|---|
| 352 | hypre_VectorData(y) = hypre_VectorData(x);
|
|---|
| 353 | hypre_SeqVectorSetDataOwner( y, 0 );
|
|---|
| 354 | hypre_SeqVectorInitialize(y);
|
|---|
| 355 |
|
|---|
| 356 | return y;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | /*--------------------------------------------------------------------------
|
|---|
| 360 | * hypre_SeqVectorScale
|
|---|
| 361 | *--------------------------------------------------------------------------*/
|
|---|
| 362 |
|
|---|
| 363 | int
|
|---|
| 364 | hypre_SeqVectorScale( double alpha,
|
|---|
| 365 | hypre_Vector *y )
|
|---|
| 366 | {
|
|---|
| 367 | double *y_data = hypre_VectorData(y);
|
|---|
| 368 | int size = hypre_VectorSize(y);
|
|---|
| 369 |
|
|---|
| 370 | int i;
|
|---|
| 371 |
|
|---|
| 372 | int ierr = 0;
|
|---|
| 373 |
|
|---|
| 374 | size *=hypre_VectorNumVectors(y);
|
|---|
| 375 |
|
|---|
| 376 | #ifdef HYPRE_USING_OPENMP
|
|---|
| 377 | #pragma omp parallel for private(i) schedule(static)
|
|---|
| 378 | #endif
|
|---|
| 379 | for (i = 0; i < size; i++)
|
|---|
| 380 | y_data[i] *= alpha;
|
|---|
| 381 |
|
|---|
| 382 | return ierr;
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | /*--------------------------------------------------------------------------
|
|---|
| 386 | * hypre_SeqVectorAxpy
|
|---|
| 387 | *--------------------------------------------------------------------------*/
|
|---|
| 388 |
|
|---|
| 389 | int
|
|---|
| 390 | hypre_SeqVectorAxpy( double alpha,
|
|---|
| 391 | hypre_Vector *x,
|
|---|
| 392 | hypre_Vector *y )
|
|---|
| 393 | {
|
|---|
| 394 | double *x_data = hypre_VectorData(x);
|
|---|
| 395 | double *y_data = hypre_VectorData(y);
|
|---|
| 396 | int size = hypre_VectorSize(x);
|
|---|
| 397 |
|
|---|
| 398 | int i;
|
|---|
| 399 |
|
|---|
| 400 | int ierr = 0;
|
|---|
| 401 |
|
|---|
| 402 | size *=hypre_VectorNumVectors(x);
|
|---|
| 403 |
|
|---|
| 404 | #ifdef HYPRE_USING_OPENMP
|
|---|
| 405 | #pragma omp parallel for private(i) schedule(static)
|
|---|
| 406 | #endif
|
|---|
| 407 | for (i = 0; i < size; i++)
|
|---|
| 408 | y_data[i] += alpha * x_data[i];
|
|---|
| 409 |
|
|---|
| 410 | return ierr;
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | /*--------------------------------------------------------------------------
|
|---|
| 414 | * hypre_SeqVectorInnerProd
|
|---|
| 415 | *--------------------------------------------------------------------------*/
|
|---|
| 416 |
|
|---|
| 417 | double hypre_SeqVectorInnerProd( hypre_Vector *x,
|
|---|
| 418 | hypre_Vector *y )
|
|---|
| 419 | {
|
|---|
| 420 | double *x_data = hypre_VectorData(x);
|
|---|
| 421 | double *y_data = hypre_VectorData(y);
|
|---|
| 422 | int size = hypre_VectorSize(x);
|
|---|
| 423 |
|
|---|
| 424 | int i;
|
|---|
| 425 |
|
|---|
| 426 | double result = 0.0;
|
|---|
| 427 |
|
|---|
| 428 | size *=hypre_VectorNumVectors(x);
|
|---|
| 429 |
|
|---|
| 430 | #ifdef HYPRE_USING_OPENMP
|
|---|
| 431 | #pragma omp parallel for private(i) reduction(+:result) schedule(static)
|
|---|
| 432 | #endif
|
|---|
| 433 | for (i = 0; i < size; i++)
|
|---|
| 434 | result += y_data[i] * x_data[i];
|
|---|
| 435 |
|
|---|
| 436 | return result;
|
|---|
| 437 | }
|
|---|
| 438 |
|
|---|
| 439 | /*--------------------------------------------------------------------------
|
|---|
| 440 | * hypre_VectorSumElts:
|
|---|
| 441 | * Returns the sum of all vector elements.
|
|---|
| 442 | *--------------------------------------------------------------------------*/
|
|---|
| 443 |
|
|---|
| 444 | double hypre_VectorSumElts( hypre_Vector *vector )
|
|---|
| 445 | {
|
|---|
| 446 | double sum = 0;
|
|---|
| 447 | double * data = hypre_VectorData( vector );
|
|---|
| 448 | int size = hypre_VectorSize( vector );
|
|---|
| 449 | int i;
|
|---|
| 450 |
|
|---|
| 451 | #ifdef HYPRE_USING_OPENMP
|
|---|
| 452 | #pragma omp parallel for private(i) reduction(+:sum) schedule(static)
|
|---|
| 453 | #endif
|
|---|
| 454 | for ( i=0; i<size; ++i ) sum += data[i];
|
|---|
| 455 |
|
|---|
| 456 | return sum;
|
|---|
| 457 | }
|
|---|