How to use OMP_NUM_THREADS OpenMP - c++

I'm studying Parallel Programming in Visual Studio using C++. So, I get the practice work in which I should to set up number of threads using environment variable - OMP_NUM_THREADS. But how its using I don't know. Here's my code:
#include <omp.h>
#include <iostream>
#include <thread>
using namespace std;
int main()
{
#pragma omp parallel
{
cout << "Hello World\n";
}
return 0;
}
So, how I can get use it? What should I do?

Try to use #pragma omp parallel num_threads(4). I tested and it did print four times. FYI, OpenMP set_num_threads() is not working.

Related

#include <date/date.h> invokes numerous errors

I am trying to show in an edit box a program execution time. I have found some examples at Stackoverflow like the code here below.
using namespace std;
using namespace date;
ostringstream out;
auto start = chrono::system_clock::now();
//some program execution
auto finish = chrono::system_clock::now();
out << finish - start;
string s = out.str();
cout << s << '\n';
I have installed the library #include <date/date.h> via vcpkg. But the problem is: as soon as I do #include <date/date.h>, and run code with Local Windows Debugger a numerous mistake is happening as indicated in the snap shot below.
I mean, simple including of <date/date.h> library leads to errors.
How can I avoid this issue?
Many thanks in advance!
UPD
#include "pch.h"
#include "framework.h"
#include "MFCApplication2.h"
#include "MFCApplication2Dlg.h"
#include "afxdialogex.h"
#include <iostream>
#include <date/date.h>
void CMFCApplication2Dlg::OnBnClickedButton1()
{
//I have cleared the code inside, but errors yet appear
}

Delay in Dev C++

I have come across to a problem while coding in C++ on Dev C++ compiler. I want to delay my statement to some milliseconds, but the problem is dev doesnt support the dos.h header file and so its contents as well. I had an alternative way for using it with the help of for loop but i aint got its proper syntax in my mind to use it properly. I wish, you folks, might be able to resolve the problem for me. Thanks in Advance..
#include <dos.h>
int main(){
delay(1000)
cout << "Hello"
return 0;
Tell me another alternative way for this please.
C++ has < thread >
< thread > has "std::this_thread::sleep_for(...)"
Example illustrating the (...)
std::this_thread::sleep_for(100ms);
The ms of 100 ms comes from
using namespace std::chrono_literals; // support suffixes like 100ms, 2s, 30 us
Probably you also need to include < chrono >
Use the header chrono and thread
#include <iostream>
#include <thread>
#include <chrono>
using namespace std::this_thread;
using namespace std::chrono;
sleep_for(nanoseconds(10));
sleep_until(system_clock::now() + seconds(1));
You can change the nanoseconds part to seconds or keep it at nanoseconds, and the number beside it is the amount of that unit that you can change to any number.
Use empty for loop
for example:
for(int i=0; i<10000; i++);
Change upper bound according to your need.

OpenMP pragma is ignored in Visual Studio 2017

I have problems getting Visual Studio 2017 to aknowledge
openMP pragmas like "#pragma omp parallel". I understand that Visual Studio should at least support OpenMP 2.0, but the behavior of my program indicates that my pragmas are alltogether ignored.
In an empty C++ project, I enable the OpenMP setting found under "MyProject>Properties>C/C++>Languages>OpenMP Support", and write the following main function:
#include <iostream>
#include <omp.h>
int main(int argc, char* argv[])
{
// I have tried both with and without the following line:
// omp_set_num_threads(15);
#pragma omp parallel
{
std::cout << "Hello world \n";
}
system("pause");
return 0;
}
I expect one "Hello world" to be printed from each active thread in the parallel region. Only one line is printed, indicating that I may have missed something.
Any suggestions?

Threads in c++ using eclipse juno

I'm trying to run the following code, but eclipse is giving the errors "type thread cannot be resolved", "thread was not declared in this scope", "expected ; before t1".
#include <iostream>
#include "Tetris.h"
#include <Windows.h> //Sleep
#include <conio.h> //getch()
#include <thread>
#define SLEEP_TIME 1000
using namespace std;
void call_from_thread() {
cout << "hello!" << endl;
}
int main(){
/* some code... */
thread t1(call_from_thread);
/* some more code... */
return 0;
}
So far i've tried the following:
I've also tried the -thread flag as I've seen someone suggesting in an answer here.
I've been looking on google and stackoverflow for hours without being able to solve the problem. "thread" still cannot be resolved and therefore I can't compile and generate a binary.
Any help would be very welcome
thanks in advance

Cross platform way to prevent high cpu usage in while loop (Without boost)?

I have a server that I want to run, and it uses a cross platform library that only gives me a tick() to call:
int main()
{
Inst inst;
while(true)
{
inst.tick();
}
}
I need to try to lower the cpu usage so that it doesnt constantly take up 1 core.
Is there a simple way to do this without boost?
Thanks
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main()
{
//5 seconds
auto duration = chrono::duration<float>(5);
this_thread::sleep_for(duration);
return 0;
}
However, even if this code is completely fine, I can't seem to compile it with the provided MinGW compiler from Code::Blocks.