Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/fglm/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ EXTRA_DIST = fglm.h \
fglm_core.c \
inner-product.c \
matrix-mult.c \
linalg-fglm.c
linalg-fglm.c \
aligned_alloc.h
50 changes: 50 additions & 0 deletions src/fglm/aligned_alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* This file is part of msolve.
*
* msolve is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* msolve is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with msolve. If not, see <https://www.gnu.org/licenses/>
*
* Authors:
* Jérémy Berthomieu
* Christian Eder
* Mohab Safey El Din */

#ifndef ALIGNED_ALLOC_HEADER_H
#define ALIGNED_ALLOC_HEADER_H

#ifdef _WIN32

#include <errno.h>
#include <malloc.h>

static inline int posix_memalign(void **__memptr, size_t __alignment, size_t __size)
{
void *p = _aligned_malloc(__size, __alignment);
if (!p)
{
return ENOMEM;
}
*__memptr = p;
return 0;
}
#endif

static inline void posix_memalign_free(void *__p)
{
#ifdef _WIN32
_aligned_free(__p);
#else
free(__p);
#endif
}

#endif /* ALIGNED_ALLOC_HEADER_H */
9 changes: 5 additions & 4 deletions src/fglm/data_fglm.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <flint/nmod_poly_factor.h>
#include <flint/ulong_extras.h>

#include "aligned_alloc.h"

static inline void free_sp_mat_fglm(sp_matfglm_t *mat){
if(mat!=NULL){
Expand Down Expand Up @@ -80,10 +81,10 @@ static inline fglm_data_t *allocate_fglm_data(szmat_t nrows, szmat_t ncols, szma


static inline void free_fglm_data(fglm_data_t *data){
free(data->vecinit);
free(data->res);
free(data->vecmult);
free(data->vvec);
posix_memalign_free(data->vecinit);
posix_memalign_free(data->res);
posix_memalign_free(data->vecmult);
posix_memalign_free(data->vvec);
free(data->pts);
free(data);
}
Expand Down
14 changes: 8 additions & 6 deletions src/fglm/fglm_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ double omp_get_wtime(void) { return realtime();}
#include "../upolmat/nmod_poly_mat_pmbasis.c"
#endif

#include "aligned_alloc.h"

void print_fglm_data(
FILE *file,
const md_t * const st,
Expand Down Expand Up @@ -756,9 +758,9 @@ static void generate_matrix_sequence(sp_matfglm_t *matxn, fglm_data_t *data,
RED_32,
RED_64);
}
free(Rmat);
free(res);
free(tres);
posix_memalign_free(Rmat);
posix_memalign_free(res);
posix_memalign_free(tres);

}

Expand Down Expand Up @@ -1525,7 +1527,7 @@ param_t *nmod_fglm_compute_trace_data(sp_matfglm_t *matrix, mod_t prime,
#endif

#if DEBUGFGLM >= 1
FILE *fmat = fopen("/tmp/matrix.fglm", "w");
FILE *fmat = fopen("/tmp/matrix.fglm", "wb");
display_fglm_matrix(fmat, matrix);
fclose(fmat);
#endif
Expand Down Expand Up @@ -1728,7 +1730,7 @@ int nmod_fglm_compute_apply_trace_data(sp_matfglm_t *matrix,
#endif

#if DEBUGFGLM >= 1
FILE *fmat = fopen("/tmp/matrix.fglm", "w");
FILE *fmat = fopen("/tmp/matrix.fglm", "wb");
display_fglm_matrix(fmat, matrix);
fclose(fmat);
#endif
Expand Down Expand Up @@ -2077,7 +2079,7 @@ param_t *nmod_fglm_guess_colon(sp_matfglmcol_t *matrix,
#endif

#if DEBUGFGLM >= 1
FILE *fmat = fopen("/tmp/matrix.fglm", "w");
FILE *fmat = fopen("/tmp/matrix.fglm", "wb");
display_fglm_colon_matrix(fmat, matrix);
fclose(fmat);
#endif
Expand Down
1 change: 1 addition & 0 deletions src/msolve/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ EXTRA_DIST = msolve-data.h \
iofiles.c \
msolve.c \
primes.c \
getdelim.h \
../crt/longlong.h \
../crt/ulong_extras.h \
../crt/mpq_reconstruct.c \
Expand Down
131 changes: 131 additions & 0 deletions src/msolve/getdelim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/* getdelim.h --- Implementation of replacement getdelim/getline function.
Copyright (C) 1994, 1996-1998, 2001, 2003, 2005-2025 Free Software
Foundation, Inc.

This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.

This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */

/* Ported from glibc by Simon Josefsson. */

#ifndef GETDELIM_HEADER_H
#define GETDELIM_HEADER_H

#ifdef _WIN32

#include <stdio.h>

#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>

static inline void
alloc_failed (void)
{
#if defined _WIN32 && ! defined __CYGWIN__
/* Avoid errno problem without using the realloc module; see:
https://lists.gnu.org/r/bug-gnulib/2016-08/msg00025.html */
errno = ENOMEM;
#endif
}

/* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
NUL-terminate it). *LINEPTR is a pointer returned from malloc (or
NULL), pointing to *N characters of space. It is realloc'ed as
necessary. Returns the number of characters read (not including
the null terminator), or -1 on error or EOF. */

static inline ssize_t
getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
{
ssize_t result;
size_t cur_len = 0;

if (lineptr == NULL || n == NULL || fp == NULL)
{
errno = EINVAL;
return -1;
}

if (*lineptr == NULL || *n == 0)
{
char *new_lineptr;
*n = 120;
new_lineptr = (char *) realloc (*lineptr, *n);
if (new_lineptr == NULL)
{
alloc_failed ();
return -1;
}
*lineptr = new_lineptr;
}

for (;;)
{
int i;

i = getc (fp);
if (i == EOF)
{
result = -1;
break;
}

/* Make enough space for len+1 (for final NUL) bytes. */
if (cur_len + 1 >= *n)
{
size_t needed_max =
SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
size_t needed = 2 * *n + 1; /* Be generous. */
char *new_lineptr;

if (needed_max < needed)
needed = needed_max;
if (cur_len + 1 >= needed)
{
errno = EOVERFLOW;
return -1;
}

new_lineptr = (char *) realloc (*lineptr, needed);
if (new_lineptr == NULL)
{
alloc_failed ();
return -1;
}

*lineptr = new_lineptr;
*n = needed;
}

(*lineptr)[cur_len] = i;
cur_len++;

if (i == delimiter)
break;
}
(*lineptr)[cur_len] = '\0';
result = cur_len ? cur_len : result;

return result;
}

static inline ssize_t
getline (char **lineptr, size_t *n, FILE *stream)
{
return getdelim (lineptr, n, '\n', stream);
}

#endif

#endif /* GETDELIM_HEADER_H */
Loading
Loading