Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(invalid -- do not use) c++ and %cython in the notebook #10249

Closed
williamstein opened this issue Nov 11, 2010 · 4 comments
Closed

(invalid -- do not use) c++ and %cython in the notebook #10249

williamstein opened this issue Nov 11, 2010 · 4 comments

Comments

@williamstein
Copy link
Contributor

Make it so that the "#clang c++" pragma in %cython blocks in the notebook correctly invokes the --cplus option to the Cython compiler.

This is related to trac #10094.

Component: misc

Issue created by migration from https://trac.sagemath.org/ticket/10249

@williamstein
Copy link
Contributor Author

Attachment: trac_10249.patch.gz

@williamstein
Copy link
Contributor Author

comment:1

You could test this by pasting the following into a notebook cell:

open("/tmp/a.h",'w').write("""
struct Elt10 {
    long a, b;
};

Elt10 operator+(Elt10 x, Elt10 y) {
    Elt10 z;
    z.a = x.a + y.a;
    z.b = x.a + y.a;
    return z;
}

void iadd(Elt10* x, Elt10 y) {
    x->a = x->a + y.a;
    x->b = x->b + y.b;
}

Elt10 operator*(Elt10 x, Elt10 y) {
    Elt10 z;
    z.a = x.a*y.a + x.b*y.b;
    z.b = x.b*y.a + x.a*y.b + x.b*y.b;
    return z;
}
""")

Then making another notebook cell and pasting the following in:

%cython
#clang c++

cdef extern from "stdlib.h":
    long random()

cdef extern from "/tmp/a.h":
    cdef cppclass Elt10:
        long a, b
    Elt10 operator+(Elt10 x, Elt10 y)
    Elt10 operator*(Elt10 x, Elt10 y)
    void iadd(Elt10* x, Elt10 y)

cdef class Vec4:
    cdef Elt10* elements
    cdef long n
    def __cinit__(self, Py_ssize_t n):
        # dynamic memory allocation on the heap
        self.elements = <Elt10*>malloc(n*sizeof(Elt10))
        self.n = n
    def __dealloc__(self):
        free(self.elements)
    def randomize(self):
        cdef Py_ssize_t m
        for m in range(self.n):
            self.elements[m].a = random(); self.elements[m].b = random()
    def dot(self, Vec4 other not None):
        cdef Elt10 ans, tmp
        ans.a = 0; ans.b = 0
        cdef Py_ssize_t m
        # This is the part that gets very worrisome, since the code does
        # not look easy to read.  And it can easily get massively more complicated
        # when the arithmetic isn't so simple!
        for m in range(self.n): 
            ans = ans + self.elements[m] * other.elements[m]
        return ans.a, ans.b

Then in a third cell do:

v = Vec4(10^6); w = Vec4(10^6); v.randomize(); w.randomize()
v.dot(w)

OK, not exactly a minimal example :-)

@vbraun
Copy link
Member

vbraun commented Nov 11, 2010

comment:2

Are there any prerequisite patches? On sage-4.6 with no other patches to the notebook, I tried

%cython
#clang c++
cdef extern from "math.h":
    double sin(double)
cpdef t():
    return sin(1)

This works without the #clang, but in c++ mode the notebook gives me an error

cp: cannot stat `_home_vbraun__sage_sage_notebook_sagenb_home_admin_2_code_sage29_spyx_0.c': No such file or directory
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_18.py", line 10, in <module>
    exec compile(u'_support_.cython_import_all("/home/vbraun/.sage/sage_notebook.sagenb/home/admin/2/code/sage29.spyx", globals())
  File "", line 1, in <module>
    
  File "/home/vbraun/Sage/sage/devel/sagenb/sagenb/misc/support.py", line 519, in cython_import_all
    create_local_c_file=create_local_c_file)
  File "/home/vbraun/Sage/sage/devel/sagenb/sagenb/misc/support.py", line 496, in cython_import
    create_local_c_file=create_local_c_file)
  File "/home/vbraun/Sage/sage/local/lib/python2.6/site-packages/sage/misc/cython.py", line 372, in cython
    raise RuntimeError, "Error converting %s to C:\n%s\n%s"%(filename, log, err)
RuntimeError: Error converting /home/vbraun/.sage/sage_notebook.sagenb/home/admin/2/code/sage29.spyx to C:

Cython created the source file _home_vbraun__sage_sage_notebook_sagenb_home_admin_2_code_sage29_spyx_0.cpp, so cp shouldn't try to copy the .c file.

@williamstein
Copy link
Contributor Author

comment:3

This is no longer needed in sage-4.6, and in fact breaks things! So marked as in valid -- do not apply.

@williamstein williamstein changed the title c++ and %cython in the notebook (invalid -- do not use) c++ and %cython in the notebook Nov 16, 2010
@sagetrac-mvngu sagetrac-mvngu mannequin removed this from the sage-4.6.1 milestone Nov 21, 2010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants