Skip to content

Commit

Permalink
Merge pull request #72 from pypr/fix-32bit
Browse files Browse the repository at this point in the history
Fix tests on 32bit architectures
  • Loading branch information
prabhuramachandran committed Dec 25, 2020
2 parents 455188d + 11abcae commit 2b8cd45
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 30 deletions.
5 changes: 3 additions & 2 deletions compyle/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ def update_minmax_gpu(ary_list, only_min=False, only_max=False,
props = ['ary_%s' % i for i in range(len(ary_list))]

dtype = ary_list[0].dtype
ctype = dtype_to_ctype(dtype)
ctype = dtype_to_ctype(dtype, backend=backend)

op = 'min' if not only_max else ''
op += 'max' if not only_min else ''
Expand Down Expand Up @@ -687,7 +687,8 @@ def __init__(self, dtype, n=0, allocate=True, backend=None):
from .cuda import set_context
set_context()
self.dtype = dtype
self.gptr_type = dtype_to_knowntype(dtype, address='global')
self.gptr_type = dtype_to_knowntype(dtype, address='global',
backend=backend)
self.minimum = 0
self.maximum = 0
self.data = None
Expand Down
9 changes: 5 additions & 4 deletions compyle/cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"""
from __future__ import print_function

from pytools import Record, RecordWithoutPickling
import logging
from pytools import Record, RecordWithoutPickling
from pytools.persistent_dict import KeyBuilder as KeyBuilderBase
from pytools.persistent_dict import WriteOncePersistentDict
from pycuda._cluda import CLUDA_PREAMBLE
Expand All @@ -14,7 +14,6 @@
from compyle.thrust.sort import argsort
import pycuda.driver as drv
from pycuda.compiler import SourceModule as _SourceModule
from pycuda.tools import dtype_to_ctype
from pytools import memoize
import numpy as np
import six
Expand Down Expand Up @@ -66,7 +65,8 @@ def add_dtype(self, dtype):
self.add_dtype(field_dtype)

_, cdecl = match_dtype_to_c_struct(
self.device, dtype_to_ctype(dtype), dtype)
self.device, dtype_to_ctype(dtype), dtype
)

self.declarations.append(cdecl)
self.declared_dtypes.add(dtype)
Expand Down Expand Up @@ -132,7 +132,8 @@ def match_dtype_to_c_struct(device, name, dtype, context=None, use_typedef=False
c_fields = []
for field_name, dtype_and_offset in fields:
field_dtype, offset = dtype_and_offset[:2]
c_fields.append(" %s %s;" % (dtype_to_ctype(field_dtype), field_name))
c_fields.append(" %s %s;" % (dtype_to_ctype(field_dtype),
field_name))

if use_typedef:
c_decl = "typedef struct {\n%s\n} %s;\n\n" % (
Expand Down
16 changes: 10 additions & 6 deletions compyle/jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .cython_generator import CythonGenerator
from .transpiler import Transpiler, BUILTINS
from .types import (dtype_to_ctype, get_declare_info,
dtype_to_knowntype, annotate)
dtype_to_knowntype, annotate, BITS)
from .extern import Extern
from .utils import getsourcelines
from .profile import profile
Expand Down Expand Up @@ -44,7 +44,7 @@ def get_ctype_from_arg(arg):
return 'double'
else:
if arg > 2147483648:
return 'long'
return 'long long' if BITS.startswith('32') else 'long'
else:
return 'int'

Expand Down Expand Up @@ -362,7 +362,7 @@ def __init__(self, reduce_expr, map_func=None, dtype_out=np.float64,
self.name = 'reduce'
self.reduce_expr = reduce_expr
self.dtype_out = dtype_out
self.type = dtype_to_ctype(dtype_out)
self.type = dtype_to_ctype(dtype_out, backend)
if backend == 'cython':
# On Windows, INFINITY is not defined so we use INFTY which we
# internally define.
Expand Down Expand Up @@ -446,7 +446,7 @@ def __init__(self, input=None, output=None, scan_expr="a+b",
self.name = 'scan'
self.scan_expr = scan_expr
self.dtype = dtype
self.type = dtype_to_ctype(dtype)
self.type = dtype_to_ctype(dtype, backend)
if backend == 'cython':
# On Windows, INFINITY is not defined so we use INFTY which we
# internally define.
Expand All @@ -463,7 +463,9 @@ def __init__(self, input=None, output=None, scan_expr="a+b",
builtin_symbols = ['item', 'prev_item', 'last_item']
self.builtin_types = {'i': 'int', 'N': 'int'}
for sym in builtin_symbols:
self.builtin_types[sym] = dtype_to_knowntype(self.dtype)
self.builtin_types[sym] = dtype_to_knowntype(
self.dtype, backend=backend
)

def get_type_info_from_kwargs(self, func, **kwargs):
type_info = {}
Expand All @@ -487,7 +489,9 @@ def _generate_kernel(self, **kwargs):
if self.input_func is not None:
arg_types = self.get_type_info_from_kwargs(
self.input_func, **kwargs)
arg_types['return_'] = dtype_to_knowntype(self.dtype)
arg_types['return_'] = dtype_to_knowntype(
self.dtype, backend=self.backend
)
helper = AnnotationHelper(self.input_func, arg_types)
declarations.update(helper.annotate())
self.input_func = helper.func
Expand Down
4 changes: 2 additions & 2 deletions compyle/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ def __init__(self, reduce_expr, map_func=None, dtype_out=np.float64,
self.name = 'reduce'
self.reduce_expr = reduce_expr
self.dtype_out = dtype_out
self.type = dtype_to_ctype(dtype_out)
self.type = dtype_to_ctype(dtype_out, backend=backend)
if backend == 'cython':
# On Windows, INFINITY is not defined so we use INFTY which we
# internally define.
Expand Down Expand Up @@ -847,7 +847,7 @@ def __init__(self, input=None, output=None, scan_expr="a+b",
self.name = 'scan'
self.scan_expr = scan_expr
self.dtype = dtype
self.type = dtype_to_ctype(dtype)
self.type = dtype_to_ctype(dtype, backend=backend)
if backend == 'cython':
# On Windows, INFINITY is not defined so we use INFTY which we
# internally define.
Expand Down
31 changes: 15 additions & 16 deletions compyle/types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import ast
import platform
import sys
import numpy as np


BITS = platform.architecture()[0]


def declare(type, num=1):
"""Declare the variable to be of the given type.
Expand Down Expand Up @@ -179,7 +183,7 @@ def _inject_types_in_module():
'unsigned short': np.uint16
}

