c++ visual studio compiler OpenMP - c++

I'm trying to compile a source code on Windows that works great on Linux.
Now, I'm facing the following issue:
The code relies on OpenMP and in there, the scheduling approach should be stored in a string and the chunk size in an integer.
The following code does extract the necessary information.
omp_sched_t omp_sched;
int chunkSize;
omp_get_schedule( &omp_sched, &chunkSize );
string jobScheduling;
switch ( omp_sched ) {
case omp_sched_static:
jobScheduling = "static";
break;
case omp_sched_dynamic:
jobScheduling = "dynamic";
break;
case omp_sched_guided:
jobScheduling = "guided";
break;
case omp_sched_auto:
jobScheduling = "auto";
break;
default:
jobScheduling = "auto";
break;
}
On Windows, by using the Microsoft Visual Studio compilers, it does not recognize omp_sched_t, omp_sched_static, omp_sched_dynamic...
Is this Linux specific? If not, how can I fix this (preferably without changing the source code)?

You are using features introduced in OpenMP 3.0 but Microsoft VisualC++ only supports OpenMP 2.0. And, AFAIK, even that support is deprecated though it's still there.
If you want support for modern OpenMP, you simply need a different compiler. Visual Studio comes with optional Clang, which has the required support, but enabling OpenMP via the Project Properties dialog can be a bit tricky. Another option is Intel C/C++ Compiler for Windows, which is a commercial piece of software.

Related

My Visual studio 2013 compiler for C++ is not working correctly [duplicate]

The "not", "and", etc... are keywords in C++ (macros in C). Is there any way to "enable" them in Visual Studio 2013? I'm able to use the words as macroses with iso646.h included. But VS seems not be able to recognize them as keywords.
Using /Za seems to enable them without including iso646.h, see it live, the following program produces an error without using /Za but works fine otherwise:
int main()
{
int x = 1, y = 0 ;
if (x and y)
{
//...
}
return 0;
}
As ta.speot.is indicates /Za disables extensions, the following documentation indicates you must include ios646.h otherwise:
Under /Ze, you have to include iso646.h if you want to use text forms of the following operators:
and it lists the alternative tokens below.
Note, I knew I saw this before, I include a link to a bug report for this in my answer to a similar question. Although this does not include the workaround noted above.
Note 2: Cheers and hth. - Alf indicates that there may be many undesirable consequences to turning off extension and therefore you may be better off just including iso646.h.
Use a forced include (option /FI) of the iso646.h header.
For work in the command interpreter you can put this in your CL environment variable, or e.g. in a response file.
Turning off Visual C++ language extensions with /Za can also do the trick, at the cost of rendering the compiler pretty much useless – since Microsoft code such as the Windows API headers generally uses the extensions.
On a more recent version of VS (tested on Version 15.7.0 Preview 3.0); ensure that conformance mode is set for visual studio:
It then compiles with the alternative operator representations.

Alternative tokens (not, and, etc...) in VisualStudio 2013

The "not", "and", etc... are keywords in C++ (macros in C). Is there any way to "enable" them in Visual Studio 2013? I'm able to use the words as macroses with iso646.h included. But VS seems not be able to recognize them as keywords.
Using /Za seems to enable them without including iso646.h, see it live, the following program produces an error without using /Za but works fine otherwise:
int main()
{
int x = 1, y = 0 ;
if (x and y)
{
//...
}
return 0;
}
As ta.speot.is indicates /Za disables extensions, the following documentation indicates you must include ios646.h otherwise:
Under /Ze, you have to include iso646.h if you want to use text forms of the following operators:
and it lists the alternative tokens below.
Note, I knew I saw this before, I include a link to a bug report for this in my answer to a similar question. Although this does not include the workaround noted above.
Note 2: Cheers and hth. - Alf indicates that there may be many undesirable consequences to turning off extension and therefore you may be better off just including iso646.h.
Use a forced include (option /FI) of the iso646.h header.
For work in the command interpreter you can put this in your CL environment variable, or e.g. in a response file.
Turning off Visual C++ language extensions with /Za can also do the trick, at the cost of rendering the compiler pretty much useless – since Microsoft code such as the Windows API headers generally uses the extensions.
On a more recent version of VS (tested on Version 15.7.0 Preview 3.0); ensure that conformance mode is set for visual studio:
It then compiles with the alternative operator representations.

/MP doesn't enable OpenMP support in VS2012

I have this code in visual c++ 2012:
void Run()
{
#pragma omp parallel for
for(int imageNo=0;imageNo<4;imageNo++)
{
RunForOneImage(imageNo, Data[imageNo],
width, height,
OutputWidth, OutputHeight);
}
}
All variables in RunForOneImage are local variables and it doesn't use any global or class member variables.
When run it, I can see that only one core became busy and other core are idle.
Why this is not running in parallel?
I selected multi processor option in visual studio (/MP). Is there any other option that I should select?
The /MP option means that the compilation will run on multiple processors, it's purpose is to "reduce the total time to compile the source files".
It has nothing to do with OpenMP API. To enable OpenMP support, there's an /openmp option (in the documentation you'll also find an example and the description of how to enable OpenMP support).
Just go to Project Properties->C/C++->Language->Open MP Support and switch it to Yes.

How can I use a C++11 to program the Arduino?

