AnotherAli

C++ async

Jan 21st, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <future>
  3. #include <chrono>
  4. #include <cmath>
  5. #include <iomanip>
  6. using namespace std;
  7. using namespace chrono;
  8.  
  9. //1
  10. int rest_from_10deg(int deg) { //returns mod( 10^{deq}, 17 )
  11. int rests[16] = { 1, 10, 15, 14, 4, 6, 9, 5, 16, 7, 2, 3, 13, 11, 8, 12 };
  12. return rests[deg % 16];
  13. }
  14. int long_rest(vector<int>& lnum) {
  15. int resrest = 0, n = lnum.size();
  16. for (int i = n - 1; i >= 0; i--)
  17. resrest = (resrest + lnum[i] * rest_from_10deg(n - i - 1)) % 17;
  18. return resrest;
  19. }
  20. int long_rest_par(vector<int>& lnum, int ibeg, int iend) { //ibeg <= iend are indices-borders of the exact interval in the whole number lnum
  21. int resrest = 0, n = lnum.size();
  22. for (int i = iend; i >= ibeg; i--)
  23. resrest += lnum[i] * rest_from_10deg(n - i - 1);
  24. return resrest % 17;
  25. }
  26. int multi_thread_long_rest(vector<int>& lnum, int nthr) {
  27. vector<future<int>> asyncs;
  28. int resrest = 0, batch = lnum.size() / nthr + !(lnum.size() % nthr);
  29. for (int i = 0; i < nthr - 1; i++)
  30. asyncs.push_back(async(long_rest_par, lnum, i * batch, (i + 1) * batch - 1));
  31. asyncs.push_back(async(long_rest_par, lnum, (nthr - 1) * batch, lnum.size() - 1));
  32. for (int i = 0; i < nthr; i++)
  33. resrest += asyncs[i].get();
  34. return resrest % 17;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment