Calculate Sum of an Array with Multi-threading

From Software Engineers Wiki
Jump to: navigation, search

Given set of N threads generate sum of all numbers in an array of known size M.

Answer #1

#include <iostream>
#include <thread>
#include <future>

void sum_array(unsigned int data_array[], unsigned int start, unsigned int endp1, unsigned long *result)
{
        unsigned long sum;

        for (sum = 0; start < endp1; ++start)
                sum += data_array[start];

        *result = sum;
}

int main(void)
{
        const int arr_size = 10000;
        const int thread_num = 10;
        const int arr_per_thread = arr_size / thread_num;

        static_assert(arr_size >= thread_num, "Array size must be equal to or greater than thread number");

        int i;
        unsigned int data_array[arr_size];

        for (i = 0; i < arr_size; ++i)
                data_array[i] = i + 1;

        std::thread *threads[thread_num];
        unsigned long result[thread_num], sum;
        unsigned int start, endp1;

        for (i = 0, start = 0; i < thread_num; ++i) {
                endp1 = (i == thread_num - 1) ? arr_size : start + arr_per_thread;
                threads[i] = new std::thread(sum_array, data_array, start, endp1, result + i);
                start = endp1;
        }

        for (i = 0, start = 0; i < thread_num; ++i) {
                threads[i]->join();
                delete threads[i];
        }

        for (i = 0, sum = 0; i < thread_num; ++i) {
                std::cout << "Thread " << i << " returned " << result[i] << std::endl;
                sum += result[i];
        }

        std::cout << "Total: " << sum << std::endl;

        return 0;
}

Answer #2 : Use promise/future

#include <iostream>
#include <thread>
#include <future>

void sum_array(unsigned int data_array[], unsigned int start, unsigned int endp1, std::promise<unsigned long> *prom)
{
        unsigned long sum;

        for (sum = 0; start < endp1; ++start)
                sum += data_array[start];

        prom->set_value(sum);
}

int main(void)
{
        const int arr_size = 10000;
        const int thread_num = 10;
        const int arr_per_thread = arr_size / thread_num;

        static_assert(arr_size >= thread_num, "Array size must be equal to or greater than thread number");

        int i;
        unsigned int data_array[arr_size];

        for (i = 0; i < arr_size; ++i)
                data_array[i] = i + 1;

        std::promise<unsigned long> prom[thread_num];
        std::future<unsigned long> fut[thread_num];
        std::thread *threads[thread_num];
        unsigned long result[thread_num], sum;
        unsigned int start, endp1;

        for (i = 0, start = 0; i < thread_num; ++i) {
                endp1 = (i == thread_num - 1) ? arr_size : start + arr_per_thread;
                threads[i] = new std::thread(sum_array, data_array, start, endp1, &prom[i]);
                start = endp1;
        }

        for (i = 0, start = 0; i < thread_num; ++i) {
                fut[i] = prom[i].get_future();
                threads[i]->join();
                result[i] = fut[i].get();
                delete threads[i];
        }

        for (i = 0, sum = 0; i < thread_num; ++i) {
                std::cout << "Thread " << i << " returned " << result[i] << std::endl;
                sum += result[i];
        }

        std::cout << "Total: " << sum << std::endl;

        return 0;
}

Answer #3 : std::async

#include <iostream>
#include <thread>
#include <future>

unsigned long sum_array(unsigned int data_array[], unsigned int start, unsigned int endp1)
{
        unsigned long sum;

        for (sum = 0; start < endp1; ++start)
                sum += data_array[start];

        return sum;
}

int main(void)
{
        const int arr_size = 10000;
        const int thread_num = 10;
        const int arr_per_thread = arr_size / thread_num;

        static_assert(arr_size >= thread_num, "Array size must be equal to or greater than thread number");

        int i;
        unsigned int data_array[arr_size];

        for (i = 0; i < arr_size; ++i)
                data_array[i] = i + 1;

        std::future<unsigned long> fut[thread_num];
        unsigned long result[thread_num], sum;
        unsigned int start, endp1;

        for (i = 0, start = 0; i < thread_num; ++i) {
                endp1 = (i == thread_num - 1) ? arr_size : start + arr_per_thread;
                fut[i] = std::async(sum_array, data_array, start, endp1);
                start = endp1;
        }

        for (i = 0, start = 0; i < thread_num; ++i)
                result[i] = fut[i].get();

        for (i = 0, sum = 0; i < thread_num; ++i) {
                std::cout << "Thread " << i << " returned " << result[i] << std::endl;
                sum += result[i];
        }

        std::cout << "Total: " << sum << std::endl;

        return 0;
}
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox