execution time serial and parallel openmp program mac os - c++

I have a problem in understanding the execution time of my program (serial and parallel version).
Here you can find the part of the main function I am talking about:
stopwatch temp2;
temp2.start();
#pragma omp parallel
{
#pragma omp for
for (int i=0; i<100; i++){
int a=itemVectorTraining->at(mapScaledItem->at(5755)).timesIWatchedAfterC(itemVectorTraining->at(mapScaledItem->at(236611)), deviceVectorTraining, 180);
}
}
temp2.stop();
cout<<"Parallel: "<<temp2.elapsed_ms()<<endl;
stopwatch temp1;
temp1.start();
for (int i=0; i<100; i++){
int a=itemVectorTraining->at(mapScaledItem->at(5755)).timesIWatchedAfterC(itemVectorTraining->at(mapScaledItem->at(236611)), deviceVectorTraining, 180);
}
temp1.stop();
cout<<"Serial: "<<temp1.elapsed_ms()<<endl;
where "stopwatch" is an object well defined (I hope so, since my professor has created it :) ) in order to have a corrected measure of time in milliseconds.
The problem is that when I execute the main with this command line:
g++-4.9 -std=c++11 -o test -Iinclude main.cpp
I obtain this output
Parallel: 140821125
Serial: 89847
while adding "-fopenmp", i.e. with this command line:
g++-4.9 -fopenmp -std=c++11 -o testVale main.cpp
I get:
Parallel: 39413
Serial: 2089786185294
And it doesn't make any sense! Moreover while the program return me such big values for Parallel in the first case and for Serial in the second case, actually it doesn't take such a long time to run the code.
I am compiling from the terminal of a MAC OS X , and normally I should obtain something like:
Parallel:38548
Serial 68007
Does anyone have an idea of what's going on with the compilation of the program?
Thank you very much!
Code of stopwatch:
#ifndef CGLIFE_STOPWATCH_HPP
#define CGLIFE_STOPWATCH_HPP
#include <chrono>
class stopwatch {
private:
typedef std::chrono::high_resolution_clock clock;
bool running;
clock::time_point start_time;
clock::duration elapsed;
public:
stopwatch() {
running = false;
}
// Starts the stopwatch.
void start() {
if (!running) {
running = true;
start_time = clock::now();
}
}
// Stops the stopwatch.
void stop() {
if (running) {
running = false;
elapsed += clock::now() - start_time;
}
}
// Resets the elapsed time to 0.
void reset() {
elapsed = clock::duration();
}
// Returns the total elapsed time in milliseconds.
// If the stopwatch is running, the elapsed time
// includes the time elapsed in the current interval.
long long elapsed_ms() const {
clock::duration total;
if (!running) {
total = elapsed;
} else {
total = elapsed + (clock::now() - start_time);
}
return std::chrono::duration_cast<std::chrono::milliseconds>(total).count();
}
};
#endif

The stopwatch::elapsed seems uninitialized. I am unsure how it can be, since it must be of a class type.
Either initialize it in stopwatch constructor:
stopwatch() {
running = false;
elapsed = clock::duration();
}
or call reset always before starting one:
stopwatch temp2;
temp2.reset();
temp2.start();

Related

Chrono time counter is inaccurate when machine goes to sleep in the middle of execution?

