My Project
Loading...
Searching...
No Matches
kChinese.h File Reference
#include "misc/auxiliary.h"
#include "misc/intvec.h"
#include "polys/monomials/p_polys.h"
#include "polys/matpol.h"
#include "polys/simpleideals.h"
#include <gmp.h>

Go to the source code of this file.

Functions

poly p_ChineseRemainder (poly *xx, mpz_ptr *x, mpz_ptr *q, int rl, mpz_ptr *C, const ring R)
ideal id_ChineseRemainder_0 (ideal *xx, number *q, int rl, const ring r)
ideal id_Farey_0 (ideal xx, number N, const ring r)

Function Documentation

◆ 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}
int BOOLEAN
Definition auxiliary.h:88
#define TRUE
Definition auxiliary.h:101
static int si_min(const int a, const int b)
Definition auxiliary.h:126
Array< CanonicalForm > CFArray
int l
Definition cfEzgcd.cc:100
int i
Definition cfEzgcd.cc:132
Variable x
Definition cfModGcd.cc:4090
int p
Definition cfModGcd.cc:4086
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
const CanonicalForm int s
Definition facAbsFact.cc:51
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 xx,
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

◆ p_ChineseRemainder()

poly p_ChineseRemainder ( poly * xx,
mpz_ptr * x,
mpz_ptr * q,
int rl,
mpz_ptr * C,
const ring R )