0

我用 Elixir 做了一个多进程插入排序程序。但是,在 32 核机器上运行时,它比单进程插入排序要慢。如果发送消息的进程运行在不同的核心上,我认为核心之间的同步可能是延迟的原因。有没有办法找出哪些进程在哪些内核上运行,或者如何控制哪些进程在哪些内核上运行?

defmodule Insertion do
  def insert(l, x) do
    case l do
      [h | hs] -> if x < h, do: [x | l], else: [h | insert(hs, x)]
      [] -> [x]
      _ -> inspect l
    end
  end

  def insertion_sort(l, x, []) do
    insert(l, x)
  end

  def insertion_sort(l, x, [y | ys]) do
    insert(l, x)
    |> insertion_sort(y, ys)
  end

  def sort(l) do
    case l do
      [] -> l
      [_] -> l
      [x | [y | xs]] -> insertion_sort([y], x, xs)
    end
  end


  #
  # Parallel
  #

  def send_to_next(x, y, :end) do
    insert_par(x, spawn(Insertion, :insert_par, [y, :end]))
  end

  def send_to_next(x, y, p) do
    send p, y
    insert_par(x, p)
  end

  def insert_par(x, next) do
    receive do
      {:ret, p} -> send p, {x, next}
      y -> if x < y, do: send_to_next(x, y, next), else: send_to_next(y, x, next)
    end
  end

  def insertion_sort_par([], _) do

  end

  def insertion_sort_par([x | xs], p) do
    send p, x
    insertion_sort_par(xs, p)
  end

  def ret_val(l, p) do
    send p, {:ret, self()}
    receive do
      {x, :end} -> [x | l]
      {x, next} -> ret_val([x | l], next)
    end
  end

  def sort_par([]) do
    []
  end

  def sort_par([x | xs]) do
    root = spawn(Insertion, :insert_par, [x, :end])
    IO.puts inspect :timer.tc(Insertion, :insertion_sort_par, [xs, root])
    ret_val([], root)
    |> Enum.reverse
  end

  def run(n) do
    x = floor :math.pow(10, n)
    l = Enum.map(1..x, fn _ -> floor :rand.uniform * x end)
    :timer.tc(Insertion, :sort_par, [l])
    |> inspect
    |> IO.puts
  end

end

在此处输入图像描述

4

1 回答 1

1

有没有办法找出哪些进程在哪些内核上运行,或者如何控制哪些进程在哪些内核上运行?

我不知道有一种方法可以查看哪些进程在哪个 CPU/调度程序上,但在 Observer 中可以查看调度程序的利用率。

如果你运行:observer.start(),你可以在“加载图表”选项卡下查看。上图显示了每个调度程序随时间的使用情况。如果一个被过度使用而其他人未被充分利用,你会在那里看到它。

于 2019-06-26T22:13:02.630 回答