# SharedArray - not updated

**URL:** <https://discourse.julialang.org/t/sharedarray-not-updated/11795>\
**Category:** General Usage\
**Tags:** question, parallel\
**Created:** [June 19, 2018, 8:20pm UTC](https://discourse.julialang.org/t/sharedarray-not-updated/11795 "2018-06-19T20:20:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedro.rafae](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pedro.rafae/32/1173_2.png) [@pedro.rafae](https://discourse.julialang.org/u/pedro.rafae)\
**Post date:** [June 19, 2018, 8:20pm UTC](https://discourse.julialang.org/t/sharedarray-not-updated/11795/1 "2018-06-19T20:20:01Z")

</div>

I have a problem with the `ekwg_monte_carlo_bias` function. In this function there are `SharedArray` which are not updated. Any suggestion?

**Note** : The function works as expected by replacing `@parallel for i in 1:m` with `for i in 1: m` and deleting `addprocs (Sys.CPU_CORES)`.

**Julia Code:**

> addprocs(Sys.CPU\_CORES)  
> @everywhere using Distributions  
> @everywhere using Optim
> 
> @everywhere function gexp(x,par)  
> λ = par[1]  
> λ \* exp(-λ \* x)  
> end
> 
> @everywhere function Gexp(x,par)  
> λ = par[1]  
> 1- exp(-λ \* x)  
> end
> 
> @everywhere function QGexp(x,par)  
> λ = par[1]  
> quantile.(Exponential(1/λ),x)  
> end
> 
> @everywhere function sample\_ekwg(QG, n, par0, par1…)  
> a = par0[1]  
> b = par0[2]  
> c = par0[3]
> 
> ```
> u = rand(n)
> 
> p = (1 - (1 - u.^(1/c)).^(1/b)).^(1/a)
> 
> QG(p, par1...)
> 
> ```
> 
> end
> 
> @everywhere function cdf\_ekwg(cdf, x, par0, par1…)  
> a = par0[1]  
> b = par0[2]  
> c = par0[3]
> 
> (1 - (1 - cdf.(x,par1…).^a).^b).^c  
> end
> 
> @everywhere function pdf\_ekwg(cdf, pdf, x, par0, par1…)  
> a = par0[1]  
> b = par0[2]  
> c = par0[3]
> 
> g = pdf(x, par1…)  
> G = cdf(x, par1…)
> 
> a \* b \* c \* g \* G.^(a-1) \* (1-G.^a).^(b-1) \* (1 - (1-G.^a).^b).^(c-1)
> 
> end
> 
> @everywhere function loglike(cdf, pdf, x, par0, par1…)  
> n = length(x)  
> soma = 0  
> for i = 1:n  
> soma += log(pdf\_ekwg(cdf, pdf, x[i], par0, par1…))  
> end  
> return -soma  
> end
> 
> @everywhere function myoptimize(sample\_boot)  
> try  
> optimize(par0 → loglike(G, g, sample\_boot, par0, par1…), starts,  
> Optim.Options(g\_tol = 1e-2))  
> catch  
> -1  
> end  
> end
> 
> @everywhere function ekwg\_bootstrap\_bias(B, G, g, data, original\_estimates, starts, par1…)
> 
> result\_boot = SharedArray{Float64}(length(original\_estimates)\*B)
> 
> j = 1  
> while j \<= B  
> sample\_boot = sample(data, length(data), replace = true)
> 
> ```
> result = myoptimize(sample_boot)
> 
> if (result == -1) || (result.g_converged == false)
> continue
> end
> result_boot[(3*j-2):3*j] = result.minimizer
> j = j+1
> 
> ```
> 
> end  
> estimates\_matrix = convert.(Float64,reshape(result\_boot,length(starts),B))’
> 
> error = std(estimates\_matrix,1)
> 
> return error, (2.\*original\_estimates’ .- mean(estimates\_matrix,1))’  
> end
> 
> function ekwg\_monte\_carlo\_bias(M, B, n, true\_parameters, seed, par1…)
> 
> ```
> result_mc_correct_vector = SharedArray{Float64}(length(true_parameters)*M)
> result_mc_vector = SharedArray{Float64}(length(true_parameters)*M)
> result_error_boot = SharedArray{Float64}(length(true_parameters)*M)
> 
> #for i in 1:M
> #Threads.@threads
> 
> @sync @parallel for i in 1:M
> true_sample = sample_ekwg(QGexp, n, true_parameters, par1...)
> result_mc = myoptimize(true_sample)
> 
> if result_mc != -1
> result_mc_vector[(3*i-2):3*i] = result_mc.minimizer
> result_error_boot[(3*i-2):3*i],result_mc_correct_vector[(3*i-2):3*i] = ekwg_bootstrap_bias(b, g, g, true_sample,
> result_mc.minimizer, true_parameters, par1...)
> end
> end
> 
> output1 = convert.(Float64,reshape(result_mc_vector,length(true_parameters),M))'
> output2 = convert.(Float64,reshape(result_mc_correct_vector,length(true_parameters),M))'
> output3 = convert.(Float64,reshape(result_error_boot,length(true_parameters),M))'
> return (mean(output1,1),mean(output2,1),mean(output3,1))
> 
> ```
> 
> end
> 
> true\_parameters = [1.0,1.0,1.0];  
> par1 = 1.5;  
> m = 10;  
> b = 50;  
> n = 100;
> 
> @time mc\_estimates, mc\_estimates\_boot, mc\_error\_boot = ekwg\_monte\_carlo\_bias(m, b, n, true\_parameters, 0, par1)
