It looks like you're new here. If you want to get involved, click one of these buttons!
Hi,
I am trying to enable threads to improve the runtime of a DRCLayer operation. It takes very long to iterate through all polygons and perform an action. I simplified the code example.
class DRC::DRCLayer
  def shape_operation(points)
    output_layer =  self.dup.merge
    output_layer.each{|shape|
      center = shape.bbox.center
      polygon = RBA::DPolygon::new(points)
      polygon.move(center.x, center.y)
      output_layer.insert(polygon)
    }
    return output_layer
  end
end
points = ...
layer.shape_operation(points)
I tried to replace the "each iteration" with a job queue and threads but I cannot get it to work.
jobs = Queue.new
POOL_SIZE = 10
output_layer.each{|i| jobs.push i}
workers = (POOL_SIZE).times.map do
  Thread.new do
    begin      
      while shape = jobs.pop(true)
        center = shape.bbox.center
        polygon = RBA::DPolygon::new(points)
        polygon.move(center.x, center.y)
        output_layer.insert(polygon)
      end
    rescue ThreadError
    end
  end
end
workers.map(&:join)
Does anyone have a good idea how I can execute the iteration in parallel? I thought about the tile processor, but I think it would be not the right way.
Best regards
Grandement
Comments
Have you tried the command to choose the number of threads ?
Laurent
First, my understanding of Ruby threads is that they do not use multiple cores. It is just a way to provide asynchronous execution.
Second, real multi-core thread enabling is higher art (MT safety, synchronization etc.). This is not as simple as this.
Third, iterating shapes this way is flat - i.e. slow.
Forth, you can optimize the loop already but taking out the Polygon constructor out of the loop and use "moved" instead of "move" to create a moved copy.
A way to speed up (because that employs hierarchy) is to use deep mode and use "processed" on the layer's "data" object (which is a
Regionobject). This concept is described here: https://www.klayout.de/doc-qt5/code/class_Region.html#method190 and https://www.klayout.de/doc-qt5/code/class_PolygonOperator.html.Matthias
Thank you for your suggestions. Shame on me, I have to update to the latest KLayout version asap. I will look at PolygonOperator class asap, looks promising, keep you posted.
 .
Btw. the change from "move" to "moved" helps already a lot in terms of runtime