|
1 | | -#ifdef HAVE_CONFIG_H |
2 | | -#include <config.h> |
3 | | -#endif |
4 | | - |
5 | | -#include <assert.h> |
6 | | -#include <math.h> |
7 | | -#include <qthread/qthread.h> |
8 | | -#include <stdio.h> |
9 | | -#include <stdlib.h> |
10 | | - |
11 | | -#include "argparsing.h" |
12 | | - |
13 | | -// https://www.geeksforgeeks.org/comparison-float-value-c/ |
14 | | -// https://dotnettutorials.net/lesson/taylor-series-using-recursion-in-c/ |
15 | | -// https://www.studytonight.com/c/programs/important-concepts/sum-of-taylor-series |
16 | | - |
17 | | -struct parts { |
18 | | - int length; |
19 | | - double exp; |
20 | | - double ans; |
21 | | - aligned_t cond; |
22 | | -}; |
23 | | - |
24 | | -// https://www.w3resource.com/c-programming-exercises/math/c-math-exercise-24.php |
25 | | -static double taylor_exponential_core(int n, double x, int yield) { |
26 | | - double exp_sum = 1; |
27 | | - for (int i = n - 1; i > 0; --i) { |
28 | | - exp_sum = 1 + x * exp_sum / i; |
29 | | - if (yield) qthread_yield(); |
30 | | - } |
31 | | - return exp_sum; |
32 | | -} |
33 | | - |
34 | | -static aligned_t taylor_exponential(void *arg) { |
35 | | - struct parts *te = (struct parts *)arg; |
36 | | - te->ans = taylor_exponential_core(te->length, te->exp, 1); |
37 | | - return 0; |
38 | | -} |
39 | | - |
40 | | -static void startQthread(struct parts *teParts) { |
41 | | - qthread_empty(&teParts->cond); |
42 | | - |
43 | | - int ret = qthread_fork(taylor_exponential, teParts, &teParts->cond); |
44 | | - test_check(ret == QTHREAD_SUCCESS); |
45 | | -} |
46 | | - |
47 | | -static aligned_t checkDoubleAsQthreads(void) { |
48 | | - struct parts teParts1 = {250, 9.0, 0.0}; |
49 | | - struct parts teParts2 = {50, 3.0, 0.0}; |
50 | | - struct parts teParts3 = {150, 11.0, 0.0}; |
51 | | - |
52 | | - startQthread(&teParts1); |
53 | | - startQthread(&teParts2); |
54 | | - startQthread(&teParts3); |
55 | | - |
56 | | - int ret = qthread_readFF(NULL, &teParts1.cond); |
57 | | - test_check(ret == QTHREAD_SUCCESS); |
58 | | - |
59 | | - ret = qthread_readFF(NULL, &teParts2.cond); |
60 | | - test_check(ret == QTHREAD_SUCCESS); |
61 | | - |
62 | | - ret = qthread_readFF(NULL, &teParts3.cond); |
63 | | - test_check(ret == QTHREAD_SUCCESS); |
64 | | - |
65 | | - double threshold = 1E-15; |
66 | | - |
67 | | - double expected_1 = 8103.0839275753824; |
68 | | - double rel_error_1 = fabs(expected_1 - teParts1.ans) / fabs(expected_1); |
69 | | - test_check(rel_error_1 < threshold); |
70 | | - |
71 | | - double expected_2 = 20.085536923187668; |
72 | | - double rel_error_2 = fabs(expected_2 - teParts2.ans) / fabs(expected_2); |
73 | | - test_check(rel_error_2 < threshold); |
74 | | - |
75 | | - double expected_3 = 59874.141715197809; |
76 | | - double rel_error_3 = fabs(expected_3 - teParts3.ans) / fabs(expected_3); |
77 | | - test_check(rel_error_3 < threshold); |
78 | | - |
79 | | - return 0; |
80 | | -} |
81 | | - |
82 | | -static void checkDoubleAsQthread(void) { |
83 | | - int ret = -1; |
84 | | - struct parts teParts = {250, 9.0, 0.0}; |
85 | | - qthread_empty(&teParts.cond); |
86 | | - |
87 | | - ret = qthread_fork(taylor_exponential, &teParts, &teParts.cond); |
88 | | - test_check(ret == QTHREAD_SUCCESS); |
89 | | - |
90 | | - ret = qthread_readFF(NULL, &teParts.cond); |
91 | | - test_check(ret == QTHREAD_SUCCESS); |
92 | | - |
93 | | - double expected = 8103.0839275753824; |
94 | | - double rel_error = fabs(expected - teParts.ans) / fabs(expected); |
95 | | - test_check(rel_error < 1E-15); |
96 | | -} |
97 | | - |
98 | | -static void checkDouble(void) { |
99 | | - double ans = taylor_exponential_core(250, 9.0, 0); |
100 | | - double expected = 8103.0839275753824; |
101 | | - double rel_error = fabs(expected - ans) / fabs(expected); |
102 | | - test_check(rel_error < 1E-15); |
103 | | -} |
104 | | - |
105 | | -int main(void) { |
106 | | - checkDouble(); |
107 | | - |
108 | | - int status = qthread_initialize(); |
109 | | - test_check(status == QTHREAD_SUCCESS); |
110 | | - |
111 | | - checkDoubleAsQthread(); |
112 | | - checkDoubleAsQthreads(); |
113 | | - return EXIT_SUCCESS; |
114 | | -} |
| 1 | +#ifdef HAVE_CONFIG_H |
| 2 | +#include <config.h> |
| 3 | +#endif |
| 4 | + |
| 5 | +#include <assert.h> |
| 6 | +#include <math.h> |
| 7 | +#include <qthread/qthread.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | + |
| 11 | +#include "argparsing.h" |
| 12 | + |
| 13 | +// https://www.geeksforgeeks.org/comparison-float-value-c/ |
| 14 | +// https://dotnettutorials.net/lesson/taylor-series-using-recursion-in-c/ |
| 15 | +// https://www.studytonight.com/c/programs/important-concepts/sum-of-taylor-series |
| 16 | + |
| 17 | +struct parts { |
| 18 | + int length; |
| 19 | + double exp; |
| 20 | + double ans; |
| 21 | + aligned_t cond; |
| 22 | +}; |
| 23 | + |
| 24 | +// https://www.w3resource.com/c-programming-exercises/math/c-math-exercise-24.php |
| 25 | +static double taylor_exponential_core(int n, double x, int yield) { |
| 26 | + double exp_sum = 1; |
| 27 | + for (int i = n - 1; i > 0; --i) { |
| 28 | + exp_sum = 1 + x * exp_sum / i; |
| 29 | + if (yield) qthread_yield(); |
| 30 | + } |
| 31 | + return exp_sum; |
| 32 | +} |
| 33 | + |
| 34 | +static aligned_t taylor_exponential(void *arg) { |
| 35 | + struct parts *te = (struct parts *)arg; |
| 36 | + te->ans = taylor_exponential_core(te->length, te->exp, 1); |
| 37 | + return 0; |
| 38 | +} |
| 39 | + |
| 40 | +static void startQthread(struct parts *teParts) { |
| 41 | + qthread_empty(&teParts->cond); |
| 42 | + |
| 43 | + int ret = qthread_fork(taylor_exponential, teParts, &teParts->cond); |
| 44 | + test_check(ret == QTHREAD_SUCCESS); |
| 45 | +} |
| 46 | + |
| 47 | +static aligned_t checkDoubleAsQthreads(void) { |
| 48 | + struct parts teParts1 = {250, 9.0, 0.0}; |
| 49 | + struct parts teParts2 = {50, 3.0, 0.0}; |
| 50 | + struct parts teParts3 = {150, 11.0, 0.0}; |
| 51 | + |
| 52 | + startQthread(&teParts1); |
| 53 | + startQthread(&teParts2); |
| 54 | + startQthread(&teParts3); |
| 55 | + |
| 56 | + int ret = qthread_readFF(NULL, &teParts1.cond); |
| 57 | + test_check(ret == QTHREAD_SUCCESS); |
| 58 | + |
| 59 | + ret = qthread_readFF(NULL, &teParts2.cond); |
| 60 | + test_check(ret == QTHREAD_SUCCESS); |
| 61 | + |
| 62 | + ret = qthread_readFF(NULL, &teParts3.cond); |
| 63 | + test_check(ret == QTHREAD_SUCCESS); |
| 64 | + |
| 65 | + double threshold = 1E-15; |
| 66 | + |
| 67 | + double expected_1 = 8103.0839275753824; |
| 68 | + double rel_error_1 = fabs(expected_1 - teParts1.ans) / fabs(expected_1); |
| 69 | + test_check(rel_error_1 < threshold); |
| 70 | + |
| 71 | + double expected_2 = 20.085536923187668; |
| 72 | + double rel_error_2 = fabs(expected_2 - teParts2.ans) / fabs(expected_2); |
| 73 | + test_check(rel_error_2 < threshold); |
| 74 | + |
| 75 | + double expected_3 = 59874.141715197809; |
| 76 | + double rel_error_3 = fabs(expected_3 - teParts3.ans) / fabs(expected_3); |
| 77 | + test_check(rel_error_3 < threshold); |
| 78 | + |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +static void checkDoubleAsQthread(void) { |
| 83 | + int ret = -1; |
| 84 | + struct parts teParts = {250, 9.0, 0.0}; |
| 85 | + qthread_empty(&teParts.cond); |
| 86 | + |
| 87 | + ret = qthread_fork(taylor_exponential, &teParts, &teParts.cond); |
| 88 | + test_check(ret == QTHREAD_SUCCESS); |
| 89 | + |
| 90 | + ret = qthread_readFF(NULL, &teParts.cond); |
| 91 | + test_check(ret == QTHREAD_SUCCESS); |
| 92 | + |
| 93 | + double expected = 8103.0839275753824; |
| 94 | + double rel_error = fabs(expected - teParts.ans) / fabs(expected); |
| 95 | + test_check(rel_error < 1E-15); |
| 96 | +} |
| 97 | + |
| 98 | +static void checkDouble(void) { |
| 99 | + double ans = taylor_exponential_core(250, 9.0, 0); |
| 100 | + double expected = 8103.0839275753824; |
| 101 | + double rel_error = fabs(expected - ans) / fabs(expected); |
| 102 | + test_check(rel_error < 1E-15); |
| 103 | +} |
| 104 | + |
| 105 | +int main(void) { |
| 106 | + checkDouble(); |
| 107 | + |
| 108 | + int status = qthread_initialize(); |
| 109 | + test_check(status == QTHREAD_SUCCESS); |
| 110 | + |
| 111 | + checkDoubleAsQthread(); |
| 112 | + checkDoubleAsQthreads(); |
| 113 | + return EXIT_SUCCESS; |
| 114 | +} |
0 commit comments