Search in sources :

Example 6 with Xblock

use of claw.tatsu.xcodeml.abstraction.Xblock in project claw-compiler by C2SM-RCM.

the class ScaGPU method applySpecificTransformation.

/**
 * Apply specific transformation steps for GPU target.
 *
 * @param xcodeml Current translation unit.
 * @throws IllegalTransformationException If any transformation fails.
 */
private void applySpecificTransformation(Configuration cfg, XcodeProgram xcodeml) throws IllegalTransformationException {
    final Context context = xcodeml.context();
    AcceleratorConfiguration config = cfg.accelerator();
    if (_fctDef.hasEmptyBody()) {
        // Nothing to do in this function
        return;
    }
    /*
         * Create a nested loop with the new defined dimensions and wrap it around the
         * whole subroutine's body. This is for the moment a really naive transformation
         * idea but it is our start point. Use the first over clause to create it.
         */
    NestedDoStatement loops;
    if (forceAssumedShapedArrayPromotion) {
        if (_promotions.isEmpty()) {
            throw new IllegalTransformationException("Cannot assume shape of " + "array in elemental function/subroutine.", _claw.getPragma().lineNo());
        }
        PromotionInfo pi = _promotions.entrySet().iterator().next().getValue();
        loops = new NestedDoStatement(_claw.getDefaultLayoutReversed(cfg), pi, xcodeml);
    } else {
        loops = new NestedDoStatement(_claw.getDefaultLayoutReversed(cfg), xcodeml);
    }
    /*
         * Subroutine/function can have a contains section with inner subroutines or
         * functions. The newly created (nested) do statements should stop before this
         * contains section if it exists.
         */
    Xnode contains = _fctDef.body().matchSeq(Xcode.F_CONTAINS_STATEMENT);
    if (contains != null) {
        Xnode parallelRegionStart;
        if (_specialParallelRegionStart == null) {
            parallelRegionStart = Directive.findParallelRegionStart(context, _fctDef, null);
        } else {
            parallelRegionStart = _specialParallelRegionStart;
        }
        Xnode parallelRegionEnd = Directive.findParallelRegionEnd(context, _fctDef, contains);
        Body.shiftIn(parallelRegionStart, parallelRegionEnd, loops.getInnerStatement().body(), true);
        contains.insertBefore(loops.getOuterStatement());
    } else {
        // No contains section, all the body is copied to the do statements.
        Xnode parallelRegionStart;
        if (_specialParallelRegionStart == null) {
            parallelRegionStart = Directive.findParallelRegionStart(context, _fctDef, null);
        } else {
            parallelRegionStart = _specialParallelRegionStart;
        }
        Xnode parallelRegionEnd = Directive.findParallelRegionEnd(context, _fctDef, null);
        // Define a hook from where we can insert the new do statement
        Xnode hook = parallelRegionEnd != null ? parallelRegionEnd.nextSibling() : null;
        Body.shiftIn(parallelRegionStart, parallelRegionEnd, loops.getInnerStatement().body(), true);
        // Hook is null then we append the do statement to the current fct body
        if (hook == null) {
            _fctDef.body().append(loops.getOuterStatement());
        } else {
            // Insert new do statement before the hook element
            hook.insertBefore(loops.getOuterStatement());
        }
    }
    // TODO nodep passing!
    int collapse = Directive.generateLoopSeq(xcodeml, loops.getInnerStatement().body(), CompilerDirective.CLAW.getPrefix() + " nodep");
    // Prepare variables list for present/pcreate clauses and handle
    // promotion/privatize local strategy
    List<String> presentList = _fctDef.getPresentVariables(xcodeml);
    List<String> privateList = Collections.emptyList();
    List<String> createList = Collections.emptyList();
    if (config.getLocalStrategy() == AcceleratorLocalStrategy.PRIVATE) {
        privateList = applyPrivateStrategy(xcodeml);
    } else if (config.getLocalStrategy() == AcceleratorLocalStrategy.PROMOTE) {
        createList = applyPromoteStrategy(cfg, xcodeml);
    }
    // Generate the data region
    Xblock doStmtBlock = new Xblock(loops.getOuterStatement());
    Directive.generateDataRegionClause(xcodeml, presentList, createList, doStmtBlock);
    // Generate the parallel region
    Directive.generateParallelLoopClause(xcodeml, privateList, loops.getOuterStatement(), loops.getOuterStatement(), null, loops.size() + collapse);
    Directive.generateRoutineDirectives(xcodeml, _fctDef);
}
Also used : Context(claw.tatsu.common.Context) Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) AcceleratorConfiguration(claw.tatsu.directive.configuration.AcceleratorConfiguration) IllegalTransformationException(claw.tatsu.xcodeml.exception.IllegalTransformationException) Xblock(claw.tatsu.xcodeml.abstraction.Xblock) NestedDoStatement(claw.tatsu.xcodeml.abstraction.NestedDoStatement) PromotionInfo(claw.tatsu.xcodeml.abstraction.PromotionInfo)

