rdf_offload_split_num.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <math.h>
  5. #include <cstring>
  6. #include <cstdio>
  7. #include <iomanip>
  8. #include <omp.h>
  9. #include "dcdread.h"
  10. #include <assert.h>
  11. #include <nvtx3/nvToolsExt.h>
  12. void pair_gpu(const double *d_x, const double *d_y, const double *d_z,
  13. unsigned int *d_g2, int numatm, int nconf,
  14. const double xbox, const double ybox, const double zbox,
  15. int d_bin);
  16. int main(int argc, char *argv[])
  17. {
  18. double xbox, ybox, zbox;
  19. double *h_x, *h_y, *h_z;
  20. unsigned int *h_g2;
  21. int nbin;
  22. int numatm, nconf, inconf;
  23. string file;
  24. ///////////////////////////////////////////////////////////////
  25. inconf = 10;
  26. nbin = 2000;
  27. file = "../input/alk.traj.dcd";
  28. ///////////////////////////////////////
  29. std::ifstream infile;
  30. infile.open(file.c_str());
  31. if (!infile)
  32. {
  33. cout << "file " << file.c_str() << " not found\n";
  34. return 1;
  35. }
  36. assert(infile);
  37. ofstream pairfile, stwo;
  38. pairfile.open("RDF.dat");
  39. stwo.open("Pair_entropy.dat");
  40. /////////////////////////////////////////////////////////
  41. dcdreadhead(&numatm, &nconf, infile);
  42. cout << "Dcd file has " << numatm << " atoms and " << nconf << " frames" << endl;
  43. if (inconf > nconf)
  44. cout << "nconf is reset to " << nconf << endl;
  45. else
  46. {
  47. nconf = inconf;
  48. }
  49. cout << "Calculating RDF for " << nconf << " frames" << endl;
  50. ////////////////////////////////////////////////////////
  51. unsigned long long int sizef = nconf * numatm * sizeof(double);
  52. unsigned long long int sizebin = nbin * sizeof(unsigned int);
  53. h_x = (double *)malloc(sizef);
  54. h_y = (double *)malloc(sizef);
  55. h_z = (double *)malloc(sizef);
  56. h_g2 = (unsigned int *)malloc(sizebin);
  57. memset(h_g2, 0, sizebin);
  58. /////////reading cordinates//////////////////////////////////////////////
  59. nvtxRangePush("Read_File");
  60. double ax[numatm], ay[numatm], az[numatm];
  61. for (int i = 0; i < nconf; i++)
  62. {
  63. dcdreadframe(ax, ay, az, infile, numatm, xbox, ybox, zbox);
  64. for (int j = 0; j < numatm; j++)
  65. {
  66. h_x[i * numatm + j] = ax[j];
  67. h_y[i * numatm + j] = ay[j];
  68. h_z[i * numatm + j] = az[j];
  69. }
  70. }
  71. nvtxRangePop(); //pop for REading file
  72. cout << "Reading of input file is completed" << endl;
  73. //////////////////////////////////////////////////////////////////////////
  74. #pragma omp target data map(h_x [0:nconf * numatm], h_y [0:nconf * numatm], h_z [0:nconf * numatm], h_g2 [0:nbin])
  75. {
  76. nvtxRangePush("Pair_Calculation");
  77. pair_gpu(h_x, h_y, h_z, h_g2, numatm, nconf, xbox, ybox, zbox, nbin);
  78. nvtxRangePop(); //Pop for Pair Calculation
  79. }
  80. ////////////////////////////////////////////////////////////////////////
  81. double pi = acos(-1.0);
  82. double rho = (numatm) / (xbox * ybox * zbox);
  83. double norm = (4.0l * pi * rho) / 3.0l;
  84. double rl, ru, nideal;
  85. double g2[nbin];
  86. double r, gr, lngr, lngrbond, s2 = 0.0l, s2bond = 0.0l;
  87. double box = min(xbox, ybox);
  88. box = min(box, zbox);
  89. double del = box / (2.0l * nbin);
  90. nvtxRangePush("Entropy_Calculation");
  91. for (int i = 0; i < nbin; i++)
  92. {
  93. rl = (i)*del;
  94. ru = rl + del;
  95. nideal = norm * (ru * ru * ru - rl * rl * rl);
  96. g2[i] = (double)h_g2[i] / ((double)nconf * (double)numatm * nideal);
  97. r = (i)*del;
  98. pairfile << (i + 0.5l) * del << " " << g2[i] << endl;
  99. if (r < 2.0l)
  100. {
  101. gr = 0.0l;
  102. }
  103. else
  104. {
  105. gr = g2[i];
  106. }
  107. if (gr < 1e-5)
  108. {
  109. lngr = 0.0l;
  110. }
  111. else
  112. {
  113. lngr = log(gr);
  114. }
  115. if (g2[i] < 1e-6)
  116. {
  117. lngrbond = 0.0l;
  118. }
  119. else
  120. {
  121. lngrbond = log(g2[i]);
  122. }
  123. s2 = s2 - 2.0l * pi * rho * ((gr * lngr) - gr + 1.0l) * del * r * r;
  124. s2bond = s2bond - 2.0l * pi * rho * ((g2[i] * lngrbond) - g2[i] + 1.0l) * del * r * r;
  125. }
  126. nvtxRangePop(); //Pop for Entropy Calculation
  127. stwo << "s2 value is " << s2 << endl;
  128. stwo << "s2bond value is " << s2bond << endl;
  129. cout << "#Freeing Host memory" << endl;
  130. free(h_x);
  131. free(h_y);
  132. free(h_z);
  133. free(h_g2);
  134. cout << "#Number of atoms processed: " << numatm << endl
  135. << endl;
  136. cout << "#Number of confs processed: " << nconf << endl
  137. << endl;
  138. return 0;
  139. }
  140. void pair_gpu(const double *d_x, const double *d_y, const double *d_z,
  141. unsigned int *d_g2, int numatm, int nconf,
  142. const double xbox, const double ybox, const double zbox, int d_bin)
  143. {
  144. double r, cut, dx, dy, dz;
  145. int ig2;
  146. double box;
  147. box = min(xbox, ybox);
  148. box = min(box, zbox);
  149. double del = box / (2.0 * d_bin);
  150. cut = box * 0.5;
  151. printf("\n %d %d ", nconf, numatm);
  152. for (int frame = 0; frame < nconf; frame++)
  153. {
  154. printf("\n %d ", frame);
  155. #pragma omp target teams distribute num_teams(65535)
  156. for (int id1 = 0; id1 < numatm; id1++)
  157. {
  158. #pragma omp parallel for private(dx, dy, dz, r, ig2)
  159. for (int id2 = 0; id2 < numatm; id2++)
  160. {
  161. dx = d_x[frame * numatm + id1] - d_x[frame * numatm + id2];
  162. dy = d_y[frame * numatm + id1] - d_y[frame * numatm + id2];
  163. dz = d_z[frame * numatm + id1] - d_z[frame * numatm + id2];
  164. dx = dx - xbox * (round(dx / xbox));
  165. dy = dy - ybox * (round(dy / ybox));
  166. dz = dz - zbox * (round(dz / zbox));
  167. r = sqrtf(dx * dx + dy * dy + dz * dz);
  168. if (r < cut)
  169. {
  170. ig2 = (int)(r / del);
  171. #pragma omp atomic
  172. d_g2[ig2] = d_g2[ig2] + 1;
  173. }
  174. }
  175. }
  176. } //frame ends
  177. }