0

I am triying to parallelize a loop in my Monte Carlo program which aims to simulate the magnetic properties of manganites. this loop calculates the dipolar magnetic interaction in the lattice. I am new in Multithreading and that's my first test. It don't work. Here below the code and tell me please where is the mistake and thaks in advance

#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);
   .
   .
   .
}

There is a missing code. I can't write the whole program because it's so long (532 line) and it's the subject of a research which may be published.

Rami Zouari
  • 109
  • 1
  • 9
  • It's quite impossible to answer your question without knowing what doesn't work. You'll need to be more specific. That said, I have a lot of experience with Physicists trying to write parallel code to speed up their calculations. It is usually not a good idea, because Physicists, too, need to spend a lot of time figuring out how to write parallel code properly. If I were you, I would look for a decent Monte Carlo package for Python and use that. – zmbq Jul 01 '15 at 19:57
  • I don't think that solves my problem. i passed more than a year writing this code to my specific case which is manganites. I am not simulating a simple lattice with no charge and orbital ordering but i am simulating manganites which are not like other materials. Anyway this is the last step. The program works even without Multithreading. I am just triying to gain time and if it's possible to use a super-calculator that solves all my problems. So what doesn't work is the multithreading sure ;) – Rami Zouari Jul 01 '15 at 20:15
  • Anyway i will look what does makes this package and thank you for your response :) – Rami Zouari Jul 01 '15 at 20:29
  • Well, there is no Monte Carlo here as far as I can see - not a single call to rand(). spin[] array is not initialized at all. PLease check http://stackoverflow.com/help/mcve – Severin Pappadeux Jul 03 '15 at 03:25

0 Answers0