Set number of thread for execution
authorpvtroshin <pvtroshin@e3abac25-378b-4346-85de-24260fe3988d>
Thu, 22 Jul 2010 13:17:08 +0000 (13:17 +0000)
committerpvtroshin <pvtroshin@e3abac25-378b-4346-85de-24260fe3988d>
Thu, 22 Jul 2010 13:17:08 +0000 (13:17 +0000)
git-svn-id: link to svn.lifesci.dundee.ac.uk/svn/barton/ptroshin/JABA2@2652 e3abac25-378b-4346-85de-24260fe3988d

runner/compbio/runner/disorder/Jronn.java

index 0bf17e4..e98ac3a 100644 (file)
@@ -23,7 +23,6 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;\r
 import java.io.IOException;\r
 import java.io.InputStream;\r
-import java.security.InvalidParameterException;\r
 import java.util.Arrays;\r
 import java.util.List;\r
 \r
@@ -32,6 +31,7 @@ import org.apache.log4j.Logger;
 import compbio.data.sequence.AnnotatedSequence;\r
 import compbio.data.sequence.SequenceUtil;\r
 import compbio.data.sequence.UnknownFileFormatException;\r
+import compbio.engine.client.CommandBuilder;\r
 import compbio.engine.client.Executable;\r
 import compbio.engine.client.SkeletalExecutable;\r
 import compbio.metadata.Limit;\r
