0% found this document useful (0 votes)
74 views2 pages

File: /home/kushan/Desktop/CS/Untitled Folder/question3.c Page 1 of 2

This document contains the code for a parallel program that calculates the integral of a function using the trapezoidal rule across multiple threads. It defines global variables, thread and mutex lock variables. The main function takes user input, initializes threads and mutex, calls the thread work function for each thread, joins the threads and prints the final integral result. The thread work function calculates the integral over its assigned interval and adds it to the total using a mutex lock for thread-safety. Auxiliary functions include one for applying the trapezoidal rule and another for the function to integrate.

Uploaded by

chanuka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views2 pages

File: /home/kushan/Desktop/CS/Untitled Folder/question3.c Page 1 of 2

This document contains the code for a parallel program that calculates the integral of a function using the trapezoidal rule across multiple threads. It defines global variables, thread and mutex lock variables. The main function takes user input, initializes threads and mutex, calls the thread work function for each thread, joins the threads and prints the final integral result. The thread work function calculates the integral over its assigned interval and adds it to the total using a mutex lock for thread-safety. Auxiliary functions include one for applying the trapezoidal rule and another for the function to integrate.

Uploaded by

chanuka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

File: /home/kushan/Desktop/CS/Untitled Folder/question3.

c Page 1 of 2

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4
5 /* Global variables */
6 double a, b, h;
7 long double total;
8 int thread_count, n, local_n;
9
10 /* Critical section lock variables */
11 pthread_mutex_t mutex;
12
13 /* Serial function */
14 void *Thread_work(void* rank);
15 long double Trap(double local_a, double local_b, int local_n, double h);
16 long double f(double x);
17
18 /* Parallel function */
19 void *Thread_work(void* rank);
20
21 /*--------------------------------------------------------------*/
22 int main(int argc, char** argv) {
23
24 pthread_t* thread_handles;
25 long i;
26
27 total = 0.0;
28
29 if (argc != 2) {
30 fprintf(stderr, "usage: %s <number of threads>\n",argv[0]);
31 exit(0);
32 }/* this is used to print warning when the running argument is wrong*/
33
34 thread_count = strtol(argv[1], NULL, 10);
35
36
37 printf("Enter a, b, n\n");
38 scanf("%lf %lf %d", &a, &b, &n);
39 h = (b-a)/n;
40 local_n = n/thread_count;
41
42 thread_handles = malloc(thread_count*sizeof(pthread_t));
43
44 pthread_mutex_init(&mutex, NULL);
45
46
47 for (i = 0; i < thread_count; i++) {
48 pthread_create(&thread_handles[i],NULL,Thread_work,(void*)i);
49 }
50
51 for (i = 0; i < thread_count; i++) {
52 pthread_join(thread_handles[i],NULL);
53 }
54
55 printf("With n = %d trapezoids, our estimate\n",n);
56 printf("of the integral from %lf to %lf = %LF\n",a, b, total);
57
58 pthread_mutex_destroy(&mutex);
59
60 return 0;
61 }
62
63
64 /*--------------------------------------------------------------*/
65 void *Thread_work(void* rank) {
66 long double local_a; /* Left endpoint my thread */
67 long double local_b; /* Right endpoint my thread */
68 long double my_int; /* Integral over my interval */
69 long my_rank = (long) rank;
70
71
72 local_a = a + my_rank*local_n*h; /* Length of each process' interval of
integration = */
73 local_b = local_a + local_n*h; /* local_n*h. So my interval starts
at: */
File: /home/kushan/Desktop/CS/Untitled Folder/question3.c Page 2 of 2

74
75 my_int = Trap(local_a, local_b, local_n, h);
76
77
78 pthread_mutex_lock(&mutex); /* in here the MUTEX is used to critical sestion*/
79 total += my_int;
80 pthread_mutex_unlock(&mutex);
81
82 }
83
84 /*--------------------------------------------------------------*/
85 long double Trap(double local_a, double local_b, int local_n, double h) {
86
87 long double integral; /* Store result in integral */
88 long double x;
89 long int i;
90
91 integral = (f(local_a) + f(local_b))/2.0;
92 x = local_a;
93 for (i = 1; i <= local_n-1; i++) {
94 x = local_a + i*h;
95 integral += f(x);
96 }
97 integral = integral*h;
98 return integral;
99 }
100
101
102 /*--------------------------------------------------------------*/
103 long double f(double x) {
104 long double return_val;
105
106 //return_val = x*x; /*we can edit function like this */
107
108 return_val = (71/3)*x + 2;
109 return return_val;
110 }
111
112

You might also like