Search code examples
multithreadingluatorch

Torch threads - Transfer data between main and threads once


So I have some threads where I would like to pass an upvalue called reset to each thread once every so often in order to reset each thread's table's values. I then want to switch off the reset until the table has finished iterating through its batches. However I have had no success in switching off the reset (reset = false) doesn't seem to stop it from continuously resetting.

for i = 1, n do
    local reset = true
    while true do
            threads:addjob(
               function()
                  if reset = true then f:reset(); reset = false; end
                  x,y = f:getBatch()
                  return x,y
               end,
               function(x,y)
                  -- do some stuff and trigger conditionMet = true if met
               end
            )
            if conditionMet == true break end

    end
end

Solution

  • Your upvalue "reset" here is read-only. The thread serializes "reset" and read it. So every iteration in the "while" loop, reset is read and serialized again by threads:addjob.

    What you seem to want instead is to break it down this way:

    for i = 1, n do
    threads:addjob(
               function()
                  __resetflag = true
                end
    )
    while true do
            threads:addjob(
               function()
                  if __resetflag == true then f:reset(); __resetflag = false; end
                  x,y = f:getBatch()
                  return x,y
               end,
               function(x,y)
                  -- do some stuff and trigger conditionMet = true if met
               end
            )
            if conditionMet == true break end
    
    end
    end