When i implement solution for Monte Carlo method to calculate pi number, in which the circle with center has the coordinates (0,0), the radius is 1 and is inscribed in the square, the given formula is as follows: pi = 4 * (number of points in circle) / (total number of points ). I implemented the solution using 2 programs serial.c (single process) and multithread.c (multi-thread process) to compare the execution speed of these two solutions.
Program serial.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
void circle_point();
static long int total_point;
static long int count_circle=0;
clock_t start_time, end_time;
int main(int argc, char *argv[]){
if(argc==1){
printf("Enter number point\n");
return -1;
}
if(argc!=2){
printf("Argument is wrong\n");
return -1;
}
total_point=atoll(argv[1]);
start_time=clock();
circle_point();
double pi=4.0*(double)count_circle/(double)total_point;
end_time=clock();
printf("PI = %17.15f\n",pi);
printf("Time to compute= %g second\n",(double)(end_time-start_time)/CLOCKS_PER_SEC);
return 0;
}
void circle_point(){
srand(time(NULL));
int i;
for(i=0;i<total_point;i++){
double x= (double)rand()/(double)RAND_MAX;
double y=(double)rand()/(double)RAND_MAX;
double r= sqrt(x*x+y*y);
if(r<=1) count_circle+=1;
}
}
Program multithread.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <pthread.h>
#define NUM_THREAD 4
void* circle_point(void *param);
pthread_t tid[NUM_THREAD]={0};
int count[NUM_THREAD]={0};
clock_t start_time, end_time;
static long int total_point;
static long int count_circle=0;
int main(int argc, char const *argv[]){
if(argc==1){
printf("Enter number point\n");
return -1;
}
if(argc!=2){
printf("Argument is wrong\n");
return -1;
}
total_point=atoll(argv[1])/NUM_THREAD;
start_time= clock();
srand(time(NULL));
static int i;
for(i=0; i<NUM_THREAD;i++)
pthread_create(&tid[i],NULL,circle_point,&count[i]);
for(i=0;i<NUM_THREAD;i++){
pthread_join(tid[i],NULL);
count_circle+=count[i];
}
double pi=4.0*(double)count_circle/(double)total_point/(double)NUM_THREAD;
end_time=clock();
printf("PI = %17.15f\n",pi);
printf("Time to compute= %g second\n",(double)(end_time-start_time)/CLOCKS_PER_SEC);
return 0;
}
void* circle_point(void *param){
int *pcount= (int*)param;
int i;
for(i=0; i<total_point;i++){
double x= (double)rand()/(double)RAND_MAX;
double y=(double)rand()/(double)RAND_MAX;
double r= sqrt(x*x+y*y);
if(r<=1) *pcount=*pcount+1;
}
pthread_exit(0);
}
The problem I encountered is that with the same total number of points input, for example 10^7, the execution speed of the serial is 0.45 seconds while the execution speed of multithread is 6.06 seconds, similar to other inputs, the execution time of the serial is always greater than the multithread. I do not understand what problems I have encountered? Because in theory, multithread will execute faster than serial, so is there any case in fact that serial execute faster than multithread as I have encountered.