MFEM v4.9.0
Finite element discretization library
Loading...
Searching...
No Matches
array.cpp
Go to the documentation of this file.
1// Copyright (c) 2010-2025, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12
13// Abstract array data type
14
15#include "array.hpp"
16#include "../general/forall.hpp"
17#include <fstream>
18#include <type_traits>
19
20namespace mfem
21{
22
23template <class T>
24void Array<T>::Print(std::ostream &os, int width) const
25{
26 for (int i = 0; i < size; i++)
27 {
28 os << data[i];
29 if ( !((i+1) % width) || i+1 == size )
30 {
31 os << '\n';
32 }
33 else
34 {
35 os << " ";
36 }
37 }
38}
39
40template <class T>
41void Array<T>::Save(std::ostream &os, int fmt) const
42{
43 if (fmt == 0)
44 {
45 os << size << '\n';
46 }
47 for (int i = 0; i < size; i++)
48 {
49 os << operator[](i) << '\n';
50 }
51}
52
53template <class T>
54void Array<T>::Load(std::istream &in, int fmt)
55{
56 if (fmt == 0)
57 {
58 int new_size;
59 in >> new_size;
60 SetSize(new_size);
61 }
62 for (int i = 0; i < size; i++)
63 {
64 in >> operator[](i);
65 }
66}
67
68template <class T>
70{
71 MFEM_ASSERT(size > 0, "Array is empty with size " << size);
72
73 T max = operator[](0);
74 for (int i = 1; i < size; i++)
75 {
76 if (max < operator[](i))
77 {
78 max = operator[](i);
79 }
80 }
81
82 return max;
83}
84
85template <class T>
87{
88 MFEM_ASSERT(size > 0, "Array is empty with size " << size);
89
90 T min = operator[](0);
91 for (int i = 1; i < size; i++)
92 {
93 if (operator[](i) < min)
94 {
95 min = operator[](i);
96 }
97 }
98
99 return min;
100}
101
102// Partial Sum
103template <class T>
105{
106 T sum = static_cast<T>(0);
107 for (int i = 0; i < size; i++)
108 {
109 sum+=operator[](i);
110 operator[](i) = sum;
111 }
112}
113
114template <class T>
116{
117 static_assert(std::is_arithmetic<T>::value, "Use with arithmetic types!");
118 const bool useDevice = UseDevice();
119 const int N = size;
120 auto y = ReadWrite(useDevice);
121 mfem::forall_switch(useDevice, N, [=] MFEM_HOST_DEVICE (int i)
122 {
123 y[i] = std::abs(y[i]);
124 });
125}
126
127// Sum
128template <class T>
130{
131 T sum = static_cast<T>(0);
132 for (int i = 0; i < size; i++)
133 {
134 sum+=operator[](i);
135 }
136
137 return sum;
138}
139
140template <class T>
142{
143 T val_prev = operator[](0), val;
144 for (int i = 1; i < size; i++)
145 {
146 val=operator[](i);
147 if (val < val_prev)
148 {
149 return 0;
150 }
151 val_prev = val;
152 }
153
154 return 1;
155}
156
157template <class T>
159{
160 if (size < 2) { return true; }
161 const T v0 = data[0];
162 for (int i = 1; i < size; i++)
163 {
164 if (data[i] != v0)
165 {
166 return false;
167 }
168 }
169
170 return true;
171}
172
173template <class T>
174void Array2D<T>::Load(const char *filename, int fmt)
175{
176 std::ifstream in;
177 in.open(filename, std::ifstream::in);
178 MFEM_VERIFY(in.is_open(), "File " << filename << " does not exist.");
179 Load(in, fmt);
180 in.close();
181}
182
183template <class T>
184void Array2D<T>::Print(std::ostream &os, int width_)
185{
186 int height = this->NumRows();
187 int width = this->NumCols();
188
189 for (int i = 0; i < height; i++)
190 {
191 os << "[row " << i << "]\n";
192 for (int j = 0; j < width; j++)
193 {
194 os << (*this)(i,j);
195 if ( (j+1) == width_ || (j+1) % width_ == 0 )
196 {
197 os << '\n';
198 }
199 else
200 {
201 os << ' ';
202 }
203 }
204 }
205}
206
207template class Array<char>;
208template class Array<int>;
209template class Array<long long>;
210template class Array<real_t>;
211template class Array2D<int>;
212template class Array2D<real_t>;
213
214} // namespace mfem
Dynamic 2D array using row-major layout.
Definition array.hpp:430
void Load(std::istream &in, int fmt=0)
Read an Array2D from the stream in using format fmt.
Definition array.hpp:487
void Print(std::ostream &out=mfem::out, int width=4)
Prints array to stream with width elements per row.
Definition array.cpp:184
T Max() const
Find the maximal element in the array, using the comparison operator < for class T.
Definition array.cpp:69
void Load(std::istream &in, int fmt=0)
Read an Array from the stream in using format fmt. The format fmt can be:
Definition array.cpp:54
bool IsConstant() const
Return true if all entries of the array are the same.
Definition array.cpp:158
T Min() const
Find the minimal element in the array, using the comparison operator < for class T.
Definition array.cpp:86
void PartialSum()
Fill the entries of the array with the cumulative sum of the entries.
Definition array.cpp:104
int IsSorted() const
Return 1 if the array is sorted from lowest to highest. Otherwise return 0.
Definition array.cpp:141
void Save(std::ostream &out, int fmt=0) const
Save the Array to the stream out using the format fmt. The format fmt can be:
Definition array.cpp:41
void Abs()
Replace each entry of the array with its absolute value.
Definition array.cpp:115
void Print(std::ostream &out=mfem::out, int width=4) const
Prints array to stream with width elements per row.
Definition array.cpp:24
T Sum() const
Return the sum of all the array entries using the '+'' operator for class 'T'.
Definition array.cpp:129
T * ReadWrite(Memory< T > &mem, int size, bool on_dev=true)
Get a pointer for read+write access to mem with the mfem::Device's DeviceMemoryClass,...
Definition device.hpp:382
void forall_switch(bool use_dev, int N, lambda &&body)
Definition forall.hpp:919