用于计算PI的并行程序 [英] Parallel program to calculate PI
问题描述
大家好,
我有下面的伪代码,我想转换成c
语言。该算法计算Pi值。我对C语言有点熟悉
,但我刚开始学习并行编程。
在这个具体的例子中,这个想法似乎很简单:一个必须
将主循环细分为可由
独立任务执行的部分。 (计算机??)。
然后,每个工人任务执行循环的一部分一定数量
次,独立于其他工作任务。一个特定的任务
扮演主任务的角色,它将收集并总结工人任务的
结果:
%描述性算法:
1.在广场内刻一个圆圈
2.在广场内生成随机点
3.确定落入圈内的点数
4.设r为圆内点数除以
总点数
5. Pi大约等于4 * r
6.生成的点越多,P值的精度越高
%pseudo - 代码(并行):
1. npoints = 10000
2. circle_count = 0
3. p =任务数量>
4. num = npoints / p
5.找出我是MASTER还是WORKER
6.做j = 1,num
7.在0和1之间生成2个随机数
8. xcoordinate = random1
9. ycoordinate = random2
10。如果(xcoordi nate,ycoordinate)在圈内然后
circle_count = circle_count + 1
11.结束做
12.如果我是MASTER
13.从工人那里收到他们的circle_counts
14.计算PI(使用MASTER和WORKER计算)
15.否则如果我是工人
16.发送给MASTER circle_count
17. endif
任何帮助都将不胜感激。
我事先感谢你们。
Hello all,
I have got the pseudo-code below that I would like to convert to c
language. The algorithm calculates Pi value. I am somewhat familiar
with C language, but I am just starting to learn parallel programming.
In this specific example, the idea seems to be simple: one must
subdivide the main loop into pieces that can be executed by
independent "tasks" (computers??).
Then, each "worker task" executes a part of the loop a certain number
of times, independently of the other worker tasks. One specific task
plays the role of "master task", which will collect and sum the
results of the worker tasks:
% descriptive algorithm:
1. Inscribe a circle inside a square
2. Generate random points inside the square
3. Determine the number of points that fell inside the circle
4. Let r be the number of points inside the circle divided by the
total number of points
5. Pi is approximately equal to 4*r
6. The more points are generated, the more is the precision in P value
% pseudo-code (parallel):
1. npoints = 10000
2. circle_count = 0
3. p = number of tasks
4. num = npoints/p
5. find out if I am MASTER or WORKER
6. do j = 1,num
7. generate 2 random numbers between 0 and 1
8. xcoordinate = random1
9. ycoordinate = random2
10. if (xcoordinate, ycoordinate) inside circle then
circle_count = circle_count + 1
11. end do
12. if I am MASTER
13. receive from WORKERS their circle_counts
14. compute PI (use MASTER and WORKER calculations)
15. else if I am WORKER
16. send to MASTER circle_count
17. endif
Any help would be much appreciated.
I thank you all in advance.
推荐答案
让我更具体一点:
我的(下面的)问题从伪代码的第5行开始:
5.找出我是主人还是工人
我如何具体说明一个工人?那会是另一台电脑吗?
如果是的话,我怎样才能在计算中访问这台远程计算机?
C语言?
如果是的话,这意味着我必须要有局域网或其他东西来执行
测试?
我发现有一些库,如OMP或MPI
可能是
使用,但我想知道是否有更多的原始这样做的方式
优先。
再次,谢谢大家。
Let me just be a bit more specific:
My (understading) problem starts in the line 5 of the pseudo-code:
5. find out if I am MASTER or WORKER
How would I specificy a "worker"? Would that be another computer?
If yes, how can I access this remote computer in the calculations, in
C language?
If yes, it means that I have to have a LAN or something to perform
tests?
I have found that there are some libraries such as OMP or MPI that
could be
used, but I''d like to know if there is a more "raw" way of doing this
first.
Again, thank you all.
5月13日15:20,Prime Mover< eple ... @ hotmail.comwrote:
On 13 May, 15:20, Prime Mover <eple...@hotmail.comwrote:
Hello all,
我有下面的伪代码,我想转换成c
语言。该算法计算Pi值。我对C语言有点熟悉
,但我刚开始学习并行编程。
在这个具体的例子中,这个想法似乎很简单:一个必须
将主循环细分为可由
独立任务执行的部分。 (计算机??)。
然后,每个工人任务执行循环的一部分一定数量
次,独立于其他工作任务。一个特定的任务
扮演主任务的角色,它将收集并总结工人任务的
结果:
%描述性算法:
1.在广场内刻一个圆圈
2.在广场内生成随机点
3.确定落入圈内的点数
4.设r为圆内点数除以
总点数
5. Pi大约等于4 * r
6.生成的点越多,P值的精度越高
%pseudo - 代码(并行):
1. npoints = 10000
2. circle_count = 0
3. p =任务数量>
4. num = npoints / p
5.找出我是MASTER还是WORKER
6.做j = 1,num
7.在0和1之间生成2个随机数
8. xcoordinate = random1
9. ycoordinate = random2
10。如果(xcoordina te,ycoordinate)圈内然后
circle_count = circle_count + 1
11.结束做
12.如果我是MASTER
13.从工人那里收到他们的circle_counts
14.计算PI(使用MASTER和WORKER计算)
15.否则如果我是工人
16.发送给MASTER circle_count
17. endif
Hello all,
I have got the pseudo-code below that I would like to convert to c
language. The algorithm calculates Pi value. I am somewhat familiar
with C language, but I am just starting to learn parallel programming.
In this specific example, the idea seems to be simple: one must
subdivide the main loop into pieces that can be executed by
independent "tasks" (computers??).
Then, each "worker task" executes a part of the loop a certain number
of times, independently of the other worker tasks. One specific task
plays the role of "master task", which will collect and sum the
results of the worker tasks:
% descriptive algorithm:
1. Inscribe a circle inside a square
2. Generate random points inside the square
3. Determine the number of points that fell inside the circle
4. Let r be the number of points inside the circle divided by the
total number of points
5. Pi is approximately equal to 4*r
6. The more points are generated, the more is the precision in P value
% pseudo-code (parallel):
1. npoints = 10000
2. circle_count = 0
3. p = number of tasks
4. num = npoints/p
5. find out if I am MASTER or WORKER
6. do j = 1,num
7. generate 2 random numbers between 0 and 1
8. xcoordinate = random1
9. ycoordinate = random2
10. if (xcoordinate, ycoordinate) inside circle then
circle_count = circle_count + 1
11. end do
12. if I am MASTER
13. receive from WORKERS their circle_counts
14. compute PI (use MASTER and WORKER calculations)
15. else if I am WORKER
16. send to MASTER circle_count
17. endif
5月13日15:28,Prime Mover< eple。 .. @ hotmail.comwrote:
On 13 May, 15:28, Prime Mover <eple...@hotmail.comwrote:
让我更具体一点:
我的(有问题的)问题开始了在伪代码的第5行:
5.找出我是主人还是工人
我如何具体说明工人?那会是另一台电脑吗?
如果是的话,我怎样才能在计算中访问这台远程计算机?
C语言?
如果是的话,这意味着我必须要有局域网或其他东西来执行
测试?
我发现有一些库,如OMP或MPI
可能是
使用,但我想知道是否有更多的原始这样做的方式
优先。
Let me just be a bit more specific:
My (understading) problem starts in the line 5 of the pseudo-code:
5. find out if I am MASTER or WORKER
How would I specificy a "worker"? Would that be another computer?
If yes, how can I access this remote computer in the calculations, in
C language?
If yes, it means that I have to have a LAN or something to perform
tests?
I have found that there are some libraries such as OMP or MPI that
could be
used, but I''d like to know if there is a more "raw" way of doing this
first.
标准C没有内置支持并行处理所以
回答
问题" ;看看我是不是MASTER或WORKER落在外面
标准C这就是这里的热门话题。我不知道什么样的硬件设置和扩展到C可以用来解决数字化的b $ b密集型并行算法。也许这里的其他人有相关的b
体验。在Google群组中搜索* parallel *我找到了
comp.parallel.mpi和comp.parallel.pvm我猜你可能会比这里有更多有用的建议。
出于好奇,你的目标是什么价值的npoints?在
你的例子中,它只有100000,你可以在几秒钟内在现代桌面上获得它。
Standard C has no built-in support for parallel processing so an
answer
to the question "find out if I am MASTER or WORKER" falls outside
standard C which is what''s topical here. I have no idea what kind of
hardware set-up and extensions to C can be used to tackle numerically
intensive parallel algorithms. Perhaps others here have the relevant
experience. Searching Google groups for *parallel* I found
comp.parallel.mpi and comp.parallel.pvm where I''m guessing you might
get more useful advice than here.
Out of curiosity for what value of npoints are you aiming for ? In
your example it''s only 100000 and you can get that on a modern desktop
in a few seconds.
>
5月13日,15:28,Prime Mover< eple ... @ hotmail.comwrote:
On 13 May, 15:28, Prime Mover <eple...@hotmail.comwrote:
>
>
让我稍微具体一点:$ b $ b
Let me just be a bit more specific:
My(understading)问题从伪代码的第5行开始:
5.找出我是MASTER还是WORKER
My (understading) problem starts in the line 5 of the pseudo-code:
5. find out if I am MASTER or WORKER
如何我会特定工人吗?那会是另一台电脑吗?
如果是的话,我怎样才能在计算中访问这台远程计算机?
C语言?
如果是的话,这意味着我必须要有局域网或其他东西来执行
测试?
How would I specificy a "worker"? Would that be another computer?
If yes, how can I access this remote computer in the calculations, in
C language?
If yes, it means that I have to have a LAN or something to perform
tests?
我发现有一些库,如OMP或MPI,
可能是
使用,但我想知道是否有更多的原始这样做的方式
优先。
I have found that there are some libraries such as OMP or MPI that
could be
used, but I''d like to know if there is a more "raw" way of doing this
first.
标准C没有内置支持并行处理所以
回答
问题" ;看看我是不是MASTER或WORKER落在外面
标准C这就是这里的热门话题。我不知道什么样的硬件设置和扩展到C可以用来解决数字化的b $ b密集型并行算法。也许这里的其他人有相关的b
体验。在Google群组中搜索* parallel *我找到了
comp.parallel.mpi和comp.parallel.pvm我猜你可能会比这里有更多有用的建议。
Standard C has no built-in support for parallel processing so an
answer
to the question "find out if I am MASTER or WORKER" falls outside
standard C which is what''s topical here. I have no idea what kind of
hardware set-up and extensions to C can be used to tackle numerically
intensive parallel algorithms. Perhaps others here have the relevant
experience. Searching Google groups for *parallel* I found
comp.parallel.mpi and comp.parallel.pvm where I''m guessing you might
get more useful advice than here.
还有comp.parallel
There''s also comp.parallel
这篇关于用于计算PI的并行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!