Boost.ProtoBoost.     #4 2011/02/26         /@fjnli id:fjnl1
Twitter       (   ) ...          2
http://atnd.org/events/11551           3
http://atnd.org/events/11551Proto           3
http://atnd.org/events/11551    ProtoLambda, Spirit, uBLAS    Proto               3
Boost.Proto4
Boost.    #4         Proto       (   )            Proto                 5
Agenda•• Boost.Proto••        +                  6
Agenda•• Boost.Proto••        +                  6
Agenda•                         15• Boost.Proto                         5••        +                  6
Agenda• Boost.Proto  • Boost.Proto  • Expression Template (ET)  • Boost.Proto                               7
Agenda•    •             Boost.Proto    • Boost.uBLAS    • General Purpose computing on GPU (GPGPU)    • ET →             ...
•                  (         )• @fjnli, id:fjnl•                    →           (   )• Ariel Networks•            : GPU   ...
•                  (           )• @fjnli, id:fjnl•                    →                      Hatena ID    i               ...
•                  (           )• @fjnli, id:fjnl•                    →                      Hatena ID                    ...
•                  (          )• @fjnli, id:fjnl•                    →            (   )• Ariel Networks•            : GPU ...
.begin(); 11
Proto• Expression Template (ET)  • a + b * c → plus<A, multiply<B, C>>• Boost 1.37• Headers only                          ...
Proto• Boost.Proto• Boost Boost.Proto               :  • Boost.Spirit (Qi, Karma, Lex)  • Boost.Phoenix v3 (in the future)...
ET•    •        •                     ´   ω    • Qi    Proto                         14
usingnamespace proto = boost::proto;using namespace boost::proto;using boost::proto::argsns_::list2,using boost::proto::ex...
a[b] + (c * d) (Syntax Tree)   :        16
a[b] + (c * d)       (Syntax Tree)                 :               (+)    ([])                   (*)a          b          ...
a[b] + (c * d)expr<tag::plus,  list2<    expr<tag::subscript,       list2<,         expr<tag::terminal,   term<int>, 0>&, ...
a[b] + (c * d)expr<tag::plus,  list2<    expr<tag::subscript, expr                 exprns_::       list2<,         expr<ta...
a[b] + (c * d)expr<tag::plus,  list2<                                   plus → +    expr<tag::subscript,                  ...
a[b] + (c * d)expr<tag::plus,  list2<    expr<tag::subscript,       list2<,          argsns_::listN         expr<tag::term...
a[b] + (c * d)expr<tag::plus,  list2<    expr<tag::subscript,                 exprns_::term       list2<,         expr<tag...
a[b] + (c * d)expr<tag::plus,  list2<    expr<tag::subscript,       list2<,         expr<tag::terminal,   term<int>, 0>&, ...
:default_context const ctx;std::cout << eval(lit(1) + 2 + 3, ctx)          << std::endl;        :6                   23
:default_context const ctx;std::cout << eval(lit(1) + 2 + 3, ctx)          << std::endl;                   24
default_context → C++null_context →                    25
+    -•x + y        x-y •                       int   +     •→                    26
+   -struct my_context {   template <     class Expr,     class Enable = void    > struct eval;};evalmy_context::eval<Expr...
+   -struct my_context {   template <     class Expr,     class Enable = void    > struct eval;                           ...
SFINAE• SFINAE (            ?)  • Substitution Failure Is Not An Error  •• boost::enable_if  •template <class T>   typenam...
+       -template <class Expr>struct eval<  Expr,  typename boost::enable_if<    matches<Expr, terminal<int>>  >::type> { ...
+       -template <class Expr>struct eval<  Expr,  typename boost::enable_if<    matches<Expr, terminal<int>>  >::type> { ...
+       -template <class Expr>struct eval<  Expr,  typename boost::enable_if<    matches<Expr, terminal<int>>  >::type> { ...
+       -template <class Expr>struct eval<  Expr,  typename boost::enable_if<    matches<Expr, terminal<int>>  >::type> { ...
+       -template <class Expr>struct eval<  Expr,  typename boost::enable_if<    matches<Expr, terminal<int>>  >::type> { ...
+       -template <class Expr>struct eval<  Expr,  typename boost::enable_if<    matches<Expr, plus<_, _>>  >::type> {  ty...
+       -template <class Expr>struct eval<  Expr,  typename boost::enable_if<    matches<Expr, plus<_, _>>  >::type> {    ...
+       -template <class Expr>struct eval<  Expr,  typename boost::enable_if<    matches<Expr, plus<_, _>>  >::type> {  ty...
+       -template <class Expr>struct eval<  Expr,  typename boost::enable_if<    matches<Expr, plus<_, _>>  >::type> {  ty...
+       -template <class Expr>struct eval<  Expr,  typename boost::enable_if<    matches<Expr, plus<_, _>>  >::type> {  ty...
2     :my_context const ctx;std::cout << eval(lit(1) + 2 + 3, ctx)          << std::endl;         :-4                   39
/•                :    • proto::left, proto::right•                                 :    • proto::result_of::left, proto::...
• Context  •• Domain  •• Extends  •• Grammer  •            41
:1.User’s Guide     http://www.boost.org/doc/libs/1_46_0/doc/html/proto/     users_guide.html2.              (C++ Advent C...
.end();.begin(); 43
Proto• ET  •    •  •              (+               )•(                         ) • Basic Linear Algebra Subprograms (BLAS)...
Boost.uBLAS• Boost.uBLAS• Boost.uBLAS• Boost.uBLAS                45
Boost.uBLAS•                  CPU, GPU    •                   →•                CPU   GPU    •    :        •        • CPU:...
ET →        Backend::scalar a;        Backend::vector b, x, y;        y = a * x + b;Backend::eval<  multiply, scalar, vect...
(Cell, GPU    )               [a * x + y]plus<multiply<scalar, vector>, vector>axpy<scalar, vector, vector>               ...
49
Upcoming SlideShare
Loading in...5
×

Boost.勉強会#4 Boost.Proto

3,136

Published on

http://atnd.org/events/11551

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,136
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
17
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Boost.勉強会#4 Boost.Proto"

  1. 1. Boost.ProtoBoost. #4 2011/02/26 /@fjnli id:fjnl1
  2. 2. Twitter ( ) ... 2
  3. 3. http://atnd.org/events/11551 3
  4. 4. http://atnd.org/events/11551Proto 3
  5. 5. http://atnd.org/events/11551 ProtoLambda, Spirit, uBLAS Proto 3
  6. 6. Boost.Proto4
  7. 7. Boost. #4 Proto ( ) Proto 5
  8. 8. Agenda•• Boost.Proto•• + 6
  9. 9. Agenda•• Boost.Proto•• + 6
  10. 10. Agenda• 15• Boost.Proto 5•• + 6
  11. 11. Agenda• Boost.Proto • Boost.Proto • Expression Template (ET) • Boost.Proto 7
  12. 12. Agenda• • Boost.Proto • Boost.uBLAS • General Purpose computing on GPU (GPGPU) • ET → 8
  13. 13. • ( )• @fjnli, id:fjnl• → ( )• Ariel Networks• : GPU 9
  14. 14. • ( )• @fjnli, id:fjnl• → Hatena ID i ( )• Ariel Networks• : GPU 9
  15. 15. • ( )• @fjnli, id:fjnl• → Hatena ID ※ i ( )• Ariel Networks• : GPU 9
  16. 16. • ( )• @fjnli, id:fjnl• → ( )• Ariel Networks• : GPU 10
  17. 17. .begin(); 11
  18. 18. Proto• Expression Template (ET) • a + b * c → plus<A, multiply<B, C>>• Boost 1.37• Headers only 12
  19. 19. Proto• Boost.Proto• Boost Boost.Proto : • Boost.Spirit (Qi, Karma, Lex) • Boost.Phoenix v3 (in the future) • Boost.MSM.eUML (experimental, 1.44) • Boost.Xpressive 13
  20. 20. ET• • • ´ ω • Qi Proto 14
  21. 21. usingnamespace proto = boost::proto;using namespace boost::proto;using boost::proto::argsns_::list2,using boost::proto::exprns_::expr;using boost::proto::exprns_::term; 15
  22. 22. a[b] + (c * d) (Syntax Tree) : 16
  23. 23. a[b] + (c * d) (Syntax Tree) : (+) ([]) (*)a b c d 16
  24. 24. a[b] + (c * d)expr<tag::plus, list2< expr<tag::subscript, list2<, expr<tag::terminal, term<int>, 0>&, expr<tag::terminal, term<int>, 0>& >, 2 > const&, expr<tag::multiplies, list2< expr<tag::terminal, term<int> 0>&, expr<tag::terminal, term<int> 0>& >, 2 > const& >, 2> const 17
  25. 25. a[b] + (c * d)expr<tag::plus, list2< expr<tag::subscript, expr exprns_:: list2<, expr<tag::terminal, term<int>, 0>&, expr<tag::terminal, term<int>, 0>& >, 2 > const&, expr<tag::multiplies, list2< expr<tag::terminal, term<int> 0>&, expr<tag::terminal, term<int> 0>& >, 2 > const& >, 2> const 18
  26. 26. a[b] + (c * d)expr<tag::plus, list2< plus → + expr<tag::subscript, multiplies → * list2<, expr<tag::terminal, subscript → [] term<int>, 0>&, expr<tag::terminal, term<int>, 0>& … >, 2 > const&, expr<tag::multiplies, list2< expr<tag::terminal, term<int> 0>&, expr<tag::terminal, term<int> 0>& >, 2 > const& >, 2> const 19
  27. 27. a[b] + (c * d)expr<tag::plus, list2< expr<tag::subscript, list2<, argsns_::listN expr<tag::terminal, term<int>, 0>&, expr<tag::terminal, term<int>, 0>& >, 2 > const&, expr<tag::multiplies, list2< expr<tag::terminal, term<int> 0>&, expr<tag::terminal, term<int> 0>& >, 2 > const& >, 2> const 20
  28. 28. a[b] + (c * d)expr<tag::plus, list2< expr<tag::subscript, exprns_::term list2<, expr<tag::terminal, term<int>, 0>&, expr<tag::terminal, term<int>, 0>& >, 2 > const&, expr<tag::multiplies, list2< expr<tag::terminal, term<int> 0>&, expr<tag::terminal, term<int> 0>& >, 2 > const& >, 2> const 21
  29. 29. a[b] + (c * d)expr<tag::plus, list2< expr<tag::subscript, list2<, expr<tag::terminal, term<int>, 0>&, expr<tag::terminal, term<int>, 0>& >, 2 > const&, expr<tag::multiplies, list2< expr<tag::terminal, term<int> 0>&, expr<tag::terminal, term<int> 0>& >, 2 > const& >, 2> const 22
  30. 30. :default_context const ctx;std::cout << eval(lit(1) + 2 + 3, ctx) << std::endl; :6 23
  31. 31. :default_context const ctx;std::cout << eval(lit(1) + 2 + 3, ctx) << std::endl; 24
  32. 32. default_context → C++null_context → 25
  33. 33. + -•x + y x-y • int + •→ 26
  34. 34. + -struct my_context { template < class Expr, class Enable = void > struct eval;};evalmy_context::eval<Expr>()(expr, ctx) 27
  35. 35. + -struct my_context { template < class Expr, class Enable = void > struct eval; SFINAE};evalmy_context::eval<Expr>()(expr, ctx) 27
  36. 36. SFINAE• SFINAE ( ?) • Substitution Failure Is Not An Error •• boost::enable_if •template <class T> typename enable_if<is_const<T>>::type foo();• SFINAE eval 28
  37. 37. + -template <class Expr>struct eval< Expr, typename boost::enable_if< matches<Expr, terminal<int>> >::type> { typedef int result_type; result_type operator ()(Expr& e, my_context const&) const { return value(e); }} 29
  38. 38. + -template <class Expr>struct eval< Expr, typename boost::enable_if< matches<Expr, terminal<int>> >::type> { proto : typedef int result_type; Expr terminal<int> result_type true operator ()(Expr& e, my_context const&) const { return value(e); }} 30
  39. 39. + -template <class Expr>struct eval< Expr, typename boost::enable_if< matches<Expr, terminal<int>> >::type> { typedef int result_type; result_type operator ()(Expr& e, C++0x my_context const&) decltype const { return value(e); }} 31
  40. 40. + -template <class Expr>struct eval< Expr, typename boost::enable_if< matches<Expr, terminal<int>> >::type> { typedef int result_type; result_type operator ()(Expr& e, my_context const&) const { return value(e); }} 32
  41. 41. + -template <class Expr>struct eval< Expr, typename boost::enable_if< matches<Expr, terminal<int>> >::type> { typedef int result_type; result_type operator ()(Expr& e, my_context const&) const { return value(e); }} 33
  42. 42. + -template <class Expr>struct eval< Expr, typename boost::enable_if< matches<Expr, plus<_, _>> >::type> { typedef int result_type; result_type operator ()(Expr& e, my_context const& ctx) const { return proto::eval(left(e), ctx) - proto::eval(right(e), ctx); }} 34
  43. 43. + -template <class Expr>struct eval< Expr, typename boost::enable_if< matches<Expr, plus<_, _>> >::type> { _ typedef int result_type; (wildcard) result_type operator ()(Expr& e, my_context const& ctx) const { return proto::eval(left(e), ctx) - proto::eval(right(e), ctx); }} 35
  44. 44. + -template <class Expr>struct eval< Expr, typename boost::enable_if< matches<Expr, plus<_, _>> >::type> { typedef int result_type; left: 1 result_type right: 2 operator ()(Expr& e, my_context const& ctx) const { return proto::eval(left(e), ctx) - proto::eval(right(e), ctx); }} 36
  45. 45. + -template <class Expr>struct eval< Expr, typename boost::enable_if< matches<Expr, plus<_, _>> >::type> { typedef int result_type; result_type operator ()(Expr& e, my_context const& ctx) const { return proto::eval(left(e), ctx) - proto::eval(right(e), ctx); }} 37
  46. 46. + -template <class Expr>struct eval< Expr, typename boost::enable_if< matches<Expr, plus<_, _>> >::type> { typedef int result_type; result_type operator ()(Expr& e, my_context const& ctx) const { return proto::eval(left(e), ctx) - proto::eval(right(e), ctx); }} 38
  47. 47. 2 :my_context const ctx;std::cout << eval(lit(1) + 2 + 3, ctx) << std::endl; :-4 39
  48. 48. /• : • proto::left, proto::right• : • proto::result_of::left, proto::result_of::right • C++0x 40
  49. 49. • Context •• Domain •• Extends •• Grammer • 41
  50. 50. :1.User’s Guide http://www.boost.org/doc/libs/1_46_0/doc/html/proto/ users_guide.html2. (C++ Advent Calender JP 2010; 16 ) http://d.hatena.ne.jp/fjnl/20101222/ 42
  51. 51. .end();.begin(); 43
  52. 52. Proto• ET • • • (+ )•( ) • Basic Linear Algebra Subprograms (BLAS) 44
  53. 53. Boost.uBLAS• Boost.uBLAS• Boost.uBLAS• Boost.uBLAS 45
  54. 54. Boost.uBLAS• CPU, GPU • →• CPU GPU • : • • CPU: , ATLAS, GotoBLAS, OpenCL • GPU: , CUBLAS, OpenCL 46
  55. 55. ET → Backend::scalar a; Backend::vector b, x, y; y = a * x + b;Backend::eval< multiply, scalar, vector>()(y, a, x);Backend::eval< plus, vector, vector>()(y, y, b); 47
  56. 56. (Cell, GPU ) [a * x + y]plus<multiply<scalar, vector>, vector>axpy<scalar, vector, vector> 48
  57. 57. 49
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×