Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <omp.h>
- using namespace std;
- float **transpose_serial(int row, int col, float **core, float **result) {
- int i, j;
- for (i = 0; i < row; i++)
- for (j = 0; j < col; j++) {
- result[j][i] = core[i][j];
- }
- return result;
- }
- float **transpose_parall(int row, int col, float **core, float **result) {
- int i, j;
- #pragma omp parallel for shared(core, b, row, col) private(i, j) default(none)
- for (i = 0; i < row; i++)
- for (j = 0; j < col; j++) {
- result[j][i] = core[i][j];
- }
- return result;
- }
- int main(int argc, char *argv[]) {
- int row = 100;
- int col = 100;
- int is_display = 0 ;
- // int row = atoi(argv[1]); // количество строк матрицы
- // int col = atoi(argv[2]); // количество столбцов матрицы
- // int is_display = atoi(argv[3]);
- int i, j;
- float **core; // матрица
- float **serial_res; // результат
- float **parall_res; // результат параллельного
- double t1parl, t2parl, t1serial, t2serial;
- core = new float *[row];
- serial_res = new float *[col];
- parall_res = new float *[col];
- for (i = 0; i < row; i = i + 1) {
- core[i] = new float[col];
- }
- for (i = 0; i < col; i = i + 1) {
- serial_res[i] = new float[row];
- parall_res[i] = new float[row];
- }
- for (i = 0; i < row; i = i + 1) {
- for (j = 0; j < col; j = j + 1) {
- core[i][j] = i * 1.0;
- }
- }
- if (!is_display) {
- // вывод матрицы
- for (i = 0; i < row; i = i + 1) {
- for (j = 0; j < col; j = j + 1) {
- cout << core[i][j] << ' ';
- }
- cout << endl;
- }
- }
- t1serial = omp_get_wtime();
- serial_res = transpose_serial(row, col, core, serial_res);
- t2serial = omp_get_wtime();
- t1parl = omp_get_wtime();
- parall_res = transpose_parall(row, col, core, parall_res);
- t2parl = omp_get_wtime();
- if (!is_display) {
- cout << endl << "Serial calculation: " << endl;
- for (i = 0; i < col; i++) {
- for (j = 0; j < row; j++) {
- cout << serial_res[i][j] << ' ';
- }
- cout << endl;
- }
- cout << endl <<"Parallel calculation: " << endl;
- for (i = 0; i < col; i++) {
- for (j = 0; j < row; j++) {
- cout << parall_res[i][j] << ' ';
- }
- cout << endl;
- }
- }
- bool flag = true;
- for (i = 0; i < col; i = i + 1) {
- for (j = 0; j < row; j = j + 1) {
- if (serial_res[i][j] != parall_res[i][j]) {
- flag = false;
- break;
- }
- }
- if (!flag)
- break;
- }
- if (flag)
- cout << "The result is the same" << endl;
- else
- cout << "The result is not the same" << endl;
- cout << endl << "Parallel calculation time = " << (t2serial - t1serial) << endl;
- cout << "Serial calculation time = " << (t2parl - t1parl) << endl;
- for (i = 0; i < row; i = i + 1) {
- delete core[i];
- }
- delete core;
- for (i = 0; i < col; i = i + 1) {
- delete serial_res[i];
- delete parall_res[i];
- }
- delete serial_res;
- delete parall_res;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement