Hi Guys, I am using @thread for my model parallelisation. However, it yields different results compared to serial for-loop. Here is my script
function batch_cost_function(p)
num_params = size(p, 2) # Number of parameter sets
out = zeros(num_params) # Store cost function values
Threads.@threads for i in 1:num_params
out[i] = getLoss(p[:, i], info.models.forward, run_helpers.space_forcing,
run_helpers.space_spinup_forcing, run_helpers.loc_forcing_t,
run_helpers.output_array, run_helpers.space_output,
run_helpers.space_land, run_helpers.tem_info,
obs_array, tbl_params, cost_options,
info.optimization.multi_constraint_method)
println(p[:, i])
println(out[i])
end
return out
end
function batch_cost_function_serial(p)
num_params = size(p, 2)
out = zeros(num_params)
for i in 1:num_params # No threading
out[i] = getLoss(p[:, i], info.models.forward, run_helpers.space_forcing,
run_helpers.space_spinup_forcing, run_helpers.loc_forcing_t,
run_helpers.output_array, run_helpers.space_output,
run_helpers.space_land, run_helpers.tem_info,
obs_array, tbl_params, cost_options,
info.optimization.multi_constraint_method)
println(p[:, i])
println(out[i])
end
return out
end
out_serial_small = batch_cost_function_serial(A_small);
out_thread_small = batch_cost_function(A_small);
In my function getLoss
, input arguments except for p
(within the for-loop) are all global variables. If I tried it with normal for loop, then out
is correct. However, if I tried it with @threads, results are different…here are running results (use only 3 for-loops):
why is that? Any suggestions are appreciated! Thanks!