How can I use C++11 when programming the Arduino?
I would be fine using either the Arduino IDE or another environment. I am most interested in the core language improvements, not things that require standard library changes.
As of version 1.6.6, the Arduino IDE enables C++11 by default.
For older versions, read on:
It is very easy to change the flags for any element of the toolchain, including the assembler, compiler, linker or archiver.
Tested on the Arduino IDE version 1.5.7 (released on July 2014),
Locate the platform.txt file,
AVR architecture => {install path}\hardware\arduino\avr\platform.txt
SAM architecture => {install path}\hardware\arduino\sam\platform.txt
Inside that file, you can change any flag, for instance,
compiler.c.flags for changing the default compilation flags for C++ files.
compiler.cpp.flags for changing the default compilation flags for C++ files.
You can also change any of the "recipes" or compile patters, at the corresponding section of the configuration file, under the title "AVR/SAM compile patterns".
After making the changes, you must restart the Arduino IDE, at least on version 1.5.7.
For instance,
To enable support for C++11 (C++0x), tested on Arduino IDE versions 1.5.7 and 1.5.8, you will simply add the flag "-std=gnu++11" at the end of the line starting with compiler.cpp.flags=".
It is expected that C++11 is enabled by default in the near future on the Arduino IDE. However, as of version 1.5.8 (Oct 2014) it is still not the case.
Arduino IDE 1.6.6 and newer have C++11 enabled by default (they have the compiler flag "-std=gnu++11" set in the platform.txt file).
Firstly, only GCC 4.7 and above (and therefore AVR-GCC 4.7 and above) support C++11. So, check the versions installed with:
gcc --version
avr-gcc --version
If AVR-GCC is 4.7 or higher, then you may be able to use C++11.
The Arduino IDE does not support custom compiler flags. This has been requested, but has not yet been implemented.
So, you are left with having to use other environments or to compile your program directly from the command line.
In case, of compiling directly from the command line using AVR-GCC, you simply need to add an extra compiler flag for enabling C++11 support.
-std=c++11
For specific development environments, most would support editing of the compiler flags from the build options within the IDE. The above mentioned flag needs to be added to the list of flags for each environment.
C++0x was the name of working draft of the C++11 standard. C++0x support is available GCC 4.3 onwards. However, this is strictly experimental support so you cannot reliably expect C++11 features to be present. Here is the complete list of features available with the corresponding version of GCC. The availability of features in AVR-GCC will be the same as what's available in the corresponding GCC version.
The compiler flag for C++0x is:
-std=c++0x
Please, note, that there is no easy way to specify additional flags from Arduino IDE or use other IDE (Eclipse, Code::Blocks, etc.) or command line.
As a hack, you can use a small proxy program (should be cross-platform):
//============================================================================
// Name : gcc-proxy.cpp
// Copyright : Use as you want
// Description : Based on http://stackoverflow.com/questions/5846934/how-to-pass-a-vector-to-execvp
//============================================================================
#include <unistd.h>
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]) {
vector<string> arguments;
vector<const char*> aptrs;
// Additional options, one per line
ifstream cfg((string(argv[0]) + ".ini").c_str());
if (cfg.bad())
cerr << "Could not open ini file (you're using proxy for some reason, er?)" << endl;
string arg;
while (cfg) {
getline(cfg, arg);
if(arg == "\r" || arg == "\n")
continue;
arguments.push_back(arg);
}
for (const string& arg : arguments)
aptrs.push_back(arg.c_str());
for (int i = 1; i < argc; ++i)
aptrs.push_back(argv[i]);
// Add null pointer at the end, execvp expects NULL as last element
aptrs.push_back(nullptr);
// Pass the vector's internal array to execvp
const char **command = &aptrs[0];
return execvp(command[0], command);
}
Compile the program.
Rename the original avr-g++.exe to avr-g++.orig.exe (or any other name).
Create avr-g++.ini file where the first line is FULL path to the original program (e.g. D:\Arduino\hardware\tools\avr\bin\avr-g++.orig.exe) and add additional parameters, one per line, as desired.
You're done!
Example avr-g++.ini:
D:\Arduino\hardware\tools\avr\bin\avr-g++.orig.exe
-std=c++0x
I use Ino and this worked:
ino build -cppflags="-std=c++0x"
This generated a hex file at least 15k in size (that's with optimizations turned on), compared to about 5k for the standard build, which is a consideration for a poor little ATmega328. It might be okay for one of the microcontrollers with a lot more program space.
If you need more control and a better IDE, I recommend using Sloeber Plugin for Eclipse or the Sloeber IDE itself.
Creating more complicated code is much easier using this IDE. It also allows to add flags to the compiler (C, C++ and linker). So to customize the compile, just right click on project and select Properties. In the Properties window, select Arduino → Compiler Option. This way you can add options to your build.

Visual Studio 2010 and SSE 4.2

I would like to know, what is necessary to set in Visual Studio 2010, to have SSE 4.2 enabled? I would like to use it because of optimized POPCNT...
How can I test, if all settings are ok?
thanks
well, I tried to use your solution, however <nmmintric.h> is not included in vstudio2010 and standard __popcnt requires int instead of std::bitset<> :(
any idea?
Thx for the hint with the correct header. However, it seems that: error C3861: '_mm_popcnt_u64': identifier not found, I found only _mm_popcnt_u32, however I don't know, how to use it with bitset, or should I use just bitset<>.count? It can't work without anz settings of compiler, can it?
nobody knows ?
you have to write _mm_popcnt_u64 in your code. Also better check it the cpu you run on supports the instruction.
And build for x64.
#include <stdio.h>
#include <nmmintrin.h>
int main ()
{
unsigned __int64 a = 0x123456789ABCDEF0;
int res = _mm_popcnt_u64(a);
printf_s("Result res should be 32: %d\n", res);
return 0;
}
MSDN example for __popcnt:
http://msdn.microsoft.com/en-us/library/bb385231.aspx
There is nothing special required for this to work.
You may use intrinsics mm* and include the appropriate header file, and it will compile if your system support the given features.
The compiler does not inspect or touch inline assembly, so whatever you put in there will go through the build, although your application will crash if your system does not support an instruction.
Other than that, the VS2010 optimizer only targets SSE2.