| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | const char * descr = |
| 30 | "This program prints the time (ms) needed to calculate a mathematical challenge. It's purpose \n\ |
| 31 | is to be able to use the result to compare the CPU power available on the machine it's being executed \n\ |
| 32 | and under the given circumstances, which can differ for the same machine. For example: \n\ |
| 33 | The printed value will be different when using 2 of 2 cores, when there an another process \n\ |
| 34 | running on one of the cores core, and when no other intense process running. \n\ |
| 35 | \n\ |
| 36 | The program expects a one argument: a numerical value of the threads to start the calculations on. \n\ |
| 37 | \n\ |
| 38 | Prints: \n\ |
| 39 | Time to calculate a mathematical challenge in MILLISECONDS. \n\ |
| 40 | \n\ |
| 41 | Returns: \n\ |
| 42 | 0 on success, \n\ |
| 43 | 1 on a missing argument, \n\ |
| 44 | 2 on an incorrect format of the argument.\n"; |
| 45 | |
| 46 | #include <iostream> |
| 47 | #include <future> |
| 48 | #include <vector> |
| 49 | #include <sstream> |
| 50 | #include <chrono> |
| 51 | |
| 52 | using namespace std; |
| 53 | |
| 54 | /** |
| 55 | Uses Leibniz Formula for Pi. |
| 56 | https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 |
| 57 | */ |
| 58 | static double calcPi(const size_t max_iter) |
| 59 | { |
| 60 | const double n = 4; |
| 61 | double pi = 0; |
| 62 | double d = 1; |
| 63 | for (size_t i = 1; i < max_iter; ++i) |
| 64 | { |
| 65 | const double a = 2.0 * (i % 2) - 1.0; |
| 66 | pi += a * n / d; |
| 67 | d += 2; |
| 68 | } |
| 69 | return pi; |
| 70 | } |
| 71 | |
| 72 | int main(int argc, const char ** argv) |
| 73 | { |
| 74 | const size_t max_iter = 1e9; |
| 75 | if (argc < 2) |
| 76 | { |
| 77 | cout << "Please pass the number of threads to run.\n"; |
| 78 | |
| 79 | cout << '\n' << descr << '\n'; |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | // Convert argument to an integer. |
| 84 | int numThreads = 1; |
| 85 | const char * numThreadsStr = argv[1]; |
| 86 | std::istringstream iss(numThreadsStr); |
| 87 | if (! (iss >> numThreads) ) |
| 88 | { |
| 89 | cout << "Incorrect format of number of threads = '" << numThreadsStr << "'\n"; |
| 90 | return 2; |
| 91 | } |
| 92 | // Run the calculation in parallel. |
| 93 | std::vector<std::future<double>> futures; |
| 94 | for(int i = 0; i < numThreads; ++i) |
| 95 | { |
| 96 | futures.push_back(std::async(calcPi, max_iter)); |
| 97 | } |
| 98 | |
| 99 | // Start measuring the time. |
| 100 | const std::chrono::steady_clock::time_point tbegin = std::chrono::steady_clock::now(); |
| 101 | for(auto & e : futures) |
| 102 | { |
| 103 | e.get(); |
| 104 | } |
| 105 | // Stop measuring the time. |
| 106 | const std::chrono::steady_clock::time_point tend = std::chrono::steady_clock::now(); |
| 107 | |
| 108 | // Print the measured duration. |
| 109 | cout << std::chrono::duration_cast<std::chrono::milliseconds>(tend - tbegin).count() << std::endl; |
| 110 | return 0; |
| 111 | } |
| 112 |