【发布时间】:2021-02-19 22:05:02
【问题描述】:
我有以下伪代码:
for ( int i = 0; i < V ; i++ )
{
for( int j = 0 ; j < V ; j++ )
{
if( ( i != j ) && ( tuple {i,j} belong to E ) )
{
R[i] := {i,j};
}
}
}
我想使用erlang 并行化这段代码。
如何使用 Erlang 实现相同的目标? 我是 Erlang 新手...
编辑:
我知道以下代码同时运行对say/2 的调用:
-module(pmap).
-export([say/2]).
say(_,0) ->
io:format("Done ~n");
say(Value,Times) ->
io:format("Hello ~n"),
say(Value,Times-1).
start_concurrency(Value1, Value2) ->
spawn(pmap, say, [Value1, 3]),
spawn(pmap, say, [Value2, 3]).
但是,我们在这里对函数进行硬编码。那么,假设我想调用say 1000 次,我需要写spawn(pmap, say, [Valuex, 3]) 1000 次吗?我可以使用递归,但它不会提供顺序性能吗?
编辑:
我尝试了以下代码,我的目标是创建 3 个线程,每个线程都想运行一个 say 函数。我想同时运行这 3 个 say 函数(请在框中评论以获得更多说明):
-module(pmap).
-export([say/1,test/1,start_concurrency/1]).
say(0) ->
io:format("Done ~n");
say(Times) ->
io:format("Hello ~p ~n",[Times]),
say(Times-1).
test(0) ->
spawn(pmap, say, [3]);
test(Times) ->
spawn(pmap, say, [3]),
test(Times-1).
start_concurrency(Times) ->
test(Times).
这段代码正确吗?
【问题讨论】:
标签: erlang parallel-processing erlang distributed-computing distributed-system erlang-otp