/*
 * This file is released under terms of BSD license
 * See LICENSE file for more information
 */

package cx2x.translator.language.helper.accelerator;

import cx2x.translator.language.helper.target.Target;
import cx2x.translator.misc.Utility;
import xcodeml.util.XmOption;

import java.util.List;

/**
 * OpenACC specific accelerator directive generator.
 *
 * @author clementval
 */
class OpenAcc extends AcceleratorGenerator {

  private static final String OPENACC_COLLAPSE = "collapse";
  private static final String OPENACC_DATA = "data";
  private static final String OPENACC_END = "end";
  private static final String OPENACC_LOOP = "loop";
  private static final String OPENACC_PARALLEL = "parallel";
  private static final String OPENACC_PREFIX = "acc";
  private static final String OPENACC_PRIVATE = "private";
  private static final String OPENACC_PRESENT = "present";
  private static final String OPENACC_ROUTINE = "routine";
  private static final String OPENACC_SEQUENTIAL = "seq";

  /**
   * Constructs a new object with the given target.
   *
   * @param target Target for which the directive must be generated.
   */
  OpenAcc(Target target) {
    super(target);
  }

  @Override
  protected String getPrefix(){
    return OPENACC_PREFIX;
  }

  @Override
  protected String getStartParallelDirective() {
    return String.format(FORMAT2, OPENACC_PREFIX, OPENACC_PARALLEL);
  }

  @Override
  protected String getEndParallelDirective() {
    return String.format(FORMAT3,
        OPENACC_PREFIX, OPENACC_END, OPENACC_PARALLEL);
  }

  @Override
  protected String getSingleDirective(String clause) {
    return String.format(FORMAT2, OPENACC_PREFIX, clause);
  }

  @Override
  protected String getParallelKeyword(){
    return OPENACC_PARALLEL;
  }

  @Override
  protected String getPrivateClause(String var) {
    return String.format(FORMATPAR, OPENACC_PRIVATE, var);
  }

  @Override
  protected String getPrivateClause(List<String> vars) {
    if(vars == null || vars.size() == 0){
      return "";
    }
    if(XmOption.isDebugOutput()){
      System.out.println("OpenACC: generate private clause for: " +
          Utility.join(",", vars));
    }
    return String.format(FORMATPAR, OPENACC_PRIVATE, Utility.join(",", vars));
  }

  @Override
  protected String getPresentClause(List<String> vars) {
    if(vars == null || vars.size() == 0){
      return "";
    }
    if(XmOption.isDebugOutput()){
      System.out.println("OpenACC: generate present clause for: " +
          Utility.join(",", vars));
    }
    return String.format(FORMATPAR, OPENACC_PRESENT, Utility.join(",", vars));
  }

  @Override
  protected String getRoutineDirective(){
    return String.format(FORMAT2, OPENACC_PREFIX, OPENACC_ROUTINE);
  }

  @Override
  public boolean isCompileGuard(String rawDirective){
    return rawDirective.toLowerCase().startsWith(OPENACC_PREFIX) &&
        rawDirective.toLowerCase().contains(COMPILE_GUARD);
  }

  @Override
  public AcceleratorDirective getDirectiveLanguage(){
    return AcceleratorDirective.OPENACC;
  }

  @Override
  public String getStartDataRegion() {
    return String.format(FORMAT2, OPENACC_PREFIX, OPENACC_DATA);
  }

  @Override
  public String getEndDataRegion() {
    return String.format(FORMAT3, OPENACC_PREFIX, OPENACC_END, OPENACC_DATA);
  }

  @Override
  public String getSequentialClause() {
    return OPENACC_SEQUENTIAL;
  }

  @Override
  protected String getStartLoopDirective(int value) {
    if(value > 1){
      return String.format(FORMAT3, OPENACC_PREFIX, OPENACC_LOOP,
          String.format("%s(%d)", OPENACC_COLLAPSE, value));
    } else {
      return String.format(FORMAT2, OPENACC_PREFIX, OPENACC_LOOP);
    }
  }

  @Override
  protected String getEndLoopDirective() {
    return null;
  }
}

@@ -1313,32 +1313,53 @@
 elDirective() %7B%0A
+    //!$acc parallel%0A
     return Strin
@@ -1403,32 +1403,32 @@
 NACC_PARALLEL);%0A
-
   %7D%0A%0A  @Override
@@ -1467,32 +1467,57 @@
 elDirective() %7B%0A
+    //!$acc end parallel%0A
     return Strin
@@ -1658,24 +1658,45 @@
 g clause) %7B%0A
+    //!$acc %3Cclause%3E%0A
     return S
@@ -2734,24 +2734,44 @@
 irective()%7B%0A
+    //!$acc routine%0A
     return S
@@ -3185,32 +3185,49 @@
 tDataRegion() %7B%0A
+    //!$acc data%0A
     return Strin
@@ -3325,24 +3325,45 @@
 aRegion() %7B%0A
+    //!$acc end data%0A
     return S
@@ -3606,16 +3606,53 @@
 e %3E 1)%7B%0A
+      //!$acc loop collapse(%3Cvalue%3E)%0A
       re
@@ -3766,24 +3766,24 @@
 E, value));%0A
-
     %7D else %7B
@@ -3783,16 +3783,35 @@
  else %7B%0A
+      //!$acc loop%0A
       re

Add comments