c - Thread program that calculates pi -
writing program class calculates pi. got coding done , program runs - values doesn't work. program supposed take in 2 parameters, 1st 1 number of elements calculate pi , second number of threads use. works flawlessly many numbers 1, 2, 5 ,6, 7, 8 , others doesn't calculate well. it's limits in loops can't figure out. appreciated. code:
#include <stdio.h> #include <stdlib.h> #include <pthread.h> //global variables int n, t; double *vsum; //pie function void* pie_runner(void* arg) { long j = (long)arg; //double *limit_ptr = (double*) arg; //double j = *limit_ptr; //for(int = (j-1)*n/t; < n*(j) /t; i++) for(int = (n/t)*(j-1); < ((n/t)*(j)); i++) { if(i % 2 == 0){ vsum[j] += 4.0/((2*j)*(2*j+1)*(2*j+2)); //printf("vsum %lu = %f\n", j, vsum[j]); } else{ vsum[j] -= 4.0/((2*j)*(2*j+1)*(2*j+2)); //printf("vsum %lu = %f\n", j, vsum[j]); } } pthread_exit(0); } int main(int argc, char **argv) { if(argc != 3) { printf("error: must send 2 parameters, sent %d\n", argc-1); exit(1); } n = atoi(argv[1]); t = atoi(argv[2]); vsum = malloc((t+1) * sizeof(*vsum)); if(vsum == null) { fprintf(stderr, "memory allocation problem\n"); exit(1); } if(n <= t) { printf("error: number of terms must greater number of threads.\n"); exit(1); } for(int p=1; p<=t; p++) //initialize array 0 { vsum[p] = 0; } double pie = 3.0; //launch threads pthread_t tids[t+1]; for(long = 1; i<=t; i++) { pthread_attr_t attr; pthread_attr_init(&attr); pthread_create(&tids[i], &attr, pie_runner, (void*)i); } //wait threads... for(int k = 1; k<=t; k++) { pthread_join(tids[k], null); } for(int x=1; x<=t; x++) { pie += vsum[x]; } printf("pi computed %d terms in %d threads %.20f\n", n, t, pie); //printf("pi computed %d terms in %d threads %20f\n", n, t, pie); free(vsum); }
values not working:
./pie1 2 1 pi computed 2 terms in 1 threads 3.00000000000000000000 ./pie1 3 1 pi computed 3 terms in 1 threads 3.16666666666666651864 ./pie1 3 2 pi computed 3 terms in 2 threads 3.13333333333333330373 ./pie1 4 2 pi computed 4 terms in 2 threads 3.00000000000000000000 ./pie1 4 1 pi computed 4 terms in 1 threads 3.00000000000000000000 ./pie1 4 3 pi computed 4 terms in 3 threads 3.14523809523809516620 ./pie1 10 1 pi computed 10 terms in 1 threads 3.00000000000000000000 ./pie1 10 2 pi computed 10 terms in 2 threads 3.13333333333333330373 ./pie1 10 3 pi computed 10 terms in 3 threads 3.14523809523809516620 ./pie1 10 4 pi computed 10 terms in 4 threads 3.00000000000000000000 ./pie1 10 5 pi computed 10 terms in 5 threads 3.00000000000000000000 ./pie1 10 6 pi computed 10 terms in 6 threads 3.14088134088134074418 ./pie1 10 7 pi computed 10 terms in 7 threads 3.14207181707181693042 ./pie1 10 8 pi computed 10 terms in 8 threads 3.14125482360776464574 ./pie1 10 9 pi computed 10 terms in 9 threads 3.14183961892940200045 ./pie1 11 2 pi computed 11 terms in 2 threads 3.13333333333333330373 ./pie1 11 4 pi computed 11 terms in 4 threads 3.00000000000000000000
i think problem used expression n/t
in code. here n
, t
integers. c language (and many others) treat integer dividion not same way hand.
for example, 5/3 = 1, not 1.66667. 1.66667, can cast 1 of integer double.
Comments
Post a Comment