My Project
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)
#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.

135{
136 (*m)=p_Init(r);
137 s=get_number(s,&p_GetCoeff(*m,r));
138 memcpy((*m)->exp,s,r->ExpL_Size*sizeof(long));
139 s+=r->ExpL_Size*sizeof(long);
140 return s;
141}
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:59
#define p_GetCoeff(p, r)
Definition monomials.h:50
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.

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

◆ get_poly()

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

Definition at line 169 of file kChinese.cc.

170{
171 long *d=(long*)s;
172 ind=*d;
173 s+=SIZEOF_LONG;
174 d=(long*)s;
175 long l=*d;
176 s+=SIZEOF_LONG;
177 for(long i=0;i<l;i++)
178 {
179 poly m;
180 s=get_mon(s,&m,r);
181 pNext(m)=*p;
182 *p=m;
183 }
184 *p=pReverse(*p);
185 return s;
186}
int i
Definition cfEzgcd.cc:132
int p
Definition cfModGcd.cc:4086
static char * get_mon(char *s, poly *m, const ring r)
Definition kChinese.cc:134
#define pNext(p)
Definition monomials.h:36
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.

200{
201 int cpus = (long) feOptValue(FE_OPT_CPUS);
202 if (cpus<1)
203 {
204 WerrorS("no sub-processes allowed");
205 return NULL;
206 }
207 int cnt=0;int rw=0; int cl=0;
208 // find max. size of xx[.]:
209 for(int j=rl-1;j>=0;j--)
210 {
211 int i=IDELEMS(xx[j])*xx[j]->nrows;
212 if (i>cnt) cnt=i;
213 if (xx[j]->nrows >rw) rw=xx[j]->nrows; // for lifting matrices
214 if (xx[j]->ncols >cl) cl=xx[j]->ncols; // for lifting matrices
215 }
216 if (rw*cl !=cnt)
217 {
218 WerrorS("format mismatch in CRT");
219 return NULL;
220 }
223 /* start no more than MAX_PROCESS-1 children */
224 cpus=si_min(cpus,cnt/5);
225 if (cpus<=1)
226 /* at least 5 polys for each process, or switch to seriell version */
227 return id_ChineseRemainder(xx,q,rl,r);
228 ideal result=idInit(cnt,xx[0]->rank);
229 result->nrows=rw; // for lifting matrices
230 result->ncols=cl; // for lifting matrices
231 int parent_pid=getpid();
232 using namespace vspace;
233 vmem_init();
234 // Create a queue of int
235 VRef<Queue<int> > queue = vnew<Queue<int> >();
236 for(int i=cnt-1;i>=0; i--)
237 {
238 queue->enqueue(i); // the tasks: construct poly p[i]
239 }
240 for(int i=cpus;i>=0;i--)
241 {
242 queue->enqueue(-1); // stop sign, one for each child
243 }
244 // Create a queue of polys
246 int *pids=(int*)omAlloc0(cpus*sizeof(int));
247 for (int i=0;i<cpus;i++)
248 {
249 int pid = fork_process();
250 if (pid==0) break; //child
251 pids[i]=pid;
252 }
253 if (parent_pid!=getpid()) // child ------------------------------------------
254 {
257 feSetOptValue(FE_OPT_CPUS,0);
258 number *x=(number *)omAlloc(rl*sizeof(number));
259 poly *p=(poly *)omAlloc(rl*sizeof(poly));
260 CFArray inv_cache(rl);
263 loop
264 {
265 int ind=queue->dequeue();
266 if (ind== -1)
267 {
268 _exit(0);
269 }
270
271 for(int j=rl-1;j>=0;j--)
272 {
273 if(ind>=IDELEMS(xx[j])*xx[j]->nrows) // out of range of this ideal
274 p[j]=NULL;
275 else
276 p[j]=xx[j]->m[ind];
277 }
278 poly res=p_ChineseRemainder(p,x,q,rl,inv_cache,r);
279 long l=size_poly(res,r);
280 //printf("size: %ld kB\n",(l+1023)/1024);
281 VRef<VString> msg = vstring(l+1);
282 char *s=(char*)msg->str();
283 send_poly(s,ind,res,r);
284 rqueue->enqueue(msg);
285 if (TEST_OPT_PROT) printf(".");
286 }
287 }
288 else // parent ---------------------------------------------------
289 {
290 if (TEST_OPT_PROT) printf("%d children created\n",cpus);
291 VRef<VString> msg;
292 while(cnt>0)
293 {
294 msg=rqueue->dequeue();
295 char *s=(char*)msg->str();
296 int ind;
297 poly p=NULL;
298 get_poly(s,ind,&p,r);
299 //printf("got res[%d]\n",ind);
300 result->m[ind]=p;
301 msg.free();
302 cnt--;
303 }
304 int i=cpus-1;
305 int done=0;
306 while(done!=cpus)
307 {
308 BOOLEAN all_done=TRUE;
309 if (pids[i]>0)
310 {
311 int p=si_waitpid(pids[i],NULL,WNOHANG);
312 if (p>0)
313 {
314 pids[i]=0;
315 done++;
316 }
317 }
318 if (i==0) i=cpus;
319 i--;
320 }
321 omFreeSize(pids,cpus*sizeof(int));
322 // removes queues
323 queue.free();
324 rqueue.free();
325 vmem_deinit();
326 }
327 return result;
328}
#define TRUE
Definition auxiliary.h:101
static int si_min(const int a, const int b)
Definition auxiliary.h:126
Array< CanonicalForm > CFArray
Variable x
Definition cfModGcd.cc:4090
cl
Definition cfModGcd.cc:4108
int int ncols
Definition cf_linsys.cc:32
int nrows
Definition cf_linsys.cc:32
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
void sig_term_hdl_child(int)
Definition cntrlc.cc:86
return result
CanonicalForm res
Definition facAbsFact.cc:60
int j
Definition facHensel.cc:110
void WerrorS(const char *s)
Definition feFopen.cc:24
const char * feSetOptValue(feOptIndex opt, char *optarg)
Definition feOpt.cc:154
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:169
static long size_poly(poly p, const ring r)
Definition kChinese.cc:188
static char * send_poly(char *s, int ind, poly p, const ring r)
Definition kChinese.cc:150
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:3075
static const int MAX_PROCESS
Definition vspace.h:1419
VRef< T > vnew()
Definition vspace.h:1872
pid_t fork_process()
Definition vspace.cc:1108
static void vmem_deinit()
Definition vspace.h:1742
static Status vmem_init()
Definition vspace.h:1738
static VRef< VString > vstring(const char *s)
Definition vspace.h:2101
#define omFreeSize(addr, size)
#define omAlloc(size)
#define omAlloc0(size)
#define NULL
Definition omList.c:12
#define TEST_OPT_PROT
Definition options.h:105
ideal idInit(int idsize, int rank)
initialise an ideal / module
ideal id_ChineseRemainder(ideal *xx, number *q, int rl, const ring r)
#define IDELEMS(i)
#define loop
Definition structs.h:71
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.

