Skip to content

Segfault when assigning to a complex Scalar #40

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

Closed
eriknw opened this issue Apr 13, 2022 · 5 comments · Fixed by #41
Closed

Segfault when assigning to a complex Scalar #40

eriknw opened this issue Apr 13, 2022 · 5 comments · Fixed by #41

Comments

@eriknw
Copy link
Member

eriknw commented Apr 13, 2022

I get a segfault when I run this code:

import suitesparse_graphblas as ssgb

ssgb.initialize()
s = ssgb.ffi.new("GrB_Scalar*")
assert ssgb.lib.GrB_Scalar_new(s, ssgb.lib.GxB_FC64) == 0
assert ssgb.lib.GxB_Scalar_setElement_FC64(s[0], 1j) == 0  # <-- segfault here

Any thoughts? I guess we should try running this in C.

@eriknw
Copy link
Member Author

eriknw commented Apr 13, 2022

Here's some spooky action: the following doesn't crash!

import suitesparse_graphblas as ssgb

ssgb.initialize()
for attr in dir(ssgb.lib):
    getattr(ssgb.lib, attr)
s = ssgb.ffi.new("GrB_Scalar*")
assert ssgb.lib.GrB_Scalar_new(s, ssgb.lib.GxB_FC64) == 0
assert ssgb.lib.GxB_Scalar_setElement_FC64(s[0], 1j) == 0

@DrTimothyAldenDavis
Copy link
Member

I just tried a simple stand-alone C test and it worked fine. Give this a try:

setEl.zip

@DrTimothyAldenDavis
Copy link
Member

What is the type of 1j? Is it complex, or a pure imaginary value (with no real part)? Does Python have such scalar types?

@DrTimothyAldenDavis
Copy link
Member

Here is the output I get from "make" and everything is fine:

$ make
gcc -g -fopenmp -I../GraphBLAS/Include setEl.c -c -o setEl.o
gcc setEl.o -L../GraphBLAS/build -Wl,-rpath,../GraphBLAS/build -lgraphblas -lgomp -lpthread -lm -ldl -o setEl
ldd setEl
	linux-vdso.so.1 (0x00007fff68ff6000)
	libgraphblas.so.7 => /home/faculty/d/davis/master/GraphBLAS/build/libgraphblas.so.7 (0x00007f40a86e3000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f40a84d3000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f40a8384000)
	libgomp.so.1 => /lib/x86_64-linux-gnu/libgomp.so.1 (0x00007f40a8342000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f40a831f000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f40b27c4000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f40a8319000)
./setEl

library: SuiteSparse:GraphBLAS 7.0.3 (Apr 8, 2022)
include: SuiteSparse:GraphBLAS 7.0.3 (Apr 8, 2022)

  1x1 GraphBLAS double complex scalar, sparse by col
  s, no entries, memory: 272 bytes


  1x1 GraphBLAS double complex scalar, full by col
  s, 1 entry, memory: 272 bytes
  iso value:    0 + 1i

    (0,0)    0 + 1i

@eriknw
Copy link
Member Author

eriknw commented Apr 13, 2022

Thanks for giving this a try @DrTimothyAldenDavis! Yeah, Python has builtin complex numbers, and x = 1j has Re(x) == 0 and Im(x) == 1.

Upon further stress testing, adding

for attr in dir(ssgb.lib):
    getattr(ssgb.lib, attr)

reliably fixes the segfault for me.

How does the joke go?

Patient: doctor, it hurts with I do this

Doctor: so don't do that!

I have no idea why the above trick works (is cffi doing something weird under the hood?), but it does (apparently), so I guess we should use it. PR forthcoming!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants