User:Fottony


 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 
 * 5) include 

volatile double integral_value = 0;

double func(double x){ return exp(1+x*x*x)/exp(1+x*x*x*x); }

void* integrate (void *parm) {   double *interval = parm; double start = interval[0]; double end = interval[1]; double step = interval[2]; double result = 0.0; double x = start; for(x < end; x += step){ result += step*(func(x) + func(x + step))/2.0; }   printf("Result I(%lg,%lg) %lg\n", start, end, result); integral_value += result; return NULL; }

int procnum{ int n_threads = 1; FILE *pipefp = popen("cat /proc/cpuinfo|grep processor|wc -l", "r"); if(pipefp){ fscanf(pipefp, "%d", &n_threads); }  	pclose(pipefp); return n_threads; }

int main{ int n_threads = procnum; pthread_t threads[n_threads]; int i = 0; int n_panels; int step = 1.0/(double)(n_panels); scanf("%d", n_panels); double interval[n_threads][3]; for(i = 0; i < n_threads; i++){ interval[i][0] = (double)(i)/(double)(n_threads); interval[i][1] = (double)(i + 1)/(double)(n_threads); interval[i][2] = step; pthread_create(&(threads[i]), NULL, integrate, &(interval[i][0])); }	for(i = 0; i < n_threads; i++){ void *result = NULL; pthread_join(threads[i], &result); }   printf("Result: %lg\n", integral_value); printf("%d\n", n_threads); return 0; }