@@ -49,142 +49,156 @@ import compbio.runner.Util;
  */\r
 public class Jronn extends SkeletalExecutable<Jronn> {\r
 \r
-       \r
-       private static Logger log = Logger.getLogger(Jronn.class);\r
-\r
-       /**\r
-        * Number of cores to use, defaults to 1 for local execution or the value of\r
-        * "jronn.cluster.cpunum" property for cluster execution\r
-        */\r
-       private int ncoreNumber = 0;\r
-\r
-       private String ncorePrm = "-n";\r
-\r
-       \r
-       // Cache for Limits information\r
-       private static LimitsManager<Jronn> limits;\r
-\r
-       public static final String KEY_VALUE_SEPARATOR = Util.SPACE;\r
-       public static final String STAT_FILE = "stat.txt";\r
-\r
-       public Jronn() {\r
-               addParameters(Arrays.asList("-jar", getLibPath(), "-s="\r
-                               + STAT_FILE, "-f=H"));\r
+    private static Logger log = Logger.getLogger(Jronn.class);\r
+\r
+    /**\r
+     * Number of cores to use, defaults to 1 for local execution or the value of\r
+     * "jronn.cluster.cpunum" property for cluster execution\r
+     */\r
+    private int ncoreNumber = 0;\r
+\r
+    private final String ncorePrm = "-n=";\r
+\r
+    // Cache for Limits information\r
+    private static LimitsManager<Jronn> limits;\r
+\r
+    public static final String KEY_VALUE_SEPARATOR = Util.SPACE;\r
+    public static final String STAT_FILE = "stat.txt";\r
+\r
+    public Jronn() {\r
+       addParameters(Arrays.asList("-jar", getLibPath(), "-s=" + STAT_FILE,\r
+               "-f=H"));\r
+    }\r
+\r
+    @SuppressWarnings("unchecked")\r
+    @Override\r
+    public List<AnnotatedSequence> getResults(String workDirectory)\r
+           throws ResultNotAvailableException {\r
+       List<AnnotatedSequence> sequences = null;\r
+       try {\r
+           InputStream inStream = new FileInputStream(new File(workDirectory,\r
+                   getOutput()));\r
+           sequences = SequenceUtil.readJRonn(inStream);\r
+           inStream.close();\r
+       } catch (FileNotFoundException e) {\r
+           log.error(e.getMessage(), e.getCause());\r
+           throw new ResultNotAvailableException(e);\r
+       } catch (IOException e) {\r
+           log.error(e.getMessage(), e.getCause());\r
+           throw new ResultNotAvailableException(e);\r
+       } catch (UnknownFileFormatException e) {\r
+           log.error(e.getMessage(), e.getCause());\r
+           throw new ResultNotAvailableException(e);\r
+       } catch (NullPointerException e) {\r
+           log.error(e.getMessage(), e.getCause());\r
+           throw new ResultNotAvailableException(e);\r
        }\r
+       return sequences;\r
+    }\r
 \r
-       @SuppressWarnings("unchecked")\r
-       @Override\r
-       public List<AnnotatedSequence> getResults(String workDirectory)\r
-       throws ResultNotAvailableException {\r
-               List<AnnotatedSequence> sequences = null;\r
-               try {\r
-                       InputStream inStream = new FileInputStream(new File(workDirectory,\r
-                                       getOutput()));\r
-                       sequences = SequenceUtil.readJRonn(inStream);\r
-                       inStream.close();\r
-               } catch (FileNotFoundException e) {\r
-                       log.error(e.getMessage(), e.getCause());\r
-                       throw new ResultNotAvailableException(e);\r
-               } catch (IOException e) {\r
-                       log.error(e.getMessage(), e.getCause());\r
-                       throw new ResultNotAvailableException(e);\r
-               } catch (UnknownFileFormatException e) {\r
-                       log.error(e.getMessage(), e.getCause());\r
-                       throw new ResultNotAvailableException(e);\r
-               } catch (NullPointerException e) {\r
-                       log.error(e.getMessage(), e.getCause());\r
-                       throw new ResultNotAvailableException(e);\r
-               }\r
-               return sequences;\r
-       }\r
+    private static String getLibPath() {\r
 \r
-       private static String getLibPath() {\r
-\r
-               String settings = ph.getProperty("jronn.jar.file");\r
-               if (compbio.util.Util.isEmpty(settings)) {\r
-                       throw new NullPointerException(\r
-                                       "Please define jronn.jar.file property in Executable.properties file"\r
-                                       + "and initialize it with the location of jronn jar file");\r
-               }\r
-               if (new File(settings).isAbsolute()) {\r
-                       // Jronn jar can be found so no actions necessary\r
-                       // no further actions is necessary\r
-                       return settings;\r
-               }\r
-               return compbio.engine.client.Util.convertToAbsolute(settings);\r
+       String settings = ph.getProperty("jronn.jar.file");\r
+       if (compbio.util.Util.isEmpty(settings)) {\r
+           throw new NullPointerException(\r
+                   "Please define jronn.jar.file property in Executable.properties file"\r
+                           + "and initialize it with the location of jronn jar file");\r
        }\r
-\r
-       @Override\r
-       public List<String> getCreatedFiles() {\r
-               return Arrays.asList(getOutput(), getError());\r
+       if (new File(settings).isAbsolute()) {\r
+           // Jronn jar can be found so no actions necessary\r
+           // no further actions is necessary\r
+           return settings;\r
        }\r
-\r
-       @Override\r
-       public Jronn setInput(String inFile) {\r
-               super.setInput(inFile);\r
-               cbuilder.setParam("-i=" + inFile);\r
-               return this;\r
+       return compbio.engine.client.Util.convertToAbsolute(settings);\r
+    }\r
+\r
+    @Override\r
+    public List<String> getCreatedFiles() {\r
+       return Arrays.asList(getOutput(), getError());\r
+    }\r
+\r
+    @Override\r
+    public Jronn setInput(String inFile) {\r
+       super.setInput(inFile);\r
+       cbuilder.setParam("-i=" + inFile);\r
+       return this;\r
+    }\r
+\r
+    @Override\r
+    public Jronn setOutput(String outFile) {\r
+       super.setOutput(outFile);\r
+       cbuilder.setParam("-o=" + outFile);\r
+       return this;\r
+    }\r
+\r
+    @Override\r
+    public Limit<Jronn> getLimit(String presetName) {\r
+       if (limits == null) {\r
+           limits = getLimits();\r
        }\r
-\r
-       @Override\r
-       public Jronn setOutput(String outFile) {\r
-               super.setOutput(outFile);\r
-               cbuilder.setParam("-o=" + outFile);\r
-               return this;\r
+       Limit<Jronn> limit = null;\r
+       if (limits != null) {\r
+           // this returns default limit if preset is undefined!\r
+           limit = limits.getLimitByName(presetName);\r
        }\r
-\r
-       @Override\r
-       public Limit<Jronn> getLimit(String presetName) {\r
-               if (limits == null) {\r
-                       limits = getLimits();\r
-               }\r
-               Limit<Jronn> limit = null;\r
-               if (limits != null) {\r
-                       // this returns default limit if preset is undefined!\r
-                       limit = limits.getLimitByName(presetName);\r
-               }\r
-               // If limit is not defined for a particular preset, then return default\r
-               // limit\r
-               if (limit == null) {\r
-                       log.debug("Limit for the preset " + presetName\r
-                                       + " is not found. Using default");\r
-                       limit = limits.getDefaultLimit();\r
-               }\r
-               return limit;\r
+       // If limit is not defined for a particular preset, then return default\r
+       // limit\r
+       if (limit == null) {\r
+           log.debug("Limit for the preset " + presetName\r
+                   + " is not found. Using default");\r
+           limit = limits.getDefaultLimit();\r
        }\r
-\r
-       @Override\r
-       public LimitsManager<Jronn> getLimits() {\r
-               // synchronise on static field\r
-               synchronized (log) {\r
-                       if (limits == null) {\r
-                               limits = Util.getLimits(this.getClass());\r
-                       }\r
-               }\r
-               return limits;\r
+       return limit;\r
+    }\r
+\r
+    @Override\r
+    public LimitsManager<Jronn> getLimits() {\r
+       // synchronise on static field\r
+       synchronized (log) {\r
+           if (limits == null) {\r
+               limits = Util.getLimits(this.getClass());\r
+           }\r
        }\r
-\r
-       @Override\r
-       public Class<? extends Executable<?>> getType() {\r
-               return this.getClass();\r
+       return limits;\r
+    }\r
+\r
+    @Override\r
+    public Class<? extends Executable<?>> getType() {\r
+       return this.getClass();\r
+    }\r
+\r
+    public static String getStatFile() {\r
+       return STAT_FILE;\r
+    }\r
+\r
+    public void setNCore(int ncoreNumber) {\r
+       if (ncoreNumber < 1 || ncoreNumber > 100) {\r
+           throw new IndexOutOfBoundsException(\r
+                   "Number of cores must be within 1 and 100 ");\r
        }\r
-\r
-       public static String getStatFile() {\r
-               return STAT_FILE;\r
+       this.ncoreNumber = ncoreNumber;\r
+       cbuilder.setParam(ncorePrm + Integer.toString(getNCore()));\r
+    }\r
+\r
+    int getNCore() {\r
+       return ncoreNumber;\r
+    }\r
+\r
+    @Override\r
+    public CommandBuilder<Jronn> getParameters(ExecProvider provider) {\r
+       // Limit number of cores to 1 for ANY execution which does not set\r
+       // Ncores explicitly using setNCore method\r
+       if (ncoreNumber == 0) {\r
+           setNCore(1);\r
        }\r
-       \r
-       public void setNCore(int ncoreNumber) {\r
-               if (ncoreNumber < 1 || ncoreNumber > 100) {\r
-                       throw new IndexOutOfBoundsException(\r
-                                       "Number of cores must be within 1 and 100 ");\r
-               }\r
-               this.ncoreNumber = ncoreNumber;\r
-               cbuilder.setParam(ncorePrm, Integer.toString(getNCore()));\r
+       // If number of cores is provided, set it for the cluster execution only!\r
+       if (provider == Executable.ExecProvider.Cluster) {\r
+           int cpunum = SkeletalExecutable.getClusterCpuNum(getType());\r
+           if (cpunum != 0) {\r
+               setNCore(cpunum);\r
+           }\r
        }\r
+       return super.getParameters(provider);\r
+    }\r
 \r
-       int getNCore() {\r
-               return ncoreNumber;\r
-       }\r
-       \r
-    \r
 }\r