Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2774 connectés 

  FORUM HardWare.fr
  Programmation
  Divers

  CUDA : Boucle for qui prend de plus en plus de temps

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

CUDA : Boucle for qui prend de plus en plus de temps

n°2073347
babyromu
Posté le 02-05-2011 à 21:09:28  profilanswer
 

Bonjour,
 
Je suis actuellement en train d'écrire un programme sous CUDA.
 
J'ai des fichiers .cu .c et .cpp tous reliés via un makefile.
 
Seulement je rencontre un gros pépin pour le moment.
 
Mon programme est une modélisation scientifique d'un gas dont on fait bouger des particules à chaque pas de temps.
 
Cependant je remarque que mon algorithme met de plus en plus de temps pour faire une itération. Pour faire les 5 premieres par exemple il met 5ms tandis que pour en faire 50 il met 1500 ms.
 
Auriez vous une idée d'a quoi pourrait etre du cet accroissement important de temps.
 
Sachant que chaque iteration regroupe une phase de deplacement de particules, un tri et un phénomène de tirage de nombre aléatoire (c'est une Direct simulation Monte Carlo que je fais au cas ou il y aurait un connaisseur).
 
Merci pour votre aide
 
Romuald

mood
Publicité
Posté le 02-05-2011 à 21:09:28  profilanswer
 

n°2073359
gilou
Modérateur
Modzilla
Posté le 02-05-2011 à 21:36:01  profilanswer
 

Peut être que le code nous permettrait de répondre, parce que sinon, bonjour la boule de cristal.
A+,


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
n°2073362
babyromu
Posté le 02-05-2011 à 22:17:29  profilanswer
 

o


Message édité par babyromu le 02-05-2011 à 22:32:12
n°2073363
babyromu
Posté le 02-05-2011 à 22:18:42  profilanswer
 

vu la taille du code, j'etais un peu perplexe à le mettre ici...

n°2073390
Joel F
Real men use unique_ptr
Posté le 03-05-2011 à 00:45:23  profilanswer
 

ca sent les alloc memoires foireuses ou des acces non coalescents

n°2073459
babyromu
Posté le 03-05-2011 à 11:09:21  profilanswer
 

Je vous met ici une partie du code. Le seul kernel que j'utilise pour le moment dans la grosse boucle est advection (car deja elle seule ralentit fortement), les autres sont commentés et n'agissent pas pour le moment. Par exemple, pour 1000 iteration elle met 13ms, our 1500 elle en met 280ms.
 

Code :
  1. extern "C" void molecule_set_CollisionsGPU(molecule_set_t molecule_set)//, histogram_t *histograms)
  2. {
  3.     unsigned int timer = 0;
  4.     cutilCheckError( cutCreateTimer( &timer));
  5.  
  6.     /*Définition des variables utilisées*/
  7.     int TAILLE;
  8.     TAILLE = molecule_set->number*11/10;
  9.     printf("On fixe la taille des tableaux à : %d\n",TAILLE); 
  10.  
  11.    /////////////////Definition des variables utilisées
  12.  
  13. int i,k,j,m;
  14. v3 *tmp,*tmp1;
  15. mlcl *h_mol;
  16. mlcl *d_mol;
  17.     float *collisions,*d_collisions, *samples,*d_samples,*statsize,*d_statsize;
  18.     int *appcell,*d_appcell,*nbrecumu, *d_nbrecumu,*nbrecell,*d_nbrecell;
  19.     molecule_t mol;
  20. cells_table_item_t cell;
  21. ////////////////Allocation mémoire sur CPU
  22. h_mol = (mlcl*)malloc(sizeof(mlcl)*TAILLE);
  23. appcell = (int*)malloc(sizeof(int)*TAILLE);
  24. nbrecumu = (int*)malloc(sizeof(int)*(molecule_set->table->size+1));
  25. nbrecumu[0]=0;
  26. collisions = (float*)malloc(sizeof(float)*molecule_set->table->size);
  27. samples = (float*)malloc(sizeof(float)*molecule_set->table->size);
  28. statsize = (float*)malloc(sizeof(float)*molecule_set->table->size);
  29. nbrecell = (int*)malloc(sizeof(int)*molecule_set->table->size);
  30. static float f_max_1, f_max_2;
  31. static float normalization_1, normalization_2;
  32.     cuda_init_inflow_distribution(&normalization_1,&normalization_2,&f_max_1,&f_max_2);
  33.     float u_max_at_boundary_1 = mean_vel_1 + width_cutoff;
  34.    
  35.    
  36.     static float flux_rate_1 = (2 *sqrt(2) *sigma) / ( exp(-(mean_vel_1*mean_vel_1))/sqrt(PI) + mean_vel_1*(1.0 + erf(mean_vel_1)) );
  37. /*Remplissage des tableaux*/
  38. for (k=0;k<molecule_set->table->size;k++) {
  39.  cell = &(molecule_set->table->array[k]);
  40.  if(cell->number>1)
  41.  {
  42.  for (i=0; i<cell->number; i++) {
  43.   mol = (molecule_t)cells_table_item_Get_Data(cell,i);
  44.   for (j=0;j<3;j++)
  45.   {
  46.    tmp = molecule_GetVelocity(mol);
  47.    tmp1= molecule_GetPosition(mol);
  48.    h_mol[i+nbrecumu[k]].vit[j]=(*tmp)[j];
  49.    h_mol[i+nbrecumu[k]].pos[j]=(*tmp1)[j];
  50.   }
  51.   appcell[i+nbrecumu[k]] =k;
  52.  }
  53.   nbrecumu[k+1]=cell->number + nbrecumu[k];
  54.   nbrecell[k]=0;
  55.   samples[k]=cell->statistics.samples;
  56.   collisions[k]=cell->collision_delay;
  57.   statsize[k]=cell->statistics.size;
  58.  }
  59. }
  60. for(k=nbrecumu[molecule_set->table->size];k<TAILLE;k++)
  61. {
  62.  appcell[k]=10000;
  63.  }
  64. ////////////////// Tableau pour les sorties de donnés : histo + statistiques
  65. int jj;
  66. statistic Stat[molecule_set->table->size];
  67. for (i=0;i<molecule_set->table->size;i++) Stat[i]=((molecule_set->table->array)[i]).statistics;
  68. statistic *d_Stat;
  69. HANDLE_ERROR( cudaMalloc((void **)&d_Stat, sizeof(statistic)*(molecule_set->table->size)));
  70. HANDLE_ERROR( cudaMemcpy(d_Stat, Stat, molecule_set->table->size* sizeof(statistic), cudaMemcpyHostToDevice));
  71. /*
  72. histoGPU Histo[n_histos];
  73. for (i=0;i<n_histos;i++)
  74. {
  75. for (jj=0;jj<3;jj++) for (j=0;j<2;j++) (Histo[i].range[j])[jj]=(histograms[i]->range[j])[jj];
  76. Histo[i].sample_size=histograms[i]->sample_size;
  77. for (j=0;j<3;j++) Histo[i].n_bins[j]=histograms[i]->n_bins[j];
  78. Histo[i].cell[0]=histo_cells[i];
  79. }
  80. */
  81. ///////////////////////////////////////////////////////////////////////////////////////////////////  
  82. //GENERATION DE LA STRUCTURE INITIALE OUR LES NOMBRES ALEATOIRES
  83. mt_struct_stripped* ds_MTConfigs;
  84. unsigned int* ds_MTStates;
  85. int* ds_MTiStates;
  86. mt_struct_stripped h_MTConfigs[N_GPU_RNG_THREADS_TOTAL];
  87.  const char* fname = "./MersenneTwister.dat";
  88.  FILE* fd = fopen(fname, "rb" );
  89.  if(!fd){
  90.   printf("initMTGPU(): failed to open %s\n", fname);
  91.   printf("TEST FAILED\n" );
  92.   exit(0);
  93.  }
  94.  if( !fread(h_MTConfigs, sizeof(h_MTConfigs), 1, fd) ){
  95.   printf("initMTGPU(): failed to load %s\n", fname);
  96.   printf("TEST FAILED\n" );
  97.   exit(0);
  98.  }
  99. fclose(fd);
  100. cudaMalloc((void **)&ds_MTConfigs,  sizeof(h_MTConfigs));
  101. cudaMemcpy(ds_MTConfigs, h_MTConfigs, sizeof(h_MTConfigs), cudaMemcpyHostToDevice);
  102. cudaMalloc((void **)&ds_MTStates,  N_GPU_RNG_THREADS_TOTAL*MT_NN*sizeof(unsigned int));
  103. cudaMalloc((void **)&ds_MTiStates,  N_GPU_RNG_THREADS_TOTAL*sizeof(int));
  104. initialize_CUDA_UniformMT(ds_MTConfigs, ds_MTStates, ds_MTiStates);
  105. //printf("N_GPU_RNG_THREADS_TOTAL %d\n",N_GPU_RNG_THREADS_TOTAL );
  106. //printf("%10lu\n",sizeof(mt_struct_stripped));
  107. //printf("%10lu\n",sizeof(h_MTConfigs));
  108. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  109. dim3 block;
  110.     dim3 grid;
  111.    
  112.     /*Allocation et envoi mémoire vers GPU*/
  113.  
  114.     HANDLE_ERROR( cudaMalloc((void **)&d_mol, sizeof(mlcl)*TAILLE));
  115. HANDLE_ERROR( cudaMalloc((void **)&d_appcell, sizeof(int)*TAILLE));
  116. HANDLE_ERROR( cudaMalloc((void **)&d_nbrecumu, sizeof(int)*(molecule_set->table->size+1)));
  117. HANDLE_ERROR( cudaMalloc((void **)&d_nbrecell, sizeof(int)*(molecule_set->table->size)));
  118. HANDLE_ERROR( cudaMalloc((void **)&d_collisions, sizeof(float)*molecule_set->table->size));
  119. HANDLE_ERROR( cudaMalloc((void **)&d_samples, sizeof(float)*molecule_set->table->size));
  120. HANDLE_ERROR( cudaMalloc((void **)&d_statsize, sizeof(float)*molecule_set->table->size));
  121. HANDLE_ERROR( cudaMemcpy(d_mol, h_mol, TAILLE* sizeof(mlcl), cudaMemcpyHostToDevice));
  122. HANDLE_ERROR( cudaMemcpy(d_nbrecumu, nbrecumu, (molecule_set->table->size+1)* sizeof(int), cudaMemcpyHostToDevice));
  123. HANDLE_ERROR( cudaMemcpy(d_nbrecell, nbrecell, (molecule_set->table->size)* sizeof(int), cudaMemcpyHostToDevice));
  124. HANDLE_ERROR( cudaMemcpy(d_samples, samples, molecule_set->table->size* sizeof(float), cudaMemcpyHostToDevice));
  125. HANDLE_ERROR( cudaMemcpy(d_collisions, collisions, molecule_set->table->size* sizeof(float), cudaMemcpyHostToDevice));
  126. HANDLE_ERROR( cudaMemcpy(d_statsize, statsize, molecule_set->table->size* sizeof(float), cudaMemcpyHostToDevice));
  127. HANDLE_ERROR( cudaMemcpy(d_appcell, appcell, TAILLE *sizeof(int), cudaMemcpyHostToDevice));
  128. //////////////////////////////////////////////////////////////////////////////////  
  129. ///////Boucle de travail//////////////////////////////////////////////////////////  
  130. //////////////////////////////////////////////////////////////////////////////////
  131. float* dtsum;
  132. dtsum = (float*)malloc(sizeof(float));
  133. dtsum[0]=0;
  134. float* d_dtsum;
  135. HANDLE_ERROR( cudaMalloc((void **)&d_dtsum, sizeof(float)));
  136. cudaMemcpy(d_dtsum, dtsum,sizeof(float), cudaMemcpyHostToDevice);
  137. int* increm;
  138. increm = (int*)malloc(sizeof(int));
  139. dtsum[0]=0;
  140. int* d_increm;
  141. HANDLE_ERROR( cudaMalloc((void **)&d_increm, sizeof(int)));
  142. cudaMemcpy(d_increm, increm,sizeof(int), cudaMemcpyHostToDevice);
  143. int* col;
  144. col = (int*)malloc(molecule_set->table->size*sizeof(int));
  145. int* d_col;
  146. HANDLE_ERROR( cudaMalloc((void **)&d_col, molecule_set->table->size*sizeof(int)));
  147. cudaMemcpy(d_col, col,molecule_set->table->size*sizeof(int), cudaMemcpyHostToDevice);
  148. int sommegpu=0;
  149. cutilCheckError( cutStartTimer( timer));
  150. for(m=1;m<1500;m++)
  151. {
  152. //////////////////                     ADVECTION  
  153. block.x = 32;
  154.     grid.x  = TAILLE/32;
  155. AdvectionGPU<<<grid,block>>>(d_mol,d_appcell,dt,x_max,x_min,y_min,y_max,z_min,z_max,molecule_set->table->dx);
  156. //cutilCheckMsg("Kernel execution failed" );
  157. /////////////////////                         TRI
  158. //grid.x = molecule_set->table->size;
  159.     //block.x  = 1;
  160.    
  161.     //tri<<<block,grid>>>(d_mol,d_nbrecumu,d_nbrecell,d_appcell);
  162.     //cutilCheckMsg("Kernel execution failed" );
  163.    
  164. // cudaMemcpy(nbrecumu, d_nbrecumu,sizeof(int)*201, cudaMemcpyDeviceToHost);
  165. // cudaMemcpy(nbrecell, d_nbrecell,sizeof(int)*200, cudaMemcpyDeviceToHost);
  166. // for (i=199;i<200;i++) printf("nombre de part dans la cellule %d vaut %d ajout de %d parti: total parti = %d et 1ere cell : %d\n",i,nbrecumu[i],nbrecell[i],nbrecell[199],nbrecell[0]);
  167. //cudaMemcpy(col, d_col,sizeof(int)*200, cudaMemcpyDeviceToHost);
  168. //for (i=199;i<200;i++) printf("le nombre de coll dans la cell %d vaut %d collisions \n",i,col[i]);
  169.  // sommegpu =0;
  170. //  for (i=0;i<200;i++) sommegpu+=col[i];
  171.  // printf("%d\n",sommegpu/m);
  172. //////////////////                          COLLISIONS
  173. // grid.x = 1;
  174.   //  block.x  = molecule_set->table->size;
  175. // CollGPU<<<grid,block>>>(d_mol,d_nbrecumu,d_nbrecell,d_collisions,d_statsize,d_samples,c_r_max,dt,n_per_cell_ref, ds_MTStates, ds_MTConfigs, ds_MTiStates,d_appcell,d_col);
  176. // cutilCheckMsg("Kernel execution failed" );
  177. /*
  178. cudaMemcpy(appcell, d_appcell,sizeof(int)*TAILLE, cudaMemcpyDeviceToHost);
  179. cudaMemcpy(h_mol, d_mol,sizeof(mlcl)*TAILLE, cudaMemcpyDeviceToHost);
  180. for (i=19100;i<19300;i++) printf("la particule %d avec une vitesse de %f est dans la cellule %d\n",i,h_mol[i].vit[0],appcell[i]);
  181. */
  182.     ///////////////////                             STAT
  183.     //if (m%25==0) UpdateStat<<<grid,block>>>(d_Stat,d_mol,d_nbrecumu);
  184.     if (m%500==0)
  185.     {
  186.      /*HANDLE_ERROR( cudaMemcpy(Stat, d_Stat, molecule_set->table->size* sizeof(statistic), cudaMemcpyDeviceToHost));
  187.  for (i=0;i<molecule_set->table->size;i++) ((molecule_set->table->array)[i]).statistics=Stat[i];
  188.      molecule_set_Output_Cell_Stats_File(molecule_set);*/
  189.      }
  190.    
  191.    
  192. }
  193.  
  194.   cutilCheckError( cutStopTimer( timer));
  195.   float gpu_time = cutGetTimerValue( timer);
  196.   printf( "GPU Processing time: %f (ms)\n", gpu_time);
  197. HANDLE_ERROR( cudaMemcpy(Stat, d_Stat, molecule_set->table->size* sizeof(statistic), cudaMemcpyDeviceToHost));
  198. cudaMemcpy(nbrecumu, d_nbrecumu,sizeof(int)*201, cudaMemcpyDeviceToHost);
  199. cudaMemcpy(nbrecell, d_nbrecell,sizeof(int)*200, cudaMemcpyDeviceToHost);
  200. for (i=0;i<molecule_set->table->size;i++) ((molecule_set->table->array)[i]).statistics=Stat[i];
  201.     cells_table_item_t myptr;
  202.     cells_table_t mytable;
  203.     mytable = molecule_set->table;
  204.     for (i=0;i<mytable->size;i++) {
  205.     myptr = &(mytable->array[i]);
  206.     myptr->number = nbrecumu[i+1]-nbrecumu[i];
  207.     }
  208. //for (i=0;i<200;i++) printf("nombre de part dans la cellule %d vaut %d ajout de %d parti\n",i,nbrecumu[i],nbrecell[i]);
  209. ///////////////////Renvoi des données sur le processeur
  210. cudaMemcpy(h_mol, d_mol, TAILLE* sizeof(mlcl), cudaMemcpyDeviceToHost);
  211. cudaMemcpy(nbrecumu, d_nbrecumu, (molecule_set->table->size+1)* sizeof(int), cudaMemcpyDeviceToHost);
  212. cudaMemcpy(samples, d_samples, molecule_set->table->size* sizeof(float), cudaMemcpyDeviceToHost);
  213. cudaMemcpy(collisions, d_collisions, molecule_set->table->size* sizeof(float), cudaMemcpyDeviceToHost);
  214. cudaMemcpy(statsize, d_statsize, molecule_set->table->size* sizeof(float), cudaMemcpyDeviceToHost);
  215. cudaFree(d_mol);
  216. cudaFree(d_nbrecumu);
  217. cudaFree(d_collisions);
  218. cudaFree(d_statsize);
  219. cudaFree(d_samples);
  220. cudaFree(d_nbrecell);
  221. cudaFree(d_appcell);
  222. cudaFree(ds_MTConfigs);
  223. cudaFree(ds_MTStates);
  224. cudaFree(ds_MTiStates);
  225. cudaFree(d_dtsum);
  226.     free(h_mol);
  227. free(nbrecumu);
  228. free(collisions);
  229. free(statsize);
  230. free(samples);
  231. free(appcell);
  232. free(nbrecell);
  233. free(dtsum);
  234. }
  235. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  236. void initialize_CUDA_UniformMT(mt_struct_stripped* ds_MTConfigs, unsigned int* ds_MTStates, int* ds_MTiStates)
  237. {
  238. c_initialize_CUDA_UniformMT(ds_MTConfigs, ds_MTStates, ds_MTiStates);
  239. }
  240. __device__ Real generateUniform(unsigned int *m_mtstate, mt_struct_stripped *m_config, int *m_istate)
  241. {
  242. int     iState = *m_istate;
  243. unsigned int mti1 = m_mtstate[iState];
  244. int iState1 = iState + 1;
  245. int iStateM = iState + MT_MM;
  246. if(iState1 >= MT_NN) iState1 -= MT_NN;
  247. if(iStateM >= MT_NN) iStateM -= MT_NN;
  248. unsigned int mti  = mti1;
  249.      mti1 = m_mtstate[iState1];
  250. unsigned int mtiM = m_mtstate[iStateM];
  251. unsigned int x;
  252. x = (mti & MT_UMASK) | (mti1 & MT_LMASK);
  253. x =  mtiM ^ (x >> 1) ^ ((x & 1) ? (*m_config).matrix_a : 0);
  254. *m_istate    = iState1;
  255. m_mtstate[iState] = x;
  256. //Tempering transformation
  257. x ^= (x >> MT_SHIFT0);
  258. x ^= (x << MT_SHIFTB) & (*m_config).mask_b;
  259. x ^= (x << MT_SHIFTC) & (*m_config).mask_c;
  260. x ^= (x >> MT_SHIFT1);
  261. //Convert to (0, 1] Real and write to global memory
  262. return ((Real)x + 1.0f) / (double)4294967296.0f;
  263. }
  264. __global__ void AdvectionGPU(mlcl *mol,int* cellule, float dt, int x_max, int x_min,int y_min,int y_max,int z_min,int z_max,float dx)
  265. {
  266.  int tid;
  267.  int i;
  268.  int cell;
  269.  float pos_collision, t_collision;
  270.  tid = blockIdx.x*blockDim.x + threadIdx.x;
  271.  if (tid<19283)
  272.  {
  273.  for (i=0;i<3;++i)
  274.  {
  275.   mol[tid].pos[i]+= dt*mol[tid].vit[i];
  276.  }
  277.  if (mol[tid].pos[0]<x_min) mol[tid].pos[0] = x_max-x_min+mol[tid].pos[0];
  278.  if (mol[tid].pos[0]>x_max) mol[tid].pos[0] = x_min-x_max+mol[tid].pos[0];
  279.  if (mol[tid].pos[1]<y_min) mol[tid].pos[1] = y_max-y_min+mol[tid].pos[1];
  280.  if (mol[tid].pos[1]>y_max) mol[tid].pos[1] = y_min-y_max+mol[tid].pos[1];
  281.  if (mol[tid].pos[2]<z_min) mol[tid].pos[2] = z_max-z_min+mol[tid].pos[2];
  282.  if (mol[tid].pos[2]>z_max) mol[tid].pos[2] = z_min-z_max+mol[tid].pos[2];
  283.  cell=(mol[tid].pos[0]-x_min)/dx;
  284.  cellule[tid] = cell;
  285.  }
  286. }
  287. __global__ void tri(mlcl *mol, int* nbrecumule,int* nbrecell,int *appcell)
  288. {
  289. int tid =0;
  290. tid = blockIdx.x*blockDim.x + threadIdx.x;
  291. int tic =0;
  292. int i;
  293. mlcl retrouve[250];
  294.  //On les rerange dans la bonne cellule
  295.  if (tid!=0 && tid!= 199)
  296.  {
  297.   for (i=nbrecumule[tid-1];i<nbrecumule[tid+2];i++)
  298.   {
  299.    if(appcell[i]==tid)
  300.    {
  301.     retrouve[tic]=mol[i];
  302.     tic++;
  303.    }
  304.   }
  305.   nbrecell[tid]=tic;
  306.  }
  307.  else if (tid==0)/////////Pas oublier les Boundary Left
  308.  {
  309.   for (i=nbrecumule[tid];i<nbrecumule[tid+2];i++)
  310.   {
  311.    if(appcell[i]==0)
  312.    {
  313.     retrouve[tic]=mol[i];
  314.     tic++;
  315.    }
  316.   }
  317.   for (i=nbrecumule[200];i<nbrecumule[200]+100;i++)
  318.   {
  319.    if(appcell[i]==0)
  320.    {
  321.     retrouve[tic]=mol[i];
  322.     tic++;
  323.    }
  324.   }
  325.   nbrecell[tid]=tic;
  326.  }
  327.  else
  328.  {
  329.   for (i=nbrecumule[tid-1];i<nbrecumule[tid+1];i++)
  330.   {
  331.    if(appcell[i]==tid)
  332.    {
  333.     retrouve[tic]=mol[i];
  334.     tic++;
  335.    }
  336.   }
  337.   nbrecell[tid]=tic;
  338.  }
  339.  nbrecumule[tid+1]=0;
  340.  __syncthreads();
  341.  for(i=0;i<tid+1;i++) nbrecumule[tid+1]+=nbrecell[i];
  342.  __syncthreads();
  343.  for(i=0;i<nbrecell[tid];i++)
  344.  {
  345.   mol[nbrecumule[tid]+i]=retrouve[i];
  346.   appcell[nbrecumule[tid]+i]=tid;
  347.  }
  348. }
  349. __global__ void CollGPU(mlcl *mol, int* nbrecumule,int* nbrecell, float* delai,float* taille, float* sample, float c_r_max,int dt, int n_per_cell_ref,unsigned int *m_mtstate, mt_struct_stripped *m_config, int *m_istate, int *appcell, int* col)
  350. {
  351.  int tid,i,j,index1,index2;
  352.  tid = blockIdx.x*blockDim.x + threadIdx.x;
  353.  float epsilon,chi,seps,schi,ceps,cchi,norm_c_r;
  354.  float B;
  355.  float n_aver_for_collision_interval, delta_t_coll;
  356.  v3 c_m,c_r,c_r_star;
  357.  float arret;
  358.  arret = float(1.0)/100;
  359.  col[tid]=0;
  360.  if(nbrecell[tid]>1)
  361.  {
  362.  ///////////////Collisions
  363.  while (delai[tid]<=arret) {
  364.   do {
  365.    index1 = nbrecumule[tid]+(nbrecumule[tid+1]-nbrecumule[tid])*generateUniform(&m_mtstate[tid],&m_config[tid],&m_istate[tid]);
  366.    index2 = nbrecumule[tid]+(nbrecumule[tid+1]-nbrecumule[tid])*generateUniform(&m_mtstate[tid],&m_config[tid],&m_istate[tid]);
  367.       index2 = (index2>=index1) ? (index2+1) : index2;
  368.    
  369.       for (i=0;i<3;i++) {
  370.        c_m[i] = (mol[index1].vit[i] + mol[index2].vit[i])/2;
  371.        c_r[i] = mol[index1].vit[i] - mol[index2].vit[i];
  372.      }
  373.      norm_c_r = sqrt(c_r[0]*c_r[0] + c_r[1]*c_r[1] + c_r[2]*c_r[2]);
  374.    } while(norm_c_r<c_r_max*generateUniform(&m_mtstate[tid],&m_config[tid],&m_istate[tid]));
  375.  col[tid]++;
  376.  epsilon = 2*generateUniform(&m_mtstate[tid],&m_config[tid],&m_istate[tid])*float(M_PI);
  377.  chi = float(M_PI)*generateUniform(&m_mtstate[tid],&m_config[tid],&m_istate[tid]);
  378.  seps = sin(epsilon);
  379.  schi=sin(chi);
  380.  cchi=cos(chi);
  381.  ceps=cos(epsilon);
  382.  B = sqrt(c_r[1]*c_r[1] + c_r[2]*c_r[2]);
  383.  c_r_star[0] = (cchi * c_r[0] + schi * seps * B);
  384.  c_r_star[1] = cchi * c_r[1] + schi * (norm_c_r * c_r[2] * ceps - c_r[0] * c_r[1] * seps)/B;
  385.  c_r_star[2] = cchi * c_r[2] - schi * (norm_c_r * c_r[1] * ceps + c_r[0] * c_r[2] * seps)/B;
  386.  for (j=0;j<3;++j) {
  387.     mol[index1].vit[j] = (c_r_star[j] + 2*c_m[j])/2;
  388.     mol[index2].vit[j] = (-c_r_star[j] + 2*c_m[j])/2;
  389.  }
  390.  n_aver_for_collision_interval = ((taille[tid] == 0) ? (nbrecumule[tid+1]-nbrecumule[tid]) : ((taille[tid]) / (sample[tid])));
  391.     delta_t_coll = (2*float(M_SQRT2)/((nbrecumule[tid+1]-nbrecumule[tid]))) * (n_per_cell_ref)/(n_aver_for_collision_interval * norm_c_r);
  392.     delai[tid] += delta_t_coll; 
  393. }
  394. delai[tid] = delai[tid] - float(1.0)/100;
  395.  }
  396. nbrecell[tid]=0;
  397. }
  398. void cuda_init_inflow_distribution(float *normalization_1,float *normalization_2,float *f_max_1,float *f_max_2)
  399.      /* Initializes the static variables used repeatedly in inflow_distribution_left  
  400. and inflow_distribution_right  
  401.      */
  402. {
  403.   float u_f_max_1, u_f_max_2;
  404.   *normalization_1 = 0.5 * exp(- beta2_o_beta1 * beta2_o_beta1 * mean_vel_1 * mean_vel_1) / (beta2_o_beta1 * beta2_o_beta1)
  405.     + mean_vel_1 * 0.5 * sqrt(M_PI) * (1.0 + erf( beta2_o_beta1 * mean_vel_1)) / beta2_o_beta1;
  406.  
  407.   *normalization_2 = 0.5 * exp(- beta2_o_beta1 * beta2_o_beta1 * mean_vel_2 * mean_vel_2) / (beta2_o_beta1 * beta2_o_beta1)
  408.     + mean_vel_2 * 0.5 * sqrt(M_PI) * (1.0 + erf( beta2_o_beta1 * mean_vel_2)) / beta2_o_beta1;
  409.  
  410.   u_f_max_1 = 0.5 * (mean_vel_1 + sqrt(mean_vel_1 * mean_vel_1 + 2.0 / (beta2_o_beta1 * beta2_o_beta1)));
  411.  
  412.   *f_max_1 = u_f_max_1 * exp(- beta2_o_beta1 * beta2_o_beta1 * pow((u_f_max_1 - mean_vel_1),2)) / *normalization_1;
  413.  
  414.   u_f_max_2 = 0.5 * (mean_vel_2 + sqrt(mean_vel_2 * mean_vel_2 + 2.0 / (beta2_o_beta1 * beta2_o_beta1)));
  415.  
  416.   *f_max_2 = u_f_max_2 * exp(- beta2_o_beta1 * beta2_o_beta1 * pow((u_f_max_2 - mean_vel_2),2)) / *normalization_2;
  417. }
  418. __global__ void UpdateStat(statistic *stat,mlcl *mol,int *nbrecumu)
  419. {
  420. int tid = blockIdx.x*blockDim.x + threadIdx.x;
  421. int i;
  422. stat[tid].size = stat[tid].size + nbrecumu[tid+1]-nbrecumu[tid];
  423.     stat[tid].samples ++;
  424.    
  425.     for (i=nbrecumu[tid];i<nbrecumu[tid+1];i++) {
  426.     stat[tid].sum_v[0] += mol[i].vit[0];
  427.     stat[tid].sum_v[1] += mol[i].vit[1];
  428.     stat[tid].sum_v[2] += mol[i].vit[2];
  429.     stat[tid].sum_v2[0] += mol[i].vit[0]*mol[i].vit[0];
  430.     stat[tid].sum_v2[1] += mol[i].vit[1]*mol[i].vit[1];
  431.     stat[tid].sum_v2[2] += mol[i].vit[2]*mol[i].vit[2];
  432.   }
  433. }


Message édité par gilou le 03-05-2011 à 11:11:10
n°2073757
GrosBocdel
Posté le 04-05-2011 à 13:21:23  profilanswer
 

cudaThreadSynchronize(); après ton appel de kernel?


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Divers

  CUDA : Boucle for qui prend de plus en plus de temps

 

Sujets relatifs
[PHP] Utiliser 2 requêtes SQL dans 1 boucle ?Quel language, et combien de temps ?
temps nécessaire au développement d'une couche securiséeTemps d'exécution en millisecondes (linux/windows) + multithreads
Excel : boucle copie de plages sous conditionBoucle vba qui ouvre des fichiers avec noms variables
Mesurer le temps d'exec d'une fonctionvariable dans une boucle
[Résolu] Update multiple dans une boucle whileEquivalence boucle while(- && - && -) et while (- || - || -)
Plus de sujets relatifs à : CUDA : Boucle for qui prend de plus en plus de temps


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR