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
|
#include "../gpu.h"
#include "../gpu-complex.h"
#include "numpy/arrayobject.h"
#include "assert.h"
#define BETA 0.066725
#define GAMMA 0.031091
#define MU 0.2195164512208958 // PBE mod in libxc
//#define MU 0.2195149727645171 from libxc
#define C2 0.26053088059892404
#define C0I 0.238732414637843
#define C1 -0.45816529328314287
#define CC1 1.9236610509315362
#define CC2 2.5648814012420482
#define IF2 0.58482236226346462
#define C3 0.10231023756535741
#define C0 4.1887902047863905
#define THIRD 0.33333333333333333
#define NMIN 1.0E-10
template <typename Tcomplex, typename Treal>
__global__ void calculate_residual_kernel(int nG, int nn,
Tcomplex* residual_nG,
Treal* eps_n,
Tcomplex* wf_nG)
{
int n = threadIdx.x + blockIdx.x * blockDim.x;
int g = threadIdx.y + blockIdx.y * blockDim.y;
if ((g < nG) && (n < nn))
{
residual_nG[n*nG + g] = residual_nG[n*nG + g] - wf_nG[n*nG + g] * eps_n[n];
}
}
// This is the [i,j,0] slice of contiguous array
#define MAT(array, nx, ny, nz, b, i, j) (array[(b) * (nx) * (ny) * (nz) + (i) * (ny) * (nz) + (j) * (nz)])
template <typename Tcomplex>
__global__ void pw_amend_insert_realwf(int nb, int nx, int ny, int nz, int n, int m, Tcomplex* array_nQ)
{
int b = threadIdx.x + blockIdx.x * blockDim.x;
int i = threadIdx.y + blockIdx.y * blockDim.y;
if (b < nb)
{
// t[0, -m:] = t[0, m:0:-1].conj()
if (i < m)
{
Tcomplex value = MAT(array_nQ, nx, ny, nz, b, 0, m - i);
value.y = -value.y;
MAT(array_nQ, nx, ny, nz, b, 0, ny - m + i) = value;
}
if (i < n)
{
for (int j=0; j<m; j++)
{
// t[n:0:-1, -m:] = t[-n:, m:0:-1].conj()
Tcomplex value = MAT(array_nQ, nx, ny, nz, b, nx - n + i, m - j);
value.y = -value.y;
MAT(array_nQ, nx, ny, nz, b, n - i, ny - m + j) = value;
// t[-n:, -m:] = t[n:0:-1, m:0:-1].conj()
value = MAT(array_nQ, nx, ny, nz, b, n - i, m - j);
value.y = -value.y;
MAT(array_nQ, nx, ny, nz, b, nx - n + i, ny - m + j) = value;
}
Tcomplex value = MAT(array_nQ, nx, ny, nz, b, n - i, 0);
value.y = -value.y;
MAT(array_nQ, nx, ny, nz, b, nx - n + i, 0) = value;
}
}
}
extern "C"
void calculate_residual_launch_kernel(
int dtypenum,
int nG,
int nn,
void* residual_nG,
void* eps_n,
void* wf_nG)
{
if ((nG == 0) || (nn == 0))
{
return;
}
if (dtypenum==NP_DOUBLE_COMPLEX)
{
auto fptr = &calculate_residual_kernel<gpuDoubleComplex, double>;
gpuLaunchKernel(fptr,
dim3((nn+15)/16, (nG+15)/16),
dim3(16, 16),
0, 0,
nG, nn,
(gpuDoubleComplex*) residual_nG,
(double*) eps_n,
(gpuDoubleComplex*) wf_nG);
}
else if (dtypenum==NP_FLOAT_COMPLEX)
{
auto fptr = &calculate_residual_kernel<gpuFloatComplex, float>;
gpuLaunchKernel(fptr,
dim3((nn+15)/16, (nG+15)/16),
dim3(16, 16),
0, 0,
nG, nn,
(gpuFloatComplex*) residual_nG,
(float*) eps_n,
(gpuFloatComplex*) wf_nG);
} else if (dtypenum==NP_FLOAT)
{
auto fptr = &calculate_residual_kernel<float, float>;
gpuLaunchKernel(fptr,
dim3((nn+15)/16, (nG+15)/16),
dim3(16, 16),
0, 0,
nG, nn,
(float*) residual_nG,
(float*) eps_n,
(float*) wf_nG);
} else if (dtypenum==NP_DOUBLE)
{
auto fptr = &calculate_residual_kernel<double, double>;
gpuLaunchKernel(fptr,
dim3((nn+15)/16, (nG+15)/16),
dim3(16, 16),
0, 0,
nG, nn,
(double*) residual_nG,
(double*) eps_n,
(double*) wf_nG);
}
}
template <bool gga> __device__ double pbe_exchange(double n, double rs, double a2,
double* dedrs, double* deda2)
{
double e = C1 / rs;
*dedrs = -e / rs;
if (gga)
{
double kappa = 0.804;
double c = C2 * rs / n;
c *= c;
double s2 = a2 * c;
double x = 1.0 + MU * s2 / kappa;
double Fx = 1.0 + kappa - kappa / x;
double dFxds2 = MU / (x * x);
double ds2drs = 8.0 * c * a2 / rs;
*dedrs = *dedrs * Fx + e * dFxds2 * ds2drs;
*deda2 = e * dFxds2 * c;
e *= Fx;
}
return e;
}
__device__ double G(double rtrs, double A, double alpha1,
double beta1, double beta2, double beta3, double beta4,
double* dGdrs)
{
double Q0 = -2.0 * A * (1.0 + alpha1 * rtrs * rtrs);
double Q1 = 2.0 * A * rtrs * (beta1 +
rtrs * (beta2 +
rtrs * (beta3 +
rtrs * beta4)));
double G1 = Q0 * log(1.0 + 1.0 / Q1);
double dQ1drs = A * (beta1 / rtrs + 2.0 * beta2 +
rtrs * (3.0 * beta3 + 4.0 * beta4 * rtrs));
*dGdrs = -2.0 * A * alpha1 * G1 / Q0 - Q0 * dQ1drs / (Q1 * (Q1 + 1.0));
return G1;
}
template <bool gga, int nspin> __device__ double pbe_correlation(double n, double rs, double zeta, double a2,
double* dedrs, double* dedzeta, double* deda2)
{
bool spinpol = nspin == 2;
double rtrs = sqrt(rs);
double de0drs;
double e0 = G(rtrs, GAMMA, 0.21370, 7.5957, 3.5876, 1.6382, 0.49294,
&de0drs);
double e;
double xp = 117.0;
double xm = 117.0;
if (spinpol)
{
double de1drs;
double e1 = G(rtrs, 0.015545, 0.20548, 14.1189, 6.1977, 3.3662,
0.62517, &de1drs);
double dalphadrs;
double alpha = -G(rtrs, 0.016887, 0.11125, 10.357, 3.6231, 0.88026,
0.49671, &dalphadrs);
dalphadrs = -dalphadrs;
double zp = 1.0 + zeta;
double zm = 1.0 - zeta;
xp = pow(zp, THIRD);
xm = pow(zm, THIRD);
double f = CC1 * (zp * xp + zm * xm - 2.0);
double f1 = CC2 * (xp - xm);
double zeta2 = zeta * zeta;
double zeta3 = zeta2 * zeta;
double zeta4 = zeta2 * zeta2;
double x = 1.0 - zeta4;
*dedrs = (de0drs * (1.0 - f * zeta4) +
de1drs * f * zeta4 +
dalphadrs * f * x * IF2);
*dedzeta = (4.0 * zeta3 * f * (e1 - e0 - alpha * IF2) +
f1 * (zeta4 * e1 - zeta4 * e0 + x * alpha * IF2));
e = e0 + alpha * IF2 * f * x + (e1 - e0) * f * zeta4;
}
else
{
*dedrs = de0drs;
e = e0;
}
if (gga)
{
double n2 = n * n;
double t2;
double y;
double phi = 117.0;
double phi2 = 117.0;
double phi3 = 117.0;
if (spinpol)
{
phi = 0.5 * (xp * xp + xm * xm);
phi2 = phi * phi;
phi3 = phi * phi2;
t2 = C3 * a2 * rs / (n2 * phi2);
y = -e / (GAMMA * phi3);
}
else
{
t2 = C3 * a2 * rs / n2;
y = -e / GAMMA;
}
double x = exp(y);
double A;
if (x != 1.0)
A = BETA / (GAMMA * (x - 1.0));
else
A = BETA / (GAMMA * y);
double At2 = A * t2;
double nom = 1.0 + At2;
double denom = nom + At2 * At2;
double H = GAMMA * log( 1.0 + BETA * t2 * nom / (denom * GAMMA));
double tmp = (GAMMA * BETA /
(denom * (BETA * t2 * nom + GAMMA * denom)));
double tmp2 = A * A * x / BETA;
double dAdrs = tmp2 * *dedrs;
if (spinpol)
{
H *= phi3;
tmp *= phi3;
dAdrs /= phi3;
}
double dHdt2 = (1.0 + 2.0 * At2) * tmp;
double dHdA = -At2 * t2 * t2 * (2.0 + At2) * tmp;
*dedrs += dHdt2 * 7 * t2 / rs + dHdA * dAdrs;
*deda2 = dHdt2 * C3 * rs / n2;
if (spinpol)
{
double dphidzeta = (1.0 / xp - 1.0 / xm) / 3.0;
double dAdzeta = tmp2 * (*dedzeta -
3.0 * e * dphidzeta / phi) / phi3;
*dedzeta += ((3.0 * H / phi - dHdt2 * 2.0 * t2 / phi ) * dphidzeta +
dHdA * dAdzeta);
*deda2 /= phi2;
}
e += H;
}
return e;
}
template <int nspin, bool gga> __global__ void evaluate_ldaorgga_kernel(int ng,
double* n_sg,
double* v_sg,
double* e_g,
double* sigma_xg,
double* dedsigma_xg)
{
int g = threadIdx.x + blockIdx.x * blockDim.x;
if (g >= ng)
{
return;
}
if (nspin == 1)
{
double n = n_sg[g];
if (n < NMIN)
n = NMIN;
double rs = pow(C0I / n, THIRD);
double dexdrs;
double dexda2;
double ex;
double decdrs;
double decda2;
double ec;
if (gga)
{
double a2 = sigma_xg[g];
ex = pbe_exchange<gga>(n, rs, a2, &dexdrs, &dexda2);
ec = pbe_correlation<gga, nspin>(n, rs, 0.0, a2, &decdrs, 0, &decda2);
dedsigma_xg[g] = n * (dexda2 + decda2);
}
else
{
ex = pbe_exchange<gga>(n, rs, 0.0, &dexdrs, 0);
ec = pbe_correlation<gga, nspin>(n, rs, 0.0, 0.0, &decdrs, 0, 0);
}
e_g[g] = n * (ex + ec);
v_sg[g] += ex + ec - rs * (dexdrs + decdrs) / 3.0;
}
else
{
const double* na_g = n_sg;
double* va_g = v_sg;
const double* nb_g = na_g + ng;
double* vb_g = va_g + ng;
const double* sigma0_g = 0;
const double* sigma1_g = 0;
const double* sigma2_g = 0;
double* dedsigma0_g = 0;
double* dedsigma1_g = 0;
double* dedsigma2_g = 0;
if (gga)
{
sigma0_g = sigma_xg;
sigma1_g = sigma0_g + ng;
sigma2_g = sigma1_g + ng;
dedsigma0_g = dedsigma_xg;
dedsigma1_g = dedsigma0_g + ng;
dedsigma2_g = dedsigma1_g + ng;
}
double na = 2.0 * na_g[g];
if (na < NMIN)
na = NMIN;
double rsa = pow(C0I / na, THIRD);
double nb = 2.0 * nb_g[g];
if (nb < NMIN)
nb = NMIN;
double rsb = pow(C0I / nb, THIRD);
double n = 0.5 * (na + nb);
double rs = pow(C0I / n, THIRD);
double zeta = 0.5 * (na - nb) / n;
double dexadrs;
double dexada2;
double exa;
double dexbdrs;
double dexbda2;
double exb;
double decdrs;
double decdzeta;
double decda2;
double ec;
if (gga)
{
exa = pbe_exchange<gga>(na, rsa, 4.0 * sigma0_g[g],
&dexadrs, &dexada2);
exb = pbe_exchange<gga>(nb, rsb, 4.0 * sigma2_g[g],
&dexbdrs, &dexbda2);
double a2 = sigma0_g[g] + 2 * sigma1_g[g] + sigma2_g[g];
ec = pbe_correlation<gga, nspin>(n, rs, zeta, a2,
&decdrs, &decdzeta, &decda2);
dedsigma0_g[g] = 2 * na * dexada2 + n * decda2;
dedsigma1_g[g] = 2 * n * decda2;
dedsigma2_g[g] = 2 * nb * dexbda2 + n * decda2;
}
else
{
exa = pbe_exchange<gga>(na, rsa, 0.0, &dexadrs, 0);
exb = pbe_exchange<gga>(nb, rsb, 0.0, &dexbdrs, 0);
ec = pbe_correlation<gga, nspin>(n, rs, zeta, 0.0,
&decdrs, &decdzeta, 0);
}
e_g[g] = 0.5 * (na * exa + nb * exb) + n * ec;
va_g[g] += (exa + ec -
(rsa * dexadrs + rs * decdrs) / 3.0 -
(zeta - 1.0) * decdzeta);
vb_g[g] += (exb + ec -
(rsb * dexbdrs + rs * decdrs) / 3.0 -
(zeta + 1.0) * decdzeta);
}
}
// The define wrappers do not allow special characters for the first argument
// Hence, here defining an expression in such way, that the first argument can be
// a well defined identifier, and the preprocessor macro parses it correctly.
constexpr void(*LDA_SPINPAIRED)(int, double*, double*, double*, double*, double*) = &evaluate_ldaorgga_kernel<1, false>;
constexpr void(*LDA_SPINPOLARIZED)(int, double*, double*, double*, double*, double*) = &evaluate_ldaorgga_kernel<2, false>;
constexpr void(*PBE_SPINPAIRED)(int, double*, double*, double*, double*, double*) = &evaluate_ldaorgga_kernel<1, true>;
constexpr void(*PBE_SPINPOLARIZED)(int, double*, double*, double*, double*, double*) = &evaluate_ldaorgga_kernel<2, true>;
extern "C"
void evaluate_pbe_launch_kernel(int nspin, int ng,
double* n,
double* v,
double* e,
double* sigma,
double* dedsigma)
{
if (nspin == 1)
{
gpuLaunchKernel(PBE_SPINPAIRED,
dim3((ng+255)/256),
dim3(256),
0, 0,
ng,
n, v, e, sigma, dedsigma);
}
else if (nspin == 2)
{
gpuLaunchKernel(PBE_SPINPOLARIZED,
dim3((ng+255)/256),
dim3(256),
0, 0,
ng,
n, v, e, sigma, dedsigma);
}
}
extern "C"
void evaluate_lda_launch_kernel(int nspin, int ng,
double* n,
double* v,
double* e)
{
if (nspin == 1)
{
gpuLaunchKernel(LDA_SPINPAIRED,
dim3((ng+255)/256),
dim3(256),
0, 0,
ng,
n, v, e, NULL, NULL);
}
else if (nspin == 2)
{
gpuLaunchKernel(LDA_SPINPOLARIZED,
dim3((ng+255)/256),
dim3(256),
0, 0,
ng,
n, v, e, NULL, NULL);
}
}
template <typename Tcomplex, typename Treal>
__global__ void pw_insert_many(int nb,
int nG,
int nQ,
Tcomplex* c_nG,
npy_int32* Q_G,
Treal scale,
Tcomplex* tmp_nQ)
{
int G = threadIdx.x + blockIdx.x * blockDim.x;
int b = threadIdx.y + blockIdx.y * blockDim.y;
__shared__ npy_int32 locQ_G[16];
if (threadIdx.y == 0)
locQ_G[threadIdx.x] = Q_G[G];
__syncthreads();
if ((G < nG) && (b < nb))
{
npy_int32 Q = locQ_G[threadIdx.x];
tmp_nQ[Q + b * nQ] = c_nG[G + b * nG] * scale;
}
}
template <typename Tcomplex, typename Treal>
__global__ void add_to_density(int nb,
int nR,
double* f_n,
Tcomplex* psit_nR,
double* rho_R)
{
constexpr bool realtype = std::is_same<Tcomplex, Treal>::value;
int R = threadIdx.x + blockIdx.x * blockDim.x;
if (R < nR)
{
double rho = 0.0;
for (int b=0; b< nb; b++)
{
int idx = b * nR + R;
if constexpr(realtype) {
rho += f_n[b] * double(psit_nR[idx] * psit_nR[idx]);
} else {
rho += f_n[b] * double(psit_nR[idx].x * psit_nR[idx].x + psit_nR[idx].y * psit_nR[idx].y);
}
}
rho_R[R] += rho;
}
}
template <typename Tcomplex, typename Treal>
__global__ void pw_insert(int nG,
int nQ,
Tcomplex* c_G,
npy_int32* Q_G,
Treal scale,
Tcomplex* tmp_Q)
{
int G = threadIdx.x + blockIdx.x * blockDim.x;
if (G < nG)
tmp_Q[Q_G[G]] = c_G[G] * scale;
}
extern "C" void gpawDeviceSynchronize()
{
gpuDeviceSynchronize();
}
extern "C"
void add_to_density_gpu_launch_kernel(int nb,
int nR,
double* f_n,
void* psit_nR,
double* rho_R,
int dtypenum)
{
if (dtypenum==NP_DOUBLE_COMPLEX)
{
auto fptr = &add_to_density<gpuDoubleComplex, double>;
gpuLaunchKernel(fptr, dim3((nR+255)/256), dim3(256), 0, 0,
nb, nR, f_n, (gpuDoubleComplex*)psit_nR, rho_R);
}
else if (dtypenum==NP_FLOAT_COMPLEX)
{
auto fptr = &add_to_density<gpuFloatComplex, float>;
gpuLaunchKernel(fptr, dim3((nR+255)/256), dim3(256), 0, 0,
nb, nR, f_n, (gpuFloatComplex*)psit_nR, rho_R);
} else if (dtypenum==NP_FLOAT)
{
auto fptr = &add_to_density<float, float>;
gpuLaunchKernel(fptr, dim3((nR+255)/256), dim3(256), 0, 0,
nb, nR, f_n, (float*) psit_nR, rho_R);
} else if (dtypenum==NP_DOUBLE)
{
auto fptr = &add_to_density<double, double>;
gpuLaunchKernel(fptr, dim3((nR+255)/256), dim3(256), 0, 0,
nb, nR, f_n, (double*) psit_nR, rho_R);
}
else
{
assert(0);
}
}
extern "C"
void pw_amend_insert_realwf_gpu_launch_kernel(int dtypenum,
int nb,
int nx,
int ny,
int nz,
int n,
int m,
void* array_nQ)
{
if (dtypenum == NP_DOUBLE_COMPLEX)
{
auto fptr = &pw_amend_insert_realwf<gpuDoubleComplex>;
gpuLaunchKernel(fptr,
dim3((nb+15)/16, (max(n,m)+15)/16),
dim3(16, 16),
0, 0,
nb, nx, ny, nz, n, m, (gpuDoubleComplex*) array_nQ);
} else if (dtypenum == NP_FLOAT_COMPLEX)
{
auto fptr = &pw_amend_insert_realwf<gpuFloatComplex>;
gpuLaunchKernel(fptr,
dim3((nb+15)/16, (max(n,m)+15)/16),
dim3(16, 16),
0, 0,
nb, nx, ny, nz, n, m, (gpuFloatComplex*) array_nQ);
} else {
assert(0);
}
}
extern "C"
void pw_insert_gpu_launch_kernel(
int dtypenum,
int nb,
int nG,
int nQ,
void* c_nG,
npy_int32* Q_G,
double scale,
void* tmp_nQ,
int rx, int ry, int rz)
{
if (nb == 1)
{
if (dtypenum == NP_DOUBLE_COMPLEX) { // Double Complex
auto fptr = &pw_insert<gpuDoubleComplex, double>;
gpuLaunchKernel(fptr,
dim3((nG+15)/16, (nb+15)/16),
dim3(16, 16),
0, 0,
nG, nQ,
(gpuDoubleComplex*) c_nG,
Q_G,
scale,
(gpuDoubleComplex*) tmp_nQ);
}
else if (dtypenum == NP_FLOAT_COMPLEX) { // Float Complex
auto fptr = &pw_insert<gpuFloatComplex, float>;
gpuLaunchKernel(fptr,
dim3((nG+15)/16, (nb+15)/16),
dim3(16, 16),
0, 0,
nG, nQ,
(gpuFloatComplex*) c_nG,
Q_G,
float(scale),
(gpuFloatComplex*) tmp_nQ);
} else assert(0);
}
else
{
if (dtypenum == NP_DOUBLE_COMPLEX) { // Double Complex
auto fptr = &pw_insert_many<gpuDoubleComplex, double>;
gpuLaunchKernel(fptr,
dim3((nG+15)/16, (nb+15)/16),
dim3(16, 16),
0, 0,
nb, nG, nQ,
(gpuDoubleComplex*) c_nG,
Q_G,
scale,
(gpuDoubleComplex*) tmp_nQ);
}
else if (dtypenum == NP_FLOAT_COMPLEX) { // Float Complex
auto fptr = &pw_insert_many<gpuFloatComplex, float>;
gpuLaunchKernel(fptr,
dim3((nG+15)/16, (nb+15)/16),
dim3(16, 16),
0, 0,
nb, nG, nQ,
(gpuFloatComplex*) c_nG,
Q_G,
float(scale),
(gpuFloatComplex*) tmp_nQ);
} else assert(0);
}
// We identify real wave functions by noting that number of cartesian planewaves
// does not equal to real space grid size (because z_Q <- z_R // 2 + 1)
if (rx * ry * rz != nQ)
{
int n = rx / 2 - 1;
int m = ry / 2 - 1;
// The rx, ry, rz are the sizes of the 3D version of Q array. Since
// we are dealing with real wave functions, the convention is that
// the last axis is actually z_R // 2 + 1.
if (dtypenum == NP_DOUBLE_COMPLEX) { // Double Complex
auto fptr = &pw_amend_insert_realwf<gpuDoubleComplex>;
gpuLaunchKernel(fptr,
dim3((nb+15)/16, (max(n,m)+15)/16),
dim3(16, 16),
0, 0,
nb, rx, ry, rz / 2 + 1, n, m, (gpuDoubleComplex*) tmp_nQ);
} else if (dtypenum == NP_FLOAT_COMPLEX) { // Float Complex
auto fptr = &pw_amend_insert_realwf<gpuFloatComplex>;
gpuLaunchKernel(fptr,
dim3((nb+15)/16, (max(n,m)+15)/16),
dim3(16, 16),
0, 0,
nb, rx, ry, rz / 2 + 1, n, m, (gpuFloatComplex*) tmp_nQ);
}
}
}
template <typename Tcomplex, typename Treal, bool strided, bool cc>
__global__ void pwlfc_expand_kernel(Treal* f_Gs,
Treal* Gk_Gv,
Treal* pos_av,
Tcomplex* eikR_a,
Treal *Y_GL,
int* l_s,
int* a_J,
int* s_J,
int* I_J,
Treal* f_GI,
int nG,
int nJ,
int nL,
int nI,
int natoms,
int nsplines)
{
int G = threadIdx.x + blockIdx.x * blockDim.x;
int J = threadIdx.y + blockIdx.y * blockDim.y;
__shared__ Tcomplex imag_powers[4];
if (threadIdx.y == 0 && threadIdx.x == 0)
imag_powers[0] = {1.0,0};
if (threadIdx.y == 0 && threadIdx.x == 1)
imag_powers[1] = {0,-1.0};
if (threadIdx.y == 0 && threadIdx.x == 2)
imag_powers[2] = {-1.0,0};
if (threadIdx.y == 0 && threadIdx.x == 3)
imag_powers[3] = {0,1.0};
__syncthreads();
//Tcomplex imag_powers[4] = {{1.0,0},{0,-1.0},{-1.0,0},{0,1.0}};
if ((G < nG) && (J < nJ))
{
f_Gs += G*nsplines;
Gk_Gv += G*3;
pos_av += a_J[J]*3;
Treal GkPos = (Gk_Gv[0] * pos_av[0] +
Gk_Gv[1] * pos_av[1] +
Gk_Gv[2] * pos_av[2]);
Tcomplex emiGR = {cos(GkPos), -sin(GkPos)};
int s = s_J[J];
int l = l_s[s];
Y_GL += G*nL + l*l;
Tcomplex f1 = emiGR * eikR_a[a_J[J]] * imag_powers[l % 4] * f_Gs[s];
if constexpr(strided) {
f_GI += G*nI*2 + I_J[J];
for (int m = 0; m < 2 * l + 1; m++) {
Tcomplex f = f1 * Y_GL[m];
f_GI[0] = f.x;
if constexpr(cc)
f_GI[nI] = -f.y;
else
f_GI[nI] = f.y;
f_GI++;
}
} else {
f_GI += (G*nI + I_J[J])*2;
for (int m = 0; m < 2 * l + 1; m++) {
Tcomplex f = f1 * Y_GL[m];
*f_GI++ = f.x;
if constexpr(cc)
*f_GI++ = -f.y;
else
*f_GI++ = f.y;
//*f_GI++ = cc ? -f.y : f.y;
}
}
}
}
template <typename Tcomplex, typename Treal>
__global__ void dH_aii_times_P_ani(int nA, int nn, int nI,
npy_int32* ni_a,
Treal* dH_aii_dev,
Tcomplex* P_ani_dev,
Tcomplex* outP_ani_dev)
{
int n1 = threadIdx.x + blockIdx.x * blockDim.x;
if (n1 < nn) {
Treal* dH_ii = dH_aii_dev;
int I = 0;
for (int a=0; a< nA; a++)
{
int ni = ni_a[a];
int Istart = I;
for (int i=0; i< ni; i++)
{
Tcomplex* outP_ni = outP_ani_dev + n1 * nI + I;
Tcomplex result;
if constexpr (std::is_same<Tcomplex, Treal>::value) {
result = 0.0;
} else {
result = {0.0, 0.0};
}
Tcomplex* P_ni = P_ani_dev + n1 * nI + Istart;
for (int i2=0; i2 < ni; i2++)
{
result = result + *P_ni * dH_ii[i2 * ni + i];
P_ni++;
}
*outP_ni = result;
//outP_ni->x = result.x;
//outP_ni->y = result.y;
I++;
}
dH_ii += ni * ni;
}
}
}
template <unsigned int blockSize, typename Treal>
__device__ void warpReduce(volatile Treal *sdata, unsigned int tid) {
if (blockSize >= 64) sdata[tid] += sdata[tid + 32];
if (blockSize >= 32) sdata[tid] += sdata[tid + 16];
if (blockSize >= 16) sdata[tid] += sdata[tid + 8];
if (blockSize >= 8) sdata[tid] += sdata[tid + 4];
if (blockSize >= 4) sdata[tid] += sdata[tid + 2];
if (blockSize >= 2) sdata[tid] += sdata[tid + 1];
}
// One block will always sum one G-vector. Thus, no block wide reduce.
template <unsigned int blockSize, typename Treal>
__global__ void pw_norm_kinetic_kernel(int nx, int nG,
Treal* result_x,
Treal* C_xG,
Treal* kin_G)
{
// Double check this line (and next)
extern __shared__ __align__(sizeof(double)) unsigned char my_sdata[];
Treal *sdata = reinterpret_cast<Treal *>(my_sdata);
unsigned int tid = threadIdx.x;
sdata[tid] = 0;
unsigned int x = blockIdx.x;
Treal* C_G = C_xG + (x * nG * 2); // C_xG is a Treal complex array
unsigned int i = tid;
while (i < nG)
{
Treal kin_i = kin_G[i] * (C_G[i*2] * C_G[i*2] + C_G[i*2+1] * C_G[i*2+1]);
sdata[tid] += kin_i;
i += blockSize;
}
__syncthreads();
if (blockSize >= 512) { if (tid < 256) { sdata[tid] += sdata[tid + 256]; } __syncthreads(); }
if (blockSize >= 256) { if (tid < 128) { sdata[tid] += sdata[tid + 128]; } __syncthreads(); }
if (blockSize >= 128) { if (tid < 64) { sdata[tid] += sdata[tid + 64]; } __syncthreads(); }
if (tid < 32) warpReduce<blockSize, Treal>(sdata, tid);
if (tid == 0) result_x[x] = sdata[0];
}
template <unsigned int blockSize, typename Treal>
__global__ void pw_norm_kernel(int nx, int nG,
Treal* result_x,
Treal* C_xG)
{
extern __shared__ __align__(sizeof(double)) unsigned char my_sdata[];
Treal *sdata = reinterpret_cast<Treal *>(my_sdata);
unsigned int tid = threadIdx.x;
sdata[tid] = 0;
unsigned int x = blockIdx.x;
Treal* C_G = C_xG + (x * nG * 2); // C_xG is a double complex array
unsigned int i = tid;
while (i < nG)
{
Treal kin_i = C_G[i*2] * C_G[i*2] + C_G[i*2+1] * C_G[i*2+1];
sdata[tid] += kin_i;
i += blockSize;
}
__syncthreads();
if (blockSize >= 512) { if (tid < 256) { sdata[tid] += sdata[tid + 256]; } __syncthreads(); }
if (blockSize >= 256) { if (tid < 128) { sdata[tid] += sdata[tid + 128]; } __syncthreads(); }
if (blockSize >= 128) { if (tid < 64) { sdata[tid] += sdata[tid + 64]; } __syncthreads(); }
if (tid < 32) warpReduce<blockSize, Treal>(sdata, tid);
if (tid == 0) result_x[x] = sdata[0];
}
extern "C"
void dH_aii_times_P_ani_launch_kernel(int dtypenum,
int nA, int nn,
int nI, npy_int32* ni_a,
void* dH_aii_dev,
void* P_ani_dev,
void* outP_ani_dev)
{
if (dtypenum == NP_DOUBLE_COMPLEX)
{
auto fptr = &dH_aii_times_P_ani<gpuDoubleComplex, double>;
gpuLaunchKernel(fptr,
dim3((nn+255)/256),
dim3(256),
0, 0,
nA, nn, nI, ni_a,
(double*) dH_aii_dev,
(gpuDoubleComplex*) P_ani_dev,
(gpuDoubleComplex*) outP_ani_dev);
}
else if (dtypenum == NP_FLOAT_COMPLEX)
{
auto fptr = &dH_aii_times_P_ani<gpuFloatComplex, float>;
gpuLaunchKernel(fptr,
dim3((nn+255)/256),
dim3(256),
0, 0,
nA, nn, nI, ni_a,
(float*) dH_aii_dev,
(gpuFloatComplex*) P_ani_dev,
(gpuFloatComplex*) outP_ani_dev);
}
else if (dtypenum == NP_DOUBLE)
{
auto fptr = &dH_aii_times_P_ani<double, double>;
gpuLaunchKernel(fptr,
dim3((nn+255)/256),
dim3(256),
0, 0,
nA, nn, nI, ni_a,
(double*) dH_aii_dev,
(double*) P_ani_dev,
(double*) outP_ani_dev);
}
else if (dtypenum == NP_FLOAT)
{
auto fptr = &dH_aii_times_P_ani<float, float>;
gpuLaunchKernel(fptr,
dim3((nn+255)/256),
dim3(256),
0, 0,
nA, nn, nI, ni_a,
(float*) dH_aii_dev,
(float*) P_ani_dev,
(float*) outP_ani_dev);
}
else assert(0);
}
extern "C" void pw_norm_gpu_launch_kernel(int dtypenum,
int nx, int nG,
void* result_x,
void* C_xG)
{
if (dtypenum == NP_DOUBLE_COMPLEX) {
auto fptr = &pw_norm_kernel<512, double>;
gpuLaunchKernel(fptr,
dim3(nx, 1),
dim3(512, 1),
sizeof(double) * 512, 0,
nx,
nG,
(double*) result_x,
(double*) C_xG);
} else if (dtypenum == NP_FLOAT_COMPLEX) {
auto fptr = &pw_norm_kernel<512, float>;
gpuLaunchKernel(fptr,
dim3(nx, 1),
dim3(512, 1),
sizeof(double) * 512, 0,
nx,
nG,
(float*) result_x,
(float*) C_xG);
} else assert(0);
}
extern "C" void pw_norm_kinetic_gpu_launch_kernel(int dtypenum,
int nx, int nG,
void* result_x,
void* C_xG,
void* kin_G)
{
if (dtypenum == NP_DOUBLE_COMPLEX) {
auto fptr = &pw_norm_kinetic_kernel<512, double>;
gpuLaunchKernel(fptr,
dim3(nx, 1),
dim3(512, 1),
sizeof(double) * 512, 0,
nx,
nG,
(double*) result_x,
(double*) C_xG,
(double*) kin_G);
} else if (dtypenum == NP_FLOAT_COMPLEX) {
auto fptr = &pw_norm_kinetic_kernel<512, float>;
gpuLaunchKernel(fptr,
dim3(nx, 1),
dim3(512, 1),
sizeof(double) * 512, 0,
nx,
nG,
(float*) result_x,
(float*) C_xG,
(float*) kin_G);
} else {
assert(0);
}
}
extern "C"
void pwlfc_expand_gpu_launch_kernel(int dtypenum,
void* f_Gs,
void* Gk_Gv,
void* pos_av,
void* eikR_a,
void *Y_GL,
int* l_s,
int* a_J,
int* s_J,
void* f_GI,
int* I_J,
int nG,
int nJ,
int nL,
int nI,
int natoms,
int nsplines,
bool cc)
{
if (dtypenum == NP_DOUBLE_COMPLEX) // Double Complex
{
auto fptr = &pwlfc_expand_kernel<gpuDoubleComplex, double, false, false>;
if (cc)
fptr = &pwlfc_expand_kernel<gpuDoubleComplex, double, false, true>;
gpuLaunchKernel(fptr,
dim3((nG+15)/16, (nJ+15)/16), // blockDimX must be > 4 due to shared initialization,
dim3(16, 16),
0, 0,
(double*) f_Gs,
(double*) Gk_Gv,
(double*) pos_av,
(gpuDoubleComplex*) eikR_a,
(double*) Y_GL,
l_s,
a_J,
s_J,
I_J,
(double*) f_GI,
nG,
nJ,
nL,
nI,
natoms,
nsplines);
}
else if(dtypenum == NP_DOUBLE) // Double Real
{
auto fptr = &pwlfc_expand_kernel<gpuDoubleComplex, double, true, false>;
if (cc)
fptr = &pwlfc_expand_kernel<gpuDoubleComplex, double, true, true>;
gpuLaunchKernel(fptr,
dim3((nG+15)/16, (nJ+15)/16), // blockDimX must be > 4 due to shared initialization,
dim3(16, 16),
0, 0,
(double*) f_Gs,
(double*) Gk_Gv,
(double*) pos_av,
(gpuDoubleComplex*) eikR_a,
(double*) Y_GL,
l_s,
a_J,
s_J,
I_J,
(double*) f_GI,
nG,
nJ,
nL,
nI,
natoms,
nsplines);
} else if (dtypenum == NP_FLOAT_COMPLEX) // Float Complex
{
auto fptr = &pwlfc_expand_kernel<gpuFloatComplex, float, false, false>;
if (cc)
fptr = &pwlfc_expand_kernel<gpuFloatComplex, float, false, true>;
gpuLaunchKernel(fptr,
dim3((nG+15)/16, (nJ+15)/16), // blockDimX must be > 4 due to shared initialization,
dim3(16, 16),
0, 0,
(float*) f_Gs,
(float*) Gk_Gv,
(float*) pos_av,
(gpuFloatComplex*) eikR_a,
(float*) Y_GL,
l_s,
a_J,
s_J,
I_J,
(float*) f_GI,
nG,
nJ,
nL,
nI,
natoms,
nsplines);
} else if (dtypenum == NP_FLOAT) // Float Real
{
auto fptr = &pwlfc_expand_kernel<gpuFloatComplex, float, true, false>;
if (cc)
fptr = &pwlfc_expand_kernel<gpuFloatComplex, float, true, true>;
gpuLaunchKernel(fptr,
dim3((nG+15)/16, (nJ+15)/16), // blockDimX must be > 4 due to shared initialization,
dim3(16, 16),
0, 0,
(float*) f_Gs,
(float*) Gk_Gv,
(float*) pos_av,
(gpuFloatComplex*) eikR_a,
(float*) Y_GL,
l_s,
a_J,
s_J,
I_J,
(float*) f_GI,
nG,
nJ,
nL,
nI,
natoms,
nsplines);
}
//gpuDeviceSynchronize();
}
|