My Project
Macros | Functions
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 "kernel/oswrapper/vspace.h"
#include "kernel/ideals.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

Go to the source code of this file.

Macros

#define mpz_isNeg(A)   ((A)->_mp_size<0)
 

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)
 
void test_n (poly n)
 

Macro Definition Documentation

◆ mpz_isNeg

#define mpz_isNeg (   A)    ((A)->_mp_size<0)

Definition at line 15 of file kChinese.cc.

Function Documentation

◆ get_mon()

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

Definition at line 131 of file kChinese.cc.

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

◆ get_number()

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

Definition at line 56 of file kChinese.cc.

57 {
58  // format: last bit 1: imm. number (long)
59  // otherwise: 0,2: size(long) mpz, size(long) mpz
60  // 6: size(long) mpz
61  // 8,10: size(long) -mpz, size(long) mpz
62  // 14: size(long) -mpz
63  long *d=(long*)s;
64  s+=SIZEOF_LONG;
65  if (((*d)&1)==1) // immidiate number
66  {
67  *n=(number)(*d);
68  }
69  else
70  {
71  *n=nlRInit(0);
72  BOOLEAN neg=(*d>=8);
73  if (neg) *d-=8;
74  (*n)->s=(*d)/2;
75  d=(long*)s;
76  s+=SIZEOF_LONG;
77  size_t l=*d;
78  mpz_realloc2((*n)->z,l*sizeof(mp_limb_t)*8);
79  mpz_import((*n)->z,l,-1,sizeof(mp_limb_t),0,0,s);
80  if (neg) mpz_neg((*n)->z,(*n)->z);
81  s+=l*sizeof(mp_limb_t);
82  if ((*n)->s!=3)
83  {
84  d=(long*)s;
85  s+=SIZEOF_LONG;
86  l=*d;
87  mpz_init2((*n)->n,l*sizeof(mp_limb_t)*8);
88  mpz_import((*n)->n,l,-1,sizeof(mp_limb_t),0,0,s);
89  s+=l*sizeof(mp_limb_t);
90  }
91  }
92  return s;
93 }
int BOOLEAN
Definition: auxiliary.h:87
int l
Definition: cfEzgcd.cc:100
number nlRInit(long i)
Definition: longrat.cc:2530

◆ get_poly()

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

Definition at line 166 of file kChinese.cc.

167 {
168  long *d=(long*)s;
169  ind=*d;
170  s+=SIZEOF_LONG;
171  d=(long*)s;
172  long l=*d;
173  s+=SIZEOF_LONG;
174  for(long i=0;i<l;i++)
175  {
176  poly m;
177  s=get_mon(s,&m,r);
178  pNext(m)=*p;
179  *p=m;
180  }
181  *p=pReverse(*p);
182  return s;
183 }
int i
Definition: cfEzgcd.cc:132
int p
Definition: cfModGcd.cc:4078
static char * get_mon(char *s, poly *m, const ring r)
Definition: kChinese.cc:131
#define pNext(p)
Definition: monomials.h:36
static poly pReverse(poly p)
Definition: p_polys.h:335

◆ id_ChineseRemainder_0()

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

Definition at line 196 of file kChinese.cc.

