如何在 Mathematica 8 中并行集成

发布于 2024-12-14 00:34:39 字数 387 浏览 4 评论 0原文

有人知道如何使用所有核心来计算积分吗?我需要使用并行化或并行表,但如何使用?

 f[r_] := Sum[(((-1)^n*(2*r - 2*n - 7)!!)/(2^n*n!*(r - 2*n - 1)!))*
 x^(r - 2*n - 1), {n, 0, r/2}]; 


 Nw := Transpose[Table[f[j], {i, 1}, {j, 5, 200, 1}]]; 

 X1 = Integrate[Nw . Transpose[Nw], {x, -1, 1}]; 

 Y1 = Integrate[D[Nw, {x, 2}] . Transpose[D[Nw, {x, 2}]], {x, -1, 1}]; 

 X1//MatrixForm
 Y1//MatrixForm

Somebody have idea how to use all cores for calculating integration? I need to use parallelize or parallel table but how?

 f[r_] := Sum[(((-1)^n*(2*r - 2*n - 7)!!)/(2^n*n!*(r - 2*n - 1)!))*
 x^(r - 2*n - 1), {n, 0, r/2}]; 


 Nw := Transpose[Table[f[j], {i, 1}, {j, 5, 200, 1}]]; 

 X1 = Integrate[Nw . Transpose[Nw], {x, -1, 1}]; 

 Y1 = Integrate[D[Nw, {x, 2}] . Transpose[D[Nw, {x, 2}]], {x, -1, 1}]; 

 X1//MatrixForm
 Y1//MatrixForm

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

前事休说 2024-12-21 00:34:40

我将列表的集成更改为集成列表,以便可以使用 ParallelTable

X1par=ParallelTable[Integrate[i, {x, -1, 1}], {i, Nw.Transpose[Nw]}];

X1par==X1

(* ===> True *)

Y1par = ParallelTable[Integrate[i,{x,-1,1}],{i,D[Nw,{x,2}].Transpose[D[Nw,{x,2}]]}]

Y1 == Y1par

(* ===> True *)

在我的时间安排中,使用 {j, 5, 30, 1} 而不是 < code>{j, 5, 200, 1} 来限制使用的时间,这在我的 quod-core 上大约快了 3.4 倍。但它可以更快地完成:

