[英]How can I implement the following loop in Erlang?
I have the following pseudocode:我有以下伪代码:
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};
}
}
}
I want to parallelise this code using erlang .我想使用erlang并行化这段代码。
How can I achieve the same thing using Erlang?如何使用 Erlang 实现相同的功能? I am new to Erlang...
我是 Erlang 的新手...
Edit:编辑:
I know that the following code runs both the calls to say/2
concurrently:我知道以下代码同时运行对
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]).
However, here we are hardcoding the functions.但是,这里我们对函数进行了硬编码。 So, suppose I want to call
say
1000 times, do I need to write spawn(pmap, say, [Valuex, 3])
1000 times?因此,假设我要打电话
say
1000次,我需要写spawn(pmap, say, [Valuex, 3])
1000倍? I can use recursion, but won't it be giving a sequential performance?我可以使用递归,但它不会提供顺序性能吗?
Edit:编辑:
I tried the following code, where I aim to create 3 threads, where each thread wants to run a say function.我尝试了以下代码,我的目标是创建 3 个线程,其中每个线程都想运行一个 say 函数。 I want to run these 3 say functions concurrently(Please comment in the box for more clarification):
我想同时运行这 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).
Is this code correct?这段代码正确吗?
I want to run these 3 say functions concurrently.
我想同时运行这 3 个 say 函数。 Is this code correct?
这段代码正确吗?
You can get rid of your start_concurrency(N)
function because it doesn't do anything.您可以摆脱
start_concurrency(N)
函数,因为它什么也不做。 Instead, you can call test(N)
directly.相反,您可以直接调用
test(N)
。
I aim to create 3 threads
我的目标是创建 3 个线程
In erlang, you create processes
.在 erlang 中,您创建
processes
。
In erlang, indenting is 4 spaces--not 2.在 erlang 中,缩进是 4 个空格——而不是 2 个。
Don't put blank lines between multiple function clauses for a function definition.不要在函数定义的多个函数子句之间放置空行。
If you want to see concurrency in action, then there has to be some waiting in the tasks you are running concurrently.如果您想看到并发运行,那么并发运行的任务中必须有一些等待。 For example:
例如:
-module(a).
-compile(export_all).
say(0) ->
io:format("Process ~p finished.~n", [self()]);
say(Times) ->
timer:sleep(rand:uniform(1000)), %%perhaps waiting to receive data from an http request
io:format("Hello ~p from process ~p~n",[Times, self()]),
say(Times-1).
loop(0) ->
spawn(a, say, [3]);
loop(Times) ->
spawn(a, say, [3]),
loop(Times-1).
In the shell:在外壳中:
3> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}
4> a:loop(3).
<0.84.0>
Hello 3 from process <0.82.0>
Hello 3 from process <0.81.0>
Hello 2 from process <0.82.0>
Hello 3 from process <0.83.0>
Hello 2 from process <0.81.0>
Hello 3 from process <0.84.0>
Hello 2 from process <0.83.0>
Hello 1 from process <0.81.0>
Process <0.81.0> finished.
Hello 1 from process <0.82.0>
Process <0.82.0> finished.
Hello 2 from process <0.84.0>
Hello 1 from process <0.83.0>
Process <0.83.0> finished.
Hello 1 from process <0.84.0>
Process <0.84.0> finished.
5>
If there is no random waiting in the tasks that you are running concurrently, then the tasks will complete sequentially:如果您并发运行的任务中没有随机等待,则任务将按顺序完成:
-module(a).
-compile(export_all).
say(0) ->
io:format("Process ~p finished.~n", [self()]);
say(Times) ->
%%timer:sleep(rand:uniform(1000)),
io:format("Hello ~p from process ~p~n",[Times, self()]),
say(Times-1).
loop(0) ->
spawn(a, say, [3]);
loop(Times) ->
spawn(a, say, [3]),
loop(Times-1).
In the shell:在外壳中:
5> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}
6> a:loop(3).
Hello 3 from process <0.91.0>
Hello 3 from process <0.92.0>
Hello 3 from process <0.93.0>
Hello 3 from process <0.94.0>
<0.94.0>
Hello 2 from process <0.91.0>
Hello 2 from process <0.92.0>
Hello 2 from process <0.93.0>
Hello 2 from process <0.94.0>
Hello 1 from process <0.91.0>
Hello 1 from process <0.92.0>
Hello 1 from process <0.93.0>
Hello 1 from process <0.94.0>
Process <0.91.0> finished.
Process <0.92.0> finished.
Process <0.93.0> finished.
Process <0.94.0> finished.
7>
When there is no random waiting in the tasks that you are running concurrently, then concurrency provides no benefit.当并发运行的任务中没有随机等待时,并发没有任何好处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.