Commits

Takafumi Arakaki  committed 35e5638

added minimum script to reproduce the strange behavior of ctypes with icc-
compiled shared library

CC=gcc CFLAGS=-O3 make # no error
CC=icc CFLAGS=-O1 make # no error
CC=icc CFLAGS=-O3 make # Segmentation fault!

  • Participants

Comments (0)

Files changed (3)

+run: libifnet_a2a2.so
+	python run_ifnet_a2a2.py
+
+libifnet_a2a2.so: ifnet_a2a2.c
+	$(CC) $(CFLAGS) $(CPPFLAGS) -Wall -std=c99 -shared $< -o $@
+
+clean:
+	-rm *.so *.o
+
+.PHONY: clean run

File ifnet_a2a2.c

+typedef struct ifnetalltoall2_{
+  int num_n, num_s, num_k;
+  double dt, ie, gl, isyn, vrest, th, ssyn, tau1, tau2, lmd1, lmd2;
+  double k1_mean, k2_mean;
+  int *fired, *total_spikes, **rec_sspike, **rec_fired;
+  double *vi, *v0, *k1, *k2, *lspike, *rec_isyn;
+  double **rec_vi, **rec_lspike;
+} IFNetAllToAll2;
+
+
+void
+vec_v0_rk2(double * v0, double * vt, double dt,
+           double gl, double isyn, double ie, double isyn_estimate, int num_n)
+{
+  int i;
+  for (i = 0; i < num_n; ++i){
+    v0[i] = vt[i] + dt + gl + isyn + ie + isyn_estimate;
+  }
+}
+
+int
+IFNetAllToAll2_init(IFNetAllToAll2 *self)
+{
+  return 0;
+}
+
+int
+IFNetAllToAll2_vec_v0_rk2(IFNetAllToAll2 *self)
+{
+  double isyn_estimate;
+  isyn_estimate =  /* ~Isyn(t+dt) */
+    self->ssyn * (self->lmd1 * self->k1_mean - self->lmd2 * self->k2_mean) /
+    (self->tau1 - self->tau2);
+  vec_v0_rk2(  /* v0(t+dt) = vi(t) + dt * (dv/dt(t) + ~dv/dt(t+dt)) / 2 */
+      self->v0, self->vi, self->dt, self->gl, self->isyn,
+      self->ie, isyn_estimate, self->num_n);
+  return 0;
+}

File run_ifnet_a2a2.py

+import ctypes
+import numpy
+
+NUM_N = 100
+NUM_S = 2
+
+int1d = ctypes.POINTER(ctypes.c_int)
+double1d = ctypes.POINTER(ctypes.c_double)
+## c_double_p = ctypes.POINTER(ctypes.c_double)
+## c_int_p = ctypes.POINTER(ctypes.c_int)
+
+
+class IFNetAllToAll2Struct(ctypes.Structure):
+    _fields_ = [
+        ("num_n", ctypes.c_int),
+        ("num_s", ctypes.c_int),
+        ("num_k", ctypes.c_int),
+        ("dt", ctypes.c_double),
+        ("ie", ctypes.c_double),
+        ("gl", ctypes.c_double),
+        ("isyn", ctypes.c_double),
+        ("vrest", ctypes.c_double),
+        ("th", ctypes.c_double),
+        ("ssyn", ctypes.c_double),
+        ("tau1", ctypes.c_double),
+        ("tau2", ctypes.c_double),
+        ("lmd1", ctypes.c_double),
+        ("lmd2", ctypes.c_double),
+        ("k1_mean", ctypes.c_double),
+        ("k2_mean", ctypes.c_double),
+        ("fired", int1d),
+        ("total_spikes", int1d),
+        ("rec_sspike", ctypes.c_void_p),
+        ("rec_fired", ctypes.c_void_p),
+        ("vi", double1d),
+        ("v0", double1d),
+        ("k1", double1d),
+        ("k2", double1d),
+        ("lspike", double1d),
+        ("rec_isyn", double1d),
+        ("rec_vi", ctypes.c_void_p),
+        ("rec_lspike", ctypes.c_void_p),
+        ]
+
+
+IFNetAllToAll2Struct_p = ctypes.POINTER(IFNetAllToAll2Struct)
+
+ifnet_a2a2 = numpy.ctypeslib.load_library('libifnet_a2a2.so', '.')
+ifnet_a2a2.IFNetAllToAll2_init.restype = ctypes.c_int
+ifnet_a2a2.IFNetAllToAll2_init.argtypes = [IFNetAllToAll2Struct_p]
+ifnet_a2a2.IFNetAllToAll2_vec_v0_rk2.restype = ctypes.c_int
+ifnet_a2a2.IFNetAllToAll2_vec_v0_rk2.argtypes = [IFNetAllToAll2Struct_p]
+
+
+def init(struct):
+    ifnet_a2a2.IFNetAllToAll2_init(ctypes.pointer(struct))
+
+
+def vec_v0_rk2(struct):
+    ifnet_a2a2.IFNetAllToAll2_vec_v0_rk2(ctypes.pointer(struct))
+
+
+if __name__ == '__main__':
+    ifn = IFNetAllToAll2Struct(
+        num_n=NUM_N, num_s=NUM_S, dt=0.1, gl=0.1, vrest=-60, ie=-3.7, th=-40,
+        ssyn=0.5, tau1=3, tau2=1,
+        )
+    ifn.num_k = ifn.num_s
+    v0 = numpy.zeros(ifn.num_n, dtype=float)
+    vi = numpy.zeros(ifn.num_n, dtype=float)
+    ifn.v0 = v0.ctypes.data_as(double1d)
+    ifn.vi = vi.ctypes.data_as(double1d)
+
+    init(ifn)
+    vec_v0_rk2(ifn)
+
+    v0 + vi