News for hackers. Stuff that matters.
Re:Multicore OCaml: April 2021 by sadiq on Thursday May 13, 2021 @06:26AM (#27140536)
Happy to answer any questions.
Re:Multicore OCaml: April 2021 by NanoCoaster on Thursday May 13, 2021 @07:25AM (#27140883)
I'm sorry if these are dumb questions, I'd be totally fine with some pointers in the right directions if you don't want to waste your time :)

I'm under the impression that the implementation is based on algebraic effects. Does that include extending the language in a way that lets users define their own effects & handlers?

Also, how's the performance? Last time I looked this stuff up (and played around with Eff), which was quite a while ago, I was told that regular usage of effects may impact performance quite noticeably.

Re:Multicore OCaml: April 2021 by sadiq on Thursday May 13, 2021 @10:33AM (#27142502)
Not dumb questions at all.

Multicore adds parallelism via Domains (which are essentially heavyweight threads) and concurrency via Effects and fibers. There's a multicore GC that supports both of those.

We plan to upstream things in two parts. First domains-only parallelism and then effects as a follow-up. When the latter lands users will be able to define their own effects and handlers, yes.

Performance is pretty good, you can see our PLDI2021 paper for a proper performance evaluation and loads more details: https://arxiv.org/abs/2104.00250

Re:Multicore OCaml: April 2021 by NanoCoaster on Thursday May 13, 2021 @11:14AM (#27143016)
Thank you, interesting stuff. Very much looking forward to user-definable effects! I like Ocaml in principal, but haven't yet strayed beyond learning the very basics some time ago. Seeing a usable implementation of algebraic effects in a somewhat popular language...that I gotta see :)
Re:Multicore OCaml: April 2021 by sadiq on Thursday May 13, 2021 @11:49AM (#27143470)
Worth pointing out you don't just need to stop at seeing it, you can get started playing around with it today: https://github.com/ocaml-multicore/multicore-opam#install-mu...
Re:Multicore OCaml: April 2021 by NanoCoaster on Thursday May 13, 2021 @12:20PM (#27143932)
Cool, thanks for the link. Will definitely play around with it as soon as I find the time.

Too many programming languages, too little free time, sadly.

Re:Multicore OCaml: April 2021 by wuschel on Thursday May 13, 2021 @07:31AM (#27140925)
Hello sadiq, I have some more general questions for you:

As understand that currently, the situation is analogous to Python. The GIL allows for process based concurrency, with the well known disadvantage regarding memory consumption. Also, my guess is that OCaml relies library based solutions at the moment?

1) What does the introduction of MultiCore potential mean for Ocaml? Will Ocaml be a much better fit to run a webserver backend? Could you perhaps give a comparison to other programming languages?

2) How will Ocaml stand out with Multicore in the PL world, and what solutions would be it uniquely suited for?

3) What tools are going to be present to deal with bugs introduced by your new runtime e.g. race conditions?

4) If one wants to have a go at Multicore and play around with it, where/how do I start?

Many thanks!

Re:Multicore OCaml: April 2021 by sadiq on Thursday May 13, 2021 @10:50AM (#27142711)
Good questions!

1) As I mentioned in https://news.ycombinator.com/item?id=27142502 there is support for parallelism and concurrency.

Giving an example of where these might be useful in a webservice.

The addition of shared-memory parallelism is beneficial where you might have a great deal of shared state that needs to be used to service requests. An in-memory cache is a good example - with a processed-based approach managing read/writes and avoiding significant overhead from marshalling the data is difficult.

Concurrency via effects at a minimum can make writing network-based services much more pleasant (and debuggable!). See the examples in https://arxiv.org/abs/2104.00250 where programs can be written in a direct-style similar to blocking IO but using effects are transformed to use asynchronous interfaces. There's work going on in the project at the moment to build fast cross-platform IO implementations that sit atop of uring/gcd/iocp.

3) This is a good question and one we're still working on. I think one of the lead developers KC has a few good ideas about instrumentation we can do to enable detecting races to global state. It's certainly going to be an issue for people porting large codebases.

4) This is the place to start: https://github.com/ocaml-multicore/multicore-opam#install-mu... .

Re:Multicore OCaml: April 2021 by wuschel on Friday May 14, 2021 @03:28AM (#27151717)
Thank you for the your answer! I will check out the URIs.
Re:Multicore OCaml: April 2021 by hajile on Thursday May 13, 2021 @09:28AM (#27141820)
StandardML implementations have had good multicore support for decades now despite having only a tiny fraction of the users and development time. Meanwhile, Ocaml has been promising support for years.

What in Ocaml makes this so much harder to implement?

Re:Multicore OCaml: April 2021 by rwmj on Thursday May 13, 2021 @09:47AM (#27142020)
OCaml (or its immediate predecessor[1]) had a multicore implementation, but it was dropped because of its complexity and effect on single-thread performance. The challenge is to add it back in a way that is maintainable and doesn't negatively affect current users.

[1] https://www.researchgate.net/publication/2774662_Concurrent_...

Re:Multicore OCaml: April 2021 by sadiq on Thursday May 13, 2021 @10:29AM (#27142451)
As the sibling comment mentions, the hard part is actually retrofitting multicore whilst maintaining compatibility _and_ performance.

Our paper last year covers most of why this is tricky: https://arxiv.org/abs/2004.11663

Re:Multicore OCaml: April 2021 by 4ad on Thursday May 13, 2021 @11:26AM (#27143195)
Algebraic effects as described in the OCaml papers about effects are dynamically typed. I recently heard in some talk that this idea was abandoned, and the new algebraic effects are in fact statically typed. Do I remember this correctly? If so, where can I read about these new algebraic effects?
Re:Multicore OCaml: April 2021 by kcsrk on Thursday May 13, 2021 @11:53PM (#27150507)
Abandoned is perhaps too strong a term :-). Effect handlers in the language are supported by fibers, lightweight stacklets managed by the runtime. The details of the implementation can be found in the upcoming research paper in PLDI'21 conference [1]. The effect handlers in Multicore OCaml today do not provide effect safety. Programs are not statically guaranteed to handle all the effects they may perform. This is only as bad as exceptions in OCaml and every other mainstream language with exceptions.

We are working on developing an effect system, which will ensure effect safety i.e, the compiler ensures that all the effects performed are caught. You also get a nice inferred type that says what effects a particular function may perform; if it performs none, then it is a pure function! This implementation would still use the current fiber support in the runtime. Leo White, one of the developers of Multicore OCaml had given a talk on this new effect system a few years ago [2]. That's the best place today to learn about the new effect handlers.

The plan is to first add the fiber runtime support to OCaml without the syntax extensions for effect handlers, and then introduce syntax along with the effect system.

[1] https://arxiv.org/abs/2104.00250

[2] https://www.janestreet.com/tech-talks/effective-programming/

Re:Multicore OCaml: April 2021 by intc on Thursday May 13, 2021 @08:16AM (#27141229)
Could you explain (in simple terms if possible) how the Multicore OCaml achieves a memory model which is much simpler on more efficient than in Java or C (mentioned at https://github.com/ocaml-multicore/ocaml-multicore/wiki)?

Didn't see any mentions of critical sections (mutexes) with C++ examples in the documentation ("Bounding Data Races in Space and Time"). I'm not sure I understand the comparisons the writers are presenting.

Re:Multicore OCaml: April 2021 by jlouis on Thursday May 13, 2021 @10:04AM (#27142217)
The key problem is program transformation, and in particular optimizations. Different CPUs/ISAs, and compilers, might want to transform your program to make it run faster.

However, in the multi-core setting, data races pose bounds and limits on how much you can trigger those optimizations. The program doesn't generally execute sequentially in a way that can be entirely reasoned about. Instructions might be reordered for the sake of the program to run faster.

Programmers can't work with that. So one proposes a memory model. Follow these rules, and our optimizations won't alter the behavior of the program. They kind-of describes what happens "in between" the critical sections of the program, hence the lack of a mutex mention.

The paper presents a local property and then shows, formally, that this property is enough to guarantee an efficient memory model. That is, a model in which you can perform optimizations, while programmers can still reason about the programs behavior.

The crux of the paper is that the property is local. This is new, because memory models which came before it are global: to reason about correctness, you have to consider the whole program, rather than consider a small (local) subset. OCaml requires more safety than most programming languages, so this is good for the fact that you can now compose local fragments of OCaml programs, without having to worry about a global safety property.

The property is also simpler for programmers to reason about.

The way you "use" the paper is that you adapt your optimizations to follow the property, and you make sure that the virtual memory model is implemented the same way on different architectures.

Finally, the examples: they explore the idea of a local reasoning. In particular, they show why the (existing) global properties fail if you view them under the stronger requirement of local reasoning. It's the setup for the paper, since it means you can't just use the existing models. They need to be adapted if you want a more localized property.

Re:Multicore OCaml: April 2021 by kcsrk on Thursday May 13, 2021 @11:32AM (#27143265)
(One of the authors of the mentioned paper [1])

Firstly, if you are using high-level synchronisation mechanisms such as mutexes and condition variables, or higher-level concurreny libraries such as java.util.concurrent, you shouldn't worry about the memory model. C++, Java and OCaml ensure that properly synchronised programs do not exhibit surprising behaviours. Such programs have sequentially consistent semantics i.e, the observed behaviour is one of the permitted interleavings of the threads in the program. Rust inherits C++ memory model [2], but if you are using the safe subset, then you will never have to think about it. Memory model is important only to those who write the concurrency libraries. If you are still keen, read on.

The OCaml memory model is certainly simpler than the C++ and Java memory model, but being more efficient is not one of our goals. C++ memory model permits a partially-ordered lattice of stronger memory accesses starting from access to non-atomic memory locations to sequentially consistent access with increasing cost as you move up the lattice. OCaml memory model only provides two -- atomic and non-atomic, representing approximately the top and the bottom of the lattice.

OCaml memory model is also stronger than Java in that our data races are bound not-only in space like Java (data races on certain variables don't affect behaviours on other variables) but also in time (surprising behaviours stop affecting the program after the race ends unlike Java; see example 2 from [1]). This permits modular reasoning of racy and non-racy parts of the program which is not the case with C++ and Java.

The catch is that we have to disallow load-to-store reordering to get the stronger guarantees. Relaxed memory models such as ARM and Power do in fact permit these reorderings, and we have to compile OCaml code (including sequential one) such that the load-to-store reordering is disallowed. This can be done fairly cheaply. It is free on x86 which doesn't perform load-to-store reorderings, and has a small cost (up to 3%) on ARM and Power architectures whose memory models permit load-to-store reorderings.

[1] https://kcsrk.info/papers/pldi18-memory.pdf

[2] https://doc.rust-lang.org/nomicon/atomics.html

Re:Multicore OCaml: April 2021 by troutwine on Thursday May 13, 2021 @12:12PM (#27143842)
> Rust inherits C++ memory model [2], but if you are using the safe subset, then you will never have to think about it.

Small correction, atomics are part of the safe subset of Rust. At a certain point it's important to have a work-a-day knowledge of the memory model with regard to atomic numerics. Dealing with allocated types, now, that's a whole different area and is specialized knowledge.

Re:Multicore OCaml: April 2021 by tialaramex on Thursday May 13, 2021 @01:46PM (#27145004)
Still learning Rust here. If I use Rust's safe subset Atomics, am I correct in thinking that I am able to write code that on some platforms does something I didn't expect because I didn't ask for an Ordering I needed to make it do what I meant?

For example if I implement an algorithm that ought to be Acquire / Release (e.g. to build my own custom mutual exclusion) but I tell Rust it's OK to have Relaxed semantics, it sounds like on this PC (an x86-64) it will work anyway, but on some other systems it won't.

And I'd have achieved this goof without writing unsafe Rust, just the same way as if I screwed up a directory traversing algorithm because I relied on semantics not present in all file systems?

Re:Multicore OCaml: April 2021 by troutwine on Thursday May 13, 2021 @02:04PM (#27145234)
This is a good question. Yes, it's exactly the same kind of problem. There is potentially a difference between Rust's memory model and what's actually present on any given target host. x64 has a "strong" memory model which that ordering will always be implicitly acquire/release. Compare this to ARM which is "weak" where your relaxed ordering will actually be relaxed. (There's actually quite a bit more nuance, discussed well here[0].) It's important to write code that is correct with regard to Rust's memory model so that it's portable, but if you don't have a weakly ordered machine to test on it's tricky. Loom[1] is helpful in this regard. This is true of any language that allows you to write atomic code where you specify the ordering.

[0] https://preshing.com/20120930/weak-vs-strong-memory-models/ [1] https://github.com/tokio-rs/loom

Re:Multicore OCaml: April 2021 by zRedShift on Thursday May 13, 2021 @02:20PM (#27145427)
You can play with atomics as much as you want in safe Rust but you can't cause a data race without aliased mutability, which means using UnsafeCell, raw pointer operations or a & to &mut transmute (unsound and UB as long as noalias is enabled in LLVM), and all require unsafe.
Re:Multicore OCaml: April 2021 by mirekrusin on Thursday May 13, 2021 @07:25AM (#27140886)
Is it currently possible to play with multicore ocaml on M1?
Re:Multicore OCaml: April 2021 by mseri on Thursday May 13, 2021 @07:55AM (#27141081)
I think not yet: https://github.com/ocaml-multicore/ocaml-multicore/issues/86...

Fwiw, OCaml itself can be compiled natively on M1.

Re:Multicore OCaml: April 2021 by sadiq on Thursday May 13, 2021 @10:35AM (#27142528)
Yes, arm64 support is non-functional but we're planning to get it back in to a working state very soon.
Re:Multicore OCaml: April 2021 by agumonkey on Thursday May 13, 2021 @05:17PM (#27147598)
How much resources are going into multicore compared to other needs / improvements in ocaml (if any) ?
Re:Multicore OCaml: April 2021 by sadiq on Thursday May 13, 2021 @05:25PM (#27147676)
Not sure I have enough of an overview to comment on that in general, unfortunately.

The overview Anil gave at the end of last year on the OCaml Platform should give you an idea of the many other strands of work that are going on: https://ocaml.org/platform/

Re:Multicore OCaml: April 2021 by agumonkey on Thursday May 13, 2021 @08:54PM (#27149383)
Thanks man, and Best wishes