X2par = Parallelize[Integrate[#, {x, -1, 1}] & /@ (Nw.Transpose[Nw])]

X2par == X1par == X1

(* ===> True *)

这大约快了 6.8 倍,其中 2.3 倍是由于 Parallelize

当涉及并行执行时,TimingAbsoluteTiming 不太值得信赖。我在每行之前和之后使用了 AbsoluteTime 并获取了差异。


编辑

我们不应该忘记 ParallelMap:

在最粗的列表级别 (1):

ParallelMap[Integrate[#, {x, -1, 1}] &, Nw.Transpose[Nw], {1}]  

在最深的列表级别(最细粒度的并行化):

ParallelMap[Integrate[#, {x, -1, 1}] &, Nw.Transpose[Nw], {2}]

I changed the integration of a list into a list of integrations so that I can use ParallelTable:

X1par=ParallelTable[Integrate[i, {x, -1, 1}], {i, Nw.Transpose[Nw]}];

X1par==X1

(* ===> True *)

Y1par = ParallelTable[Integrate[i,{x,-1,1}],{i,D[Nw,{x,2}].Transpose[D[Nw,{x,2}]]}]

Y1 == Y1par

(* ===> True *)

In my timings, with {j, 5, 30, 1} instead of {j, 5, 200, 1} to restrict the time used somewhat, this is about 3.4 times faster on my quod-core. But it can be done even faster with:

X2par = Parallelize[Integrate[#, {x, -1, 1}] & /@ (Nw.Transpose[Nw])]

X2par == X1par == X1

(* ===> True *)

This is about 6.8 times faster, a factor of 2.3 of which is due to Parallelize.

Timing and AbsoluteTiming are not very trustworthy when parallel execution is concerned. I used AbsoluteTime before and after each line and took the difference.


EDIT

We shouldn't forget ParallelMap:

At the coarsest list level (1):

ParallelMap[Integrate[#, {x, -1, 1}] &, Nw.Transpose[Nw], {1}]  

At the deepest list level (most fine-grained parallelization):

ParallelMap[Integrate[#, {x, -1, 1}] &, Nw.Transpose[Nw], {2}]
瀞厅☆埖开 2024-12-21 00:34:40

如果首先通过扩展矩阵元素来帮助积分,
只要付出一点努力,事情就可以完成。

在装有 Windows 和 Mathematica 8.0.4 的四核笔记本电脑上,运行以下代码
对于要求的 DIM=200 在大约 13 分钟内,
对于 DIM=50,代码运行时间为 6 秒。


$starttime = AbsoluteTime[]; Quiet[LaunchKernels[]]; 
DIM = 200; 
Print["$Version = ", $Version, "  |||  ", "Number of Kernels : ", Length[Kernels[]]]; 
f[r_] := f[r] = Sum[(((-1)^n*(-(2*n) + 2*r - 7)!!)*x^(-(2*n) + r - 1))/(2^n*n!*(-(2*n) + r - 1)!), {n, 0, r/2}]; 
Nw = Transpose[Table[f[j], {i, 1}, {j, 5, DIM, 1}]]; 
nw2 = Nw . Transpose[Nw]; 
Print["Seconds for expanding Nw.Transpose[Nm] ", Round[First[AbsoluteTiming[nw3 = ParallelMap[Expand, nw2]; ]]]]; 
Print["do the integral once: ", Integrate[x^n, {x, -1, 1}, Assumptions -> n > -1]]; 
Print["the integration can be written as a simple rule: ", intrule = (pol_Plus)?(PolynomialQ[#1, x] & ) :> 
     (Select[pol,  !FreeQ[#1, x] & ] /. x^(n_.) /; n > -1 :> ((-1)^n + 1)/(n + 1)) + 2*(pol /. x -> 0)]; 
Print["Seconds for integrating Nw.Transpose[Nw] : ", Round[First[AbsoluteTiming[X1 = ParallelTable[row /. intrule, {row, nw3}]; ]]]]; 
Print["expanding: ", Round[First[AbsoluteTiming[preY1 = ParallelMap[Expand, D[Nw, {x, 2}] . Transpose[D[Nw, {x, 2}]]]; ]]]]; 
Print["Seconds for integrating : ", Round[First[AbsoluteTiming[Y1 = ParallelTable[py /. intrule, {py, preY1}]; ]]]]; 
Print["X1 = ", (Shallow[#1, {4, 4}] & )[X1]]; 
Print["Y1 = ", (Shallow[#1, {4, 4}] & )[Y1]]; 
Print["seq Y1 : ", Simplify[FindSequenceFunction[Diagonal[Y1], n]]]; 
Print["seq X1 0 : ",Simplify[FindSequenceFunction[Diagonal[X1, 0], n]]]; 
Print["seq X1 2: ",Simplify[FindSequenceFunction[Diagonal[X1, 2], n]]]; 
Print["seq X1 4: ",Simplify[FindSequenceFunction[Diagonal[X1, 4], n]]]; 
Print["overall time needed in seconds: ", Round[AbsoluteTime[] - $starttime]]; 

If one helps Integrate a bit by expanding the matrix elements first,
things are doable with a little bit of effort.

On a quad-core laptop with Windows and Mathematica 8.0.4 the following code below runs
for the asked DIM=200 in about 13 minutes,
for DIM=50 the code runs in 6 second.


$starttime = AbsoluteTime[]; Quiet[LaunchKernels[]]; 
DIM = 200; 
Print["$Version = ", $Version, "  |||  ", "Number of Kernels : ", Length[Kernels[]]]; 
f[r_] := f[r] = Sum[(((-1)^n*(-(2*n) + 2*r - 7)!!)*x^(-(2*n) + r - 1))/(2^n*n!*(-(2*n) + r - 1)!), {n, 0, r/2}]; 
Nw = Transpose[Table[f[j], {i, 1}, {j, 5, DIM, 1}]]; 
nw2 = Nw . Transpose[Nw]; 
Print["Seconds for expanding Nw.Transpose[Nm] ", Round[First[AbsoluteTiming[nw3 = ParallelMap[Expand, nw2]; ]]]]; 
Print["do the integral once: ", Integrate[x^n, {x, -1, 1}, Assumptions -> n > -1]]; 
Print["the integration can be written as a simple rule: ", intrule = (pol_Plus)?(PolynomialQ[#1, x] & ) :> 
     (Select[pol,  !FreeQ[#1, x] & ] /. x^(n_.) /; n > -1 :> ((-1)^n + 1)/(n + 1)) + 2*(pol /. x -> 0)]; 
Print["Seconds for integrating Nw.Transpose[Nw] : ", Round[First[AbsoluteTiming[X1 = ParallelTable[row /. intrule, {row, nw3}]; ]]]]; 
Print["expanding: ", Round[First[AbsoluteTiming[preY1 = ParallelMap[Expand, D[Nw, {x, 2}] . Transpose[D[Nw, {x, 2}]]]; ]]]]; 
Print["Seconds for integrating : ", Round[First[AbsoluteTiming[Y1 = ParallelTable[py /. intrule, {py, preY1}]; ]]]]; 
Print["X1 = ", (Shallow[#1, {4, 4}] & )[X1]]; 
Print["Y1 = ", (Shallow[#1, {4, 4}] & )[Y1]]; 
Print["seq Y1 : ", Simplify[FindSequenceFunction[Diagonal[Y1], n]]]; 
Print["seq X1 0 : ",Simplify[FindSequenceFunction[Diagonal[X1, 0], n]]]; 
Print["seq X1 2: ",Simplify[FindSequenceFunction[Diagonal[X1, 2], n]]]; 
Print["seq X1 4: ",Simplify[FindSequenceFunction[Diagonal[X1, 4], n]]]; 
Print["overall time needed in seconds: ", Round[AbsoluteTime[] - $starttime]]; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文