changeset 13245:027a2186cd90

parfor keyword and infrastructure, but handle parfor as normal for loop for now * octave.gperf (octave_kw_id): New keyword ids, parfor_kw and end_parfor_kw. (octave_kw): Add parfor and end_parfor to the struct. * lex.ll (is_keyword_token): Handle parfor and end_parfor. * token.h (token::parfor_end): New end_tok_type enum value. * oct-parse.yy (PARFOR): New token. (loop_command): Handle PARFOR statements. (make_for_command): New args tok_id and maxproc. Handle PARFOR loops. * pt-loop.h (tree_simple_for_command::parallel, tree_simple_for_command:maxproc): New data members. (tree_simple_for_command::tree_simple_for_command): New args parallel_arg and maxproc_arg. Initialize new data members. (tree_simple_for_command::parallel): New function. (tree_simple_for_command::maxproc_expr): New function. * pt-loop.cc (tree_simple_for_command::~tree_simple_for_command): Delete maxproc. (tree_simple_for_command::dup): Pass parallel and maxproc to constructor for duplicate object. * pt-pr-code.cc (tree_print_code::visit_simple_for_command): Handle parallel form. * pt-check.cc (tree_checker::visit_simple_for_command): Likewise. * pt-eval.cc (tree_evaluator::visit_simple_for_command): Note that this is where parallel loops need to be handled.
author John W. Eaton <jwe@octave.org>
date Thu, 29 Sep 2011 02:50:53 -0400
parents f9aec0bcf826
children 0c69a564f2be
files src/lex.ll src/oct-parse.yy src/octave.gperf src/pt-check.cc src/pt-eval.cc src/pt-loop.cc src/pt-loop.h src/pt-pr-code.cc src/token.h
diffstat 9 files changed, 97 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/src/lex.ll	Wed Sep 28 21:46:39 2011 -0400
+++ b/src/lex.ll	Thu Sep 29 02:50:53 2011 -0400
@@ -1496,6 +1496,11 @@
           lexer_flags.at_beginning_of_statement = true;
           break;
 
+        case end_parfor_kw:
+          yylval.tok_val = new token (token::parfor_end, l, c);
+          lexer_flags.at_beginning_of_statement = true;
+          break;
+
         case end_try_catch_kw:
           yylval.tok_val = new token (token::try_catch_end, l, c);
           lexer_flags.at_beginning_of_statement = true;
@@ -1551,7 +1556,9 @@
           lexer_flags.at_beginning_of_statement = true;
           break;
 
+
         case for_kw:
+        case parfor_kw:
         case while_kw:
           promptflag--;
           lexer_flags.looping++;
--- a/src/oct-parse.yy	Wed Sep 28 21:46:39 2011 -0400
+++ b/src/oct-parse.yy	Thu Sep 29 02:50:53 2011 -0400
@@ -232,9 +232,10 @@
 
 // Build a for command.
 static tree_command *
-make_for_command (token *for_tok, tree_argument_list *lhs,
-                  tree_expression *expr, tree_statement_list *body,
-                  token *end_tok, octave_comment_list *lc);
+make_for_command (int tok_id, token *for_tok, tree_argument_list *lhs,
+                  tree_expression *expr, tree_expression *maxproc,
+                  tree_statement_list *body, token *end_tok,
+                  octave_comment_list *lc);
 
 // Build a break command.
 static tree_command *
@@ -439,7 +440,7 @@
 %token <tok_val> NAME
 %token <tok_val> END
 %token <tok_val> DQ_STRING SQ_STRING
-%token <tok_val> FOR WHILE DO UNTIL
+%token <tok_val> FOR PARFOR WHILE DO UNTIL
 %token <tok_val> IF ELSEIF ELSE
 %token <tok_val> SWITCH CASE OTHERWISE
 %token <tok_val> BREAK CONTINUE FUNC_RET
@@ -1143,12 +1144,26 @@
                   }
                 | FOR stash_comment assign_lhs '=' expression opt_sep opt_list END
                   {
-                    if (! ($$ = make_for_command ($1, $3, $5, $7, $8, $2)))
+                    if (! ($$ = make_for_command (FOR, $1, $3, $5, 0,
+                                                  $7, $8, $2)))
                       ABORT_PARSE;
                   }
                 | FOR stash_comment '(' assign_lhs '=' expression ')' opt_sep opt_list END
                   {
-                    if (! ($$ = make_for_command ($1, $4, $6, $9, $10, $2)))
+                    if (! ($$ = make_for_command (FOR, $1, $4, $6, 0,
+                                                  $9, $10, $2)))
+                      ABORT_PARSE;
+                  }
+                | PARFOR stash_comment assign_lhs '=' expression opt_sep opt_list END
+                  {
+                    if (! ($$ = make_for_command (PARFOR, $1, $3, $5,
+                                                  0, $7, $8, $2)))
+                      ABORT_PARSE;
+                  }
+                | PARFOR stash_comment '(' assign_lhs '=' expression ',' expression ')' opt_sep opt_list END
+                  {
+                    if (! ($$ = make_for_command (PARFOR, $1, $4, $6,
+                                                  $8, $11, $12, $2)))
                       ABORT_PARSE;
                   }
                 ;
