1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
// Copyright (C) 2021 - 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "../../shared/gpubuf.h"
#include "../../shared/rocfft_params.h"
#include "../samples/rocfft/examplekernels.h"
#include "../samples/rocfft/exampleutils.h"
#include "accuracy_test.h"
#include "rocfft/rocfft.h"
#include <functional>
#include <gtest/gtest.h>
#include <hip/hip_runtime_api.h>
#include <memory>
#include <random>
#include <thread>
#include <vector>
void run_1D_hermitian_test(size_t length)
{
// Run two 1D C2R transforms, on:
// * random input
// * identical random input, but modified to be Hermitian-symmetric
// We should tolerate the input being having non-zero imaginary part in the DC mode
// and the Nyquist frequency (of the length is even).
rocfft_params p;
p.length = {length};
p.precision = fft_precision_double;
p.transform_type = fft_transform_type_real_inverse;
p.placement = fft_placement_notinplace;
p.validate();
if(verbose)
{
std::cout << p.str("\n\t") << std::endl;
}
ASSERT_TRUE(p.valid(verbose));
std::vector<hipDoubleComplex> h_input(p.isize[0]);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<double> dis(0.0, 1.0);
for(auto& val : h_input)
{
val.x = dis(gen);
val.y = dis(gen);
}
if(verbose > 2)
{
std::cout << "non-Hermitian input:";
for(const auto& val : h_input)
{
std::cout << " "
<< "(" << val.x << ", " << val.y << ")";
}
std::cout << std::endl;
}
gpubuf ibuf;
ASSERT_TRUE(ibuf.alloc(p.ibuffer_sizes()[0]) == hipSuccess);
ASSERT_TRUE(hipMemcpy(ibuf.data(), h_input.data(), ibuf.size(), hipMemcpyHostToDevice)
== hipSuccess);
gpubuf obuf;
ASSERT_TRUE(obuf.alloc(p.obuffer_sizes()[0]) == hipSuccess);
ASSERT_TRUE(p.create_plan() == fft_status_success);
std::vector<void*> pibuf = {ibuf.data()};
std::vector<void*> pobuf = {obuf.data()};
ASSERT_TRUE(p.execute(pibuf.data(), pobuf.data()) == fft_status_success);
std::vector<double> h_output(p.osize[0]);
ASSERT_TRUE(hipMemcpy(h_output.data(), obuf.data(), obuf.size(), hipMemcpyDeviceToHost)
== hipSuccess);
ASSERT_TRUE(hipDeviceSynchronize() == hipSuccess);
if(verbose > 2)
{
std::cout << "output:";
for(const auto& val : h_output)
{
std::cout << " " << val;
}
std::cout << std::endl;
}
std::vector<hipDoubleComplex> h_input1(p.isize[0]);
std::copy(h_input.begin(), h_input.end(), h_input1.begin());
// Impose Hermitian symmetry on the input:
h_input1[0].y = 0.0;
if(p.length[0] % 2 == 0)
{
h_input1.back().y = 0.0;
}
if(verbose > 2)
{
std::cout << "Hermitian input:";
for(const auto& val : h_input1)
{
std::cout << " "
<< "(" << val.x << ", " << val.y << ")";
}
std::cout << std::endl;
}
double maxdiff = 0.0;
for(unsigned int i = 0; i < h_input.size(); ++i)
{
auto val = std::abs(
rocfft_complex<double>(h_input[i].x - h_input1[i].x, h_input[i].y - h_input1[i].y));
if(val > maxdiff)
maxdiff = val;
}
ASSERT_TRUE(maxdiff > 0.0);
ASSERT_TRUE(hipMemcpy(ibuf.data(), h_input1.data(), ibuf.size(), hipMemcpyHostToDevice)
== hipSuccess);
ASSERT_TRUE(p.execute(pibuf.data(), pobuf.data()) == fft_status_success);
std::vector<double> h_output1(p.osize[0]);
ASSERT_TRUE(hipMemcpy(h_output1.data(), obuf.data(), obuf.size(), hipMemcpyDeviceToHost)
== hipSuccess);
if(verbose > 2)
{
std::cout << "output:";
for(const auto& val : h_output1)
{
std::cout << " " << val;
}
std::cout << std::endl;
}
double maxerr = 0;
for(unsigned int i = 0; i < h_output.size(); ++i)
{
auto val = std::abs(h_output[i] - h_output1[i]);
if(val > maxerr)
maxerr = val;
}
if(verbose)
std::cout << maxerr << std::endl;
EXPECT_TRUE(maxerr == 0.0);
}
// test a case that's small enough that it only needs one kernel
TEST(rocfft_UnitTest, 1D_hermitian_single_small)
{
run_1D_hermitian_test(8);
}
// test a case that's big enough that it needs multiple kernels
TEST(rocfft_UnitTest, 1D_hermitian_single_large)
{
run_1D_hermitian_test(8192);
}
template <typename T>
std::string str(T begin, T end)
{
std::stringstream ss;
bool first = true;
for(; begin != end; begin++)
{
if(!first)
ss << ", ";
ss << *begin;
first = false;
}
return ss.str();
}
// Test that the GPU Hermitian symmetrizer code produces the correct results.
TEST(rocfft_UnitTest, gpu_symmetrizer)
{
std::vector<std::vector<size_t>> lengths = {{4, 4, 3},
{5},
{8},
{5, 5},
{5, 8},
{8, 5},
{8, 8},
{5, 5, 5},
{8, 5, 5},
{5, 8, 5},
{5, 5, 8},
{5, 8, 8},
{8, 5, 8},
{8, 8, 5},
{8, 8, 8}};
for(const auto& length : lengths)
{
// Symmetrize complex data and ensure that the checker sees that it's symmetric.
// Use the params class to set up strides and lengths:
rocfft_params p;
p.length = length;
p.precision = fft_precision_double;
p.transform_type = fft_transform_type_real_inverse;
p.placement = fft_placement_notinplace;
p.validate();
if(verbose)
{
std::cout << "\t" << p.str("\n\t") << std::endl;
}
ASSERT_TRUE(p.valid(verbose));
// Data buffers:
gpubuf buf;
ASSERT_TRUE(buf.alloc(sizeof(hipDoubleComplex) * p.isize[0]) == hipSuccess);
std::vector<hipDoubleComplex> hbuf(p.isize[0]);
// Initialize a Hermitian-symmetric array; it should be symmetric.
init_hermitiancomplex_cm(p.length_cm(), p.ilength_cm(), p.istride_cm(), buf.data());
ASSERT_TRUE(hipMemcpy(hbuf.data(), buf.data(), buf.size(), hipMemcpyDeviceToHost)
== hipSuccess);
if(verbose > 1)
{
printbuffer_cm(hbuf, p.ilength_cm(), p.istride_cm(), p.nbatch, p.idist);
}
EXPECT_TRUE(
check_symmetry_cm(hbuf, p.length_cm(), p.istride_cm(), p.nbatch, p.idist, verbose > 0))
<< "length: " << str(length.begin(), length.end());
// This should not be symmetric:
std::mt19937_64 rng;
std::seed_seq ss{uint32_t(10)};
rng.seed(ss);
std::uniform_real_distribution<double> unif(0, 1);
for(auto& v : hbuf)
{
v.x = unif(rng);
v.y = unif(rng);
}
if(verbose > 2)
{
printbuffer_cm(hbuf, p.ilength_cm(), p.istride_cm(), p.nbatch, p.idist);
}
EXPECT_TRUE(
!check_symmetry_cm(hbuf, p.length_cm(), p.istride_cm(), p.nbatch, p.idist, false))
<< "length: " << str(length.begin(), length.end());
}
for(const auto& length : lengths)
{
// Generate Hermitian-symmetric data and ensure that applying the symmetrizer has no effect.
rocfft_params p;
p.length = length;
p.precision = fft_precision_double;
p.transform_type = fft_transform_type_real_forward;
p.placement = fft_placement_notinplace;
p.validate();
if(verbose)
{
std::cout << "\t" << p.str("\n\t") << std::endl;
}
ASSERT_TRUE(p.valid(verbose));
ASSERT_TRUE(p.create_plan() == fft_status_success);
gpubuf ibuf, obuf;
ASSERT_TRUE(ibuf.alloc(p.ibuffer_sizes()[0]) == hipSuccess);
ASSERT_TRUE(obuf.alloc(p.obuffer_sizes()[0]) == hipSuccess);
initreal_cm(p.length_cm(), p.istride_cm(), ibuf.data());
std::vector<void*> pibuf = {ibuf.data()};
std::vector<void*> pobuf = {obuf.data()};
ASSERT_TRUE(p.execute(pibuf.data(), pobuf.data()) == fft_status_success);
std::vector<hipDoubleComplex> h_output(p.osize[0]);
std::fill(h_output.begin(), h_output.end(), hipDoubleComplex{0.0, 0.0});
ASSERT_TRUE(
hipMemcpy(h_output.data(), obuf.data(), p.obuffer_sizes()[0], hipMemcpyDeviceToHost)
== hipSuccess);
impose_hermitian_symmetry_cm(p.length_cm(), p.olength_cm(), p.ostride_cm(), obuf.data());
std::vector<hipDoubleComplex> h_output_resym(p.osize[0]);
std::fill(h_output_resym.begin(), h_output_resym.end(), hipDoubleComplex{0.0, 0.0});
ASSERT_TRUE(
hipMemcpy(
h_output_resym.data(), obuf.data(), p.obuffer_sizes()[0], hipMemcpyDeviceToHost)
== hipSuccess);
double maxdiff = 0;
for(unsigned int i = 0; i < h_output.size(); ++i)
{
auto rdiff = std::abs(h_output[i].x - h_output_resym[i].x);
auto idiff = std::abs(h_output[i].y - h_output_resym[i].y);
maxdiff = std::max({maxdiff, rdiff, idiff});
}
if(verbose)
{
std::cout << "maxdiff: " << maxdiff << std::endl;
}
if(verbose > 2)
{
std::cout << "before symmetrization:\n";
printbuffer_cm(h_output, p.olength_cm(), p.ostride_cm(), p.nbatch, p.odist);
std::cout << "after symmetrization:\n";
printbuffer_cm(h_output_resym, p.olength_cm(), p.ostride_cm(), p.nbatch, p.odist);
}
EXPECT_TRUE(maxdiff < 1e-13) << maxdiff << "\n" << p.str() << "\n";
}
}
|