nextflow icon indicating copy to clipboard operation
nextflow copied to clipboard

Add an intdiv method to nextflow.util.MemoryUnit

Open jinmingda opened this issue 1 year ago • 2 comments

New feature

Is it possible to implement an intdiv() method in nextflow.util.MemoryUnit as followed?.

assert 7.GB == (15.GB).intdiv(2)

Usage scenario

I would like to assign a long type of value to memory in a process label when the memory allocation is determined programmatically based on another parameter.

withLabel:biTenancy {
    memory = (params.max_memory - 1.GB).intdiv(2)
}

Suggest implementation

MemoryUnit intdiv( Number value ) {
    return new MemoryUnit( (long)(size.intdiv(value)) )
}

jinmingda avatar Aug 01 '22 18:08 jinmingda

MemoryUnit has already a method to divide ('div' )

For example:

main.nf

workflow {
 def max = params.max_memory as MemoryUnit
 def mem = (max - 1.GB ) / 2
 println mem
 println mem.bytes  // long
}

nextflow run ./memunit.nf  --max_memory=10GB
N E X T F L O W  ~  version 22.04.5
Launching `./memunit.nf` [evil_mclean] DSL2 - revision: 9c0e87b85c
4.5 GB
4831838208

jorgeaguileraseqera avatar Aug 02 '22 11:08 jorgeaguileraseqera

Hi @jorgeaguileraseqera thank you for your reply. To clarify, I was wondering if I could pass a long integer in GB to memory without having to converting it to bytes e.g. assert (15.GB).intdiv(2) == 7.GB. Currently I can only achieve this by converting memory unit to long, call intdiv and convert it back to corresponding MemorryUnit. Just thought an intdiv method directly comes with the class can be helpful in this case.

jinmingda avatar Aug 03 '22 04:08 jinmingda