197 {
198  int cnt=0;int rw=0; int cl=0;
199  // find max. size of xx[.]:
200  for(int j=rl-1;j>=0;j--)
201  {
202  int i=IDELEMS(xx[j])*xx[j]->nrows;
203  if (i>cnt) cnt=i;
204  if (xx[j]->nrows >rw) rw=xx[j]->nrows; // for lifting matrices
205  if (xx[j]->ncols >cl) cl=xx[j]->ncols; // for lifting matrices
206  }
207  if (rw*cl !=cnt)
208  {
209  WerrorS("format mismatch in CRT");
210  return NULL;
211  }
212  int cpus=(int)(long)feOptValue(FE_OPT_CPUS);
213  if ((cpus==1) || (2*cpus>=cnt))
214  /* at least 2 polys for each process, or switch to seriell version */
215  return id_ChineseRemainder(xx,q,rl,r);
216  ideal result=idInit(cnt,xx[0]->rank);
217  result->nrows=rw; // for lifting matrices
218  result->ncols=cl; // for lifting matrices
219  int parent_pid=getpid();
220  using namespace vspace;
221  vmem_init();
222  // Create a queue of int
223  VRef<Queue<int> > queue = vnew<Queue<int> >();
224  for(int i=cnt-1;i>=0; i--)
225  {
226  queue->enqueue(i); // the tasks: construct poly p[i]
227  }
228  for(int i=cpus;i>=0;i--)
229  {
230  queue->enqueue(-1); // stop sign, one for each child
231  }
232  // Create a queue of polys
233  VRef<Queue<VRef<VString> > > rqueue = vnew<Queue<VRef<VString> > >();
234  for (int i=0;i<cpus;i++)
235  {
236  int pid = fork_process();
237  if (pid==0) break; //child
238  }
239  if (parent_pid!=getpid()) // child ------------------------------------------
240  {
241  number *x=(number *)omAlloc(rl*sizeof(number));
242  poly *p=(poly *)omAlloc(rl*sizeof(poly));
243  CFArray inv_cache(rl);
245  n_SwitchChinRem=1;
246  loop
247  {
248  int ind=queue->dequeue();
249  if (ind== -1)
250  {
251  exit(0);
252  }
253 
254  for(int j=rl-1;j>=0;j--)
255  {
256  if(ind>=IDELEMS(xx[j])*xx[j]->nrows) // out of range of this ideal
257  p[j]=NULL;
258  else
259  p[j]=xx[j]->m[ind];
260  }
261  poly res=p_ChineseRemainder(p,x,q,rl,inv_cache,r);
262  long l=size_poly(res,r);
263  //printf("size: %ld kB\n",(l+1023)/1024);
264  VRef<VString> msg = vstring(l+1);
265  char *s=(char*)msg->str();
266  send_poly(s,ind,res,r);
267  rqueue->enqueue(msg);
268  if (TEST_OPT_PROT) printf(".");
269  }
270  }
271  else // parent ---------------------------------------------------
272  {
273  if (TEST_OPT_PROT) printf("%d children created\n",cpus);
274  VRef<VString> msg;
275  while(cnt>0)
276  {
277  msg=rqueue->dequeue();
278  char *s=(char*)msg->str();
279  int ind;
280  poly p=NULL;
281  get_poly(s,ind,&p,r);
282  //printf("got res[%d]\n",ind);
283  result->m[ind]=p;
284  msg.free();
285  cnt--;
286  }
287  // removes queues
288  queue.free();
289  rqueue.free();
290  vmem_deinit();
291  }
292  return result;
293 }
Variable x
Definition: cfModGcd.cc:4082
cl
Definition: cfModGcd.cc:4100
int int ncols
Definition: cf_linsys.cc:32
int nrows
Definition: cf_linsys.cc:32
return result
Definition: facAbsBiFact.cc:75
CanonicalForm res
Definition: facAbsFact.cc:60
int j
Definition: facHensel.cc:110
void WerrorS(const char *s)
Definition: feFopen.cc:24
static void * feOptValue(feOptIndex opt)
Definition: feOpt.h:40
#define EXTERN_VAR
Definition: globaldefs.h:6
static char * get_poly(char *s, int &ind, poly *p, const ring r)
Definition: kChinese.cc:166
static long size_poly(poly p, const ring r)
Definition: kChinese.cc:185
static char * send_poly(char *s, int ind, poly p, const ring r)
Definition: kChinese.cc:147
poly p_ChineseRemainder(poly *xx, mpz_ptr *x, mpz_ptr *q, int rl, mpz_ptr *C, const ring R)
VAR int n_SwitchChinRem
Definition: longrat.cc:3094
pid_t fork_process()
Definition: vspace.cc:977
static void vmem_deinit()
Definition: vspace.h:1742
static VRef< VString > vstring(const char *s)
Definition: vspace.h:2101
static Status vmem_init()
Definition: vspace.h:1738
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define NULL
Definition: omList.c:12
#define TEST_OPT_PROT
Definition: options.h:103
ideal idInit(int idsize, int rank)
initialise an ideal / module
Definition: simpleideals.cc:35
ideal id_ChineseRemainder(ideal *xx, number *q, int rl, const ring r)
#define IDELEMS(i)
Definition: simpleideals.h:23
#define loop
Definition: structs.h:75
void free()
Definition: vspace.h:1805

