多线程蒙特卡罗

时间:2015-07-01 19:47:57

标签: montecarlo

我正在试图在我的蒙特卡洛程序中并行化一个循环,该程序旨在模拟锰氧化物的磁性。该环路计算晶格中的偶极磁相互作用。我是多线程的新手,这是我的第一次测试。它不起作用。在下面的代码中,请告诉我哪里有错误,并提前解决

#include <stdio.h>                                               
#include <stdlib.h>   
#include <math.h>                       
#include <time.h>
#include <pthread.h>              
#include <unistd.h>   
.
.
.
#define CORES                     4                     // number of threads

pthread_mutex_t  mutex = PTHREAD_MUTEX_INITIALIZER;

int     rc,n,N,i,j,p,l,NTOT = ILIGNE*ICOLONE*ICOUCHE,Nc;
double  CTEC; // current thread contribution to energy change
double  V,G,E,F,dU;
FILE    *voisins = NULL;
float   r[3],d,w[3];
void *Dipolar_Interaction(){
   float spin[3*NTOT];
   n = 1;
   while(n < Nc/CORES){
      printf("%f\n",(float)n/(float)Nc);
      fscanf(voisins,"%d%d%f%f%f%f",&i,&j,&r[0],&r[1],&r[2],&d);
      V = 0.0;E = 0.0;F = 0.0;
      for(p = 0;p < 3;p++){
         V += (D/pow(d,3.0))*(spin[3*i-3+p]-w[p])*spin[3*j-3+p];
         E += (spin[3*i-3+p]-w[p])*r[p];
         F += spin[3*j-3+p]*r[p];
      }
      G = -3*(D/pow(d,5.0))*E*F;
      CTEC += (V+G);
      n++;
   }
   rc = pthread_mutex_lock(&mutex);
   dU += CTEC;
   rc = pthread_mutex_unlock(&mutex);
   pthread_exit(NULL);
}
main(){
   int   th;
   .
   .
   .
   pthread_t thread[CORES];
   .
   .
   .
   for( th = 1; th <= CORES; th++ )
      pthread_create(&thread[th], NULL, Dipolar_Interaction, (void*)th);
   for( th = 1; i <= CORES; th++ )
      pthread_join(thread[th], NULL);
   pthread_exit(NULL);
   .
   .
   .
}

缺少代码。我无法编写整个程序,因为它很长(532行)并且它是可能发表的研究的主题。

0 个答案:

没有答案