erlang中并行快速排序的问题

发布于 2024-09-17 01:12:16 字数 892 浏览 4 评论 0原文

我在 erlang 中编写快速排序时遇到问题。我正在做的是生成两个进程,然后阻止当前进程,直到我得到左右子数组的响应。得到这两个响应后,我向其父级发送一条消息,为其提供计算列表。 家长! {self(), Lone ++ [H] ++ Ltwo}

但是我在两个子进程中都遇到了获取 undef 的错误。 这是代码。

  quick(Parent, []) -> Parent ! {self(), []};
  quick(Parent, [H | T]) ->
      Pone = spawn_link(main, quick, [ self(), [ X || X <- T, H >= X ] ]) ,
      Ptwo = spawn_link(main, quick, [ self(), [ Y || Y <- T, H < Y ] ]) ,
      receive
          {Pone, Lone} ->
              receive
                  {Ptwo, Ltwo} -> Parent ! {self(), Lone ++ [H] ++ Ltwo}
              end;
          {Ptwo, Ltwo} ->
              receive
                  {Pone, Lone} -> Parent ! {self(), Lone ++ [H] ++ Ltwo}
              end
      end.

  sortquick(List) ->
      quick(self(), List).

称为:

main:sortquick([12,4,7,22,25]).

I am facing problem with writing quicksort in erlang. What I am doing is I am spawning two processes and then blocking the current process till I get the response from both the left and right sub-arrays. Getting both these responses I send a message to its parent giving it the computed list. Parent ! {self(), Lone ++ [H] ++ Ltwo}

But I am getting error of gettting undef in both the sub-processes.
Here is the code.

  quick(Parent, []) -> Parent ! {self(), []};
  quick(Parent, [H | T]) ->
      Pone = spawn_link(main, quick, [ self(), [ X || X <- T, H >= X ] ]) ,
      Ptwo = spawn_link(main, quick, [ self(), [ Y || Y <- T, H < Y ] ]) ,
      receive
          {Pone, Lone} ->
              receive
                  {Ptwo, Ltwo} -> Parent ! {self(), Lone ++ [H] ++ Ltwo}
              end;
          {Ptwo, Ltwo} ->
              receive
                  {Pone, Lone} -> Parent ! {self(), Lone ++ [H] ++ Ltwo}
              end
      end.

  sortquick(List) ->
      quick(self(), List).

called as:

main:sortquick([12,4,7,22,25]).

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

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

发布评论

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

