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

Shared mem #18

Draft
wants to merge 6 commits into
base: cuda_main_temp
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
111 changes: 103 additions & 8 deletions pyccel/ast/cudaext.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from .basic import PyccelAstNode
from .builtins import (PythonTuple,PythonList)

from .core import Module, PyccelFunctionDef
from .core import Module, PyccelFunctionDef, Import

from .datatypes import NativeInteger
from .datatypes import NativeInteger, NativeVoid, NativeFloat, TimeVal

Check warning on line 6 in pyccel/ast/cudaext.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pyccel/ast/cudaext.py#L6

Unused NativeInteger imported from datatypes

from .internals import PyccelInternalFunction, get_final_precision

Expand All @@ -26,7 +26,8 @@
'CudaMemCopy',
'CudaNewArray',
'CudaSynchronize',
'CudaThreadIdx'
'CudaThreadIdx',
'CudaTime'
)

#==============================================================================
Expand Down Expand Up @@ -109,14 +110,55 @@
def memory_location(self):
return self._memory_location

class CudaSharedArray(CudaNewArray):
"""
Represents a call to cuda.shared.array for code generation.

arg : list, tuple, PythonList

"""

__slots__ = ('_dtype','_precision','_shape','_rank','_order', '_memory_location')
name = 'array'

def __init__(self, shape, dtype, order='C'):

# Convert shape to PythonTuple
self._shape = process_shape(False, shape)

# Verify dtype and get precision
self._dtype, self._precision = process_dtype(dtype)

self._rank = len(self._shape)
self._order = self._order = NumpyNewArray._process_order(self._rank, order)
self._memory_location = 'shared'
super().__init__()

@property
def memory_location(self):
return self._memory_location

class CudaSynchronize(PyccelInternalFunction):
"Represents a call to Cuda.deviceSynchronize for code generation."

__slots__ = ()
_attribute_nodes = ()
_shape = None
_rank = 0
_dtype = NativeInteger()
_dtype = NativeVoid()
_precision = None
_order = None
def __init__(self):
super().__init__()

class CudaSyncthreads(PyccelInternalFunction):
"Represents a call to __syncthreads for code generation."

__slots__ = ()
_attribute_nodes = ()
_shape = None
_rank = 0
_dtype = NativeVoid()
_precision = None
_order = None
def __init__(self):
Expand Down Expand Up @@ -156,6 +198,47 @@
def dim(self):
return self._dim

class CudaTime(PyccelInternalFunction):

Check notice on line 201 in pyccel/ast/cudaext.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pyccel/ast/cudaext.py#L201

Missing class docstring
__slots__ = ()
_attribute_nodes = ()
_shape = None
_rank = 0
_dtype = TimeVal()
_precision = 0
_order = None
def __init__(self):
super().__init__()

class CudaTimeDiff(PyccelAstNode):
"""
Represents a General Class For Cuda internal Variables Used To locate Thread In the GPU architecture"

Parameters
----------
dim : NativeInteger
Represent the dimension where we want to locate our thread.

"""
__slots__ = ('_start','_end', '_dtype', '_precision')
_attribute_nodes = ('_start','_end',)
_shape = None
_rank = 0
_order = None

def __init__(self, start=None, end=None):
#...
self._start = start
self._end = end
self._dtype = NativeFloat()
self._precision = 8
super().__init__()

@property
def start(self):

Check notice on line 237 in pyccel/ast/cudaext.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pyccel/ast/cudaext.py#L237

Missing function or method docstring
return self._start
@property
def end(self):

Check notice on line 240 in pyccel/ast/cudaext.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pyccel/ast/cudaext.py#L240

Missing function or method docstring
return self._end

class CudaCopy(CudaNewArray):
"""
Expand Down Expand Up @@ -251,17 +334,19 @@
return expr[0]
return PythonTuple(*expr)



cuda_funcs = {
'array' : PyccelFunctionDef('array' , CudaArray),
'copy' : PyccelFunctionDef('copy' , CudaCopy),
'synchronize' : PyccelFunctionDef('synchronize' , CudaSynchronize),
'syncthreads' : PyccelFunctionDef('syncthreads' , CudaSyncthreads),
'threadIdx' : PyccelFunctionDef('threadIdx' , CudaThreadIdx),
'blockDim' : PyccelFunctionDef('blockDim' , CudaBlockDim),
'blockIdx' : PyccelFunctionDef('blockIdx' , CudaBlockIdx),
'gridDim' : PyccelFunctionDef('gridDim' , CudaGridDim),
'grid' : PyccelFunctionDef('grid' , CudaGrid)
'grid' : PyccelFunctionDef('grid' , CudaGrid),
'time' : PyccelFunctionDef('time' , CudaTime),
'timediff' : PyccelFunctionDef('timediff' , CudaTimeDiff),

}

cuda_Internal_Var = {
Expand All @@ -271,6 +356,16 @@
'CudaGridDim' : 'gridDim'
}

# cuda_sharedmemory = {
# 'array' : PyccelFunctionDef('array' , CudaSharedArray),
# }

cuda_sharedmemory = Module('shared', (),
[ PyccelFunctionDef('array' , CudaSharedArray)])

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

Check notice on line 371 in pyccel/ast/cudaext.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pyccel/ast/cudaext.py#L371

Final newline missing
6 changes: 6 additions & 0 deletions pyccel/ast/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,9 @@
return 'bool'
else:
raise TypeError('Unknown datatype {0}'.format(str(dtype)))


class TimeVal(DataType):
"""Class representing timeval datatype"""
__slots__ = ()
_name = 'timeval'

Check notice on line 385 in pyccel/ast/datatypes.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pyccel/ast/datatypes.py#L385

Final newline missing
4 changes: 4 additions & 0 deletions pyccel/ast/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .datatypes import (NativeBool, NativeInteger, NativeFloat,
NativeComplex, NativeString,
NativeNumeric)
from .datatypes import TimeVal

from .internals import max_precision

Expand Down Expand Up @@ -393,6 +394,7 @@ def _calculate_dtype(cls, *args):
floats = [a for a in args if a.dtype is NativeFloat()]
complexes = [a for a in args if a.dtype is NativeComplex()]
strs = [a for a in args if a.dtype is NativeString()]
time = [a for a in args if a.dtype is TimeVal()]

if strs:
assert len(integers + floats + complexes) == 0
Expand All @@ -403,6 +405,8 @@ def _calculate_dtype(cls, *args):
return cls._handle_float_type(args)
elif integers:
return cls._handle_integer_type(args)
elif time:
return time
else:
raise TypeError('cannot determine the type of {}'.format(args))

Expand Down
4 changes: 2 additions & 2 deletions pyccel/ast/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ def __init__(
raise ValueError("memory_handling must be 'heap', 'stack' or 'alias'")
self._memory_handling = memory_handling

if memory_location not in ('host', 'device', 'managed'):
raise ValueError("memory_location must be 'host', 'device' or 'managed'")
if memory_location not in ('host', 'device', 'managed', 'shared'):
raise ValueError("memory_location must be 'host', 'device' , 'shared' or 'managed'")
self._memory_location = memory_location

if not isinstance(is_const, bool):
Expand Down
Loading