if sys.platform.startswith('win'):
if sys.platform.startswith('win') or BITS.startswith('32bit'):
NP_C_TYPE_MAP[np.dtype(np.int64)] = 'long long'
NP_C_TYPE_MAP[np.dtype(np.uint64)] = 'unsigned long long'
C_NP_TYPE_MAP['long long'] = np.int64
Expand All @@ -195,19 +199,14 @@ def _inject_types_in_module():
NP_TYPE_LIST = list(C_NP_TYPE_MAP.values())


def dtype_to_ctype(dtype):
try:
# FIXME: pyopencl depency

from pyopencl.compyte.dtypes import \
dtype_to_ctype as dtype_to_ctype_pyopencl
ctype = dtype_to_ctype_pyopencl(dtype)
except ValueError:
pass
except ImportError:
pass
else:
return ctype
def dtype_to_ctype(dtype, backend=None):
if backend in ('opencl', 'cuda'):
try:
from pyopencl.compyte.dtypes import \
dtype_to_ctype as d2c_opencl
return d2c_opencl(dtype)
except (ValueError, ImportError):
pass
dtype = np.dtype(dtype)
return NP_C_TYPE_MAP[dtype]

Expand All @@ -224,8 +223,8 @@ def knowntype_to_ctype(knowntype):
raise ValueError("Not a vaild known type")


def dtype_to_knowntype(dtype, address='scalar'):
ctype = dtype_to_ctype(dtype)
def dtype_to_knowntype(dtype, address='scalar', backend=None):
ctype = dtype_to_ctype(dtype, backend=backend)
if 'unsigned' in ctype:
ctype = 'u%s' % ctype.replace('unsigned ', '')
knowntype = ctype.replace(' ', '')
Expand Down

0 comments on commit 2b8cd45

Please sign in to comment.