windows 多核编程:需要做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1590976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multicore programming: what's necessary to do it?
提问by Nope
I have a quadcore processor and I would really like to take advantage of all those cores when I'm running quick simulations. The problem is I'm only familiar with the small Linux cluster we have in the lab and I'm using Vista at home.
我有一个四核处理器,当我运行快速模拟时,我真的很想利用所有这些内核。问题是我只熟悉实验室中的小型 Linux 集群,而且我在家里使用的是 Vista。
What sort of things do I want to look into for multicore programming with C or Java? What is the lingo that I want to google?
使用 C 或 Java 进行多核编程时,我想研究哪些方面?我想用谷歌搜索的术语是什么?
Thanks for the help.
谢谢您的帮助。
回答by Alex Martelli
The key word is "threading" - wouldn't work in a cluster, but it will be just fine in a single multicore machine (actually, on any kind of Windows, muchbetter in general than spawning multiple processes -- Windows' processes are quite heavy-weight compared to Linux ones). Not quite that easy in C, very easy in Java -- for example, start here!
关键词是“线程”——在集群中不起作用,但在单个多核机器上会很好(实际上,在任何类型的 Windows 上,通常比产生多个进程要好得多——Windows 的进程是与 Linux 相比相当重量级)。在 C 中不太容易,在 Java 中很容易——例如,从这里开始!
回答by DigitalRoss
You want Threads and Actors
你想要线程和演员
Good point ... you can't google for it unless you know some keywords.
好点……除非你知道一些关键字,否则你不能用谷歌搜索它。
C:google pthread, short for Posix Thread, although the win32 native interface is non-posix, see Creating Threads on MSDN.
C:google pthread,Posix Thread 的缩写,虽然 win32 本机接口是非 posix 的,请参阅MSDN 上的创建线程。
Java:See class Thread
Java:请参阅类线程
Finally, you should read up a bit on functional programming, actor concurrency, and immutable objects. It turns out that managing concurrency in plain old shared memory is quite difficult, but message passing and functional programming can allow you to use styles that are inherently much safer and avoid concurrency problems. Java does allow you to do everything the hard way, where data is mutable shared memory and you desperately try to manually interlock shared state structures. But you can also use an advanced style from within java. Perhaps start with this JavaWorld article: Actors on the JVM.
最后,您应该阅读一些关于函数式编程、actor 并发性和不可变对象的内容。事实证明,在普通的旧共享内存中管理并发非常困难,但是消息传递和函数式编程可以让您使用本质上更安全的样式并避免并发问题。Java 确实允许您以艰难的方式做所有事情,其中数据是可变的共享内存,您拼命尝试手动互锁共享状态结构。但是您也可以在 java 中使用高级样式。或许可以从这篇 JavaWorld 文章开始:JVM 上的参与者。
回答by Igor ostrovsky
Check out this book: Java Concurrency in Practice
看看这本书:Java并发实践
回答by Anon
I think you should consider Clojure, too. It runs on the JVM and has good Java interoperability. As a Lisp, it's different from what you're used to with C and Java, so it might not be your cup of tea, but it's worth taking a look at the issues addressed by Clojure anyway, since the concepts are valuable regardless of what language you use. Check out this video, and then, if you're so inclined, the clojure site, which has links to some other good screencasts more specifically about Clojure in the upper right.
我认为你也应该考虑 Clojure。它运行在JVM上,具有良好的Java互操作性。作为一个 Lisp,它与你习惯使用 C 和 Java 的不同,所以它可能不是你的一杯茶,但无论如何值得一看 Clojure 解决的问题,因为无论什么概念都是有价值的你使用的语言。观看此视频,然后,如果您愿意,可以查看clojure站点,该站点在右上角提供了指向其他一些更具体的关于 Clojure 的优秀截屏视频的链接。
回答by Dirk Eddelbuettel
It depends on what your preferred language is to get the job done.
这取决于您完成工作的首选语言。
Besides the threading solutions, you may can also consider MPIas a possibility from Java and C --- as well as from Python or Ror whatever you like. DeinoMPIappears to be popular on Windows, and OpenMPIjust started with support for Windows too in the current release 1.3.3.
除了线程解决方案之外,您还可以将 MPI视为来自 Java 和 C 的可能性——以及来自 Python 或R或您喜欢的任何东西。 DeinoMPI似乎在 Windows 上很流行,而OpenMPI在当前版本 1.3.3 中也刚刚开始支持 Windows。
回答by Michael Dillon
A lot of people have talked about threading, which is one approach, but consider another way of doing it. What if you had several JVM's started up, connected to the network, and waiting for work to come their way? How would you program an application so that it could leverage all of those JVMs without knowing whether or not they are on the same CPU?
很多人都谈过线程,这是一种方法,但考虑另一种方法。如果您有多个 JVM 启动、连接到网络并等待工作来怎么办?您将如何编写一个应用程序,以便它可以在不知道它们是否在同一个 CPU 上的情况下利用所有这些 JVM?
On a quadcore machine, you should be able to run 12 or more JVMs to process work. And if you approach the problem from this angle, scaling up to multiple computers is fairly simple, although you do have to consider higher network latencies when your communication is across a real network.
在四核机器上,您应该能够运行 12 个或更多 JVM 来处理工作。如果您从这个角度处理问题,扩展到多台计算机是相当简单的,尽管当您的通信通过真实网络时,您必须考虑更高的网络延迟。
回答by Danny Varod
If you want to do easy threading, such as parallel loops, I recommend check out .NET 4.0 Beta (C# in VS2010 Beta).
如果您想进行简单的线程处理,例如并行循环,我建议您查看 .NET 4.0 Beta(VS2010 Beta 中的 C#)。
The book chapter Joe linked to is a very good one I use myself and highly recommend, but doesn't cover the new parallel extensions to the .NET framework.
Joe 链接到的一章是我自己使用并强烈推荐的非常好的一章,但没有涵盖 .NET 框架的新并行扩展。
回答by Paul
Even though you asked specifically for C or Java, Erlang isn't a bad choice of language if this is just a learning exercise
即使您专门询问 C 或 Java,如果这只是一个学习练习,Erlang 也不是一个糟糕的语言选择
It allows you to do multiprocess style programming very very easily and it has a large set of libraries that let you dive in at just about any level you like.
它使您可以非常轻松地进行多进程风格的编程,并且它拥有大量的库,让您可以深入到您喜欢的任何级别。
It was built for distributed programming in a very pragmatic way. If you are comfortable with java, the transition shouldn't be too difficult.
它是以一种非常务实的方式为分布式编程而构建的。如果您对 java 感到满意,那么转换应该不会太困难。
If you are interested, I would recommend the book, "Programming Erlang" by Joe Armstrong.
如果您有兴趣,我会推荐 Joe Armstrong 所著的《Programming Erlang》一书。
(as a note: there are other languages designed to run on in highly parallel environments like Haskell. Erlang just tends to be more pragmatic than languages like Haskell which are rooted more in theory)
(注意:还有其他语言设计用于在高度并行的环境中运行,例如 Haskell。Erlang 只是比 Haskell 之类的语言更实用,后者更植根于理论)
回答by Jerry Coffin
You need to create multithreaded programs. Java supports multi-threading out of the box (though older JVMs ran all threads on one core). For C, you'll either need to use platform specific code to to create and manipulate threads (pthread* for Linux, CreateThread and company for Windows). Alternatively, you might want to do your threading from C++, where there are a fair number of libraries (e.g. Boost::threads) to make life a bit simpler and allow portable code.
您需要创建多线程程序。Java 支持开箱即用的多线程(尽管较旧的 JVM 在一个内核上运行所有线程)。对于 C,您需要使用特定于平台的代码来创建和操作线程(Linux 为 pthread*,Windows 为 CreateThread 和 company)。或者,您可能希望从 C++ 进行线程处理,其中有相当数量的库(例如 Boost::threads)使生活更简单并允许可移植代码。
If you want code that'll be portable across a single machine with multiple cores AND a cluster, you might look into MPI. It's really intended for the cluster situation, but has been ported to work on a single machine with multiple processors or multiple cores -- though it's not as efficient as code written specifically for a single machine.
如果您希望代码可以在具有多个内核和集群的单台机器上移植,您可以查看 MPI。它确实适用于集群情况,但已被移植到具有多个处理器或多个内核的单台机器上——尽管它不如专门为单台机器编写的代码那样高效。