From kevin.hendricks at sympatico.ca  Wed Mar 18 17:08:17 2009
From: kevin.hendricks at sympatico.ca (Kevin Hendricks)
Date: Wed, 18 Mar 2009 12:08:17 -0400
Subject: [Rd] R thread safe
In-Reply-To: <83E1179B-072A-42F1-98FB-A06790DA393D@r-project.org>
References: <49C0F021.8000108@health.sdu.dk>
	<4BD0BA7D-242B-4D27-A346-745AB8AD088E@r-project.org>
	<4f1819890903180711pcdd2bdbuc4eb15f7af8575f4@mail.gmail.com>
	<83E1179B-072A-42F1-98FB-A06790DA393D@r-project.org>
Message-ID: <BLU0-SMTP15182A31C747C6DD88103A87990@phx.gbl>

>

Hi,


> I don't think so, because IMHO it makes no sense - you're missing  
> the main point that R is not thread safe. There are ways to use  
> threads from within R very cautiously (see Luke's parallelized  
> vector math operations for R for example). There are many good  
> methods to use threads in general (pthreads, OpenMP, GCD, ...) and  
> you can do that as long as you don't use memory allocation in R and  
> don't call any R functions that may do that (which is most of  
> them ;)). Making R thread-safe is not really an issue of a threading  
> toolkit...


Memory allocation does not necessarily make a function non-reentrant  
unless non-shared static variables are involved.  There are a number  
of thread-safe malloc implementations.    I admit,  I have not looked  
at the R-internals in a long time.

Based on converting code to be thread safe when I helped port the JDK  
to Linux, I was amazed about how much code was already reentrant  
capable and therefore basically thread-safe (or could be made so with  
small effort - adding a few locks).  In fact the original JVM (jdk  
1.0) had a "green-threads" implementation that basically ended up  
adding wrappers to most of the memory allocation and io system calls  
system calls to make the whole thing work.

  How much of the code itself is now reentrant safe - I noticed that  
some of the R-internal routines actually used reentrant code and even  
recursion?  How hard would it be to make the internal object/memory  
allocation scheme thread-safe?  As you noted there are many posix  
threads (pthreads) implmentations out there.

Is there any official effort underway to make R thread-safe? If so,  
are they looking for volunteers.  Would making R fully thread-safe  
really make that much sense given you can parallelize vector/matrix  
operations now (as you noted) which probably provides the most bang  
for the buck.

Thanks,

Kevin



[Rd] R thread safe
Kevin Hendricks