Nektar++
Loading...
Searching...
No Matches
TestDeviceFFT.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestDeviceFFT.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include <cmath>
36
37#include <stdexcept>
38
39#include <boost/test/unit_test.hpp>
40
45
47{
48
49using namespace Nektar::LibUtilities;
50
51static constexpr double TOL = 1.0e-12;
52
53#if !defined(NEKTAR_ENABLE_SYCL)
54// Graph capture needs a non-default stream: stream 0 is the legacy default
55// stream, which cannot be captured.
56static constexpr unsigned int GRAPH_STREAM_ID = 1;
57#endif // !NEKTAR_ENABLE_SYCL
58
59// Signal helpers
60
61static void FillSine(Array<OneD, double> &phys, int N, int k)
62{
63 const double omega = 2.0 * M_PI * k / N;
64 for (int n = 0; n < N; ++n)
65 phys[n] = std::sin(omega * n);
66}
67
68static void FillCosine(Array<OneD, double> &phys, int N, int k)
69{
70 const double omega = 2.0 * M_PI * k / N;
71 for (int n = 0; n < N; ++n)
72 phys[n] = std::cos(omega * n);
73}
74
75static void FillSignal(Array<OneD, double> &arr, int N)
76{
77 const double omega = 2.0 * M_PI / N;
78 for (int i = 0; i < static_cast<int>(arr.size()); ++i)
79 {
80 const int n = i % N;
81 arr[i] =
82 std::sin(3.0 * omega * n) + 0.5 * std::cos(7.0 * omega * n) + 1.0;
83 }
84}
85
86static void CheckOtherModesZero(const Array<OneD, double> &coef, int N,
87 int skipReal, int skipImag)
88{
89 for (int i = 0; i < N; ++i)
90 {
91 if (i == skipReal || i == skipImag)
92 {
93 continue;
94 }
95 BOOST_CHECK_SMALL(coef[i], TOL);
96 }
97}
98
99// Correctness tests
100
101BOOST_AUTO_TEST_CASE(TestFactoryRegistration)
102{
103 BOOST_CHECK_NO_THROW(
104 GetNektarFFTFactory().CreateInstance("NekDeviceFFT", 16));
105}
106
108{
109 const int N = 16;
111 Array<OneD, double> phys(N, 3.5), coef(N, 0.0);
112 fft->FFTFwdTrans(phys, coef);
113 BOOST_CHECK_SMALL(coef[0] - 3.5, TOL);
114 CheckOtherModesZero(coef, N, 0, -1);
115}
116
117BOOST_AUTO_TEST_CASE(TestSineCosineMode)
118{
119 for (const int k : {1, 3})
120 {
121 const int N = 16;
123 Array<OneD, double> phys(N, 0.0), coef(N, 0.0);
124 FillSine(phys, N, k);
125 fft->FFTFwdTrans(phys, coef);
126 BOOST_CHECK_SMALL(coef[2 * k], TOL);
127 BOOST_CHECK_SMALL(coef[2 * k + 1] + 1.0, TOL);
128 CheckOtherModesZero(coef, N, 2 * k, 2 * k + 1);
129 }
130 {
131 const int N = 16, k = 1;
133 Array<OneD, double> phys(N, 0.0), coef(N, 0.0);
134 FillCosine(phys, N, k);
135 fft->FFTFwdTrans(phys, coef);
136 BOOST_CHECK_SMALL(coef[2 * k] - 1.0, TOL);
137 BOOST_CHECK_SMALL(coef[2 * k + 1], TOL);
138 CheckOtherModesZero(coef, N, 2 * k, 2 * k + 1);
139 }
140}
141
143{
144 for (const int N : {16, 64, 256})
145 {
147 Array<OneD, double> phys(N, 0.0), coef(N, 0.0), recv(N, 0.0);
148 FillSignal(phys, N);
149 fft->FFTFwdTrans(phys, coef);
150 fft->FFTBwdTrans(coef, recv);
151 for (int n = 0; n < N; ++n)
152 BOOST_CHECK_SMALL(recv[n] - phys[n], TOL);
153 }
154}
155
156BOOST_AUTO_TEST_CASE(TestRoundTripDeviceResident)
157{
158 const int N = 64;
160 Array<OneD, double> phys(N, 0.0), recv(N, 0.0);
161 FillSignal(phys, N);
162 fft->UploadPhys(phys.data());
163 fft->FFTFwdTransDevice();
164 fft->FFTBwdTransDevice();
165 fft->DownloadPhys(recv.data());
166 for (int n = 0; n < N; ++n)
167 BOOST_CHECK_SMALL(recv[n] - phys[n], TOL);
168}
169
170BOOST_AUTO_TEST_CASE(TestBatchedRoundTrip)
171{
172 for (const int M : {2, 8, 32})
173 {
174 const int N = 64;
176 Array<OneD, double> phys(M * N, 0.0), coef(M * N, 0.0),
177 recv(M * N, 0.0);
178 FillSignal(phys, N);
179 fft->FFTFwdTrans(phys, coef);
180 fft->FFTBwdTrans(coef, recv);
181 for (int i = 0; i < M * N; ++i)
182 BOOST_CHECK_SMALL(recv[i] - phys[i], TOL);
183 }
184}
185
186BOOST_AUTO_TEST_CASE(TestPlanCacheIsPopulated)
187{
188 const int N = 64, M = 2;
189 {
191 }
192 int deviceId = Nektar::nekGetDevice();
193 BOOST_CHECK(NekDeviceFFT::IsCacheWarmed(deviceId, N, M));
194}
195
196// Graph capture is a CUDA and HIP facility. The SYCL backend has no
197// equivalent and resubmits the pipeline instead, so the round-trip cases below
198// are replaced there by one that pins that behaviour.
199#if defined(NEKTAR_ENABLE_SYCL)
200
201BOOST_AUTO_TEST_CASE(TestGraphCaptureUnsupported)
202{
204 BOOST_CHECK(!fft->HasGraph());
205 BOOST_CHECK_THROW(fft->BeginGraphCapture(), std::runtime_error);
206 BOOST_CHECK(!fft->HasGraph());
207}
208
209#else
210
211BOOST_AUTO_TEST_CASE(TestGraphCaptureDefaultStream)
212{
214 BOOST_CHECK_THROW(fft->BeginGraphCapture(), std::runtime_error);
215 BOOST_CHECK(!fft->HasGraph());
216}
217
218BOOST_AUTO_TEST_CASE(TestGraphRoundTrip)
219{
220 for (const int N : {16, 64, 256})
221 {
223 N, 1, GRAPH_STREAM_ID);
224 Array<OneD, double> phys(N, 0.0), ref(N, 0.0), recv(N, 0.0);
225 FillSignal(phys, N);
226 fft->UploadPhys(phys.data());
227 fft->FFTFwdTransDevice();
228 fft->FFTBwdTransDevice();
229 fft->DownloadPhys(ref.data());
230 fft->BeginGraphCapture();
231 fft->FFTFwdTransDevice();
232 fft->FFTBwdTransDevice();
233 fft->EndGraphCapture();
234 for (int rep = 0; rep < 3; ++rep)
235 {
236 fft->UploadPhys(phys.data());
237 fft->LaunchGraph();
238 fft->DownloadPhys(recv.data());
239 for (int n = 0; n < N; ++n)
240 BOOST_CHECK_SMALL(recv[n] - ref[n], TOL);
241 }
242 }
243}
244
245#endif // NEKTAR_ENABLE_SYCL
246
247// LaunchGraph() without a capture raises the same error on every backend; on
248// SYCL that is the only outcome, there being no capture to make.
249BOOST_AUTO_TEST_CASE(TestLaunchGraphWithoutCapture)
250{
252 BOOST_CHECK_THROW(fft->LaunchGraph(), std::runtime_error);
253}
254
255// Float-precision tests
256
257BOOST_AUTO_TEST_CASE(TestFloatFactoryRegistration)
258{
259 BOOST_CHECK_NO_THROW(
260 GetNektarFFTFloatFactory().CreateInstance("NekDeviceFFT", 16));
261}
262
263BOOST_AUTO_TEST_CASE(TestFloatDCSignal)
264{
265 const int N = 16;
267 Array<OneD, float> phys(N, 3.5f), coef(N, 0.0f);
268 fft->FFTFwdTrans(phys, coef);
269 BOOST_CHECK_SMALL(coef[0] - 3.5f, 1.0e-5f);
270 for (int i = 1; i < N; ++i)
271 BOOST_CHECK_SMALL(coef[i], 1.0e-5f);
272}
273
274BOOST_AUTO_TEST_CASE(TestFloatRoundTrip)
275{
276 for (const int N : {16, 64, 256})
277 {
279 Array<OneD, float> phys(N, 0.0f), coef(N, 0.0f), recv(N, 0.0f);
280 const float omega = 2.0f * static_cast<float>(M_PI) / N;
281 for (int i = 0; i < N; ++i)
282 phys[i] = std::sin(3.0f * omega * i) +
283 0.5f * std::cos(7.0f * omega * i) + 1.0f;
284 fft->FFTFwdTrans(phys, coef);
285 fft->FFTBwdTrans(coef, recv);
286 for (int n = 0; n < N; ++n)
287 BOOST_CHECK_SMALL(recv[n] - phys[n], 1.0e-5f);
288 }
289}
290
291BOOST_AUTO_TEST_CASE(TestFloatBatchedRoundTrip)
292{
293 const int N = 64, M = 8;
295 Array<OneD, float> phys(M * N, 0.0f), coef(M * N, 0.0f), recv(M * N, 0.0f);
296 const float omega = 2.0f * static_cast<float>(M_PI) / N;
297 for (int i = 0; i < M * N; ++i)
298 {
299 const int n = i % N;
300 phys[i] = std::sin(3.0f * omega * n) +
301 0.5f * std::cos(7.0f * omega * n) + 1.0f;
302 }
303 fft->FFTFwdTrans(phys, coef);
304 fft->FFTBwdTrans(coef, recv);
305 for (int i = 0; i < M * N; ++i)
306 BOOST_CHECK_SMALL(recv[i] - phys[i], 1.0e-5f);
307}
308
309BOOST_AUTO_TEST_CASE(TestFloatSineCosineMode)
310{
311 const int N = 16, k = 3;
313 Array<OneD, float> phys(N, 0.0f), coef(N, 0.0f);
314 const float omega = 2.0f * static_cast<float>(M_PI) * k / N;
315 for (int n = 0; n < N; ++n)
316 phys[n] = std::sin(omega * n);
317 fft->FFTFwdTrans(phys, coef);
318 BOOST_CHECK_SMALL(coef[2 * k], 1.0e-5f);
319 BOOST_CHECK_SMALL(coef[2 * k + 1] + 1.0f, 1.0e-5f);
320 for (int i = 0; i < N; ++i)
321 if (i != 2 * k && i != 2 * k + 1)
322 BOOST_CHECK_SMALL(coef[i], 1.0e-5f);
323}
324
325BOOST_AUTO_TEST_CASE(TestFloatRoundTripDeviceResident)
326{
327 const int N = 64;
329 Array<OneD, float> phys(N, 0.0f), recv(N, 0.0f);
330 const float omega = 2.0f * static_cast<float>(M_PI) / N;
331 for (int i = 0; i < N; ++i)
332 phys[i] = std::sin(3.0f * omega * i) + 1.0f;
333 fft->UploadPhys(phys.data());
334 fft->FFTFwdTransDevice();
335 fft->FFTBwdTransDevice();
336 fft->DownloadPhys(recv.data());
337 for (int n = 0; n < N; ++n)
338 BOOST_CHECK_SMALL(recv[n] - phys[n], 1.0e-5f);
339}
340
341#if !defined(NEKTAR_ENABLE_SYCL)
342
343BOOST_AUTO_TEST_CASE(TestFloatGraphRoundTrip)
344{
345 const int N = 64;
347 N, 1, GRAPH_STREAM_ID);
348 Array<OneD, float> phys(N, 0.0f), ref(N, 0.0f), recv(N, 0.0f);
349 const float omega = 2.0f * static_cast<float>(M_PI) / N;
350 for (int i = 0; i < N; ++i)
351 phys[i] = std::sin(3.0f * omega * i) + 1.0f;
352 fft->UploadPhys(phys.data());
353 fft->FFTFwdTransDevice();
354 fft->FFTBwdTransDevice();
355 fft->DownloadPhys(ref.data());
356 fft->BeginGraphCapture();
357 fft->FFTFwdTransDevice();
358 fft->FFTBwdTransDevice();
359 fft->EndGraphCapture();
360 for (int rep = 0; rep < 3; ++rep)
361 {
362 fft->UploadPhys(phys.data());
363 fft->LaunchGraph();
364 fft->DownloadPhys(recv.data());
365 for (int n = 0; n < N; ++n)
366 BOOST_CHECK_SMALL(recv[n] - ref[n], 1.0e-5f);
367 }
368}
369
370#endif // !NEKTAR_ENABLE_SYCL
371
372} // namespace Nektar::DeviceFFTUnitTests
#define N
static bool IsCacheWarmed(int deviceId, int N, int M)
Return true if the plan cache has been warmed up for the given device, transform size,...
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
static constexpr double TOL
static constexpr unsigned int GRAPH_STREAM_ID
static void FillSignal(Array< OneD, double > &arr, int N)
BOOST_AUTO_TEST_CASE(TestFactoryRegistration)
static void FillCosine(Array< OneD, double > &phys, int N, int k)
static void FillSine(Array< OneD, double > &phys, int N, int k)
static void CheckOtherModesZero(const Array< OneD, double > &coef, int N, int skipReal, int skipImag)
NektarFFTFactory & GetNektarFFTFactory()
Definition NektarFFT.cpp:40
NektarFFTFloatFactory & GetNektarFFTFloatFactory()
Definition NektarFFT.cpp:46
static unsigned int nekGetDevice()
Definition Backends.hpp:380