- Exercise
In this exercise, you will design asynchronous functions and compare the performance of single and multithreaded programs.
We'll cover the following...
Task 1
The calculation of the scalar product in the program below is quite easy to parallelize. Take a look at the explanation below to better understand the code.
Launch four asynchronous functions to calculate the scalar product.
Press + to interact
// dotProduct.cpp#include <chrono>#include <iostream>#include <numeric>#include <random>#include <vector>static const int NUM= 100000000;long long getDotProduct(std::vector<int>& v, std::vector<int>& w){return std::inner_product(v.begin(), v.end(),w.begin(),0LL);}int main(){std::cout << std::endl;// get NUM random numbers from 0 .. 100std::random_device seed;// generatorstd::mt19937 engine(seed());// distributionstd::uniform_int_distribution<int> dist(0, 100);// fill the vectorsstd::vector<int> v, w;v.reserve(NUM);w.reserve(NUM);for (int i=0; i< NUM; ++i){v.push_back(dist(engine));w.push_back(dist(engine));}// measure the execution timestd::chrono::system_clock::time_point start = std::chrono::system_clock::now();std::cout << "getDotProduct(v, w): " << getDotProduct(v, w) << std::endl;std::chrono::duration<double> dur = std::chrono::system_clock::now() - start;std::cout << "Sequential Execution: "<< dur.count() << std::endl;std::cout << std::endl;}
...