Skip to content

Commit

Permalink
#174 add option to read kernel filename from env var. Allow add_kerne…
Browse files Browse the repository at this point in the history
…ls() to be called multiple times.
  • Loading branch information
arporter committed Jun 15, 2018
1 parent c998f56 commit aeb3563
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions lib/opencl/ocl_env_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module ocl_env_mod

private

!> Whether or not this module has been initialised
logical, save :: cl_env_initialised = .False.
!> Pointer to the OpenCL device being used
integer(c_intptr_t) :: cl_device
!> The OpenCL context
Expand All @@ -21,8 +23,10 @@ module ocl_env_mod

!> The OpenCL kernels used by the model
integer, save :: cl_num_kernels
character(len=CL_UTIL_STR_LEN), allocatable :: cl_kernel_names(:)
integer(c_intptr_t), target, allocatable :: cl_kernels(:)
!> The maximum number of kernels we can store
integer, parameter :: cl_max_num_kernels = 30
character(len=CL_UTIL_STR_LEN) :: cl_kernel_names(cl_max_num_kernels)
integer(c_intptr_t), target :: cl_kernels(cl_max_num_kernels)

public ocl_env_init
public cl_context, cl_device, get_num_cmd_queues, get_cmd_queues
Expand All @@ -32,15 +36,16 @@ module ocl_env_mod
!===================================================

!> Initialise the GOcean environment
subroutine ocl_env_init(opencl)
subroutine ocl_env_init()
use ocl_utils_mod, only: init_device
implicit none
!> Whether or not to use OpenCL
logical, optional :: opencl
integer :: ierr

if(cl_env_initialised)return

! Initialise the OpenCL device
call init_device(cl_device, cl_version_str, cl_context)

! Create command queue(s)
cl_num_queues = 4
allocate(cl_cmd_queues(cl_num_queues), Stat=ierr)
Expand All @@ -49,6 +54,12 @@ subroutine ocl_env_init(opencl)
end if
call init_cmd_queues(cl_num_queues, cl_cmd_queues, cl_context, cl_device)

! At this point we have no kernels
cl_num_kernels = 0

! Environment now initialised
cl_env_initialised = .True.

end subroutine ocl_env_init

!===================================================
Expand All @@ -58,25 +69,36 @@ subroutine add_kernels(nkernels, kernel_names, filename)
use ocl_utils_mod, only: get_program, get_kernel, release_program
integer, intent(in) :: nkernels
character(len=*), intent(in) :: kernel_names(nkernels)
character(len=*), intent(in) :: filename
character(len=*), intent(in), optional :: filename
! Locals
integer :: ik, ierr
integer(c_intptr_t), target :: prog
character(len=300) :: lfilename

! Get a program object containing all of our kernels
prog = get_program(cl_context, cl_device, cl_version_str, filename)
if(.not. cl_env_initialised)then
call ocl_env_init()
end if

cl_num_kernels = nkernels
if(.not. present(filename))then
call get_environment_variable("PSYCLONE_KERNELS_FILE", lfilename)
else
lfilename = filename
end if

allocate(cl_kernels(cl_num_kernels), cl_kernel_names(cl_num_kernels), &
Stat=ierr)
if(ierr /= 0)then
stop "Failed to allocate memory for kernel table"
if((nkernels + cl_num_kernels) > cl_max_num_kernels)then
write(*,"('add_kernels: Adding ',I2,' kernels will exceed the &
&maximum number of ',I3,' - increase ocl_env_mod::cl_max_num_kernels')") &
nkernels, cl_max_num_kernels
stop
end if

do ik = 1, cl_num_kernels
cl_kernels(ik) = get_kernel(prog, kernel_names(ik))
! Get a program object containing all of our kernels
prog = get_program(cl_context, cl_device, cl_version_str, lfilename)

do ik = 1, nkernels
cl_kernels(cl_num_kernels+ik) = get_kernel(prog, kernel_names(ik))
end do
cl_num_kernels = cl_num_kernels + nkernels

! Release the program now that we've created the kernels
call release_program(prog)
Expand Down Expand Up @@ -131,7 +153,7 @@ end function get_cmd_queues

!===================================================

subroutine gocean_release()
subroutine ocl_release()
integer :: i

do i=1, cl_num_kernels
Expand All @@ -142,6 +164,6 @@ subroutine gocean_release()

call release_context(cl_context)

end subroutine gocean_release
end subroutine ocl_release

end module ocl_env_mod

0 comments on commit aeb3563

Please sign in to comment.