Epetra Package Browser (Single Doxygen Collection) Development
Loading...
Searching...
No Matches
Epetra_LongLongVector.cpp
Go to the documentation of this file.
1
2//@HEADER
3// ************************************************************************
4//
5// Epetra: Linear Algebra Services Package
6// Copyright 2011 Sandia Corporation
7//
8// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9// the U.S. Government retains certain rights in this software.
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17//
18// 2. Redistributions in binary form must reproduce the above copyright
19// notice, this list of conditions and the following disclaimer in the
20// documentation and/or other materials provided with the distribution.
21//
22// 3. Neither the name of the Corporation nor the names of the
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39//
40// ************************************************************************
41//@HEADER
42
43#include "Epetra_ConfigDefs.h"
45#include "Epetra_Map.h"
46#include "Epetra_Comm.h"
47#include <limits>
48
49#ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
50
51//=============================================================================
53 : Epetra_DistObject(map, "Epetra::LongLongVector"),
54 Values_(0),
55 UserAllocated_(false),
56 Allocated_(false)
57{
58 if(!map.GlobalIndicesLongLong())
59 throw ReportError("Epetra_LongLongVector::Epetra_LongLongVector: cannot be called with non long long map index type", -1);
60
62 if(zeroOut) PutValue(0); // Zero out values
63}
64//=============================================================================
66 : Epetra_DistObject(Source),
67 Values_(0),
68 UserAllocated_(false),
69 Allocated_(false)
70{
71 if(!Source.Map().GlobalIndicesLongLong())
72 throw ReportError("Epetra_LongLongVector::Epetra_LongLongVector: cannot be called with non long long map index type", -1);
73
75 DoCopy(Source.Values_);
76}
77//=============================================================================
79 : Epetra_DistObject(map, "Epetra::LongLongVector"),
80 Values_(0),
81 UserAllocated_(false),
82 Allocated_(false)
83{
84 if(!map.GlobalIndicesLongLong())
85 throw ReportError("Epetra_LongLongVector::Epetra_LongLongVector: cannot be called with non long long map index type", -1);
86
87 if (CV==Copy) {
89 DoCopy(V);
90 }
91 else {
93 DoView(V);
94 }
95}
96//=========================================================================
98
99
100 if (Allocated_ && (!UserAllocated_) && Values_!=0) delete [] Values_;
101}
102
103//=========================================================================
105{
106
107 if (Allocated_) return(0);
108
109 int myLength = MyLength();
110 if (myLength>0)
111 Values_ = new long long[myLength];
112 else
113 Values_ = 0;
114
115 Allocated_ = true;
116 UserAllocated_ = false;
117 return(0);
118}
119
120//=========================================================================
122{
123 int iend = MyLength();
124 for (int i=0; i<iend; i++) Values_[i] = V[i];
125
126 return(0);
127}
128//=========================================================================
130{
131
132 Allocated_ = true;
133 UserAllocated_ = true;
134
135 return(0);
136}
137
138//=========================================================================
140{
141
142 Values_ = V;
143
144 return(0);
145}
146//=============================================================================
147int Epetra_LongLongVector::ExtractCopy(long long *V) const {
148
149 int iend = MyLength();
150 for (int i=0; i<iend; i++) V[i] = Values_[i];
151 return(0);
152}
153
154//=============================================================================
156{
157 *V = Values_;
158 return(0);
159}
160
161//=============================================================================
163 int iend = MyLength();
164 for (int i=0; i<iend; i++) Values_[i] = Value;
165 return(0);
166}
167//=============================================================================
169 long long result = std::numeric_limits<long long>::min(); // smallest 64 bit int
170 int iend = MyLength();
171 if (iend>0) result = Values_[0];
172 for (int i=0; i<iend; i++) result = EPETRA_MAX(result, Values_[i]);
173 long long globalResult;
174 this->Comm().MaxAll(&result, &globalResult, 1);
175 return(globalResult);
176}
177//=============================================================================
179 long long result = std::numeric_limits<long long>::max(); // largest 64 bit int
180 int iend = MyLength();
181 if (iend>0) result = Values_[0];
182 for (int i=0; i<iend; i++) result = EPETRA_MIN(result, Values_[i]);
183 long long globalResult;
184 this->Comm().MinAll(&result, &globalResult, 1);
185 return(globalResult);
186}
187//========================================================================
189
190
191 if (MyLength() != V.MyLength())
192 throw ReportError("Length of LongLongVectors incompatible in Assign. The this LongLongVector has MyLength = " + toString(MyLength())
193 + ". The V LongLongVector has MyLength = " + toString(V.MyLength()), -1);
194
195 int iend = MyLength();
196 for (int i=0; i<iend; i++) Values_[i] =V[i];
197 return(*this);
198}
199
200void Epetra_LongLongVector::Print(std::ostream& os) const {
201 int MyPID = Map().Comm().MyPID();
202 int NumProc = Map().Comm().NumProc();
203
204 for (int iproc=0; iproc < NumProc; iproc++) {
205 if (MyPID==iproc) {
206 int NumMyElements1 =Map(). NumMyElements();
207 int MaxElementSize1 = Map().MaxElementSize();
208 long long * MyGlobalElements1 = Map().MyGlobalElements64();
209 int * FirstPointInElementList1=0;
210 if (MaxElementSize1!=1) FirstPointInElementList1 = Map().FirstPointInElementList();
211
212 if (MyPID==0) {
213 os.width(8);
214 os << " MyPID"; os << " ";
215 os.width(12);
216 if (MaxElementSize1==1)
217 os << "GID ";
218 else
219 os << " GID/Point";
220 os.width(20);
221 os << "Value ";
222 os << std::endl;
223 }
224 for (int i=0; i < NumMyElements1; i++) {
225 for (int ii=0; ii< Map().ElementSize(i); ii++) {
226 int iii;
227 os.width(10);
228 os << MyPID; os << " ";
229 os.width(10);
230 if (MaxElementSize1==1) {
231 os << MyGlobalElements1[i] << " ";
232 iii = i;
233 }
234 else {
235 os << MyGlobalElements1[i]<< "/" << ii << " ";
236 iii = FirstPointInElementList1[i]+ii;
237 }
238 os.width(20);
239 os << Values_[iii];
240 os << std::endl;
241 }
242 }
243 os << std::flush;
244 }
245
246 // Do a few global ops to give I/O a chance to complete
247 Map().Comm().Barrier();
248 Map().Comm().Barrier();
249 Map().Comm().Barrier();
250 }
251 return;
252}
253//=========================================================================
255{
256 (void)Source;
257 return(0);
258}
259
260//=========================================================================
262 int NumSameIDs,
263 int NumPermuteIDs,
264 int * PermuteToLIDs,
265 int *PermuteFromLIDs,
266 const Epetra_OffsetIndex * Indexor,
267 Epetra_CombineMode CombineMode)
268{
269 (void)Indexor;
270 const Epetra_LongLongVector & A = dynamic_cast<const Epetra_LongLongVector &>(Source);
271
272 long long * From;
273 A.ExtractView(&From);
274 long long *To = Values_;
275
276 int * ToFirstPointInElementList = 0;
277 int * FromFirstPointInElementList = 0;
278 int * FromElementSizeList = 0;
279 int MaxElementSize = Map().MaxElementSize();
280 bool ConstantElementSize = Map().ConstantElementSize();
281
282 if (!ConstantElementSize) {
283 ToFirstPointInElementList = Map().FirstPointInElementList();
284 FromFirstPointInElementList = A.Map().FirstPointInElementList();
285 FromElementSizeList = A.Map().ElementSizeList();
286 }
287 int j, jj, jjj, k;
288
289 int NumSameEntries;
290
291 bool Case1 = false;
292 bool Case2 = false;
293 // bool Case3 = false;
294
295 if (MaxElementSize==1) {
296 Case1 = true;
297 NumSameEntries = NumSameIDs;
298 }
299 else if (ConstantElementSize) {
300 Case2 = true;
301 NumSameEntries = NumSameIDs * MaxElementSize;
302 }
303 else {
304 // Case3 = true;
305 NumSameEntries = FromFirstPointInElementList[NumSameIDs];
306 }
307
308 // Short circuit for the case where the source and target vector is the same.
309 if (To==From) NumSameEntries = 0;
310
311 // Do copy first
312 if (NumSameIDs>0)
313 if (To!=From) {
314 if (CombineMode==Epetra_AddLocalAlso)
315 for (j=0; j<NumSameEntries; j++) To[j] += From[j]; // Add to existing value
316 else
317 for (j=0; j<NumSameEntries; j++) To[j] = From[j];
318 }
319 // Do local permutation next
320 if (NumPermuteIDs>0) {
321
322 // Point entry case
323 if (Case1) {
324
325 if (CombineMode==Epetra_AddLocalAlso)
326 for (j=0; j<NumPermuteIDs; j++) To[PermuteToLIDs[j]] += From[PermuteFromLIDs[j]]; // Add to existing value
327 else for (j=0; j<NumPermuteIDs; j++) To[PermuteToLIDs[j]] = From[PermuteFromLIDs[j]];
328 }
329 // constant element size case
330 else if (Case2) {
331
332 if (CombineMode==Epetra_AddLocalAlso)
333 for (j=0; j<NumPermuteIDs; j++) {
334 jj = MaxElementSize*PermuteToLIDs[j];
335 jjj = MaxElementSize*PermuteFromLIDs[j];
336 for (k=0; k<MaxElementSize; k++)
337 To[jj+k] += From[jjj+k];
338 }
339 else
340 for (j=0; j<NumPermuteIDs; j++) {
341 jj = MaxElementSize*PermuteToLIDs[j];
342 jjj = MaxElementSize*PermuteFromLIDs[j];
343 for (k=0; k<MaxElementSize; k++)
344 To[jj+k] = From[jjj+k];
345 }
346 }
347
348 // variable element size case
349 else {
350
351 if (CombineMode==Epetra_AddLocalAlso)
352 for (j=0; j<NumPermuteIDs; j++) {
353 jj = ToFirstPointInElementList[PermuteToLIDs[j]];
354 jjj = FromFirstPointInElementList[PermuteFromLIDs[j]];
355 int ElementSize = FromElementSizeList[PermuteFromLIDs[j]];
356 for (k=0; k<ElementSize; k++)
357 To[jj+k] += From[jjj+k];
358 }
359 else
360 for (j=0; j<NumPermuteIDs; j++) {
361 jj = ToFirstPointInElementList[PermuteToLIDs[j]];
362 jjj = FromFirstPointInElementList[PermuteFromLIDs[j]];
363 int ElementSize = FromElementSizeList[PermuteFromLIDs[j]];
364 for (k=0; k<ElementSize; k++)
365 To[jj+k] = From[jjj+k];
366 }
367 }
368 }
369 return(0);
370}
371
372//=========================================================================
374 int NumExportIDs,
375 int * ExportLIDs,
376 int & LenExports,
377 char * & Exports,
378 int & SizeOfPacket,
379 int * Sizes,
380 bool & VarSizes,
381 Epetra_Distributor & Distor)
382{
383 (void)Sizes;
384 (void)VarSizes;
385 (void)Distor;
386 const Epetra_LongLongVector & A = dynamic_cast<const Epetra_LongLongVector &>(Source);
387
388 int j, jj, k;
389
390 long long * From;
391 A.ExtractView(&From);
392 int MaxElementSize = Map().MaxElementSize();
393 bool ConstantElementSize = Map().ConstantElementSize();
394
395 int * FromFirstPointInElementList = 0;
396 int * FromElementSizeList = 0;
397
398 if (!ConstantElementSize) {
399 FromFirstPointInElementList = A.Map().FirstPointInElementList();
400 FromElementSizeList = A.Map().ElementSizeList();
401 }
402
403 SizeOfPacket = MaxElementSize * (int)sizeof(long long);
404
405 if(NumExportIDs*SizeOfPacket>LenExports) {
406 if (LenExports>0) delete [] Exports;
407 LenExports = NumExportIDs*SizeOfPacket;
408 Exports = new char[LenExports];
409 }
410
411 long long * ptr;
412
413 if (NumExportIDs>0) {
414 ptr = (long long *) Exports;
415
416 // Point entry case
417 if (MaxElementSize==1) for (j=0; j<NumExportIDs; j++) *ptr++ = From[ExportLIDs[j]];
418
419 // constant element size case
420 else if (ConstantElementSize) {
421
422 for (j=0; j<NumExportIDs; j++) {
423 jj = MaxElementSize*ExportLIDs[j];
424 for (k=0; k<MaxElementSize; k++)
425 *ptr++ = From[jj+k];
426 }
427 }
428
429 // variable element size case
430 else {
431
432 int thisSizeOfPacket = MaxElementSize;
433 for (j=0; j<NumExportIDs; j++) {
434 ptr = (long long *) Exports + j*thisSizeOfPacket;
435 jj = FromFirstPointInElementList[ExportLIDs[j]];
436 int ElementSize = FromElementSizeList[ExportLIDs[j]];
437 for (k=0; k<ElementSize; k++)
438 *ptr++ = From[jj+k];
439 }
440 }
441 }
442
443 return(0);
444}
445
446//=========================================================================
448 int NumImportIDs,
449 int * ImportLIDs,
450 int LenImports,
451 char * Imports,
452 int & SizeOfPacket,
453 Epetra_Distributor & Distor,
454 Epetra_CombineMode CombineMode,
455 const Epetra_OffsetIndex * Indexor)
456{
457 (void)Source;
458 (void)LenImports;
459 (void)SizeOfPacket;
460 (void)Distor;
461 (void)Indexor;
462 int j, jj, k;
463
464 if( CombineMode != Add
465 && CombineMode != Zero
466 && CombineMode != Insert
467 && CombineMode != Average
468 && CombineMode != AbsMax )
469 EPETRA_CHK_ERR(-1); //Unsupported CombinedMode, will default to Zero
470
471 if (NumImportIDs<=0) return(0);
472
473 long long * To = Values_;
474 int MaxElementSize = Map().MaxElementSize();
475 bool ConstantElementSize = Map().ConstantElementSize();
476
477 int * ToFirstPointInElementList = 0;
478 int * ToElementSizeList = 0;
479
480 if (!ConstantElementSize) {
481 ToFirstPointInElementList = Map().FirstPointInElementList();
482 ToElementSizeList = Map().ElementSizeList();
483 }
484
485 long long * ptr;
486 // Unpack it...
487
488 ptr = (long long *) Imports;
489
490 // Point entry case
491 if (MaxElementSize==1) {
492
493 if (CombineMode==Add)
494 for (j=0; j<NumImportIDs; j++) To[ImportLIDs[j]] += *ptr++; // Add to existing value
495 else if(CombineMode==Insert)
496 for (j=0; j<NumImportIDs; j++) To[ImportLIDs[j]] = *ptr++;
497 else if(CombineMode==AbsMax)
498 for (j=0; j<NumImportIDs; j++) {
499 To[ImportLIDs[j]] = EPETRA_MAX( To[ImportLIDs[j]],(*ptr > 0 ? *ptr : -*ptr)); // CJ: No std::abs(long long) in VS2005
500 ptr++;
501 }
502 // Note: The following form of averaging is not a true average if more that one value is combined.
503 // This might be an issue in the future, but we leave this way for now.
504 else if(CombineMode==Average)
505 for (j=0; j<NumImportIDs; j++) {To[ImportLIDs[j]] += *ptr++; To[ImportLIDs[j]] /= 2;}
506 }
507
508 // constant element size case
509
510 else if (ConstantElementSize) {
511
512 if (CombineMode==Add) {
513 for (j=0; j<NumImportIDs; j++) {
514 jj = MaxElementSize*ImportLIDs[j];
515 for (k=0; k<MaxElementSize; k++)
516 To[jj+k] += *ptr++; // Add to existing value
517 }
518 }
519 else if(CombineMode==Insert) {
520 for (j=0; j<NumImportIDs; j++) {
521 jj = MaxElementSize*ImportLIDs[j];
522 for (k=0; k<MaxElementSize; k++)
523 To[jj+k] = *ptr++;
524 }
525 }
526 else if(CombineMode==AbsMax) {
527 for (j=0; j<NumImportIDs; j++) {
528 jj = MaxElementSize*ImportLIDs[j];
529 for (k=0; k<MaxElementSize; k++) {
530 To[jj+k] = EPETRA_MAX( To[jj+k], (*ptr > 0 ? *ptr : -*ptr)); // CJ: No std::abs(long long) in VS2005
531 ptr++;
532 }
533 }
534 }
535 // Note: The following form of averaging is not a true average if more that one value is combined.
536 // This might be an issue in the future, but we leave this way for now.
537 else if(CombineMode==Average) {
538 for (j=0; j<NumImportIDs; j++) {
539 jj = MaxElementSize*ImportLIDs[j];
540 for (k=0; k<MaxElementSize; k++)
541 { To[jj+k] += *ptr++; To[jj+k] /= 2;}
542 }
543 }
544 }
545
546 // variable element size case
547
548 else {
549
550 int thisSizeOfPacket = MaxElementSize;
551
552 if (CombineMode==Add) {
553 for (j=0; j<NumImportIDs; j++) {
554 ptr = (long long *) Imports + j*thisSizeOfPacket;
555 jj = ToFirstPointInElementList[ImportLIDs[j]];
556 int ElementSize = ToElementSizeList[ImportLIDs[j]];
557 for (k=0; k<ElementSize; k++)
558 To[jj+k] += *ptr++; // Add to existing value
559 }
560 }
561 else if(CombineMode==Insert){
562 for (j=0; j<NumImportIDs; j++) {
563 ptr = (long long *) Imports + j*thisSizeOfPacket;
564 jj = ToFirstPointInElementList[ImportLIDs[j]];
565 int ElementSize = ToElementSizeList[ImportLIDs[j]];
566 for (k=0; k<ElementSize; k++)
567 To[jj+k] = *ptr++;
568 }
569 }
570 else if(CombineMode==AbsMax){
571 for (j=0; j<NumImportIDs; j++) {
572 ptr = (long long *) Imports + j*thisSizeOfPacket;
573 jj = ToFirstPointInElementList[ImportLIDs[j]];
574 int ElementSize = ToElementSizeList[ImportLIDs[j]];
575 for (k=0; k<ElementSize; k++) {
576 To[jj+k] = EPETRA_MAX( To[jj+k], (*ptr > 0 ? *ptr : -*ptr)); // CJ: No std::abs(long long) in VS2005
577 ptr++;
578 }
579 }
580 }
581 // Note: The following form of averaging is not a true average if more that one value is combined.
582 // This might be an issue in the future, but we leave this way for now.
583 else if(CombineMode==Average) {
584 for (j=0; j<NumImportIDs; j++) {
585 ptr = (long long *) Imports + j*thisSizeOfPacket;
586 jj = ToFirstPointInElementList[ImportLIDs[j]];
587 int ElementSize = ToElementSizeList[ImportLIDs[j]];
588 for (k=0; k<ElementSize; k++)
589 { To[jj+k] += *ptr++; To[jj+k] /= 2;}
590 }
591 }
592 }
593
594 return(0);
595}
596
597#endif // EPETRA_NO_64BIT_GLOBAL_INDICES
Epetra_CombineMode
@ Insert
@ AbsMax
@ Average
@ Zero
@ Epetra_AddLocalAlso
#define EPETRA_MIN(x, y)
#define EPETRA_MAX(x, y)
#define EPETRA_CHK_ERR(a)
Epetra_DataAccess
@ Copy
Epetra_BlockMap: A class for partitioning block element vectors and matrices.
int MaxElementSize() const
Maximum element size across all processors.
long long * MyGlobalElements64() const
int * ElementSizeList() const
List of the element sizes corresponding to the array MyGlobalElements().
int ElementSize() const
Returns the size of elements in the map; only valid if map has constant element size.
int * FirstPointInElementList() const
Pointer to internal array containing a mapping between the local elements and the first local point n...
const Epetra_Comm & Comm() const
Access function for Epetra_Comm communicator.
bool ConstantElementSize() const
Returns true if map has constant element size.
bool GlobalIndicesLongLong() const
Returns true if map create with long long NumGlobalElements.
virtual int MaxAll(double *PartialMaxs, double *GlobalMaxs, int Count) const =0
Epetra_Comm Global Max function.
virtual int NumProc() const =0
Returns total number of processes.
virtual int MinAll(double *PartialMins, double *GlobalMins, int Count) const =0
Epetra_Comm Global Min function.
virtual int MyPID() const =0
Return my process ID.
virtual void Barrier() const =0
Epetra_Comm Barrier function.
Epetra_DistObject: A class for constructing and using dense multi-vectors, vectors and matrices in pa...
const Epetra_BlockMap & Map() const
Returns the address of the Epetra_BlockMap for this multi-vector.
const Epetra_Comm & Comm() const
Returns the address of the Epetra_Comm for this multi-vector.
Epetra_Distributor: The Epetra Gather/Scatter Setup Base Class.
Epetra_LongLongVector: A class for constructing and using dense integer vectors on a parallel compute...
virtual void Print(std::ostream &os) const
Print method.
int CopyAndPermute(const Epetra_SrcDistObject &Source, int NumSameIDs, int NumPermuteIDs, int *PermuteToLIDs, int *PermuteFromLIDs, const Epetra_OffsetIndex *Indexor, Epetra_CombineMode CombineMode=Zero)
Perform ID copies and permutations that are on processor.
int PutValue(long long Value)
Set all elements of the vector to Value.
int MyLength() const
Returns the local vector length on the calling processor of vectors in the multi-vector.
int ExtractView(long long **V) const
Set user-provided address of V.
long long MinValue()
Find minimum value.
int UnpackAndCombine(const Epetra_SrcDistObject &Source, int NumImportIDs, int *ImportLIDs, int LenImports, char *Imports, int &SizeOfPacket, Epetra_Distributor &Distor, Epetra_CombineMode CombineMode, const Epetra_OffsetIndex *Indexor)
Perform any unpacking and combining after call to DoTransfer().
Epetra_LongLongVector(const Epetra_BlockMap &Map, bool zeroOut=true)
Basic Epetra_LongLongVector constuctor.
int PackAndPrepare(const Epetra_SrcDistObject &Source, int NumExportIDs, int *ExportLIDs, int &LenExports, char *&Exports, int &SizeOfPacket, int *Sizes, bool &VarSizes, Epetra_Distributor &Distor)
Perform any packing or preparation required for call to DoTransfer().
long long MaxValue()
Find maximum value.
Epetra_LongLongVector & operator=(const Epetra_LongLongVector &Source)
= Operator.
int ExtractCopy(long long *V) const
Put vector values into user-provided array.
virtual ~Epetra_LongLongVector()
Epetra_LongLongVector destructor.
int CheckSizes(const Epetra_SrcDistObject &A)
Allows the source and target (this) objects to be compared for compatibility, return nonzero if not.
virtual int ReportError(const std::string Message, int ErrorCode) const
Error reporting method.
std::string toString(const int &x) const
Epetra_OffsetIndex: This class builds index for efficient mapping of data from one Epetra_CrsGraph ba...
Epetra_SrcDistObject: A class for supporting flexible source distributed objects for import/export op...