precomp_mat.txx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /**
  2. * \file precomp_mat.txx
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 3-07-2011
  5. * \brief This file contains the implementation of the PrecompMat class.
  6. * Handles storage of precomputed translation matrices.
  7. */
  8. #include <omp.h>
  9. #include <cassert>
  10. #include <stdint.h>
  11. #ifdef PVFMM_HAVE_SYS_STAT_H
  12. #include <sys/stat.h>
  13. #endif
  14. #include <mem_mgr.hpp>
  15. #include <profile.hpp>
  16. #include <vector.hpp>
  17. namespace pvfmm{
  18. #define PRECOMP_MIN_DEPTH 40
  19. template <class T>
  20. PrecompMat<T>::PrecompMat(bool homogen, int max_d): homogeneous(homogen), max_depth(max_d){
  21. if(!homogen) mat.resize((max_d+PRECOMP_MIN_DEPTH)*Type_Count); //For each U,V,W,X
  22. else mat.resize(Type_Count);
  23. for(size_t i=0;i<mat.size();i++)
  24. mat[i].resize(500);
  25. perm.resize(Type_Count);
  26. for(size_t i=0;i<Type_Count;i++){
  27. perm[i].resize(Perm_Count);
  28. }
  29. perm_r.resize((max_d+PRECOMP_MIN_DEPTH)*Type_Count);
  30. perm_c.resize((max_d+PRECOMP_MIN_DEPTH)*Type_Count);
  31. for(size_t i=0;i<perm_r.size();i++){
  32. perm_r[i].resize(500);
  33. perm_c[i].resize(500);
  34. }
  35. }
  36. template <class T>
  37. Matrix<T>& PrecompMat<T>::Mat(int l, Mat_Type type, size_t indx){
  38. int level=(homogeneous?0:l+PRECOMP_MIN_DEPTH);
  39. assert(level*Type_Count+type<mat.size());
  40. #pragma omp critical (PrecompMAT)
  41. if(indx>=mat[level*Type_Count+type].size()){
  42. mat[level*Type_Count+type].resize(indx+1);
  43. assert(false); //TODO: this is not thread safe.
  44. }
  45. return mat[level*Type_Count+type][indx];
  46. }
  47. template <class T>
  48. Permutation<T>& PrecompMat<T>::Perm_R(int l, Mat_Type type, size_t indx){
  49. int level=l+PRECOMP_MIN_DEPTH;
  50. assert(level*Type_Count+type<perm_r.size());
  51. #pragma omp critical (PrecompMAT)
  52. if(indx>=perm_r[level*Type_Count+type].size()){
  53. perm_r[level*Type_Count+type].resize(indx+1);
  54. assert(false); //TODO: this is not thread safe.
  55. }
  56. return perm_r[level*Type_Count+type][indx];
  57. }
  58. template <class T>
  59. Permutation<T>& PrecompMat<T>::Perm_C(int l, Mat_Type type, size_t indx){
  60. int level=l+PRECOMP_MIN_DEPTH;
  61. assert(level*Type_Count+type<perm_c.size());
  62. #pragma omp critical (PrecompMAT)
  63. if(indx>=perm_c[level*Type_Count+type].size()){
  64. perm_c[level*Type_Count+type].resize(indx+1);
  65. assert(false); //TODO: this is not thread safe.
  66. }
  67. return perm_c[level*Type_Count+type][indx];
  68. }
  69. template <class T>
  70. Permutation<T>& PrecompMat<T>::Perm(Mat_Type type, size_t indx){
  71. assert(indx<Perm_Count);
  72. return perm[type][indx];
  73. }
  74. template <class T>
  75. size_t PrecompMat<T>::CompactData(int l, Mat_Type type, Matrix<char>& comp_data, size_t offset){
  76. std::vector<Matrix<T> >& mat_=mat[(homogeneous?0:l+PRECOMP_MIN_DEPTH)*Type_Count+type];
  77. size_t mat_cnt=mat_.size();
  78. size_t indx_size=0;
  79. size_t mem_size=0;
  80. int omp_p=omp_get_max_threads();
  81. { // Determine memory size.
  82. indx_size+=3*sizeof(size_t); //total_size, mat_cnt, max_depth
  83. indx_size+=mat_cnt*(1+(2+2)*max_depth)*sizeof(size_t); //Mat, Perm_R, Perm_C.
  84. indx_size=((uintptr_t)indx_size+(uintptr_t)(MEM_ALIGN-1)) & ~ (uintptr_t)(MEM_ALIGN-1);
  85. for(size_t j=0;j<mat_cnt;j++){
  86. Matrix <T>& M =Mat (l,type,j);
  87. if(M.Dim(0)>0 && M.Dim(1)>0){
  88. mem_size+=M.Dim(0)*M.Dim(1)*sizeof(T); mem_size=((uintptr_t)mem_size+(uintptr_t)(MEM_ALIGN-1)) & ~ (uintptr_t)(MEM_ALIGN-1);
  89. }
  90. for(size_t l=0;l<max_depth;l++){
  91. Permutation<T>& Pr=Perm_R(l,type,j);
  92. Permutation<T>& Pc=Perm_C(l,type,j);
  93. if(Pr.Dim()>0){
  94. mem_size+=Pr.Dim()*sizeof(PERM_INT_T); mem_size=((uintptr_t)mem_size+(uintptr_t)(MEM_ALIGN-1)) & ~ (uintptr_t)(MEM_ALIGN-1);
  95. mem_size+=Pr.Dim()*sizeof(T); mem_size=((uintptr_t)mem_size+(uintptr_t)(MEM_ALIGN-1)) & ~ (uintptr_t)(MEM_ALIGN-1);
  96. }
  97. if(Pc.Dim()>0){
  98. mem_size+=Pc.Dim()*sizeof(PERM_INT_T); mem_size=((uintptr_t)mem_size+(uintptr_t)(MEM_ALIGN-1)) & ~ (uintptr_t)(MEM_ALIGN-1);
  99. mem_size+=Pc.Dim()*sizeof(T); mem_size=((uintptr_t)mem_size+(uintptr_t)(MEM_ALIGN-1)) & ~ (uintptr_t)(MEM_ALIGN-1);
  100. }
  101. }
  102. }
  103. }
  104. if(comp_data.Dim(0)*comp_data.Dim(1)<offset+indx_size+mem_size){ // Resize if needed.
  105. Matrix<char> old_data;
  106. if(offset>0) old_data=comp_data;
  107. comp_data.Resize(1,offset+indx_size+mem_size);
  108. if(offset>0) mem::memcopy(comp_data[0], old_data[0], offset); //TODO: This will affect NUMA.
  109. }
  110. { // Create indx.
  111. char* indx_ptr=comp_data[0]+offset;
  112. size_t data_offset=offset+indx_size;
  113. ((size_t*)indx_ptr)[0]=indx_size+mem_size; indx_ptr+=sizeof(size_t);
  114. ((size_t*)indx_ptr)[0]= mat_cnt ; indx_ptr+=sizeof(size_t);
  115. ((size_t*)indx_ptr)[0]= max_depth ; indx_ptr+=sizeof(size_t);
  116. for(size_t j=0;j<mat_cnt;j++){
  117. Matrix <T>& M =Mat (l,type,j);
  118. ((size_t*)indx_ptr)[0]=data_offset; indx_ptr+=sizeof(size_t);
  119. data_offset+=M.Dim(0)*M.Dim(1)*sizeof(T); data_offset=((uintptr_t)data_offset+(uintptr_t)(MEM_ALIGN-1)) & ~ (uintptr_t)(MEM_ALIGN-1);
  120. for(size_t l=0;l<max_depth;l++){
  121. Permutation<T>& Pr=Perm_R(l,type,j);
  122. ((size_t*)indx_ptr)[0]=data_offset; indx_ptr+=sizeof(size_t);
  123. data_offset+=Pr.Dim()*sizeof(PERM_INT_T); data_offset=((uintptr_t)data_offset+(uintptr_t)(MEM_ALIGN-1)) & ~ (uintptr_t)(MEM_ALIGN-1);
  124. ((size_t*)indx_ptr)[0]=data_offset; indx_ptr+=sizeof(size_t);
  125. data_offset+=Pr.Dim()*sizeof(T); data_offset=((uintptr_t)data_offset+(uintptr_t)(MEM_ALIGN-1)) & ~ (uintptr_t)(MEM_ALIGN-1);
  126. Permutation<T>& Pc=Perm_C(l,type,j);
  127. ((size_t*)indx_ptr)[0]=data_offset; indx_ptr+=sizeof(size_t);
  128. data_offset+=Pc.Dim()*sizeof(PERM_INT_T); data_offset=((uintptr_t)data_offset+(uintptr_t)(MEM_ALIGN-1)) & ~ (uintptr_t)(MEM_ALIGN-1);
  129. ((size_t*)indx_ptr)[0]=data_offset; indx_ptr+=sizeof(size_t);
  130. data_offset+=Pc.Dim()*sizeof(T); data_offset=((uintptr_t)data_offset+(uintptr_t)(MEM_ALIGN-1)) & ~ (uintptr_t)(MEM_ALIGN-1);
  131. }
  132. }
  133. }
  134. { // Copy data.
  135. char* indx_ptr=comp_data[0]+offset;
  136. size_t& total_size=((size_t*)indx_ptr)[0]; indx_ptr+=sizeof(size_t);
  137. size_t& mat_cnt =((size_t*)indx_ptr)[0]; indx_ptr+=sizeof(size_t);
  138. size_t& max_depth=((size_t*)indx_ptr)[0]; indx_ptr+=sizeof(size_t);
  139. Matrix<size_t> data_offset(mat_cnt,1+(2+2)*max_depth, (size_t*)indx_ptr, false);
  140. offset+=total_size;
  141. for(size_t j=0;j<mat_cnt;j++){
  142. Matrix <T>& M =Mat (l,type,j);
  143. if(M.Dim(0)>0 && M.Dim(1)>0){
  144. #pragma omp parallel for
  145. for(int tid=0;tid<omp_p;tid++){
  146. size_t a=(M.Dim(0)*M.Dim(1)* tid )/omp_p;
  147. size_t b=(M.Dim(0)*M.Dim(1)*(tid+1))/omp_p;
  148. mem::memcopy(comp_data[0]+data_offset[j][0]+a*sizeof(T), &M[0][a], (b-a)*sizeof(T));
  149. }
  150. }
  151. for(size_t l=0;l<max_depth;l++){
  152. Permutation<T>& Pr=Perm_R(l,type,j);
  153. Permutation<T>& Pc=Perm_C(l,type,j);
  154. if(Pr.Dim()>0){
  155. mem::memcopy(comp_data[0]+data_offset[j][1+4*l+0], &Pr.perm[0], Pr.Dim()*sizeof(PERM_INT_T));
  156. mem::memcopy(comp_data[0]+data_offset[j][1+4*l+1], &Pr.scal[0], Pr.Dim()*sizeof( T));
  157. }
  158. if(Pc.Dim()>0){
  159. mem::memcopy(comp_data[0]+data_offset[j][1+4*l+2], &Pc.perm[0], Pc.Dim()*sizeof(PERM_INT_T));
  160. mem::memcopy(comp_data[0]+data_offset[j][1+4*l+3], &Pc.scal[0], Pc.Dim()*sizeof( T));
  161. }
  162. }
  163. }
  164. }
  165. return offset;
  166. }
  167. template <class T>
  168. void PrecompMat<T>::Save2File(const char* fname, bool replace){
  169. FILE* f=fopen(fname,"r");
  170. if(f!=NULL) { //File already exists.
  171. fclose(f);
  172. if(!replace) return;
  173. }
  174. f=fopen(fname,"wb");
  175. if(f==NULL) return;
  176. int tmp;
  177. tmp=sizeof(T);
  178. fwrite(&tmp,sizeof(int),1,f);
  179. tmp=(homogeneous?1:0);
  180. fwrite(&tmp,sizeof(int),1,f);
  181. tmp=max_depth;
  182. fwrite(&tmp,sizeof(int),1,f);
  183. for(size_t i=0;i<mat.size();i++){
  184. int n=mat[i].size();
  185. fwrite(&n,sizeof(int),1,f);
  186. for(int j=0;j<n;j++){
  187. Matrix<T>& M=mat[i][j];
  188. int n1=M.Dim(0);
  189. fwrite(&n1,sizeof(int),1,f);
  190. int n2=M.Dim(1);
  191. fwrite(&n2,sizeof(int),1,f);
  192. if(n1*n2>0)
  193. fwrite(&M[0][0],sizeof(T),n1*n2,f);
  194. }
  195. }
  196. fclose(f);
  197. }
  198. #define MY_FREAD(a,b,c,d) { \
  199. size_t r_cnt=fread(a,b,c,d); \
  200. if(r_cnt!=c){ \
  201. fputs ("Reading error ",stderr); \
  202. exit (-1); \
  203. } }
  204. template <class T>
  205. void PrecompMat<T>::LoadFile(const char* fname, MPI_Comm comm){
  206. Profile::Tic("LoadMatrices",&comm,true,3);
  207. Profile::Tic("ReadFile",&comm,true,4);
  208. size_t f_size=0;
  209. char* f_data=NULL;
  210. int np, myrank;
  211. MPI_Comm_size(comm,&np);
  212. MPI_Comm_rank(comm,&myrank);
  213. if(myrank==0){
  214. FILE* f=fopen(fname,"rb");
  215. if(f==NULL){
  216. f_size=0;
  217. }else{
  218. struct stat fileStat;
  219. if(stat(fname,&fileStat) < 0) f_size=0;
  220. else f_size=fileStat.st_size;
  221. //fseek (f, 0, SEEK_END);
  222. //f_size=ftell (f);
  223. }
  224. if(f_size>0){
  225. f_data=mem::aligned_new<char>(f_size);
  226. fseek (f, 0, SEEK_SET);
  227. MY_FREAD(f_data,sizeof(char),f_size,f);
  228. fclose(f);
  229. }
  230. }
  231. Profile::Toc();
  232. Profile::Tic("Broadcast",&comm,true,4);
  233. MPI_Bcast( &f_size, sizeof(size_t), MPI_BYTE, 0, comm );
  234. if(f_size==0){
  235. Profile::Toc();
  236. Profile::Toc();
  237. return;
  238. }
  239. if(f_data==NULL) f_data=mem::aligned_new<char>(f_size);
  240. char* f_ptr=f_data;
  241. int max_send_size=1000000000;
  242. while(f_size>0){
  243. if(f_size>(size_t)max_send_size){
  244. MPI_Bcast( f_ptr, max_send_size, MPI_BYTE, 0, comm );
  245. f_size-=max_send_size;
  246. f_ptr+=max_send_size;
  247. }else{
  248. MPI_Bcast( f_ptr, f_size, MPI_BYTE, 0, comm );
  249. f_size=0;
  250. }
  251. }
  252. f_ptr=f_data;
  253. {
  254. int tmp;
  255. tmp=*(int*)f_ptr; f_ptr+=sizeof(int);
  256. assert(tmp==sizeof(T));
  257. tmp=*(int*)f_ptr; f_ptr+=sizeof(int);
  258. homogeneous=tmp;
  259. int max_depth_;
  260. max_depth_=*(int*)f_ptr; f_ptr+=sizeof(int);
  261. size_t mat_size=(size_t)Type_Count*(homogeneous?1:max_depth_+PRECOMP_MIN_DEPTH);
  262. if(mat.size()<mat_size){
  263. max_depth=max_depth_;
  264. mat.resize(mat_size);
  265. }
  266. for(size_t i=0;i<mat_size;i++){
  267. int n;
  268. n=*(int*)f_ptr; f_ptr+=sizeof(int);
  269. if(mat[i].size()<(size_t)n)
  270. mat[i].resize(n);
  271. for(int j=0;j<n;j++){
  272. Matrix<T>& M=mat[i][j];
  273. int n1;
  274. n1=*(int*)f_ptr; f_ptr+=sizeof(int);
  275. int n2;
  276. n2=*(int*)f_ptr; f_ptr+=sizeof(int);
  277. if(n1*n2>0){
  278. M.Resize(n1,n2);
  279. mem::memcopy(&M[0][0], f_ptr, sizeof(T)*n1*n2); f_ptr+=sizeof(T)*n1*n2;
  280. }
  281. }
  282. }
  283. // Resize perm_r, perm_c
  284. perm_r.resize((max_depth_+PRECOMP_MIN_DEPTH)*Type_Count);
  285. perm_c.resize((max_depth_+PRECOMP_MIN_DEPTH)*Type_Count);
  286. for(size_t i=0;i<perm_r.size();i++){
  287. perm_r[i].resize(500);
  288. perm_c[i].resize(500);
  289. }
  290. }
  291. mem::aligned_delete<char>(f_data);
  292. Profile::Toc();
  293. Profile::Toc();
  294. }
  295. #undef MY_FREAD
  296. #undef PRECOMP_MIN_DEPTH
  297. template <class T>
  298. std::vector<T>& PrecompMat<T>::RelativeTrgCoord(){
  299. return rel_trg_coord;
  300. }
  301. template <class T>
  302. bool PrecompMat<T>::Homogen(){
  303. return homogeneous;
  304. }
  305. }//end namespace