◆ id_Farey_0()

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

Definition at line 295 of file kChinese.cc.

296 {
297  int cnt=IDELEMS(x)*x->nrows;
298  int cpus=(int)(long)feOptValue(FE_OPT_CPUS);
299  if (2*cpus>=cnt) /* at least 2 polys for each process,
300  or switch to seriell version */
301  return id_Farey(x,N,r);
302  ideal result=idInit(cnt,x->rank);
303  result->nrows=x->nrows; // for lifting matrices
304  result->ncols=x->ncols; // for lifting matrices
305 
306  int parent_pid=getpid();
307  using namespace vspace;
308  vmem_init();
309  // Create a queue of int
310  VRef<Queue<int> > queue = vnew<Queue<int> >();
311  for(int i=cnt-1;i>=0; i--)
312  {
313  queue->enqueue(i); // the tasks: construct poly p[i]
314  }
315  for(int i=cpus;i>=0;i--)
316  {
317  queue->enqueue(-1); // stop sign, one for each child
318  }
319  // Create a queue of polys
320  VRef<Queue<VRef<VString> > > rqueue = vnew<Queue<VRef<VString> > >();
321  for (int i=0;i<cpus;i++)
322  {
323  int pid = fork_process();
324  if (pid==0) break; //child
325  }
326  if (parent_pid!=getpid()) // child ------------------------------------------
327  {
328  loop
329  {
330  int ind=queue->dequeue();
331  if (ind== -1)
332  {
333  exit(0);
334  }
335 
336  poly res=p_Farey(x->m[ind],N,r);
337  long l=size_poly(res,r);
338  VRef<VString> msg = vstring(l+1);
339  char *s=(char*)msg->str();
340  send_poly(s,ind,res,r);
341  rqueue->enqueue(msg);
342  if (TEST_OPT_PROT) printf(".");
343  }
344  }
345  else // parent ---------------------------------------------------
346  {
347  if (TEST_OPT_PROT) printf("%d children created\n",cpus);
348  VRef<VString> msg;
349  while(cnt>0)
350  {
351  msg=rqueue->dequeue();
352  char *s=(char*)msg->str();
353  int ind;
354  poly p=NULL;
355  get_poly(s,ind,&p,r);
356  //printf("got res[%d]\n",ind);
357  result->m[ind]=p;
358  msg.free();
359  cnt--;
360  }
361  // removes queues
362  queue.free();
363  rqueue.free();
364  vmem_deinit();
365  }
366  return result;
367 }
const CanonicalForm CFMap CFMap & N
Definition: cfEzgcd.cc:56
ideal id_Farey(ideal x, number N, const ring r)
Definition: ideals.cc:2842
poly p_Farey(poly p, number N, const ring r)
Definition: p_polys.cc:54

◆ nlRInit()

number nlRInit ( long  i)

Definition at line 2530 of file longrat.cc.

2531 {
2532  number z=ALLOC_RNUMBER();
2533 #if defined(LDEBUG)
2534  z->debug=123456;
2535 #endif
2536  mpz_init_set_si(z->z,i);
2537  z->s = 3;
2538  return z;
2539 }
#define ALLOC_RNUMBER()
Definition: coeffs.h:87

◆ send_mon()

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

Definition at line 122 of file kChinese.cc.

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

◆ send_number()

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

Definition at line 25 of file kChinese.cc.

