- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I am using intel c++ compiler icpc version 12.1.0 and I get this compilation error when compiling the following code. Can someone point out why it occurs?.
I am pasting my sample .h and .cpp file here:
myheader.h:
---------------
extern int j;
#pragma omp threadprivate(j)
==============================
mycppfile.cpp
-----------------
#include
#include
#include "myheader.h"
using namespace std;
int j;
int
main(int argc, char *argv[])
{
#pragma omp parallel num_threads(2)
{
cout<<"Number of threads created is" << omp_get_num_threads() << endl;
cout<<"My thread ID is " << omp_get_thread_num() << endl;
j = j + 10;
cout << "j=" << j;
}
return(0);
}
Basically because of the OMP primitives I should see j=10 printed twice. If the OMP primitives are removed one thread should print j=10 and other j=20. It is working with gcc but icpc 12.1.0 throws the error.I was using icpc version 12.0.3 previously and did not come across this error.I am currently working with a large code base containing similar scenario.Can some one point whats wrong here.
Thanks,
karthik
Link Copied
7 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Your application exiting before the values are printed. I have modified the code as below:
$ icc -openmp mycppfile.cpp
// mycppfile.cpp
#include
#include
#include "myheader.h"
using namespace std;
int j;
int
main(int argc, char *argv[])
{
#pragma omp parallel num_threads(2)
{
cout<<"Number of threads created is" << omp_get_num_threads() << endl;
cout<<"My thread ID is " << omp_get_thread_num() << endl;
j = j + 10;
cout << "j=" << j << endl;
}
cout.flush();
return(0);
}
$ icc -V
Intel C Intel 64 Compiler XE for applications running on Intel 64, Version 12.1.1.235 Build 20110824
Copyright (C) 1985-2011 Intel Corporation. All rights reserved.
$ icc -openmp mycppfile.cpp
$ ./a.out
Number of threads created is2
My thread ID is 0
j=10
Number of threads created is2
My thread ID is 1
j=10
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Om Sachan,
Thanks for the modification of code. I really appreciate it. Also should'nt we be using intel's c++ compiler icpc for compiling the code and linking with the c++ libraries. I am experiencing the posted error while using icpc 12.1.0 and my large c++ code base uses STLs a lot and hence it needs to be linked with c++ library. Please let me know your comments.
Thanks,
karthik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The icc driver is smart enough to recognize the .cpp extension on the file and compile as C++.
It's not that smart for some other file extensions so we still have icpc when you need it.
It's not that smart for some other file extensions so we still have icpc when you need it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
icpc includes -lstdc++ at link time (as well as including corresponding header search path in the compilation), finding that library in the g++ installation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
>> oops, disregard the following, I did not pay attention to the contents of the header file
j isn't initialized.
Although this can, or should I say may be, initialized to zero using linker switch, the code does not assert the initialization. (IOW bad programming assumption).
Assuming you explicitly have int j=0; or by reliance on linker to initialize j to 0, then due to data race in your code you have three possible results:
ID0, ID1
10, 20 (ID0 beat ID1)
20, 10 (ID1 beat ID0)
10, 10 (ID0 and ID1 performed Read of j before either performed modify/write)
Also note that j not being volatile or atomic tends to cause issues where j becomes registerized.
Jim Dempsey
j isn't initialized.
Although this can, or should I say may be, initialized to zero using linker switch, the code does not assert the initialization. (IOW bad programming assumption).
Assuming you explicitly have int j=0; or by reliance on linker to initialize j to 0, then due to data race in your code you have three possible results:
ID0, ID1
10, 20 (ID0 beat ID1)
20, 10 (ID1 beat ID0)
10, 10 (ID0 and ID1 performed Read of j before either performed modify/write)
Also note that j not being volatile or atomic
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Thanks to all for their replies. I really appreciate it.
But the main issue is I do not want to link with stdc++ libraries provided by gcc/g++. This is because the current libstdc++ does not allow non POD(Plain Old Data) types like class objects in thread local storage. Hence I switched to icc/icpc.
So my issue is why does this error occurs when we compile with icc/icpc 12.1.0using only intel libraries. But icc/icpc 12.0.3 does not give this error for the same code.
Thanks,
karthik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Both 12.0 and 12.1 compilers link against the same libstdc++, if you haven't changed your active g++ installation.
Newer Intel compiler versions tend to become more stricter about source code issues, sometimes even giving erroneous indications. I think maybe you don't mean to focus on this libstdc++ question. If you build C++, for g++ compatibility, icpc and g++ make the same requirements about presence of the same STL headers and libraries. If you don't use them in your code or libraries, they have no effect.
Newer Intel compiler versions tend to become more stricter about source code issues, sometimes even giving erroneous indications. I think maybe you don't mean to focus on this libstdc++ question. If you build C++, for g++ compatibility, icpc and g++ make the same requirements about presence of the same STL headers and libraries. If you don't use them in your code or libraries, they have no effect.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page