I have done sequential and parallel addition of 2 vectors that I have filled with some dummy values. I am also measuring the time of execution for both, sequential and parallel execution.
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <functional>
#include <chrono>
#include <cassert>
#include <thread>
#define NUM_OF_THREADS 8
#define VECTOR_SIZE 512
using time_point = std::chrono::high_resolution_clock::time_point;
template<typename T>
std::vector<T> operator+(std::vector<T> const& first, std::vector<T> const& second)
{
assert(first.size() == second.size());
std::vector<T> result;
result.reserve(first.size());
std::transform(first.begin(), first.end(), second.begin(), std::back_inserter(result), std::plus<T>());
return result;
}
template<typename T>
void parallel_addition(std::vector<T>& result,
std::vector<T> const& first,
std::vector<T> const& second,
size_t lower_bound,
size_t upper_bound)
{
for (size_t i = lower_bound; i < upper_bound; i++)
{
result[i] = first[i] + second[i];
}
}
int main(int argc, char *argv[])
{
std::vector<int> first_vec, second_vec;
/* Filling up the vectors with dummy values */
for (int i = 1; i <= VECTOR_SIZE; i++)
{
first_vec.push_back(i);
second_vec.push_back(i);
}
/* Sequential addition of vectors */
time_point t1_seq = std::chrono::high_resolution_clock::now();
std::vector<int> sequential_result = first_vec + second_vec;
time_point t2_seq = std::chrono::high_resolution_clock::now();
auto duration_seq = std::chrono::duration_cast<std::chrono::microseconds>(t2_seq - t1_seq).count();
std::cout << "Sequential addition of vectors - execution time: " << duration_seq << "us" << std::endl;
/* Parallel addition of vectors */
std::vector<std::thread> threads;
std::vector<int> parallel_result(VECTOR_SIZE);
time_point t1_par = std::chrono::high_resolution_clock::now();
for (int i = 0; i < NUM_OF_THREADS; i++)
{
threads.push_back(std::thread(parallel_addition<int>,
std::ref(parallel_result),
first_vec,
second_vec,
i * (VECTOR_SIZE / NUM_OF_THREADS),
(i + 1) * (VECTOR_SIZE / NUM_OF_THREADS)));
}
for (auto &thread : threads)
{
thread.join();
}
time_point t2_par = std::chrono::high_resolution_clock::now();
auto duration_par = std::chrono::duration_cast<std::chrono::microseconds>(t2_par - t1_par).count();
std::cout << "Parallel (" << NUM_OF_THREADS << " threads) addition of vectors - execution time: " << duration_par << "us" << std::endl;
return 0;
}
Despite style review, can you also tell me if there is another, better, approach to addition of two vectors in multiple threads? My intention was to write in C++17.