[Rd] Set the number of threads using openmp with .Fortran?

Ignacio Martinez |gn@c|o82 @end|ng |rom gm@||@com
Sat Feb 2 17:34:15 CET 2019


I was able to make some progress by using this
<https://github.com/bert9bert/ParallelForest/blob/master/src/Makevars> as a
reference. Now:

*This is my makevars:*
#####  Compiler flags  #####
PKG_FCFLAGS = $(SHLIB_OPENMP_FFLAGS)
PKG_LIBS = $(SHLIB_OPENMP_CFLAGS)

#####  Phony target for R's build system to invoke  #####
all: $(SHLIB)

#####  Clean target  #####
clean:
rm -f *.o *.mod

And when I run my hello world function all the threads are used
regardless of what i specify:

> hello(ncores = 2) Hello from           1
 Hello from           3
 Hello from           0
 Hello from           9
 Hello from           8
 Hello from           2
 Hello from           6
 Hello from          10
 Hello from          11
 Hello from           5
 Hello from           7
 Hello from           4
$ncores
[1] 2



What am I missing? My Fortran subroutine uses !$call
omp_set_num_threads(ncores) to set the number of threads. Why is this
not working? How can I fix it?

Thanks a lot for the help

Ignacio


On Fri, Feb 1, 2019 at 4:51 PM Ignacio Martinez <ignacio82 using gmail.com> wrote:

> Hi everybody,
>
> I'm trying to develop an R package with Fortran and OpenMP. I wrote a
> simple hello world but I'm not able to set the number of threads. I found this
> old email chain
> <http://r.789695.n4.nabble.com/Set-the-number-of-threads-using-openmp-with-C-td2284685.html> and
> I tried to set my compile instructions accordingly but i had no luck.
>
> *This is my makevars:*
>
> PKG_FCFLAGS="-fno-stack-protector"
> F90FLAGS =  "-fopenmp"
> LDFLAGS = "-fopenmp"
>
> *This is my Fortran module:*
>
> module hello_openmp
>    use omp_lib
>    implicit none
>    contains
>
> subroutine hello(ncores) bind(C, name="hello_")
>   use, intrinsic                                         :: iso_c_binding,
> only : c_double, c_int
>   integer(c_int), intent(in)                             :: ncores
>   integer                                                :: iam
>   ! Specify number of threads to use:
>   !$call omp_set_num_threads(ncores)
>   !$omp parallel private(iam)
>   iam=omp_get_thread_num()
>   !$omp critical
>   write(*,*) 'Hello from', iam
>   !$omp end critical
>   !$omp end parallel
> end subroutine hello
>
> end module hello_openmp
>
>
> *and this is my R function:*
>
> #'@export
> #'@useDynLib helloOpenMP, .registration = TRUE
>
> hello <- function(ncores=4) {
>   .Fortran("hello", ncores = as.integer(ncores))
> }
>
>
> *Alas, when I call hello things only run with one thread:*
>
> > hello(ncores = 2)$ncores
>  Hello from           0
> [1] 2
>
>
> Could you point me in the right direction? What am I missing?
>
>
> Thanks,
>
>
> Ignacio
>
>

	[[alternative HTML version deleted]]



More information about the R-devel mailing list