r/ocaml • • Mar 28 '16

Multicore support?

Does OCaml's 4.02.3 standard have support for multicore programming? I've found this link that seems to be a recent multicore implementation (~two weeks ago), but I am not sure if this is official (it seems to be part of some lab group that is trying its best to make OCaml support threads finally... glad it's ditching the Python realm of "it doesn't really matter" excuses). I would appreciate if anybody knew how legit this multicore library was (or if there is a better one/plans to make a better one). I know that INRIA mentioned that "if all goes well", OCaml 4.03 will support multicore, but it looks like 4.02.3 seems to have some support for it given that library I linked above? Or am I just wrong?

I mean I know you can make user-threads, but it'd be nice to make kernel-threads and have parallelism (not the same as the concurrency it offers right now via time-slicing). Also, if F# supports multicore and OCaml doesn't, something must be off, since F# is basically a clone of OCaml the last time I checked. I know the developers at INRIA are busy, but couldn't they borrow ideas/take cues from Microsoft's F# and work with that to make OCaml multicore, too? To me, that's the biggest turnoff for using OCaml for high-performance things that need to be multithreaded. Otherwise, OCaml is the bomb (way more friendly than C for close to the same performance).

Also, Haskell and other flavors of *ML support multicore (PolyML comes to mind). Do the folks over at INRIA just not like multicore (I would understand if parallelism was new about a year ago, but it's been close to a decade)? Isn't it the forefront of modern programming—why would they not implement it? This just makes me sad. :(

8 Upvotes

11 comments sorted by

6

u/MercurialAlchemist Mar 28 '16

It would be nice to have an "OCaml roadmap" pinned post, occasionally updated by the team.

1

u/[deleted] Mar 28 '16

Agreed.

1

u/lpw25 Mar 30 '16

Keeping something like that up to date is further work for an already thinly spread team. Instead why don't you just create it, plenty of people in the community have a rough idea of what's coming and who is working on what. If anything is egregiously wrong I'm sure someone on the team will point it out.

2

u/MercurialAlchemist Mar 31 '16

I'm the least qualified person to write something like that, since my idea of the roadmap is "modular implicits and multicore at some point during my lifetime", which doesn't really help anyone.

Without being snarky, I'll point out that if the roadmap changes often enough that spending 15 min tops updating a reddit post is likely to overstretch the team, there are indeed some serious issues with the project :)

11

u/[deleted] Mar 28 '16

I'm bored and on a bus, so I'll take the time to answer your questions. Hopefully others will find this thread with the search.

Does OCaml's 4.02.3 standard have support for multicore programming?

You can do that by spawning a thread, calling a C function, releasing the global lock, doing your computation, acquiring the lock and returning the results to OCaml. That way you can have all of your high level async beautiful type safe code in OCaml, and all the nasty number crunching in C in true multicore parallel. Another way is to spawn worker processes and send them work to do with message passing. Not as clean as "real parallelism", but this approach scales up to distributed systems, unlike multithread parallelism.

Anyway, OCaml 4.02.3 right now supports multithreading already and they're real system level threads, but only one can run at a time. That won't change in 4.03.0 as we all hoped it would, but it almost certainly will in 4.04.0.

Also, if F# supports multicore and OCaml doesn't, something must be off, since F# is basically a clone of OCaml the last time I checked.

Language design is orthogonal to language implementation. F# is built on top of the CLR, which is a relatively slow bytecode interpreter without any sort of JIT optimizations. Through the CLR, F# also inherited all of the bad historical decisions of C#, such as pervasive nulls and terrible module abstractions that make the great OCaml modules and functors impossible, so you're missing out on the real power of OCaml. But F# also inherits nice concurrency and parallelism out of the box.

OCaml on the other hand, comes from the old school UNIX ideology. Single threaded, binary/executable first and a lot of tricks to make it run fast. Many of those tricks in the GC make it impossible to work in parallel. So, in order to make it work, they're having to completely redo huge parts of the GC and the allocator. Each thread will have its own memory space and there's a new system that will let threads go update mutable fields and variables in order threads without causing corruption. It's a lot of work.

I know the developers at INRIA are busy, but couldn't they borrow ideas/take cues from Microsoft's F# and work with that to make OCaml multicore, too? To me, that's the biggest turnoff for using OCaml for high-performance things that need to be multithreaded. Otherwise, OCaml is the bomb (way more friendly than C for close to the same performance).

It should be obvious by now, but Microsoft couldn't help INRIA anyway. The design of those 2 languages under the hood is incredibly different. They have nothing in common. In any case, the link to the OCaml Labs repo for multicore is I think (I'm not sure on that one) where the multicore work is happening.

Also, Haskell and other flavors of *ML support multicore (PolyML comes to mind). Do the folks over at INRIA just not like multicore (I would understand if parallelism was new about a year ago, but it's been close to a decade)? Isn't it the forefront of modern programming—why would they not implement it? This just makes me sad. :(

Again, it's not that they don't want to, it's not that they don't know how it should look like from the developer's perspective, it's that it's really really hard to get it right under the hood. Haskell's implementation is quite poor, from what I've heard. A thing I appreciate about OCaml is that there's very few "mistakes" in the language that we're stuck with forever. They're conservative in a good way and they either get things right the first time or they wait until it can be done right.

2

u/[deleted] Mar 28 '16

Thanks for the in-depth reply! I think I understand better now. It sucks that it takes a while to rewrite a lot of things, but I guess that's the tradeoff. I like that OCaml has a lot of expressiveness and fewer "mistakes" (unlike, say, Java...). I'm willing to wait for them to get it right, even if it makes things weird. Thanks for all of the information!

4

u/[deleted] Mar 28 '16 edited Mar 28 '16

You're welcome. Don't be afraid of spawning multiple processes and passing "jobs" to worker threads, btw. That way you can even scale up your app to multiple computers each with multiple cores, not just multiple cores on a single computer. Message passing also eliminates many race conditions.

1

u/[deleted] Mar 28 '16

Gotcha. That sounds interesting actually. I'll remember this if I ever work on a large-scale project like that.

10

u/BluddyCurry Mar 28 '16

I don't disagree with most of the answer, but I would advise you to stay away from stating the performance characteristics of other languages, aside from the generalities of 'naive interpreters are slower than compiled languages', 'high level languages are slower than C' etc. This can only result in a flame war.

Microsoft's CLR is an amazing product of engineering. It's very fast, uses JIT technology, and is extremely competitive. OCaml simply doesn't have the resources to keep up with a behemoth like Microsoft. Where OCaml has the advantage is in the design decisions that couldn't be replicated in F#, such as modules (as you mentioned), functors and polymorphic variants. But F# has its own cool features, like built-in units of measure.

0

u/[deleted] Mar 28 '16

[deleted]

3

u/[deleted] Mar 28 '16 edited Mar 28 '16

On the C side it looks like this

On the OCaml side, you can use ctypes to wrap your C function so it's called transparently. Then use Lwt_preemptive.detach to call your C function so that it gets its own OS level thread. Combined with releasing the lock in C, you get true multithreading, type safety (as long as your ctypes bindings are correct) and a monadic interface with Lwt. It looks and feels as if your code is just another async Lwt function, but it's actually truly parallel.

0

u/[deleted] Mar 28 '16

[deleted]

1

u/[deleted] Mar 28 '16

Thanks, I should have been more careful. I just wanted an update about this specific repo, though. :)