Вы находитесь на странице: 1из 13

OpenMP: : 24.01.

2009 ,

. , . OpenMP, . OpenMP . . , , OpenMP , , . OpenMP.

. . . . , . , : , . . ( , , , . . , , . , , N1. OpenMP. ? , , , , ) . , ,

Function, 1. .
double Function(int N) { double x, y, s=0; for (int i=1; i<=N; i++) { x = (double)i/N; y = x; for (int j=1; j<=N; j++) { s += j * y; y = y * x; }; }; return s; }

N,

15000,

287305025.528. OpenMP.

for (
double FunctionOpenMP(int N) { double x, y, s=0; #pragma omp parallel for num_threads(2) for (int i=1; i<=N; i++) { x = (double)i/N; y = x; for (int j=1; j< =N; j++) { s += j * y; y = y * x; }; };

2).

return s; }

, . . , . . . Microsoft Visual Studio 2005, . . , , OpenMP, . OpenMP , , . , y). . x, x , race condition , , , data race ( " " . , " " ). . Thread Checker. : http://www.viva64.com/go.php?url=526. Intel VTune Performance Analyzer, . Intel Thread Checker , , deadlocks (" ) . Intel Thread Checker Analyzer New Project Threading Wizards ( Intel Thread Checker Wizard. . , , Intel Thread Checker, 1. . - Intel ; , , , s , . , x, x, y s , , x y (x , , . , (private), , (shared), , , 298441282.231.

",

Intel VTune Performance ), ,

1,

Thread Checker

, ,

rce V e (

Chec er

2.

. Thre

2-

Intel Thread Checker Chec er . ,

, . . ,

- V v MP, V v MP

V v MP: y y y y V v MP

OpenMP OpenMP .

"

V sual Studio 2005/2008 3.

 

/ru/v v

p-tool/.

 



. http://www.v v 64.c

OpenMP


.

 



Inte Thre

 

   

V v MP

OpenMP. . .

3VivaMP ,

VivaMP, ,

Visual Studio 2005 4

4, ( . . , Intel Thread Chec er VivaMP)

VivaMP .

Intel Thread Chec er VivaMP x parallel for , , s. , . s += j*y. s, . s += j*y, . . , , , , . , y : private (x, y). : , x y.

4.

#pragma omp

s, s. , , , , s, s, , , . , #pragma omp atomic. 3. , , , . s += j*y . , , . , . , ,

j*y

double FixedFunctionOpenMP(int N) { double x, y, s=0; #pragma omp parallel for \ private(x,y) num_threads(2) for (int i=1; i<=N; i++) { x = (double)i/N; y = x; for (int j=1; j<=N; j++) { #pragma omp atomic s += j * y; y = y * x; }; }; return s; }

Thread Checker . , MathFunction. . , return , VivaMP

. . N=15000 , 31.

, : 1. , 2-

287305025.528 0.5781 298441282.231 2.9531 287305025.528 36.8281

? .

, . 60 . , ? , .

, s, . reduction. &, |, ^, &&, ||.


double OptimizedFunction(int N) { double x, y, s=0; #pragma omp parallel for private(x,y) \ num_threads(2) reduction(+: s) for (int i=1; i<=N; i++) { x = (double)i/N; y = x; for (int j=1; j<=N; j++) { s += j * y; y = y * x; }; }; return s; }

32)
atomic. reduction . 4. , : +, *, -,

1 %

1 10 &) ('

atomic 1-

&%

, ( 2). . 2 ( 1.85 ),

, atomic ,

287305025.528 0.5781 298441282.231 2.9531 287305025.528 36.8281 287305025.528 0.3125

Intel Thread Checker VivaMP, .

N1.
#include "stdafx.h" #include <omp.h> #include <stdlib.h> #include <windows.h> class VivaMeteringTimeStruct { public: VivaMeteringTimeStruct() { m_userTime = GetCurrentUserTime(); } ~VivaMeteringTimeStruct() { printf("Time = %.4f seconds \n", GetUserSeconds()); } double GetUserSeconds(); private: __int64 GetCurrentUserTime() const; __int64 m_userTime; }; __int64 VivaMeteringTimeStruct::GetCurrentUserTime() const

BA8
, . , . .

@ 4

@ @9 58 76

reduction 2-

54

{ FILETIME creationTime, exitTime, kernelTime, userTime; GetThreadTimes(GetCurrentThread(), &creationTime, &exitTime, &kernelTime, &userTime); __int64 curTime; curTime = userTime.dwHighDateTime; curTime <<= 32; curTime += userTime.dwLowDateT ime; return curTime; } double VivaMeteringTimeStruct::GetUserSeconds() { __int64 delta = GetCurrentUserTime() - m_userTime; return double(delta) / 10000000.0; } double Function(int N) { double x, y, s=0; for (int i=1; i<=N; i++) { x = (double)i/N; y = x; for (int j=1; j<=N; j++) { s += j * y; y = y * x; }; }; return s; } double FunctionOpenMP(int N) {

double x, y, s=0; #pragma omp parallel for num_threads(2) for (int i=1; i<=N; i++) { x = (double)i/N; y = x; for (int j=1; j<=N; j++) { s += j * y; y = y * x; }; }; return s; } double FixedFunctionOpenMP(int N) { double x, y, s=0; #pragma omp parallel for private(x,y) num_threads(2) for (int i=1; i<=N; i++) { x = (double)i/N; y = x; for (int j=1; j<=N; j++) { #pragma omp atomic s += j * y; y = y * x; }; }; return s; } double OptimizedFunction(int N) { double x, y, s=0;

#pragma omp parallel for private(x,y) \ num_threads(2) reduction(+: s) for (int i=1; i<=N; i++) { x = (double)i/N; y = x; for (int j=1; j<=N; j++) { s += j * y; y = y * x; }; }; return s; } int _tmain(int , _TCHAR* []) { int N = 15000; { VivaMeteringTimeStruct Timer; printf("Result = %.3f } { VivaMeteringTimeStruct Timer; printf("Result = %.3f } { VivaMeteringTimeStruct Timer; printf("Result = %.3f } { VivaMeteringTimeStruct Timer; printf("Result = %.3f ", OptimizedFunction(N)); ", FixedFunctionOpenMP(N)); ", FunctionOpenMP(N)); ", Function(N));

} return 0; }

Оценить