source: git/Singular/comm.cc @ 55ef0b

spielwiese
Last change on this file since 55ef0b was 55ef0b, checked in by Hans Schönemann <hannes@…>, 27 years ago
* hannes/brunko: added comm.h comm.cc git-svn-id: file:///usr/local/Singular/svn/trunk@605 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 15.1 KB
Line 
1#include "mod2.h"
2
3#include <string.h>
4//#include <sys/socket.h>
5//#include <sys/types.h>
6//#include <netinet/in.h>
7//#include <unistd.h>
8//#include <signal.h>
9#include <stdio.h>
10#include "mod2.h"
11
12#ifdef STDTRACE
13
14#include "mmemory.h"
15#include "polys.h"
16#include "febase.h"
17#include "kutil.h"
18#include "kstd1.h"
19#include "kstd2.h"
20#include "khstd.h"
21#include "spolys.h"
22#include "stairc.h"
23#include "weight.h"
24#include "cntrlc.h"
25#include "intvec.h"
26#include "ideals.h"
27#include "ipshell.h"
28#include "ipid.h"
29#include "timer.h"
30#include "tok.h"
31#include "lists.h"
32
33#include "mpsr.h"
34#include "ring.h"
35#include "maps.h"
36#include "comm.h"
37#include "timer.h"
38
39#define DEBUG 0
40
41void skstdLink::Kill()
42{
43//   PrintS("Kill\n");
44  if(DEBUG) PrintS("Kill\n");
45  if(msg!=NULL)
46    delete(msg);
47  if(links!=NULL)
48    delete(links);
49  if(res!=NULL)
50    {
51      res->~skintarray();
52      delete(res);
53    }
54   
55  Free((ADDRESS)Warteliste,(Wartelistemax)*sizeof(LObject));
56}
57
58//###################################################################
59
60void skstdLink::Init(leftv lv, int rw)
61{
62//   PrintS("Init\n");
63  si_link l;
64  leftv ptr = lv;
65  int i;
66  procnum =0;
67  for(i=0;ptr->next!=NULL;i++,procnum++)
68    ptr=ptr->next;
69  procnum++;
70
71  msg    = new intvec(procnum,1,0);
72  links  = new sklinkvec(procnum);
73  res    = new skintarray(procnum);
74
75  if(rw==0)
76    {
77      lnModus=lnRead;
78      Modus = ModRead;
79    }
80  else
81    {
82      lnModus=lnWrite;
83      Modus = ModWrite;
84    }
85
86  //++++++++++++++++++++++++++++++++++++++++++++++++++++
87  // Links kopieren
88  //++++++++++++++++++++++++++++++++++++++++++++++++++++
89  ptr = lv;
90  for(i=0; i<procnum ; i++)
91    {
92      (*links)[i] = (si_link) ptr->Data();
93      ptr=ptr->next;
94    }
95  //++++++++++++++++++++++++++++++++++++++++++++++++++++
96  // Verschiedenes initialisieren
97  //++++++++++++++++++++++++++++++++++++++++++++++++++++
98
99  Verwaltung  = FALSE;
100  Receive     = TRUE;
101  TupelL      = FALSE;
102  SPoly       = FALSE;
103  Reduzieren  = FALSE;
104  TupelStore  = FALSE;
105  TupelMelden = FALSE;
106  TupelTesten = FALSE;
107  TupelPosition = FALSE;
108  ResultSend  = FALSE;
109
110  //++++++++++++++++++++++++++++++++++++++++++++++++++++
111  // Warteliste für Tupel initialisieren
112  //++++++++++++++++++++++++++++++++++++++++++++++++++++
113  if(lnModus==lnRead)
114    {
115      Warteliste = initL();
116      Wartelistemax = 16;
117      Wartelistel=0;
118    }
119}
120
121//###################################################################
122
123void skstdLink::Store(LObject P)
124{
125//   PrintS("Store\n");
126  if(DEBUG) PrintS("Store\n");
127  enterL(&Warteliste,&Wartelistel,&Wartelistemax,P ,aktuell++);
128  P.p=NULL;
129}
130
131//###################################################################
132
133BOOLEAN skstdLink::Restore(kStrategy strat)
134{
135//   PrintS("Restore\n");
136  if(DEBUG) PrintS("Restore\n");
137 
138  int i = 0;
139  int l = 0;
140  if(aktuell>0)
141    {
142      while(i<aktuell)
143        {
144          l = strat->posInL(strat->L,strat->Ll,Warteliste[i],strat);
145          enterL(&(strat->L),&(strat->Ll),&(strat->Lmax),Warteliste[i++],l);
146        }
147     
148      aktuell=0;
149      return TRUE;
150    }
151  else
152    return FALSE;
153}
154
155//###################################################################
156
157BOOLEAN skstdLink::QueryRestore()
158{
159//   PrintS("QueryRestore\n");
160  if(DEBUG) PrintS("QueryRestore\n");
161
162 return ((aktuell>0) || (links==NULL));
163}
164
165//###################################################################
166
167void skstdLink::SendMsg(kStrategy strat, stdMsg msg,int li)
168{
169//   PrintS("SendMsg\n");
170  int i;
171  if(lnModus == lnWrite)
172    {
173      if(DEBUG) PrintS("SendMsg ");
174
175      int pos=0;
176      intvec exp(pVariables+1, 1, 0);
177      exp[0]=msg;
178      leftv lv = (leftv) Alloc0(sizeof(sleftv));
179      lv->next=NULL;
180      lv->rtyp = INTVEC_CMD;
181      switch(msg)
182        {
183
184        case MsgTupel:
185          for(i=0; i<pVariables; i++)
186            exp[i+1]=pGetExp(strat->P.p, i+1);
187          break;
188
189        case MsgTupelPosition:
190          for(i=0; i<pVariables; i++)
191            exp[i+1]=0;
192
193          // Neue Position von strat->P.p bestimmen
194          pos=-1;
195          for(i=strat->Ll-1; i>=0; i--)
196            {
197              if((strat->L[i].p1 == strat->P.p1) && (strat->L[i].p2 == strat->P.p2))
198                { 
199                  pos = i;
200                  continue;
201                }
202            }
203          exp[1]=pos;
204          break;
205
206        default:
207          for(i=0; i<pVariables; i++)
208            exp[i+1]=0;
209          break;
210        }
211      lv->data=ivCopy(&exp);
212      if(li==-1)
213        {
214          for(i=0; i< procnum; i++)
215            if((*links)[i]!=NULL)
216              slWrite((*links)[i], lv);
217        }
218      else
219        {
220          slWrite((*links)[li], lv);
221        }
222      lv->CleanUp();
223      Free(lv,sizeof(sleftv));
224      if(DEBUG) PrintS(" Ende\n");
225    }
226}
227
228//###################################################################
229
230BOOLEAN skstdLink::ReceiveMsg()
231{
232//   PrintS("ReceiveMsg\n");
233  if(lnModus==lnRead)
234    {
235      intvec *rec = NULL;
236      if(DEBUG) PrintS("ReceiveMsg\n");
237 
238      leftv lv = NULL;
239      for(int i=0; i<procnum;i++)
240        {
241          lv=slRead((*links)[i]);
242          rec=ivCopy((intvec *) lv->Data());
243          if((*res)[i]!=NULL)
244            {
245              delete (*res)[i];
246              (*res)[i]=NULL;
247            }
248          (*msg)[i] = (*rec)[0];
249          if((stdMsg)(*msg)[i]==MsgTupel)
250            {
251              (*res)[i] = new intvec(pVariables,1,0);
252              for(int j = 0; j<pVariables ; j++)
253                (*(*res)[i])[j]=(*rec)[j+1];
254            }
255        }
256        lv->CleanUp();
257        Free(lv, sizeof(sleftv));
258
259      delete rec;
260      if(DEBUG) PrintS("ReceiveMsg Ende\n");
261      return TRUE; 
262    }
263}
264
265//###################################################################
266
267void skstdLink::SetModus(stdModus NeuerModus, kStrategy strat=NULL)
268{
269 //  PrintS("SetModus\n");
270  if(DEBUG) PrintS("SetModus\n");
271  Modus=NeuerModus;
272  switch(Modus)
273    {
274    case ModRead:  // Warte auf erste Nachricht
275       Verwaltung  = FALSE;
276       Receive     = TRUE;
277       TupelL      = FALSE;
278       SPoly       = FALSE;
279       Reduzieren  = FALSE;
280       TupelMelden = FALSE;
281       TupelTesten = FALSE;
282       TupelPosition = FALSE;
283       ResultSend  = FALSE;
284       TupelStore  = FALSE;
285       break;
286    case ModWrite:
287    case ModRun:  // Berechne und sende Tupel
288      Verwaltung  = TRUE;     
289      Receive     = FALSE;   
290      TupelL      = TRUE;     
291      SPoly       = TRUE;     
292      Reduzieren  = TRUE;     
293      TupelMelden = TRUE;     
294      TupelTesten = FALSE;   
295      TupelPosition = TRUE;
296      ResultSend  = FALSE;   
297      TupelStore  = FALSE;   
298      break;
299    case ModStep:  // Warte auf MsgStep, berechne und sende
300      Verwaltung  = TRUE;     
301      Receive     = TRUE;   
302      TupelL      = TRUE;     
303      SPoly       = TRUE;     
304      Reduzieren  = TRUE;     
305      TupelMelden = TRUE;     
306      TupelPosition = TRUE;
307      TupelTesten = FALSE;   
308      ResultSend  = TRUE;   
309      TupelStore  = FALSE;   
310      break;
311    case ModCalc: // Warte auf Tupel und berechne
312      Verwaltung  = TRUE; 
313      Receive     = TRUE;   
314      TupelL      = TRUE;     
315      TupelMelden = FALSE;   
316      TupelPosition = FALSE;
317      ResultSend  = TRUE;
318      TupelStore  = (*msg)[0]==MsgTupelisZero;
319      if(TupelStore)
320        {
321          SPoly      = FALSE; 
322          Reduzieren = FALSE;
323          TupelTesten = FALSE;
324        }
325      else
326        {
327          SPoly      = TRUE; 
328          Reduzieren = TRUE; 
329          TupelTesten = TRUE;
330        }
331      break;
332    case ModPosition: // Tupel neu positionieren
333      Verwaltung  = TRUE; 
334      Receive     = TRUE;   
335      TupelL      = TRUE;     
336      TupelMelden = FALSE;   
337      TupelPosition = FALSE;
338      ResultSend  = TRUE;
339      TupelStore  = FALSE;
340      SPoly       = TRUE;
341      Reduzieren  = TRUE;
342      TupelTesten = FALSE;
343      break;
344    case ModCheck: // Nachrechnen
345      Restore(strat);
346      if(strat->Ll>=0)
347        {
348          Verwaltung  = TRUE;
349          Receive     = FALSE;
350          TupelL      = TRUE;
351          TupelStore  = FALSE;   
352          SPoly       = TRUE;
353          TupelMelden = FALSE;
354          TupelPosition = FALSE;
355          TupelTesten = FALSE;
356          ResultSend  = TRUE;
357          Reduzieren  = TRUE;
358        }
359      else
360        {
361          SetModus(ModEnde,strat);
362        }
363      break;
364    case ModEnde:  //sofort beenden
365      Verwaltung  = FALSE;
366      Receive     = FALSE;   
367      TupelWarte  = FALSE;   
368      TupelL      = FALSE;   
369      TupelStore  = FALSE;   
370      TupelTesten = FALSE;   
371      SPoly       = FALSE;   
372      Reduzieren  = FALSE;   
373      TupelMelden = FALSE;   
374      ResultSend  = FALSE;   
375      break;
376    }
377  if(DEBUG) PrintS("SetModus Ende\n");
378}
379
380//###################################################################
381
382BOOLEAN skstdLink::CheckEnd(kStrategy strat)
383{
384//   PrintS("CheckEnd\n");
385  if(DEBUG) PrintS("CheckEnd\n");
386  switch(Modus)
387    {
388    case ModRun:    // Normales beenden
389      SetModus(ModRun);
390      return strat->Ll<0;
391      break;
392    case ModStep:    // Normales beenden
393      SetModus(ModStep);
394      return strat->Ll<0;
395      break;
396    case ModCalc:   // Beenden erst dann, wenn Calc2 erreicht ist.
397      SetModus(ModCalc);
398      if(strat->Ll<0) 
399        {
400          if(BTEST1(OPT_INTERRUPT))
401            return TRUE;
402          if(TEST_OPT_PROT)
403            {
404              PrintS("\n<C>\n");
405              writeTime("used time: #");
406            }
407          SetModus(ModCheck,strat);
408        }
409        return FALSE;
410      break;
411    case ModCheck:
412      SetModus(ModCheck,strat);
413      return strat->Ll<0;
414    case ModEnde:
415      SetModus(ModEnde,strat);
416      return TRUE;
417      break;
418    case ModRead:
419      SetModus(ModCalc,strat);
420      return FALSE;
421      break;
422    case ModWrite:
423      SetModus(ModRun,strat);
424      return FALSE;
425      break;
426    }
427  return FALSE;
428}
429
430//###################################################################
431
432void skstdLink::CheckPosition(kStrategy strat, int oldLl)
433{
434//   PrintS("CheckPosition\n");
435  if(lnModus == lnWrite)
436    {
437      if(strat->Ll != oldLl )
438        {
439          SendMsg(strat, MsgTupelPosition);
440          TupelMelden = FALSE;
441        }
442    }
443 
444}
445
446//###################################################################
447
448void skstdLink::CheckHilb(kStrategy strat, int oldLl)
449{
450//   PrintS("CheckHilb\n");
451  if(strat->Ll < oldLl)
452    for(int i= oldLl - strat->Ll; i>0 ; i--)
453      SendMsg(strat, MsgTupelHilb);
454}
455
456//###################################################################
457
458void skstdLink::ParseMessage(kStrategy strat)
459{
460//   PrintS("ParseMessage\n");
461  if(DEBUG) PrintS("ParseMessage\n");
462  BOOLEAN err=FALSE; 
463  // Testen, ob alle Nachrichten korrekt sind,
464  // wenn ja, dann ParseMessageFinal
465  // sonst die richtige finden, andere Prozesse killen
466 
467  // Ist nur ein proc vorhanden, dann automatisch weiter
468  for(int i=1; i< procnum; i++) 
469    err = err || ((*msg)[i] != (*msg)[0]);
470  if(!err)
471    ParseMessageFinal(strat);
472  else
473    {
474      PrintS("Unterschiedliche Messages !\n");
475      // Mögliche Kombinationen
476      // TupelNull + Tupel      : Alle "TupelNull" löschen
477      // Unterschiedliche Tupel : In TupelDifferent alle abweichler killen
478      for(int i=0; i<procnum; i++)
479        if(((stdMsg)(*msg)[i]) == MsgTupelisZero)
480          {
481            PrintS(" AAA ");
482            KillChild(strat,i);
483          }
484      ParseMessageFinal(strat);
485    }
486  if(DEBUG) PrintS("ParseMessage Ende\n");
487}
488
489//###################################################################
490
491void skstdLink::ParseMessageFinal(kStrategy strat, int i)
492{
493//   PrintS("ParseMessageFinal\n");
494  if(DEBUG) PrintS("ParseMessageFinal\n");
495 
496  switch(((stdMsg) (*msg)[i]))
497    {
498    case MsgTupel:  // In Step-Modus schalten bzw. bleiben.     
499    case MsgTupelisZero:
500
501       if(procnum>0)
502        {
503          SetModus(ModCalc,strat);
504        }
505       else
506        {
507          SetModus(ModCheck,strat);
508        }
509      break;
510    case MsgTupelPosition:
511      // aktuelles Tupel neu positionieren
512      if(TEST_OPT_PROT)
513        PrintS("P");
514      SetModus(ModPosition);
515      break;
516     case MsgEnd:  // In Calc1-Modus schalten bzw. bleiben.     
517       SetModus(ModCheck,strat);
518       if (TEST_OPT_PROT) PrintS("\n<E>");
519       break;
520    default:
521      break;
522    }
523  if(DEBUG) PrintS("ParseMessageFinal Ende\n");
524}
525
526//###################################################################
527
528void skstdLink::TupelDifferent(kStrategy strat)
529{
530//   PrintS("TupelDifferen\n");
531  if(DEBUG) PrintS("TupelDifferent\n");
532  BOOLEAN Different=FALSE ;
533  for(int i=procnum-1; i>=0; i--)
534    {
535      if(strat->P.p==NULL)
536        {
537          PrintS(" BBB ");
538          KillChild(strat,i);
539        }
540      else
541        {
542          if((*res)[i]==NULL)
543            {
544              PrintS(" CCC ");
545              KillChild(strat,i);
546            }
547          else
548            {
549              Different = FALSE;
550              for(int j=0; j< pVariables; j++)
551                {
552                  Different = Different || ((*(*res)[i])[j]!=pGetExp(strat->P.p,j+1));
553                }
554              if(Different)
555                {
556                  PrintS(" DDD ");
557                  KillChild(strat,i);
558                }
559            }
560        }
561    }
562  if(procnum<=0)
563    SetModus(ModCheck,strat);
564}
565
566//###################################################################
567
568void skstdLink::SendTupel(kStrategy strat)
569{
570//   PrintS("SendTupel\n");
571  if(DEBUG) PrintS("SendTupel\n");
572  if(strat->P.p==NULL)
573    {
574      SendMsg(strat,MsgTupelisZero);
575    }
576  else
577      SendMsg(strat,MsgTupel);
578}
579
580//###################################################################
581
582void skstdLink::KillChild(kStrategy strat, int li,BOOLEAN Cancled)
583{
584  //    PrintS("KillChild\n");
585  if(DEBUG) PrintS("KillChild\n");
586  if(li>=0)
587     {
588       if(procnum>=2)
589        {
590          si_link swaplink = (*links)[li];
591          intvec *swapres = (*res)[li];
592          (*msg)[li]   = (*msg)[procnum-1];
593          (*links)[li] = (*links)[procnum-1];
594          (*res)[li]   = (*res)[procnum-1];
595          (*links)[procnum-1] = swaplink;
596          (*res)[procnum-1]   = swapres;
597        }
598        else
599          {
600            if(TEST_OPT_PROT)
601              {
602                PrintS("\n<K>\n");
603                writeTime("used time: #");
604              }
605            SetModus(ModCheck,strat);
606          }
607       procnum--;
608     }
609   else
610     {
611       for(int i=procnum - 1 ; i>=0 ; i--)
612        KillChild(strat,i,Cancled);
613     }
614 }
615
616//###################################################################
617
618void skstdLink::Start(kStrategy strat)
619{
620    if(lnModus==lnRead)
621      {
622        SetModus(ModCalc);
623      }
624    else
625      {
626        SetModus(ModRun  );
627      }
628}
629
630//###################################################################
631
632void skstdLink::End(kStrategy strat)
633{
634  if(DEBUG) PrintS("End\n");
635}
636
637//###################################################################
638
639lists skstdLink::RestTupel()
640{
641  int i=0;
642  lists l=(lists)Alloc(sizeof(slists));
643  ideal I;
644  if(aktuell>0)
645    {
646      l->Init(aktuell);
647      for(i=0; i<aktuell; i++)
648        {
649          I=idInit(2,1);
650          l->m[i].rtyp = IDEAL_CMD;
651          if(Warteliste[i].p1==NULL)
652            I->m[0]=NULL;
653          else
654            I->m[0]=pCopy(Warteliste[i].p1);
655          if(Warteliste[i].p2==NULL)
656            I->m[1]=NULL;
657          else
658            I->m[1]=pCopy(Warteliste[i].p2);
659          l->m[i].data = (void *) idCopy(I);
660          idDelete(&I);
661        }
662      return l;
663    }
664  else
665    {
666      l->Init(0);
667      return l;
668    }
669 
670}
671
672//###################################################################
673
674void skstdLink::DispMsg(stdMsg msg)
675{
676    {
677//       Print("r(%.5i) Msg =",-1);
678      switch(msg) {
679      case MsgTupelisZero:  // In Step-Modus schalten bzw. bleiben.     
680        PrintS("MsgTupelisZero ");
681        break;
682      case MsgTupel:  // In Step-Modus schalten bzw. bleiben.     
683        PrintS("MsgTupel       ");
684        break;
685      case MsgEnd:
686        PrintS("MsgEnd         ");
687        break;
688      }
689    }
690}
691 
692void skstdLink::DispMod(int i)
693{
694  if(i==-1 )
695    {
696      Print("r(%.5i) Mod = ",-1);
697     
698      switch(Modus) {
699      case ModCheck:
700        PrintS("Check ");
701        break;
702      case ModRun:
703        PrintS("Run   ");
704        break;
705      case ModStep:
706        PrintS("Step  ");
707        break;
708      case ModCalc:
709        PrintS("Calc  ");
710        break;
711      case ModRead:
712        PrintS("Read ");
713        break;
714      case ModWrite:
715        PrintS("Write  ");
716        break;
717      }
718      PrintS("\n");
719    }
720}
721#endif
Note: See TracBrowser for help on using the repository browser.