I have the code sample bellow to measure the execution time of some piece of code:
int main()
{
auto before = chrono::steady_clock::now();
Sleep(30000);
auto after = chrono::steady_clock::now();
int duration = (std::chrono::duration_cast<std::chrono::seconds> ((after - before)).count());
cout << duration << endl;
return 0;
}
Normally it works fine and prints out 30 in the cout statement.
However, during testing I observed that if the computer were to go to sleep in between the auto before = ... statement and the auto after = ... statement (due to inactivity or whatever other reason), then the printed out time also counts the entire time the machine was asleep. This makes perfect sense since we are comparing a timepoint from before the machine going to sleep and one with after.
So my question is how can I make it so that the duration the machine was asleep is not counted in my final duration? Probably will need a ticker that doesn't increment while machine is asleep rather than timepoint measurements but I'm not aware of such a ticker.
This is a Windows specific question. As I understand, MacOS has mach_absolute_time which is exactly what I'm looking for in windows. I'm using MSVC 19.29.30147.0 as my compiler.
After looking around and testing it out, the solution is to use QueryUnbiasedInterruptTime
Running the following code snippet, I manually put my machine to sleep while the program was stuck on the sleep statement and I observed that the second print out consistently outputs 15 seconds regardless of how long I leave my machine in a sleeping state. However, the first print-out that uses GetTickCount64 will include the amount of time the machine was asleep.
int main()
{
ULONGLONG before_query, after_query= 0;
QueryUnbiasedInterruptTime(&before_query);
auto beforeticks = GetTickCount64();
Sleep(15000);
QueryUnbiasedInterruptTime(&after_query);
auto afterticks = GetTickCount64();
cout << "Ticks from gettickcount64 is " << (double (afterticks-beforeticks))/1000 << endl;
cout << "Unbiased time measure is " << double((after_query - before_query)/10000000) << endl;
return 0;
}
You are correct that the easiest way is to use a counter that is incremented each second. This is easily implemented with threads:
#include <thread>
#include <atomic>
#include <chrono>
using namespace std::literals::chrono_literals;
class ellapsed_counter {
std::atomic<bool> finished = false;
std::atomic<unsigned int> value = 0;
std::thread worker { [this] {
while(!finished) {
value++;
std::this_thread::sleep_for(1s);
}
} };
public:
void finish() noexcept {
finished = true;
if(worker.joinable()) worker.join();
}
unsigned int ellapsed() const noexcept { return value; }
};
This will keep incrementing on 1s intervals (probably with some error) as long as the process is running and should cease so when it is sleeping.
You can use it like this:
#include <iostream>
int main(int argc, const char *argv[]) {
ellapsed_counter counter;
unsigned int last = 0, count = 0;
while(count < 10) {
count = counter.ellapsed();
if(count != last) {
last = count;
std::cout << count << std::endl;
}
}
counter.finish();
return 0;
}
This will count from 1 to 10 seconds and exit.

How can I change precision in timer in std::chrono? It starts at value 1000

I made a timer class using std::chrono. I want to measure time of some process in code. The problem is that this process is really fast (less than 1000 nanoseconds) but timer starts counting values at 1000. If it is less than 1000 it changes to 0. Then my output is "0 Nanoseconds" and i want to get e.g "50 Nano seconds". Is there any solution for my problem?
class Timer {
std::chrono::time_point<std::chrono::high_resolution_clock> start_time;
std::chrono::time_point<std::chrono::high_resolution_clock> stop_time;
public:
void start()
{
start_time = std::chrono::high_resolution_clock::now();
}
void stop()
{
stop_time = std::chrono::high_resolution_clock::now();
}
void print_time_duration()
{
std::cout<<"This Function took: "<<std::chrono::duration_cast<std::chrono::nanoseconds>(stop_time - start_time).count()<<" Nanoseconds."<<std::endl;
}
int return_time_duration()
{
return std::chrono::duration_cast<std::chrono::nanoseconds>(stop_time - start_time).count();
}
};
And then...
Timer time;
time.start();
// DO SOMETHING HERE
time.stop();
time.print_time_duration();

How do I create a timer for the program with clocking?

