views:

1133

answers:

12

Straightforward C#/Java code is extremely difficult to parallelize, multi-thread, etc. As a result, straightforward C#/Java code will use less and less of the total processing power on a box (because everything is now going to be multi-core).

Solving this problem in C# and Java is not simple. Mutability and side effects are key to getting stuff done in C# and Java, but this is exactly what makes multi-core, multi-threading programming so difficult.

Hence, functional programming is going to become increasingly important.

Given that the J2EE/Ruby world will splinter amongst many functional/multi-core approaches (just like it does for just about everything else) while the .NET folks will all use F#, this line of thinking suggests that F# will be huge in two years.

What is wrong with this line of thinking? Why isn't it obvious that F# is going to be huge?

(Edit) Larry O'Brien nails it in this blog post: "Language-wise, in my opinion, this is a set of exercises where C and C++ shine — at least until the multithreading stuff. Languages with list-processing idioms will also do well initially, but may have memory-consumption issues (especially functional languages). Ultimately, I think that the managed C-derived language (Java and C#) have the easiest route to Exercise 9 and then face serious shortcomings with Exercise 10, where concurrency issues play the major role. In my opinion, concurrency is going to become the central issue in professional development in the next half-decade, so these shortcomings are very significant."

+2  A: 
  1. Imperative code is easier to write than functional code. (At least, its easier to find people who can right acceptable imperative code vs. functional code)
  2. Some things are inherently single threaded (UI* is the best known example).
  3. There's alot of C#/C/C++ code out there already, and multiple languages in the same project makes management of said project more difficult.