331{
332 int cnt=IDELEMS(x)*x->nrows;
333 int cpus=(int)(long)feOptValue(FE_OPT_CPUS);
336 /* start no more than MAX_PROCESS-1 children */
337 cpus=si_min(cpus,cnt/5);
338 if (cpus<=1) /* at least 5 polys for each process,
339 or switch to seriell version */
340 return id_Farey(x,N,r);
341 ideal result=idInit(cnt,x->rank);
342 result->nrows=x->nrows; // for lifting matrices
343 result->ncols=x->ncols; // for lifting matrices
344
345 int parent_pid=getpid();
346 using namespace vspace;
347 vmem_init();
348 // Create a queue of int
349 VRef<Queue<int> > queue = vnew<Queue<int> >();
350 for(int i=cnt-1;i>=0; i--)
351 {
352 queue->enqueue(i); // the tasks: construct poly p[i]
353 }
354 for(int i=cpus;i>=0;i--)
355 {
356 queue->enqueue(-1); // stop sign, one for each child
357 }
358 // Create a queue of polys
360 int *pids=(int*)omAlloc0(cpus*sizeof(int));
361 for (int i=0;i<cpus;i++)
362 {
363 int pid = fork_process();
364 if (pid==0) break; //child
365 pids[i]=pid;
366 }
367 if (parent_pid!=getpid()) // child ------------------------------------------
368 {
371 feSetOptValue(FE_OPT_CPUS,0);
372 loop
373 {
374 int ind=queue->dequeue();
375 if (ind== -1)
376 {
377 _exit(0);
378 }
379
380 poly res=p_Farey(x->m[ind],N,r);
381 long l=size_poly(res,r);
382 VRef<VString> msg = vstring(l+1);
383 char *s=(char*)msg->str();
384 send_poly(s,ind,res,r);
385 rqueue->enqueue(msg);
386 if (TEST_OPT_PROT) printf(".");
387 }
388 }
389 else // parent ---------------------------------------------------
390 {
391 if (TEST_OPT_PROT) printf("%d children created\n",cpus);
392 VRef<VString> msg;
393 while(cnt>0)
394 {
395 msg=rqueue->dequeue();
396 char *s=(char*)msg->str();
397 int ind;
398 poly p=NULL;
399 get_poly(s,ind,&p,r);
400 //printf("got res[%d]\n",ind);
401 result->m[ind]=p;
402 msg.free();
403 cnt--;
404 }
405 int i=cpus-1;
406 int done=0;
407 while(done!=cpus)
408 {
409 BOOLEAN all_done=TRUE;
410 if (pids[i]>0)
411 {
412 int p=si_waitpid(pids[i],NULL,WNOHANG);
413 if (p>0)
414 {
415 pids[i]=0;
416 done++;
417 }
418 }
419 if (i==0) i=cpus;
420 i--;
421 }
422 omFreeSize(pids,cpus*sizeof(int));
423 // removes queues
424 queue.free();
425 rqueue.free();
426 vmem_deinit();
427 }
428 return result;
429}
const CanonicalForm CFMap CFMap & N
Definition cfEzgcd.cc:56
ideal id_Farey(ideal x, number N, const ring r)
Definition ideals.cc:3108
poly p_Farey(poly p, number N, const ring r)
Definition p_polys.cc:54

