Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save telishev/770d3f7072fd35286225c6d34dd78c6b to your computer and use it in GitHub Desktop.
Save telishev/770d3f7072fd35286225c6d34dd78c6b to your computer and use it in GitHub Desktop.
parallel copy
#include <vector>
#include <thread>
void do_parallel (size_t n)
{
std::vector<int> a (n), b (n);
double c = clock ();
std::vector<std::thread> threads;
int total_threads = std::thread::hardware_concurrency ();
for (int i = 0; i < total_threads; i++)
threads.emplace_back ([&, i]
{
size_t start = i * n / total_threads;
size_t end = (i + 1) * n / total_threads;
std::copy (a.begin () + start, a.begin () + end, b.begin () + start);
});
for (auto &thr : threads)
thr.join ();
printf ("parallel time = %lf\n", (clock () - c) / CLOCKS_PER_SEC);
}
void do_serial (size_t n)
{
std::vector<int> a (n), b (n);
double c = clock ();
std::copy (a.begin (), a.end (), b.begin ());
printf ("serial time = %lf\n", (clock () - c) / CLOCKS_PER_SEC);
}
int main (int argc, char **argv)
{
if (argc < 2) return 0;
int n;
sscanf (argv[1], "%d", &n);
do_serial (n);
do_parallel (n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment