Hello,
I created a Console Project to test Open MP inside QtCreator/MingW32. My Qt version is 5.3. The compiler is mingw482_32.
Here is the project file
#-------------------------------------------------
#
# Project created by QtCreator 2021-06-18T17:05:54
#
#-------------------------------------------------
QT += core
QT -= gui
TARGET = OpenMPTest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
win32:CONFIG(release, debug|release):QMAKE_CXXFLAGS += -std=c++11 -O3
QMAKE_CXXFLAGS+= -openmp
QMAKE_LFLAGS += -fopenmp
SOURCES += main.cpp \
testOpenMP.cpp
HEADERS += \
testOpenMP.h
#-------------------------------------------------
#
# Project created by QtCreator 2021-06-18T17:05:54
#
#-------------------------------------------------
QT += core
QT -= gui
TARGET = OpenMPTest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
win32:CONFIG(release, debug|release):QMAKE_CXXFLAGS += -std=c++11 -O3
QMAKE_CXXFLAGS+= -openmp
QMAKE_LFLAGS += -fopenmp
SOURCES += main.cpp \
testOpenMP.cpp
HEADERS += \
testOpenMP.h
To copy to clipboard, switch view to plain text mode
And here is the content of testOpenMP.cpp:
#include <iostream>
#include <math.h>
#include <time.h>
#include <omp.h>
#define SIZE_ARRAY 20000
int array_floor_total[SIZE_ARRAY];
void do_compute(int j)
{
double total = 0;
for (int i = 0; i < SIZE_ARRAY; ++i)
total += sqrt(i+j);
int floor_total;
floor_total = floor(total);
array_floor_total[j] = floor_total % 2;// the various threads need to write to common memory
}
void test_accellerate_loop()
{
int end = SIZE_ARRAY;
clock_t t1 = clock();
for (int i = 0; i < end; ++i)
do_compute(i);
clock_t t2 = clock();
std::cout << "time taken (no acceleration)"<<t2 -t1<<"\n";
clock_t t3 = clock();
#pragma omp parallel for // This OMP directive tells the compiler to parallelise the next loop
for (int i = 0; i < end; ++i)
do_compute(i);
clock_t t4 = clock();
std::cout << "time taken (with acceleration)" << t4 - t3 << "\n";
std::cout << "Press return\n";
getchar();// pause
}
#include <iostream>
#include <math.h>
#include <time.h>
#include <omp.h>
#define SIZE_ARRAY 20000
int array_floor_total[SIZE_ARRAY];
void do_compute(int j)
{
double total = 0;
for (int i = 0; i < SIZE_ARRAY; ++i)
total += sqrt(i+j);
int floor_total;
floor_total = floor(total);
array_floor_total[j] = floor_total % 2;// the various threads need to write to common memory
}
void test_accellerate_loop()
{
int end = SIZE_ARRAY;
clock_t t1 = clock();
for (int i = 0; i < end; ++i)
do_compute(i);
clock_t t2 = clock();
std::cout << "time taken (no acceleration)"<<t2 -t1<<"\n";
clock_t t3 = clock();
#pragma omp parallel for // This OMP directive tells the compiler to parallelise the next loop
for (int i = 0; i < end; ++i)
do_compute(i);
clock_t t4 = clock();
std::cout << "time taken (with acceleration)" << t4 - t3 << "\n";
std::cout << "Press return\n";
getchar();// pause
}
To copy to clipboard, switch view to plain text mode
The main file is essentially a call to test_accellerate_loop();
Now here's the unexpected fact:
Whereas the acceleration is considerable in Debug mode, it's almost non existent in Release mode.
Here is one MignW32 output
time taken (no acceleration)840
time taken (with acceleration)847
I compiled the same testOpenMp.cpp in Visual Studio. There is a big difference. And outside VS the program runs even much faster.
Visual Studio executable, at the command line.
time taken (no acceleration)1076
time taken (with acceleration)203
Any explanation? Do I have the wrong optimiser flags?
Bookmarks