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 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
|
// Copyright (C) 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.
#ifndef DATA_GEN_DEVICE_H
#define DATA_GEN_DEVICE_H
// rocRAND can generate warnings if inline asm is not available for
// some architectures. data generation isn't performance-critical,
// so just disable inline asm to prevent the warnings.
#define ROCRAND_DISABLE_INLINE_ASM
#include "../shared/arithmetic.h"
#include "../shared/device_properties.h"
#include "../shared/gpubuf.h"
#include "../shared/increment.h"
#include "../shared/rocfft_complex.h"
#include <hip/hip_runtime.h>
#include <hip/hip_runtime_api.h>
#include <hiprand/hiprand.h>
#include <hiprand/hiprand_kernel.h>
#include <limits>
#include <vector>
static const unsigned int DATA_GEN_THREADS = 8;
static const unsigned int DATA_GEN_GRID_Y_MAX = 64;
template <typename T>
struct input_val_1D
{
T val1;
};
template <typename T>
struct input_val_2D
{
T val1;
T val2;
};
template <typename T>
struct input_val_3D
{
T val1;
T val2;
T val3;
};
template <typename T>
static input_val_1D<T> get_input_val(const T& val)
{
return input_val_1D<T>{val};
}
template <typename T>
static input_val_2D<T> get_input_val(const std::tuple<T, T>& val)
{
return input_val_2D<T>{std::get<0>(val), std::get<1>(val)};
}
template <typename T>
static input_val_3D<T> get_input_val(const std::tuple<T, T, T>& val)
{
return input_val_3D<T>{std::get<0>(val), std::get<1>(val), std::get<2>(val)};
}
template <typename T>
__device__ static size_t
compute_index(const input_val_1D<T>& length, const input_val_1D<T>& stride, size_t base)
{
return (length.val1 * stride.val1) + base;
}
template <typename T>
__device__ static size_t
compute_index(const input_val_2D<T>& length, const input_val_2D<T>& stride, size_t base)
{
return (length.val1 * stride.val1) + (length.val2 * stride.val2) + base;
}
template <typename T>
__device__ static size_t
compute_index(const input_val_3D<T>& length, const input_val_3D<T>& stride, size_t base)
{
return (length.val1 * stride.val1) + (length.val2 * stride.val2) + (length.val3 * stride.val3)
+ base;
}
template <typename T>
static inline input_val_1D<T> make_zero_length(const input_val_1D<T>& whole_length)
{
return input_val_1D<T>{0};
}
template <typename T>
static inline input_val_2D<T> make_zero_length(const input_val_2D<T>& whole_length)
{
return input_val_2D<T>{0, 0};
}
template <typename T>
static inline input_val_3D<T> make_zero_length(const input_val_3D<T>& whole_length)
{
return input_val_3D<T>{0, 0, 0};
}
template <typename T>
static inline input_val_1D<T> make_unit_stride(const input_val_1D<T>& whole_length)
{
return input_val_1D<T>{1};
}
template <typename T>
static inline input_val_2D<T> make_unit_stride(const input_val_2D<T>& whole_length)
{
return input_val_2D<T>{1, whole_length.val1};
}
template <typename T>
static inline input_val_3D<T> make_unit_stride(const input_val_3D<T>& whole_length)
{
return input_val_3D<T>{1, whole_length.val1, whole_length.val1 * whole_length.val2};
}
template <typename T>
__device__ static input_val_1D<T> get_length(const size_t i, const input_val_1D<T>& whole_length)
{
auto xlen = whole_length.val1;
auto xidx = i % xlen;
return input_val_1D<T>{xidx};
}
template <typename T>
__device__ static input_val_2D<T> get_length(const size_t i, const input_val_2D<T>& whole_length)
{
auto xlen = whole_length.val1;
auto ylen = whole_length.val2;
auto xidx = i % xlen;
auto yidx = i / xlen % ylen;
return input_val_2D<T>{xidx, yidx};
}
template <typename T>
__device__ static input_val_3D<T> get_length(const size_t i, const input_val_3D<T>& whole_length)
{
auto xlen = whole_length.val1;
auto ylen = whole_length.val2;
auto zlen = whole_length.val3;
auto xidx = i % xlen;
auto yidx = i / xlen % ylen;
auto zidx = i / xlen / ylen % zlen;
return input_val_3D<T>{xidx, yidx, zidx};
}
template <typename T>
__device__ static size_t get_batch(const size_t i, const input_val_1D<T>& whole_length)
{
auto xlen = whole_length.val1;
auto yidx = i / xlen;
return yidx;
}
template <typename T>
__device__ static size_t get_batch(const size_t i, const input_val_2D<T>& whole_length)
{
auto xlen = whole_length.val1;
auto ylen = whole_length.val2;
auto zidx = i / xlen / ylen;
return zidx;
}
template <typename T>
__device__ static size_t get_batch(const size_t i, const input_val_3D<T>& length)
{
auto xlen = length.val1;
auto ylen = length.val2;
auto zlen = length.val3;
auto widx = i / xlen / ylen / zlen;
return widx;
}
__device__ static double make_random_val(hiprandStatePhilox4_32_10* gen_state, double offset)
{
return hiprand_uniform_double(gen_state) + offset;
}
__device__ static float make_random_val(hiprandStatePhilox4_32_10* gen_state, float offset)
{
return hiprand_uniform(gen_state) + offset;
}
__device__ static _Float16 make_random_val(hiprandStatePhilox4_32_10* gen_state, _Float16 offset)
{
return static_cast<_Float16>(hiprand_uniform(gen_state)) + offset;
}
template <typename Tcomplex>
__device__ static void set_imag_zero(const size_t pos, Tcomplex* x)
{
x[pos].y = 0.0;
}
template <typename Tfloat>
__device__ static void set_imag_zero(const size_t pos, Tfloat* xreal, Tfloat* ximag)
{
ximag[pos] = 0.0;
}
template <typename Tcomplex>
__device__ static void conjugate(const size_t pos, const size_t cpos, Tcomplex* x)
{
x[pos].x = x[cpos].x;
x[pos].y = -x[cpos].y;
}
template <typename Tfloat>
__device__ static void conjugate(const size_t pos, const size_t cpos, Tfloat* xreal, Tfloat* ximag)
{
xreal[pos] = xreal[cpos];
ximag[pos] = -ximag[cpos];
}
template <typename Tint, typename Treal>
__global__ static void __launch_bounds__(DATA_GEN_THREADS)
generate_random_interleaved_data_kernel(const Tint whole_length,
const Tint zero_length,
const size_t idist,
const size_t isize,
const Tint istride,
rocfft_complex<Treal>* data)
{
auto const i = static_cast<size_t>(threadIdx.x) + blockIdx.x * blockDim.x
+ blockIdx.y * gridDim.x * DATA_GEN_THREADS;
static_assert(sizeof(i) >= sizeof(isize));
if(i < isize)
{
auto i_length = get_length(i, whole_length);
auto i_batch = get_batch(i, whole_length);
auto i_base = i_batch * idist;
auto seed = compute_index(zero_length, istride, i_base);
auto idx = compute_index(i_length, istride, i_base);
hiprandStatePhilox4_32_10 gen_state;
hiprand_init(seed, idx, 0, &gen_state);
data[idx].x = make_random_val(&gen_state, static_cast<Treal>(-0.5));
data[idx].y = make_random_val(&gen_state, static_cast<Treal>(-0.5));
}
}
template <typename Tint, typename Treal>
__global__ static void __launch_bounds__(DATA_GEN_THREADS)
generate_interleaved_data_kernel(const Tint whole_length,
const size_t idist,
const size_t isize,
const Tint istride,
const Tint ustride,
const Treal inv_scale,
rocfft_complex<Treal>* data)
{
auto const i = static_cast<size_t>(threadIdx.x) + blockIdx.x * blockDim.x
+ blockIdx.y * gridDim.x * DATA_GEN_THREADS;
static_assert(sizeof(i) >= sizeof(isize));
if(i < isize)
{
const auto i_length = get_length(i, whole_length);
const auto i_batch = get_batch(i, whole_length);
const auto i_base = i_batch * idist;
const auto val = static_cast<Treal>(-0.5)
+ static_cast<Treal>(
static_cast<unsigned long long>(compute_index(i_length, ustride, 0)))
* inv_scale;
const auto idx = compute_index(i_length, istride, i_base);
data[idx].x = val;
data[idx].y = val;
}
}
template <typename Tint, typename Treal>
__global__ static void __launch_bounds__(DATA_GEN_THREADS)
generate_random_planar_data_kernel(const Tint whole_length,
const Tint zero_length,
const size_t idist,
const size_t isize,
const Tint istride,
Treal* real_data,
Treal* imag_data)
{
auto const i = static_cast<size_t>(threadIdx.x) + blockIdx.x * blockDim.x
+ blockIdx.y * gridDim.x * DATA_GEN_THREADS;
static_assert(sizeof(i) >= sizeof(isize));
if(i < isize)
{
auto i_length = get_length(i, whole_length);
auto i_batch = get_batch(i, whole_length);
auto i_base = i_batch * idist;
auto seed = compute_index(zero_length, istride, i_base);
auto idx = compute_index(i_length, istride, i_base);
hiprandStatePhilox4_32_10 gen_state;
hiprand_init(seed, idx, 0, &gen_state);
real_data[idx] = make_random_val(&gen_state, static_cast<Treal>(-0.5));
imag_data[idx] = make_random_val(&gen_state, static_cast<Treal>(-0.5));
}
}
template <typename Tint, typename Treal>
__global__ static void __launch_bounds__(DATA_GEN_THREADS)
generate_planar_data_kernel(const Tint whole_length,
const size_t idist,
const size_t isize,
const Tint istride,
const Tint ustride,
const Treal inv_scale,
Treal* real_data,
Treal* imag_data)
{
auto const i = static_cast<size_t>(threadIdx.x) + blockIdx.x * blockDim.x
+ blockIdx.y * gridDim.x * DATA_GEN_THREADS;
static_assert(sizeof(i) >= sizeof(isize));
if(i < isize)
{
const auto i_length = get_length(i, whole_length);
const auto i_batch = get_batch(i, whole_length);
const auto i_base = i_batch * idist;
const auto val = static_cast<Treal>(-0.5)
+ static_cast<Treal>(
static_cast<unsigned long long>(compute_index(i_length, ustride, 0)))
* inv_scale;
const auto idx = compute_index(i_length, istride, i_base);
real_data[idx] = val;
imag_data[idx] = val;
}
}
template <typename Tint, typename Treal>
__global__ static void __launch_bounds__(DATA_GEN_THREADS)
generate_random_real_data_kernel(const Tint whole_length,
const Tint zero_length,
const size_t idist,
const size_t isize,
const Tint istride,
Treal* data)
{
auto const i = static_cast<size_t>(threadIdx.x) + blockIdx.x * blockDim.x
+ blockIdx.y * gridDim.x * DATA_GEN_THREADS;
static_assert(sizeof(i) >= sizeof(isize));
if(i < isize)
{
auto i_length = get_length(i, whole_length);
auto i_batch = get_batch(i, whole_length);
auto i_base = i_batch * idist;
auto seed = compute_index(zero_length, istride, i_base);
auto idx = compute_index(i_length, istride, i_base);
hiprandStatePhilox4_32_10 gen_state;
hiprand_init(seed, idx, 0, &gen_state);
data[idx] = make_random_val(&gen_state, static_cast<Treal>(-0.5));
}
}
template <typename Tint, typename Treal>
__global__ static void __launch_bounds__(DATA_GEN_THREADS)
generate_real_data_kernel(const Tint whole_length,
const size_t idist,
const size_t isize,
const Tint istride,
const Tint ustride,
const Treal inv_scale,
Treal* data)
{
auto const i = static_cast<size_t>(threadIdx.x) + blockIdx.x * blockDim.x
+ blockIdx.y * gridDim.x * DATA_GEN_THREADS;
static_assert(sizeof(i) >= sizeof(isize));
if(i < isize)
{
const auto i_length = get_length(i, whole_length);
const auto i_batch = get_batch(i, whole_length);
const auto i_base = i_batch * idist;
const auto val = static_cast<Treal>(-0.5)
+ static_cast<Treal>(
static_cast<unsigned long long>(compute_index(i_length, ustride, 0)))
* inv_scale;
const auto idx = compute_index(i_length, istride, i_base);
data[idx] = val;
}
}
// For complex-to-real transforms, the input data must be Hermitiam-symmetric.
// That is, u_k is the complex conjugate of u_{-k}, where k is the wavevector in Fourier
// space. For multi-dimensional data, this means that we only need to store a bit more
// than half of the complex values; the rest are redundant. However, there are still
// some restrictions:
// * the origin and Nyquist value(s) must be real-valued
// * some of the remaining values are still redundant, and you might get different results
// than you expect if the values don't agree.
template <typename Tcomplex>
__global__ static void impose_hermitian_symmetry_interleaved_1D_kernel(Tcomplex* x,
const size_t Nx,
const size_t xstride,
const size_t dist,
const size_t batch_total,
const bool Nxeven)
{
auto id_batch = static_cast<size_t>(threadIdx.x) + blockIdx.x * blockDim.x;
static_assert(sizeof(id_batch) == sizeof(size_t));
if(id_batch < batch_total)
{
id_batch *= dist;
set_imag_zero(id_batch, x);
if(Nxeven)
set_imag_zero(id_batch + (Nx / 2) * xstride, x);
}
}
template <typename Tfloat>
__global__ static void impose_hermitian_symmetry_planar_1D_kernel(Tfloat* xreal,
Tfloat* ximag,
const size_t Nx,
const size_t xstride,
const size_t dist,
const size_t batch_total,
const bool Nxeven)
{
auto id_batch = static_cast<size_t>(threadIdx.x) + blockIdx.x * blockDim.x;
static_assert(sizeof(id_batch) == sizeof(size_t));
if(id_batch < batch_total)
{
id_batch *= dist;
set_imag_zero(id_batch, xreal, ximag);
if(Nxeven)
set_imag_zero(id_batch + (Nx / 2) * xstride, xreal, ximag);
}
}
template <typename Tcomplex>
__global__ static void impose_hermitian_symmetry_interleaved_2D_kernel(Tcomplex* x,
const size_t Nx,
const size_t Ny,
const size_t xstride,
const size_t ystride,
const size_t dist,
const size_t batch_total,
const size_t x_total,
const bool Nxeven,
const bool Nyeven)
{
auto id_batch = static_cast<size_t>(threadIdx.x) + blockIdx.x * blockDim.x;
const auto id_x = static_cast<size_t>(threadIdx.y) + blockIdx.y * blockDim.y;
static_assert(sizeof(id_batch) == sizeof(size_t));
static_assert(sizeof(id_x) == sizeof(size_t));
if(id_batch < batch_total)
{
id_batch *= dist;
if(id_x == 0)
set_imag_zero(id_batch, x);
if(id_x == 0 && Nxeven)
set_imag_zero(id_batch + (Nx / 2) * xstride, x);
if(id_x == 0 && Nyeven)
set_imag_zero(id_batch + ystride * (Ny / 2), x);
if(id_x == 0 && Nxeven && Nyeven)
set_imag_zero(id_batch + xstride * (Nx / 2) + ystride * (Ny / 2), x);
if(id_x < x_total)
{
conjugate(id_batch + xstride * (Nx - (id_x + 1)), id_batch + xstride * (id_x + 1), x);
if(Nyeven)
conjugate(id_batch + xstride * (Nx - (id_x + 1)) + ystride * (Ny / 2),
id_batch + xstride * (id_x + 1) + ystride * (Ny / 2),
x);
}
}
}
template <typename Tfloat>
__global__ static void impose_hermitian_symmetry_planar_2D_kernel(Tfloat* xreal,
Tfloat* ximag,
const size_t Nx,
const size_t Ny,
const size_t xstride,
const size_t ystride,
const size_t dist,
const size_t batch_total,
const size_t x_total,
const bool Nxeven,
const bool Nyeven)
{
auto id_batch = static_cast<size_t>(threadIdx.x) + blockIdx.x * blockDim.x;
const auto id_x = static_cast<size_t>(threadIdx.y) + blockIdx.y * blockDim.y;
static_assert(sizeof(id_batch) == sizeof(size_t));
static_assert(sizeof(id_x) == sizeof(size_t));
if(id_batch < batch_total)
{
id_batch *= dist;
if(id_x == 0)
set_imag_zero(id_batch, xreal, ximag);
if(id_x == 0 && Nxeven)
set_imag_zero(id_batch + (Nx / 2) * xstride, xreal, ximag);
if(id_x == 0 && Nyeven)
set_imag_zero(id_batch + ystride * (Ny / 2), xreal, ximag);
if(id_x == 0 && Nxeven && Nyeven)
set_imag_zero(id_batch + xstride * (Nx / 2) + ystride * (Ny / 2), xreal, ximag);
if(id_x < x_total)
{
conjugate(id_batch + xstride * (Nx - (id_x + 1)),
id_batch + xstride * (id_x + 1),
xreal,
ximag);
if(Nyeven)
conjugate(id_batch + xstride * (Nx - (id_x + 1)) + ystride * (Ny / 2),
id_batch + xstride * (id_x + 1) + ystride * (Ny / 2),
xreal,
ximag);
}
}
}
template <typename Tcomplex>
__global__ static void impose_hermitian_symmetry_interleaved_3D_kernel(Tcomplex* x,
const size_t Nx,
const size_t Ny,
const size_t Nz,
const size_t xstride,
const size_t ystride,
const size_t zstride,
const size_t dist,
const size_t batch_total,
const size_t x_total,
const size_t y_total,
const size_t y_total_half,
const bool Nxeven,
const bool Nyeven,
const bool Nzeven)
{
auto id_batch = static_cast<size_t>(threadIdx.x) + blockIdx.x * blockDim.x;
const auto id_x = static_cast<size_t>(threadIdx.y) + blockIdx.y * blockDim.y;
const auto id_y = static_cast<size_t>(threadIdx.z) + blockIdx.z * blockDim.z;
static_assert(sizeof(id_batch) == sizeof(size_t));
static_assert(sizeof(id_x) == sizeof(size_t));
static_assert(sizeof(id_y) == sizeof(size_t));
if(id_batch < batch_total)
{
auto id_x_y_zero = (id_x == 0 && id_y == 0);
id_batch *= dist;
if(id_x_y_zero)
set_imag_zero(id_batch, x);
if(Nxeven && id_x_y_zero)
set_imag_zero(id_batch + xstride * (Nx / 2), x);
if(Nyeven && id_x_y_zero)
set_imag_zero(id_batch + ystride * (Ny / 2), x);
if(Nzeven && id_x_y_zero)
set_imag_zero(id_batch + zstride * (Nz / 2), x);
if(Nxeven && Nyeven && id_x_y_zero)
set_imag_zero(id_batch + xstride * (Nx / 2) + ystride * (Ny / 2), x);
if(Nxeven && Nzeven && id_x_y_zero)
set_imag_zero(id_batch + xstride * (Nx / 2) + zstride * (Nz / 2), x);
if(Nyeven && Nzeven && id_x_y_zero)
set_imag_zero(id_batch + ystride * (Ny / 2) + zstride * (Nz / 2), x);
if(Nxeven && Nyeven && Nzeven && id_x_y_zero)
set_imag_zero(id_batch + xstride * (Nx / 2) + ystride * (Ny / 2) + zstride * (Nz / 2),
x);
if(id_x == 0 && id_y < y_total_half)
conjugate(id_batch + ystride * (Ny - (id_y + 1)), id_batch + ystride * (id_y + 1), x);
if(Nxeven && id_x == 0 && id_y < y_total_half)
conjugate(id_batch + xstride * (Nx / 2) + ystride * (Ny - (id_y + 1)),
id_batch + xstride * (Nx / 2) + ystride * (id_y + 1),
x);
if(id_x < x_total && id_y == 0)
conjugate(id_batch + xstride * (Nx - (id_x + 1)), id_batch + xstride * (id_x + 1), x);
if(Nyeven && id_x < x_total && id_y == 0)
conjugate(id_batch + xstride * (Nx - (id_x + 1)) + ystride * (Ny / 2),
id_batch + xstride * (id_x + 1) + ystride * (Ny / 2),
x);
if(id_x < x_total && id_y < y_total)
conjugate(id_batch + xstride * (Nx - (id_x + 1)) + ystride * (Ny - (id_y + 1)),
id_batch + xstride * (id_x + 1) + ystride * (id_y + 1),
x);
if(Nzeven)
{
if(id_x < x_total && id_y == 0)
conjugate(id_batch + xstride * (Nx - (id_x + 1)) + zstride * (Nz / 2),
id_batch + xstride * (id_x + 1) + zstride * (Nz / 2),
x);
if(Nyeven && id_x < x_total && id_y == 0)
conjugate(id_batch + xstride * (Nx - (id_x + 1)) + zstride * (Nz / 2),
id_batch + xstride * (id_x + 1) + zstride * (Nz / 2),
x);
if(id_x == 0 && id_y < y_total_half)
conjugate(id_batch + ystride * (Ny - (id_y + 1)) + zstride * (Nz / 2),
id_batch + ystride * (id_y + 1) + zstride * (Nz / 2),
x);
if(Nxeven && id_x == 0 && id_y < y_total_half)
conjugate(id_batch + xstride * (Nx / 2) + ystride * (Ny - (id_y + 1))
+ zstride * (Nz / 2),
id_batch + xstride * (Nx / 2) + ystride * (id_y + 1) + zstride * (Nz / 2),
x);
if(id_x < x_total && id_y < y_total)
conjugate(id_batch + xstride * (Nx - (id_x + 1)) + ystride * (Ny - (id_y + 1))
+ zstride * (Nz / 2),
id_batch + xstride * (id_x + 1) + ystride * (id_y + 1)
+ zstride * (Nz / 2),
x);
}
}
}
template <typename Tfloat>
__global__ static void impose_hermitian_symmetry_planar_3D_kernel(Tfloat* xreal,
Tfloat* ximag,
const size_t Nx,
const size_t Ny,
const size_t Nz,
const size_t xstride,
const size_t ystride,
const size_t zstride,
const size_t dist,
const size_t batch_total,
const size_t x_total,
const size_t y_total,
const size_t y_total_half,
const bool Nxeven,
const bool Nyeven,
const bool Nzeven)
{
auto id_batch = static_cast<size_t>(threadIdx.x) + blockIdx.x * blockDim.x;
const auto id_x = static_cast<size_t>(threadIdx.y) + blockIdx.y * blockDim.y;
const auto id_y = static_cast<size_t>(threadIdx.z) + blockIdx.z * blockDim.z;
static_assert(sizeof(id_batch) == sizeof(size_t));
static_assert(sizeof(id_x) == sizeof(size_t));
static_assert(sizeof(id_y) == sizeof(size_t));
if(id_batch < batch_total)
{
auto id_x_y_zero = (id_x == 0 && id_y == 0);
id_batch *= dist;
if(id_x_y_zero)
set_imag_zero(id_batch, xreal, ximag);
if(Nxeven && id_x_y_zero)
set_imag_zero(id_batch + xstride * (Nx / 2), xreal, ximag);
if(Nyeven && id_x_y_zero)
set_imag_zero(id_batch + ystride * (Ny / 2), xreal, ximag);
if(Nzeven && id_x_y_zero)
set_imag_zero(id_batch + zstride * (Nz / 2), xreal, ximag);
if(Nxeven && Nyeven && id_x_y_zero)
set_imag_zero(id_batch + xstride * (Nx / 2) + ystride * (Ny / 2), xreal, ximag);
if(Nxeven && Nzeven && id_x_y_zero)
set_imag_zero(id_batch + xstride * (Nx / 2) + zstride * (Nz / 2), xreal, ximag);
if(Nyeven && Nzeven && id_x_y_zero)
set_imag_zero(id_batch + ystride * (Ny / 2) + zstride * (Nz / 2), xreal, ximag);
if(Nxeven && Nyeven && Nzeven && id_x_y_zero)
set_imag_zero(id_batch + xstride * (Nx / 2) + ystride * (Ny / 2) + zstride * (Nz / 2),
xreal,
ximag);
if(id_x == 0 && id_y < y_total_half)
conjugate(id_batch + ystride * (Ny - (id_y + 1)),
id_batch + ystride * (id_y + 1),
xreal,
ximag);
if(Nxeven && id_x == 0 && id_y < y_total_half)
conjugate(id_batch + xstride * (Nx / 2) + ystride * (Ny - (id_y + 1)),
id_batch + xstride * (Nx / 2) + ystride * (id_y + 1),
xreal,
ximag);
if(id_x < x_total && id_y == 0)
conjugate(id_batch + xstride * (Nx - (id_x + 1)),
id_batch + xstride * (id_x + 1),
xreal,
ximag);
if(Nyeven && id_x < x_total && id_y == 0)
conjugate(id_batch + xstride * (Nx - (id_x + 1)) + ystride * (Ny / 2),
id_batch + xstride * (id_x + 1) + ystride * (Ny / 2),
xreal,
ximag);
if(id_x < x_total && id_y < y_total)
conjugate(id_batch + xstride * (Nx - (id_x + 1)) + ystride * (Ny - (id_y + 1)),
id_batch + xstride * (id_x + 1) + ystride * (id_y + 1),
xreal,
ximag);
if(Nzeven)
{
if(id_x < x_total && id_y == 0)
conjugate(id_batch + xstride * (Nx - (id_x + 1)) + zstride * (Nz / 2),
id_batch + xstride * (id_x + 1) + zstride * (Nz / 2),
xreal,
ximag);
if(Nyeven && id_x < x_total && id_y == 0)
conjugate(id_batch + xstride * (Nx - (id_x + 1)) + zstride * (Nz / 2),
id_batch + xstride * (id_x + 1) + zstride * (Nz / 2),
xreal,
ximag);
if(id_x == 0 && id_y < y_total_half)
conjugate(id_batch + ystride * (Ny - (id_y + 1)) + zstride * (Nz / 2),
id_batch + ystride * (id_y + 1) + zstride * (Nz / 2),
xreal,
ximag);
if(Nxeven && id_x == 0 && id_y < y_total_half)
conjugate(id_batch + xstride * (Nx / 2) + ystride * (Ny - (id_y + 1))
+ zstride * (Nz / 2),
id_batch + xstride * (Nx / 2) + ystride * (id_y + 1) + zstride * (Nz / 2),
xreal,
ximag);
if(id_x < x_total && id_y < y_total)
conjugate(id_batch + xstride * (Nx - (id_x + 1)) + ystride * (Ny - (id_y + 1))
+ zstride * (Nz / 2),
id_batch + xstride * (id_x + 1) + ystride * (id_y + 1)
+ zstride * (Nz / 2),
xreal,
ximag);
}
}
}
// get grid dimensions for data gen kernel
static dim3 generate_data_gridDim(const size_t isize)
{
auto blockSize = DATA_GEN_THREADS;
// total number of blocks needed in the grid
auto numBlocks_setup = DivRoundingUp<size_t>(isize, blockSize);
// Total work items per dimension in the grid is counted in
// uint32_t. Since each thread initializes one element, very
// large amounts of data will overflow this total size if we do
// all this work in one grid dimension, causing launch failure.
//
// CUDA also generally allows for effectively unlimited grid X
// dim, but Y and Z are more limited.
auto gridDim_y = std::min<unsigned int>(DATA_GEN_GRID_Y_MAX, numBlocks_setup);
auto gridDim_x = DivRoundingUp<unsigned int>(numBlocks_setup, DATA_GEN_GRID_Y_MAX);
return {gridDim_x, gridDim_y};
}
// get grid dimensions for hermitian symmetrizer kernel
static dim3 generate_hermitian_gridDim(const std::vector<size_t>& length,
const size_t batch,
const size_t blockSize)
{
dim3 gridDim;
switch(length.size())
{
case 1:
gridDim = dim3(DivRoundingUp<size_t>(batch, blockSize));
break;
case 2:
gridDim = dim3(DivRoundingUp<size_t>(batch, blockSize),
DivRoundingUp<size_t>((length[0] + 1) / 2 - 1, blockSize));
break;
case 3:
gridDim = dim3(DivRoundingUp<size_t>(batch, blockSize),
DivRoundingUp<size_t>((length[0] + 1) / 2 - 1, blockSize),
DivRoundingUp<size_t>(length[1] - 1, blockSize));
break;
default:
throw std::runtime_error("Invalid dimension for impose_hermitian_symmetry");
}
return gridDim;
}
static dim3 generate_blockDim(const std::vector<size_t>& length, const size_t blockSize)
{
dim3 blockDim;
switch(length.size())
{
case 1:
blockDim = dim3(blockSize);
break;
case 2:
blockDim = dim3(blockSize, blockSize);
break;
case 3:
blockDim = dim3(blockSize, blockSize, blockSize);
break;
default:
throw std::runtime_error("Invalid dimension for impose_hermitian_symmetry");
}
return blockDim;
}
template <typename Tint, typename Treal>
static void generate_random_interleaved_data(const Tint& whole_length,
const size_t idist,
const size_t isize,
const Tint& whole_stride,
rocfft_complex<Treal>* input_data,
const hipDeviceProp_t& deviceProp)
{
auto input_length = get_input_val(whole_length);
auto zero_length = make_zero_length(input_length);
auto input_stride = get_input_val(whole_stride);
dim3 gridDim = generate_data_gridDim(isize);
dim3 blockDim{DATA_GEN_THREADS};
launch_limits_check("generate_random_interleaved_data_kernel", gridDim, blockDim, deviceProp);
hipLaunchKernelGGL(
HIP_KERNEL_NAME(generate_random_interleaved_data_kernel<decltype(input_length), Treal>),
gridDim,
blockDim,
0, // sharedMemBytes
0, // stream
input_length,
zero_length,
idist,
isize,
input_stride,
input_data);
auto err = hipGetLastError();
if(err != hipSuccess)
throw std::runtime_error("generate_random_interleaved_data_kernel launch failure: "
+ std::string(hipGetErrorName(err)));
}
template <typename Tint, typename Treal>
static void generate_interleaved_data(const Tint& whole_length,
const size_t idist,
const size_t isize,
const Tint& whole_stride,
const size_t nbatch,
rocfft_complex<Treal>* input_data,
const hipDeviceProp_t& deviceProp)
{
const auto input_length = get_input_val(whole_length);
const auto input_stride = get_input_val(whole_stride);
const auto unit_stride = make_unit_stride(input_length);
const auto inv_scale
= static_cast<Treal>(1.0)
/ static_cast<Treal>(static_cast<unsigned long long>(isize) / nbatch - 1);
dim3 gridDim = generate_data_gridDim(isize);
dim3 blockDim{DATA_GEN_THREADS};
launch_limits_check("generate_interleaved_data_kernel", gridDim, blockDim, deviceProp);
hipLaunchKernelGGL(
HIP_KERNEL_NAME(generate_interleaved_data_kernel<decltype(input_length), Treal>),
gridDim,
blockDim,
0, // sharedMemBytes
0, // stream
input_length,
idist,
isize,
input_stride,
unit_stride,
inv_scale,
input_data);
auto err = hipGetLastError();
if(err != hipSuccess)
throw std::runtime_error("generate_interleaved_data_kernel launch failure: "
+ std::string(hipGetErrorName(err)));
}
template <typename Tint, typename Treal>
static void generate_random_planar_data(const Tint& whole_length,
const size_t idist,
const size_t isize,
const Tint& whole_stride,
Treal* real_data,
Treal* imag_data,
const hipDeviceProp_t& deviceProp)
{
const auto input_length = get_input_val(whole_length);
const auto zero_length = make_zero_length(input_length);
const auto input_stride = get_input_val(whole_stride);
dim3 gridDim = generate_data_gridDim(isize);
dim3 blockDim{DATA_GEN_THREADS};
launch_limits_check("generate_random_planar_data_kernel", gridDim, blockDim, deviceProp);
hipLaunchKernelGGL(
HIP_KERNEL_NAME(generate_random_planar_data_kernel<decltype(input_length), Treal>),
gridDim,
blockDim,
0, // sharedMemBytes
0, // stream
input_length,
zero_length,
idist,
isize,
input_stride,
real_data,
imag_data);
auto err = hipGetLastError();
if(err != hipSuccess)
throw std::runtime_error("generate_random_planar_data_kernel launch failure: "
+ std::string(hipGetErrorName(err)));
}
template <typename Tint, typename Treal>
static void generate_planar_data(const Tint& whole_length,
const size_t idist,
const size_t isize,
const Tint& whole_stride,
const size_t nbatch,
Treal* real_data,
Treal* imag_data,
const hipDeviceProp_t& deviceProp)
{
const auto input_length = get_input_val(whole_length);
const auto input_stride = get_input_val(whole_stride);
const auto unit_stride = make_unit_stride(input_length);
const auto inv_scale
= static_cast<Treal>(1.0)
/ static_cast<Treal>(static_cast<unsigned long long>(isize) / nbatch - 1);
dim3 gridDim = generate_data_gridDim(isize);
dim3 blockDim{DATA_GEN_THREADS};
launch_limits_check("generate_planar_data_kernel", gridDim, blockDim, deviceProp);
hipLaunchKernelGGL(HIP_KERNEL_NAME(generate_planar_data_kernel<decltype(input_length), Treal>),
gridDim,
blockDim,
0, // sharedMemBytes
0, // stream
input_length,
idist,
isize,
input_stride,
unit_stride,
inv_scale,
real_data,
imag_data);
auto err = hipGetLastError();
if(err != hipSuccess)
throw std::runtime_error("generate_planar_data_kernel launch failure: "
+ std::string(hipGetErrorName(err)));
}
template <typename Tint, typename Treal>
static void generate_random_real_data(const Tint& whole_length,
const size_t idist,
const size_t isize,
const Tint& whole_stride,
Treal* input_data,
const hipDeviceProp_t& deviceProp)
{
const auto input_length = get_input_val(whole_length);
const auto zero_length = make_zero_length(input_length);
const auto input_stride = get_input_val(whole_stride);
dim3 gridDim = generate_data_gridDim(isize);
dim3 blockDim{DATA_GEN_THREADS};
launch_limits_check("generate_random_real_data_kernel", gridDim, blockDim, deviceProp);
hipLaunchKernelGGL(
HIP_KERNEL_NAME(generate_random_real_data_kernel<decltype(input_length), Treal>),
gridDim,
blockDim,
0, // sharedMemBytes
0, // stream
input_length,
zero_length,
idist,
isize,
input_stride,
input_data);
auto err = hipGetLastError();
if(err != hipSuccess)
throw std::runtime_error("generate_random_real_data_kernel launch failure: "
+ std::string(hipGetErrorName(err)));
}
template <typename Tint, typename Treal>
static void generate_real_data(const Tint& whole_length,
const size_t idist,
const size_t isize,
const Tint& whole_stride,
const size_t nbatch,
Treal* input_data,
const hipDeviceProp_t& deviceProp)
{
const auto input_length = get_input_val(whole_length);
const auto input_stride = get_input_val(whole_stride);
const auto unit_stride = make_unit_stride(input_length);
const auto inv_scale
= static_cast<Treal>(1.0)
/ static_cast<Treal>(static_cast<unsigned long long>(isize) / nbatch - 1);
dim3 gridDim = generate_data_gridDim(isize);
dim3 blockDim{DATA_GEN_THREADS};
launch_limits_check("generate_real_data_kernel", gridDim, blockDim, deviceProp);
hipLaunchKernelGGL(HIP_KERNEL_NAME(generate_real_data_kernel<decltype(input_length), Treal>),
gridDim,
blockDim,
0, // sharedMemBytes
0, // stream
input_length,
idist,
isize,
input_stride,
unit_stride,
inv_scale,
input_data);
auto err = hipGetLastError();
if(err != hipSuccess)
throw std::runtime_error("generate_real_data_kernel launch failure: "
+ std::string(hipGetErrorName(err)));
}
template <typename Tcomplex>
static void impose_hermitian_symmetry_interleaved(const std::vector<size_t>& length,
const std::vector<size_t>& ilength,
const std::vector<size_t>& stride,
const size_t dist,
const size_t batch,
Tcomplex* input_data,
const hipDeviceProp_t& deviceProp)
{
auto blockSize = DATA_GEN_THREADS;
auto blockDim = generate_blockDim(length, blockSize);
auto gridDim = generate_hermitian_gridDim(length, batch, blockSize);
switch(length.size())
{
case 1:
{
launch_limits_check(
"impose_hermitian_symmetry_interleaved_1D_kernel", gridDim, blockDim, deviceProp);
hipLaunchKernelGGL(impose_hermitian_symmetry_interleaved_1D_kernel<Tcomplex>,
gridDim,
blockDim,
0,
0,
input_data,
length[0],
stride[0],
dist,
batch,
length[0] % 2 == 0);
break;
}
case 2:
{
launch_limits_check(
"impose_hermitian_symmetry_interleaved_2D_kernel", gridDim, blockDim, deviceProp);
hipLaunchKernelGGL(impose_hermitian_symmetry_interleaved_2D_kernel<Tcomplex>,
gridDim,
blockDim,
0,
0,
input_data,
length[0],
length[1],
stride[0],
stride[1],
dist,
batch,
(ilength[0] + 1) / 2 - 1,
length[0] % 2 == 0,
length[1] % 2 == 0);
break;
}
case 3:
{
launch_limits_check(
"impose_hermitian_symmetry_interleaved_3D_kernel", gridDim, blockDim, deviceProp);
hipLaunchKernelGGL(impose_hermitian_symmetry_interleaved_3D_kernel<Tcomplex>,
gridDim,
blockDim,
0,
0,
input_data,
length[0],
length[1],
length[2],
stride[0],
stride[1],
stride[2],
dist,
batch,
(ilength[0] + 1) / 2 - 1,
ilength[1] - 1,
(ilength[1] + 1) / 2 - 1,
length[0] % 2 == 0,
length[1] % 2 == 0,
length[2] % 2 == 0);
break;
}
default:
throw std::runtime_error("Invalid dimension for impose_hermitian_symmetry");
}
auto err = hipGetLastError();
if(err != hipSuccess)
throw std::runtime_error("impose_hermitian_symmetry_interleaved_kernel launch failure: "
+ std::string(hipGetErrorName(err)));
}
template <typename Tfloat>
static void impose_hermitian_symmetry_planar(const std::vector<size_t>& length,
const std::vector<size_t>& ilength,
const std::vector<size_t>& stride,
const size_t dist,
const size_t batch,
Tfloat* input_data_real,
Tfloat* input_data_imag,
const hipDeviceProp_t& deviceProp)
{
auto blockSize = DATA_GEN_THREADS;
auto blockDim = generate_blockDim(length, blockSize);
auto gridDim = generate_hermitian_gridDim(length, batch, blockSize);
switch(length.size())
{
case 1:
{
launch_limits_check(
"impose_hermitian_symmetry_planar_1D_kernel", gridDim, blockDim, deviceProp);
hipLaunchKernelGGL(impose_hermitian_symmetry_planar_1D_kernel<Tfloat>,
gridDim,
blockDim,
0,
0,
input_data_real,
input_data_imag,
length[0],
stride[0],
dist,
batch,
length[0] % 2 == 0);
break;
}
case 2:
{
launch_limits_check(
"impose_hermitian_symmetry_planar_2D_kernel", gridDim, blockDim, deviceProp);
hipLaunchKernelGGL(impose_hermitian_symmetry_planar_2D_kernel<Tfloat>,
gridDim,
blockDim,
0,
0,
input_data_real,
input_data_imag,
length[0],
length[1],
stride[0],
stride[1],
dist,
batch,
(ilength[0] + 1) / 2 - 1,
length[0] % 2 == 0,
length[1] % 2 == 0);
break;
}
case 3:
{
launch_limits_check(
"impose_hermitian_symmetry_planar_3D_kernel", gridDim, blockDim, deviceProp);
hipLaunchKernelGGL(impose_hermitian_symmetry_planar_3D_kernel<Tfloat>,
gridDim,
blockDim,
0,
0,
input_data_real,
input_data_imag,
length[0],
length[1],
length[2],
stride[0],
stride[1],
stride[2],
dist,
batch,
(ilength[0] + 1) / 2 - 1,
ilength[1] - 1,
(ilength[1] + 1) / 2 - 1,
length[0] % 2 == 0,
length[1] % 2 == 0,
length[2] % 2 == 0);
break;
}
default:
throw std::runtime_error("Invalid dimension for impose_hermitian_symmetry");
}
auto err = hipGetLastError();
if(err != hipSuccess)
throw std::runtime_error("impose_hermitian_symmetry_planar_kernel launch failure: "
+ std::string(hipGetErrorName(err)));
}
#endif // DATA_GEN_DEVICE_H
|