多核和并发 - 语言、库和开发技术

发布于 2024-07-05 03:37:07 字数 1560 浏览 9 评论 0原文

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

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

发布评论

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

评论(15

岁月静好 2024-07-12 03:37:07

OpenMP

它为您处理线程,因此您只需担心 C++ 应用程序的哪些部分要并行运行。

例如。

#pragma omp parallel for
for (int i=0; i < SIZE; i++) 
{
// do something with an element
}

上面的代码将在您告诉 openmp 运行时使用的线程数上运行 for 循环,因此,如果 SIZE 为 100,并且您有一个四核机器,则 for 循环将在每个核心上运行 25 个项目。

还有一些针对各种语言的其他并行扩展,但我最感兴趣的是在图形卡上运行的扩展。 这是真正的并行处理:)(示例:GPU++libSh)

OpenMP.

It handles threads for you so you only worry about which parts of your C++ application you want to run in parallel.

eg.

#pragma omp parallel for
for (int i=0; i < SIZE; i++) 
{
// do something with an element
}

the above code will run the for loop on as many threads as you've told the openmp runtime to use, so if SIZE is 100, and you have a quad-core box, that for loop will run 25 items on each core.

There are a few other parallel extensions for various languages, but the ones I'm most interested in are the ones that run on your graphics card. That's real parallel processing :) (examples: GPU++ and libSh)

独留℉清风醉 2024-07-12 03:37:07

C++0x 将提供 std::lock 函数来将多个互斥锁锁定在一起。 这将有助于缓解由于乱序锁定而导致的死锁。 此外,C++0x 线程库将具有 Promise、Future 和打包任务,它们允许线程等待在另一个线程上执行的操作的结果,而无需任何用户级锁。

C++0x will provide std::lock functions for locking more than one mutex together. This will help alleviate deadlock due to out-of-order locking. Also, the C++0x thread library will have promises, futures and packaged tasks, which allow a thread to wait for the result of an operation performed on another thread without any user-level locks.

情独悲 2024-07-12 03:37:07

multiprocessing 是一个简化多核编程的 python 库,正如另一个答案中提到的。

使用 python 的多处理编写的程序可以轻松修改以将工作发送到云上,而不是本地核心。 piCloud 利用这一点在云上提供强大的按需处理能力:您只需修改 2你的代码行。

因此,要点如下:在选择多核库时,人们可能想问云方法是否也有意义。

multiprocessing is a python library that simplifies multi-core programming, as mentionned in another answer.

Program written with python's multiprocessing can easily be modified to ship work on the cloud, instead of to local cores. piCloud takes advantage of that to provide large, on-demand processing power on the cloud: you just need to modify 2 lines of your code.

So, here is the take-away: when selecting a library for multi-core, one may want to ask whether a cloud approach would also make sense.

亢潮 2024-07-12 03:37:07

英特尔的 C++ Threading Building Blocks 对我来说非常有趣。 它提供了比原始线程更高级别的抽象。 如果您喜欢死树文档,O'Reilly 有一本好书。 另请参阅有使用英特尔线程构建模块的经验吗?

Intel's Threading Building Blocks for C++ looks very interesting to me. It offers a much higher level of abstraction than raw threads. O'Reilly has a very nice book if you like dead tree documentation. See, also, Any experiences with Intel’s Threading Building Blocks?.

夜深人未静 2024-07-12 03:37:07

我会说:

模型:线程+共享状态、参与者+消息传递、事务内存、map/reduce?
语言:Erlang、Io、Scala、Clojure、Reia
库:Retlang、Jetlang、Kilim、Cilk++、fork/join、MPI、Kamaelia、Terracotta

我维护一个关于此类内容的并发链接博客(Erlang、Scala、Java 线程、actor 模型等),每天都会发布几个链接:

http://concurrency.tumblr.com

I would say:

Models: threads + shared state, actors + message passing, transactional memory, map/reduce?
Languages: Erlang, Io, Scala, Clojure, Reia
Libraries: Retlang, Jetlang, Kilim, Cilk++, fork/join, MPI, Kamaelia, Terracotta

