views:

1432

answers:

7

Is it possible that Microsoft will be able to make F# programs, either at VM execution time, or more likely at compile time, detect that a program was built with a functional language and automatically parallelize it better?

Right now I believe there is no such effort to try and execute a program that was built as single threaded program as a multi threaded program automatically.

That is to say, the developer would code a single threaded program. And the compiler would spit out a compiled program that is multi-threaded complete with mutexes and synchronization where needed.

Would these optimizations be visible in task manager in the process thread count, or would it be lower level than that?

+3  A: 

Being that F# is derived from Ocaml and Ocaml compilers can optimize your programs far better than other compilers, it probably could be done.

Vasil
+1  A: 

There's active research for autoparallelization and auto vectorization for a variety of languages. And one could hope (since I really like F#) that they would concive a way to determine if a "pure" side-effect free subset was used and then parallelize that. Also since Simon Peyton-Jones the father of Haskell is working at Microsoft I have a hard time not beliving there's some fantastic stuff comming.

Torbjörn Gyllebring
+8  A: 

I think this is unlikely in the near future. And if it does happen, I think it would be more likely at the IL level (assembly rewriting) rather than language level (e.g. something specific to F#/compiler). It's an interesting question, and I expect that some fine minds have been looking at this and will continue to look at this for a while, but in the near-term, I think the focus will be on making it easier for humans to direct the threading/parallelization of programs, rather than just having it all happen as if by magic.

(Language features like F# async workflows, and libraries like the task-parallel library and others, are good examples of near-term progress here; they can do most of the heavy lifting for you, especially when your program is more declarative than imperative, but they still require the programmer to opt-in, do analysis for correctness/meaningfulness, and probably make slight alterations to the structure of the code to make it all work.)

Anyway, that's all speculation; who can say what the future will bring? I look forward to finding out (and hopefully making some of it happen). :)

Brian
One of the reasons that PLINQ is being developed is remove the need for the developer to perform "analysis for correctness/meaningfulness".
Mitch Wheat
+3  A: 

I think the question misses the point of the .NET architecture-- F#, C# and VB (etc.) all get compiled to IL, which then gets compiled to machine code via the JIT compiler. The fact that a program was written in a functional language isn't relevant-- if there are optimizations (like tail recursion, etc.) available to the JIT compiler from the IL, the compiler should take advantage of it.

Naturally, this doesn't mean that writing functional code is irrelevant-- obviously, there are ways to write IL which will parallelize better-- but many of these techniques could be used in any .NET language.

So, there's no need to flag the IL as coming from F# in order to examine it for potential parallelism, nor would such a thing be desirable.

Michael Dorfman
Functional programming leads to the ability to better split up a task and execute it concurrently. Of course there are people working on this, and of course it is desirable.
Brian R. Bondy
I don't think you're getting my point. F# (and other functional languages) make it easy for you to write code in ways that parallelize better. This is desirable. However, these benefits show up in the IL, and would also show up in the IL if someone wrote C# code that followed the same patterns.
Michael Dorfman
Once it gets to IL I agree that you can't do much. But before getting to IL is where the optimizations can be done.
Brian R. Bondy
Even optimizations like implicit caching of function results, given commonly used input parameters. This can be done in a language like F# but not C#
Brian R. Bondy
Sure. But you can explicitly cache in C#, and to the IL, it wouldn't make a difference.Re-read the original question. Tagging IL with a flag indicating the code came from F# won't help the JIT "automatically parallelize" it.
Michael Dorfman
+2  A: 

It's possible but unlikely. Microsoft spends most of it's time supporting and implementing features requested by their biggest clients. That usually means C#, VB.Net, and C++ (not necessarily in that order). F# doesn't seem like it's high on the list of priorities.

ilitirit
+2  A: 

I don't believe it is possible to autovectorize code in a generally-useful way and the functional programming facet of F# is essentially irrelevant in this context.

The hardest problem is not detecting when you can perform subcomputations in parallel, it is determining when that will not degrade performance, i.e. when the subtasks will take sufficiently long to compute that it is worth taking the performance hit of a parallel spawn.

We have researched this in detail in the context of scientific computing and we have adopted a hybrid approach in our F# for Numerics library. Our parallel algorithms, built upon Microsoft's Task Parallel Library, require an additional parameter that is a function giving the estimated computational complexity of a subtask. This allows our implementation to avoid excessive subdivision and ensure optimal performance. Moreover, this solution is ideal for the F# programming language because the function parameter describing the complexity is typically an anonymous first-class function.

Cheers, Jon Harrop.

Jon Harrop
+1  A: 

Microsoft is currently developing 2 avenues for parallelisation of code: PLINQ (Pararllel Linq, which owes much to functional languages) and the Task Parallel Library (TPL) which was originally part of Robotics Studio. A beta of PLINQ is available here.

I would put my money on PLINQ becoming the norm for auto-parallelisation of .NET code.

Mitch Wheat