主要内容

Use Distributed Arrays to Solve Systems of Linear Equations with Direct Methods

分布式阵列非常适合大型数学计算,例如线性代数的大问题。此示例显示了如何求解表单的线性方程系统 A x = b in parallel with a direct method using distributed arrays. In the same way as for arrays stored in the client memory, you can usemldivideto solve can systems of linear equations defined using distributed arrays, so you do not need to change your code.

分布式阵列distribute data from your client workspace to a parallel pool in your local machine or in a cluster. Each worker stores a portion of the array in its memory, but can also communicate with the other workers to access all segments of the array. Distributed arrays can contain different types of data including full and sparse matrices.

Direct methods of solving linear equations typically factorize the coefficient matrix A 计算解决方案。mldivideselects one of a set of direct solver methods depending on the structure of A 是否 A 饱满或稀疏。

此示例演示了如何求解形式的线性方程的简单系统 Ax = b with an exact, known solution x 。The system is defined by the matrix A 和列矢量 b 。The solution x 也是列矢量。在此示例中,系统是使用完整和稀疏矩阵定义的。对于使用分布式数组或客户端存储器上定义的系统,所需的代码相同。

有关如何使用迭代求解器和分布式数组的相关示例,请参见使用分布式阵列用迭代方法求解线性方程的系统

Solve a Full Matrix System

First define the coefficient matrix A 作为客户端内存中的变量,A, and then pass this matrix to thedistributedfunction to create a distributed version of the same matrix,ADist。当您使用distributed功能,MATLAB会使用您的默认群集设置自动启动并行池。

n = 1e3; A = randi(100,n,n); ADist = distributed(A);

You can now define the right hand vector b 。In this example, b is defined as the row sum of A , which leads to an exact solution to A x = b 形式 x exact = [ 1 , , 1 ] T

b = sum(a,2);bdist = sum(Adist,2);

自从sum作用于分布式阵列,bDistis also distributed and its data is stored in the memory of the workers of your parallel pool. Finally, define the exact solutions for comparison with the solutions obtained using direct numerical methods.

Xex =一个(n,1);xdistex =一个(n,1,'distributed');

Now that you have defined your system of linear equations, you can usemldivideto solve the system directly. In MATLAB, you can callmldivideusing the special operator\。You do not have to change your code to solve the distributed system asmldivide对分布式阵列有自动支持。万博1manbetx

Once you have calculated the solution, you can check the error between each element of the obtained result x 和the expected values of x exact

x = A\b; err = abs(xEx-x); xDist = ADist\bDist; errDist = abs(xDistEx-xDist); figure subplot(2,1,1) semilogy(err,'o');标题('System of Linear Equations with Full Matrices');ylabel(“绝对错误”);xlabel(X中的元素);Ylim([10E-17,10E-13])子图(2,1,2)半学(errdist,'o');标题('System of Linear Equations with Distributed Full Matrices');ylabel(“绝对错误”);xlabel(X中的元素);ylim ([10 e-17, e-13])

For both the distributed arrays and the arrays stored on the client, the absolute error between the calculated results for x 和确切的结果 x exact 是小。对于两种阵列类型,解决方案的精度大致相同。

mean(err)
ANS = 1.6031E-13
mean(errDist)
ANS = 1.2426E-13

Solve a Sparse Matrix System

Distributed arrays can also contain sparse data. To create the coefficient matrix A , 利用sprandspeye直接生成一个随机数的稀疏矩阵加上稀疏的身份矩阵。添加身份矩阵有助于防止创建 A 作为一个单数或近乎单位的矩阵,两者都难以分解。

n = 1e3; density = 0.2; A = sprand(n,n,density) + speye(n); ADist = distributed(A);

Choosing the right hand vector b as the row sum of A 产生与完整矩阵系统解决方案相同形式的精确解。

b = sum(a,2);bdist = sum(Adist,2);Xex =一个(n,1);xdistex =一个(n,1,'distributed');

In the same way as with full matrices, you can now solve this system of linear equations directly usingmldivide和check the error between the obtained result and its expected value.

x = A\b; err = abs(xEx-x); xDist = ADist\bDist; errDist = abs(xDistEx-xDist); figure subplot(2,1,1) semilogy(err,'o');标题(“具有稀疏矩阵的线性方程系统”);ylabel(“绝对错误”);xlabel(X中的元素);Ylim([10E-17,10E-13])子图(2,1,2)半学(errdist,'o');标题(“具有分布式稀疏矩阵的线性方程系统”);ylabel(“绝对错误”);xlabel(X中的元素);ylim ([10 e-17, e-13])

与完整的矩阵系统一样,使用离线阵列和分布式阵列求解线性方程的系统也会产生具有可比精度的解决方案。万博 尤文图斯

mean(err)
ANS = 1.6031E-13
mean(errDist)
ANS = 1.2426E-13

After you are done with your computations, you can delete your parallel pool. Thegcpfunction returns the current parallel pool object so you can delete the current pool.

delete(gcp('nocreate'));

Improving Efficiency of the Solution

对于某些类型的大和稀疏系数矩阵 A ,与解决系统的直接分解相比,有更有效的方法。在这些情况下,迭代方法在解决线性方程系统方面可能更有效。迭代方法生成一系列近似解决方案,这些解决方案会收敛到最终结果。万博 尤文图斯有关如何使用迭代方法求解具有较大稀疏输入矩阵的线性方程的示例,请参见使用分布式阵列用迭代方法求解线性方程的系统

See Also

||

相关话题