I maintain a concurrency link blog about stuff like this (Erlang, Scala, Java threading, actor model, etc) and put up a couple links a day:

http://concurrency.tumblr.com

罪歌 2024-07-12 03:37:07

我用 Ada 进行并发编程已经近 20 年了。

该语言本身(不是一些附加的库)支持线程(“任务”)、多个调度模型和多个同步范例。 您甚至可以使用内置原语构建自己的同步方案。

您可以将 Ada 的 集合点 视为一种面向程序的同步设施,而受保护的对象 更加面向对象。 Rendezvous 类似于 监视器 的旧 CS 概念,但功能更强大。 受保护的对象是具有同步原语的特殊类型,允许您构建与操作系统锁、信号量、事件等完全相同的东西。但是,它足够强大,您还可以根据您的具体需求发明和创建自己类型的同步对象。

I've been doing concurrent programming in Ada for nearly 20 years now.

The language itself (not some tacked on library) supports threading ("tasks"), multiple scheduling models, and multiple synchronization paradigms. You can even build your own synchronization schemes using the built in primitives.

You can think of Ada's rendezvous as sort of a procedural-oriented synchronization facility, while protected objects are more object-oriented. Rendezvous are similar to the old CS-concept of monitors, but much more powerful. Protected objects are special types with synchronization primitives that allow you to build things exactly like OS locks, semaphores, events, etc. However, it is powerful enough that you can also invent and create your own kinds of sync objects, depending on your exact needs.

左秋 2024-07-12 03:37:07

问题什么并行编程模型您今天建议利用明天的众核处理器吗? 已经有人问过。 我也在那里给出了以下答案。

Kamaelia 是一个 python 框架,用于构建具有大量通信进程的应用程序。

Kamaelia - 并发变得有用、有趣

在 Kamaelia 中,您可以使用相互通信的简单组件构建系统。 这可以加快开发速度,极大地帮助维护,并且还意味着您可以构建自然并发的软件。 它旨在供任何开发人员(包括新手)使用。 这也让它变得有趣:)

什么样的系统? 网络服务器、客户端、桌面应用程序、基于 pygame 的游戏、转码系统和管道、数字电视系统、垃圾邮件根除器、教学工具以及更多:)

Here's a video from Pycon 2009. It starts by comparing Kamaelia to Twisted and Parallel Python and then gives a hands on demonstration of Kamaelia.

与 Kamaelia 轻松并发 - 第 1 部分 (59:08)
使用 Kamaelia 轻松实现并发 - 第 2 部分 (18:15)

The question What parallel programming model do you recommend today to take advantage of the manycore processors of tomorrow? has already been asked. I gave the following answer there too.

Kamaelia is a python framework for building applications with lots of communicating processes.

Kamaelia - Concurrency made useful, fun

In Kamaelia you build systems from simple components that talk to each other. This speeds development, massively aids maintenance and also means you build naturally concurrent software. It's intended to be accessible by any developer, including novices. It also makes it fun :)

What sort of systems? Network servers, clients, desktop applications, pygame based games, transcode systems and pipelines, digital TV systems, spam eradicators, teaching tools, and a fair amount more :)

Here's a video from Pycon 2009. It starts by comparing Kamaelia to Twisted and Parallel Python and then gives a hands on demonstration of Kamaelia.

Easy Concurrency with Kamaelia - Part 1 (59:08)
Easy Concurrency with Kamaelia - Part 2 (18:15)

蒗幽 2024-07-12 03:37:07

我知道 Reia - 一种基于 Erlang 但看起来更像 Python/的语言红宝石。

I know of Reia - a language that is based on Erlang but looks more like Python/Ruby.

山有枢 2024-07-12 03:37:07

我正在密切关注 .NET 并行扩展并行 LINQ

I am keeping a close eye on Parallel Extensions for .NET and Parallel LINQ.

心是晴朗的。 2024-07-12 03:37:07

我使用了 Python 的 processing 。 它模仿 threading 模块的 API,因此非常易于使用。

如果您碰巧使用 map/imap 或生成器/列表理解,则将代码转换为使用 processing 非常简单:

