1 | #include <stdio.h>
|
2 | #include <math.h>
|
3 | #include <stdlib.h>
|
4 | #include <string.h>
|
5 | #include <pthread.h>
|
6 | #define N25000000 25000000
|
7 |
|
8 |
|
9 | double pi;
|
10 | double invsqrt2pi;
|
11 |
|
12 | int thread_count;
|
13 | pthread_mutex_t mutex1;
|
14 |
|
15 |
|
16 | struct arguments
|
17 | {
|
18 | double start;
|
19 | double end;
|
20 | double *final_res;
|
21 | int num_pieces;
|
22 | };
|
23 |
|
24 |
|
25 | void createArguments(struct arguments *args, double *final_res, double start, double end, int pieces);
|
26 | void* integral(void *arg);
|
27 | double f(double x);
|
28 | int divideWork(int thread_count);
|
29 | double simpson(double x1,double x2,double x3);
|
30 |
|
31 |
|
32 |
|
33 | int main(int argc, char* argv[])
|
34 | {
|
35 | double final_result;
|
| 1 | Variable 'final_result' declared without an initial value | |
|
36 | long thread;
|
37 | if(argc < 2)
|
| |
| |
38 | {
|
39 | printf("No specified number of threads, defaulting to 2 threads\n");
|
40 | thread_count = 2;
|
41 | }
|
42 | else
|
43 | thread_count = strtol(argv[1], NULL((void*)0), 10);
|
44 |
|
45 | pthread_mutex_init(&mutex1, NULL((void*)0));
|
46 | pthread_t* thread_handles;
|
47 | thread_handles = malloc(thread_count*sizeof(thread_handles));
|
48 |
|
49 | pi=2*acos(0);
|
50 | invsqrt2pi=1/sqrt(2*pi);
|
51 |
|
52 | int thread_work_part = divideWork(thread_count);
|
53 | double start = -1;
|
54 | double sub_parts = (double)2/thread_count;
|
55 |
|
56 | for(thread = 0; thread < thread_count; thread ++)
|
| 4 | | Assuming 'thread' is >= 'thread_count' | |
|
| 5 | | Loop condition is false. Execution continues on line 67 | |
|
57 | {
|
58 | double start_pos = (start + (sub_parts*(int)thread));
|
59 | struct arguments *arg;
|
60 | arg = malloc(sizeof(*arg));
|
61 |
|
62 | createArguments(arg ,&final_result, start_pos, start_pos + sub_parts, thread_work_part);
|
63 | pthread_create(&thread_handles[thread], NULL((void*)0), integral, (void*)arg);
|
64 | }
|
65 |
|
66 |
|
67 | for(thread =0; thread < thread_count; thread++)
|
| 6 | | Loop condition is false. Execution continues on line 71 | |
|
68 | {
|
69 | pthread_join(thread_handles[thread], NULL((void*)0));
|
70 | }
|
71 | free(thread_handles);
|
72 |
|
73 | printf("the result is %.6f\n",final_result);
|
| 7 | | Function call argument is an uninitialized value |
|
74 | return 0;
|
75 | }
|
76 |
|
77 |
|
78 | int divideWork(int thread_count)
|
79 | {
|
80 | if(N25000000 % thread_count != 0)
|
81 | {
|
82 | printf("Thread count not divisible by number of tasks! aborting\n");
|
83 | exit(-1);
|
84 | }
|
85 | else
|
86 | return N25000000 / thread_count;
|
87 | }
|
88 |
|
89 |
|
90 | void createArguments(struct arguments *args, double *final_res, double start, double end, int pieces)
|
91 | {
|
92 | args->start = start;
|
93 | args->end = end;
|
94 | args->final_res = final_res;
|
95 | args->num_pieces = pieces;
|
96 | }
|
97 |
|
98 | double f(double x)
|
99 | {
|
100 | return invsqrt2pi*exp(-0.5*x*x);
|
101 | }
|
102 |
|
103 |
|
104 | double simpson(double x1,double x2,double x3)
|
105 | {
|
106 | return (x2-x1)*(f(x1)+4*f(x2)+f(x3))/3;
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | void* integral(void *arg)
|
114 | {
|
115 | struct arguments* args = (struct arguments*)arg;
|
116 | double start = args -> start;
|
117 | double end = args -> end;
|
118 | int num_pieces = args -> num_pieces;
|
119 | double *final_result = args -> final_res;
|
120 | double res = 0.0;
|
121 | double h = (end-start)/num_pieces;
|
122 |
|
123 | for(int i=0;i<num_pieces;i+=2)
|
124 | {
|
125 | res += simpson(start+i*h, start+(i+1)*h, start+(i+2)*h);
|
126 | }
|
127 |
|
128 | pthread_mutex_lock(&mutex1);
|
129 | *final_result += res;
|
130 | pthread_mutex_unlock(&mutex1);
|
131 |
|
132 | return NULL((void*)0);
|
133 | }
|