-1

I want to know how to properly implement a program in C++, in which I have a function func that I want to be executed in a single thread. I want to do this, because I want to test the Single Core Speed of my CPU. I will loop this function(func) for about 20 times, and record the execution time of each repetition, then I will sum the results and get the average execution time.

#include <thread>

int func(long long x)
{
    int div = 0;
    for(long i = 1; i <= x / 2; i++)
        if(x % i == 0)
            div++;
    return div + 1;
}

int main()
{
    std::thread one_thread (func,100000000);
    one_thread.join(); 
    return 0;
}

So , in this program, does the func is executed on a single particular core ?

Here is the source code of my program:

#include <iostream>
#include <thread>
#include <iomanip>
#include <windows.h>
#include "font.h"
#include "timer.h"

using namespace std;

#define steps 20

int func(long long x)
{
    int div = 0;
    for(long i = 1; i <= x / 2; i++)
        if(x % i == 0)
            div++;
    return div + 1;
}

int main()
{
    SetFontConsolas(); // Set font consolas
    ShowConsoleCursor(false); // Turn off the cursor
    timer t;
    short int number = 0;
    cout << number << "%";
    for(int i = 0 ; i < steps ; i++)
    {
        t.restart(); // start recording
        std::thread one_thread (func,100000000);
        one_thread.join(); // wait function return
        t.stop(); // stop recording
        t.record(); // save the time in vector
        number += 5;
        cout << "\r    ";
        cout << "\r" << number << "%";
    }
    double time = 0.0;
    for(int i = 0 ; i < steps ; i++)
        time += t.times[i]; // sum all recorded times
    time /= steps; // get the average execution time
    cout << "\nExecution time: " << fixed << setprecision(4) << time << '\n';
    double score = 0.0;
    score = (1.0 * 100) / time; // calculating benchmark score
    cout << "Score: ";
    SetColor(12);
    cout << setprecision(2) << score << " pts";
    SetColor(15);
    cout << "\nPress any key to continue.\n";
    cin.get();
    return 0;
}
  • 3
    If you want it to run on only one core, then simply don't use any threading. Single threaded is what you get unless you explicitly add threads. – Jesper Juhl Apr 15 '17 at 10:26
  • If you want to lock your program to a *specific* core and prevent the OS from bouncing it between cores, then you can (on Linux (and other *NIX systems)) use [sched_setaffinity()](https://linux.die.net/man/2/sched_setaffinity). – Jesper Juhl Apr 15 '17 at 12:13
  • I am on Windows. I try to use SetThreadAffinityMask. But I wonder how to get the HANDLE of a certain thread. – Ioan Rîpan Apr 15 '17 at 12:43

3 Answers3

2

No, your program has at least two treads: main, and the one you've created to run func. Moreover, neither of these threads is guaranteed to get executed on particular core. Depending on OS scheduler they may switch cores in unpredictable manner. Though main thread will mostly just wait. If you want to lock thread execution on particular core then you need to set thread core affinity by some platform-specific method such as SetThreadAffinityMask on Windows. But you don't really need to go that deep because there is no core switch sensitive code in your example. There is even no need to spawn separate thread dedicated to perform calculations.

user7860670
  • 35,849
  • 4
  • 58
  • 84
1

If your program doesn't have multiple threads in the source and if the compiler does not insert automatic parallelization, the program should run on a single core (at a time).

Now depending on your compiler you can use appropriate optimization levels to ensure that it doesn't parallelize.

On the other hand what might happen is that the compiler can completely eliminate the loop in the function if it can statically compute the result. That however doesn't seem to be the issue with your case.

Ajay Brahmakshatriya
  • 8,993
  • 3
  • 26
  • 49
  • I understand. I want to ask you, do you think this is a valid test of single core performance ? Do you have any other ideas of a better implementation ? (Sorry for my bad english) – Ioan Rîpan Apr 15 '17 at 10:39
  • There are multiple components to the speed of a processor. In your program you do the modulo and division operation in the loop. Some processors might be good for that, some might be not. There are many issues like pipelining, cache misses etc. To compare two processors for just simple operations this might be okay, but usually other factors matter too. I should ask you to look at the spec benchmarks. They are created with exactly the same intention. To get a comprehensive idea of the processor and other tools (compilers etc). – Ajay Brahmakshatriya Apr 15 '17 at 10:47
  • The spec suite has a bunch of examples which evaluate different aspects of the processor, memory, bus disk etc. – Ajay Brahmakshatriya Apr 15 '17 at 10:48
  • I will have a look at spec benchmarks. Thank you so much. – Ioan Rîpan Apr 15 '17 at 11:01
0

I don't think any C++ compiler makes use of multiple core, behind your back. There would be large language issues in doing that. If you neither spawn threads nor use a parallel library such as MPI, the program should execute on only one core.

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18