Ignacio Martinez
2019-Feb-01 21:51 UTC
[Rd] Set the number of threads using openmp with .Fortran?
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]]
Ignacio Martinez
2019-Feb-02 16:34 UTC
[Rd] Set the number of threads using openmp with .Fortran?
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 1Hello 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]]
Ignacio Martinez
2019-Feb-02 22:14 UTC
[Rd] Set the number of threads using openmp with .Fortran?
I got this to work on Linux but it is not working on Windows. *My
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!"
Alas, on Windows the same command just returns "Each thread will say hi to
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]]
Possibly Parallel Threads
- Set the number of threads using openmp with .Fortran?
- Set the number of threads using openmp with .Fortran?
- Rcpp with OpenMP - Need example Makevars
- [LLVMdev] multithreaded performance disaster with -fprofile-instr-generate (contention on profile counters)
- Set the number of threads using openmp with .C