Untitled
unknown
plain_text
8 months ago
3.1 kB
4
Indexable
// functions: 3, 7, 11
// 3: sin^2(x)*cos(x)
// 7: sin^2(x)*(1–cos(x))
// 11: sin(x)*(1+cos^2(x))
// a: -2pi
// b: 0
// n 12
#include <stdio.h>
#include <pthread.h>
#include <math.h>
#include <unistd.h>
#define M_PI 3.14159265358979323846
float f1_res;
float f2_res;
float f3_res;
pthread_barrier_t bar_first;
pthread_barrier_t bar_second;
struct args
{
float a;
float b;
float n;
};
void* f1(void* arg)
{
float xi;
float h;
struct args real_args = *((struct args*)arg);
for (int i = 0; i <= real_args.n; i++)
{
h = (real_args.b - real_args.a) / real_args.n;
xi = real_args.a + i * h;
// 3: sin^2(x) * cos(x)
f1_res = sin(xi) * sin(xi) * cos(xi);
pthread_barrier_wait(&bar_first);
pthread_barrier_wait(&bar_second);
}
}
void* f2(void* arg)
{
float xi;
float h;
struct args real_args = *((struct args*)arg);
for (int i = 0; i <= real_args.n; i++)
{
h = (real_args.b - real_args.a) / real_args.n;
xi = real_args.a + i * h;
// 7: sin^2(x) * (1 - cos(x))
f2_res = sin(xi) * sin(xi) * (1 - cos(xi));
pthread_barrier_wait(&bar_first);
pthread_barrier_wait(&bar_second);
}
}
void* f3(void* arg)
{
float xi;
float h;
struct args real_args = *((struct args*)arg);
for (int i = 0; i <= real_args.n; i++)
{
h = (real_args.b - real_args.a) / real_args.n;
xi = real_args.a + i * h;
// 11: sin(x) * (1 + cos^2(x))
f3_res = sin(xi) * (1 + cos(xi) * cos(xi));
pthread_barrier_wait(&bar_first);
pthread_barrier_wait(&bar_second);
}
}
int main()
{
pthread_t thread_1;
pthread_t thread_2;
pthread_t thread_3;
struct args data;
data.a = -2 * M_PI;
data.b = 0;
data.n = 12;
char sep[] = "_______________________";
printf("_%23s_%23s_%23s_%23s_\n", sep, sep, sep, sep);
printf("|%23s|%23s|%23s|%23s|\n", "x", "sin^2(x)*cos(x)", "sin^2(x)*(1 - cos(x))", "sin(x)*(1 + cos^2(x))");
printf("|%23s|%23s|%23s|%23s|\n", sep, sep, sep, sep);
pthread_barrier_init(&bar_first, NULL, 4);
pthread_barrier_init(&bar_second, NULL, 4);
pthread_create(&thread_1, NULL, &f1, &data);
pthread_create(&thread_2, NULL, &f2, &data);
pthread_create(&thread_3, NULL, &f3, &data);
float h;
float xi;
for (int i = 0; i <= data.n; i++)
{
h = (data.b - data.a) / data.n;
xi = data.a + i * h;
printf("|%23.4f|", xi);
pthread_barrier_wait(&bar_first);
printf("%23.4f|", f1_res);
printf("%23.4f|", f2_res);
printf("%23.4f|\n", f3_res);
pthread_barrier_wait(&bar_second);
}
printf("|%23s|%23s|%23s|%23s|\n", sep, sep, sep, sep);
pthread_join(thread_1, NULL);
pthread_join(thread_2, NULL);
pthread_join(thread_3, NULL);
pthread_barrier_destroy(&bar_first);
pthread_barrier_destroy(&bar_second);
return 0;
}
Editor is loading...
Leave a Comment