Loading...
Searching...
No Matches
kChinese.cc File Reference
`#include "misc/auxiliary.h"`
`#include "misc/intvec.h"`
`#include "misc/options.h"`
`#include "polys/monomials/p_polys.h"`
`#include "polys/matpol.h"`
`#include "polys/simpleideals.h"`
`#include "coeffs/longrat.h"`
`#include "Singular/feOpt.h"`
`#include "kernel/polys.h"`
`#include "kernel/mod2.h"`
`#include "reporter/si_signals.h"`
`#include "kernel/oswrapper/vspace.h"`
`#include "kernel/ideals.h"`
`#include "Singular/cntrlc.h"`
`#include <sys/types.h>`
`#include <sys/wait.h>`
`#include <unistd.h>`
`#include "Singular/links/ssiLink.h"`

Go to the source code of this file.

## Macros

#define | mpz_isNeg(A)

## Functions

number | nlRInit (long i)
static char * | send_number (char *s, number n)
static char * | get_number (char *s, number *n)
static long | size_number (number n)
static char * | send_mon (char *s, poly m, const ring r)
static char * | get_mon (char *s, poly *m, const ring r)
static long | size_mon (poly m, const ring r)
static char * | send_poly (char *s, int ind, poly p, const ring r)
static char * | get_poly (char *s, int &ind, poly *p, const ring r)
static long | size_poly (poly p, const ring r)
ideal | id_ChineseRemainder_0 (ideal *xx, number *q, int rl, const ring r)
ideal | id_Farey_0 (ideal x, number N, const ring r)

## Macro Definition Documentation

## mpz_isNeg

#define mpz_isNeg | ( |  | A | ) |

Value:
((A)->_mp_size<0)
A
#define A
Definition sirandom.c:24

Definition at line 15 of file kChinese.cc.

## Function Documentation

## get_mon()

char * get_mon | ( | char * | s,
|  | poly * | m,
|  | const ring | r )

static

Definition at line 134 of file kChinese.cc.

{
(*m)=p_Init(r);
s=get_number(s,&p_GetCoeff(*m,r));
memcpy((*m)->exp,s,r->ExpL_Size*sizeof(long));
s+=r->ExpL_Size*sizeof(long);
return s;
}
m
int m
Definition cfEzgcd.cc:128
s
const CanonicalForm int s
Definition facAbsFact.cc:51
get_number
static char * get_number(char *s, number *n)
Definition kChinese.cc:59
p_GetCoeff
#define p_GetCoeff(p, r)
Definition monomials.h:50
p_Init
static poly p_Init(const ring r, omBin bin)
Definition p_polys.h:1341

## get_number()

char * get_number | ( | char * | s,
|  | number * | n )

static

Definition at line 59 of file kChinese.cc.

{
// format: last bit 1: imm. number (long)
// otherwise: 0,2: size(long) mpz, size(long) mpz
// 6: size(long) mpz
// 8,10: size(long) -mpz, size(long) mpz
// 14: size(long) -mpz
long *d=(long*)s;
s+=SIZEOF_LONG;
if (((*d)&1)==1) // immediate number
{
*n=(number)(*d);
}
else
{
*n=nlRInit(0);
BOOLEAN neg=(*d>=8);
if (neg) *d-=8;
(*n)->s=(*d)/2;
d=(long*)s;
s+=SIZEOF_LONG;
size_t l=*d;
mpz_realloc2((*n)->z,l*sizeof(mp_limb_t)*8);
mpz_import((*n)->z,l,-1,sizeof(mp_limb_t),0,0,s);
if (neg) mpz_neg((*n)->z,(*n)->z);
s+=l*sizeof(mp_limb_t);
if ((*n)->s!=3)
{
d=(long*)s;
s+=SIZEOF_LONG;
l=*d;
mpz_init2((*n)->n,l*sizeof(mp_limb_t)*8);
mpz_import((*n)->n,l,-1,sizeof(mp_limb_t),0,0,s);
s+=l*sizeof(mp_limb_t);
}
}
return s;
}
BOOLEAN
int BOOLEAN
Definition auxiliary.h:88
l
int l
Definition cfEzgcd.cc:100
nlRInit
number nlRInit(long i)
Definition longrat.cc:2501