Example 7 with Xblock

use of claw.tatsu.xcodeml.abstraction.Xblock in project claw-compiler by C2SM-RCM.

the class Directive method generateParallelLoopClause.

/**
 * Generate directive directive for a parallel loop.
 *
 * @param xcodeml   Object representation of the current XcodeML representation
 *                  in which the pragmas will be generated.
 * @param privates  List of variables to be set privates.
 * @param startStmt Start statement representing the beginning of the parallel
 *                  region.
 * @param endStmt   End statement representing the end of the parallel region.
 * @param collapse  If value bigger than 0, a corresponding collapse constructs
 *                  can be generated.
 * @return Block with start and end directive if generated.
 */
public static Xblock generateParallelLoopClause(XcodeProgram xcodeml, List<String> privates, Xnode startStmt, Xnode endStmt, String extraDirective, int collapse) {
    if (xcodeml.context().getGenerator().getDirectiveLanguage() == CompilerDirective.NONE) {
        return null;
    }
    DirectiveGenerator dg = xcodeml.context().getGenerator();
    Xnode startBlock = addPragmasBefore(xcodeml, dg.getStartParallelDirective(null), startStmt);
    addPragmasBefore(xcodeml, dg.getStartLoopDirective(collapse, false, false, format(dg.getPrivateClause(privates), extraDirective)), startStmt);
    Xnode endBlock = addPragmaAfter(xcodeml, dg.getEndParallelDirective(), endStmt);
    addPragmaAfter(xcodeml, dg.getEndLoopDirective(), endStmt);
    return new Xblock(startBlock, endBlock);
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) DirectiveGenerator(claw.tatsu.directive.generator.DirectiveGenerator) Xblock(claw.tatsu.xcodeml.abstraction.Xblock)

Example 8 with Xblock

use of claw.tatsu.xcodeml.abstraction.Xblock in project claw-compiler by C2SM-RCM.

the class Directive method insertPragmas.

/**
 * Generate corresponding pragmas to surround the code with a parallel
 * accelerated region.
 *
 * @param xcodeml        Current XcodeML program unit. representation in which
 *                       the pragmas will be generated.
 * @param hook           Hook node to insert pragmas around.
 * @param startDirective String value of the start directive.
 * @param endDirective   String value of the end directive.
 * @return Last stmt inserted or null if nothing is inserted.
 */
private static Xblock insertPragmas(XcodeProgram xcodeml, Xblock hook, String[] startDirective, String[] endDirective) {
    if (xcodeml.context().getGenerator().getDirectiveLanguage() == CompilerDirective.NONE) {
        return null;
    }
    Xnode begin = addPragmasBefore(xcodeml, startDirective, hook.getStart());
    Xnode end = addPragmaAfter(xcodeml, endDirective, hook.getEnd());
    return new Xblock(begin, end);
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) Xblock(claw.tatsu.xcodeml.abstraction.Xblock)

Aggregations

Xblock (claw.tatsu.xcodeml.abstraction.Xblock)8 Xnode (claw.tatsu.xcodeml.xnode.common.Xnode)8 Context (claw.tatsu.common.Context)4 IllegalTransformationException (claw.tatsu.xcodeml.exception.IllegalTransformationException)4 FfunctionDefinition (claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition)3 Configuration (claw.wani.x2t.configuration.Configuration)3 ClawTranslator (claw.wani.x2t.translator.ClawTranslator)3 PromotionInfo (claw.tatsu.xcodeml.abstraction.PromotionInfo)2 Xid (claw.tatsu.xcodeml.xnode.common.Xid)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Transformation (claw.shenron.transformation.Transformation)1 Translator (claw.shenron.translator.Translator)1 Target (claw.tatsu.common.Target)1 DataMovement (claw.tatsu.directive.common.DataMovement)1 Directive (claw.tatsu.directive.common.Directive)1 AcceleratorConfiguration (claw.tatsu.directive.configuration.AcceleratorConfiguration)1 DirectiveGenerator (claw.tatsu.directive.generator.DirectiveGenerator)1 Range (claw.tatsu.primitive.Range)1 FunctionCall (claw.tatsu.xcodeml.abstraction.FunctionCall)1