评论(3

小帐篷 2024-09-24 01:12:16

代码本身不是问题。快速排序效果很好。您在子进程中得到 undef 的原因可能是由于函数 Quick/2 根本没有导出。当您使用模块和函数调用spawn_link时,需要导出该函数。

您可以通过添加

-export([quick/2]).

或将spawn_links更改为类似的内容

spawn_link(fun() -> quick(Self, [Y || Y <- T, H < Y]) end

来解决此问题,但如果您采用后一种方式,则需要

Self = self()

在进行调用之前创建一个变量,否则它将不会返回到正确的进程。

The code itself is not the problem. The quick sort works fine. The reason, probably, why you are getting an undef in the subprocesses is due to the fact that the function quick/2 is not exported at all. When you call spawn_link with the module and function, the function needs to be exported.

You can fix this by either added

-export([quick/2]).

Or by changing the spawn_links to something like

spawn_link(fun() -> quick(Self, [Y || Y <- T, H < Y]) end

Though if you do it the latter way, you'll need to create a variable

Self = self()

Before you make the calls or else it wont return to the proper process.

旧梦荧光笔 2024-09-24 01:12:16

上面的代码通过导出 fast/2 函数可以正常工作。

后来我比较了生成的快速排序和未生成的快速排序的运行时间。
生成的快速排序需要 15 秒到 32 秒的时间来对范围 (1,1000000) 内的 100 万个随机数的列表进行排序。

未生成的快速排序是(下面的代码):

 55 quicksort([]) -> [];
 56 quicksort([H]) -> [H];
 57 quicksort([H | T]) ->
 58     [Headnew | Tailnew] = [H | T],
 59     quicksort([ X || X <- Tailnew, Headnew >= X ]) ++ [Headnew] ++ quicksort([ Y || Y <- Tailnew, Headnew < Y ]).

需要 5 秒到 8 秒之间的任何时间来对一百万个随机数的相同列表进行排序。

我正在我的旧 Thinkpad、1.7 Ghz 处理器(单核)和 512 Mb RAM 上测试代码。

有什么解释可以解释生成的快速排序比未生成的快速排序性能差吗?

The code above works fine by exporting quick/2 function.

Later on I compared the run time of the spawned quicksort vs the unspawned quicksort.
The spawned quicksort is taking anywhere between 15sec to 32 sec to sort a list of 1 million random numbers in range (1,1000000).

The unspawned quicksort is(code below):

 55 quicksort([]) -> [];
 56 quicksort([H]) -> [H];
 57 quicksort([H | T]) ->
 58     [Headnew | Tailnew] = [H | T],
 59     quicksort([ X || X <- Tailnew, Headnew >= X ]) ++ [Headnew] ++ quicksort([ Y || Y <- Tailnew, Headnew < Y ]).

takes anywhere between 5sec and 8sec to sort same list of a million random numbers.

I am testing code on my old Thinkpad, 1.7 Ghz processor (single core) and 512 Mb RAM.

Any explanation for spawned quicksort to perform poorer than unspawned one ?

入怼 2024-09-24 01:12:16

我对代码进行了轻微修改,以防止它生成到许多进程。当它达到树中的某个级别时,它会切换到顺序。

qsort([]) ->
    [];
qsort([H | T]) -> 
    qsort([ X || X <- T, X < H ]) ++ [H] ++ qsort([ X || X <- T, X >= H ]).

quick(Parent, [], _) -> Parent ! {self(), []};
quick(Parent, [H | T], C) when C > 0->
   Pone = spawn_link(test, quick, [ self(), [ X || X <- T, H >= X ], C-1 ]) ,
   Ptwo = spawn_link(test, quick, [ self(), [ Y || Y <- T, H < Y ], C-1 ]) ,
   receive
       {Pone, Lone} ->
              receive
                  {Ptwo, Ltwo} -> Parent ! {self(), Lone ++ [H] ++ Ltwo}
              end;
          {Ptwo, Ltwo} ->
              receive
                  {Pone, Lone} -> Parent ! {self(), Lone ++ [H] ++ Ltwo}
              end
      end;
quick(Parent, [H | T], _) -> 
    Parent ! {self(), qsort([ X || X <- T, X < H ]) ++ [H] ++ qsort([ X || X <- T, X >= H ])}.

sortquick(List) ->
    quick(self(), List, 4).

I made a slight modification of the code to prevent it from spawning to many processes. When it reaches a certain level in the tree it switches to sequential.

qsort([]) ->
    [];
qsort([H | T]) -> 
    qsort([ X || X <- T, X < H ]) ++ [H] ++ qsort([ X || X <- T, X >= H ]).

quick(Parent, [], _) -> Parent ! {self(), []};
quick(Parent, [H | T], C) when C > 0->
   Pone = spawn_link(test, quick, [ self(), [ X || X <- T, H >= X ], C-1 ]) ,
   Ptwo = spawn_link(test, quick, [ self(), [ Y || Y <- T, H < Y ], C-1 ]) ,
   receive
       {Pone, Lone} ->
              receive
                  {Ptwo, Ltwo} -> Parent ! {self(), Lone ++ [H] ++ Ltwo}
              end;
          {Ptwo, Ltwo} ->
              receive
                  {Pone, Lone} -> Parent ! {self(), Lone ++ [H] ++ Ltwo}
              end
      end;
quick(Parent, [H | T], _) -> 
    Parent ! {self(), qsort([ X || X <- T, X < H ]) ++ [H] ++ qsort([ X || X <- T, X >= H ])}.

sortquick(List) ->
    quick(self(), List, 4).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文