## get_poly()

char * get_poly | ( | char * | s,
|  | int & | ind,
|  | poly * | p,
|  | const ring | r )

static

Definition at line 169 of file kChinese.cc.

{
long *d=(long*)s;
ind=*d;
s+=SIZEOF_LONG;
d=(long*)s;
long l=*d;
s+=SIZEOF_LONG;
for(long i=0;i<l;i++)
{
poly m;
s=get_mon(s,&m,r);
pNext(m)=*p;
*p=m;
}
*p=pReverse(*p);
return s;
}
i
int i
Definition cfEzgcd.cc:132
p
int p
Definition cfModGcd.cc:4086
get_mon
static char * get_mon(char *s, poly *m, const ring r)
Definition kChinese.cc:134
pNext
#define pNext(p)
Definition monomials.h:36
pReverse
static poly pReverse(poly p)
Definition p_polys.h:337

## id_ChineseRemainder_0()

ideal id_ChineseRemainder_0 | ( | ideal * | xx,
|  | number * | q,
|  | int | rl,
|  | const ring | r )

Definition at line 199 of file kChinese.cc.

{
int cpus = (long) feOptValue(FE_OPT_CPUS);
if (cpus<1)
{
WerrorS("no sub-processes allowed");
return NULL;
}
int cnt=0;int rw=0; int cl=0;
// find max. size of xx[.]:
for(int j=rl-1;j>=0;j--)
{
int i=IDELEMS(xx[j])*xx[j]->nrows;
if (i>cnt) cnt=i;
if (xx[j]->nrows >rw) rw=xx[j]->nrows; // for lifting matrices
if (xx[j]->ncols >cl) cl=xx[j]->ncols; // for lifting matrices
}
if (rw*cl !=cnt)
{
WerrorS("format mismatch in CRT");
return NULL;
}
if (cpus>=vspace::internals::MAX_PROCESS)
cpus=vspace::internals::MAX_PROCESS-1;
/* start no more than MAX_PROCESS-1 children */
cpus=si_min(cpus,cnt/5);
if (cpus<=1)
/* at least 5 polys for each process, or switch to seriell version */
return id_ChineseRemainder(xx,q,rl,r);
ideal result=idInit(cnt,xx[0]->rank);
result->nrows=rw; // for lifting matrices
result->ncols=cl; // for lifting matrices
int parent_pid=getpid();
using namespace vspace;
vmem_init();
// Create a queue of int
VRef<Queue<int> > queue = vnew<Queue<int> >();
for(int i=cnt-1;i>=0; i--)
{
queue->enqueue(i); // the tasks: construct poly p[i]
}
for(int i=cpus;i>=0;i--)
{
queue->enqueue(-1); // stop sign, one for each child
}
// Create a queue of polys
VRef<Queue<VRef<VString> > > rqueue = vnew<Queue<VRef<VString> > >();
int *pids=(int*)omAlloc0(cpus*sizeof(int));
for (int i=0;i<cpus;i++)
{
int pid = fork_process();
if (pid==0) break; //child
pids[i]=pid;
}
if (parent_pid!=getpid()) // child ------------------------------------------
{
si_set_signal(SIGTERM,sig_term_hdl_child);
singular_close_links();
feSetOptValue(FE_OPT_CPUS,0);
number *x=(number *)omAlloc(rl*sizeof(number));
poly *p=(poly *)omAlloc(rl*sizeof(poly));
CFArray inv_cache(rl);
EXTERN_VAR int n_SwitchChinRem;
n_SwitchChinRem=1;
loop
{
int ind=queue->dequeue();
if (ind== -1)
{
_exit(0);
}
for(int j=rl-1;j>=0;j--)
{
if(ind>=IDELEMS(xx[j])*xx[j]->nrows) // out of range of this ideal
p[j]=NULL;
else
p[j]=xx[j]->m[ind];
}
poly res=p_ChineseRemainder(p,x,q,rl,inv_cache,r);
long l=size_poly(res,r);
//printf("size: %ld kB\n",(l+1023)/1024);
VRef<VString> msg = vstring(l+1);
char *s=(char*)msg->str();
send_poly(s,ind,res,r);
rqueue->enqueue(msg);
if (TEST_OPT_PROT) printf(".");
}
}
else // parent ---------------------------------------------------
{
if (TEST_OPT_PROT) printf("%d children created\n",cpus);
VRef<VString> msg;
while(cnt>0)
{
msg=rqueue->dequeue();
char *s=(char*)msg->str();
int ind;
poly p=NULL;
get_poly(s,ind,&p,r);
//printf("got res[%d]\n",ind);
result->m[ind]=p;
msg.free();
cnt--;
}
int i=cpus-1;
int done=0;
while(done!=cpus)
{
BOOLEAN all_done=TRUE;
if (pids[i]>0)
{
int p=si_waitpid(pids[i],NULL,WNOHANG);
if (p>0)
{
pids[i]=0;
done++;
}
}
if (i==0) i=cpus;
i--;
}
omFreeSize(pids,cpus*sizeof(int));
// removes queues
queue.free();
rqueue.free();
vmem_deinit();
}
return result;
}
TRUE
#define TRUE
Definition auxiliary.h:101
si_min
static int si_min(const int a, const int b)
Definition auxiliary.h:126
CFArray
Array< CanonicalForm > CFArray
Definition canonicalform.h:397
x
Variable x
Definition cfModGcd.cc:4090
cl
cl
Definition cfModGcd.cc:4108
ncols
int int ncols
Definition cf_linsys.cc:32
nrows
int nrows
Definition cf_linsys.cc:32
si_set_signal
si_hdl_typ si_set_signal(int sig, si_hdl_typ signal_handler)
meta function for binding a signal to an handler
Definition cntrlc.cc:121
sig_term_hdl_child
void sig_term_hdl_child(int)
Definition cntrlc.cc:86
result
return result
Definition facAbsBiFact.cc:76
res
CanonicalForm res
Definition facAbsFact.cc:60
j
int j
Definition facHensel.cc:110
WerrorS
void WerrorS(const char *s)
Definition feFopen.cc:24
feSetOptValue
const char * feSetOptValue(feOptIndex opt, char *optarg)
Definition feOpt.cc:154
feOptValue
static void * feOptValue(feOptIndex opt)
Definition feOpt.h:40
EXTERN_VAR
#define EXTERN_VAR
Definition globaldefs.h:6
get_poly
static char * get_poly(char *s, int &ind, poly *p, const ring r)
Definition kChinese.cc:169
size_poly
static long size_poly(poly p, const ring r)
Definition kChinese.cc:188
send_poly
static char * send_poly(char *s, int ind, poly p, const ring r)
Definition kChinese.cc:150
p_ChineseRemainder
poly p_ChineseRemainder(poly *xx, mpz_ptr *x, mpz_ptr *q, int rl, mpz_ptr *C, const ring R)
n_SwitchChinRem
VAR int n_SwitchChinRem
Definition longrat.cc:3074
vspace::internals::MAX_PROCESS
static const int MAX_PROCESS
Definition vspace.h:1419
vspace
Definition vspace.cc:615
vspace::vnew
VRef< T > vnew()
Definition vspace.h:1872
vspace::fork_process
pid_t fork_process()
Definition vspace.cc:1081
vspace::vmem_deinit
static void vmem_deinit()
Definition vspace.h:1742
vspace::vmem_init
static Status vmem_init()
Definition vspace.h:1738
vspace::vstring
static VRef< VString > vstring(const char *s)
Definition vspace.h:2101
omFreeSize
#define omFreeSize(addr, size)
Definition omAllocDecl.h:260
omAlloc
#define omAlloc(size)
Definition omAllocDecl.h:210
omAlloc0
#define omAlloc0(size)
Definition omAllocDecl.h:211
NULL
#define NULL
Definition omList.c:12
TEST_OPT_PROT
#define TEST_OPT_PROT
Definition options.h:105
idInit
ideal idInit(int idsize, int rank)
initialise an ideal / module
Definition simpleideals.cc:35
id_ChineseRemainder
ideal id_ChineseRemainder(ideal *xx, number *q, int rl, const ring r)
Definition simpleideals.cc:2147
IDELEMS
#define IDELEMS(i)
Definition simpleideals.h:23
singular_close_links
void singular_close_links()
Definition ssiLink.cc:3455
loop
#define loop
Definition structs.h:71
vspace::VRef
Definition vspace.h:1747
vspace::VRef::free
void free()
Definition vspace.h:1805