def do_something(x):
    return x**(x*x)

results = [do_something(n) for n in range(10000)]

可以并行化

import processing
pool = processing.Pool(processing.cpuCount())
results = pool.map(do_something, range(10000))

,无论您拥有多少个处理器来计算结果。 还有惰性 (Pool.imap) 和异步变体 (Pool.map_async)。

有一个队列类实现 Queue.Queue ,以及类似于线程的工作人员。

问题

processing 基于 fork(),必须在 Windows 上进行模拟。 对象通过 pickle/unpickle 传输,因此您必须确保其有效。 分叉一个已经获取资源的进程可能不是您想要的(想想数据库连接),但一般来说它是有效的。 它运行得非常好,以至于它已被快速添加到 Python 2.6 中(参见 PEP -317)。

I've used processing for Python. It mimicks the API of the threading module and is thus quite easy to use.

If you happen to use map/imap or a generator/list comprehension, converting your code to use processing is straightforward:

def do_something(x):
    return x**(x*x)

results = [do_something(n) for n in range(10000)]

can be parallelized with

import processing
pool = processing.Pool(processing.cpuCount())
results = pool.map(do_something, range(10000))

which will use however many processors you have to calculate the results. There are also lazy (Pool.imap) and asynchronous variants (Pool.map_async).

There is a queue class which implements Queue.Queue, and workers that are similar to threads.

Gotchas

processing is based on fork(), which has to be emulated on Windows. Objects are transferred via pickle/unpickle, so you have to make sure that this works. Forking a process that has acquired resources already might not be what you want (think database connections), but in general it works. It works so well that it has been added to Python 2.6 on the fast track (cf. PEP-317).

小梨窩很甜 2024-07-12 03:37:07

你提到了Java,但你只提到了线程。 你看过Java的并发库吗? 它与 Java 5 及更高版本捆绑在一起。

这是一个非常好的库,其中包含 ThreadPools、CopyOnWriteCollections 等。 查看 Java 教程 中的文档。 或者,如果您愿意,Java文档

You mentioned Java, but you only mention threads. Have you looked at Java's concurrent library? It comes bundled with Java 5 and above.

It's a very nice library containing ThreadPools, CopyOnWriteCollections to name a very few. Check out the documentation at the Java Tutorial. Or if you prefer, the Java docs.

你没皮卡萌 2024-07-12 03:37:07

我建议进行两种范式转变:

软件事务内存

您可能想了解一下软件事务内存 (STM) 的概念。 这个想法是使用乐观并发:任何与其他操作并行运行的操作都尝试在独立事务中完成其工作; 如果在某个时刻提交了另一个事务,该事务使该事务正在处理的数据无效,则该事务的工作将被丢弃并再次运行该事务。

我认为这个想法的第一个广为人知的实现(如果不是概念验证和第一个实现)是 Haskell 中的实现:有关 Haskell 事务内存的论文和演示Wikipedia 的 STM 文章 中列出了许多其他实现。

事件循环和 Promise

处理并发的另一种非常不同的方式是在 [E 编程语言](http://en.wikipedia.org/wiki/E_(programming_language%29)

请注意,它处理并发的方式以及语言设计的其他部分在很大程度上基于 Actor模型。

I'd suggest two paradigm shifts:

Software Transactional Memory

You may want to take a look at the concept of Software Transactional Memory (STM). The idea is to use optimistic concurrency: any operation that runs in parallel to others try to complete its job in an isolated transaction; if at some point another transaction has been committed that invalidates data on which this transaction is working, the transaction's work is throwed away and the transaction run again.

I think the first widely known implementation of the idea (if not the proof-of-concept and first one) is the one in Haskell : Papers and presentations about transactional memory in Haskell. Many other implementations are listed on Wikipedia's STM article.

Event loops and promises

Another very different way of dealing with concurrency is implemented in the [E programming language](http://en.wikipedia.org/wiki/E_(programming_language%29).

Note that its way of dealing with concurrency, as well as other parts of the language design, is heavily based on the Actor model.

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