OpenMP pragma is ignored in Visual Studio 2017 - c++

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?

Related

How to use OMP_NUM_THREADS OpenMP

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.

Deferred expression in Visual Studio macros

I tried examples from this page in Visual Studio 2017 (Windows Console Application). And this code doesn't work as expected:
#include <iostream>
using namespace std;
#define EMPTY()
#define DEFER(id) id EMPTY()
#define A() 123
int main() {
cout << DEFER(A)() << endl;
return 0;
}
The code outputs 123 on the console, so DEFER(A)() expands to 123. For instance, in CLion (cygwin 2.9.0) the code works according example from the page, DEFER(A)() expands to A() and causes an error.
So, do Visual Studio macros have some significant differences from others and how to make deferred expressions in Visual Studio in this case? Thanks!

build does not stop in Visual Studio 2010

I try to build a simple piece of code in Visual Studio, but the building process gets entangled in an infinite loop.
The code is simple:
// test.cpp : Defines the entry point for the console application.
//
#include <iostream.h>
using namespace std;
int main(void)
{
cout << "Hi." << endl;
return 0;
}
Any idea what is going on?
Thanks.
spot several things:
not sure what's in 'stdafx.h'
#include < iostream >
std::cout, std::endl (unless using namespace std; somewhere)
Build works fine for me, though.

small program much slower in visual studio 2012 vs. visual studio 2005

We are using Visual Studio 2005. We are looking at maybe upgrading to Visual Studio 2012 once it is released. I tried this small program in Visual Studio 2012 RC and was surprised to see it ran more than 2X slower than it does in Visual Studio 2005. In VS2012 I used default release build settings. For me it takes about 20ms in VS2005 and about 50ms in VS2012. Why is it that much slower?
#include <windows.h>
#include <deque>
using namespace std;
deque<int> d;
int main(int argc, char* argv[])
{
const int COUNT = 5000000;
timeBeginPeriod(1);
for (int i = 0; i < COUNT; ++i)
{
d.push_back(i);
}
double sum = 0;
DWORD start = timeGetTime();
for (int i = 0; i < COUNT; ++i)
{
sum += d[i];
}
printf("time=%dms\n", timeGetTime() - start);
printf("sum=%f\n", sum);
return 0;
}
So we reposted this question to the Microsoft forum.
http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/72234b06-7622-445e-b359-88f572b4de52
The short answer is that the implementation of std::deque::operator[] in VS2012RC is just slower compared to VS2005. Other common stl containers tested as equal or faster. It will be interesting to retest when VS2012 is production to see if the operator[] performance is resolved.
ps
Hi Raphael
Karl
My suspicion is that you're running into thread-safety code and that 2012 configures your libraries for multi-threaded code by default, meaning there are a bunch of lock and unlock operations built into your deque accesses.
Try comparing the compiler and linker options of the two builds to see how they differ.
(I'd try this myself but I don't have a Windows system with the relevant software on it handy. Sorry.)
Try timing both of those loops separately. I bet the issue is that the stl container implementation is slower in the new compiler.
Err wait- I meant try timing something that doesn't use STL.

What kind of VS 2010 C++ Project I must choose to compile a simple Borland C++ 5 Console app?

I have a simple Win32 console (no vcl) app written in Borland C++ 5, now I want compile the same application in VS 2010. but I'm new using this IDE and I don't know how run the code in VS. I tried choosing Win32 Console Application. but even i very simple app like this
#include <iostream.h>
#pragma hdrstop
#pragma argsused
int main(int argc, char* argv[])
{
cout << "Hello" << endl;
getchar();
return 0;
}
does not compile in VS.
So, What kind of VS 2010 C++ Project I must choose to compile a simple Borland C++ 5 Console app? or I need modify my app in order to use VS C++?
<iostream.h> is deprecated, and VS10 does not support it, use <iostream> instead, and you'll also need std::cout, std::endl , etc.. i.e.
#include <iostream>
#pragma hdrstop
// #pragma argsused // I don't believe this is valid in VS10
int main(int argc, char* argv[])
{
std::cout << "Hello" << std::endl;
std::cin.get();
return 0;
}
Alternatively, if you don't want to prefix your library uses with std::, you can put a using declaration at the top, after the headers:
using namespace std;