MFEM v4.10.0
Finite element discretization library
Loading...
Searching...
No Matches
array.cpp
Go to the documentation of this file.
1// Copyright (c) 2010-2026, 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 <typename U>
115MFEM_HOST_DEVICE inline U abs_signed(U v) { return (v < U(0)) ? -v : v; }
116
117template <typename U>
118void AbsImpl(std::true_type /*signed*/, U* y, int N, bool useDevice)
119{
120 mfem::forall_switch(useDevice, N, [=] MFEM_HOST_DEVICE (int i)
121 {
122 y[i] = abs_signed(y[i]);
123 });
124}
125
126template <typename U>
127void AbsImpl(std::false_type /*unsigned*/, U* /*y*/, int /*N*/,
128 bool /*useDevice*/)
129{
130 // no-op
131}
132
133template <class T>
135{
136 static_assert(std::is_arithmetic<T>::value, "Use with arithmetic types!");
137 const bool useDevice = UseDevice();
138 const int N = size;
139 auto y = ReadWrite(useDevice);
140 AbsImpl<T>(std::is_signed<T> {}, y, N, useDevice);
141}
142
143// Sum
144template <class T>
146{
147 T sum = static_cast<T>(0);
148 for (int i = 0; i < size; i++)
149 {
150 sum+=operator[](i);
151 }
152
153 return sum;
154}
155
156template <class T>
158{
159 T val_prev = operator[](0), val;
160 for (int i = 1; i < size; i++)
161 {
162 val=operator[](i);
163 if (val < val_prev)
164 {
165 return 0;
166 }
167 val_prev = val;
168 }
169
170 return 1;
171}
172
173template <class T>
175{
176 if (size < 2) { return true; }
177 const T v0 = data[0];
178 for (int i = 1; i < size; i++)
179 {
180 if (data[i] != v0)
181 {
182 return false;
183 }
184 }
185
186 return true;
187}
188
189template <class T>
190void Array2D<T>::Load(const char *filename, int fmt)
191{
192 std::ifstream in;
193 in.open(filename, std::ifstream::in);
194 MFEM_VERIFY(in.is_open(), "File " << filename << " does not exist.");
195 Load(in, fmt);
196 in.close();
197}
198
199template <class T>
200void Array2D<T>::Print(std::ostream &os, int width_)
201{
202 int height = this->NumRows();
203 int width = this->NumCols();
204
205 for (int i = 0; i < height; i++)
206 {
207 os << "[row " << i << "]\n";
208 for (int j = 0; j < width; j++)
209 {
210 os << (*this)(i,j);
211 if ( (j+1) == width_ || (j+1) % width_ == 0 )
212 {
213 os << '\n';
214 }
215 else
216 {
217 os << ' ';
218 }
219 }
220 }
221}
222
223template class Array<char>;
224template class Array<int>;
225template class Array<long long>;
226template class Array<unsigned int>;
227template class Array<real_t>;
228template class Array2D<int>;
229template class Array2D<real_t>;
230
231} // namespace mfem
Dynamic 2D array using row-major layout.
Definition array.hpp:459
void Load(std::istream &in, int fmt=0)
Read an Array2D from the stream in using format fmt.
Definition array.hpp:516
void Print(std::ostream &out=mfem::out, int width=4)
Prints array to stream with width elements per row.
Definition array.cpp:200
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:174
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:157
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:134
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:145
MFEM_HOST_DEVICE U abs_signed(U v)
Definition array.cpp:115
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:403
void AbsImpl(std::true_type, U *y, int N, bool useDevice)
Definition array.cpp:118
void forall_switch(bool use_dev, int N, lambda &&body)
Definition forall.hpp:1214