From 9a0e799226e459bd1eba25aff25ab0a5242e62ad Mon Sep 17 00:00:00 2001 From: Uday Bondhugula Date: Wed, 23 May 2012 16:27:37 +0530 Subject: [PATCH] clast-based loop parallelization support This patch provides support for printing OMP parallel loops if marked appropriately. clast_for->parallel can be set to CLAST_PARALLEL_OMP, CLAST_PARALLEL_MPI, or CLAST_PARALLEL_OMP + CLAST_PARALLEL_MPI. The loop will be annotated with omp parallel pragmas and/or distributed via MPI; list of private and reduction variables for omp parallelization can be set as well. Signed-off-by: Uday Bondhugula Signed-off-by: Cedric Bastoul --- include/cloog/clast.h | 9 ++++++++ source/clast.c | 5 ++++ source/pprint.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/include/cloog/clast.h b/include/cloog/clast.h index b455369..675c5ff 100644 --- a/include/cloog/clast.h +++ b/include/cloog/clast.h @@ -31,6 +31,10 @@ struct clast_term { struct clast_expr *var; }; +#define CLAST_PARALLEL_NOT 0 +#define CLAST_PARALLEL_OMP 1 +#define CLAST_PARALLEL_MPI 2 + enum clast_red_type { clast_red_sum, clast_red_min, clast_red_max }; struct clast_reduction { struct clast_expr expr; @@ -98,6 +102,11 @@ struct clast_for { struct clast_expr * UB; cloog_int_t stride; struct clast_stmt * body; + int parallel; + /* Comma separated list of loop private variables for OpenMP parallelization */ + char *private_vars; + /* Comma separated list of reduction variable/operators for OpenMP parallelization */ + char *reduction_vars; }; struct clast_equation { diff --git a/source/clast.c b/source/clast.c index 0b67532..7cd1e45 100644 --- a/source/clast.c +++ b/source/clast.c @@ -211,6 +211,8 @@ static void free_clast_for(struct clast_stmt *s) free_clast_expr(f->UB); cloog_int_clear(f->stride); cloog_clast_free(f->body); + if (f->private_vars) free(f->private_vars); + if (f->reduction_vars) free(f->reduction_vars); free(f); } @@ -226,6 +228,9 @@ struct clast_for *new_clast_for(CloogDomain *domain, const char *it, f->LB = LB; f->UB = UB; f->body = NULL; + f->parallel = CLAST_PARALLEL_NOT; + f->private_vars = NULL; + f->reduction_vars = NULL; cloog_int_init(f->stride); if (stride) cloog_int_set(f->stride, stride->stride); diff --git a/source/pprint.c b/source/pprint.c index 9c7f1d4..9ada11b 100644 --- a/source/pprint.c +++ b/source/pprint.c @@ -405,6 +405,56 @@ void pprint_guard(struct cloogoptions *options, FILE *dst, int indent, void pprint_for(struct cloogoptions *options, FILE *dst, int indent, struct clast_for *f) { + if (options->language == CLOOG_LANGUAGE_C) { + if ((f->parallel & CLAST_PARALLEL_OMP) && !(f->parallel & CLAST_PARALLEL_MPI)) { + if (f->LB) { + fprintf(dst, "lbp="); + pprint_expr(options, dst, f->LB); + fprintf(dst, ";\n"); + } + if (f->UB) { + fprintf(dst, "%*s", indent, ""); + fprintf(dst, "ubp="); + pprint_expr(options, dst, f->UB); + fprintf(dst, ";\n"); + } + fprintf(dst, "#pragma omp parallel for%s%s%s%s%s%s\n", + (f->private_vars)? " private(":"", + (f->private_vars)? f->private_vars: "", + (f->private_vars)? ")":"", + (f->reduction_vars)? " reduction(": "", + (f->reduction_vars)? f->reduction_vars: "", + (f->reduction_vars)? ")": ""); + fprintf(dst, "%*s", indent, ""); + } + if (f->parallel & CLAST_PARALLEL_MPI) { + if (f->LB) { + fprintf(dst, "_lb_dist="); + pprint_expr(options, dst, f->LB); + fprintf(dst, ";\n"); + } + if (f->UB) { + fprintf(dst, "%*s", indent, ""); + fprintf(dst, "_ub_dist="); + pprint_expr(options, dst, f->UB); + fprintf(dst, ";\n"); + } + fprintf(dst, "%*s", indent, ""); + fprintf(dst, "polyrt_loop_dist(_lb_dist, _ub_dist, nprocs, my_rank, &lbp, &ubp);\n"); + if (f->parallel & CLAST_PARALLEL_OMP) { + fprintf(dst, "#pragma omp parallel for%s%s%s%s%s%s\n", + (f->private_vars)? " private(":"", + (f->private_vars)? f->private_vars: "", + (f->private_vars)? ")":"", + (f->reduction_vars)? " reduction(": "", + (f->reduction_vars)? f->reduction_vars: "", + (f->reduction_vars)? ")": ""); + } + fprintf(dst, "%*s", indent, ""); + } + + } + if (options->language == CLOOG_LANGUAGE_FORTRAN) fprintf(dst, "DO "); else @@ -412,7 +462,11 @@ void pprint_for(struct cloogoptions *options, FILE *dst, int indent, if (f->LB) { fprintf(dst, "%s=", f->iterator); + if (f->parallel & (CLAST_PARALLEL_OMP | CLAST_PARALLEL_MPI)) { + fprintf(dst, "lbp"); + }else{ pprint_expr(options, dst, f->LB); + } } else if (options->language == CLOOG_LANGUAGE_FORTRAN) cloog_die("unbounded loops not allowed in FORTRAN.\n"); @@ -424,8 +478,13 @@ void pprint_for(struct cloogoptions *options, FILE *dst, int indent, if (f->UB) { if (options->language != CLOOG_LANGUAGE_FORTRAN) fprintf(dst,"%s<=", f->iterator); - pprint_expr(options, dst, f->UB); - } else if (options->language == CLOOG_LANGUAGE_FORTRAN) + + if (f->parallel & (CLAST_PARALLEL_OMP | CLAST_PARALLEL_MPI)) { + fprintf(dst, "ubp"); + }else{ + pprint_expr(options, dst, f->UB); + } + }else if (options->language == CLOOG_LANGUAGE_FORTRAN) cloog_die("unbounded loops not allowed in FORTRAN.\n"); if (options->language == CLOOG_LANGUAGE_FORTRAN) { -- 2.11.4.GIT