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.