1 #include <stdio.h>
2 #include <stdint.h>
3 #include <assert.h>
4
5 #include <pthreadpool.h>
6
7 struct array_addition_context {
8 double *augend;
9 double *addend;
10 double *sum;
11 };
12
add_arrays(struct array_addition_context * context,size_t i)13 static void add_arrays(struct array_addition_context* context, size_t i) {
14 context->sum[i] = context->augend[i] + context->addend[i];
15 }
16
17 #define ARRAY_SIZE 4
18
main()19 int main() {
20 double augend[ARRAY_SIZE] = { 1.0, 2.0, 4.0, -5.0 };
21 double addend[ARRAY_SIZE] = { 0.25, -1.75, 0.0, 0.5 };
22 double sum[ARRAY_SIZE];
23
24 pthreadpool_t threadpool = pthreadpool_create(0);
25 assert(threadpool != NULL);
26
27 const size_t threads_count = pthreadpool_get_threads_count(threadpool);
28 printf("Created thread pool with %zu threads\n", threads_count);
29
30 struct array_addition_context context = { augend, addend, sum };
31 pthreadpool_parallelize_1d(threadpool,
32 (pthreadpool_task_1d_t) add_arrays,
33 (void**) &context,
34 ARRAY_SIZE,
35 PTHREADPOOL_FLAG_DISABLE_DENORMALS /* flags */);
36
37 pthreadpool_destroy(threadpool);
38 threadpool = NULL;
39
40 printf("%8s\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n", "Augend",
41 augend[0], augend[1], augend[2], augend[3]);
42 printf("%8s\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n", "Addend",
43 addend[0], addend[1], addend[2], addend[3]);
44 printf("%8s\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n", "Sum",
45 sum[0], sum[1], sum[2], sum[3]);
46
47 return 0;
48 }
49