Code Monkey home page Code Monkey logo

lltypes's Introduction

lltypes

A type system for Python backed by llvm and ctypes

This project is a wrapping for spelling and translating between ctypes, LLVM types and Numpy dtype.

Example: Structs

In C99 we might define the following structure:

struct {
   bool a;
   int b;
   float c;
} mystruct;

We can map this structure in Python:

from lltypes import *
mystruct = Struct(
    'mystruct',
    Bool('a'),
    Int32('b'),
    Float32('c'),
)

Which can be converted to ctypes using to_ctypes:

mycstruct = mystruct.to_ctypes()

inst = mycstruct(
    True,
    3,
    3.14
)

>>> inst.a
True
>>> inst.b
3
>>> inst.c
3.140000104904175

And to LLVM using to_llvm:

llstruct = mystruct.to_llvm()

>>> print llstruct
%mystruct = type { i1, i32, float }

And to dtype using to_dtype:

dtstruct = mystruct.to_dtype()

>>> print dtstruct
dtype([('a', '?'), ('b', 'i32'), ('c', '<f4')])

Example: Arrays

Blaze defines a family of parameterized types for its array objects. These are first class polytypes in lltypes with the following schema:

nd := 1 | 2 | 3 | 4 | 5

mono := Byte | Int8 | Int32 | ...

poly := Array_C <mono> <nd>
      | Array_F <mono> <nd>
      | Array_S <mono> <nd>

In C these are structures of array kinds parameterized by eltype and nd.

// Contiguous or Fortran
struct {
   eltype *data;
   intp shape[nd];
} Array_C;

struct {
   eltype *data;
   diminfo shape[nd];
} Array_F;

struct {
   eltype *data;
   intp shape[nd];
   intp stride[nd];
} Array_S;

In lltypes these are expanded out into lower types by a simple function.

def Array_C(name, ty, nd):
    return Struct('Array_C',
        Pointer(ty('data')),
        Sequence(UNInt8('shape'), nd),
    )

def Array_F(name, ty, nd):
    return Struct('Array_F',
        Pointer(ty('data')),
        Sequence(UNInt8('shape'), nd),
    )

def Array_S(name, ty, nd):
    return Struct('Array_S',
        Pointer(ty('data')),
        Sequence(UNInt8('shape'), nd),
        Sequence(UNInt8('stride'), nd),
    )
>>> c = Array_C('foo', UNInt8, 3)
>>> f = Array_F('foo', UNInt8, 3)
>>> s = Array_S('foo', UNInt8, 3)

>>> print c.to_llvm()
%Array_C = type { i8*, [3 x i8] }
>>> print f.to_llvm()
%Array_F = type { i8*, [3 x i8] }
>>> print s.to_llvm()
%Array_S = type { i8*, [3 x i8], [3 x i8] }

Tests

Test suite can be run with either of the following:

python -m unittest discover

or:

from lltypes import test
test()

lltypes's People

Contributors

sdiehl avatar teoliphant avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.