I am trying to make a basic keylogger, and I want to be able to close the program after 20 minutes. looking online I found this clock_T functions, but I can't figure out how to transform this into seconds (and from seconds I will be able to make mins).
I tried to use the good old timer based on the "sleep" function.
but it gave me a huge amount of problems considering that I must be able to type anytime and save it in a log.
I am not able to understand high level of coding, therefore I struggled quite a bit looking at videos or descriptions on the internet.
I was expecting to see displayed the seconds increasing over time, so I would later on be able to shut down the code once the desired amount of second would be reached, but I instead ran into a spam of the same number for the specific second.
example: 1111111111111111111111111111111111 (after one second it increases)22222222222222222222222222222222 (increases again) 333333333333333333333333. and so on.
#include <iostream>
#include <string>
#include <Windows.h>
#include <fstream>
#include <time.h>
using namespace std;
int main() {
// FreeConsole();
clock_t start = 0;
clock_t end = 0;
clock_t delta = 0;
start = clock();
fstream info;
string filename = "Data.txt";
while (true) {
info.open(filename.c_str(), ios::app);
for (char i = 31; i < 122; i++) {
if (GetAsyncKeyState(i) == -32767) {
info << i;
cout << i;
}
}
info.close();
end = clock();
delta = end - start;
delta = delta; // 1000;
std::cout << delta/CLOCKS_PER_SEC << endl;
}
return 0;
}
I have this class template that uses the chrono library. Here is a simple example of its use.
main.cpp
#include <iostream>
#include "Timer.h"
int main() {
while( Timer<minutes>(1).isRunning() ) {
// do something for 1 minute
}
while ( Timer<seconds>(30).isRunning() ) {
// do something for 30 seconds
}
return 0;
}
Timer.h
#pragma once
#include <chrono>
using namespace std::chrono;
template<class Resolution = seconds>
class Timer {
public:
using Clock = std::conditional_t<high_resolution_clock::is_steady,
high_resolution_clock, steady_clock>;
private:
Clock::time_point startTime_;
Clock::time_point timeToRunFor_;
bool isRunning_ = false;
public:
explicit Timer(int count) :
startTime_{ Clock::now() },
timeToRunFor_{ Clock::now() + Resolution(count) },
isRunning_{ true }
{
run();
}
~Timer() {
const auto stopTime = Clock::now();
std::ostringstream stream;
stream << "Time Elapsed: "
<< duration_cast<Resolution>(stopTime - startTime_).count()
<< std::endl;
std::cout << stream.str() << std::endl;
}
bool isRunning() {
return isRunning_;
}
private:
void run() {
while (steady_clock::now() < timeToRunFor_) {}
isRunning_ = false;
}
};
Output
Time Elapsed: 1
Time Elapsed: 30
Time waited for first about 1 minute then printed 1, then waited for about 30 seconds then printed 30. This is a nice light weight class and is simple to use.
I'm currently in the process of adding more to this class to give support for manual starting and stopping usages with a default constructor. As this class currently stands above you could create an instance or an object of this class as a variable, and give it a time explicitly and it will run for that long, but you can not manually start and stop this timer when you want to. Once I finish this class, the default constructor will not use the internal members timeToRunFor_ and run() as they are meant to be used with the explicit constructor version.
Once completed you can set how long you want something to run for via the while loop then terminate after desired time has expired via the Explicit constructor version, or you can create a local instance of this class as an object, invoke the start function, do some other operations for and unknown amount of time, then call the stop function after and perform the query of the time elapsed. I need a little more time to finish this class so I will post this as is for now and once I complete my updates to the class I'll update it here to the newer version!

Why is my parallel task management so slow?