## id_Farey_0()

ideal id_Farey_0 | ( | ideal | x,
|  | number | N,
|  | const ring | r )

Definition at line 330 of file kChinese.cc.

{
int cnt=IDELEMS(x)*x->nrows;
int cpus=(int)(long)feOptValue(FE_OPT_CPUS);
if (cpus>=vspace::internals::MAX_PROCESS)
cpus=vspace::internals::MAX_PROCESS-1;
/* start no more than MAX_PROCESS-1 children */
cpus=si_min(cpus,cnt/5);
if (cpus<=1) /* at least 5 polys for each process,
or switch to seriell version */
return id_Farey(x,N,r);
ideal result=idInit(cnt,x->rank);
result->nrows=x->nrows; // for lifting matrices
result->ncols=x->ncols; // for lifting matrices
int parent_pid=getpid();
using namespace vspace;
vmem_init();
// Create a queue of int
VRef<Queue<int> > queue = vnew<Queue<int> >();
for(int i=cnt-1;i>=0; i--)
{
queue->enqueue(i); // the tasks: construct poly p[i]
}
for(int i=cpus;i>=0;i--)
{
queue->enqueue(-1); // stop sign, one for each child
}
// Create a queue of polys
VRef<Queue<VRef<VString> > > rqueue = vnew<Queue<VRef<VString> > >();
int *pids=(int*)omAlloc0(cpus*sizeof(int));
for (int i=0;i<cpus;i++)
{
int pid = fork_process();
if (pid==0) break; //child
pids[i]=pid;
}
if (parent_pid!=getpid()) // child ------------------------------------------
{
si_set_signal(SIGTERM,sig_term_hdl_child);
singular_close_links();
feSetOptValue(FE_OPT_CPUS,0);
loop
{
int ind=queue->dequeue();
if (ind== -1)
{
_exit(0);
}
poly res=p_Farey(x->m[ind],N,r);
long l=size_poly(res,r);
VRef<VString> msg = vstring(l+1);
char *s=(char*)msg->str();
send_poly(s,ind,res,r);
rqueue->enqueue(msg);
if (TEST_OPT_PROT) printf(".");
}
}
else // parent ---------------------------------------------------
{
if (TEST_OPT_PROT) printf("%d children created\n",cpus);
VRef<VString> msg;
while(cnt>0)
{
msg=rqueue->dequeue();
char *s=(char*)msg->str();
int ind;
poly p=NULL;
get_poly(s,ind,&p,r);
//printf("got res[%d]\n",ind);
result->m[ind]=p;
msg.free();
cnt--;
}
int i=cpus-1;
int done=0;
while(done!=cpus)
{
BOOLEAN all_done=TRUE;
if (pids[i]>0)
{
int p=si_waitpid(pids[i],NULL,WNOHANG);
if (p>0)
{
pids[i]=0;
done++;
}
}
if (i==0) i=cpus;
i--;
}
omFreeSize(pids,cpus*sizeof(int));
// removes queues
queue.free();
rqueue.free();
vmem_deinit();
}
return result;
}
N
const CanonicalForm CFMap CFMap & N
Definition cfEzgcd.cc:56
id_Farey
ideal id_Farey(ideal x, number N, const ring r)
Definition ideals.cc:3103
p_Farey
poly p_Farey(poly p, number N, const ring r)
Definition p_polys.cc:54

