}
}
while(lLeftIt < pLeft.size())
{
lResult.push_back(pLeft.at(lLeftIt));
lLeftIt++;
}
while(lRightIt < pRight.size())
{
lResult.push_back(pRight.at(lRightIt));
lRightIt++;
}
return lResult;
}
QVector<double> mergesort(QVector<double> &pVec, int threads)
{
if(pVec.size() == 1)
{
return pVec;
}
int mid = pVec.size() / 2;
QVector<double> lLeft;
for(int i = 0; i < mid; i++)
{
lLeft.append(pVec.at(i));
}
QVector<double> lRight;
for(int i = mid; i < pVec.size(); i++)
{
lRight.append(pVec.at(i));
}
if (threads > 1)
{
#pragma omp parallel sections
{
#pragma omp section
{
lLeft = mergesort(lLeft, threads / 2);
}
#pragma omp section
{
lRight = mergesort(lRight, threads - threads / 2);
}
}
}
else
{
lLeft = mergesort(lLeft, 1);
lRight = mergesort(lRight, 1);
}
return merge(lLeft, lRight);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int n = 0;
int high = 10000;
int low = 0;
int threads = 1;
printf("N = ");
scanf("%d", &n);
printf("Threads count = ");
scanf("%d", &threads);
QVector<double> vec;
for(int i = 0; i < n; i++)
{
vec.append(qrand() % ((high + 1) - low) + low);
}
double t_start = omp_get_wtime();
vec = mergesort(vec, threads);
double t_end = omp_get_wtime();
printf("Time = %lf\n", t_end - t_start);
return a.exec();
}
openmp_example.pro
QT += core
QT -= gui
TARGET = openmp_example
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
QMAKE_CXXFLAGS += -fopenmp
LIBS += -fopenmp
SOURCES += main.cpp
Додаток Б «Клас CEngine»
#include "cengine.h"
CEngine::CEngine()
{
}
void CEngine::set_numgroups(int num)
{
this->numgroups = num;
this->groups = new CObject[num];
}
void CEngine::set_numteachers(int num)
{
this->numteachers = num;
this->teachers = new CObject[num];
}
void CEngine::set_numclassrooms(int num)
{
this->numclassrooms = num;
this->classrooms = new CObject[num];
}
void CEngine::set_numsubjects(int num)
{
this->numsubjects = num;
this->subjects = new CObject[num];
}
void CEngine::Start_Sort()
{
int teach = -1;
int aud = -1;
bool set = false;
for(int i = 0; i < numgroups; i++)
{
for(int j = 0; j < groups[i].task_num; j++)
{
if(groups[i].task[j] > 0)
{
teach = -1;
aud = -1;
set = false;
for(int l = 0; l < 6; l++)
{
for(int k = 0; k < 5; k++)