text: user252: I am creating a pipeline in Nextflow. One step is creating a pangenome with Roary. Roary takes threads as an argument and if no number of threads is supplied as an argument it defaults to one. 
  Is there a way in Nextflow to pass the maximum number of available threads as an argument? At the moment the best I can come up with is defining an arbitrarily large number of threads as an argument which will cap out and run with the max number of threads. I would like to know, is there a 'cleaner' or more canonical way of doing this?
  At the moment my process looks like this:
  
  process roary {
  
      publishDir "${params.outdir}/roary", mode: 'copy'
  
      cpus 200
  
      input:
      file gff from gff.collect()
  
      output:
      file("*") into roary
      file("pan_genome_reference.fa") into pan_genome
      file("pan_genome_sequences/*") into alignment_files
      file("gene_presence_absence.Rtab") into gene_presence
      set file("*accessory*"),
          file("*.Rtab"),
          file("_*"),
          file("*.txt"),
          file("*csv") into roary_out
  
  
      script:
      """
      roary -p ${task.cpus} -e -n -v -z $gff
      """
  }
  
  Thanks in advance. Also, apologies as I can see why this might not strictly be bioinformatics, but it's where Nextflow has the most traction.
  
  user4413: Here's an example I wrote recently:
  #!/usr/bin/env nextflow
  
  params.requestedCPU = 80
  params.requestedMem = 50
  
  maxcpus = Runtime.runtime.availableProcessors()
  requestedCpus = Math.round((params.requestedCPU * maxcpus) / 100)
  
  maxmem = Runtime.runtime.totalMemory() / 10241024
  requestedMem = (Math.round((params.requestedMem * maxmem) / 100))
  
  process foo {
      echo true
      cpus = requestedCpus
      memory = requestedMem.GB
  
      script:
          """
          echo $requestedCpus
   echo $requestedMem
          """
  }
  
  You can pass the number of CPUs or Memory directly on the command line in percentage, e.g. nextflow run main.nf --requestedCPU 100 to have all CPU usages. Hope this helps.
  
  user678: I have not read the full Nextflow manual in depth, but I think there is no Nextflow command to detect the maximum number of threads you can use.
  However, you can try to automatize this task by using the nproc command.
  nproc gives you the number of processing units available. An idea would be to include something such nproc - 1 as the max number of threads for Roary.  
  
original_text: 252: <p>I am creating a pipeline in Nextflow. One step is creating a pangenome with Roary. Roary takes threads as an argument and if no number of threads is supplied as an argument it defaults to one. </p>
  
  <p>Is there a way in Nextflow to pass the maximum number of available threads as an argument? At the moment the best I can come up with is defining an arbitrarily large number of threads as an argument which will cap out and run with the max number of threads. I would like to know, is there a 'cleaner' or more canonical way of doing this?</p>
  
  <p>At the moment my process looks like this:</p>
  
  <pre><code>
  process roary {
  
      publishDir "${params.outdir}/roary", mode: 'copy'
  
      cpus 200
  
      input:
      file gff from gff.collect()
  
      output:
      file("*") into roary
      file("pan_genome_reference.fa") into pan_genome
      file("pan_genome_sequences/*") into alignment_files
      file("gene_presence_absence.Rtab") into gene_presence
      set file("*accessory*"),
          file("*.Rtab"),
          file("_*"),
          file("*.txt"),
          file("*csv") into roary_out
  
  
      script:
      """
      roary -p <span class="math-container">${task.cpus} -e -n -v -z $</span>gff
      """
  }
  </code></pre>
  
  <p>Thanks in advance. Also, apologies as I can see why this might not strictly be bioinformatics, but it's where Nextflow has the most traction.</p>
   4413: <p>Here's an example I wrote recently:</p>
  
  <pre class="lang-java prettyprint-override"><code>#!/usr/bin/env nextflow
  
  params.requestedCPU = 80
  params.requestedMem = 50
  
  maxcpus = Runtime.runtime.availableProcessors()
  requestedCpus = Math.round((params.requestedCPU * maxcpus) / 100)
  
  maxmem = Runtime.runtime.totalMemory() / 10241024
  requestedMem = (Math.round((params.requestedMem * maxmem) / 100))
  
  process foo {
      echo true
      cpus = requestedCpus
      memory = requestedMem.GB
  
      script:
          """
          echo <span class="math-container">$requestedCpus
   echo $</span>requestedMem
          """
  }
  </code></pre>
  
  <p>You can pass the number of CPUs or Memory directly on the command line in percentage, e.g. <code>nextflow run main.nf --requestedCPU 100</code> to have all CPU usages. Hope this helps.</p>
   678: <p>I have not read the full Nextflow manual in depth, but I think there is no Nextflow command to detect the maximum number of threads you can use.</p>
  
  <p>However, you can try to automatize this task by using the <code>nproc</code> command.</p>
  
  <p><code>nproc</code> gives you the number of processing units available. An idea would be to include something such <code>nproc - 1</code> as the max number of threads for Roary.  </p>
  