/**
   * Pin a given list of variables i.e., set the "clean up" state in corresponding matrix objects,
   * so that the cached data inside these objects is not cleared and the corresponding HDFS files
   * are not deleted (through rmvar instructions).
   *
   * <p>This is necessary for: function input variables, parfor result variables, parfor shared
   * inputs that are passed to functions.
   *
   * <p>The function returns the OLD "clean up" state of matrix objects.
   *
   * @param varList variable list
   * @return map of old cleanup state of matrix objects
   */
  public HashMap<String, Boolean> pinVariables(ArrayList<String> varList) {
    // 2-pass approach since multiple vars might refer to same matrix object
    HashMap<String, Boolean> varsState = new HashMap<String, Boolean>();

    // step 1) get current information
    for (String var : varList) {
      Data dat = _variables.get(var);
      if (dat instanceof MatrixObject) {
        MatrixObject mo = (MatrixObject) dat;
        varsState.put(var, mo.isCleanupEnabled());
        // System.out.println("pre-pin "+var+" ("+mo.isCleanupEnabled()+")");
      }
    }

    // step 2) pin variables
    for (String var : varList) {
      Data dat = _variables.get(var);
      if (dat instanceof MatrixObject) {
        MatrixObject mo = (MatrixObject) dat;
        mo.enableCleanup(false);
        // System.out.println("pin "+var);
      }
    }

    return varsState;
  }
 public void cleanupMatrixObject(MatrixObject mo) throws DMLRuntimeException {
   try {
     if (mo.isCleanupEnabled()) {
       // compute ref count only if matrix cleanup actually necessary
       if (!getVariables().hasReferences(mo)) {
         // clean cached data
         mo.clearData();
         if (mo.isHDFSFileExists()) {
           // clean hdfs data
           String fpath = mo.getFileName();
           if (fpath != null) {
             MapReduceTool.deleteFileIfExistOnHDFS(fpath);
             MapReduceTool.deleteFileIfExistOnHDFS(fpath + ".mtd");
           }
         }
       }
     }
   } catch (Exception ex) {
     throw new DMLRuntimeException(ex);
   }
 }