How could I iterate over a set of objects and perform a function on them in parallel?

Title is pretty self explanatory. I’m making a raytraced lighting system that operates on a (downscaled resolution) per-pixel basis using frames, so I need a way to iterate over and perform actions on the frames in a parallel manner. The developer documentation for parallel LuaU stumps me, so i figured i would ask here for an example.

2 Likes

Forgive me for the rough code, but just based on my understanding of what you want:

local function itemFunction(itemParameter)
    --insert item function
end

for _, item in ipairs(listOfObjects) do
     coroutine.wrap(itemFunction(item))() --runs the function parallel
end
1 Like

How would I do this using Lua actors? Since coroutines aren’t truly multithreaded.

1 Like

Please note I am a little new to actors myself but this seems to be the gist of it.

--creating an actor and worker script in main script 
local actor = Instance.new("Actor")
actor.parent = workspace

local worker = path.to.ray.script:Clone()
worker.parent = actor

actor:SendMessage("Run", tupleOfParameters)

--worker script 
local mod = require(path.to.module) --if needed do this before desynchronization 

local function longComplexFunction(tupleOfParameters)
   task.desynchronise
   --do work, read values outside of actor only, write within actor instance children 

  task.synchronise
  --return results, write to values outside of actor
end

script:GetActor():BindToMessage("Run", longComplexFunction)

As far as I know there is no limit to the number of actors you can have, but obviously there is to the number of possible threads which can be processed simultaneously, which would have to queue.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.