Skip to content

Commit 7764003

Browse files
committed
use memory allocator in sage/combinat/designs/designs_pyx.pyx
1 parent 1be0a58 commit 7764003

File tree

1 file changed

+11
-41
lines changed

1 file changed

+11
-41
lines changed

src/sage/combinat/designs/designs_pyx.pyx

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ from sage.data_structures.bitset_base cimport *
1111

1212
from libc.string cimport memset
1313

14-
from cysignals.memory cimport sig_malloc, sig_calloc, sig_realloc, sig_free
14+
from cysignals.memory cimport sig_malloc, sig_realloc, sig_free
15+
from memory_allocator cimport MemoryAllocator
1516

1617
from sage.misc.unknown import Unknown
1718

@@ -305,7 +306,8 @@ def is_orthogonal_array(OA, int k, int n, int t=2, verbose=False, terminology='O
305306
cdef int i,j,l
306307

307308
# A copy of OA
308-
cdef unsigned short * OAc = <unsigned short *> sig_malloc(k*n2*sizeof(unsigned short))
309+
cdef MemoryAllocator mem = MemoryAllocator()
310+
cdef unsigned short * OAc = <unsigned short *> mem.malloc(k*n2*sizeof(unsigned short))
309311

310312
cdef unsigned short * C1
311313
cdef unsigned short * C2
@@ -321,7 +323,6 @@ def is_orthogonal_array(OA, int k, int n, int t=2, verbose=False, terminology='O
321323
if verbose:
322324
print({"OA": "{} is not in the interval [0..{}]".format(x,n-1),
323325
"MOLS": "Entry {} was expected to be in the interval [0..{}]".format(x,n-1)}[terminology])
324-
sig_free(OAc)
325326
return False
326327
OAc[j*n2+i] = x
327328

@@ -338,14 +339,12 @@ def is_orthogonal_array(OA, int k, int n, int t=2, verbose=False, terminology='O
338339
bitset_add(seen,n*C1[l]+C2[l])
339340

340341
if bitset_len(seen) != n2: # Have we seen all pairs ?
341-
sig_free(OAc)
342342
bitset_free(seen)
343343
if verbose:
344344
print({"OA": "Columns {} and {} are not orthogonal".format(i,j),
345345
"MOLS": "Squares {} and {} are not orthogonal".format(i,j)}[terminology])
346346
return False
347347

348-
sig_free(OAc)
349348
bitset_free(seen)
350349
return True
351350

@@ -469,7 +468,8 @@ def is_group_divisible_design(groups, blocks, v, G=None, K=None, lambd=1, verbos
469468
print("{} does not belong to [0,...,{}]".format(x, n-1))
470469
return False
471470

472-
cdef unsigned short * matrix = <unsigned short *> sig_calloc(n*n, sizeof(unsigned short))
471+
cdef MemoryAllocator mem = MemoryAllocator()
472+
cdef unsigned short * matrix = <unsigned short *> mem.calloc(n*n, sizeof(unsigned short))
473473
if matrix is NULL:
474474
raise MemoryError
475475

@@ -500,7 +500,6 @@ def is_group_divisible_design(groups, blocks, v, G=None, K=None, lambd=1, verbos
500500
if not len(g) in G:
501501
if verbose:
502502
print("a group has size {} while G={}".format(len(g),list(G)))
503-
sig_free(matrix)
504503
return False
505504

506505
# Checks that two points of the same group were never covered
@@ -513,7 +512,6 @@ def is_group_divisible_design(groups, blocks, v, G=None, K=None, lambd=1, verbos
513512
if matrix[ii*n+jj] != 0:
514513
if verbose:
515514
print("the pair ({},{}) belongs to a group but appears in some block".format(ii, jj))
516-
sig_free(matrix)
517515
return False
518516

519517
# We fill the entries with what is expected by the next loop
@@ -526,11 +524,8 @@ def is_group_divisible_design(groups, blocks, v, G=None, K=None, lambd=1, verbos
526524
if matrix[i*n+j] != l:
527525
if verbose:
528526
print("the pair ({},{}) has been seen {} times but lambda={}".format(i,j,matrix[i*n+j],l))
529-
sig_free(matrix)
530527
return False
531528

532-
sig_free(matrix)
533-
534529
return True if not guess_groups else (True, groups)
535530

536531

@@ -836,16 +831,11 @@ def is_quasi_difference_matrix(M, G, int k, int lmbda, int mu, int u, verbose=Fa
836831
cdef dict group_to_int = {v:i for i,v in enumerate(int_to_group)}
837832

838833
# Allocations
839-
cdef int ** x_minus_y = <int **> sig_malloc((n+1)*sizeof(int *))
840-
cdef int * x_minus_y_data = <int *> sig_malloc((n+1)*(n+1)*sizeof(int))
841-
cdef int * M_c = <int *> sig_malloc(k*M_nrows*sizeof(int))
842-
cdef int * G_seen = <int *> sig_malloc((n+1)*sizeof(int))
843-
if (x_minus_y == NULL or x_minus_y_data == NULL or M_c == NULL or G_seen == NULL):
844-
sig_free(x_minus_y)
845-
sig_free(x_minus_y_data)
846-
sig_free(G_seen)
847-
sig_free(M_c)
848-
raise MemoryError
834+
cdef MemoryAllocator mem = MemoryAllocator()
835+
cdef int ** x_minus_y = <int **> mem.malloc((n+1)*sizeof(int *))
836+
cdef int * x_minus_y_data = <int *> mem.malloc((n+1)*(n+1)*sizeof(int))
837+
cdef int * M_c = <int *> mem.malloc(k*M_nrows*sizeof(int))
838+
cdef int * G_seen = <int *> mem.malloc((n+1)*sizeof(int))
849839

850840
# The "x-y" table. If g_i, g_j \in G, then x_minus_y[i][j] is equal to
851841
# group_to_int[g_i-g_j].
@@ -883,10 +873,6 @@ def is_quasi_difference_matrix(M, G, int k, int lmbda, int mu, int u, verbose=Fa
883873
if bit:
884874
if verbose:
885875
print("Row {} contains more than one empty entry".format(i))
886-
sig_free(x_minus_y_data)
887-
sig_free(x_minus_y)
888-
sig_free(G_seen)
889-
sig_free(M_c)
890876
return False
891877
bit = True
892878

@@ -900,10 +886,6 @@ def is_quasi_difference_matrix(M, G, int k, int lmbda, int mu, int u, verbose=Fa
900886
if verbose:
901887
print("Column {} contains {} empty entries instead of the expected "
902888
"lambda.u={}.{}={}".format(j, ii, lmbda, u, lmbda*u))
903-
sig_free(x_minus_y_data)
904-
sig_free(x_minus_y)
905-
sig_free(G_seen)
906-
sig_free(M_c)
907889
return False
908890

909891
# We are now ready to test every pair of columns
@@ -917,10 +899,6 @@ def is_quasi_difference_matrix(M, G, int k, int lmbda, int mu, int u, verbose=Fa
917899
if verbose:
918900
print("Columns {} and {} generate 0 exactly {} times "
919901
"instead of the expected mu(={})".format(i,j,G_seen[0],mu))
920-
sig_free(x_minus_y_data)
921-
sig_free(x_minus_y)
922-
sig_free(G_seen)
923-
sig_free(M_c)
924902
return False
925903

926904
for ii in range(1,n): # bad number of g_ii\in G
@@ -929,16 +907,8 @@ def is_quasi_difference_matrix(M, G, int k, int lmbda, int mu, int u, verbose=Fa
929907
print("Columns {} and {} do not generate all elements of G "
930908
"exactly lambda(={}) times. The element {} appeared {} "
931909
"times as a difference.".format(i,j,lmbda,int_to_group[ii],G_seen[ii]))
932-
sig_free(x_minus_y_data)
933-
sig_free(x_minus_y)
934-
sig_free(G_seen)
935-
sig_free(M_c)
936910
return False
937911

938-
sig_free(x_minus_y_data)
939-
sig_free(x_minus_y)
940-
sig_free(G_seen)
941-
sig_free(M_c)
942912
return True
943913

944914

0 commit comments

Comments
 (0)