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

cuda.host_empty function #67

Open
wants to merge 135 commits into
base: devel
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
135 commits
Select commit Hold shift + click to select a range
c7a6638
Trigger tests on push to devel or main branch
EmilyBourne Mar 11, 2024
821a1c5
Add cuda workflow to test cuda developments on CI
EmilyBourne Mar 11, 2024
092b557
Trigger tests on push to devel or main branch
EmilyBourne Mar 11, 2024
02a2360
[init] Adding CUDA language/compiler and CodePrinter (#32)
bauom Feb 28, 2024
bd73514
Fix import handling (#49)
smazouz42 May 15, 2024
261c152
Add support for kernels (#42)
smazouz42 Jun 27, 2024
80f905b
Begin implementation of CUDA arrays: adding cudaempty and cudafull fu…
smazouz42 Jul 2, 2024
7e8cf9e
work in progress
smazouz42 Jul 3, 2024
2dbcfae
work in progress
smazouz42 Jul 3, 2024
f3911d5
work in progress
smazouz42 Jul 3, 2024
37289f9
work in progress
smazouz42 Jul 8, 2024
ba66b48
work in progress
smazouz42 Jul 8, 2024
406a88b
work in progress
smazouz42 Jul 8, 2024
3afad1b
work in progress
smazouz42 Jul 9, 2024
190c5a2
work in progress
smazouz42 Jul 10, 2024
eeeb249
cleaning up my PR
smazouz42 Jul 10, 2024
de0f5ab
cleaning up my PR
smazouz42 Jul 10, 2024
d6ba6ad
cleaning up my PR
smazouz42 Jul 10, 2024
8286a89
work in progress
smazouz42 Jul 10, 2024
96c3f29
work in progress
smazouz42 Jul 10, 2024
b414d62
work in progress
smazouz42 Jul 10, 2024
7c93416
Trigger tests on push to devel or main branch
EmilyBourne Mar 11, 2024
f8ec722
Add cuda workflow to test cuda developments on CI
EmilyBourne Mar 11, 2024
cc3a93e
Trigger tests on push to devel or main branch
EmilyBourne Mar 11, 2024
3be623d
[init] Adding CUDA language/compiler and CodePrinter (#32)
bauom Feb 28, 2024
b6d1549
Fix import handling (#49)
smazouz42 May 15, 2024
7da772a
Add support for kernels (#42)
smazouz42 Jun 27, 2024
ca701d9
Updated CUDA Name Clash Checker By Added CUDA-specific keywords (#60)
smazouz42 Jul 3, 2024
828d166
add handle for custom device (#61)
smazouz42 Jul 3, 2024
a28c724
work in progress
smazouz42 Jul 11, 2024
658bb83
Merge branch 'devel' of https://github.com/pyccel/pyccel-cuda into is…
smazouz42 Jul 13, 2024
22686d7
work in progress
smazouz42 Jul 14, 2024
ab68eb4
work in progress
smazouz42 Jul 14, 2024
73c4c81
work in progress
smazouz42 Jul 14, 2024
af4d097
Trigger tests on push to devel or main branch
EmilyBourne Mar 11, 2024
061996e
Add cuda workflow to test cuda developments on CI
EmilyBourne Mar 11, 2024
32457cc
Trigger tests on push to devel or main branch
EmilyBourne Mar 11, 2024
9f03889
[init] Adding CUDA language/compiler and CodePrinter (#32)
bauom Feb 28, 2024
57b643e
Fix import handling (#49)
smazouz42 May 15, 2024
af589a1
Add support for kernels (#42)
smazouz42 Jun 27, 2024
91d6101
Updated CUDA Name Clash Checker By Added CUDA-specific keywords (#60)
smazouz42 Jul 3, 2024
9234e99
add handle for custom device (#61)
smazouz42 Jul 3, 2024
c79b56d
work in progress
smazouz42 Jul 15, 2024
d482a8e
Merge branch 'devel' of https://github.com/pyccel/pyccel-cuda into is…
smazouz42 Jul 15, 2024
1c7ec43
work in progress
smazouz42 Jul 15, 2024
ba103ee
work in progress
smazouz42 Jul 16, 2024
947ce32
work in progress
smazouz42 Jul 16, 2024
1eeed75
adding test for host array
smazouz42 Jul 17, 2024
a8dbc18
add documentation for CUDA arrays
smazouz42 Jul 17, 2024
5d91031
fix: remove unnecessary spaces
smazouz42 Jul 17, 2024
7c74bbd
refactoring the code
smazouz42 Jul 17, 2024
95cf821
refactoring the code
smazouz42 Jul 17, 2024
0fc4a1b
refactoring the code
smazouz42 Jul 17, 2024
f43fba8
refactoring the code
smazouz42 Jul 17, 2024
f4546e1
fix a doc problem
smazouz42 Jul 17, 2024
ec0132b
fix a doc problem
smazouz42 Jul 17, 2024
9030dfe
fix a doc problem
smazouz42 Jul 17, 2024
e47373c
fix a doc problem
smazouz42 Jul 17, 2024
eee6202
fix a doc problem
smazouz42 Jul 17, 2024
afd05c1
make sure tests are running successfully
smazouz42 Jul 17, 2024
ff95029
fix a doc problem
smazouz42 Jul 17, 2024
21f70c0
Trigger tests on push to devel or main branch
EmilyBourne Mar 11, 2024
13efc4e
Add cuda workflow to test cuda developments on CI
EmilyBourne Mar 11, 2024
e07587d
Trigger tests on push to devel or main branch
EmilyBourne Mar 11, 2024
94b1357
[init] Adding CUDA language/compiler and CodePrinter (#32)
bauom Feb 28, 2024
c0006dd
Fix import handling (#49)
smazouz42 May 15, 2024
1145d91
Add support for kernels (#42)
smazouz42 Jun 27, 2024
31aae9d
Updated CUDA Name Clash Checker By Added CUDA-specific keywords (#60)
smazouz42 Jul 3, 2024
21c93b5
add handle for custom device (#61)
smazouz42 Jul 3, 2024
f2c1f65
Merge branch 'devel' of https://github.com/pyccel/pyccel-cuda into is…
smazouz42 Jul 18, 2024
d66442d
fix a doc problem
smazouz42 Jul 18, 2024
976e729
fix a doc problem
smazouz42 Jul 18, 2024
0289a77
debuging perpose
smazouz42 Jul 18, 2024
ca49b1b
debuging perpose
smazouz42 Jul 18, 2024
ce1ddbc
debuging perpose
smazouz42 Jul 18, 2024
717c4a4
debuging perpose
smazouz42 Jul 18, 2024
a19c559
debuging perpose
smazouz42 Jul 18, 2024
33842e9
debuging perpose
smazouz42 Jul 18, 2024
a52f4a2
debuging perpose
smazouz42 Jul 18, 2024
40d7c0d
debuging perpose
smazouz42 Jul 18, 2024
532cf03
debuging perpose
smazouz42 Jul 18, 2024
3424b83
debuging perpose
smazouz42 Jul 18, 2024
3c1387b
debuging perpose
smazouz42 Jul 18, 2024
c204481
debuging perpose
smazouz42 Jul 19, 2024
2517c43
debuging perpose
smazouz42 Jul 19, 2024
9778a86
debuging perpose
smazouz42 Jul 19, 2024
bc8b5b6
debuging perpose
smazouz42 Jul 19, 2024
f2909f4
debuging perpose
smazouz42 Jul 19, 2024
8f61d13
debuging perpose
smazouz42 Jul 19, 2024
7d02720
cleaning up my PR
smazouz42 Jul 19, 2024
a98a6c2
Target failing test
EmilyBourne Jul 19, 2024
e4fcff4
Examine compilation output
EmilyBourne Jul 19, 2024
73085bd
Run pyccel in verbose mode
EmilyBourne Jul 19, 2024
8215d77
Correct flag
EmilyBourne Jul 19, 2024
9e75cba
debuging perpose
smazouz42 Jul 19, 2024
f71e741
Examine compilation output
smazouz42 Jul 19, 2024
44f3503
Check files being compiled and existence
EmilyBourne Jul 19, 2024
82f1c69
Examine library copy
EmilyBourne Jul 19, 2024
b9e5c94
Trigger tests on push to devel or main branch
EmilyBourne Mar 11, 2024
df24e81
Add cuda workflow to test cuda developments on CI
EmilyBourne Mar 11, 2024
31d7247
Trigger tests on push to devel or main branch
EmilyBourne Mar 11, 2024
17aa0e6
[init] Adding CUDA language/compiler and CodePrinter (#32)
bauom Feb 28, 2024
2c58573
Fix import handling (#49)
smazouz42 May 15, 2024
0d154f8
Add support for kernels (#42)
smazouz42 Jun 27, 2024
2ffa7fc
Updated CUDA Name Clash Checker By Added CUDA-specific keywords (#60)
smazouz42 Jul 3, 2024
8eef19d
add handle for custom device (#61)
smazouz42 Jul 3, 2024
93dbc27
Merge branch 'devel' of https://github.com/pyccel/pyccel-cuda into is…
smazouz42 Jul 19, 2024
c02b661
include cuda_ndarrays.cu in package distribution
smazouz42 Jul 19, 2024
bbbf6f8
add , to cuda include
smazouz42 Jul 19, 2024
88d74d1
cleaning up my PR
smazouz42 Jul 19, 2024
2705949
cleaning up my PR
smazouz42 Jul 19, 2024
77a93e7
cleaning up my PR
smazouz42 Jul 19, 2024
ff74a4a
enable import cucomplex in ndarrays header
smazouz42 Jul 19, 2024
e138ae5
cleaning up my PR
smazouz42 Jul 19, 2024
8c55fd9
adding test for cuda array addition
smazouz42 Jul 21, 2024
d5b733f
refactoring the code
smazouz42 Jul 21, 2024
1fcb3a2
adding test for cuda 2d array addition
smazouz42 Jul 22, 2024
c5c9db1
cleaning up my PR
smazouz42 Jul 22, 2024
bbd46e1
cleaning up my PR
smazouz42 Jul 22, 2024
89cd3aa
cleaning up my PR
smazouz42 Jul 22, 2024
5c49cd3
cleaning up my PR
smazouz42 Jul 22, 2024
4ff9ed2
pdate Changelog
smazouz42 Jul 22, 2024
843429b
refactoring the code
smazouz42 Jul 22, 2024
0e9292e
Change doc of cuda+host_empty and fix some errors
smazouz42 Jul 24, 2024
8a4106f
Split a long line to make it readable
smazouz42 Jul 24, 2024
eea028a
fix doc string of host_empty
smazouz42 Jul 25, 2024
cc5a8cf
Trigger tests on push to devel or main branch
EmilyBourne Mar 11, 2024
a822c41
Add cuda workflow to test cuda developments on CI
EmilyBourne Mar 11, 2024
99b1838
Trigger tests on push to devel or main branch
EmilyBourne Mar 11, 2024
8828e56
[init] Adding CUDA language/compiler and CodePrinter (#32)
bauom Feb 28, 2024
7ad90da
Fix import handling (#49)
smazouz42 May 15, 2024
b3de549
Add support for kernels (#42)
smazouz42 Jun 27, 2024
4ac5182
Updated CUDA Name Clash Checker By Added CUDA-specific keywords (#60)
smazouz42 Jul 3, 2024
12d98b6
add handle for custom device (#61)
smazouz42 Jul 3, 2024
09c6b74
Merge branch 'devel' of https://github.com/pyccel/pyccel-cuda into is…
smazouz42 Jul 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add support for kernels (#42)
This pull request addresses issue #28 by implementing a new feature in
Pyccel that allows users to define custom GPU kernels. The syntax for
creating these kernels is inspired by Numba. and I also need to fix
issue #45 for testing purposes

**Commit Summary**

- Introduced KernelCall class
- Added cuda printer methods _print_KernelCall and _print_FunctionDef to
generate the corresponding CUDA representation for both kernel calls and
definitions
- Added IndexedFunctionCall  represents an indexed function call
- Added CUDA module and cuda.synchronize()
- Fixing a bug that I found in the header: it does not import the
necessary header for the used function

---------

Co-authored-by: EmilyBourne <[email protected]>
Co-authored-by: bauom <[email protected]>
Co-authored-by: Emily Bourne <[email protected]>
4 people committed Jul 11, 2024
commit 7da772a5096082d6268c6baf50cf2fc56c5d6152
1 change: 1 addition & 0 deletions .dict_custom.txt
Original file line number Diff line number Diff line change
@@ -118,3 +118,4 @@ datatyping
datatypes
indexable
traceback
GPUs
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ All notable changes to this project will be documented in this file.

- #32 : Add support for `nvcc` Compiler and `cuda` language as a possible option.
- #48 : Fix incorrect handling of imports in `cuda`.
- #42 : Add support for custom kernel in`cuda`.
- #42 : Add Cuda module to Pyccel. Add support for `cuda.synchronize` function.

## \[UNRELEASED\]

23 changes: 23 additions & 0 deletions docs/cuda.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Getting started GPU

Pyccel now supports NVIDIA CUDA, empowering users to accelerate numerical computations on GPUs seamlessly. With Pyccel's high-level syntax and automatic code generation, harnessing the power of CUDA becomes effortless. This documentation provides a quick guide to enabling CUDA in Pyccel

## Cuda Decorator

### kernel

The kernel decorator allows the user to declare a CUDA kernel. The kernel can be defined in Python, and the syntax is similar to that of Numba.

```python
from pyccel.decorators import kernel

@kernel
def my_kernel():
pass

blockspergrid = 1
threadsperblock = 1
# Call your kernel function
my_kernel[blockspergrid, threadsperblock]()

```
37 changes: 37 additions & 0 deletions pyccel/ast/core.py
Original file line number Diff line number Diff line change
@@ -73,6 +73,7 @@
'If',
'IfSection',
'Import',
'IndexedFunctionCall',
'InProgram',
'InlineFunctionDef',
'Interface',
@@ -2065,6 +2066,42 @@ def _ignore(cls, c):
"""
return c is None or isinstance(c, (FunctionDef, *cls._ignored_types))

class IndexedFunctionCall(FunctionCall):
"""
Represents an indexed function call in the code.

Class representing indexed function calls, encapsulating all
relevant information for such calls within the code base.

Parameters
----------
func : FunctionDef
The function being called.

args : iterable of FunctionCallArgument
The arguments passed to the function.

indexes : iterable of TypedAstNode
The indexes of the function call.

current_function : FunctionDef, optional
The function where the call takes place.
"""
__slots__ = ('_indexes',)
_attribute_nodes = FunctionCall._attribute_nodes + ('_indexes',)
def __init__(self, func, args, indexes, current_function = None):
self._indexes = indexes
super().__init__(func, args, current_function)

@property
def indexes(self):
"""
Indexes of function call.

Represents the indexes of the function call
"""
return self._indexes

class ConstructorCall(FunctionCall):

"""
65 changes: 65 additions & 0 deletions pyccel/ast/cuda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-
#------------------------------------------------------------------------------------------#
# This file is part of Pyccel which is released under MIT License. See the LICENSE file or #
# go to https://github.com/pyccel/pyccel/blob/master/LICENSE for full license details. #
#------------------------------------------------------------------------------------------#
"""
CUDA Module
This module provides a collection of classes and utilities for CUDA programming.
"""
from pyccel.ast.core import FunctionCall

__all__ = (
'KernelCall',
)

class KernelCall(FunctionCall):
"""
Represents a kernel function call in the code.

The class serves as a representation of a kernel
function call within the codebase.

Parameters
----------
func : FunctionDef
The definition of the function being called.

args : iterable of FunctionCallArgument
The arguments passed to the function.

num_blocks : TypedAstNode
The number of blocks. These objects must have a primitive type of `PrimitiveIntegerType`.

tp_block : TypedAstNode
The number of threads per block. These objects must have a primitive type of `PrimitiveIntegerType`.

current_function : FunctionDef, optional
The function where the call takes place.
"""
__slots__ = ('_num_blocks','_tp_block')
_attribute_nodes = (*FunctionCall._attribute_nodes, '_num_blocks', '_tp_block')

def __init__(self, func, args, num_blocks, tp_block, current_function = None):
self._num_blocks = num_blocks
self._tp_block = tp_block
super().__init__(func, args, current_function)

@property
def num_blocks(self):
"""
The number of blocks in the kernel being called.

The number of blocks in the kernel being called.
"""
return self._num_blocks

@property
def tp_block(self):
"""
The number of threads per block.

The number of threads per block.
"""
return self._tp_block

42 changes: 42 additions & 0 deletions pyccel/ast/cudaext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#------------------------------------------------------------------------------------------#
# This file is part of Pyccel which is released under MIT License. See the LICENSE file or #
# go to https://github.com/pyccel/pyccel/blob/master/LICENSE for full license details. #
#------------------------------------------------------------------------------------------#
"""
CUDA Extension Module
Provides CUDA functionality for code generation.
"""
from .internals import PyccelFunction

from .datatypes import VoidType
from .core import Module, PyccelFunctionDef

__all__ = (
'CudaSynchronize',
)

class CudaSynchronize(PyccelFunction):
"""
Represents a call to Cuda.synchronize for code generation.

This class serves as a representation of the Cuda.synchronize method.
"""
__slots__ = ()
_attribute_nodes = ()
_shape = None
_class_type = VoidType()
def __init__(self):
super().__init__()

cuda_funcs = {
'synchronize' : PyccelFunctionDef('synchronize' , CudaSynchronize),
}

cuda_mod = Module('cuda',
variables=[],
funcs=cuda_funcs.values(),
imports=[]
)

4 changes: 3 additions & 1 deletion pyccel/ast/utilities.py
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
from .literals import LiteralInteger, LiteralEllipsis, Nil
from .mathext import math_mod
from .sysext import sys_mod
from .cudaext import cuda_mod

from .numpyext import (NumpyEmpty, NumpyArray, numpy_mod,
NumpyTranspose, NumpyLinspace)
@@ -49,7 +50,8 @@
decorators_mod = Module('decorators',(),
funcs = [PyccelFunctionDef(d, PyccelFunction) for d in pyccel_decorators.__all__])
pyccel_mod = Module('pyccel',(),(),
imports = [Import('decorators', decorators_mod)])
imports = [Import('decorators', decorators_mod),
Import('cuda', cuda_mod)])

# TODO add documentation
builtin_import_registry = Module('__main__',
46 changes: 43 additions & 3 deletions pyccel/codegen/printing/cucode.py
Original file line number Diff line number Diff line change
@@ -9,11 +9,12 @@
enabling the direct translation of high-level Pyccel expressions into CUDA code.
"""

from pyccel.codegen.printing.ccode import CCodePrinter, c_library_headers
from pyccel.codegen.printing.ccode import CCodePrinter

from pyccel.ast.core import Import, Module
from pyccel.ast.core import Import, Module
from pyccel.ast.literals import Nil

from pyccel.errors.errors import Errors
from pyccel.errors.errors import Errors


errors = Errors()
@@ -61,6 +62,44 @@ def _print_Module(self, expr):
self.exit_scope()
return code

def function_signature(self, expr, print_arg_names = True):
"""
Get the Cuda representation of the function signature.

Extract from the function definition `expr` all the
information (name, input, output) needed to create the
function signature and return a string describing the
function.
This is not a declaration as the signature does not end
with a semi-colon.

Parameters
----------
expr : FunctionDef
The function definition for which a signature is needed.

print_arg_names : bool, default : True
Indicates whether argument names should be printed.

Returns
-------
str
Signature of the function.
"""
cuda_decorater = '__global__' if 'kernel' in expr.decorators else ''
c_function_signature = super().function_signature(expr, print_arg_names)
return f'{cuda_decorater} {c_function_signature}'

def _print_KernelCall(self, expr):
func = expr.funcdef
args = [a.value or Nil() for a in expr.args]

args = ', '.join(self._print(a) for a in args)
return f"{func.name}<<<{expr.num_blocks}, {expr.tp_block}>>>({args});\n"

def _print_CudaSynchronize(self, expr):
return 'cudaDeviceSynchronize();\n'

def _print_ModuleHeader(self, expr):
self.set_scope(expr.module.scope)
self._in_header = True
@@ -87,6 +126,7 @@ def _print_ModuleHeader(self, expr):
}}\n'
return '\n'.join((f"#ifndef {name.upper()}_H",
f"#define {name.upper()}_H",
imports,
global_variables,
function_declaration,
"#endif // {name.upper()}_H\n"))
10 changes: 10 additions & 0 deletions pyccel/cuda/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#------------------------------------------------------------------------------------------#
# This file is part of Pyccel which is released under MIT License. See the LICENSE file or #
# go to https://github.com/pyccel/pyccel/blob/master/LICENSE for full license details. #
#------------------------------------------------------------------------------------------#
"""
This module is for exposing the CudaSubmodule functions.
"""
from .cuda_sync_primitives import synchronize

__all__ = ['synchronize']
16 changes: 16 additions & 0 deletions pyccel/cuda/cuda_sync_primitives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#------------------------------------------------------------------------------------------#
# This file is part of Pyccel which is released under MIT License. See the LICENSE file or #
# go to https://github.com/pyccel/pyccel/blob/master/LICENSE for full license details. #
#------------------------------------------------------------------------------------------#
"""
This submodule contains CUDA methods for Pyccel.
"""


def synchronize():
"""
Synchronize CUDA device execution.

Synchronize CUDA device execution.
"""

32 changes: 32 additions & 0 deletions pyccel/decorators.py
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
'sympy',
'template',
'types',
'kernel'
)


@@ -109,3 +110,34 @@ def allow_negative_index(f,*args):
def identity(f):
return f
return identity

def kernel(f):
"""
Decorator for marking a Python function as a kernel.

This class serves as a decorator to mark a Python function
as a kernel function, typically used for GPU computations.
This allows the function to be indexed with the number of blocks and threads.

Parameters
----------
f : function
The function to which the decorator is applied.

Returns
-------
KernelAccessor
A class representing the kernel function.
"""
class KernelAccessor:
"""
Class representing the kernel function.

Class representing the kernel function.
"""
def __init__(self, f):
self._f = f
def __getitem__(self, args):
return self._f

return KernelAccessor(f)
8 changes: 8 additions & 0 deletions pyccel/errors/messages.py
Original file line number Diff line number Diff line change
@@ -162,3 +162,11 @@
WRONG_LINSPACE_ENDPOINT = 'endpoint argument must be boolean'
NON_LITERAL_KEEP_DIMS = 'keep_dims argument must be a literal, otherwise rank is unknown'
NON_LITERAL_AXIS = 'axis argument must be a literal, otherwise pyccel cannot determine which dimension to operate on'
MISSING_KERNEL_CONFIGURATION = 'Kernel launch configuration not specified'
INVALID_KERNEL_LAUNCH_CONFIG = 'Expected exactly 2 parameters for kernel launch'
INVALID_KERNEL_CALL_BP_GRID = 'Invalid Block per grid parameter for Kernel call'
INVALID_KERNEL_CALL_TP_BLOCK = 'Invalid Thread per Block parameter for Kernel call'




Loading