## nlRInit()

number nlRInit | ( | long | i | ) |

Definition at line 2501 of file longrat.cc.

{
number z=ALLOC_RNUMBER();
#if defined(LDEBUG)
z->debug=123456;
#endif
mpz_init_set_si(z->z,i);
z->s = 3;
return z;
}
ALLOC_RNUMBER
#define ALLOC_RNUMBER()
Definition coeffs.h:94

## send_mon()

char * send_mon | ( | char * | s,
|  | poly | m,
|  | const ring | r )

static

Definition at line 125 of file kChinese.cc.

{
// format: number exp[0..r->ExpL_Size]
s=send_number(s,p_GetCoeff(m,r));
memcpy(s,m->exp,r->ExpL_Size*sizeof(long));
s+=r->ExpL_Size*sizeof(long);
return s;
}
send_number
static char * send_number(char *s, number n)
Definition kChinese.cc:28

## send_number()

char * send_number | ( | char * | s,
|  | number | n )

static

Definition at line 28 of file kChinese.cc.

{
long *d=(long*)s;
if (SR_HDL(n) & SR_INT)
{
*d=(long)n;
s+=SIZEOF_LONG;
}
else
{
*d=n->s*2;/* n->s in 0..3: 0..6, use +8 for neg. numbers */
s+=SIZEOF_LONG;
if (mpz_isNeg(n->z)) { *d+=8; mpz_neg(n->z,n->z); }
size_t l;
d=(long*)s;
s+=SIZEOF_LONG;
mpz_export(s,&l,-1,sizeof(mp_limb_t),0,0,n->z);
*d=l;
s+=l*sizeof(mp_limb_t);
if (n->s!=3)
{
d=(long*)s;
s+=SIZEOF_LONG;
mpz_export(s,&l,-1,sizeof(mp_limb_t),0,0,n->n);
*d=l;
s+=l*sizeof(mp_limb_t);
}
}
return s;
}
mpz_isNeg
#define mpz_isNeg(A)
Definition kChinese.cc:15
SR_INT
#define SR_INT
Definition longrat.h:67
SR_HDL
#define SR_HDL(A)
Definition tgb.cc:35

