// Hannuo. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include" OMP. H "# include <windows. h> # include <iostream> # include <time. h> # define num_threads 2 using namespace STD; int _ tmain (INT argc, _ tchar * argv []) {omp_set_num_threads (num_threads); int A [400] [400]; // inint matrix int counter = 1; for (INT I = 0; I <400; I ++) {for (Int J = 0; j <400; j ++) {A [I] [J] = counter; counter ++; }}// for (INT I = 0; I <20; I ++) // {// For (Int J = 0; j <20; j ++) // cout <A [I] [J] <""; // Cout <Endl; //} cout <"parallel begin" <Endl; clock_t time_begin = clock (); int I = 0, j = 0; # pragma OMP parallel for private (I, j) for (I = 0; I <400; I ++) for (j = I + 1; j <400; j ++) // starts from I + 1. Run {long TMP = 0; TMP = A [I] [J]; A [I] [J] = A [J] [I]; A [J] [I] = TMP;} clock_t time_end = clock (); double S1 = time_end-time_begin; cout <"parallel end" <Endl; cout <"parallel time =" <S1 <Endl; // ================================================ ========================================================== ============================== time_begin = clock (); cout <"Serail begin" <Endl; For (INT I = 0; I <400; I ++) for (Int J = I + 1; j <400; j ++) // starts with I + 1. Run {int TMP = 0; TMP = A [I] [J]; A [I] [J] = A [J] [I]; A [J] [I] = TMP;} time_end = clock (); cout <"Serail end" <Endl; double S2 = time_end-time_begin; cout <"Serail time =" <S2 <Endl; System ("pause"); Return 0 ;}
Matrix transpose in OpenMP