Guruh talabasi Qo`narov Shohrux parallel ishlov berish fanidan


Download 79.13 Kb.
bet2/2
Sana31.01.2024
Hajmi79.13 Kb.
#1831862
1   2
Amaliy qism
OpenMP kutubxonasi yordamida oddiy misol ishlash:
OpenMP paketi parallel ishlashni amalga oshiradi va biz bajarayotgan hisob-kitob ishlarni sezilarli darajada tezlashtiradi. Quyida keltirilgan misollar (yordamida massivlar ustida hisob-kitoblarni parallel mintaqasiz ishlaganda masalani ishlash jarayoni qancha vaqt talab etganini ko‘rishimiz mumkin:
OpenMP da parallel mintaqasiz ishlash (Natija 12.3-rasm)
// ConsoleApplication3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include
#include
#include
#include
using namespace std;

int matriz1() {


int a[4][4], i, j;
for (i = 0; i < 4; ++i)
{
for (j = 0; j < 4; ++j)
{
a[i][j] = rand() % 100 + 1;
}
}

for (i = 0; i < 4; ++i)


{
for (j = 0; j < 4; ++j)
std::cout << a[i][j] << '\t';
std::cout << '\n';

}
std::cout << '\n';


std::cout << "x" << std::endl;
std::cout << '\n';
std::cout << "Matriz 2:" << std::endl;

int b[4][4], k, l;


for (k = 0; k < 4; ++k)
{
for (l = 0; l < 4; ++l)
{
b[k][l] = rand() % 100 + 1;
}
}

for (k = 0; k < 4; ++k)


{
for (l = 0; l < 4; ++l)

std::cout << b[k][l] << '\t';


std::cout << '\n';
}

std::cout << '\n';


int c[4][4], m, n, x;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
for (k = 0; k < 4; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
cout << " RESULTADO!!!!!!!!!!!!!!!!!!!!!!" << endl;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
cout << c[i][j] << "\t";
}
cout << "\n";
}
return 0;
}
int main()
{

//jarayon bajarilish vaqtini aniqlash uchun kerakli kalit so‘zlar


double itime, ftime, exec_time;
srand(time(0));
std::cout << "Matriz 1:" << std::endl;
std::cout << matriz1() << std::endl;
// Jarayonga ketgan vaqtni hisoblash
ftime = omp_get_wtime();
exec_time = ftime - itime;
cout << "Sarflangan vaqt: " << exec_time << endl;
system("pause");
return 0;
}

12.3-rasm. Parallel mintaqasiz olingan natija
12.3-rasmda keltirilgan natija asosida biz massivlar ustida hisob-kitoblarni parallel mintaqasiz masalani ishlash jarayoni qancha vaqt talab etganini ko‘rdik, ana endi ushbu misolni parallel mintaqa bilan ishlaganimizda masalani ishlash jarayoni vaqti sezilarli darajada kamayganini ko‘rishimiz mumkin:
OpenMP da parallel bilan ishlash (natija: 12.4-rasm)
// OpenMP.cpp
#include "stdafx.h"
#include
#include
#include
#include
#define ARRAY_SIZE 100000000
#define ARRAY_VALUE 1231
using namespace std;
int matriz1() {
int a[4][4], i, j;
#pragma omp parallel for
for (i = 0; i < 4; ++i)
{
#pragma omp parallel for
for (j = 0; j < 4; ++j)
{
a[i][j] = rand() % 100 + 1;
}
}

#pragma omp parallel for


for (i = 0; i < 4; ++i)
{
#pragma omp parallel for
for (j = 0; j < 4; ++j)
std::cout << a[i][j] << '\t';
std::cout << '\n';

}
std::cout << '\n';


std::cout << "x" << std::endl;
std::cout << '\n';
std::cout << "Matriz 2:" << std::endl;

int b[4][4], k, l;


#pragma omp parallel for
for (k = 0; k < 4; ++k)
{
#pragma omp parallel for
for (l = 0; l < 4; ++l)
{
b[k][l] = rand() % 100 + 1;
}
}

#pragma omp parallel for


for (k = 0; k < 4; ++k)
{
#pragma omp parallel for
for (l = 0; l < 4; ++l)

std::cout << b[k][l] << '\t';


std::cout << '\n';
}

std::cout << '\n';


int c[4][4], m, n, x;
#pragma omp parallel for
for (i = 0; i < 4; i++) {
#pragma omp parallel for
for (j = 0; j < 4; j++) {
#pragma omp parallel for
for (k = 0; k < 4; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
cout << " RESULTADO!!!!!!!!!!!!!!!!!!!!!!" << endl;
#pragma omp parallel for
for (i = 0; i < 4; i++) {
#pragma omp parallel for
for (j = 0; j < 4; j++) {
cout << c[i][j] << "\t";
}
cout << "\n";
}
return 0;
}
int main() {
//jarayon bajarilish vaqtini aniqlash uchun kerakli kalit so‘zlar
double itime, ftime, exec_time;
itime = omp_get_wtime();
omp_set_num_threads(2);
srand(time(0));
std::cout << "Matriz 1:" << std::endl;
std::cout << matriz1() << std::endl;
// Jarayonga ketgan vaqtni hisoblash
ftime = omp_get_wtime();
exec_time = ftime - itime;
cout << "Sarflangan vaqt: " << exec_time << endl;
system("pause");
return 0;

}


12.4-rasm. Parallel mintaqada olingan natija
Yuqorida keltirilgan natijalarga ko‘rib turgamnimizdek, OpenMP C/C++ da multi-threading(ko‘p-oqimlar)dan foydalanish bizga ko‘p vaqtni tejash imkonini beradi.

Download 79.13 Kb.

Do'stlaringiz bilan baham:
1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling