I'm trying to run the below c++ code in Visual Studio compiling with both Visual C++ and İntel Compiler. I'm setting the /Qopenmp option. Although the omp_get_max_threads() result is 2 the "printf("Running on multiple threads\n")" part is not printed twice.
This is the code:
#include <omp.h>
int main(int argc, char* argv[])
{
printf("Starting Program!\n");
int ntr;
omp_set_dynamic(0);
omp_set_num_threads(2);
#pragma omp parallel
{
printf("Running on multiple threads\n");
ntr = omp_get_max_threads();
printf("%d\n",ntr);
}
printf("Finished!\n");
return 0;
}
This is the output
Starting Program!
Running on multiple threads
2
Finished!
What is wrong with this code or Visual Studio settings?
Related
I have found a strange behavior (probably an issue) while trying to compile a simple application with MS VC 15.3.1 (after applying VC 2017 Upgrade 3):
#include <iostream>
#include <algorithm>
#include <string>
// compiles if commenting out the line below
#include <vector>
#include <list>
class A
{
std::string s;
public:
A(const std::string& str) : s(str)
{
}
A(A&& other)
{
// compiles if changing std::swap<std::string>() to std::swap()
std::swap<std::string>(s, other.s);
}
};
int main(int argc, char *argv[])
{
return 0;
}
the compiler emits error :
1>d:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.11.25503\include\vector(2131):
error C2039: '_Alloc': is not a member of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
This code compiles without any issues with VC 15.2, VS2015 and VS2013 toolsets.
I tried to use C++ amp in Visual Studio 2017, but compiler said, "error C3564" and I tried the old code(which worked in Visual Studio 2015), and it said same thing.
Does Visual Studio 2017 support C++ amp?
#include "stdafx.h"
using namespace concurrency;
int main(void){
int size;
scanf_s("%d", &size);
array_view<int, 1> a(1);
parallel_for_each(extent<1>(1),
[=](index<1> &idx) restrict(amp)
{
a(idx) = size;
});
}
the code above will generate problem.
just added the amp.h file and the code above compiles.
i am also working on other project with C++AMP in vs 2017 and it working fine.
#include "stdafx.h"
#include <amp.h>
using namespace concurrency;
int main(void) {
int size;
scanf_s("%d", &size);
array_view<int, 1> a(1);
parallel_for_each(extent<1>(1),
[=](index<1> &idx) restrict(amp)
{
a(idx) = size;
});
}
Recently i installed visual studio community edition and tried to write small console application.
Program is getting build successfully. But console window is not getting pop up.
Is there a problem, i installed the visual studio on my System "D" drive.
My code snippet :
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Hello world";
return 0;
}
Kindly help
Add a cin.get() like so:
int main() {
cout << "Hello world";
cin.get(); // <- Waits for any key press directed at the console window
return 0;
}
I am trying to use VS2008 memory leak tool, but I have failed to build it at all.
The simplest scenarios works well, but when I try to use CObject - it does not compile
Here is the code (Its a newly create console application)
#include "stdafx.h"
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif // _DEBUG
#define _AFXDLL
#include "afx.h"
class DRV : public CObject {};
int _tmain(int argc, _TCHAR* argv[])
{
DRV *d = new DRV;
}
This results : error C2059: syntax error : 'constant' in afx.h:
void* PASCAL operator new(size_t nSize);
If I try to move the #ifdef _DEBUG below the #include "afx.h", I get:
error C2661: 'CObject::operator new' : no overloaded function takes 4 arguments
on line:
DRV *d = new DRV;
So - what am I doing wrong?
Can I use the build in VS2008 memory leak detector?
Please help
Create file DebugNew.h and add this code to it:
#pragma once
#include "crtdbg.h"
#ifdef _DEBUG
#define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_NEW
#endif
In cpp file:
#include "stdafx.h"
#include "DebugNew.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
int _tmain(int argc, _TCHAR* argv[])
{
CrtSetDbgFlag( _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
char *d = new char[100];
}
DebugNew.h file defines new operator, which allows to include source line information for every allocation. #define new DEBUG_NEW line redefines default new to DEBUG_NEW, only in Debug build. This line should be placed after all #include lines in all .cpp files. CrtSetDbgFlag enables memory leak allocation in debug build - when the program exits, all unreleased allocations are printed. Since new operator is redefined, they are printed with source line information.
For MFC projects, you only need to add lines
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
to every .cpp file. All other things are already done by MFC. MFC project, created by MFC application Wizard, already contains all required stuff by default. For example, create Win32 Console Application with MFC support using Wizard - memory leaks tracking is working. You only need to add new DEBUG_NEW redefinition to every new file added to the project.
The program below crashes when I build it in Release x64 (all other configurations run fine).
Am I doing it wrong or is it an OpenMP issue?
Well-grounded workarounds are highly appreciated.
To reproduce build a project (console application) with the code below.
Build with /openmp and /GL and (/O1 or /O2 or /Ox) options in Release x64 configuration.
That is OpenMP support and C++ optimization must be turned on. The resulting program should (should not) crash.
#include <omp.h>
#include <vector>
class EmptyClass
{
public:
EmptyClass() {}
};
class SuperEdge
{
public:
SuperEdge() {mp_points[0] = NULL; mp_points[1] = NULL;}
private:
const int* mp_points[2];
};
EmptyClass CreateEmptyClass(SuperEdge s)
{
return EmptyClass();
}
int main(int argc, wchar_t* argv[], wchar_t* envp[])
{
std::vector<int> v;
long count = 1000000;
SuperEdge edge;
#pragma omp parallel for
for(long i = 0; i < count; ++i)
{
EmptyClass p = CreateEmptyClass(edge);
#pragma omp critical
{
v.push_back(0);
}
}
return 0;
}
I think it is a bug. Looking at the ASM output with /O2 on the push_back call has been optimized away and there are just a couple of reserve calls and what looks like direct accesses instead. The reserve calls however don't appear to be in the critical section and you end up with Heap corruption. Doing a release x64 with /openmp /GL /Od you will see that there is a call to push_back in the asm, and it is between the _vcomp_enter_critsect calls, and doesn't crash. I'd report it to MS. (tested with VS 2010)