|
| 1 | +/* POSIX version of: https://github.com/cirosantilli/algorithm-cheat/blob/c8489f41c059971008337500013cd9cdf8e86a76/src/cpp/interactive/sum_array_parallel.cpp */ |
| 2 | + |
| 3 | +#define _XOPEN_SOURCE 700 |
| 4 | +#include <assert.h> |
| 5 | +#include <inttypes.h> |
| 6 | +#include <pthread.h> |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include <unistd.h> |
| 10 | + |
| 11 | +typedef uint64_t DataType; |
| 12 | + |
| 13 | +/* Single threaded array sum. */ |
| 14 | +DataType sum_array( |
| 15 | + DataType *begin, |
| 16 | + DataType *end |
| 17 | +) { |
| 18 | + DataType sum = 0; |
| 19 | + while (begin != end) { |
| 20 | + sum += *begin; |
| 21 | + begin++; |
| 22 | + } |
| 23 | + return sum; |
| 24 | +} |
| 25 | + |
| 26 | +typedef struct { |
| 27 | + DataType *begin; |
| 28 | + DataType *end; |
| 29 | + DataType output; |
| 30 | +} SumArrayThreadData; |
| 31 | + |
| 32 | +/* Thread interface. */ |
| 33 | +void* sum_array_thread(void *arg) { |
| 34 | + SumArrayThreadData *thread_data = (SumArrayThreadData*)arg; |
| 35 | + DataType *begin = thread_data->begin; |
| 36 | + DataType *end = thread_data->end; |
| 37 | + thread_data->output = sum_array(begin, end); |
| 38 | + return NULL; |
| 39 | +} |
| 40 | + |
| 41 | +/* Parallel array sum. */ |
| 42 | +DataType sum_array_parallel( |
| 43 | + DataType *begin, |
| 44 | + DataType *end, |
| 45 | + long nthreads |
| 46 | +) { |
| 47 | + size_t array_size = end - begin; |
| 48 | + if (array_size < (size_t)nthreads) { |
| 49 | + nthreads = array_size; |
| 50 | + } |
| 51 | + size_t delta = array_size / nthreads; |
| 52 | + pthread_t *threads = malloc(nthreads * sizeof(pthread_t)); |
| 53 | + SumArrayThreadData *thread_datas = malloc(nthreads * sizeof(SumArrayThreadData)); |
| 54 | + for (long i = 0; i < nthreads; ++i) { |
| 55 | + thread_datas[i].begin = begin; |
| 56 | + thread_datas[i].end = begin + delta; |
| 57 | + assert(!pthread_create( |
| 58 | + &threads[i], |
| 59 | + NULL, |
| 60 | + sum_array_thread, |
| 61 | + (void*)&thread_datas[i] |
| 62 | + )); |
| 63 | + begin += delta; |
| 64 | + } |
| 65 | + DataType sum = 0; |
| 66 | + for (long i = 0; i < nthreads; ++i) { |
| 67 | + assert(!pthread_join(threads[i], NULL)); |
| 68 | + sum += thread_datas[i].output; |
| 69 | + } |
| 70 | + free(threads); |
| 71 | + free(thread_datas); |
| 72 | + return sum + sum_array(begin, end); |
| 73 | +} |
| 74 | + |
| 75 | +void print_result( |
| 76 | + unsigned int nthreads, |
| 77 | + struct timespec *tstart, |
| 78 | + struct timespec *tend |
| 79 | +) { |
| 80 | + printf( |
| 81 | + "%d %.4f\n", |
| 82 | + nthreads, |
| 83 | + ( |
| 84 | + ((double)tend->tv_sec + 1.0e-9 * tend->tv_nsec) - |
| 85 | + ((double)tstart->tv_sec + 1.0e-9 * tstart->tv_nsec) |
| 86 | + ) |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +int main(int argc, char **argv) { |
| 91 | + unsigned long long array_size; |
| 92 | + struct timespec tstart, tend; |
| 93 | + |
| 94 | + /* Handle CLI arguments. */ |
| 95 | + if (argc > 1) { |
| 96 | + array_size = strtoll(argv[1], NULL, 10); |
| 97 | + } else { |
| 98 | + array_size = 10; |
| 99 | + } |
| 100 | + |
| 101 | + /* Initialize array with random numbers. */ |
| 102 | + DataType *array = malloc(sizeof(DataType) * array_size); |
| 103 | + assert(array); |
| 104 | + srand(time(NULL)); |
| 105 | + for (size_t i = 0; i < array_size; ++i) { |
| 106 | + array[i] = rand(); |
| 107 | + } |
| 108 | + |
| 109 | + /* Output header. */ |
| 110 | + printf("nthreads elapsed_time_seconds\n"); |
| 111 | + |
| 112 | + /* Single threaded sanity check. */ |
| 113 | + clock_gettime(CLOCK_MONOTONIC, &tstart); |
| 114 | + DataType serial_result = sum_array(array, array + array_size); |
| 115 | + clock_gettime(CLOCK_MONOTONIC, &tend); |
| 116 | + print_result(0, &tstart, &tend); |
| 117 | + |
| 118 | + /* Use different number of threads. */ |
| 119 | + long max_nthreads = sysconf(_SC_NPROCESSORS_ONLN) * 2; |
| 120 | + for (long nthreads = 1; nthreads <= max_nthreads; ++nthreads) { |
| 121 | + clock_gettime(CLOCK_MONOTONIC, &tstart); |
| 122 | + DataType result = sum_array_parallel(array, array + array_size, nthreads); |
| 123 | + clock_gettime(CLOCK_MONOTONIC, &tend); |
| 124 | + print_result(nthreads, &tstart, &tend); |
| 125 | + /* Sanity check that our implementation is correct. */ |
| 126 | + assert(result == serial_result); |
| 127 | + } |
| 128 | + |
| 129 | + /* Cleanup. */ |
| 130 | + free(array); |
| 131 | + return EXIT_SUCCESS; |
| 132 | +} |
0 commit comments