Skip to content

Commit

Permalink
Make pyweld Python 3 friendly (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
radujica authored and sppalkia committed Sep 5, 2018
1 parent 0a3b580 commit 912ee3b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 36 deletions.
3 changes: 1 addition & 2 deletions python/pyweld/setup.py
Expand Up @@ -2,10 +2,9 @@
import platform
import shutil
import subprocess
import sys

from setuptools import setup, Distribution
import setuptools.command.build_ext as _build_ext
from setuptools import setup, Distribution
from setuptools.command.develop import develop
from setuptools.extension import Extension

Expand Down
14 changes: 6 additions & 8 deletions python/pyweld/weld/bindings.py
Expand Up @@ -2,11 +2,9 @@
# Implements a wrapper around the Weld API.
#

from ctypes import *

import os
import platform
import copy
import platform
from ctypes import *

import pkg_resources

Expand Down Expand Up @@ -46,7 +44,7 @@ def __init__(self, code, conf, err):
c_char_p, c_weld_conf, c_weld_err]
weld_module_compile.restype = c_weld_module

code = c_char_p(code)
code = c_char_p(code.encode('utf-8'))
self.module = weld_module_compile(code, conf.conf, err.error)

def run(self, conf, arg, err):
Expand Down Expand Up @@ -113,16 +111,16 @@ def __init__(self):
self.conf = weld_conf_new()

def get(self, key):
key = c_char_p(key)
key = c_char_p(key.encode('utf-8'))
weld_conf_get = weld.weld_conf_get
weld_conf_get.argtypes = [c_weld_conf, c_char_p]
weld_conf_get.restype = c_char_p
val = weld_conf_get(self.conf, key)
return copy.copy(val)

def set(self, key, value):
key = c_char_p(key)
value = c_char_p(value)
key = c_char_p(key.encode('utf-8'))
value = c_char_p(value.encode('utf-8'))
weld_conf_set = weld.weld_conf_set
weld_conf_set.argtypes = [c_weld_conf, c_char_p, c_char_p]
weld_conf_set.restype = None
Expand Down
10 changes: 5 additions & 5 deletions python/pyweld/weld/encoders.py
Expand Up @@ -3,13 +3,13 @@
Python types to Weld types.
"""

from types import *

from weldobject import WeldObjectEncoder, WeldObjectDecoder
import bindings as cweld
import ctypes

import numpy as np
import ctypes

from . import bindings as cweld
from .types import *
from .weldobject import WeldObjectEncoder, WeldObjectDecoder


def dtype_to_weld_type(dtype):
Expand Down
42 changes: 21 additions & 21 deletions python/pyweld/weld/weldobject.py
Expand Up @@ -3,29 +3,28 @@
#
# Holds an object that can be evaluated.
#
from __future__ import print_function

import ctypes

import os
import time

import bindings as cweld
from types import *
from . import bindings as cweld
from .types import *


class WeldObjectEncoder(object):
"""
An abstract class that must be overwridden by libraries. This class
is used to marshall objects from Python types to Weld types.
"""
def encode(obj):
def encode(self, obj):
"""
Encodes an object. All objects encodable by this encoder should return
a valid Weld type using py_to_weld_type.
"""
raise NotImplementedError

def py_to_weld_type(obj):
def py_to_weld_type(self, obj):
"""
Returns a WeldType corresponding to a Python object
"""
Expand All @@ -37,7 +36,7 @@ class WeldObjectDecoder(object):
An abstract class that must be overwridden by libraries. This class
is used to marshall objects from Weld types to Python types.
"""
def decode(obj, restype):
def decode(self, obj, restype):
"""
Decodes obj, assuming object is of type `restype`. obj's Python
type is ctypes.POINTER(restype.ctype_class).
Expand Down Expand Up @@ -97,6 +96,14 @@ def __init__(self, encoder, decoder):
def __repr__(self):
return self.weld_code + " " + str(self.context) + " " + str([obj_id for obj_id in self.dependencies])

@staticmethod
def generate_input_name(value_str):
name = "_inp%d" % WeldObject._var_num
WeldObject._var_num += 1
WeldObject._registry[value_str] = name

return name

def update(self, value, tys=None, override=True):
"""
Update this context. if value is another context,
Expand All @@ -113,9 +120,7 @@ def update(self, value, tys=None, override=True):
if value_str in WeldObject._registry:
name = WeldObject._registry[value_str]
else:
name = "_inp%d" % WeldObject._var_num
WeldObject._var_num += 1
WeldObject._registry[value_str] = name
name = WeldObject.generate_input_name(value_str)
self.context[name] = value
if tys is not None and not override:
self.argtypes[name] = tys
Expand All @@ -142,14 +147,11 @@ def get_let_statements(self):
return "\n".join(let_statements)

def to_weld_func(self):
names = self.context.keys()
names.sort()
names = sorted(self.context.keys())
arg_strs = ["{0}: {1}".format(str(name),
str(self.encoder.py_to_weld_type(self.context[name])))
for name in names]
header = "|" + ", ".join(arg_strs) + "|"
keys = self.dependencies.keys()
keys.sort()
text = header + " " + self.get_let_statements() + "\n" + self.weld_code
return text

Expand All @@ -165,8 +167,7 @@ class Args(ctypes.Structure):

# Encode each input argument. This is the positional argument list
# which will be wrapped into a Weld struct and passed to the Weld API.
names = self.context.keys()
names.sort()
names = sorted(self.context.keys())

start = time.time()
encoded = []
Expand All @@ -181,9 +182,8 @@ class Args(ctypes.Structure):
encoded.append(self.encoder.encode(self.context[name]))
end = time.time()
if verbose:
print "Python->Weld:", end - start
print("Python->Weld:", end - start)

start = time.time()
Args = args_factory(zip(names, argtypes))
weld_args = Args()
for name, value in zip(names, encoded):
Expand All @@ -207,7 +207,7 @@ class Args(ctypes.Structure):
function, err.message()))
end = time.time()
if verbose:
print "Weld compile time:", end - start
print("Weld compile time:", end - start)

start = time.time()
conf = cweld.WeldConf()
Expand All @@ -225,7 +225,7 @@ class Args(ctypes.Structure):
data = ctypes.cast(weld_ret.data(), ptrtype)
end = time.time()
if verbose:
print "Weld:", end - start
print("Weld:", end - start)

start = time.time()
if decode:
Expand All @@ -236,6 +236,6 @@ class Args(ctypes.Structure):
ctypes.c_int64)).contents.value
end = time.time()
if verbose:
print "Weld->Python:", end - start
print("Weld->Python:", end - start)

return result

0 comments on commit 912ee3b

Please sign in to comment.