a proposal for user defined reductions in openmp
play

A proposal for User-Defined Reductions in OpenMP A. Duran 1 , R. - PowerPoint PPT Presentation

A proposal for User-Defined Reductions in OpenMP A. Duran 1 , R. Ferrer 1 , M. Klemm 2 , B. de Supinski 3 , E. Ayguad 1 1 BSC, 2 Intel, 3 LLNL June 16th 2010 Outline Motivation 1 UDR Design rationale 2 Declaring UDRs 3 Array reductions 4


  1. A proposal for User-Defined Reductions in OpenMP A. Duran 1 , R. Ferrer 1 , M. Klemm 2 , B. de Supinski 3 , E. Ayguadé 1 1 BSC, 2 Intel, 3 LLNL June 16th 2010

  2. Outline Motivation 1 UDR Design rationale 2 Declaring UDRs 3 Array reductions 4 5 C++ specific extensions Conclusions 6 Duran et al. () UDRs in OpenMP June 16th 2010 2 / 28

  3. Motivation Outline Motivation 1 UDR Design rationale 2 Declaring UDRs 3 Array reductions 4 C++ specific extensions 5 Conclusions 6 Duran et al. () UDRs in OpenMP June 16th 2010 3 / 28

  4. Motivation Reductions in OpenMP 3.0 Current OpenMP supports reduction: basic scalar types simple arithmetic operators (+,-,*,&,...) array reductions (Fortran only) min and max operators (Fortran only) Duran et al. () UDRs in OpenMP June 16th 2010 4 / 28

  5. Motivation Reductions in OpenMP 3.0 Current OpenMP supports reduction: basic scalar types simple arithmetic operators (+,-,*,&,...) array reductions (Fortran only) min and max operators (Fortran only) Users with other reductions must find their way manually: Using critical Using atomic (when possible) Adding complex code to implement the reduction Duran et al. () UDRs in OpenMP June 16th 2010 4 / 28

  6. Motivation Reductions by hand 1 complex_t complex_mul ( complex_t a , complex_t b ) ; 2 3 void example ( complex_t ∗ array , size_t N) { complex_t prd = {1.0 , 0 . 0 } ; 4 5 6 7 8 #pragma omp parallel reduction ( prd ) 9 { 10 11 #pragma omp for 12 ( size_t i = 0; i < N; i ++) for 13 prd = complex_mul ( prd , array [ i ] ) ; 14 15 } 16 17 18 19 } Duran et al. () UDRs in OpenMP June 16th 2010 5 / 28

  7. Motivation Reductions by hand 1 complex_t complex_mul ( complex_t a , complex_t b ) ; 2 3 void example ( complex_t ∗ array , size_t N) { complex_t prd = {1.0 , 0 . 0 } ; 4 5 int nthreads = omp_get_max_threads ( ) ; 6 complex_t part_prd [ nthreads ] ; 7 8 #pragma omp parallel shared ( part_prd ) private ( prd ) 9 { 10 prd = {1.0 , 0 . 0 } ; 11 #pragma omp for 12 ( size_t i = 0; i < N; i ++) for 13 prd = complex_mul ( prd , array [ i ] ) ; 14 part_prd [ omp_get_thread_num ( ) ] = prd ; 15 } 16 17 for ( int t h r = 0; t h r < nthreads ; t h r ++) 18 prd = complex_mul ( prd , part_prd [ t h r ] ) ; 19 20 21 } Duran et al. () UDRs in OpenMP June 16th 2010 5 / 28

  8. Motivation Not good Drawbacks More complex user code Error prone Doesn’t benefit from implementation improvements Duran et al. () UDRs in OpenMP June 16th 2010 6 / 28

  9. Motivation Not good Drawbacks More complex user code Error prone Doesn’t benefit from implementation improvements Solution Add user-defined reductions to OpenMP Duran et al. () UDRs in OpenMP June 16th 2010 6 / 28

  10. Motivation User-defined reductions Allow the user to inform OpenMP about new reductions by providing: A type An operation over that type The identity value for that operation and type Duran et al. () UDRs in OpenMP June 16th 2010 7 / 28

  11. Motivation User-defined reductions Allow the user to inform OpenMP about new reductions by providing: A type An operation over that type The identity value for that operation and type Presented to the OpenMP language committee (still on discussion) Duran et al. () UDRs in OpenMP June 16th 2010 7 / 28

  12. UDR Design rationale Outline Motivation 1 UDR Design rationale 2 Declaring UDRs 3 Array reductions 4 C++ specific extensions 5 Conclusions 6 Duran et al. () UDRs in OpenMP June 16th 2010 8 / 28

  13. UDR Design rationale Driving design goals follow OpenMP directive-based philosophy support all OpenMP base languages but mantain a common syntax as much as possible follow a declaration/usage pattern Allow code re-use Allow efficient implementation require associativity and commutativity Duran et al. () UDRs in OpenMP June 16th 2010 9 / 28

  14. Declaring UDRs Outline Motivation 1 UDR Design rationale 2 Declaring UDRs 3 Array reductions 4 C++ specific extensions 5 Conclusions 6 Duran et al. () UDRs in OpenMP June 16th 2010 10 / 28

  15. Declaring UDRs Declaring an UDR Syntax C: #pragma omp declare reduction ( operator − l i s t : type ) [ clause ] 1 C++: 1 #pragma omp declare reduction ( [ template <template − params >] operator − l i s t : type − l i s t ) [ clause ] Fortran: ! $omp declare reduction ( operator − l i s t : typename ) [ clause ] 1 where clause is: brace − i n i t i a l i z e r C / C ++ ) ] C ++ 1 identity ( expression | | constructor [ ( argument − l i s t ) Duran et al. () UDRs in OpenMP June 16th 2010 11 / 28

  16. Declaring UDRs Example complex_t complex_mul ( complex_t a , complex_t b ) ; 1 2 3 #pragma omp declare reduction ( complex_mul : complex_t ) \ identity ( { 1 . 0 , 0 . 0 } ) 4 5 6 void example ( complex_t ∗ array , size_t N) { complex_t prd = {1.0 , 0 . 0 } ; 7 8 #pragma omp parallel for reduction ( complex_mul : prd ) 9 for ( size_t i = 0; i < N; i ++) 10 prd = complex_mul ( prd , array [ i ] ) ; 11 12 } 13 Duran et al. () UDRs in OpenMP June 16th 2010 12 / 28

  17. Declaring UDRs Operator requisites binary operators with compatible arguments with UDR type Return value either by return or parameter (*,&) Return value priority: function return, left parameter, right parameter Allow const and references (C++) Unary member functions specified by prepending a dot to the operator name (C++) valid overloaded operators associative commutative available using base language “symbol“ lookup both at declaration and usage Duran et al. () UDRs in OpenMP June 16th 2010 13 / 28

  18. Declaring UDRs Identity value By default: C/Fortran Zero initialization C++ C++ rules for value-initialization Can be overrided by the identity clause The identity expression must evaluate always to the same value An implementation can evaluate it one or more times Duran et al. () UDRs in OpenMP June 16th 2010 14 / 28

  19. Array reductions Outline Motivation 1 UDR Design rationale 2 Declaring UDRs 3 Array reductions 4 C++ specific extensions 5 Conclusions 6 Duran et al. () UDRs in OpenMP June 16th 2010 15 / 28

  20. Array reductions Array UDRs Declaration Types can be prepended with [] that indicate that is going to be an array UDR One [] per dimension Dimension size is not fix at declaration Operators can have additional integer parameters to get the actual size Usage Array UDRs can be applied to variables of: Array types Pointer to array types allows to ”recover” arrays through function calls Support for VLA (pointers to) arrays is limited to C. Duran et al. () UDRs in OpenMP June 16th 2010 16 / 28

  21. Array reductions Example 1 const int N = 10; 2 void vector_add ( int ∗ A, int ∗ B, int n ) ; 3 4 #pragma omp declare reduction ( vector_add : int [ ] ) 5 6 void foo ( int ∗ a , int ∗ b , int n ) { 7 int v1 [N ] ; 8 int ( ∗ v2 ) [N] = ( int ( ∗ ) [N] ) a ; 9 ( ∗ v3 ) [ n ] = ( int ( ∗ ) [ n ] ) b ; / / VLA; Only v a l i d in C int 10 11 #pragma omp for reduction ( vector_add : v1 , v2 , v3 ) 12 for ( . . . ) { . . . } 13 } 14 Duran et al. () UDRs in OpenMP June 16th 2010 17 / 28

  22. C++ specific extensions Outline Motivation 1 UDR Design rationale 2 Declaring UDRs 3 Array reductions 4 C++ specific extensions 5 Conclusions 6 Duran et al. () UDRs in OpenMP June 16th 2010 18 / 28

  23. C++ specific extensions Constructed indentities The special constructor keyword can be used in the identity clause Private copies will be initialized with a constructor instead of by-assignment Duran et al. () UDRs in OpenMP June 16th 2010 19 / 28

  24. C++ specific extensions Constructed indentities The special constructor keyword can be used in the identity clause Private copies will be initialized with a constructor instead of by-assignment #pragma omp declare reduction ( ∗ : Complex ) identity ( constructor ( 1 . 0 , 0 . 0 1 Duran et al. () UDRs in OpenMP June 16th 2010 19 / 28

  25. C++ specific extensions Inheritance support Idea Support the C++ philosophy of allowing to use methods defined over base classes with derived classes Duran et al. () UDRs in OpenMP June 16th 2010 20 / 28

  26. C++ specific extensions Inheritance support Idea Support the C++ philosophy of allowing to use methods defined over base classes with derived classes UDR of base classes can be used for derived classes Only if it does not create object slicing operator parameters must be pointers or references Duran et al. () UDRs in OpenMP June 16th 2010 20 / 28

  27. C++ specific extensions Template UDRs Allows to declare an UDR on all (or partial) instantiations of template type Duran et al. () UDRs in OpenMP June 16th 2010 21 / 28

  28. C++ specific extensions Template UDRs Allows to declare an UDR on all (or partial) instantiations of template type 1 #pragma omp declare \ reduction ( template < class T> std : : l i s t <T > : : merge : std : : l i s t <T> ) 2 3 4 void foo ( ) { 5 std : : l i s t < int > l i ; 6 std : : l i s t < float > l f ; 7 8 #pragma omp parallel for reduction ( std : : l i s t < int > : : merge : l i ) 9 reduction ( std : : l i s t < float > : : merge : l f ) 10 for ( . . . ) { . . . } 11 } 12 Duran et al. () UDRs in OpenMP June 16th 2010 21 / 28

Recommend


More recommend