Set the number of threads using openmp with .Fortran?
Ignacio, Maybe this is something else to try. Write back and explain if this works or not. Parallel Programming in Fortran 95 using OpenMP OMP_NUM_THREADS This environment variable specifies the number of threads to be used during execution of the parallel regions https://www.openmp.org/wp-content/uploads/F95_OpenMPv1_v2.pdf Andre Mikulec Andre_Mikulec at Hotmail.com
From: R-devel <r-devel-bounces at r-project.org> on behalf of Ignacio Martinez <ignacio82 at gmail.com>
Sent: Saturday, February 2, 2019 5:14 PM
To: R-devel
Subject: Re: [Rd] Set the number of threads using openmp with .Fortran?
Sent: Saturday, February 2, 2019 5:14 PM
To: R-devel
Subject: Re: [Rd] Set the number of threads using openmp with .Fortran?
understanding is that this should also work on windows, is that correct?*
If so, what should I do? differently?
To get it to work on Linux, I modified my R script as follows:
#' OpenMP Hello World
#'
#' @param nthreads The number of threads that you want to use
#' @example
#' hello(nthreads=2)
#' @export
#' @useDynLib helloOpenMP, .registration = TRUE
hello <- function(nthreads=4) {
(OpenMPController::omp_set_num_threads(nthreads))
.Fortran("hello")
return('Each thread will say hi to you!')
}
> hello(nthreads = 2) Hello from 0
Hello from 1
[1] "Each thread will say hi to you!"
you!" without the Hello from X
Thanks for your help,
Ignacio
On Sat, Feb 2, 2019 at 11:34 AM Ignacio Martinez <ignacio82 at gmail.com>
wrote:
>
>
> 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 at 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]]
______________________________________________
R-devel at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel