rdf.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright (c) 2021 NVIDIA Corporation. All rights reserved.
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cmath>
  6. #include <cstring>
  7. #include <cstdio>
  8. #include <iomanip>
  9. #include "dcdread.h"
  10. #include <assert.h>
  11. #include <Kokkos_Core.hpp> // Note:: Included the Kokkos core library
  12. #include <nvToolsExt.h>
  13. int l_round(float num);
  14. //Todo: Fill the correct data type and dimensions in the code
  15. typedef Kokkos::View<Fill here> view_type_double;
  16. typedef Kokkos::View<Fill here> view_type_long;
  17. typedef view_type_double::HostMirror host_view_type_double;
  18. typedef view_type_long::HostMirror host_view_type_long;
  19. void pair_gpu(view_type_double d_x, view_type_double d_y, view_type_double d_z,
  20. view_type_long d_g2, int numatm, int nconf,
  21. const double xbox, const double ybox, const double zbox,
  22. int d_bin);
  23. int main(int argc, char *argv[])
  24. {
  25. //Note:: We are initailizing the Kokkos library before calling any Kokkos API
  26. Kokkos::initialize(argc, argv);
  27. {
  28. //Note: This will print the default execution space with which Kokkos library was built
  29. printf("Default Kokkos execution space %s\n",
  30. typeid(Kokkos::DefaultExecutionSpace).name());
  31. double xbox, ybox, zbox;
  32. int nbin;
  33. int numatm, nconf, inconf;
  34. string file;
  35. ///////////////////////////////////////////////////////////////
  36. inconf = 10;
  37. nbin = 2000;
  38. file = "../input/alk.traj.dcd";
  39. ///////////////////////////////////////
  40. std::ifstream infile;
  41. infile.open(file.c_str());
  42. if (!infile)
  43. {
  44. cout << "file " << file.c_str() << " not found\n";
  45. return 1;
  46. }
  47. assert(infile);
  48. ofstream pairfile, stwo;
  49. pairfile.open("RDF.dat");
  50. stwo.open("Pair_entropy.dat");
  51. /////////////////////////////////////////////////////////
  52. dcdreadhead(&numatm, &nconf, infile);
  53. cout << "Dcd file has " << numatm << " atoms and " << nconf << " frames" << endl;
  54. if (inconf > nconf)
  55. cout << "nconf is reset to " << nconf << endl;
  56. else
  57. {
  58. nconf = inconf;
  59. }
  60. cout << "Calculating RDF for " << nconf << " frames" << endl;
  61. ////////////////////////////////////////////////////////
  62. //Todo: Fill the correct dimension is view type. This is where the allocation on default Memory space will occur
  63. view_type_double x("x", Fill here);
  64. view_type_double y("y", Fill here);
  65. view_type_double z("z", Fill here);
  66. view_type_long g2("g2", Fill here);
  67. //Todo : Fill the right mirror image variabe here
  68. host_view_type_double h_x = Kokkos::create_mirror_view(x);
  69. host_view_type_double h_y = Kokkos::create_mirror_view(Fill here);
  70. host_view_type_double h_z = Kokkos::create_mirror_view(Fill here);
  71. host_view_type_long h_g2 = Kokkos::create_mirror_view(Fill here);
  72. /////////reading cordinates//////////////////////////////////////////////
  73. nvtxRangePush("Read_File");
  74. double ax[numatm], ay[numatm], az[numatm];
  75. for (int i = 0; i < nconf; i++)
  76. {
  77. dcdreadframe(ax, ay, az, infile, numatm, xbox, ybox, zbox);
  78. for (int j = 0; j < numatm; j++)
  79. {
  80. h_x(i * numatm + j) = ax[j];
  81. h_y(i * numatm + j) = ay[j];
  82. h_z(i * numatm + j) = az[j];
  83. }
  84. }
  85. for (int i = 0; i < nbin; i++)
  86. h_g2(0) = 0;
  87. nvtxRangePop(); //pop for Reading file
  88. cout << "Reading of input file is completed" << endl;
  89. nvtxRangePush("Pair_Calculation");
  90. //Todo: Copy from Host to device h_x->x,h_y->y, h_z-> z and h_g2->g2
  91. Kokkos::deep_copy(Fill Destination View, Fill Source View);
  92. Kokkos::deep_copy(Fill Destination View, Fill Source View);
  93. Kokkos::deep_copy(Fill Destination View, Fill Source View);
  94. Kokkos::deep_copy(Fill Destination View, Fill Source View);
  95. //////////////////////////////////////////////////////////////////////////
  96. pair_gpu(x, y, z, g2, numatm, nconf, xbox, ybox, zbox, nbin);
  97. //Todo: Copy from Device to host g2 -> h_g2 before being used on host
  98. Kokkos::deep_copy(Fill Destination View, Fill Source View);
  99. nvtxRangePop(); //Pop for Pair Calculation
  100. double pi = acos(-1.0l);
  101. double rho = (numatm) / (xbox * ybox * zbox);
  102. double norm = (4.0l * pi * rho) / 3.0l;
  103. double rl, ru, nideal;
  104. double t_g2[nbin];
  105. double r, gr, lngr, lngrbond, s2 = 0.0l, s2bond = 0.0l;
  106. double box = min(xbox, ybox);
  107. box = min(box, zbox);
  108. double del = box / (2.0l * nbin);
  109. nvtxRangePush("Entropy_Calculation");
  110. for (int i = 0; i < nbin; i++)
  111. {
  112. rl = (i)*del;
  113. ru = rl + del;
  114. nideal = norm * (ru * ru * ru - rl * rl * rl);
  115. t_g2[i] = (double)h_g2(i) / ((double)nconf * (double)numatm * nideal);
  116. r = (i)*del;
  117. pairfile << (i + 0.5l) * del << " " << t_g2[i] << endl;
  118. if (r < 2.0l)
  119. {
  120. gr = 0.0l;
  121. }
  122. else
  123. {
  124. gr = t_g2[i];
  125. }
  126. if (gr < 1e-5)
  127. {
  128. lngr = 0.0l;
  129. }
  130. else
  131. {
  132. lngr = log(gr);
  133. }
  134. if (t_g2[i] < 1e-6)
  135. {
  136. lngrbond = 0.0l;
  137. }
  138. else
  139. {
  140. lngrbond = log(t_g2[i]);
  141. }
  142. s2 = s2 - 2.0l * pi * rho * ((gr * lngr) - gr + 1.0l) * del * r * r;
  143. s2bond = s2bond - 2.0l * pi * rho * ((t_g2[i] * lngrbond) - t_g2[i] + 1.0l) * del * r * r;
  144. }
  145. nvtxRangePop(); //Pop for Entropy Calculation
  146. stwo << "s2 value is " << s2 << endl;
  147. stwo << "s2bond value is " << s2bond << endl;
  148. cout << "#Freeing Host memory" << endl;
  149. cout << "#Number of atoms processed: " << numatm << endl
  150. << endl;
  151. cout << "#Number of confs processed: " << nconf << endl
  152. << endl;
  153. } // Kokkos Initialize ends here
  154. //Note:: Free up the memory
  155. Kokkos::finalize();
  156. return 0;
  157. }
  158. int l_round(float num)
  159. {
  160. return num < 0 ? num - 0.5 : num + 0.5;
  161. }
  162. void pair_gpu(view_type_double d_x, view_type_double d_y, view_type_double d_z,
  163. view_type_long d_g2, int numatm, int nconf,
  164. const double xbox, const double ybox, const double zbox,
  165. int d_bin)
  166. {
  167. printf("\n %d %d ", nconf, numatm);
  168. for (int frame = 0; frame < nconf; frame++)
  169. {
  170. printf("\n %d ", frame);
  171. //Fill here the pattern we intend to use along with loop size
  172. Kokkos::Fill_Here(
  173. Fill the loop size here, KOKKOS_LAMBDA(const int index) {
  174. int id1 = index / numatm;
  175. int id2 = index % numatm;
  176. double r, cut, dx, dy, dz;
  177. int ig2;
  178. double box;
  179. int myround;
  180. float num;
  181. box = min(xbox, ybox);
  182. box = min(box, zbox);
  183. double del = box / (2.0 * d_bin);
  184. cut = box * 0.5;
  185. dx = d_x(frame * numatm + id1) - d_x(frame * numatm + id2);
  186. dy = d_y(frame * numatm + id1) - d_y(frame * numatm + id2);
  187. dz = d_z(frame * numatm + id1) - d_z(frame * numatm + id2);
  188. num = dx / xbox;
  189. myround = num < 0 ? num - 0.5 : num + 0.5;
  190. dx = dx - xbox * myround;
  191. num = dy / ybox;
  192. myround = num < 0 ? num - 0.5 : num + 0.5;
  193. dy = dy - ybox * myround;
  194. num = dz / zbox;
  195. myround = num < 0 ? num - 0.5 : num + 0.5;
  196. dz = dz - zbox * myround;
  197. r = sqrtf(dx * dx + dy * dy + dz * dz);
  198. if (r < cut)
  199. {
  200. ig2 = (int)(r / del);
  201. //Note: We are using a atomic increment here
  202. Kokkos::atomic_increment(&d_g2(ig2));
  203. }
  204. });
  205. }
  206. }