NaN's are getting reported as 0 while compiling with icpx and not as NaN - intel

On running this program, in the output Nan"s are being reported as 0, when building with icpx V2022.1, it is working fine with other compilers.
Compiling with the command: icpx -O3 -qmkl=sequential
#define ARMA_DONT_USE_WRAPPER
#include <armadillo>
int main() {
arma::Col<double> var;
var.randu(4);
var.print();
std::cout << std::endl;
var[0] = arma::datum::nan;
// Same with var[0] = std::numeric_limits<double>::quiet_NaN()
var.print();
return 0;
}

The Intel compilers' default optimization setting is -O2.
As suggested by Peter Cordes in the above comment, You can use the command"-fp-model=precise" flag, to instruct the compiler to strictly follow value-safe optimizations when implementing floating-point computations.
Hope this solves the issue.

Related

OpenMP code in CUDA source file not compiling on Google Colab

I am trying to run a simple Hello World program with OpenMP directives on Google Colab using OpenMP library and CUDA. I have followed this tutorial but I am getting an error even if I am trying to include %%cu in my code. This is my code-
%%cu
#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
/* Main Program */
int main(int argc , char **argv)
{
int Threadid, Noofthreads;
printf("\n\t\t---------------------------------------------------------------------------");
printf("\n\t\t Objective : OpenMP program to print \"Hello World\" using OpenMP PARALLEL directives\n ");
printf("\n\t\t..........................................................................\n");
/* Set the number of threads */
/* omp_set_num_threads(4); */
/* OpenMP Parallel Construct : Fork a team of threads */
#pragma omp parallel private(Threadid)
{
/* Obtain the thread id */
Threadid = omp_get_thread_num();
printf("\n\t\t Hello World is being printed by the thread : %d\n", Threadid);
/* Master Thread Has Its Threadid 0 */
if (Threadid == 0) {
Noofthreads = omp_get_num_threads();
printf("\n\t\t Master thread printing total number of threads for this execution are : %d\n", Noofthreads);
}
}/* All thread join Master thread */
return 0;
}
And this is the error I am getting-
/tmp/tmpxft_00003eb7_00000000-10_15fcc2da-f354-487a-8206-ea228a09c770.o: In function `main':
tmpxft_00003eb7_00000000-5_15fcc2da-f354-487a-8206-ea228a09c770.cudafe1.cpp:(.text+0x54): undefined reference to `omp_get_thread_num'
tmpxft_00003eb7_00000000-5_15fcc2da-f354-487a-8206-ea228a09c770.cudafe1.cpp:(.text+0x78): undefined reference to `omp_get_num_threads'
collect2: error: ld returned 1 exit status
Without OpenMP directives, a simple Hello World program is running perfectly as can be seen below-
%%cu
#include <iostream>
int main()
{
std::cout << "Welcome To GeeksforGeeks\n";
return 0;
}
Output-
Welcome To GeeksforGeeks
There are two problems here:
nvcc doesn't enable or natively support OpenMP compilation. This has to be enabled by additional command line arguments passed through to the host compiler (gcc by default)
The standard Google Colab/Jupyter notebook plugin for nvcc doesn't allow passing of extra compilation arguments, meaning that even if you solve the first issue, it doesn't help in Colab or Jupyter.
You can solve the first problem as described here, and you can solve the second as described here and here.
Combining these in Colab got me this:
and then this:

64-bit ELF yielding unexplainable results

Can someone explain why the following code yields different results on the second printf if I comment the first printf line or not, in 64 bits?
/* gcc -O0 -o test test.c */
#include <stdio.h>
#include <stdlib.h>
int main() {
char a[20] = {0};
char b = 'a';
int count=-1;
// printf("%.16llx %.16llx\n", a, &b);
printf("%x\n", *(a+count));
return 0;
}
I get the following results for the second printf:
commented: 0
uncommented: 61
Thanks in advance!
iansus
Can someone explain why the following code yields different results on the second printf if I comment the first printf line or not
Your program uses a[-1], and thus exhibits undefined behavior. Anything can happen, and figuring out exactly why one or the other thing happenes is pointless.
The precise reason is that you are reading memory that gets written to by the first printf (when commented in).
I get a different result (which is expected with undefined behavior):
// with first `printf` commented out:
ffffffff
// with it commented in:
00007fffffffdd20 00007fffffffdd1b
ffffffff
You could see where that memory is written to by setting a GDB watchpoint on it:
(gdb) p a[-1]
$1 = 0 '\000'
(gdb) p &a[-1]
$2 = 0x7fffffffdd1f ""
(gdb) watch *(int*)0x7fffffffdd1f
Hardware watchpoint 4: *(int*)0x7fffffffdd1f
(gdb) c
Continuing.
Hardware watchpoint 4: *(int*)0x7fffffffdd1f
Old value = 0
New value = 255
main () at t.c:12
12 printf("%.16llx %.16llx\n", a, &b);
It my case above, the value is written as part of initializing count=-1. That is, with my version of gcc, count is located just before a[0]. But this may depend on compiler version, exactly how this compiler was built, etc. etc.

What is wrong with foreach in Qt?

Well, I tried it all. This should be very simple, yet I am stock at finding out what in the world is going on with my foreach. It just don't help.
#include <QCoreApplication>
//coreapplication or Qapplication the error is there
#include <QList>
#include <QDebug>
int main()
{
QList<int> list;
list << 1 << 2 << 3 << 4 << 5;
foreach (int i, list) //expected token ';' got 'int'.
{
qDebug() << i;
}
}
/*
QT += core gui
TARGET = QtTest
CONFIG += console
CONFIG -= app_bundle
CONFIG += no_keywords
TEMPLATE = app
SOURCES += main.cpp
*/
You specified no_keywords in your config. You have to use Q_FOREACH instead of foreach. See the documentation for foreach.
That being said, I would switch to the C++11 range-based for, since it doesn't have issues with commas in types. For example,
Q_FOREACH (QPair<int, int> p, pairList)
won't compile since the preprocessor thinks you're trying to invoke the macro with 3 arguments instead of 2.
Instead you can use the C++11 for(:):
for(int i:list)
{
qDebug() << i;
}
Note that you will have to compile with the C++-11 flag, therefore add this line to your project file:
QMAKE_CXXFLAGS += -std=c++11
Note that the C++11 for is more efficient than the Qt foreach as indicated by: Qt foreach loop ordering vs. for loop for QList
Edit:
Like commented by Frank Osterfeld you can also use:
CONFIG+=c++11
in your .pro file since Qt 5.4 as commented here: How to use C++11 in your Qt Projects.

clGetPlatformID error when running OpenCL program.

I've been trying to make OpenCL work in my laptop. I followed this link for that. I have an NVIDIA GT525M video card and Windows 8.1.
The steps I followed were:
Installing the up-to-date NVIDIA drivers.
Installing Visual Studio 2013 Community Edition.
Adding the paths for the linker and the compiler.
and then I tried to run the following code as given in that page:
#include <stdio.h>
#include <CL/cl.h>
int main(void)
{
cl_int err;
cl_uint* numPlatforms=NULL;
err = clGetPlatformIDs(0, NULL,numPlatforms);
if (CL_SUCCESS == err)
printf("\nDetected OpenCL platforms: %d", numPlatforms);
else
printf("\nError calling clGetPlatformIDs. Error code: %d", err);
getchar();
return 0;
}
The code builds successfully but the result I get is:
Error calling clGetPlatformIDs. Error code: -30
I get zero as the number of platforms.
I've been looking all over the internet for a solution but couldn't find one. Please help.
This:
cl_uint* numPlatforms=NULL;
err = clGetPlatformIDs(0, NULL,numPlatforms);
Is equivalent to this:
err = clGetPlatformIDs(0, NULL, NULL);
Which doesn't make sense. As per the documentation:
Returns CL_SUCCESS if the function is executed successfully. Otherwise it returns CL_INVALID_VALUE if num_entries is equal to zero and platforms is not NULL, or if both num_platforms and platforms are NULL.
CL_INVALID_VALUE is the -30 you are getting, for the reasons stated above.
What you really want is the following:
cl_uint numPlatforms = 0;
err = clGetPlatformIDs(0, NULL, &numPlatforms);

Qt 4.7.4: Is there a way to find out the status of CAPS LOCK?

I know there were earlier problems with this in < 4.7.4 Qt versions. has this been resolved?
I don't know any Qt solution.
However this code should work on both windows (not tested) and x11-based os (works on linux)
#include <X11/XKBlib.h>
#include <QX11Info>
bool capsOn()
{
#ifdef Q_WS_WIN // MS Windows version
return GetKeyState(VK_CAPITAL) == 1;
#elif Q_WS_X11 // X11 version
unsigned int n = 0;
Display *d = QX11Info::display();
XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
return (n & 0x01) == 1;
#else
# error Platform not supported
#endif
}
On X11 don't forget to add -lX11 to LIBS in your qmake project file.
I don't exactly know how to do this on OS X. If you need it, take a look at IOHIKeyboard and its's alphaLock() function. Also check this, especially the function darwinQueryHIDModifiers.

Resources