0

I have simplified my code, and it compiles, but it doesn't do anything. It doesn't error out though either. I am trying to get 7 threads (on my 8-core processor) in this example to write to a variable to benchmark my system. I would like to do this with multiple threads to see if it's faster. It's based off other code that worked before I added multithreading. When I run, it just terminates. It should show progress each second of how many total iterations all the threads have done together. Some of the includes are there from other code I am working on.

I would like to also gracefully terminate all 7 threads when Ctrl-C is pressed. Help would be appreciated. Thanks!

//Compiled using: g++ ./test.cpp -lpthread -o ./test

#include <stdio.h>
#include <string>
#include <iostream>
#include <time.h>
#include <ctime>
#include <ratio>
#include <chrono>
#include <iomanip>
#include <locale.h>
#include <cstdlib>
#include <pthread.h>

using namespace std;
using namespace std::chrono;

const int NUM_THREADS = 7;
const std::string VALUE_TO_WRITE = "TEST";
unsigned long long int total_iterations = 0;

void * RunBenchmark(void * threadid);

class comma_numpunct: public std::numpunct < char > {
    protected: virtual char do_thousands_sep() const {
        return ',';
    }

    virtual std::string do_grouping() const {
        return "\03";
    }
};

void * RunBenchmark(void * threadid) {
    unsigned long long int iterations = 0;
    std::string benchmark;

    int seconds = 0;

    std::locale comma_locale(std::locale(), new comma_numpunct());
    std::cout.imbue(comma_locale);

    auto start = std::chrono::system_clock::now();
    auto end = std::chrono::system_clock::now();

    do {
        start = std::chrono::system_clock::now();
        while ((std::chrono::duration_cast < std::chrono::seconds > (end - start).count() != 1)) {
            benchmark = VALUE_TO_WRITE;
            iterations += 1;
        }
        total_iterations += iterations;
        iterations = 0;
        cout << "Total Iterations: " << std::setprecision(0) << std::fixed << total_iterations << "\r";
    } while (1);
}

int main(int argc, char ** argv) {
    unsigned long long int iterations = 0;
    int tc, tn;

    pthread_t threads[NUM_THREADS];

    for (tn = 0; tn < NUM_THREADS; tn++) {
        tc = pthread_create( & threads[tn], NULL, & RunBenchmark, NULL);
    }
    return 0;
}
  • I *think* you're probably on Linux, so `perf stat -I 1000 ./a.out` could be useful for monitoring aggregate throughput at 1 second (1000 ms) intervals. (In instructions, not loop iterations, but you can find out how many instructions your iterations take). Note that benchmarking without optimization enabled is normally pointless. [Idiomatic way of performance evaluation?](https://stackoverflow.com/q/60291987) – Peter Cordes Oct 24 '20 at 03:12
  • Thank you for the advice on optimization enabling. I am now using the -O3 flag for the most optimization. I hope that's not overkill. – Anthro Teacher Oct 24 '20 at 04:00
  • 1
    `-O3` is a good starting point. I'd usually use `-O3 -march=native` if I wanted to let it auto-vectorize with AVX2 on my Skylake CPU, and use BMI2 for variable-count shifts and so on. Also to set tuning options. Even better would be profile-guided optimization, where you compile with `-O3 -fprofile-generate`, do a test-run with representative input, then rebuild with `-O3 -fprofile-use`, so it knows which loops are hot and can unroll them. And if there was any floating-point code, I'd be using `-fno-math-errno` and other options. – Peter Cordes Oct 24 '20 at 04:10
  • 1. You should [join created threads](https://linux.die.net/man/3/pthread_join) before `main` returns. 2. If your are using c++11 or later, [std::thread](https://en.cppreference.com/w/cpp/thread/thread) can be prefered. 3. Your `while` loop(that inside the `do-while` loop) is a infinite loop since `start` and `end` never change. – VainMan Oct 24 '20 at 07:26
  • 4. Additionally, I guess your simple assignment(`benchmark = VALUE_TO_WRITE;`) may be optimized out by the compiler, eg. after the first iteration. – VainMan Oct 24 '20 at 07:37

0 Answers0