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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
|
// Copyright (c) 2018 - 2022 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 "hipfft/hipfft.h"
#include <fftw3.h>
#include <gtest/gtest.h>
#include <hip/hip_vector_types.h>
#include <vector>
#include "../hipfft_params.h"
DISABLE_WARNING_PUSH
DISABLE_WARNING_DEPRECATED_DECLARATIONS
DISABLE_WARNING_RETURN_TYPE
#include <hip/hip_runtime_api.h>
DISABLE_WARNING_POP
// Function to return maximum error for float and double types.
template <typename Tfloat>
inline double type_epsilon();
template <>
inline double type_epsilon<float>()
{
return 1e-6;
}
template <>
inline double type_epsilon<double>()
{
return 1e-7;
}
TEST(hipfftTest, Create1dPlan)
{
hipfftHandle plan = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan), HIPFFT_SUCCESS);
size_t length = 1024;
ASSERT_EQ(hipfftPlan1d(&plan, length, HIPFFT_C2C, 1), HIPFFT_SUCCESS);
ASSERT_EQ(hipfftDestroy(plan), HIPFFT_SUCCESS);
}
TEST(hipfftTest, CreatePlanMany)
{
int const rank = 3;
int const nX = 64;
int const nY = 128;
int const nZ = 23;
int n[3] = {nX, nY, nZ};
int inembed[3] = {nX, nY, nZ};
int* inembed_null = nullptr;
int const istride = 1;
int const idist = nX * nY * nZ;
int onembed[3] = {nX, nY, nZ};
int* onembed_null = nullptr;
int const ostride = 1;
int const odist = nX * nY * nZ;
hipfftType type = HIPFFT_C2C;
int const batch = 1000;
size_t workSize;
// Tests plan creation with null and not null
// combinations of inembed and onembed.
//
// Valid combinations:
// inembed == null && onembed == null
// or
// inembed != null && onembed != null
//
// otherwise HIPFFT_INVALID_VALUE should be
// returned to maintain compatibility with cuFFT
// inembed == null && onembed == null
{
hipfftHandle plan_valid_1 = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan_valid_1), HIPFFT_SUCCESS);
auto ret_hipfft = hipfftMakePlanMany(plan_valid_1,
rank,
(int*)n,
inembed_null,
istride,
idist,
onembed_null,
ostride,
odist,
type,
batch,
&workSize);
ASSERT_EQ(ret_hipfft, HIPFFT_SUCCESS)
<< "inembed == null && onembed == null failed: " << hipfftResult_string(ret_hipfft);
ASSERT_EQ(hipfftSetAutoAllocation(plan_valid_1, 0), HIPFFT_SUCCESS);
ASSERT_EQ(hipfftDestroy(plan_valid_1), HIPFFT_SUCCESS);
}
// inembed != null && onembed != null
{
hipfftHandle plan_valid_2 = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan_valid_2), HIPFFT_SUCCESS);
auto ret_hipfft = hipfftMakePlanMany(plan_valid_2,
rank,
(int*)n,
inembed,
istride,
idist,
onembed,
ostride,
odist,
type,
batch,
&workSize);
ASSERT_EQ(ret_hipfft, HIPFFT_SUCCESS)
<< "inembed != null && onembed != null failed: " << hipfftResult_string(ret_hipfft);
ASSERT_EQ(hipfftSetAutoAllocation(plan_valid_2, 0), HIPFFT_SUCCESS);
ASSERT_EQ(hipfftDestroy(plan_valid_2), HIPFFT_SUCCESS);
}
// inembed != null && onembed == null
{
hipfftHandle plan_invalid_1 = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan_invalid_1), HIPFFT_SUCCESS);
auto ret_hipfft = hipfftMakePlanMany(plan_invalid_1,
rank,
(int*)n,
inembed,
istride,
idist,
onembed_null,
ostride,
odist,
type,
batch,
&workSize);
ASSERT_EQ(ret_hipfft, HIPFFT_INVALID_VALUE)
<< "inembed != null && onembed == null failed: " << hipfftResult_string(ret_hipfft);
ASSERT_EQ(hipfftDestroy(plan_invalid_1), HIPFFT_SUCCESS);
}
// inembed == null && onembed != null
{
hipfftHandle plan_invalid_2 = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan_invalid_2), HIPFFT_SUCCESS);
auto ret_hipfft = hipfftMakePlanMany(plan_invalid_2,
rank,
(int*)n,
inembed_null,
istride,
idist,
onembed,
ostride,
odist,
type,
batch,
&workSize);
ASSERT_EQ(ret_hipfft, HIPFFT_INVALID_VALUE)
<< "inembed == null && onembed != null failed: " << hipfftResult_string(ret_hipfft);
ASSERT_EQ(hipfftDestroy(plan_invalid_2), HIPFFT_SUCCESS);
}
}
TEST(hipfftTest, CreatePlanMany64)
{
int const rank = 3;
long long int const nX = 64;
long long int const nY = 128;
long long int const nZ = 23;
long long int n[3] = {nX, nY, nZ};
long long int inembed[3] = {nX, nY, nZ};
long long int const istride = 1;
long long int const idist = nX * nY * nZ;
long long int onembed[3] = {nX, nY, nZ};
long long int onembed_invalid[3] = {nX, nY, -nZ};
long long int const ostride = 1;
long long int const odist = nX * nY * nZ;
hipfftType type = HIPFFT_C2C;
long long int const batch = 1000;
long long int const batch_invalid = -2;
size_t workSize;
// Tests the 64-bit version of plan creation
// with valid/invalid data layouts.
// First test with a valid data layout
{
hipfftHandle plan_valid = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan_valid), HIPFFT_SUCCESS);
auto ret_hipfft = hipfftMakePlanMany64(plan_valid,
rank,
(long long int*)n,
inembed,
istride,
idist,
onembed,
ostride,
odist,
type,
batch,
&workSize);
ASSERT_EQ(ret_hipfft, HIPFFT_SUCCESS);
ASSERT_EQ(hipfftSetAutoAllocation(plan_valid, 0), HIPFFT_SUCCESS);
ASSERT_EQ(hipfftDestroy(plan_valid), HIPFFT_SUCCESS);
}
// invalid data layout (n array has a negative entry). only test rocFFT
// backend, since it's more strict
#ifdef __HIP_PLATFORM_AMD__
long long int n_invalid[3] = {nX, -nY, nZ};
{
hipfftHandle plan_invalid_1 = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan_invalid_1), HIPFFT_SUCCESS);
auto ret_hipfft = hipfftMakePlanMany64(plan_invalid_1,
rank,
(long long int*)n_invalid,
inembed,
istride,
idist,
onembed,
ostride,
odist,
type,
batch,
&workSize);
ASSERT_EQ(ret_hipfft, HIPFFT_INVALID_VALUE);
ASSERT_EQ(hipfftSetAutoAllocation(plan_invalid_1, 0), HIPFFT_SUCCESS);
ASSERT_EQ(hipfftDestroy(plan_invalid_1), HIPFFT_SUCCESS);
}
#endif
// invalid data layout (onembed array has a negative entry)
{
hipfftHandle plan_invalid_2 = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan_invalid_2), HIPFFT_SUCCESS);
auto ret_hipfft = hipfftMakePlanMany64(plan_invalid_2,
rank,
(long long int*)n,
inembed,
istride,
idist,
onembed_invalid,
ostride,
odist,
type,
batch,
&workSize);
ASSERT_EQ(ret_hipfft, HIPFFT_INVALID_SIZE);
ASSERT_EQ(hipfftSetAutoAllocation(plan_invalid_2, 0), HIPFFT_SUCCESS);
ASSERT_EQ(hipfftDestroy(plan_invalid_2), HIPFFT_SUCCESS);
}
// invalid data layout (batch is negative)
{
hipfftHandle plan_invalid_3 = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan_invalid_3), HIPFFT_SUCCESS);
auto ret_hipfft = hipfftMakePlanMany64(plan_invalid_3,
rank,
(long long int*)n,
inembed,
istride,
idist,
onembed,
ostride,
odist,
type,
batch_invalid,
&workSize);
ASSERT_EQ(ret_hipfft, HIPFFT_INVALID_SIZE);
ASSERT_EQ(hipfftSetAutoAllocation(plan_invalid_3, 0), HIPFFT_SUCCESS);
ASSERT_EQ(hipfftDestroy(plan_invalid_3), HIPFFT_SUCCESS);
}
}
TEST(hipfftTest, hipfftGetSizeMany)
{
int const rank = 3;
int const nX = 33;
int const nY = 128;
int const nZ = 100;
int n[3] = {nX, nY, nZ};
int inembed[3] = {nX, nY, nZ};
int const istride = 1;
int const idist = nX * nY * nZ;
int onembed[3] = {nX, nY, nZ};
int const ostride = 1;
int const odist = nX * nY * nZ;
hipfftType type = HIPFFT_C2C;
int const batch = 1;
size_t workSize;
hipfftHandle plan = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan), HIPFFT_SUCCESS);
auto ret_hipfft = hipfftGetSizeMany(plan,
rank,
(int*)n,
inembed,
istride,
idist,
onembed,
ostride,
odist,
type,
batch,
&workSize);
ASSERT_EQ(ret_hipfft, HIPFFT_SUCCESS);
ASSERT_EQ(hipfftSetAutoAllocation(plan, 0), HIPFFT_SUCCESS);
ASSERT_EQ(hipfftDestroy(plan), HIPFFT_SUCCESS);
}
TEST(hipfftTest, hipfftGetSizeMany64)
{
int const rank = 3;
long long int const nX = 133;
long long int const nY = 354;
long long int const nZ = 256;
long long int n[3] = {nX, nY, nZ};
long long int inembed[3] = {nX, nY, nZ};
long long int const istride = 1;
long long int const idist = nX * nY * nZ;
long long int onembed[3] = {nX, nY, nZ};
long long int const ostride = 1;
long long int const odist = nX * nY * nZ;
hipfftType type = HIPFFT_C2C;
long long int const batch = 2;
size_t workSize;
hipfftHandle plan = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan), HIPFFT_SUCCESS);
auto ret_hipfft = hipfftGetSizeMany64(plan,
rank,
(long long int*)n,
inembed,
istride,
idist,
onembed,
ostride,
odist,
type,
batch,
&workSize);
ASSERT_EQ(ret_hipfft, HIPFFT_SUCCESS);
ASSERT_EQ(hipfftSetAutoAllocation(plan, 0), HIPFFT_SUCCESS);
ASSERT_EQ(hipfftDestroy(plan), HIPFFT_SUCCESS);
}
TEST(hipfftTest, CheckBufferSizeC2C)
{
hipfftHandle plan = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan), HIPFFT_SUCCESS);
size_t n = 1024;
size_t workSize = 0;
ASSERT_EQ(hipfftMakePlan1d(plan, n, HIPFFT_C2C, 1, &workSize), HIPFFT_SUCCESS);
#ifdef __HIP_PLATFORM_AMD__
// No extra work buffer for C2C
EXPECT_EQ(workSize, 0);
#endif
ASSERT_EQ(hipfftDestroy(plan), HIPFFT_SUCCESS);
}
TEST(hipfftTest, CheckBufferSizeR2C)
{
hipfftHandle plan = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan), HIPFFT_SUCCESS);
// real forward transform cannot modify input, so we need to pick
// a sufficiently small N such that rocFFT can fuse
// post-processing into one kernel and avoid a temp buffer
size_t n = 256;
size_t workSize = 0;
ASSERT_EQ(hipfftMakePlan1d(plan, n, HIPFFT_R2C, 1, &workSize), HIPFFT_SUCCESS);
#ifdef __HIP_PLATFORM_AMD__
// NOTE: keep this condition for ease of changing n for ad-hoc tests
//
// cppcheck-suppress knownConditionTrueFalse
if(n % 2 == 0)
{
EXPECT_EQ(workSize, 0);
}
else
{
EXPECT_EQ(workSize, 2 * n * sizeof(float));
}
#endif
EXPECT_EQ(hipfftDestroy(plan), HIPFFT_SUCCESS);
}
TEST(hipfftTest, CheckBufferSizeC2R)
{
hipfftHandle plan = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan), HIPFFT_SUCCESS);
size_t n = 2048;
size_t workSize = 0;
ASSERT_EQ(hipfftMakePlan1d(plan, n, HIPFFT_C2R, 1, &workSize), HIPFFT_SUCCESS);
#ifdef __HIP_PLATFORM_AMD__
// NOTE: keep this condition for ease of changing n for ad-hoc tests
//
// cppcheck-suppress knownConditionTrueFalse
if(n % 2 == 0)
{
EXPECT_EQ(workSize, 0);
}
else
{
EXPECT_EQ(workSize, 2 * n * sizeof(float));
}
#endif
ASSERT_EQ(hipfftDestroy(plan), HIPFFT_SUCCESS);
}
TEST(hipfftTest, CheckBufferSizeD2Z)
{
hipfftHandle plan = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan), HIPFFT_SUCCESS);
// real forward transform cannot modify input, so we need to pick
// a sufficiently small N such that rocFFT can fuse
// post-processing into one kernel and avoid a temp buffer
size_t n = 256;
size_t batch = 1000;
size_t workSize = 0;
ASSERT_EQ(hipfftMakePlan1d(plan, n, HIPFFT_D2Z, batch, &workSize), HIPFFT_SUCCESS);
#ifdef __HIP_PLATFORM_AMD__
// NOTE: keep this condition for ease of changing n for ad-hoc tests
//
// cppcheck-suppress knownConditionTrueFalse
if(n % 2 == 0)
{
EXPECT_EQ(workSize, 0);
}
else
{
EXPECT_EQ(workSize, 2 * n * sizeof(double));
}
#endif
ASSERT_EQ(hipfftDestroy(plan), HIPFFT_SUCCESS);
}
TEST(hipfftTest, CheckBufferSizeZ2D)
{
hipfftHandle plan = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan), HIPFFT_SUCCESS);
size_t n = 2048;
size_t batch = 1000;
size_t workSize = 0;
ASSERT_EQ(hipfftMakePlan1d(plan, n, HIPFFT_Z2D, batch, &workSize), HIPFFT_SUCCESS);
#ifdef __HIP_PLATFORM_AMD__
// NOTE: keep this condition for ease of changing n for ad-hoc tests
//
// cppcheck-suppress knownConditionTrueFalse
if(n % 2 == 0)
{
EXPECT_EQ(workSize, 0);
}
else
{
EXPECT_EQ(workSize, 2 * n * sizeof(double));
}
#endif
ASSERT_EQ(hipfftDestroy(plan), HIPFFT_SUCCESS);
}
#ifdef __HIP_PLATFORM_AMD__
TEST(hipfftTest, CheckNullWorkBuffer)
{
hipfftHandle plan = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan), HIPFFT_SUCCESS);
size_t n = 2048;
size_t batch = 1000;
size_t workSize = 0;
ASSERT_EQ(hipfftMakePlan1d(plan, n, HIPFFT_Z2D, batch, &workSize), HIPFFT_SUCCESS);
EXPECT_EQ(hipfftSetWorkArea(plan, nullptr), HIPFFT_SUCCESS);
ASSERT_EQ(hipfftDestroy(plan), HIPFFT_SUCCESS);
}
#endif
TEST(hipfftTest, RunR2C)
{
const size_t N = 4096;
float in[N];
for(size_t i = 0; i < N; i++)
in[i] = i + (i % 3) - (i % 7);
hipfftReal* d_in;
hipfftComplex* d_out;
ASSERT_EQ(hipMalloc(&d_in, N * sizeof(hipfftReal)), hipSuccess);
ASSERT_EQ(hipMalloc(&d_out, (N / 2 + 1) * sizeof(hipfftComplex)), hipSuccess);
ASSERT_EQ(hipMemcpy(d_in, in, N * sizeof(hipfftReal), hipMemcpyHostToDevice), hipSuccess);
hipfftHandle plan = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan), HIPFFT_SUCCESS);
size_t workSize;
ASSERT_EQ(hipfftMakePlan1d(plan, N, HIPFFT_R2C, 1, &workSize), HIPFFT_SUCCESS);
EXPECT_EQ(hipfftExecR2C(plan, d_in, d_out), HIPFFT_SUCCESS);
std::vector<hipfftComplex> out(N / 2 + 1);
ASSERT_EQ(hipMemcpy(&out[0], d_out, (N / 2 + 1) * sizeof(hipfftComplex), hipMemcpyDeviceToHost),
hipSuccess);
ASSERT_EQ(hipfftDestroy(plan), HIPFFT_SUCCESS);
ASSERT_EQ(hipFree(d_in), hipSuccess);
ASSERT_EQ(hipFree(d_out), hipSuccess);
;
// NOTE: keep this condition for ease of changing n for ad-hoc tests
//
// cppcheck-suppress knownConditionTrueFalse
if(N % 2 != 0)
{
EXPECT_TRUE(workSize != 0);
}
double ref_in[N];
for(size_t i = 0; i < N; i++)
ref_in[i] = in[i];
fftw_complex* ref_out;
fftw_plan ref_p;
ref_out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * (N / 2 + 1));
ref_p = fftw_plan_dft_r2c_1d(N, ref_in, ref_out, FFTW_ESTIMATE);
fftw_execute(ref_p);
double maxv = 0;
double nrmse = 0; // normalized root mean square error
for(size_t i = 0; i < (N / 2 + 1); i++)
{
// printf("element %d: FFTW result %f, %f; hipFFT result %f, %f \n", (int)i, ref_out[i][0], ref_out[i][1], out[i].x, out[i].y);
double dr = ref_out[i][0] - out[i].x;
double di = ref_out[i][1] - out[i].y;
maxv = fabs(ref_out[i][0]) > maxv ? fabs(ref_out[i][0]) : maxv;
maxv = fabs(ref_out[i][1]) > maxv ? fabs(ref_out[i][1]) : maxv;
nrmse += ((dr * dr) + (di * di));
}
nrmse /= (double)((N / 2 + 1));
nrmse = sqrt(nrmse);
nrmse /= maxv;
EXPECT_TRUE(nrmse < type_epsilon<double>());
fftw_destroy_plan(ref_p);
fftw_free(ref_out);
}
// ask for a transform whose parameters are only valid out-of-place.
// since hipFFT generates both in-place and out-place plans up front
// (because it's not told about the placement until exec time), this
// ensures that a failure to create an in-place plan doesn't prevent
// the out-place plan from working.
TEST(hipfftTest, OutplaceOnly)
{
static const int N_in_const = 4;
static const int N_out_const = N_in_const / 2 + 1;
// mutable sizes for passing to hipFFT
int N_in = N_in_const;
int N_out = N_out_const;
float in[N_in_const];
for(int i = 0; i < N_in; i++)
in[i] = i + (i % 3) - (i % 7);
hipfftReal* d_in;
hipfftComplex* d_out;
ASSERT_EQ(hipMalloc(&d_in, N_in * sizeof(hipfftReal)), hipSuccess);
ASSERT_EQ(hipMalloc(&d_out, N_out * sizeof(hipfftComplex)), hipSuccess);
ASSERT_EQ(hipMemcpy(d_in, in, N_in * sizeof(hipfftReal), hipMemcpyHostToDevice), hipSuccess);
hipfftHandle plan = hipfft_params::INVALID_PLAN_HANDLE;
ASSERT_EQ(hipfftCreate(&plan), HIPFFT_SUCCESS);
ASSERT_EQ(hipfftPlanMany(&plan, 1, &N_in, &N_in, 1, N_in, &N_out, 1, N_out, HIPFFT_R2C, 1),
HIPFFT_SUCCESS);
ASSERT_EQ(plan == hipfft_params::INVALID_PLAN_HANDLE, false);
ASSERT_EQ(hipfftExecR2C(plan, d_in, d_out), HIPFFT_SUCCESS) << "hipfftExecR2C failed";
std::vector<hipfftComplex> out(N_out);
ASSERT_EQ(hipMemcpy(out.data(), d_out, N_out * sizeof(hipfftComplex), hipMemcpyDeviceToHost),
hipSuccess);
// in-place transform isn't really *supposed* to work - this
// might or might not fail but we can at least check that it
// doesn't blow up.
//hipfftExecR2C(plan, reinterpret_cast<hipfftReal*>(d_out), d_out);
ASSERT_EQ(hipfftDestroy(plan), HIPFFT_SUCCESS);
ASSERT_EQ(hipFree(d_in), hipSuccess);
ASSERT_EQ(hipFree(d_out), hipSuccess);
double ref_in[N_in_const];
for(int i = 0; i < N_in_const; i++)
ref_in[i] = in[i];
fftw_complex* ref_out;
fftw_plan ref_p;
ref_out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * N_out);
ref_p = fftw_plan_dft_r2c_1d(N_in, ref_in, ref_out, FFTW_ESTIMATE);
fftw_execute(ref_p);
double maxv = 0;
double nrmse = 0; // normalized root mean square error
for(int i = 0; i < N_out; i++)
{
// printf("element %d: FFTW result %f, %f; hipFFT result %f, %f \n", (int)i, ref_out[i][0], ref_out[i][1], out[i].x, out[i].y);
double dr = ref_out[i][0] - out[i].x;
double di = ref_out[i][1] - out[i].y;
maxv = fabs(ref_out[i][0]) > maxv ? fabs(ref_out[i][0]) : maxv;
maxv = fabs(ref_out[i][1]) > maxv ? fabs(ref_out[i][1]) : maxv;
nrmse += ((dr * dr) + (di * di));
}
nrmse /= (double)(N_out);
nrmse = sqrt(nrmse);
nrmse /= maxv;
ASSERT_TRUE(nrmse < type_epsilon<double>());
fftw_destroy_plan(ref_p);
fftw_free(ref_out);
}
|