forked from pyccel/pyccel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[init] Adding CUDA language/compiler and CodePrinter (#32)
This PR aims to make the C code compilable using nvcc. The cuda language was added as well as a CudaCodePrinter. Changes to stdlib: Wrapped expressions using complex types in an `ifndef __NVCC__` to avoid processing them with the nvcc compiler --------- Co-authored-by: Mouad Elalj, EmilyBourne
- Loading branch information
1 parent
5bbe1fd
commit a66a068
Showing
18 changed files
with
298 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,6 +110,7 @@ Valgrind | |
variadic | ||
subclasses | ||
oneAPI | ||
Cuda | ||
getter | ||
setter | ||
bitwise | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# 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. # | ||
#------------------------------------------------------------------------------------------# | ||
""" | ||
Provide tools for generating and handling CUDA code. | ||
This module is designed to interface Pyccel's Abstract Syntax Tree (AST) with CUDA, | ||
enabling the direct translation of high-level Pyccel expressions into CUDA code. | ||
""" | ||
|
||
from pyccel.codegen.printing.ccode import CCodePrinter, c_library_headers | ||
|
||
from pyccel.ast.core import Import, Module | ||
|
||
from pyccel.errors.errors import Errors | ||
|
||
|
||
errors = Errors() | ||
|
||
__all__ = ["CudaCodePrinter"] | ||
|
||
class CudaCodePrinter(CCodePrinter): | ||
""" | ||
Print code in CUDA format. | ||
This printer converts Pyccel's Abstract Syntax Tree (AST) into strings of CUDA code. | ||
Navigation through this file utilizes _print_X functions, | ||
as is common with all printers. | ||
Parameters | ||
---------- | ||
filename : str | ||
The name of the file being pyccelised. | ||
prefix_module : str | ||
A prefix to be added to the name of the module. | ||
""" | ||
language = "cuda" | ||
|
||
def __init__(self, filename, prefix_module = None): | ||
|
||
errors.set_target(filename) | ||
|
||
super().__init__(filename) | ||
|
||
def _print_Module(self, expr): | ||
self.set_scope(expr.scope) | ||
self._current_module = expr.name | ||
body = ''.join(self._print(i) for i in expr.body) | ||
|
||
global_variables = ''.join(self._print(d) for d in expr.declarations) | ||
|
||
# Print imports last to be sure that all additional_imports have been collected | ||
imports = [Import(expr.name, Module(expr.name,(),())), *self._additional_imports.values()] | ||
c_headers_imports = '' | ||
local_imports = '' | ||
|
||
for imp in imports: | ||
if imp.source in c_library_headers: | ||
c_headers_imports += self._print(imp) | ||
else: | ||
local_imports += self._print(imp) | ||
|
||
imports = f'{c_headers_imports}\ | ||
extern "C"{{\n\ | ||
{local_imports}\ | ||
}}' | ||
|
||
code = f'{imports}\n\ | ||
{global_variables}\n\ | ||
{body}\n' | ||
|
||
self.exit_scope() | ||
return code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# 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. # | ||
#------------------------------------------------------------------------------------------# | ||
""" | ||
Handles name clash problems in Cuda | ||
""" | ||
from .languagenameclashchecker import LanguageNameClashChecker | ||
|
||
class CudaNameClashChecker(LanguageNameClashChecker): | ||
""" | ||
Class containing functions to help avoid problematic names in Cuda. | ||
A class which provides functionalities to check or propose variable names and | ||
verify that they do not cause name clashes. Name clashes may be due to | ||
new variables, or due to the use of reserved keywords. | ||
""" | ||
# Keywords as mentioned on https://en.cppreference.com/w/c/keyword | ||
keywords = set(['isign', 'fsign', 'csign', 'auto', 'break', 'case', 'char', 'const', | ||
'continue', 'default', 'do', 'double', 'else', 'enum', | ||
'extern', 'float', 'for', 'goto', 'if', 'inline', 'int', | ||
'long', 'register', 'restrict', 'return', 'short', 'signed', | ||
'sizeof', 'static', 'struct', 'switch', 'typedef', 'union', | ||
'unsigned', 'void', 'volatile', 'whie', '_Alignas', | ||
'_Alignof', '_Atomic', '_Bool', '_Complex', 'Decimal128', | ||
'_Decimal32', '_Decimal64', '_Generic', '_Imaginary', | ||
'_Noreturn', '_Static_assert', '_Thread_local', 't_ndarray', | ||
'array_create', 'new_slice', 'array_slicing', 'alias_assign', | ||
'transpose_alias_assign', 'array_fill', 't_slice', | ||
'GET_INDEX_EXP1', 'GET_INDEX_EXP2', 'GET_INDEX_EXP2', | ||
'GET_INDEX_EXP3', 'GET_INDEX_EXP4', 'GET_INDEX_EXP5', | ||
'GET_INDEX_EXP6', 'GET_INDEX_EXP7', 'GET_INDEX_EXP8', | ||
'GET_INDEX_EXP9', 'GET_INDEX_EXP10', 'GET_INDEX_EXP11', | ||
'GET_INDEX_EXP12', 'GET_INDEX_EXP13', 'GET_INDEX_EXP14', | ||
'GET_INDEX_EXP15', 'NUM_ARGS_H1', 'NUM_ARGS', | ||
'GET_INDEX_FUNC_H2', 'GET_INDEX_FUNC', 'GET_INDEX', | ||
'INDEX', 'GET_ELEMENT', 'free_array', 'free_pointer', | ||
'get_index', 'numpy_to_ndarray_strides', | ||
'numpy_to_ndarray_shape', 'get_size', 'order_f', 'order_c', 'array_copy_data']) | ||
|
||
def has_clash(self, name, symbols): | ||
""" | ||
Indicate whether the proposed name causes any clashes. | ||
Checks if a suggested name conflicts with predefined | ||
keywords or specified symbols,returning true for a clash. | ||
This method is crucial for maintaining namespace integrity and | ||
preventing naming conflicts in code generation processes. | ||
Parameters | ||
---------- | ||
name : str | ||
The suggested name. | ||
symbols : set | ||
Symbols which should be considered as collisions. | ||
Returns | ||
------- | ||
bool | ||
True if the name is a collision. | ||
False if the name is collision free. | ||
""" | ||
return any(name == k for k in self.keywords) or \ | ||
any(name == s for s in symbols) | ||
|
||
def get_collisionless_name(self, name, symbols): | ||
""" | ||
Get a valid name which doesn't collision with symbols or Cuda keywords. | ||
Find a new name based on the suggested name which will not cause | ||
conflicts with Cuda keywords, does not appear in the provided symbols, | ||
and is a valid name in Cuda code. | ||
Parameters | ||
---------- | ||
name : str | ||
The suggested name. | ||
symbols : set | ||
Symbols which should be considered as collisions. | ||
Returns | ||
------- | ||
str | ||
A new name which is collision free. | ||
""" | ||
if len(name)>4 and all(name[i] == '_' for i in (0,1,-1,-2)): | ||
# Ignore magic methods | ||
return name | ||
if name[0] == '_': | ||
name = 'private'+name | ||
return self._get_collisionless_name(name, symbols) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.