@@ -2480,13 +2495,16 @@
 // Build a for command.
 
 static tree_command *
-make_for_command (token *for_tok, tree_argument_list *lhs,
-                  tree_expression *expr, tree_statement_list *body,
-                  token *end_tok, octave_comment_list *lc)
+make_for_command (int tok_id, token *for_tok, tree_argument_list *lhs,
+                  tree_expression *expr, tree_expression *maxproc,
+                  tree_statement_list *body, token *end_tok,
+                  octave_comment_list *lc)
 {
   tree_command *retval = 0;
 
-  if (end_token_ok (end_tok, token::for_end))
+  bool parfor = tok_id == PARFOR;
+
+  if (end_token_ok (end_tok, parfor ? token::parfor_end : token::for_end))
     {
       octave_comment_list *tc = octave_comment_buffer::get_comment ();
 
@@ -2499,14 +2517,19 @@
         {
           tree_expression *tmp = lhs->remove_front ();
 
-          retval = new tree_simple_for_command (tmp, expr, body,
-                                                lc, tc, l, c);
+          retval = new tree_simple_for_command (parfor, tmp, expr, maxproc,
+                                                body, lc, tc, l, c);
 
           delete lhs;
         }
       else
-        retval = new tree_complex_for_command (lhs, expr, body,
-                                               lc, tc, l, c);
+        {
+          if (parfor)
+            yyerror ("invalid syntax for parfor statement");
+          else
+            retval = new tree_complex_for_command (lhs, expr, body,
+                                                   lc, tc, l, c);
+        }
     }
 
   return retval;
--- a/src/octave.gperf	Wed Sep 28 21:46:39 2011 -0400
+++ b/src/octave.gperf	Thu Sep 29 02:50:53 2011 -0400
@@ -35,6 +35,7 @@
   else_kw,
   elseif_kw,
   end_kw,
+  end_parfor_kw,
   end_try_catch_kw,
   end_unwind_protect_kw,
   endclassdef_kw,
@@ -56,6 +57,7 @@
   magic_line_kw,
   methods_kw,
   otherwise_kw,
+  parfor_kw,
   properties_kw,
   return_kw,
   set_kw,
@@ -79,6 +81,7 @@
 else, ELSE, else_kw
 elseif, ELSEIF, elseif_kw
 end, END, end_kw
+end_parfor, END, end_parfor_kw
 end_try_catch, END, end_try_catch_kw
 end_unwind_protect, END, end_unwind_protect_kw
 endclassdef, END, endclassdef_kw 
@@ -98,6 +101,7 @@
 if, IF, if_kw
 methods, METHODS, methods_kw
 otherwise, OTHERWISE, otherwise_kw
+parfor, PARFOR, parfor_kw
 persistent, STATIC, static_kw
 properties, PROPERTIES, properties_kw
 return, FUNC_RET, return_kw
--- a/src/pt-check.cc	Wed Sep 28 21:46:39 2011 -0400
+++ b/src/pt-check.cc	Thu Sep 29 02:50:53 2011 -0400
@@ -154,6 +154,11 @@
   if (expr)
     expr->accept (*this);
 
+  tree_expression *maxproc = cmd.maxproc_expr ();
+
+  if (maxproc)
+    maxproc->accept (*this);
+
   tree_statement_list *list = cmd.body ();
 
   if (list)
--- a/src/pt-eval.cc	Wed Sep 28 21:46:39 2011 -0400
+++ b/src/pt-eval.cc	Thu Sep 29 02:50:53 2011 -0400
@@ -290,6 +290,9 @@
   if (debug_mode)
     do_breakpoint (cmd.is_breakpoint ());
 
+  // FIXME -- need to handle PARFOR loops here using cmd.in_parallel ()
+  // and cmd.maxproc_expr ();
+
   unwind_protect frame;
 
   frame.protect_var (in_loop_command);
--- a/src/pt-loop.cc	Wed Sep 28 21:46:39 2011 -0400
+++ b/src/pt-loop.cc	Thu Sep 29 02:50:53 2011 -0400
@@ -92,6 +92,7 @@
 tree_simple_for_command::~tree_simple_for_command (void)
 {
   delete expr;
+  delete maxproc;
   delete list;
   delete lead_comm;
   delete trail_comm;
@@ -101,12 +102,13 @@
 tree_simple_for_command::dup (symbol_table::scope_id scope,
                               symbol_table::context_id context) const
 {
-  return new tree_simple_for_command (lhs ? lhs->dup (scope, context) : 0,
-                                      expr ? expr->dup (scope, context) : 0,
-                                      list ? list->dup (scope, context) : 0,
-                                      lead_comm ? lead_comm->dup () : 0,
-                                      trail_comm ? trail_comm->dup () : 0,
-                                      line (), column ());
+  return new tree_simple_for_command
+    (parallel, lhs ? lhs->dup (scope, context) : 0,
+     expr ? expr->dup (scope, context) : 0,
+     maxproc ? maxproc->dup (scope, context) : 0,
+     list ? list->dup (scope, context) : 0,
+     lead_comm ? lead_comm->dup () : 0,
+     trail_comm ? trail_comm->dup () : 0, line (), column ());
 }
 
 void
--- a/src/pt-loop.h	Wed Sep 28 21:46:39 2011 -0400
+++ b/src/pt-loop.h	Thu Sep 29 02:50:53 2011 -0400
@@ -145,23 +145,30 @@
 public:
 
   tree_simple_for_command (int l = -1, int c = -1)
-    : tree_command (l, c), lhs (0), expr (0), list (0), lead_comm (0),
-      trail_comm (0) { }
+    : tree_command (l, c), parallel (false), lhs (0), expr (0),
+      maxproc (0), list (0), lead_comm (0), trail_comm (0) { }
 
-  tree_simple_for_command (tree_expression *le, tree_expression *re,
+  tree_simple_for_command (bool parallel_arg, tree_expression *le,
+                           tree_expression *re,
+                           tree_expression *maxproc_arg,
                            tree_statement_list *lst,
                            octave_comment_list *lc = 0,
                            octave_comment_list *tc = 0,
                            int l = -1, int c = -1)
-    : tree_command (l, c), lhs (le), expr (re), list (lst),
+    : tree_command (l, c), parallel (parallel_arg), lhs (le),
+      expr (re), maxproc (maxproc_arg), list (lst),
       lead_comm (lc), trail_comm (tc) { }
 
   ~tree_simple_for_command (void);
 
+  bool in_parallel (void) { return parallel; }
+
   tree_expression *left_hand_side (void) { return lhs; }
 
   tree_expression *control_expr (void) { return expr; }
 
+  tree_expression *maxproc_expr (void) { return maxproc; }
+
   tree_statement_list *body (void) { return list; }
 
   octave_comment_list *leading_comment (void) { return lead_comm; }
@@ -175,12 +182,20 @@
 
 private:
 
+  // TRUE means operate in parallel (subject to the value of the
+  // maxproc expression).
+  bool parallel;
+
   // Expression to modify.
   tree_expression *lhs;
 
   // Expression to evaluate.
   tree_expression *expr;
 
+  // Expression to tell how many processors should be used (only valid
+  // if parallel is TRUE).
+  tree_expression *maxproc;
+
   // List of commands to execute.
   tree_statement_list *list;
 
--- a/src/pt-pr-code.cc	Wed Sep 28 21:46:39 2011 -0400
+++ b/src/pt-pr-code.cc	Thu Sep 29 02:50:53 2011 -0400
@@ -214,10 +214,15 @@
 
   indent ();
 
-  os << "for ";
+  os << (cmd.in_parallel () ? "parfor " : "for ");
 
   tree_expression *lhs = cmd.left_hand_side ();
 
+  tree_expression *maxproc = cmd.maxproc_expr ();
+
+  if (maxproc)
+    os << "(";
+
   if (lhs)
     lhs->accept (*this);
 
@@ -228,6 +233,13 @@
   if (expr)
     expr->accept (*this);
 
+  if (maxproc)
+    {
+      os << ", ";
+      maxproc->accept (*this);
+      os << ")";
+    }
+
   newline ();
 
   tree_statement_list *list = cmd.body ();
--- a/src/token.h	Wed Sep 28 21:46:39 2011 -0400
+++ b/src/token.h	Thu Sep 29 02:50:53 2011 -0400
@@ -50,6 +50,7 @@
       function_end,
       if_end,
       methods_end,
+      parfor_end,
       properties_end,
       switch_end,
       while_end,