Skip to content
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

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 arg(self):
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 __init__(self, dim=None):
def dim(self):
return self._dim

class CudaTime(PyccelInternalFunction):
__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):
return self._start
@property
def end(self):
return self._end

class CudaCopy(CudaNewArray):
"""
Expand Down Expand Up @@ -251,17 +334,19 @@ def __new__(cls, dim=0):
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 @@ def __new__(cls, dim=0):
'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),
])
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 @@ def str_dtype(dtype):
return 'bool'
else:
raise TypeError('Unknown datatype {0}'.format(str(dtype)))


class TimeVal(DataType):
"""Class representing timeval datatype"""
__slots__ = ()
_name = 'timeval'
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