Changeset 89b59f in git for kernel/tgb.cc


Ignore:
Timestamp:
Feb 7, 2007, 1:40:06 PM (17 years ago)
Author:
Michael Brickenstein <bricken@…>
Branches:
(u'spielwiese', '4a9821a93ffdc22a6696668bd4f6b8c9de3e6c5f')
Children:
508937797961aad4ba379a8c5136675e42495d8a
Parents:
19370ce8f2f2a00e555d8de12946e08fb4e20b7c
Message:
+ noro cache


git-svn-id: file:///usr/local/Singular/svn/trunk@9822 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/tgb.cc

    r19370c r89b59f  
    55*  Computer Algebra System SINGULAR     *
    66****************************************/
    7 /* $Id: tgb.cc,v 1.123 2007-02-07 10:49:41 Singular Exp $ */
     7/* $Id: tgb.cc,v 1.124 2007-02-07 12:39:55 bricken Exp $ */
    88/*
    99* ABSTRACT: slimgb and F4 implementation
     
    2929#include <stdlib.h>
    3030#include <stdio.h>
     31#include <map>
    3132#define SR_HDL(A) ((long)(A))
    3233static const int bundle_size=1000;
     
    21822183   
    21832184}
     2185
     2186#ifdef NORO_CACHE
     2187class NoroCache{
     2188public:
     2189  void insert(poly term, poly nf, int len){
     2190    assume(impl.find(p_Copy(term,currRing))==impl.end());
     2191    assume(len==pLength(nf));
     2192    if (term==nf){
     2193      poly copied=p_Copy(term, currRing);
     2194      term=copied;
     2195      nf=copied;
     2196      ressources.push_back(copied);
     2197    } else{
     2198      term=p_Copy(term,currRing);
     2199
     2200      ressources.push_back(term);
     2201      if (nf){
     2202        nf=p_Copy(nf,currRing);
     2203        ressources.push_back(nf);
     2204      }
     2205    }
     2206    impl[term]=std::pair<PolySimple,int> (nf,len);
     2207  }
     2208  poly lookup(poly term, BOOLEAN& succ, int& len){
     2209    cache_map::iterator it=impl.find(term);
     2210    if (it!=impl.end()){
     2211      succ=TRUE;
     2212      //PrintS("Hit");
     2213      len=it->second.second;
     2214      return it->second.first.impl;
     2215    } else {
     2216      //PrintS("Miss");
     2217      succ=FALSE;
     2218      return NULL;
     2219    }
     2220  }
     2221  ~NoroCache(){
     2222    int s=ressources.size();
     2223    int i;
     2224    for(i=0;i<s;i++){
     2225      p_Delete(&ressources[i].impl,currRing);
     2226    }
     2227  }
     2228protected:
     2229  typedef std::vector<PolySimple> poly_vec;
     2230  poly_vec ressources;
     2231  typedef std::map<PolySimple,std::pair<PolySimple,int> > cache_map;
     2232  cache_map impl;
     2233};
     2234
     2235static poly noro_red(poly p, int &len, NoroCache* cache,slimgb_alg* c);
     2236
     2237static poly noro_red_mon(poly t, int &len, BOOLEAN& changed, NoroCache* cache,slimgb_alg* c){
     2238  BOOLEAN succ;
     2239  //wrp(t);
     2240  changed=TRUE;
     2241  poly cache_lookup=cache->lookup(t,succ, len);//don't own this yet
     2242  if (succ){
     2243    if (p_LmEqual(cache_lookup,t,c->r)){
     2244      //know they are equal
     2245      len=1;
     2246      changed=FALSE;
     2247      return t;
     2248    }
     2249    poly t_new=ppMult_nn(cache_lookup,pGetCoeff(t));
     2250    pDelete(&t);
     2251    return t_new;
     2252  } else {
     2253    unsigned long sev=p_GetShortExpVector(t,currRing);
     2254    int i=kFindDivisibleByInS_easy(c->strat,t,sev);
     2255    if (i>=0){
     2256      number coef_bak=pGetCoeff(t);
     2257      if (!(npIsOne(coef_bak))){
     2258        number coef_inverse=npInvers(coef_bak);
     2259        t=pMult_nn(t,coef_inverse);
     2260
     2261      }
     2262      assume(npIsOne(p_GetCoeff(c->strat->S[i],c->r)));
     2263      number coefstrat=p_GetCoeff(c->strat->S[i],c->r);
     2264    //assume(nIsOne(coef));
     2265     
     2266      poly t_copy_mon=p_Copy(t,c->r);
     2267
     2268     
     2269      //TODO: t=spoly(t,..)
     2270      p_ExpVectorSub(t,c->strat->S[i],c->r);
     2271      p_SetCoeff(t,npNeg(npInvers(coefstrat)),c->r);
     2272      p_Setm(t,c->r);
     2273      assume(c->strat->S[i]!=NULL);
     2274      poly t_to_del=t;
     2275      t=pp_Mult_mm(pNext(c->strat->S[i]),t,c->r);
     2276      p_Delete(&t_to_del,c->r);
     2277      len=c->strat->lenS[i]-1;
     2278      t=noro_red(t,len,cache,c);
     2279     
     2280     
     2281      cache->insert(t_copy_mon,t,pLength(t));
     2282      p_Delete(&t_copy_mon,c->r);
     2283      t=pMult_nn(t,coef_bak);
     2284      return t;
     2285    } else {
     2286      number coef_bak=p_GetCoeff(t,c->r);
     2287      number one=npInit(1);
     2288      p_SetCoeff(t,one,c->r);
     2289      len=1;
     2290      cache->insert(t,t,len);
     2291      p_SetCoeff(t,coef_bak,c->r);
     2292      return t;
     2293    }
     2294  }
     2295}
     2296//len input and out: Idea: reverse addition
     2297static poly noro_red(poly p, int &len, NoroCache* cache,slimgb_alg* c){
     2298  assume(len==pLength(p));
     2299  poly orig_p=p;
     2300  //poly* pre_buckets=(poly*) omalloc(len*sizeof(poly));
     2301  //int* pre_buckets_len=(int*) omalloc(len*sizeof(int));
     2302  //wrp(p);
     2303  if (p==NULL) {
     2304    len=0;
     2305    return NULL;
     2306  }
     2307  kBucket_pt bucket=kBucketCreate(currRing);
     2308  kBucketInit(bucket,NULL,0);
     2309  int pos=0;
     2310  poly unchanged_head=NULL;
     2311  poly unchanged_tail=NULL;
     2312  int unchanged_size=0;
     2313  while(p){
     2314    poly t=p;
     2315    pIter(p);
     2316    pNext(t)=NULL;
     2317    int l;
     2318    //poly reference=p_Head(t,c->r);
     2319    BOOLEAN changed;
     2320    t=noro_red_mon(t,l,changed,cache,c);
     2321    if (!(changed)){
     2322      unchanged_size++;
     2323      if (unchanged_head){
     2324        pNext(unchanged_tail)=t;
     2325        pIter(unchanged_tail);
     2326      } else{
     2327        unchanged_tail=t;
     2328        unchanged_head=t;
     2329      }
     2330    } else{
     2331      assume(l==pLength(t));
     2332
     2333      kBucket_Add_q(bucket,t,&l);
     2334    }
     2335    //pre_buckets[pos]=t;
     2336    //pre_buckets_len[pos++]=l;
     2337  }
     2338    //todo catch in general the length from cache
     2339
     2340  assume(pos==len);
     2341  int i;
     2342  //for(i=pos-1;i>=0;i--){
     2343  //  kBucket_Add_q(bucket,pre_buckets[i],&pre_buckets_len[i]);
     2344  //}
     2345  poly res;
     2346  len=0;
     2347  kBucket_Add_q(bucket,unchanged_head,&unchanged_size);
     2348  kBucketClear(bucket,&res,&len);
     2349  kBucketDestroy(&bucket);
     2350  //omfree(pre_buckets);
     2351  //omfree(pre_buckets_len);
     2352  return res;
     2353}
     2354#endif
     2355
    21842356static void noro_step(poly*p,int &pn,slimgb_alg* c){
    21852357  poly* reduced=(poly*) omalloc(pn*sizeof(poly));
     
    21872359  int* reduced_len=(int*) omalloc(pn*sizeof(int));
    21882360  int reduced_c=0;
    2189   if (TEST_OPT_PROT)
    2190     PrintS("reduced system:\n");
     2361  //if (TEST_OPT_PROT)
     2362  //  PrintS("reduced system:\n");
     2363#ifdef NORO_CACHE
     2364  NoroCache cache;
     2365#endif
    21912366  for(j=0;j<pn;j++){
    21922367   
    21932368    poly h=p[j];
    21942369    int h_len=pLength(h);
     2370
    21952371    number coef;
     2372#ifndef NORO_CACHE
    21962373    h=redNF2(p_Copy(h,c->r),c,h_len,coef,0);
     2374#else
     2375    h=noro_red(p_Copy(h,c->r),h_len,&cache,c);
     2376    assume(pLength(h)==h_len);
     2377#endif
    21972378    if (h!=NULL){
     2379#ifndef NORO_CACHE
     2380     
    21982381      h=redNFTail(h,c->strat->sl,c->strat,h_len);
    21992382      h_len=pLength(h);
     2383#endif
     2384     
     2385
    22002386      reduced[reduced_c]=h;
    22012387      reduced_len[reduced_c]=h_len;
     
    22342420  pn=rank;
    22352421  omfree(reduced);
     2422
    22362423  if (TEST_OPT_PROT)
    22372424    PrintS("\n");
Note: See TracChangeset for help on using the changeset viewer.