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
4f08263
commit d9a1fb5
Showing
19 changed files
with
324 additions
and
94 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 |
---|---|---|
|
@@ -106,5 +106,6 @@ Valgrind | |
variadic | ||
subclasses | ||
oneAPI | ||
Cuda | ||
getter | ||
setter |
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
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, 'file') | ||
|
||
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
Oops, something went wrong.