Skip to content

Commit

Permalink
Add tests for CUDAConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapb committed Oct 24, 2018
1 parent 599fe14 commit cefc028
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions pysph/cpy/tests/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ..types import annotate, declare
from ..translator import (
CConverter, CodeGenerationError, CStructHelper, KnownType,
OpenCLConverter, py2c
OpenCLConverter, CUDAConverter, py2c
)


Expand Down Expand Up @@ -1164,7 +1164,7 @@ def not_me(self):
assert result.strip() == expect.strip()


def test_opencl_conversion():
def check_opencl_cuda_conversion(converter_obj):
# Note that LID_0 etc. are predefined symbols when we include the CLUDA
# preamble, therefore should be known.
src = dedent('''
Expand All @@ -1174,7 +1174,7 @@ def f(s_idx, s_p, d_idx, d_p, J=0, t=0.0, l=[0,0], xx=(0, 0)):

# When
known_types = {'d_p': KnownType('GLOBAL_MEM int*')}
converter = OpenCLConverter(known_types=known_types)
converter = converter_obj(known_types=known_types)
code = converter.convert(src)

# Then
Expand All @@ -1188,6 +1188,14 @@ def f(s_idx, s_p, d_idx, d_p, J=0, t=0.0, l=[0,0], xx=(0, 0)):
assert code.strip() == expect.strip()


def test_cuda_conversion():
check_opencl_cuda_conversion(CUDAConverter)


def test_opencl_conversion():
check_opencl_cuda_conversion(OpenCLConverter)


def test_opencl_class():
src = dedent('''
class Foo(object):
Expand All @@ -1209,6 +1217,42 @@ def g(self, x=0.0):
assert code.strip() == expect.strip()


def test_cuda_local_conversion():
@annotate(xc='ldoublep', yc='lintp')
def knl(xc, yc):
xc[LID_0] = 1
yc[LID_0] = 1

# When
converter = CUDAConverter()
code = converter.parse(knl)

# Then
expect_1 = dedent('''
WITHIN_KERNEL void knl(int size_xc, int size_yc)
{
extern LOCAL_MEM float shared_buff[];
double* xc = (double*) shared_buff;
int* yc = (int*) &xc[size_xc];
xc[LID_0] = 1;
yc[LID_0] = 1;
}
''')

expect_2 = dedent('''
WITHIN_KERNEL void knl(int size_xc, int size_yc)
{
extern LOCAL_MEM float shared_buff[];
int* yc = (int*) shared_buff;
double* xc = (double*) &yc[size_yc];
xc[LID_0] = 1;
yc[LID_0] = 1;
}
''')

assert code.strip() == expect_1.strip() or code.strip() == expect_2.strip()


def test_handles_parsing_functions():
# Given
def f(x=1.0):
Expand Down

0 comments on commit cefc028

Please sign in to comment.