Seed random number generator when using pmap


I am trying to use pmap to increase the speed of my code. And I try to seed the random generator in different processors, so I have a code like

using Distributed

@everywhere using LinearAlgebra
@everywhere using StatsBase
@everywhere using Statistics
@everywhere using Random
@everywhere using StaticArrays
@everywhere using Future

#generate random number generator for each processor
@everywhere const LOCAL_R = Future.randjump(MersenneTwister(123), nprocs())[myid()]

@everywhere function rand_haar2(::Val{n}) where n
    M = randn(LOCAL_R,SMatrix{ComplexF64, n,n}) # = rand(Ginibre(2, n))
    q = qr(M).Q
    L = cispi.(2 .* @SVector(rand(n)))
    return q*diagm(L)

@everywhere function main2(; ps=2, theta = 1.08, Nk = 800, Nr = 10000)
    sum_z =@MVector zeros(Nk)
    sum_zlog =@MVector zeros(Nk)
    sum_s =@MVector zeros(Nk)
    sum_slog =@MVector zeros(Nk)
    Npool = Int(floor(10^ps))
    pool = zeros(Npool)
    pool_sample = zeros(Npool)
    logpool = zeros(Npool)

    Kup= @SMatrix[cos(theta) 0; 0 sin(theta)]
    Kdown = @SMatrix[sin(theta) 0; 0 cos(theta)]
    P2up = kron(@SMatrix[1. 0.;0. 1.], @SMatrix[1 0; 0 0])
    P2down = kron(@SMatrix[1 0;0 1], @SMatrix[0 0;0 1])
    for i in 1:Nr
        pool .= 0.5
        pool_sample .= 0

        for k in 1:Nk
            poolcount = 0
            while poolcount < Npool
                z1 = pool[rand(1:Npool)]
                rho1 = diagm(@SVector[z1,1-z1])

                z2 = pool[rand(1:Npool)]
                rho2 = diagm(@SVector[z2,1-z2])

                u1 = rand_haar2(Val{2}())
                u2 = rand_haar2(Val{2}())

                K1up = u1*Kup*u1'
                K1down = u1*Kdown*u1'
                K2up = u2*Kup*u2'
                K2down = u2*Kdown*u2'

                rho1p = K1up*rho1*K1up'
                rho2p = K2up*rho2*K2up'

                p1 = real(tr(rho1p+rho1p'))/2
                p2 = real(tr(rho2p+rho2p'))/2
                if rand(LOCAL_R)<p1
                    rho1p = (rho1p+rho1p')/(2*p1)
                    rho1p = K1down*rho1*K1down'/((1-p1)) 

                if rand(LOCAL_R)<p2
                    rho2p = (rho2p+rho2p')/(2*p2)
                    rho2p = K2down*rho2*K2down'/((1-p2))
                rho = kron(rho1p,rho2p)

                U = rand_haar2(Val{4}())
                rho_p = P2up*U*rho*U'*P2up'
                p = real(tr(rho_p+rho_p'))/2
                if rand(LOCAL_R)<p
                    temp =(rho_p+rho_p')/2
                    #rho_f = @SMatrix[tr(temp[1:2,1:2]) tr(temp[1:2,3:4]); tr(temp[3:4,1:2]) tr(temp[3:4,3:4])]/(p)
                    rho_f = @SMatrix[temp[1,1]+temp[2,2] temp[1,3]+temp[2,4]; temp[3,1]+temp[4,2] temp[3,3]+temp[4,4]]/(p)
                    temp = P2down*U*rho*U'*P2down'
                    rho_f = @SMatrix[temp[1,1]+temp[2,2] temp[1,3]+temp[2,4]; temp[3,1]+temp[4,2] temp[3,3]+temp[4,4]]/(1-p)
                rho_f = (rho_f+rho_f')/2
                t = abs(tr(rho_f*rho_f))
                z = (1-t)/(1+sqrt(2*t-1))
                if !iszero(abs(z))
                    poolcount = poolcount+1
                    pool_sample[poolcount] = abs(z)

            pool .= pool_sample
            pool_sample .= 0

            logpool .=log.(pool)

            sum_z[k] += mean(pool)
            sum_zlog[k] += mean(logpool)/sqrt(Npool)
            sum_s[k] += (std(pool)/sqrt(Npool))^2
            sum_slog[k] += (std(logpool)/sqrt(Npool))^2

    return sum_z/Nr, sqrt.(sum_s)/Nr, sum_zlog/Nr, sqrt.(sum_slog)/Nr

@time ret = pmap(main2,Nr = [2500,2500,2500,2500])

But when I try to run this code, I got an error

ERROR: On worker 2:
MethodError: no method matching getindex(::MersenneTwister, ::Int64)
   @ Distributed D:\Julia-1.7.2\share\julia\stdlib\v1.7\Distributed\src\macros.jl:219
 [4] top-level scope
   @ D:\Julia-1.7.2\share\julia\stdlib\v1.7\Distributed\src\macros.jl:203

Also, I have some keywords arguments in function main2. Nr =10000 represent the total number of iterations of one loop in the function. Now I want to split this loop to four processors and each one does just 2500 iterations. So I try @time ret = pmap(main2,Nr = [2500,2500,2500,2500]), is this correct or not?

I am sorry that I know very little about the parallel computing in Julia. I tried to read the manual, but I still don’t understand how it works.


Could you not just seed each process in main2?

@everywhere function main2(; ps=2, theta = 1.08, Nk = 800, Nr = 10000, processID)
    Random.seed!(seedvalue) # pass in the process ID here  (see additional argument)

Yes I think this is also a possible way. I will try it thanks.