Skip to content

Commit

Permalink
WIP: python macros adapted for host only functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
awnawab committed Jan 28, 2025
1 parent 16100f3 commit 8bfd956
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmake/field_api_get_offload_model.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ macro( field_api_get_offload_model )
DESCRIPTION "CUDA" DEFAULT ON
CONDITION CMAKE_CUDA_COMPILER AND HAVE_ACC )

set(FIELD_API_OFFLOAD_MODEL "None")
set(FIELD_API_OFFLOAD_MODEL "HostOnly")
if( HAVE_CUDA )
set(FIELD_API_OFFLOAD_MODEL "NvidiaOpenACCCUDA")
elseif( HAVE_ACC )
Expand Down
1 change: 1 addition & 0 deletions python_utils/offload_backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
# nor does it submit to any jurisdiction.

from offload_backends.nvidia import *
from offload_backends.host_only import *
18 changes: 18 additions & 0 deletions python_utils/offload_backends/host_only.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# (C) Copyright 2022- ECMWF.
# (C) Copyright 2022- Meteo-France.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.


__all__ = ['HostOnly']

class HostOnly():
"""
A dummy class only to be used if GPU offload is disabled.
"""

pragma = ''
53 changes: 42 additions & 11 deletions python_utils/offload_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
# nor does it submit to any jurisdiction.

import fypp
from offload_backends import NvidiaOpenACC
from offload_backends import NvidiaOpenACC, HostOnly

"""
A common entry point for retrieving macros from the various GPU offload backends.
"""

_offload_map = {
'NvidiaOpenACC': NvidiaOpenACC
'NvidiaOpenACC': NvidiaOpenACC,
'HostOnly': HostOnly
}

def _wrap_lines(input_str, ref_len, pragma='', indent=0):
Expand Down Expand Up @@ -92,76 +93,106 @@ def _get_offload_backend():

return _offload_map[offload_model]

def _empty_string(*args, **kwargs):
"""Simple method to return an empty string."""
return ""

def _get_method(backend, method):
"""
Retrieve the appropriate method from the given backend.
"""

try:
return getattr(backend, method)
except AttributeError:
return _empty_string

def RuntimeApiImport(indent=0):
"""
Import the runtime API.
"""

backend = _get_offload_backend()
method = _get_method(backend, 'runtime_api_import')

return _format_lines(backend.runtime_api_import())
return _format_lines(method())

def CDevptrDecl(symbols, indent=0):
"""
Declare symbols of type `TYPE(C_DEVPTR)` (or equivalent).
"""

backend = _get_offload_backend()
method = _get_method(backend, 'c_devptr_declaration')

return _format_lines(backend.c_devptr_declaration(symbols))
return _format_lines(method(symbols))

def HostDataStart(symbols, indent=0):
"""
Start a `host_data` (or equivalent) region.
"""

backend = _get_offload_backend()
return _format_lines(backend.host_data_start(symbols), indent=indent, pragma=backend.pragma)
method = _get_method(backend, 'host_data_start')

return _format_lines(method(symbols), indent=indent, pragma=backend.pragma)

def HostDataEnd(indent=0):
"""
End a `host_data` (or equivalent) region.
"""

backend = _get_offload_backend()
return _format_lines(backend.host_data_end(), indent=indent)
method = _get_method(backend, 'host_data_end')

return _format_lines(method(), indent=indent)

def DevptrCLOC(symbol, indent=0):
"""
Get the C address of a device variable.
"""

backend = _get_offload_backend()
return _format_lines(backend.devptr_c_loc(symbol))
method = _get_method(backend, 'devptr_c_loc')

return _format_lines(method(symbol))

def CopyToDevice1D(dev, host, size, indent=0):
"""
Copy a contiguous section of data from host to device.
"""

backend = _get_offload_backend()
return _format_lines(backend.copy_to_device_1D(dev, host, size), indent=indent)
method = _get_method(backend, 'copy_to_device_1D')

return _format_lines(method(dev, host, size), indent=indent)

def CopyToDevice1DAsync(dev, host, size, queue, indent=0):
"""
Asynchronously copy a contiguous section of data from host to device.
"""

backend = _get_offload_backend()
return _format_lines(backend.copy_to_device_1D_async(dev, host, size, queue), indent=indent)
method = _get_method(backend, 'copy_to_device_1D_async')

return _format_lines(method(dev, host, size, queue), indent=indent)

def CopyFromDevice1D(dev, host, size, indent=0):
"""
Copy a contiguous section of data from device to host.
"""

backend = _get_offload_backend()
return _format_lines(backend.copy_from_device_1D(dev, host, size), indent=indent)
method = _get_method(backend, 'copy_from_device_1D')

return _format_lines(method(dev, host, size), indent=indent)

def CopyFromDevice1DAsync(dev, host, size, queue, indent=0):
"""
Asynchronously copy a contiguous section of data from device to host.
"""

backend = _get_offload_backend()
return _format_lines(backend.copy_from_device_1D_async(dev, host, size, queue), indent=indent)
method = _get_method(backend, 'copy_from_device_1D_async')

return _format_lines(method(dev, host, size, queue), indent=indent)

0 comments on commit 8bfd956

Please sign in to comment.