Writing functional programs in non-functional languages
Asked Answered
A

5

5

Suppose I write a program using immutable data structures in Java. Even though it is not a functional language, it should be able to execute parallely. How do I ensure that my program is being executed using all the cores of my processer? How does the computer decide which code can be run parallely?

P.S. My intent in asking this question was not to find out how to parrallelize java programs. But to know - how does the computer parallelize code. Can it do it in a functional program written in a non functional language?

Amling answered 11/5, 2009 at 9:53 Comment(0)
M
3

i dont think you can "force" the JVM to parallelize your program, but having a separate thread executing each "task", if you can break down your program that way, would probably do the trick in most cases? parallelism is still not guaranteed however.

Marinna answered 11/5, 2009 at 12:25 Comment(0)
C
8

Java programs are parallelized through threads. The computer can't magically figure out how to distribute the pieces of your application across all the cores in an imperative language like Java. Only a functional language like Erlang or Haskell could do that. Read up on Java threads.

Curt answered 11/5, 2009 at 9:58 Comment(5)
I don't see why that should be the preserve of "pure functional" languages.Romilda
A program using immutable structures in Erlang or Haskell doesn't parallelize magically either. The parallelization tools are much easier to use, but you still have to explicitly use them.Ineffective
@Nathan Sanders: I said could, aka it would be easy to write such a program/framework in those languages. Doing so in Java would take a lot more work, and it would be a lot less elegant.Curt
@musicfreak: Can you please explain this part "Only a functional language like Erlang or Haskell could do that" What is so special about functional languages. Actually, I do not understand why Java cannot do it in first place. Thanks!Pearlinepearlman
@Lazer: You cannot automatically parallelize a procedural language like Java because the compiler has no way of knowing what code can be spread across CPU cores. Any function may or may not have side effects. In functional language, everything is immutable, which makes being "thread-safe" much easier, and allows you to write functions that can automatically parallelize (think Map/Reduce). I'm not saying that parallel programming is impossible in Java, it's just much more painful and not even close to seamless.Curt
R
5

I am not aware of automatic parallelization JVMs. They do exist for other languages such as FORTRAN.

You might find the JSR166y fork-join framework scheduled for JDK7 interesting.

Romilda answered 11/5, 2009 at 11:47 Comment(0)
M
3

i dont think you can "force" the JVM to parallelize your program, but having a separate thread executing each "task", if you can break down your program that way, would probably do the trick in most cases? parallelism is still not guaranteed however.

Marinna answered 11/5, 2009 at 12:25 Comment(0)
C
1

You can write functions with automatically parallelise tasks, it is fairly easy to do for specific cases, however I am not aware of any built-in Java API which does this. (Except perhaps the Executor/ExecutorService)

Cut answered 13/5, 2009 at 6:19 Comment(0)
C
0

Something that I used in school that did alot of the work for you.

http://www.cs.rit.edu/~ark/pj.shtml

It has the capability to do SMP or message based parallelism.

Whether or not it is useful to you is a different question :-)

Cheyenne answered 13/5, 2009 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.