◆ nlRInit()

number nlRInit ( long i)

Definition at line 2502 of file longrat.cc.

2503{
2504 number z=ALLOC_RNUMBER();
2505#if defined(LDEBUG)
2506 z->debug=123456;
2507#endif
2508 mpz_init_set_si(z->z,i);
2509 z->s = 3;
2510 return z;
2511}
#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.

126{
127 // format: number exp[0..r->ExpL_Size]
129 memcpy(s,m->exp,r->ExpL_Size*sizeof(long));
130 s+=r->ExpL_Size*sizeof(long);
131 return s;
132}
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.

29{
30 long *d=(long*)s;
31 if (SR_HDL(n) & SR_INT)
32 {
33 *d=(long)n;
34 s+=SIZEOF_LONG;
35 }
36 else
37 {
38 *d=n->s*2;/* n->s in 0..3: 0..6, use +8 for neg. numbers */
39 s+=SIZEOF_LONG;
40 if (mpz_isNeg(n->z)) { *d+=8; mpz_neg(n->z,n->z); }
41 size_t l;
42 d=(long*)s;
43 s+=SIZEOF_LONG;
44 mpz_export(s,&l,-1,sizeof(mp_limb_t),0,0,n->z);
45 *d=l;
46 s+=l*sizeof(mp_limb_t);
47 if (n->s!=3)
48 {
49 d=(long*)s;
50 s+=SIZEOF_LONG;
51 mpz_export(s,&l,-1,sizeof(mp_limb_t),0,0,n->n);
52 *d=l;
53 s+=l*sizeof(mp_limb_t);
54 }
55 }
56 return s;
57}
#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()

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

Definition at line 150 of file kChinese.cc.

151{
152 // format: index(long) length(long) mon...
153 //p_Write(p,r);PrintLn();
154 long *d=(long*)s;
155 *d=ind;
156 s+=SIZEOF_LONG;
157 long l=pLength(p);
158 d=(long*)s;
159 *d=l;
160 s+=SIZEOF_LONG;
161 while(p!=NULL)
162 {
163 s=send_mon(s,p,r);
164 pIter(p);
165 }
166 return s;
167}
static char * send_mon(char *s, poly m, const ring r)
Definition kChinese.cc:125
#define pIter(p)
Definition monomials.h:37
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.

144{
145 long ll=size_number(p_GetCoeff(m,r));
146 ll+=r->ExpL_Size*sizeof(long);
147 return ll;
148}
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.

99{
100 long ll=SIZEOF_LONG;
101 if (SR_HDL(n) & SR_INT)
102 {
103 return SIZEOF_LONG;
104 }
105 else
106 {
107 if (n->s==3)
108 {
109 ll+=SIZEOF_LONG*2; /* n->s, mpz size */
110 long l=mpz_size1(n->z);
111 ll+=l*sizeof(mp_limb_t);
112 }
113 else
114 {
115 ll+=SIZEOF_LONG*3; /* n->s, mpz size(n->z) mpz size(n->n)*/
116 size_t l=mpz_size1(n->z);
117 ll+=l*sizeof(mp_limb_t);
118 l=mpz_size1(n->n);
119 ll+=l*sizeof(mp_limb_t);
120 }
121 }
122 return ll;
123}
#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.

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