我从Queue.get()中得到什么(Python)

我从Queue.get()中得到什么(Python),python,python-3.x,Python,Python 3.x,总体问题:调用Queue.get()时,如何知道从队列对象中得到了什么?如何对其进行排序或识别?您是否可以从队列中获取特定项目并留下其他项目 上下文: 我想学习一点关于多进程(线程?)的知识,以提高求解矩阵方程的效率 下面是我的工作代码,用于在不利用多核的情况下求解矩阵方程Ax=b。解是[1,1,1] def jacobi(A, b, x_k): N = len(x_k) x_kp1 = np.copy(x_k) E_rel = 1 iteration = 0

总体问题:调用Queue.get()时,如何知道从队列对象中得到了什么?如何对其进行排序或识别?您是否可以从队列中获取特定项目并留下其他项目

上下文:

我想学习一点关于多进程(线程?)的知识,以提高求解矩阵方程的效率

下面是我的工作代码,用于在不利用多核的情况下求解矩阵方程Ax=b。解是[1,1,1]

def jacobi(A, b, x_k):

    N = len(x_k)
    x_kp1 = np.copy(x_k)
    E_rel = 1
    iteration = 0

    if (N != A.shape[0] or N != A.shape[1]):
        raise ValueError('Matrix/vector dimensions do not match.')

    while E_rel > ((10**(-14)) * (N**(1/2))):
        for i in range(N):

            sum = 0

            for j in range(N):
                if j != i:
                    sum = sum + A[i,j] * x_k[j]

            x_kp1[i] =(1 / A[i,i]) * (b[i] - sum)

        E_rel = 0
        for n in range(N):
            E_rel = E_rel + abs(x_kp1[n] - x_k[n]) / ((abs(x_kp1[n]) + abs(x_k[n])) / 2)

        iteration += 1
        # print("relative error for this iteration:", E_rel)
        if iteration < 11:
            print("iteration ", iteration, ":", x_kp1)

        x_k = np.copy(x_kp1)

    return x_kp1

if __name__ == '__main__':

    A = np.matrix([[12.,7,3],[1,5,1],[2,7,-11]])
    b = np.array([22.,7,-2])
    x = np.array([1.,2,1])

    print("Jacobi Method:")
    x_1 = jacobi(A, b, x)
使向量的元素混乱

以下是我使用多处理库更新的代码:

import numpy as np
import multiprocessing as mu

np.set_printoptions(precision=15)

def Jacobi_step(index, initial_vector, q):
    N = len(initial_vector)
    sum = 0
    for j in range(N):
        if j != i:
            sum = sum + A[i, j] * initial_vector[j]

    # this result is the updated element at given index of our solution vector.
    q.put((1 / A[index, index]) * (b[index] - sum))


if __name__ == '__main__':

    A = np.matrix([[12.,7,3],[1,5,1],[2,7,-11]])
    b = np.array([22.,7,-2])
    x = np.array([1.,2,1])
    q = mu.Queue()
    N = len(x)
    x_update = np.copy(x)
    p = []
    error = 1
    iteration = 0

    while error > ((10**(-14)) * (N**(1/2))):

        # assign a process to each element in the vector x, 
        # update one element with a single Jacobi step
        for i in range(N):
            process = mu.Process(target=Jacobi_step(i, x, q))
            p.append(process)
            process.start()

        # fill in the updated vector with each new element aquired by the last step
        for i in range(N):
            x_update[i] = q.get(True)

        # check for convergence 
        error = 0
        for n in range(N):
            error = error + abs(x_update[n] - x[n]) / ((abs(x_update[n]) + abs(x[n])) / 2)
            p[i].join()

        x = np.copy(x_update)

        iteration += 1
        print("iteration ", iteration, ":", x)
        del p[:]
队列是先进先出的,这意味着插入的第一个元素是检索到的第一个元素,按插入顺序排列

由于您无法控制这一点,我建议您在队列中插入元组,其中包含值和一些可用于排序/关联原始计算的标识对象

result = (1 / A[index, index]) * (b[index] - sum)
q.put((index, result))
本例将索引与结果一起放入
队列
,以便稍后当您
.get()
时,也会获得索引,并使用它来了解这是用于哪种计算:

i, x_i = q.get(True)
x_update[i] = x_i
或者类似的

i, x_i = q.get(True)
x_update[i] = x_i