如何在具有多个处理器的机器上并行化算法?

发布于 2024-09-16 10:54:44 字数 286 浏览 6 评论 0原文

  • Intel Core2Duo,例如应该有一个芯片,但有两个核心

  • 因此,应该可以控制在哪个核心上处理什么,这意味着可以指示我的算法并行使用两个核心。

问题是如何?

我是否需要在内核级别进行此操作,还是有更简单的方法?更具体地说,实现双核合并排序需要什么?

  • Intel Core2Duo, for example is supposed to have a single die but two cores.

  • So, it should be possible to control what is processed on which core, which means that it is possible to instruct my algorithm to use the two cores in parallel.

The question is how?

Do I need to go down at the kernel level to do this, or is there a simpler way? To be more concrete, what does it take to implement a dual-core-merge-sort?

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

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

发布评论

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

评论(6

药祭#氼 2024-09-23 10:54:45

我希望您希望为每个核心分配线程..
这是关于可以做什么以及如何做的详细描述。

处理器亲和性

希望这会有所帮助。

I hope you are looking to assigning threads to each of the core ..
This is a detailed description of what can be done and how to do it.

Processor affinity

Hope this helps.

合久必婚 2024-09-23 10:54:45

并行编程范例:并行合并排序实现。这是 Erlang 中的一个。为了获得更精确的答案,你必须提出更精确的问题。

A Specimen of Parallel Programming: Parallel Merge Sort Implementation. And here is one in Erlang. For more precise answers, you have to ask a more precise question.

哀由 2024-09-23 10:54:45

这取决于您想要用什么编程语言来实现这一目标。例如:

只需选择语言并寻找该语言的并行处理能力。

祝你好运!

It depends in what programming language you want to achieve this. For example for:

Just choose language and look for parallel processing abilities in that language.

Good luck!

执笔绘流年 2024-09-23 10:54:45

虽然 POSIX 线程(pthreads)可能是一个好主意,但这并不是唯一的。

C 中的多线程实际上并不是微不足道的,因此,我建议使用 fork()

使用合并排序算法的一小部分为每个 CPU 启动一个工作分支,然后在管理分支中重新组装它们。

在开发并行解决方案时,我会在线程之前考虑分叉,因为它们更容易实现,并且您可以快速获得初步结果。一旦成功,您可能需要花一些时间来使用 pthreads。

While POSIX threads (pthreads) are probably a good idea to start, this is not exclusive.

Multithreading in C isn't actually trivial, hence, I'd advice fork().

start one worker fork for each CPU with a subsection of you'r mergesort algorithm, and then reassemble them in the manager fork.

When working towards a parallel solution, I consider forks before threads, since they're easier to implement, and you get a quick preliminary result. Once that works, you might want to take some time and work with pthreads.

聽兲甴掵 2024-09-23 10:54:44

从您过去的问题来看,我想说您正在寻求用 C/C++ 实现,但我相信无论使用哪种语言,答案都大致相同。

如果要并行化任何操作,请将其设为多线程。您可以拥有与核心数量一样多的并行并发线程。

这是一个相关问题:
如何使用 C# 实现分治算法多线程?

据我了解,将特定线程绑定到核心或处理器称为处理器亲和力。这通常不是一个好主意,因为操作系统的目的是在处理器之间处理线程。在这方面你不可能比操作系统做得更好。

Judging by your past questions, I'd say you're looking to implement in C/C++, but I believe the answer is roughly the same regardless of language.

If you want to parallelize any operation, make it multithreaded. You can have as many parallel, concurrent threads as you have cores.

Here's a related question:
How to implement divide and conquer algorithms in C# using multithreading?

As I understand it, binding a particular thread to a core or processor is called processor affinity. It's generally not a good idea, because the purpose of the operating system is to juggle threads between processors. It's unlikely that you'll do a better job of this than the OS can.

预谋 2024-09-23 10:54:44

要实现利用多核的算法,请考虑 OpenMP

当然,具有强数据依赖性的算法可能无法很好地并行化。

To implement an algorithm that takes advantage of multiple cores, consider OpenMP.

Of course, algorithms that have strong data dependencies may not parallelize well.

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