26 {
27  long *d=(long*)s;
28  if (SR_HDL(n) & SR_INT)
29  {
30  *d=(long)n;
31  s+=SIZEOF_LONG;
32  }
33  else
34  {
35  *d=n->s*2;/* n->s in 0..3: 0..6, use +8 for neg. numbers */
36  s+=SIZEOF_LONG;
37  if (mpz_isNeg(n->z)) { *d+=8; mpz_neg(n->z,n->z); }
38  size_t l;
39  d=(long*)s;
40  s+=SIZEOF_LONG;
41  mpz_export(s,&l,-1,sizeof(mp_limb_t),0,0,n->z);
42  *d=l;
43  s+=l*sizeof(mp_limb_t);
44  if (n->s!=3)
45  {
46  d=(long*)s;
47  s+=SIZEOF_LONG;
48  mpz_export(s,&l,-1,sizeof(mp_limb_t),0,0,n->n);
49  *d=l;
50  s+=l*sizeof(mp_limb_t);
51  }
52  }
53  return s;
54 }
#define mpz_isNeg(A)
Definition: kChinese.cc:15
#define SR_INT
Definition: longrat.h:67
#define SR_HDL(A)
Definition: tgb.cc:35

◆ send_poly()

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

Definition at line 147 of file kChinese.cc.

148 {
149  // format: index(long) length(long) mon...
150  //p_Write(p,r);PrintLn();
151  long *d=(long*)s;
152  *d=ind;
153  s+=SIZEOF_LONG;
154  long l=pLength(p);
155  d=(long*)s;
156  *d=l;
157  s+=SIZEOF_LONG;
158  while(p!=NULL)
159  {
160  s=send_mon(s,p,r);
161  pIter(p);
162  }
163  return s;
164 }
static char * send_mon(char *s, poly m, const ring r)
Definition: kChinese.cc:122
#define pIter(p)
Definition: monomials.h:37
static unsigned pLength(poly a)
Definition: p_polys.h:191

◆ size_mon()

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

Definition at line 140 of file kChinese.cc.

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

◆ size_number()

static long size_number ( number  n)
static

Definition at line 95 of file kChinese.cc.

96 {
97  long ll=SIZEOF_LONG;
98  if (SR_HDL(n) & SR_INT)
99  {
100  return SIZEOF_LONG;
101  }
102  else
103  {
104  if (n->s==3)
105  {
106  ll+=SIZEOF_LONG*2; /* n->s, mpz size */
107  long l=mpz_size1(n->z);
108  ll+=l*sizeof(mp_limb_t);
109  }
110  else
111  {
112  ll+=SIZEOF_LONG*3; /* n->s, mpz size(n->z) mpz size(n->n)*/
113  size_t l=mpz_size1(n->z);
114  ll+=l*sizeof(mp_limb_t);
115  l=mpz_size1(n->n);
116  ll+=l*sizeof(mp_limb_t);
117  }
118  }
119  return ll;
120 }
#define mpz_size1(A)
Definition: si_gmp.h:17

◆ size_poly()

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

Definition at line 185 of file kChinese.cc.

186 {
187  long l=SIZEOF_LONG*2;
188  while(p!=NULL)
189  {
190  l+=size_mon(p,r);
191  pIter(p);
192  }
193  return l;
194 }
static long size_mon(poly m, const ring r)
Definition: kChinese.cc:140

◆ test_n()

void test_n ( poly  n)

Definition at line 369 of file kChinese.cc.

370 {
371  p_Write(n,currRing);
372  char *buf=(char*)omAlloc0(2048*1000);
373  int ll=size_poly(n,currRing);
374  printf("size: %d\n",ll);
375  char *s=send_poly(buf,12345,n,currRing);
376  printf("send len: %d\n",(int)(s-buf));
377  long *d=(long*)buf;
378  for(int i=0;i<=ll/SIZEOF_LONG;i++) printf("%ld ",d[i]);
379  printf("\n");
380  n=NULL;
381  s=get_poly(buf,ll,&n,currRing);
382  printf("read len: %d\n",(int)(s-buf));
383  Print(":index: %d\n",ll);
384  p_Write(n,currRing);
385  PrintLn();
386  omFree(buf);
387 }
#define Print
Definition: emacs.cc:80
#define omFree(addr)
Definition: omAllocDecl.h:261
#define omAlloc0(size)
Definition: omAllocDecl.h:211
void p_Write(poly p, ring lmRing, ring tailRing)
Definition: polys0.cc:342
VAR ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition: polys.cc:13
void PrintLn()
Definition: reporter.cc:310
int status int void * buf
Definition: si_signals.h:59