## send_poly()

char * send_poly | ( | char * | s,
|  | int | ind,
|  | poly | p,
|  | const ring | r )

static

Definition at line 150 of file kChinese.cc.

{
// format: index(long) length(long) mon...
//p_Write(p,r);PrintLn();
long *d=(long*)s;
*d=ind;
s+=SIZEOF_LONG;
long l=pLength(p);
d=(long*)s;
*d=l;
s+=SIZEOF_LONG;
while(p!=NULL)
{
s=send_mon(s,p,r);
pIter(p);
}
return s;
}
send_mon
static char * send_mon(char *s, poly m, const ring r)
Definition kChinese.cc:125
pIter
#define pIter(p)
Definition monomials.h:37
pLength
static int pLength(poly a)
Definition p_polys.h:190

## size_mon()

long size_mon | ( | poly | m,
|  | const ring | r )

static

Definition at line 143 of file kChinese.cc.

{
long ll=size_number(p_GetCoeff(m,r));
ll+=r->ExpL_Size*sizeof(long);
return ll;
}
size_number
static long size_number(number n)
Definition kChinese.cc:98

## size_number()

long size_number | ( | number | n | ) |

static

Definition at line 98 of file kChinese.cc.

{
long ll=SIZEOF_LONG;
if (SR_HDL(n) & SR_INT)
{
return SIZEOF_LONG;
}
else
{
if (n->s==3)
{
ll+=SIZEOF_LONG*2; /* n->s, mpz size */
long l=mpz_size1(n->z);
ll+=l*sizeof(mp_limb_t);
}
else
{
ll+=SIZEOF_LONG*3; /* n->s, mpz size(n->z) mpz size(n->n)*/
size_t l=mpz_size1(n->z);
ll+=l*sizeof(mp_limb_t);
l=mpz_size1(n->n);
ll+=l*sizeof(mp_limb_t);
}
}
return ll;
}
mpz_size1
#define mpz_size1(A)
Definition si_gmp.h:17

## size_poly()

long size_poly | ( | poly | p,
|  | const ring | r )

static

Definition at line 188 of file kChinese.cc.

{
long l=SIZEOF_LONG*2;
while(p!=NULL)
{
l+=size_mon(p,r);
pIter(p);
}
return l;
}
size_mon
static long size_mon(poly m, const ring r)
Definition kChinese.cc:143