From kev|nu@hey @end|ng |rom gm@||@com  Sat Apr 13 03:50:58 2019
From: kev|nu@hey @end|ng |rom gm@||@com (Kevin Ushey)
Date: Fri, 12 Apr 2019 18:50:58 -0700
Subject: [Rd] SUGGESTION: Settings to disable forked processing in R,
 e.g. parallel::mclapply()
In-Reply-To: <CAPLMX9Fxe1T+npHdkXAzugApuopXN9F+5HgRSk0MyosRngopsw@mail.gmail.com>
References: <CAFDcVCSPzMYE9A9V==Gs2yYTuH+YDqWhT_q=WU4AU5JL0WzX=A@mail.gmail.com>
 <CALEXWq3RapiDCTOFUerzxfiawYBu3LFxpCQpYRwmL3MQBh-S4g@mail.gmail.com>
 <CAPLMX9E6rcPNR0MMmaRPRQOBbhopiC43EBDEaHE56tWFGu5ZTQ@mail.gmail.com>
 <CALEXWq2gkuDW48SmGT+YWjn-GmuEjW56bsXj28xTRO=E-tZ48Q@mail.gmail.com>
 <CAPLMX9Fxe1T+npHdkXAzugApuopXN9F+5HgRSk0MyosRngopsw@mail.gmail.com>
Message-ID: <CAJXgQP1i0EGEmHj2UAqY-H3QnYfspsJ3BkhY8fGE6oOKCb9vnw@mail.gmail.com>

I think it's worth saying that mclapply() works as documented: it
relies on forking, and so doesn't work well in environments where it's
unsafe to fork. This is spelled out explicitly in the documentation of
?mclapply:

It is strongly discouraged to use these functions in GUI or embedded
environments, because it leads to several processes sharing the same
GUI which will likely cause chaos (and possibly crashes). Child
processes should never use on-screen graphics devices.

I believe the expectation is that users who need more control over the
kind of cluster that's used for parallel computations would instead
create the cluster themselves with e.g. `makeCluster()` and then use
`clusterApply()` / `parLapply()` or other APIs as appropriate.

In environments where forking works, `mclapply()` is nice because you
don't need to think -- the process is forked, and anything available
in your main session is automatically available in the child
processes. This is a nice convenience for when you know it's safe to
fork R (and know what you're doing is safe to do within a forked
process). When it's not safe, it's better to prefer the other APIs
available for computation on a cluster.

Forking can be unsafe and dangerous, but it's also convenient and
sometimes that convenience can outweigh the other concerns.

Finally, I want to add: the onus should be on the front-end to work
well with R, and not the other way around. I don't think it's fair to
impose extra work / an extra maintenance burden on the R Core team for
something that's already clearly documented ...

Best,
Kevin


On Fri, Apr 12, 2019 at 6:04 PM Travers Ching <traversc at gmail.com> wrote:
>
> Hi Inaki,
>
> > "Performant"... in terms of what. If the cost of copying the data
> > predominates over the computation time, maybe you didn't need
> > parallelization in the first place.
>
> Performant in terms of speed.  There's no copying in that example
> using `mclapply` and so it is significantly faster than other
> alternatives.
>
> It is a very simple and contrived example, but there are lots of
> applications that depend on processing of large data and benefit from
> multithreading.  For example, if I read in large sequencing data with
> `Rsamtools` and want to check sequences for a set of motifs.
>
> > I don't see why mclapply could not be rewritten using PSOCK clusters.
>
> Because it would be much slower.
>
> > To implement copy-on-write, Linux overcommits virtual memory, and this
> >  is what causes scripts to break unexpectedly: everything works fine,
> > until you change a small unimportant bit and... boom, out of memory.
> > And in general, running forks in any GUI would cause things everywhere
> > to break.
>
> > I'm not sure how did you setup that, but it does complete. Or do you
> > mean that you ran out of memory? Then try replacing "x" with, e.g.,
> > "x+1" in your mclapply example and see what happens (hint: save your
> > work first).
>
> Yes, I meant that it ran out of memory on my desktop.  I understand
> the limits, and it is not perfect because of the GUI issue you
> mention, but I don't see a better alternative in terms of speed.
>
> Regards,
> Travers
>
>
>
>
> On Fri, Apr 12, 2019 at 3:45 PM I?aki Ucar <iucar at fedoraproject.org> wrote:
> >
> > On Fri, 12 Apr 2019 at 21:32, Travers Ching <traversc at gmail.com> wrote:
> > >
> > > Just throwing my two cents in:
> > >
> > > I think removing/deprecating fork would be a bad idea for two reasons:
> > >
> > > 1) There are no performant alternatives
> >
> > "Performant"... in terms of what. If the cost of copying the data
> > predominates over the computation time, maybe you didn't need
> > parallelization in the first place.
> >
> > > 2) Removing fork would break existing workflows
> >
> > I don't see why mclapply could not be rewritten using PSOCK clusters.
> > And as a side effect, this would enable those workflows on Windows,
> > which doesn't support fork.
> >
> > > Even if replaced with something using the same interface (e.g., a
> > > function that automatically detects variables to export as in the
> > > amazing `future` package), the lack of copy-on-write functionality
> > > would cause scripts everywhere to break.
> >
> > To implement copy-on-write, Linux overcommits virtual memory, and this
> > is what causes scripts to break unexpectedly: everything works fine,
> > until you change a small unimportant bit and... boom, out of memory.
> > And in general, running forks in any GUI would cause things everywhere
> > to break.
> >
> > > A simple example illustrating these two points:
> > > `x <- 5e8; mclapply(1:24, sum, x, 8)`
> > >
> > > Using fork, `mclapply` takes 5 seconds.  Using "psock", `clusterApply`
> > > does not complete.
> >
> > I'm not sure how did you setup that, but it does complete. Or do you
> > mean that you ran out of memory? Then try replacing "x" with, e.g.,
> > "x+1" in your mclapply example and see what happens (hint: save your
> > work first).
> >
> > --
> > I?aki ?car
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] SUGGESTION: Settings to disable forked processing in R, e.g. parallel::mclapply()
Kevin Ushey