Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiled omp do program #4030

Open
Pranavchiku opened this issue May 18, 2024 · 1 comment
Open

compiled omp do program #4030

Pranavchiku opened this issue May 18, 2024 · 1 comment
Labels
openmp Issue or pull request specific to openmp

Comments

@Pranavchiku
Copy link
Contributor

Pranavchiku commented May 18, 2024

This now works!

With #4028, we can now move ahead and see if we can compile other codes with LFortran

program openmp
use omp_lib

integer :: i
!$omp parallel
!$omp do
do i=1,100
  print *, "i = ", i, " thread = ", omp_get_thread_num()
end do
!$omp end do
!$omp end parallel
end program

Can be transformed to

module thread_data_module
    use, intrinsic :: iso_c_binding
    type, bind(C) :: thread_data
        integer(c_int) :: n
    end type thread_data
end module thread_data_module

subroutine lcompilers_function(data) bind(C)
use thread_data_module
use iso_c_binding
use libgompinterop
implicit none
type(c_ptr), value :: data
type(thread_data), pointer :: tdata

integer(c_int) :: i, n, num_threads, chunk, leftovers, thread_num, start, end

call c_f_pointer(data, tdata)

n = tdata%n

num_threads = omp_get_max_threads()
chunk = n / num_threads
leftovers = mod(n, num_threads)

thread_num = omp_get_thread_num()
start = chunk * thread_num

if (thread_num < leftovers) then
    start = start + thread_num
else
    start = start + leftovers
end if

end = start + chunk

if (thread_num < leftovers) then
    end = end + 1
end if

do i = start, end
    ! print *, "Thread ", thread_num, " is processing element ", i
end do

call GOMP_barrier()

end subroutine

program main
use libgompinterop
use thread_data_module
implicit none

interface
subroutine lcompilers_function(data) bind(C)
use iso_c_binding
type(c_ptr), value :: data
end subroutine
end interface

type(thread_data), target :: data
type(c_ptr) :: tdata

data%n = 1000000

tdata = c_loc(data)

call GOMP_parallel(c_funloc(lcompilers_function), tdata, 0, 0)

end program main

Which can be compiled using

% time lfortran -c libgompinterop.f90 && time lfortran d-transformed.f90 -L/Users/pranavchiku/repos/llvm-project/openmp/build/runtime/src/ -lomp -Wl,-rpath,/Users/pranavchiku/repos/llvm-project/openmp/build/runtime/src/
lfortran -c libgompinterop.f90  0.08s user 0.01s system 96% cpu 0.088 total
lfortran d-transformed.f90  -lomp   0.13s user 0.02s system 122% cpu 0.122 total
@Pranavchiku
Copy link
Contributor Author

libgompinterop.f90

module libgompinterop
use iso_c_binding
implicit none

interface
subroutine GOMP_parallel (fn, data, num_threads, flags) bind (C, name="GOMP_parallel")
import :: c_funptr, c_ptr, c_int
type(c_funptr), value :: fn
type(c_ptr), value :: data
integer(c_int), value :: num_threads
integer(c_int), value :: flags
end subroutine

subroutine GOMP_barrier() bind(C, name="GOMP_barrier")
end subroutine

subroutine GOMP_critical_start() bind(C, name="GOMP_critical_start")
end subroutine

subroutine GOMP_critical_end() bind(C, name="GOMP_critical_end")
end subroutine

function omp_get_max_threads() bind(c, name="omp_get_max_threads")
import :: c_int
integer(c_int) :: omp_get_max_threads
end function omp_get_max_threads

function omp_get_thread_num() bind(c, name="omp_get_thread_num")
import :: c_int
integer(c_int) :: omp_get_thread_num
end function omp_get_thread_num

end interface

end module

@Pranavchiku Pranavchiku added the openmp Issue or pull request specific to openmp label May 18, 2024
@Pranavchiku Pranavchiku changed the title compile omp do program compiled omp do program May 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
openmp Issue or pull request specific to openmp
Projects
None yet
Development

No branches or pull requests

1 participant