Can I using mutilthread to convert mutil file DXF to PNG at the same time

I have a code to convert dxf to png

def process_images(input_dxf_file, output_dxf_file):
layout = db.Layout()
layout.read(input_dxf_file)
layout_view = lay.LayoutView()
layout_view.max_hier()

layout_view.max_hier()
top_cell = layout.top_cell()
layout_view.active_cellview().cell = top_cell
box = layout.top_cell().bbox()
roi = top_cell.dbbox()
layout_view.save_image_with_options(output_dxf_file, box.width()//10, box.height()//10, 0, 0, 0, roi, False)

Can I using mutilthread to convert mutil file DXF to PNG at the same time?

And Could this time process is less than time process one by one file ?

Comments

  • edited November 2023

    Hi @hieund,

    Many years ago, before the KLayout standalone Python package existed,
    I wrote an OASIS --> PNG converter that calls the klayout executable, as shown below.
    The contents of koas2png.py are very similar to your code.

    klayout -e          \
      -c  ConfigFile    \
      -rd InOASIS=$1    \
      -rd OutPNG=$2     \
      -rd LayerProp=$3  \
      -rd ImgSizeX=$4   \
      -rd ImgSizeY=$5   \
      -rd ImgBorder=$6  \
      -r ./koas2png.py  \
      -z
    where,
         $1 is an OASIS file name
         $2 is an output PNG file name
         $3 is KLayout's layer property file
         $4|$5 is an image size in [pixel]
         $6 is ['yes'|'no']
    

    The program is not a multi-threaded tool, but a multi-process tool with the interface shown below.

    ---------------------------------------------------------------------------------
     oas2png-py3.py version 1.0 (Rev.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
    << To convert OASIS file(s) to PNG file(s) with the help of KLayout >>
    
    $ [python] oas2png-py3.py <-i|--input <OASIS file(s)>>               ('')
                              <-l|--lyp   <layer property file>>         ('')
                              <-c|--conf  <KLayout's config file>>       ('')
                              [-x|--sizex <image pixel size in X>]       (1000)
                              [-y|--sizey <image pixel size in Y>]       (1000)
                              [-b|--noborder] : without the image border (disabled)
                              [-m|--multi] : try to use multiprocess     (disabled)
                              [-d|--debug  <level>]                      (1)
                              [-?|--?] : print this usage and exit       (disabled)
                              You may use a wild card like -i "*.oas"
    ---------------------------------------------------------------------------------
    

    Suppose we have seven OASIS files to convert.
    Let's compare sequential and parallel conversions.

    Linux_4Core_8Threads {sekigawa}(1)$ ll
    total 4972
    1084 -rw-rw-r-- 1 sekigawa sekigawa 1106837 Nov 28 17:49 01.oas
     844 -rw-rw-r-- 1 sekigawa sekigawa  862832 Nov 28 17:49 02.oas
     652 -rw-rw-r-- 1 sekigawa sekigawa  667619 Nov 28 17:49 03.oas
     608 -rw-rw-r-- 1 sekigawa sekigawa  618830 Nov 28 17:49 04.oas
     512 -rw-rw-r-- 1 sekigawa sekigawa  522152 Nov 28 17:49 05.oas
     420 -rw-rw-r-- 1 sekigawa sekigawa  429317 Nov 28 17:49 06.oas
     384 -rw-rw-r-- 1 sekigawa sekigawa  390596 Nov 28 17:49 07.oas
     388 -rw-rw-r-- 1 sekigawa sekigawa  397304 Nov 28 17:49 common.lyp
      80 -rw-rw-r-- 1 sekigawa sekigawa   78151 Nov 28 17:49 klayoutrc
    

    1. Sequential Conversion

    Linux_4Core_8Threads {sekigawa}(2)$ oas2png-py3.py -c klayoutrc -l common.lyp -i "*.oas"
    ### Initial: size of FIFO = 7
    
    ### <0001> Generating <01.png> from OASIS <01.oas> ...
    
    ### <0002> Generating <02.png> from OASIS <02.oas> ...
    
    ### <0003> Generating <03.png> from OASIS <03.oas> ...
    
    ### <0004> Generating <04.png> from OASIS <04.oas> ...
    
    ### <0005> Generating <05.png> from OASIS <05.oas> ...
    
    ### <0006> Generating <06.png> from OASIS <06.oas> ...
    
    ### <0007> Generating <07.png> from OASIS <07.oas> ...
    
    >>> Elapsed Time = 22.237 [sec] >>>
    

    2. Multi-process Conversion

    Linux_4Core_8Threads {sekigawa}(3)$ oas2png-py3.py -c klayoutrc -l common.lyp -i "*.oas" -m
    ### Initial: size of FIFO = 7
    
    ########################################################################
    ### Process <Worker1> processing <01.oas> ...
    ########################################################################
    ### Generating <01.png> from OASIS <01.oas> ...
    ########################################################################
    ### Process <Worker2> processing <02.oas> ...
    ########################################################################
    ### Generating <02.png> from OASIS <02.oas> ...
    ########################################################################
    ### Process <Worker3> processing <03.oas> ...
    ########################################################################
    ### Generating <03.png> from OASIS <03.oas> ...
    ########################################################################
    ### Process <Worker4> processing <04.oas> ...
    ########################################################################
    ### Generating <04.png> from OASIS <04.oas> ...
    ########################################################################
    ### Process <Worker5> processing <05.oas> ...
    ########################################################################
    ### Generating <05.png> from OASIS <05.oas> ...
    ########################################################################
    ### Process <Worker6> processing <06.oas> ...
    ########################################################################
    ### Generating <06.png> from OASIS <06.oas> ...
    ########################################################################
    ### Process <Worker7> processing <07.oas> ...
    ########################################################################
    ### Generating <07.png> from OASIS <07.oas> ...
    
    ########################################################################
    ### Process <Worker6> done <06.oas> ...
    ########################################################################
    
    
    ########################################################################
    ### Process <Worker7> done <07.oas> ...
    ########################################################################
    
    
    ########################################################################
    ### Process <Worker5> done <05.oas> ...
    ########################################################################
    
    
    ########################################################################
    ### Process <Worker3> done <03.oas> ...
    ########################################################################
    
    
    ########################################################################
    ### Process <Worker2> done <02.oas> ...
    ########################################################################
    
    
    ########################################################################
    ### Process <Worker1> done <01.oas> ...
    ########################################################################
    
    
    ########################################################################
    ### Process <Worker4> done <04.oas> ...
    ########################################################################
    
    >>> "oas2png-py3.py" processed <7> OASIS file(s) in total >>>
    >>> Elapsed Time = 5.456 [sec] >>>
    

    I think you can do something similar with the KLayout standalone Python package.
    Good luck with that.

    Kazzz-S

  • Mr @sekigawa, this is pretty good idea thanks for your help.

  • Very good. Thanks for this discussion.

    I favor the multi-process way as there is little overhead except that you basically need two times the memory for the layout because you load it into each process. But on the plus side you can parallelize across multiple machines (not only cores).

    Kind regards,

    Matthias

Sign In or Register to comment.