Personally, I think functional languages will become increasingly mainstream (heck F# itself is a testament to that) but probably never gain lingua franca status like C/C++/Java/C#/etc. have or will.


*This is apparently a somewhat contentious view, so I'll expand upon it.

In a multi-threaded UI, each UI event is dispatched asynchronously and on a thread of its own (the actual management of threads is probably more sophisticated than just spinning up a new one, but that's not really germane to the discussion).

Imagine if this were the case, and you're rendering the window.

  1. The window manager asks you to draw each element (expect a message, or a function invokation for each element).
  2. Each element reads its state (implicitly reading the application state)
  3. Each element draws itself.

In step 2, every element MUST lock the application state (or the subset of it that affects display). Otherwise, in the event the application state is updated, the end result of rendering the window could include elements that reflect two different application states.

This is a lock convoy. Each render thread will lock, render, and then release; therefore they'll execute serially.

Now, imagine you're dealing with user input. First, users are pretty slow so the benefits are going to be non-existent unless you're doing considerable work on the (one-of-many) UI thread; so I'm going to assume thats the case.

  1. The Window Manager informs your application of user input (once again, message, function call, whatever).
  2. Read what's needed from the application state. (Locks needed here)
  3. Spend noticable time crunching some numbers.
  4. Update the application state. (Locks needed here as well)

All you've accomplished is changing from explicitly starting a worker thread, to implicitly doing so; at the cost of potential heisenbugs & deadlocks if you're loose with locking your state.

The fundamental problem with UI api's is that you're dealing with a many-to-one (or one-to-many depending on how you look at it) relationship. Either many windows, many elements, or many "input types" all of which affect a single window/surface. Some sort of synchronization has to happen, and when it does multi-threading doesn't have any benefits anymore just detractions.

Kevin Montrose
UI is only "inherently single threaded" because Microsoft's implementation gives everything thread affinity.
jalf
Name a UI framework that isn't single threaded. Thread affinity just causes "fail-fast" behavior, and after working with Swing/AWT for years its a very welcome behavior when I jump over .NET land.
Kevin Montrose
I agree with 1-3. (1) means that there is going to be a great schism, just like there was when we transitioned from procedural programming to OO. (2) means that it will take some people a very long time to figure out that they should be writing in a thread safe way. (3) means that there is a huge business opportunity.If you don't think that F# is going to be as big as Java and C#, how are Java and C# programmers going to solve the multi-core/multi-threading problem?
You can still do multi-threading in imperative languages, its just more work. Those projects that absolutely need it will put in the extra effort and so long as that effort is less than that required to rewrite the entire project in well architect-ed F# they come out ahead.
Kevin Montrose
@Kevin Montrose I think that is an interesting line of thinking. The question then becomes: Given the choice between learning how to multithread C#/Java and learning a new programming language, what will people do? Is this analogous to noting that while it is possible to do OO in Fortran it is much easier to just switch to C#/Java?
Microsoft designed their ui to be single threaded for a good reason - in an environment where users are potentially able to do many things at the same time multithreading could easily make even the most simple ui a concurency nightmare!
Kragen
@Kevin Just because all useful UI framework to date are single threaded, doesn't mean it will continue to be that way. There is a lot of merit in having the UI be multi-thread capable. That merit will become more and more apparent and important as we start utilizing multi-core architecture.
Joseph
@Jospeh - Can you think of a benefit to multi-threaded UI? You've got one user*, one screen, one keyboard, one mouse; where's the benefit to complicating the stack? [*This is per "desktop"/session/whatever, not per machine.]
Kevin Montrose
@Kevin User Input is not the only justification for changing UI. That is the single largest benefit I am aware of. With a parralleled UI framework you could theoretically deal with multiple UI elements in parrallel instead of in sequence. Imagine how much faster Windows could load/unload/react if you could split off UI rendering to multiple cores. There are benefits abound.
Joseph
@Jospeh You'd have to lock the window state or risk rendering elements "out of sync" (for lack of a better term). This devolves into a lock convoy, clearly, and thus performs no better than the single threaded version. So, in exchange for a more complicated (but multi-threaded) UI api you get... nothing but the potential for heisenbugs?
Kevin Montrose
@Kevin There is no doubt in my mind that this will present a much more complicated model than we currently use. However, this is definitely something that is going to happen. As multi-core becomes more mainstream we will start seeing a paradigm shift in the way UI work will be done. We will have to abstract away these parrallel concepts to be able to accomplish these things, but it will be just like everything else. The first abstraction will be complicated and hard to use, subsequent ones will get better and better. I agree with you, though, that it will be complicated in the beginning.
Joseph
@Jospeh The thing is, I don't think there will be a shift. There are no benefits to making UI multithreaded; as the gains in multithreading are primarily increased CPU resources. UI does not, and should never, need much in the way of CPU time, making a complicated API that lets a UI peg a CPU at 100% is to no-ones benefit.
Kevin Montrose
@Kevin Then I respectfully disagree with you. There are many industry sectors that can and will benefit heavily from allowing CPU resources to be better managed in regards to UI concerns. Even in typical LOB apps there is plenty of opportunity to increase CPU resource utilization. Your assertion that UI should never need much in the way of CPU time may apply to broad LOB apps, but is not true in a lot of cases. In either case, the demand for such things will drive what is built. I guess we'll have to wait and see what happens =)
Joseph
@Joseph Fair enough. I would personally like to see a multi-threaded UI framework purely out of professional curiosity, so if you're sitting on a grand unified theory of UI: "happy hacking", :)
Kevin Montrose
Are there any studies that show writing in functional style to be intrinsically harder than imperative? How do you discount the bias of so many folks starting in C or Java or similar? Sure, after you've forced yourself to think in terms of pushing bits around it might be hard -- but I don't see why that should be the easier way in general.
MichaelGG
@MichaelGG If we were starting with 100% new developers that might matter. That's why I wrote the (...) part.
Kevin Montrose
+6  A: 

Functional programming is harder to get your head around than imperative programming. F# is a more difficult language in many ways than C#. Most 'developers' don't understand functional programming concepts, and can't even write very good imperative code in C#. So what hope have they got of writing good functional code in F#?

And when you consider that everybody on the team needs to be able to understand, write, debug, fix, etc. the code in the language you choose, it means you need a very strong team -- not just a very strong person -- to be able to use F# as it's meant to be used. And there aren't many of those around.

Add into the mix the fact that there's 8 years of C#/VB code lying around which is unlikely to be migrated, and that it's easier to create libraries that look and feel like the BCL in C#/VB as it's less easy to leak stuff like tuples etc. through public interfaces, and I reckon that F# will struggle to gain anything more than usage by strong teams on new, internal projects.

Greg Beech
But what does the C#/Java developer do when he is using 10% of the processing power on the machine while the F# programmer uses 80%? How does the C#/Java developer justify his existence?
@unknown - The industry has been surviving for many years with a lot of so-called developers barely knowing their arse from their elbow (just check thedailywtf.com). Middle and senior managers, largely, haven't got a clue about any of these issues, and still hire crap developers who churn out crap code. The fact that it's only running on 10% of the hardware is probably the least of anybody's worries.
Greg Beech
Also, many things are not easily parallelizable, even if you use a functional language like F#, and some things do not need to be parallelized at all.
Robert Harvey
Yes, F# won't take off because, unfortunately, it's syntax isn't C-like enough :). But I am serious -- even though the core F# language is far simpler than C#, it's too much of a change for too many people. This will probably stay true, even as C++ is adding lambda support. So, interop and ...ability (is that a non-offensive way of saying it?)... are going to be the primary reasons why F# doesn't get as popular...
MichaelGG
Functional programming may be harder to learn than imperative programming but it is *way* easier to learn than modern object oriented programming.
Jon Harrop
@Jon - I didn't even mention object-oriented programming? My comparison was between functional and imperative code. Imperative code does not imply object-orientation; you can write procedural imperative code quite happily. And my point is that there are a huge number of developers who can't even write procedural imperative code well, so what hope would they have with functional programming? I'm quite sure the same people would struggle with object orientation too.
Greg Beech
@Greg Beech: You're talking about C#. You cannot get far in C# without touching OOP.
Jon Harrop
@Jon - Without touching it, no. Without understanding it, yes. I'd bet the huge majority of C# developers do not really understand object oriented concepts, and have even less idea of how to apply them. Sure, they may build simple objects, but there's a huge difference between being able to build simple objects and understanding object orientation.
Greg Beech
@Greg: In that case, can you not do the same with F#? Use functional programming (e.g. pipelines) without having to understand it?
Jon Harrop
+6  A: 

Straightforward C#/Java code is extremely difficult to parallelize

Not if you use the Task Parallel Library.

Whether F# becomes huge depends on whether the cost/benefit is there, which is not at all obvious. If .NET developers find out that they can write some programs in 1/3 of the time using a functional rather than an imperative approach (which I think might be true for certain types of programs), then there should be some motivation for F# adoption.

Paul Graham's story of his use of Lisp in a startup company is illustrative of this process. Lisp provided them with a huge competitive advantage, yet Lisp didn't take over the world, not because it wasn't powerful, but for other reasons, like lack of library support. That F# has access to the .NET framework gives it a fighting chance.

http://www.paulgraham.com/avg.html

Robert Harvey
Yes, but you see TPL, like every other C#/Java solution I've seen to this problem, requires the developer to (more or less) explicitly parallelize the code. That is *extremely* difficult to do because it requires the developer to have a deep understanding of what is going on in the hardware. This is in sharp contrast to functional programming where the developer gets multi-threading for free.
To use the parallel fx library effectively, you just need to know how to write a good for loop. You might want to check out the 30 minute podcast about the library http://channel9.msdn.com/shows/Going+Deep/Programming-in-the-Age-of-Concurrency-Anders-Hejlsberg-and-Joe-Duffy-Concurrent-Programming-with/
Robert Harvey
In addition, there is some overlap between the parallel fx library and the internals of F#: see http://channel8.msdn.com/Posts/401/
Robert Harvey
I don't see how TPL requires much knowledge about the hardware/OS as the TPL abstracts parallelization to a higher level: to that of tasks (units of work), rather than *threads*. You never deal with threads, just tasks. If you can chop the work that you want done into multiple chunks, you're done.
JulianR
@user128807: automatic parallelization is still very experimental, even for Haskell which is where cutting-edge functional programming research happens.
Mauricio Scheffer
+3  A: 

There isn't really any case against F#, but you have to understand the context of the situation we, as developers, are in currently.

The multi-core architecture is still in it's infancy. The major driving force to change single-threaded apps over to a parrellel architecture is going to take time.

F# is very useful for a number of reasons, parrallelism being one of them, but not the only one. Functional programming is also extremely useful for scientific purposes. This will be huge in many sectors.

However, the way you're wording your question it sounds like you're stipulating that F# is already fighting a losing battle, which is definitely not the case. I've talked to many scientists to date that are using things such as MatLab and the like, and a lot of them are already aware of F#, and excited about it.

Joseph
Right on. Everyone I've talked with who has made the change to F# for a project has been overwhelmingly positive about it. I've yet to hear of anyone learning F#, using it, then going back to, say, C#.
MichaelGG
+6  A: 
  1. Ask a programming question on SO and specify you are using F#.
  2. Ask the same question and specify you are using C#.
  3. Compare the answers.

Using a novel programming language is a calculated risk--you may get more built-in functionality and syntactic sugar, but you will lose in community support, ability to hire programmers, and working around blind spots in the language.

I'm not picking on F#--every decision of programming language is a risk equation you need to work out. If people didn't take that risk on C#, we'd all still be using VB6 and C++ now. Same with those languages versus their predecessors. You have to decide for your project whether the advantages outweigh the risks.

richardtallent
+3  A: 

I disagree with the premise that C# is hard to parallelize. It really isn't if you know what you're doing. Additionally, parallel linq will make this even easier. Do I wish there was an OpenMP for C#? Of course, but the tools C# provides allow you to do almost everything you want if you are good enough (and I feel one doesn't even have to be that good anymore).

Steve
I would say the same about java... Azul is very java heavy and they have built their business on delivering performance on a lot of (hundreds) of cores. My own customers are usually running their java apps on some 32 cores and they scale quite well also. Even if we are proud of the tools we give them it is not rocket science to scale to multiple cores in java or C#.
Fredrik
+2  A: 

There is a few things worth noting about technology

  • The best technical solution is not always the most popular or most used. (And I don't know if F# is any good) I would argue that SQL is the most used, most asked for programming language by employers and its not a nice,cool,fast,friendly,fun language in my book. If the best technical solution always "won", how do you explain qwerty keyboards? And if you ever read the "design" for x86/x64 processors.. ;)
  • Azul with 864 core servers exclusively uses Java, and the trend is bigger servers in future.
Peter Lawrey
+2  A: 

What is wrong with this line of thinking? Why isn't it obvious that F# is going to be huge?

You're assuming the large masses actually write programs that need multicore support - or the programs would gain significant benefit from being parallellized. That's a false assumption.

Server side there's even less need for a parallell language. Backend server processing already takes enough advantage of multicore/processor support by it's inherent nature of being concurrent(work is divided on clients via threads and among processes(e.g. one app server, one db server, one web container.. ).

nos
I think this is a key question. Is it the case that many programs are already fast enough and there is minimal benefit to making them faster (beyond the speedup they will get from faster single cores)? It would be nice to characterize what set of programs would benefit from speedup and which would not.Your comment about server side parallelization doesn't distinguish between coarse grain and fine grain parallelism. Many server side apps will benefit from being multithreaded. For example, all the transaction processing stuff I'm familiar with benefits from parallelization.
A: 
  • Linking assemblies together is not trivial.

  • F# is tied to the .NET typing system, which is significantly more restricted than, say, PHP. It's probably right up there with Java in the land of Strong Typing. That makes the entry barrier pretty high for someone who isn't intimately familiar with the .NET types.

  • Single-assignment code is hard to write; most algorithms use the typical Turing machine model, which permits multiple assignments and single-assignment code does not really neatly fit into a good model for How We Think. At least, for those of us who write Turing Machine code for a living. Perhaps it's different for those of us who write Lambda Machine code out there...

  • F# is tied to Microsoft, which produces knee-jerk hate from many geeks. They would rather use Lisp or Scheme or Haskell(or whatever). Although mono supports it, it doesn't support it well last time I tried to work on mono(it was quite slow).

  • Most of our existing code lives in imperative, sequential code bases, and most of our applications are oriented around imperative, sequential operations with side-effects.

Which is all to say, pure functional approaches do not neatly model the real world, so F# is going to have to carve out a niche where it easily manages real-world problems. It cannot be a general purpose language, because it does not neatly solve general purpose problems.

Paul Nathan
F# is a multi-paradigm language. You do not have to use functional patterns where they will not fit. In fact, even using only its object oriented features you can write very concise and powerful code.
Dave Berk
useful perspective, even if disagreed with. +1
zvolkov
+2  A: 

What is wrong with this line of reasoning is that it assumes that everything will work out as planned.

There is the assumption that it will be easier to write multithreaded programs in F# than in C#. Historically, functional languages have not done all that well in popularity, and there's probably reasons why. Therefore, while it is generally easier to multithread functional than imperative languages, it's generally been easier to find people to program in imperative languages. These two things balance out somehow, depending probably on the people and the app. It may or may not be easier in general to write multithreaded applications in functional or imperative languages. It's far too early to tell.

There's the assumption that people are going to demand efficient use of their 1K-core computers. There are always applications that can take as much CPU power as they can find, but these aren't the most common applications. Most applications people run are not in any way limited by CPU power nowadays, but by delays in local I/O, networking, and users. This may change, but it won't change at all quickly.

Also, it isn't clear that massively multicore processors are the wave of the future. There may be a fairly small market for them, so chip manufacturers will produce smaller chips instead of more powerful, or will devote resources to other things that we aren't clear about right now.

There's the assumption that F# is going to be the winner among functional languages. As the VS 2010 functional language, it does have a considerable advantage. However, the race hasn't really started yet, and there's plenty of time for things to happen. It may turn out that F#.NET isn't a particularly good language to program massively parallel PCs, and something else may come about. It may happen that Microsoft and .NET won't be all that important by the time 64-core processors routinely come on cheap laptops. (Shifts like that aren't all that common, but they tend to come by surprise. They also are more likely to happen during times of conceptual change, and a mass move to functional languages would qualify.)

On the assumption that F# will continue to be the primary Microsoft functional language, that Microsoft programming languages will continue to be dominant, that getting maximum performance out of massively multicore processors will be important, that all the technical arguments won't be swamped by business inertia, and that F# will be considerably better than C# and other such languages at writing massively multithreaded applications, and that you're right. However, that's a whole lot of assumptions strung together and linked by plausible reasons rather than rigid logic.

You seem to be trying to predict the future as a combination of next year's stuff extended by one line of reasoning about technical issues, and that's extremely unreliable.

David Thornley
Of the assumptions that you list in the second to last paragraph, the only one I'm concerned about is "F# will be considerably better than C#" at multithreading. Certainly, all of the C# developers I talk to say that Microsoft is going to give them a solution that solves the problem. I find it difficult to believe this, however, since everything in C# is mutable and has side effects, but I may just not be aware of the latest approaches in this area.
C# doesn't look like a particularly promising multithreading language, but I can't immediately tell how much better F# will be. CPUs with more than four cores aren't in popular use yet, and we don't have the experience writing massively multicore user software.
David Thornley
+1  A: 

If we assume the battle is between C# and F#, I do not think F# will win over C# within 2 years for the following reasons:

  1. The features of F# that C# does not have are not features people have been missing. For instance, I think Seq.map, Seq.iter, Seq.fold and friends are great, but I don't see a majority of developers switching from foreach to these constructs.

  2. The performance benefits of multicores are irrelevant to most of the existing programs, as only few programs are cpu-bound. For those programs where performance really is important, (e.g. video games), C++ will remain predominant, at least for the 2 years to come. It's not that hard to use threads in C++, assuming one avoids side-effects (which you can decide to do even in C++). Isn't that what Google is doing?

For F# to become really big, I think it has to become one of the main languages used in teaching, the way Java has been. This is actually quite likely, seeing how the academic world is fond of functional languages. Should that happen, I don't think the effects will become visible before 5 years.

Joh
But the academic world rely on Unices... a lot ! The support for F# on Mono is weak, at best. Unless Microsoft change course, I doubt either C# or F# will invade the adamic world.
Shinka
@Shinka: Microsoft have now committed resources to F# support on Mono.
Jon Harrop
+3  A: 

The only 'case' against it (if there is such a thing) is that most modern, professional developers use different tools (as well as different tool types). F# brings some unique tools to the game, and those of us who embrace them will find our respective, specialized talents useful for other programming tasks -- especially those tasks involving analysis and manipulation of large data collections.

What I've seen of F# truly amazes me. Every demo leaves me grinning because F# strikes me as an advanced edition of what I remember from 'the good old days' when functional programming was much more common (probably more 'old' than 'good' to be sure, but such is nostalgia).

Hardryv