I need to run a program on different ammounts of threads so I can compare the times spend.
My initial program is this one, a mount carlo simulation:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#define SEED 35791246
main(int argc, char* argv)
{
int points=10000000;
double x,y;
int i,count=0;
double z;
double pi;
clock_t begin = clock();
srand(SEED);
count=0;
for ( i=0; i<points; i++) {
x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;
z = x*x+y*y;
if (z<=1) count++;
}
pi=(double)count/points*4;
printf("The number o variables is %d , and the pi estimation is %g \n",points,pi);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time Spent: %f \n", time_spent);
}
Everything works fine, but now I need to run it differents amounts of threads, let's say 2 and 4, I tried this but its not working:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#define SEED 35791246
#define NUM_THREADS1 2
#define NUM_THREADS2 4
main(int argc, char* argv)
{
pthread_t thread1, thread2;
pthread_t thread1[NUM_THREADS1]
pthread_t thread2[NUM_THREADS2]
int points=10000000;
double x,y;
int i,count=0;
double z;
double pi;
srand(SEED);
count=0;
for ( i=0; i< NUM_THREADS1; i++) {
x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;
z = x*x+y*y;
if (z<=1) count++;
}
pi=(double)count/points*4;
printf("The number o variables is %d , and the pi estimation is %g \n",points,pi);
srand(SEED);
count=0;
for ( i=0; i< NUM_THREADS2; i++) {
x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;
z = x*x+y*y;
if (z<=1) count++;
}
pi=(double)count/points*4;
printf("The number o variables is %d , and the pi estimation is %g \n",points,pi);
}
I'm new to threads so I could use a little input on how to solve this. Thank you in advance!