arrays - 并行数据处理

标签 arrays parallel-processing julia

我有一个大矩阵数据,我想以某种方式“组织”它。该矩阵有 5 列,约 200 万行。前 4 列是每个观察的特征(这些是整数),最后一列是我感兴趣的结果变量(包含实数)。我想将这个矩阵组织在数组数组中。由于 data 非常大,我正在尝试并行化此操作:

addprocs(3)

@everywhere data = readcsv("datalocation", Int)

@everywhere const Z = 65
@everywhere const z = 16
@everywhere const Y = 16
@everywhere const y = 10

@everywhere const arr = Array{Vector}(Z-z+1,Y-y+1,Z-z+1,Y-y+1)

@parallel (vcat) for a1 in z:Z, e1 in y:Y, a2 in z:Z, e2 in y:Y
    arr[a1-z+1,e1-y+1,a2-z+1,e2-y+1] = data[(data[:,1].==a1) & (data[:,2].==e1) & (data[:,3].==a2) & (data[:,4].==e2), end]
end

但是,当我尝试运行 for 循环时出现错误:

Error: syntax: invalid assignment location

循环完成后,我希望所有处理器都可以使用arr。我做错了什么?

编辑: 输入矩阵 data 如下所示(行无特定顺序):

16   10   16   10   100
16   10   16   11   200
20   12   21   13   500
16   10   16   10   300
20   12   21   13   500

请注意,某些行可以重复,而其他一些行将具有相同的“键”,但第五列不同。

我想要的输出如下所示(注意我如何使用 arr 的维度作为“字典”的“键”:

arr[16-z+1, 10-y+1, 16-z+1, 10-y+1] = [100, 300]
arr[16-z+1, 10-y+1, 16-z+1, 11-y+1] = [200]
arr[20-z+1, 12-y+1, 21-z+1, 13-y+1] = [500, 500]

arr中索引(16-z+1, 10-y+1, 16-z+1, 10-y+1)处的元素> 是向量[100, 300]。我不关心行的顺序或向量最后一列的顺序。

最佳答案

这对你有用吗?我尝试通过重复您提供的片段 1000 次来模拟您的数据。它并不像我想要的那么优雅,特别是,我无法完全让 remotecall_fetch() 按我想要的方式工作(即使用 @async 包装它)所以我不得不将调用和获取分成两个步骤。让我知道这看起来如何。

addprocs(n)

@everywhere begin
    if myid() != 1
        multiplier = 10^3;
        Data = readdlm("/path/to/Input.txt")
        global data = kron(Data,ones(multiplier));
        println(size(data))
    end
end

@everywhere begin
    function Select_Data(a1, e1, a2, e2, data=data)
        return data[(data[:,1].==a1) & (data[:,2].==e1) & (data[:,3].==a2) & (data[:,4].==e2), end]
    end
end

n_workers = nworkers()
function next_pid(pid, n_workers)
    if pid <= n_workers
        return pid + 1
    else
        return 2
    end
end

const arr = Array{Any}(Z-z+1,Y-y+1,Z-z+1,Y-y+1);
println("Beginning Processing Work")
@sync begin
    pid = 2
    for a1 in z:Z, e1 in y:Y, a2 in z:Z, e2 in y:Y
        pid = next_pid(pid, n_workers)
        arr[a1-z+1,e1-y+1,a2-z+1,e2-y+1] = remotecall(pid, Select_Data, a1, e1, a2, e2)
    end
end
println("Retrieving Completed Jobs")
@sync begin
    pid = 2
    for a1 in z:Z, e1 in y:Y, a2 in z:Z, e2 in y:Y
        arr[a1-z+1,e1-y+1,a2-z+1,e2-y+1] = fetch(arr[a1-z+1,e1-y+1,a2-z+1,e2-y+1])
    end
end

关于arrays - 并行数据处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38448323/

相关文章:

Javascript 替换数组中的每 6 个冒号

c++ - 将结构转换为 uint8_t 的 constexpr 数组

java - Stream reduce() 要求到底包含什么?

julia - Julia 中的空数据框添加行

audio - Julia 中用于音频处理的实时 STFT 和 ISTFT

arrays - MATLAB中没有for循环的多个数组的交集

arrays - 哪个 haskell 库可以让我将 2D 数组/向量保存到 png/jpg/gif... 文件中?

haskell - 比这更通用的 parfoldr

c - 如何修改此组合算法以在启用 cuda 的 gpu 上并行运行?

julia,线性代数,是否有一个函数可以找到与给定向量正交的所有向量?