Search in sources :

Example 1 with ReshapeInfo

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

the class LoopHoist method analyze.

/**
 * @see Transformation#analyze(XcodeProgram, Translator)
 */
@Override
public boolean analyze(XcodeProgram xcodeml, Translator translator) {
    if (_clawEnd == null) {
        xcodeml.addError("loop-hoist directive requires an end directive.", _clawStart.getPragma().lineNo());
        return false;
    }
    int pragmaDepthLevel = _clawStart.getPragma().depth();
    // Find all the group of nested loops that can be part of the hoisting
    List<HoistedNestedDoStatement> statements = XnodeUtil.findDoStatementForHoisting(_clawStart.getPragma(), _clawEnd.getPragma(), _clawStart.values(ClawClause.HOIST_INDUCTIONS));
    if (statements.isEmpty()) {
        xcodeml.addError("No do statement group meets the criteria of hoisting.", _clawStart.getPragma().lineNo());
        return false;
    }
    for (HoistedNestedDoStatement hoistedNestedDoStmt : statements) {
        int depth = hoistedNestedDoStmt.getOuterStatement().depth();
        if (depth != pragmaDepthLevel) {
            Xnode tmpIf = hoistedNestedDoStmt.getOuterStatement().matchAncestor(Xcode.F_IF_STATEMENT);
            Xnode tmpSelect = hoistedNestedDoStmt.getOuterStatement().matchAncestor(Xcode.F_SELECT_CASE_STATEMENT);
            Xnode tmpDo = hoistedNestedDoStmt.getOuterStatement().matchAncestor(Xcode.F_DO_STATEMENT);
            if (tmpIf == null && tmpSelect == null && tmpDo == null) {
                xcodeml.addError("A nested do stmt group is nested in an unsupported " + "statement for loop hoisting.", hoistedNestedDoStmt.getOuterStatement().lineNo());
                return false;
            }
            int ifDepth = (tmpIf != null) ? tmpIf.depth() : Xnode.UNDEF_DEPTH;
            int selectDepth = (tmpSelect != null) ? tmpSelect.depth() : Xnode.UNDEF_DEPTH;
            int doDepth = (tmpDo != null) ? tmpDo.depth() : Xnode.UNDEF_DEPTH;
            if ((pragmaDepthLevel <= ifDepth || pragmaDepthLevel <= selectDepth || pragmaDepthLevel <= doDepth) && (ifDepth < depth || selectDepth < depth || doDepth < depth)) {
                hoistedNestedDoStmt.setExtraction();
            } else {
                xcodeml.addError("Group is nested in an unsupported " + "statement for loop hoisting or depth is too high " + "(Group index starts at 0).", _clawStart.getPragma().lineNo());
                return false;
            }
        }
        _hoistedGroups.add(hoistedNestedDoStmt);
    }
    HoistedNestedDoStatement master = _hoistedGroups.get(0);
    for (int i = 1; i < _hoistedGroups.size(); ++i) {
        HoistedNestedDoStatement next = _hoistedGroups.get(i);
        for (int j = 0; j < master.size(); ++j) {
            // Iteration range are identical, just merge
            if (j == 0 && (!Loop.hasSameIndexRange(master.get(j), next.get(j)) && Loop.hasSameIndexRangeBesidesLower(master.get(j), next.get(j)))) {
                // Iteration range are identical besides lower-bound, if creation
                next.setIfStatement();
            } else if (!Loop.hasSameIndexRange(master.get(j), next.get(j))) {
                // Iteration range are too different, stop analysis
                xcodeml.addError("Iteration range of do statements group " + i + " differs from group 0. Loop hoisting aborted.", _clawStart.getPragma().lineNo());
                return false;
            }
        }
    }
    // Check reshape mandatory points
    if (_clawStart.hasClause(ClawClause.RESHAPE)) {
        FfunctionDefinition fctDef = _clawStart.getPragma().findParentFunction();
        if (fctDef == null) {
            xcodeml.addError("Unable to matchSeq the function/subroutine/module " + "definition including the current directive", _clawStart.getPragma().lineNo());
            return false;
        }
        for (ReshapeInfo r : _clawStart.getReshapeClauseValues()) {
            if (!fctDef.getSymbolTable().contains(r.getArrayName()) || !fctDef.getDeclarationTable().contains(r.getArrayName())) {
                // Check in the parent def if present
                if (!checkUpperDefinition(fctDef, r.getArrayName())) {
                    xcodeml.addError(String.format("Reshape variable %s not found in " + "the definition of %s", r.getArrayName(), fctDef.getName()), _clawStart.getPragma().lineNo());
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) FfunctionDefinition(claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition) HoistedNestedDoStatement(claw.tatsu.xcodeml.abstraction.HoistedNestedDoStatement) ReshapeInfo(claw.tatsu.xcodeml.abstraction.ReshapeInfo)

Example 2 with ReshapeInfo

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

the class LoopHoist method transform.

/**
 * @see Transformation#transform(XcodeProgram, Translator, Transformation)
 */
@Override
public void transform(XcodeProgram xcodeml, Translator translator, Transformation transformation) throws Exception {
    ClawTranslator ct = (ClawTranslator) translator;
    HoistedNestedDoStatement hoisted = Loop.hoist(_hoistedGroups, _clawStart.getPragma(), _clawEnd.getPragma(), xcodeml);
    // Generate dynamic transformation (interchange)
    ct.generateAdditionalTransformation(_clawStart, xcodeml, hoisted.getOuterStatement());
    // Apply cleanup clause
    if (_clawStart.hasClause(ClawClause.CLEANUP)) {
        Pragma.remove(_clawStart.getCleanupClauseValue(), _clawStart.getPragma(), _clawEnd.getPragma());
    }
    // Apply reshape clause
    if (_clawStart.hasClause(ClawClause.RESHAPE)) {
        FfunctionDefinition fctDef = _clawStart.getPragma().findParentFunction();
        if (fctDef == null) {
            throw new IllegalTransformationException("Cannot apply reshape clause." + "Parent function definition not found.", _clawStart.getPragma().lineNo());
        }
        for (ReshapeInfo reshapeInfo : _clawStart.getReshapeClauseValues()) {
            Field.reshape(fctDef, reshapeInfo, xcodeml);
        }
    }
    removePragma();
}
Also used : FfunctionDefinition(claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition) IllegalTransformationException(claw.tatsu.xcodeml.exception.IllegalTransformationException) ClawTranslator(claw.wani.x2t.translator.ClawTranslator) HoistedNestedDoStatement(claw.tatsu.xcodeml.abstraction.HoistedNestedDoStatement) ReshapeInfo(claw.tatsu.xcodeml.abstraction.ReshapeInfo)

Example 3 with ReshapeInfo

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

the class ClawPragmaTest method loopHoistTest.

/**
 * Test various input for the CLAW loop-hoist directive.
 */
@Test
public void loopHoistTest() {
    // Valid directives
    analyzeValidLoopHoist("claw loop-hoist(i,j)", Arrays.asList("i", "j"), false, null, false, null, null, false, null, 0, false, null);
    analyzeValidLoopHoist("claw loop-hoist(i,j) interchange", Arrays.asList("i", "j"), true, null, false, null, null, false, null, 0, false, null);
    analyzeValidLoopHoist("claw loop-hoist(i,j) interchange(j,i)", Arrays.asList("i", "j"), true, Arrays.asList("j", "i"), false, null, null, false, null, 0, false, null);
    List<Integer> empty = Collections.emptyList();
    ReshapeInfo info1 = new ReshapeInfo("zmd", 0, empty);
    ReshapeInfo info2 = new ReshapeInfo("zsediflux", 1, Collections.singletonList(2));
    analyzeValidLoopHoist("claw loop-hoist(i,j) " + "reshape(zmd(0), zsediflux(1,2))", Arrays.asList("i", "j"), false, null, true, Arrays.asList(info1, info2), null, false, null, 0, false, null);
    analyzeValidLoopHoist("claw loop-hoist(i,j) target(cpu) interchange", Arrays.asList("i", "j"), true, null, false, null, Collections.singletonList(Target.CPU), false, null, 0, false, null);
    analyzeValidLoopHoist("claw loop-hoist(i,j) interchange target(cpu, gpu)", Arrays.asList("i", "j"), true, null, false, null, Arrays.asList(Target.CPU, Target.GPU), false, null, 0, false, null);
    analyzeValidLoopHoist("claw loop-hoist(i,j) target(mic)", Arrays.asList("i", "j"), false, null, false, null, Collections.singletonList(Target.MIC), false, null, 0, false, null);
    analyzeValidLoopHoist("claw loop-hoist(i,j) fusion", Arrays.asList("i", "j"), false, null, false, null, null, true, null, 0, false, null);
    analyzeValidLoopHoist("claw loop-hoist(i,j) fusion group(j1)", Arrays.asList("i", "j"), false, null, false, null, null, true, "j1", 0, false, null);
    analyzeValidLoopHoist("claw loop-hoist(i,j) fusion collapse(2)", Arrays.asList("i", "j"), false, null, false, null, null, true, null, 2, false, null);
    analyzeValidLoopHoist("claw loop-hoist(i,j) cleanup", Arrays.asList("i", "j"), false, null, false, null, null, false, null, 0, true, CompilerDirective.NONE);
    analyzeValidLoopHoist("claw loop-hoist(i,j) cleanup(acc)", Arrays.asList("i", "j"), false, null, false, null, null, false, null, 0, true, CompilerDirective.OPENACC);
    analyzeValidLoopHoist("claw loop-hoist(i,j) cleanup(omp)", Arrays.asList("i", "j"), false, null, false, null, null, false, null, 0, true, CompilerDirective.OPENMP);
    // Invalid directives
    analyzeInvalidClawLanguage("claw loop-hoist");
    analyzeInvalidClawLanguage("claw loop-hoist()");
    analyzeInvalidClawLanguage("claw loop-hoist(i,j) interchange()");
    analyzeInvalidClawLanguage("claw loop-hoist(i,j) cleanup()");
    analyzeInvalidClawLanguage("claw loop-hoist(i,j) cleanup(claw)");
    analyzeInvalidClawLanguage("claw loop-hoist(i,j) cleanup(dummy)");
    analyzeValidSimpleClaw("claw end loop-hoist", ClawDirective.LOOP_HOIST, true, null);
    analyzeValidSimpleClaw("claw   end   loop-hoist  ", ClawDirective.LOOP_HOIST, true, null);
}
Also used : ReshapeInfo(claw.tatsu.xcodeml.abstraction.ReshapeInfo) Test(org.junit.Test)

Aggregations

ReshapeInfo (claw.tatsu.xcodeml.abstraction.ReshapeInfo)3 HoistedNestedDoStatement (claw.tatsu.xcodeml.abstraction.HoistedNestedDoStatement)2 FfunctionDefinition (claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition)2 IllegalTransformationException (claw.tatsu.xcodeml.exception.IllegalTransformationException)1 Xnode (claw.tatsu.xcodeml.xnode.common.Xnode)1 ClawTranslator (claw.wani.x2t.translator.ClawTranslator)1 Test (org.junit.Test)1