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

Ignacio Martinez |gn@c|o82 @end|ng |rom gm@||@com
Fri Feb 1 22:51:10 CET 2019


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