For reasons explained below I have started to investigate the time it takes to create and run a thread. The way I do it, I found this process to take about 26 ms for 10 threads which is much longer than it should be - at least from my understanding.
A short background:
I'm working on a game that uses pathfinding. After adding more entities it became necessary to parallise the process.
I want this to be as readable as possible so I've created a ParallelTask class that holds a thread, std::function (that should be executed by the tread), a mutex to protect some write operations and a bool is completed that is set to true once the thread has finished executing.
I'm new to multithreading so I have no idea if this is a good approach to begin with but never the less I'm confused why it takes so long to execute.
I have written the code below to isolate the problem.
int main()
{
std::map<int, std::unique_ptr<ParallelTask>> parallelTaskDictionary;
auto start = std::chrono::system_clock::now();
for (size_t i = 0; i < 10; i++)
{
parallelTaskDictionary.emplace(i, std::make_unique<ParallelTask>());
parallelTaskDictionary[i]->Execute();
}
auto end = std::chrono::system_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << elapsed.count() << std::endl;
parallelTaskDictionary.clear();
return 0;
}
class ParallelTask
{
public:
ParallelTask();
// Join the treads
~ParallelTask();
public:
inline std::vector<int> GetPath() const { return path; }
void Execute();
private:
std::thread thread;
mutable std::mutex mutex;
std::function<void()> threadFunction;
bool completed;
std::vector<int> path;
};
ParallelTask::ParallelTask()
{
threadFunction = [this]() {
{
std::lock_guard<std::mutex> lock(mutex);
this->completed = true;
}
};
}
ParallelTask::~ParallelTask()
{
if (thread.joinable())
thread.join();
}
void ParallelTask::Execute()
{
this->completed = false;
// Launch the thread
this->thread = std::thread(threadFunction);
}
Running this code gives me between 25 and 26 milliseconds of execution time. Since this is meant to be used in a game its of course inacceptable.
As previously mentioned, I do not understand why, especially since the threadFunction itself does literally noting. In case you wonder, I have even removed the mutex lock and it gave me literally the same result so there must be something else going on here. (From my research creating a thread shouldn't take more than a couple microseconds but maybe I'm just wrong with that ^^)
PS: Oh yeah and while we are at it, I still don't really understand who should own the mutex. (Is there one global or one per object...)???
If you want to measure the time of execution only, I think you should put the now and end statements inside the threadFunction only where the work is done, as shown in the code below.
#include <map>
#include <iostream>
#include <memory>
#include <chrono>
#include <vector>
#include <thread>
#include <mutex>
#include <functional>
class ParallelTask
{
public:
ParallelTask();
// Join the treads
~ParallelTask();
public:
inline std::vector<int> GetPath() const { return path; }
void Execute();
private:
std::thread thread;
mutable std::mutex mutex;
std::function<void()> threadFunction;
bool completed;
std::vector<int> path;
};
ParallelTask::ParallelTask()
{
threadFunction = [this]() {
{
auto start = std::chrono::system_clock::now();
std::lock_guard<std::mutex> lock(mutex);
this->completed = true;
auto end = std::chrono::system_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "elapsed time" << elapsed.count() << std::endl;
}
};
}
ParallelTask::~ParallelTask()
{
if (thread.joinable())
thread.join();
}
void ParallelTask::Execute()
{
this->completed = false;
// Launch the thread
this->thread = std::thread(threadFunction);
}
int main()
{
std::map<int, std::unique_ptr<ParallelTask>> parallelTaskDictionary;
for (size_t i = 0; i < 10; i++)
{
parallelTaskDictionary.emplace(i, std::make_unique<ParallelTask>());
parallelTaskDictionary[i]->Execute();
}
parallelTaskDictionary.clear();
return 0;
}
which gives an output:
elapsed time1
elapsed time0
elapsed time0
elapsed time0
elapsed time0
elapsed time0elapsed time
0
elapsed time0
elapsed time0
elapsed time0
Because we exclude the time it takes to spin up the thread.
And just as a sanity check, if you really want to see the effect of real work, you could add,
using namespace std::chrono_literals;
std::this_thread::sleep_for(2s);
to your threadFunction, to make it look like this
ParallelTask::ParallelTask()
{
threadFunction = [this]() {
{
auto start = std::chrono::system_clock::now();
std::lock_guard<std::mutex> lock(mutex);
this->completed = true;
using namespace std::chrono_literals;
std::this_thread::sleep_for(2s);
auto end = std::chrono::system_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "elapsed time" << elapsed.count() << std::endl;
}
};
}
and the output will be,
elapsed time2000061
elapsed timeelapsed time2000103
elapsed timeelapsed time20000222000061
elapsed time2000050
2000072
elapsed time2000061
elapsed time200012

Getting a time difference in milliseconds

I´m trying to do something that I thought would be very simple but I have looked everywhere and I can´t figure it out. I´m also new to C++ and have no good understanding of templates and such.
I just need a function that measures the time from the program´s launch to a certain point in milliseconds, something like:
class timeCounter {
private:
long startTime;
long currentTime;
long timeDifference;
public:
long getTime();
}
timeCounter::timeCounter () {
startTime = time.now();
}
long timeCounter::getTimePassed () {
currentTime = time.now();
timeDifference = timeNow - timeStart;
return timeDifference;
}
I´ve tried with clock() / CLOCKS_PER_SECONDS but the result is slower than a second.
Can anyone help me out?
Thank you very much!
I was recently writing a similar system to get the delta time for a game engine.
Using the std::chrono library, here's an example:
#include <iostream>
#include <chrono>
#include <thread>
class timer
{
// alias our types for simplicity
using clock = std::chrono::system_clock;
using time_point_type = std::chrono::time_point < clock, std::chrono::milliseconds > ;
public:
// default constructor that stores the start time
timer()
{
start = std::chrono::time_point_cast<std::chrono::milliseconds>(clock::now());
}
// gets the time elapsed from construction.
long /*milliseconds*/ getTimePassed()
{
// get the new time
auto end = clock::now();
// return the difference of the times
return (end - start).count();
}
private:
time_point_type start;
};
int main()
{
timer t;
std::this_thread::sleep_for(std::chrono::seconds(5));
std::cout << t.getTimePassed();
std::cin.get();
}