65int PF_RawSend(
int dest,
void *buf, LONG l,
int tag);
66LONG
PF_RawRecv(
int *src,
void *buf,LONG thesize,
int *tag);
68int PF_RawIsend(
int dest,
const void *buf,
int count, MPI_Datatype type,
int tag, MPI_Request *request);
69int PF_RawWaitAll(
int count, MPI_Request *request, MPI_Status *status);
74static int PF_WaitAllSlaves(
void);
76static void PF_PackRedefinedPreVars(
void);
77static void PF_UnpackRedefinedPreVars(
void);
79static int PF_Wait4MasterIP(
int tag);
80static int PF_DoOneExpr(
void);
81static int PF_ReadMaster(
void);
82static int PF_Slave2MasterIP(
int src);
83static int PF_Master2SlaveIP(
int dest,
EXPRESSIONS e);
84static int PF_WalkThrough(WORD *t, LONG l, LONG chunk, LONG *count);
85static int PF_SendChunkIP(
FILEHANDLE *curfile,
POSITION *position,
int to, LONG thesize);
86static int PF_RecvChunkIP(
FILEHANDLE *curfile,
int from, LONG thesize);
88static void PF_ReceiveErrorMessage(
int src,
int tag);
89static void PF_CatchErrorMessages(
int *src,
int *tag);
90static void PF_CatchErrorMessagesForAll(
void);
91static int PF_ProbeWithCatchingErrorMessages(
int *src);
93static void PF_RaiseRuntimeError(
void);
94static void PF_BroadcastRuntimeError(
void);
95static void PF_PostEndSortBarrier(
void);
101static int PF_processing;
102static LONG PF_goutterms;
110#ifdef PF_WITH_SCHED_YIELD
115 #define PRINTFBUF(TEXT,TERM,SIZE) { UBYTE lbuf[24]; if(PF.log){ WORD iii;\
116 NumToStr(lbuf,AC.CModule); \
117 fprintf(stderr,"[%d|%s] %s : ",PF.me,lbuf,(char*)TEXT);\
118 if(TERM){ fprintf(stderr,"[%d] ",(int)(*TERM));\
119 if((SIZE)<500 && (SIZE)>0) for(iii=1;iii<(SIZE);iii++)\
120 fprintf(stderr,"%d ",TERM[iii]); }\
121 fprintf(stderr,"\n");\
124 #define PRINTFBUF(TEXT,TERM,SIZE) {}
133 char swap_tmp__[sizeof(x) == sizeof(y) ? (int)sizeof(x) : -1]; \
134 memcpy(swap_tmp__, &y, sizeof(x)); \
135 memcpy(&y, &x, sizeof(x)); \
136 memcpy(&x, swap_tmp__, sizeof(x)); \
142#define PACK_LONG(p, n) \
144 *(p)++ = (UWORD)((ULONG)(n) & (ULONG)WORDMASK); \
145 *(p)++ = (UWORD)(((ULONG)(n) >> BITSINWORD) & (ULONG)WORDMASK); \
151#define UNPACK_LONG(p, n) \
153 (n) = (LONG)((((ULONG)(p)[1] & (ULONG)WORDMASK) << BITSINWORD) | ((ULONG)(p)[0] & (ULONG)WORDMASK)); \
160#define CHECK(condition) _CHECK(condition, __FILE__, __LINE__)
161#define _CHECK(condition, file, line) __CHECK(condition, file, line)
162#define __CHECK(condition, file, line) \
164 if ( !(condition) ) { \
165 Error0("Fatal error at " file ":" #line); \
173#define DBGOUT(lv1, lv2, a) do { if ( lv1 >= lv2 ) { printf a; fflush(stdout); } } while (0)
176#define DBGOUT_NINTERMS(lv, a)
184static LONG PF_linterms;
185#define PF_STATS_SIZE 5
186static LONG **PF_stats = NULL;
187static LONG PF_laststat;
188static LONG PF_statsinterval;
202static int PF_Statistics(LONG **stats,
int proc)
209 if ( AT.SS == AM.S0 && PF.me == MASTER ) {
210 real =
PF_RealTime(PF_TIME); rpart = (WORD)(real%100); real /= 100;
212 if ( PF_stats == NULL ) {
213 PF_stats = (LONG**)Malloc1(PF.numtasks*
sizeof(LONG*),
"PF_stats 1");
214 for ( i = 0; i < PF.numtasks; i++ ) {
215 PF_stats[i] = (LONG*)Malloc1(PF_STATS_SIZE*
sizeof(LONG),
"PF_stats 2");
216 for ( j = 0; j < PF_STATS_SIZE; j++ ) PF_stats[i][j] = 0;
219 if ( proc > 0 )
for ( i = 0; i < PF_STATS_SIZE; i++ ) PF_stats[proc][i] = stats[0][i];
221 if ( real >= PF_laststat + PF_statsinterval || proc == 0 ) {
222 LONG sum[PF_STATS_SIZE];
224 for ( i = 0; i < PF_STATS_SIZE; i++ ) sum[i] = 0;
226 cpart = (WORD)(cpu%1000);
229 if ( AC.OldParallelStats ) MesPrint(
"");
230 if ( proc > 0 && AC.StatsFlag && AC.OldParallelStats ) {
231 MesPrint(
"proc CPU in gen left byte");
232 MesPrint(
"%3d : %7l.%2i %10l",0,cpu,cpart,AN.ninterms);
234 else if ( AC.StatsFlag && AC.OldParallelStats ) {
235 MesPrint(
"proc CPU in gen out byte");
236 MesPrint(
"%3d : %7l.%2i %10l %10l %10l",0,cpu,cpart,AN.ninterms,0,PF_goutterms);
239 for ( i = 1; i < PF.numtasks; i++ ) {
240 cpart = (WORD)(PF_stats[i][0]%1000);
241 cpu = PF_stats[i][0] / 1000;
243 if ( AC.StatsFlag && AC.OldParallelStats )
244 MesPrint(
"%3d : %7l.%2i %10l %10l %10l",i,cpu,cpart,
245 PF_stats[i][2],PF_stats[i][3],PF_stats[i][4]);
246 for ( j = 0; j < PF_STATS_SIZE; j++ ) sum[j] += PF_stats[i][j];
248 cpart = (WORD)(sum[0]%1000);
251 if ( AC.StatsFlag && AC.OldParallelStats ) {
252 MesPrint(
"Sum = %7l.%2i %10l %10l %10l",cpu,cpart,sum[2],sum[3],sum[4]);
253 MesPrint(
"Real = %7l.%2i %20s (%l) %16s",
254 real,rpart,AC.Commercial,AC.CModule,EXPRNAME(AR.CurExpr));
286static WORD **PF_term;
287static WORD **PF_newcpos;
288static WORD *PF_newclen;
294static WORD *PF_WorkSpace;
295static UWORD *PF_ScratchSpace;
318static PF_BUFFER *PF_AllocBuf(
int nbufs, LONG bsize, WORD free)
326 (LONG)(
sizeof(
PF_BUFFER) + 4*nbufs*
sizeof(WORD*) + (nbufs-free)*bsize);
329 (LONG)( nbufs * ( 2 *
sizeof(MPI_Status)
330 +
sizeof(MPI_Request)
331 +
sizeof(MPI_Datatype)
333 allocsize += (LONG)( nbufs * 3 *
sizeof(
int) );
335 if ( ( buf = (
PF_BUFFER*)Malloc1(allocsize,
"PF_AllocBuf") ) == NULL )
return(NULL);
338 stop = ((UBYTE *)buf) + allocsize;
340 buf->numbufs = nbufs;
343 buf->buff = (WORD**)p; p += buf->numbufs*
sizeof(WORD*);
344 buf->fill = (WORD**)p; p += buf->numbufs*
sizeof(WORD*);
345 buf->full = (WORD**)p; p += buf->numbufs*
sizeof(WORD*);
346 buf->stop = (WORD**)p; p += buf->numbufs*
sizeof(WORD*);
347 buf->status = (MPI_Status *)p; p += buf->numbufs*
sizeof(MPI_Status);
348 buf->retstat = (MPI_Status *)p; p += buf->numbufs*
sizeof(MPI_Status);
349 buf->request = (MPI_Request *)p; p += buf->numbufs*
sizeof(MPI_Request);
350 buf->type = (MPI_Datatype *)p; p += buf->numbufs*
sizeof(MPI_Datatype);
351 buf->index = (
int *)p; p += buf->numbufs*
sizeof(int);
353 for ( i = 0; i < buf->numbufs; i++ ) buf->request[i] = MPI_REQUEST_NULL;
354 buf->tag = (
int *)p; p += buf->numbufs*
sizeof(int);
355 buf->from = (
int *)p; p += buf->numbufs*
sizeof(int);
359 for ( i = free; i < buf->numbufs; i++ ) {
360 buf->buff[i] = (WORD*)p; p += bsize;
361 buf->stop[i] = (WORD*)p;
362 buf->fill[i] = buf->full[i] = buf->buff[i];
365 MesPrint(
"Error in PF_AllocBuf p = %x stop = %x\n",p,stop);
387static int PF_InitTree(
void)
392 int numrbufs,numtasks = PF.numtasks;
393 int i, j, src, numnodes;
394 int numslaves = numtasks - 1;
400 if ( PF_term == NULL ) {
401 size = 2*numtasks*
sizeof(WORD*) +
sizeof(WORD)*
402 ( numtasks*(1 + AM.MaxTal) + (AM.MaxTer/
sizeof(WORD)+1) + 2*(AM.MaxTal+2));
404 PF_term = (WORD **)Malloc1(size,
"PF_term");
405 stop = ((UBYTE*)PF_term) + size;
406 p = ((UBYTE*)PF_term) + numtasks*
sizeof(WORD*);
408 PF_newcpos = (WORD **)p; p +=
sizeof(WORD*) * numtasks;
409 PF_newclen = (WORD *)p; p +=
sizeof(WORD) * numtasks;
410 for ( i = 0; i < numtasks; i++ ) {
411 PF_newcpos[i] = (WORD *)p; p +=
sizeof(WORD)*AM.MaxTal;
414 PF_WorkSpace = (WORD *)p; p += AM.MaxTer+
sizeof(WORD);
415 PF_ScratchSpace = (UWORD*)p; p += 2*(AM.MaxTal+2)*
sizeof(UWORD);
417 if ( p != stop ) { MesPrint(
"error in PF_InitTree");
return(-1); }
423 numrbufs = PF.numrbufs;
427 size = (AT.SS->sTop2 - AT.SS->lBuffer - 1)/(PF.numtasks - 1);
429 if ( rbuf == NULL ) {
430 if ( ( rbuf = (
PF_BUFFER**)Malloc1(numtasks*
sizeof(
PF_BUFFER*),
"Master: rbufs") ) == NULL )
return(-1);
431 if ( (rbuf[0] = PF_AllocBuf(1,0,1) ) == NULL )
return(-1);
432 for ( i = 1; i < numtasks; i++ ) {
433 if (!(rbuf[i] = PF_AllocBuf(numrbufs,
sizeof(WORD)*size,1)))
return(-1);
436 rbuf[0]->buff[0] = AT.SS->lBuffer;
437 rbuf[0]->full[0] = rbuf[0]->fill[0] = rbuf[0]->buff[0];
438 rbuf[0]->stop[0] = rbuf[1]->buff[0] = rbuf[0]->buff[0] + 1;
439 rbuf[1]->full[0] = rbuf[1]->fill[0] = rbuf[1]->buff[0];
440 for ( i = 2; i < numtasks; i++ ) {
441 rbuf[i-1]->stop[0] = rbuf[i]->buff[0] = rbuf[i-1]->buff[0] + size;
442 rbuf[i]->full[0] = rbuf[i]->fill[0] = rbuf[i]->buff[0];
444 rbuf[numtasks-1]->stop[0] = rbuf[numtasks-1]->buff[0] + size;
446 for ( i = 1; i < numtasks; i++ ) {
447 for ( j = 0; j < rbuf[i]->numbufs; j++ ) {
448 rbuf[i]->full[j] = rbuf[i]->fill[j] = rbuf[i]->buff[j] + AM.MaxTer/
sizeof(WORD) + 2;
450 PF_term[i] = rbuf[i]->fill[rbuf[i]->active];
455 PF_term[0] = rbuf[0]->buff[0];
464 if ( numslaves < 3 ) numnodes = 1;
467 while ( numnodes < numslaves ) numnodes *= 2;
471 if ( PF_root == NULL )
472 if ( ( PF_root = (
NODE*)Malloc1(
sizeof(
NODE)*numnodes,
"nodes in mergetree") ) == NULL )
478 for ( i = 0; i < numnodes; i++ ) {
479 if ( 2*(i+1) <= numnodes ) {
480 PF_root[i].left = &(PF_root[2*(i+1)-1]);
485 if ( src < numtasks ) PF_root[i].lsrc = src++;
486 else PF_root[i].lsrc = 0;
488 PF_root[i].lloser = 0;
490 for ( i = 0; i < numnodes; i++ ) {
491 if ( 2*(i+1)+1 <= numnodes ) {
492 PF_root[i].rght = &(PF_root[2*(i+1)]);
497 if (src<numtasks) PF_root[i].rsrc = src++;
498 else PF_root[i].rsrc = 0;
500 PF_root[i].rloser = 0;
531static WORD *PF_PutIn(
int src)
538 int a = rbuf->active;
539 int next = a+1 >= rbuf->numbufs ? 0 : a+1 ;
540 WORD *lastterm = PF_term[src];
541 WORD *term = rbuf->fill[a];
543 if ( src <= 0 )
return(PF_term[0]);
545 if ( rbuf->full[a] == rbuf->buff[a] + AM.MaxTer/
sizeof(WORD) + 2 ) {
550 if ( tag == PF_RUNTIME_ERROR_MSGTAG ) {
553 rbuf->full[a] += size;
554 if ( tag == PF_ENDBUFFER_MSGTAG ) *rbuf->full[a]++ = 0;
555 else if ( rbuf->numbufs > 1 ) {
559 rbuf->full[next] = rbuf->buff[next] + AM.MaxTer/
sizeof(WORD) + 2;
560 size = (LONG)(rbuf->stop[next] - rbuf->full[next]);
564 if ( *term == 0 && term != rbuf->full[a] )
return(PF_term[0]);
568 if ( term + *term > rbuf->full[a] || term + 1 >= rbuf->full[a] ) {
570 m1 = rbuf->buff[next] + AM.MaxTer/
sizeof(WORD) + 1;
571 if ( *term < 0 || term == rbuf->full[a] ) {
575 m2 = rbuf->full[a] - 1;
576 while ( m2 >= term ) *m1-- = *m2--;
577 rbuf->fill[next] = term = m1 + 1;
578 m2 = lastterm + *lastterm - 1;
579 while ( m2 >= lastterm ) *m1-- = *m2--;
586 m2 = rbuf->full[a] - 1;
587 while ( m2 >= term ) *m1-- = *m2--;
588 rbuf->fill[next] = term = m1 + 1;
590 if ( rbuf->numbufs == 1 ) {
591 rbuf->full[a] = rbuf->buff[a] + AM.MaxTer/
sizeof(WORD) + 2;
592 size = (LONG)(rbuf->stop[a] - rbuf->full[a]);
598 rbuf->full[next] = rbuf->buff[next] + AM.MaxTer/
sizeof(WORD) + 2;
600 rbuf->full[next] += size;
601 if ( tag == PF_ENDBUFFER_MSGTAG ) {
602 *rbuf->full[next]++ = 0;
604 else if ( rbuf->numbufs > 1 ) {
608 rbuf->full[a] = rbuf->buff[a] + AM.MaxTer/
sizeof(WORD) + 2;
609 size = (LONG)(rbuf->stop[a] - rbuf->full[a]);
615 a = rbuf->active = next;
623 r = term[1] - im + 1;
625 m2 = lastterm - im + 1;
626 while ( ++im <= 0 ) *--m1 = *--m2;
628 rbuf->fill[a] = term = m1;
629 if ( term + *term > rbuf->full[a] )
goto newterms;
631 rbuf->fill[a] += *term;
658static int PF_GetLoser(
NODE *n)
663 if ( PF_loser == 0 ) {
667 if ( n->left ) n->lloser = PF_GetLoser(n->left);
670 if ( *(PF_term[n->lsrc] = PF_PutIn(n->lsrc)) == 0) n->lloser = 0;
673 if ( n->rght ) n->rloser = PF_GetLoser(n->rght);
676 if ( *(PF_term[n->rsrc] = PF_PutIn(n->rsrc)) == 0 ) n->rloser = 0;
680 else if ( PF_loser == n->lloser ) {
681 if ( n->left ) n->lloser = PF_GetLoser(n->left);
684 if ( *(PF_term[n->lsrc] = PF_PutIn(n->lsrc)) == 0 ) n->lloser = 0;
687 else if ( PF_loser == n->rloser ) {
689 if ( n->rght ) n->rloser = PF_GetLoser(n->rght);
692 if ( *(PF_term[n->rsrc] = PF_PutIn(n->rsrc)) == 0 ) n->rloser = 0;
695 if ( n->lloser > 0 && n->rloser > 0 ) {
696 comp = CompareTerms(BHEAD PF_term[n->lloser],PF_term[n->rloser],(WORD)0);
697 if ( comp > 0 )
return(n->lloser);
698 else if (comp < 0 )
return(n->rloser);
705 WORD lclen, rclen, newclen, newnlen;
714 WORD *ml = PF_term[n->lloser];
715 WORD *mr = PF_term[n->rloser];
717 if ( ( r1 = (
int)*PF_term[n->lloser] ) <= 0 ) r1 = 20;
718 if ( ( r2 = (
int)*PF_term[n->rloser] ) <= 0 ) r2 = 20;
722 if ( S->PolyFlag == 2 ) {
724 if ( *tt1 + w[1] - ml[1] > AM.MaxTer/((LONG)
sizeof(WORD)) ) {
725 MesPrint(
"Term too complex in PolyRatFun addition. MaxTermSize of %10l is too small",AM.MaxTer);
732 if ( w + ml[1] + mr[1] > AT.WorkTop ) {
733 MesPrint(
"A WorkSpace of %10l is too small",AM.WorkSize);
739 if ( r1 <= FUNHEAD || ( w[FUNHEAD] == -SNUMBER && w[FUNHEAD+1] == 0 ) ) {
745 else if ( r1 < ml[1] ) {
749 while ( --r1 >= 0 ) *--ml = *--mr;
752 while ( --r1 >= 0 ) *--ml = *--mr;
754 PF_term[n->lloser] = ml;
758 if ( r2 > 2*AM.MaxTal )
759 MesPrint(
"warning: new term in polyfun is large");
764 PF_term[n->lloser] = mr;
769 PF_newclen[n->rloser] = 0;
770 PF_loser = n->rloser;
776 if ( ( lclen = PF_newclen[n->lloser] ) != 0 ) lcpos = PF_newcpos[n->lloser];
778 lcpos = PF_term[n->lloser];
779 lclen = *(lcpos += *lcpos - 1);
780 lcpos -= ABS(lclen) - 1;
782 if ( ( rclen = PF_newclen[n->rloser] ) != 0 ) rcpos = PF_newcpos[n->rloser];
784 rcpos = PF_term[n->rloser];
785 rclen = *(rcpos += *rcpos - 1);
786 rcpos -= ABS(rclen) -1;
788 lclen = ( (lclen > 0) ? (lclen-1) : (lclen+1) ) >> 1;
789 rclen = ( (rclen > 0) ? (rclen-1) : (rclen+1) ) >> 1;
790 newcpos = PF_ScratchSpace;
791 if ( AddRat(BHEAD (UWORD *)lcpos,lclen,(UWORD *)rcpos,rclen,newcpos,&newnlen) )
return(-1);
792 if ( AN.ncmod != 0 ) {
793 if ( ( AC.modmode & POSNEG ) != 0 ) {
796 if ( BigLong(newcpos,newnlen,(UWORD *)AC.cmod,ABS(AN.ncmod)) >=0 ) {
798 SubPLon(newcpos,newnlen,(UWORD *)AC.cmod,ABS(AN.ncmod),newcpos,&newnlen);
799 newcpos[newnlen] = 1;
800 for ( ii = 1; ii < newnlen; ii++ ) newcpos[newnlen+ii] = 0;
803 if ( newnlen == 0 ) {
808 PF_loser = n->lloser;
809 PF_newclen[n->lloser] = 0;
810 if ( n->left ) n->lloser = PF_GetLoser(n->left);
813 if ( *(PF_term[n->lsrc] = PF_PutIn(n->lsrc)) == 0 ) n->lloser = 0;
815 PF_loser = n->rloser;
816 PF_newclen[n->rloser] = 0;
824 newclen = ( newnlen > 0 ) ? ( newnlen + 1 ) : ( newnlen - 1 );
825 if ( newnlen < 0 ) newnlen = -newnlen;
826 PF_newclen[n->lloser] = newclen;
827 lcpos = PF_newcpos[n->lloser];
828 if ( newclen < 0 ) newclen = -newclen;
829 while ( newclen-- ) *lcpos++ = *newcpos++;
830 PF_loser = n->rloser;
831 PF_newclen[n->rloser] = 0;
839 if (n->lloser > 0)
return(n->lloser);
840 if (n->rloser > 0)
return(n->rloser);
881 LONG size, noutterms;
886 if ( AT.SS != AT.S0 || !PF.parallel )
return 0;
888 if ( PF.me != MASTER ) {
896 size = (S->sTop2 - S->lBuffer - 1)/(PF.numtasks - 1);
897 size -= (AM.MaxTer/
sizeof(WORD) + 2);
898 if ( fout->POsize < (LONG)(size*
sizeof(WORD)) ) size = fout->POsize/
sizeof(WORD);
899 if ( sbuf == NULL ) {
900 if ( (sbuf = PF_AllocBuf(PF.numsbufs, size*
sizeof(WORD), 1)) == NULL )
return -1;
904 sbuf->buff[0] = fout->PObuffer;
905 sbuf->stop[0] = fout->PObuffer+size;
906 if ( sbuf->stop[0] > fout->POstop )
return -1;
907 for ( i = 0; i < PF.numsbufs; i++ )
908 sbuf->fill[i] = sbuf->full[i] = sbuf->buff[i];
910 fout->PObuffer = sbuf->buff[sbuf->active];
911 fout->POstop = sbuf->stop[sbuf->active];
912 fout->POsize = size*
sizeof(WORD);
913 fout->POfill = fout->POfull = fout->PObuffer;
929 if ( AR.PolyFun == 0 ) { S->PolyFlag = 0; }
930 else if ( AR.PolyFunType == 1 ) { S->PolyFlag = 1; }
931 else if ( AR.PolyFunType == 2 ) {
932 if ( AR.PolyFunExp == 2
933 || AR.PolyFunExp == 3 ) S->PolyFlag = 1;
934 else S->PolyFlag = 2;
936 *AR.CompressPointer = 0;
937 SeekScratch(fout, &position);
938 oldposition = position;
939 oldgzipCompress = AR.gzipCompress;
944 while ( PF_loser >= 0 ) {
945 if ( (PF_loser = PF_GetLoser(PF_root)) == 0 )
break;
946 outterm = PF_term[PF_loser];
949 if ( PF_newclen[PF_loser] != 0 ) {
953 outterm = PF_WorkSpace;
954 pp = PF_term[PF_loser];
956 while ( cc-- ) *outterm++ = *pp++;
957 outterm = (outterm[-1] > 0) ? outterm-outterm[-1] : outterm+outterm[-1];
958 if ( PF_newclen[PF_loser] > 0 ) cc = (WORD)PF_newclen[PF_loser] - 1;
959 else cc = -(WORD)PF_newclen[PF_loser] - 1;
960 pp = PF_newcpos[PF_loser];
961 while ( cc-- ) *outterm++ = *pp++;
962 *outterm++ = PF_newclen[PF_loser];
963 *PF_WorkSpace = outterm - PF_WorkSpace;
964 outterm = PF_WorkSpace;
965 *PF_newcpos[PF_loser] = 0;
966 PF_newclen[PF_loser] = 0;
971 PRINTFBUF(
"PF_EndSort to PutOut: ",outterm,*outterm);
972 PutOut(BHEAD outterm,&position,fout,1);
975 AR.gzipCompress = oldgzipCompress;
978 S->TermsLeft = PF_goutterms = noutterms;
979 DIFPOS(PF_exprsize, position, oldposition);
980 AR.gzipCompress = oldgzipCompress;
991static WORD *PF_CurrentBracket;
1016static WORD PF_GetTerm(WORD *term)
1018 assert(PF.me != MASTER);
1020 FILEHANDLE *fi = AC.RhsExprInModuleFlag && PF.rhsInParallel ? &PF.slavebuf : AR.infile;
1022 WORD *next, *np, *last, *lp = 0, *nextstop, *tp=term;
1024 AN.deferskipped = 0;
1025 if ( fi->POfill >= fi->POfull || fi->POfull == fi->PObuffer ) {
1031 int src = MASTER, tag;
1033 LONG size,cpu,space = 0;
1036 fprintf(stderr,
"[%d] Starting to send to Master\n",PF.me);
1044 PF_Pack(&PF_linterms ,1,PF_LONG);
1045 PF_Pack(&(AM.S0->GenTerms) ,1,PF_LONG);
1046 PF_Pack(&(AM.S0->TermsLeft),1,PF_LONG);
1050 fprintf(stderr,
"[%d] Now sending with tag = %d\n",PF.me,PF_READY_MSGTAG);
1054 PF_Send(MASTER, PF_READY_MSGTAG);
1057 fprintf(stderr,
"[%d] returning from send\n",PF.me);
1061 size = fi->POstop - fi->PObuffer - 1;
1064 fi->POfill = fi->PObuffer;
1069 if ( fi->POfill < fi->POfull ) {
1070 DBGOUT_NINTERMS(2, (
"PF.me=%d AN.ninterms=%d PF_linterms=%d ninterms=%d GET\n", (
int)PF.me, (
int)AN.ninterms, (
int)PF_linterms, (
int)ninterms));
1071 AN.ninterms = ninterms - 1;
1073 DBGOUT_NINTERMS(2, (
"PF.me=%d AN.ninterms=%d PF_linterms=%d ninterms=%d GETEND\n", (
int)PF.me, (
int)AN.ninterms, (
int)PF_linterms, (
int)ninterms));
1076 fi->POfull = fi->PObuffer + size;
1077 if ( tag == PF_ENDSORT_MSGTAG ) *fi->POfull++ = 0;
1082 if ( PF_CurrentBracket ) *PF_CurrentBracket = 0;
1084 if ( *fi->POfill == 0 ) {
1085 fi->POfill = fi->POfull = fi->PObuffer;
1089 if ( AR.DeferFlag ) {
1090 if ( !PF_CurrentBracket ) {
1095 (WORD*)Malloc1(AM.MaxTer,
"PF_CurrentBracket");
1096 *PF_CurrentBracket = 0;
1101 while ( *PF_CurrentBracket ) {
1106 nextstop = next + *next; nextstop -= ABS(nextstop[-1]);
1108 last = PF_CurrentBracket+1;
1109 while ( next < nextstop ) {
1113 if ( *last == HAAKJE && *next == HAAKJE ) {
1117 PRINTFBUF(
"PF_GetTerm skips",fi->POfill,*fi->POfill);
1123 np = next; next += next[1];
1124 lp = last; last += last[1];
1125 while ( np < next )
if ( *lp++ != *np++ )
goto strip;
1130 fi->POfill += *fi->POfill;
1135 if ( fi->POfill >= fi->POfull || fi->POfull == fi->PObuffer )
1137 if ( *fi->POfill == 0 ) {
1138 fi->POfill = fi->POfull = fi->PObuffer;
1154 nextstop = next + *next; nextstop -= ABS(nextstop[-1]);
1157 lp = PF_CurrentBracket + 1;
1158 while ( next < nextstop ) {
1159 if ( *next == HAAKJE ) {
1160 fi->POfill += *fi->POfill;
1161 while ( next < fi->POfill ) *lp++ = *next++;
1162 *PF_CurrentBracket = lp - PF_CurrentBracket;
1167 *term = WORDDIF(tp,term);
1168 PRINTFBUF(
"PF_GetTerm new brack",PF_CurrentBracket,*PF_CurrentBracket);
1169 PRINTFBUF(
"PF_GetTerm POfill",fi->POfill,*fi->POfill);
1172 np = next; next += next[1];
1173 while ( np < next ) *tp++ = *lp++ = *np++;
1182 while ( i-- ) *tp++ = *fi->POfill++;
1184 PRINTFBUF(
"PF_GetTerm returns",term,*term);
1206 FILEHANDLE *fi = AC.RhsExprInModuleFlag && PF.rhsInParallel ? &PF.slavebuf : AR.infile;
1207 WORD *next = fi->POfill;
1208 WORD *termout = AT.WorkPointer;
1209 WORD *oldwork = AT.WorkPointer;
1211 AT.WorkPointer = (WORD *)((UBYTE *)(AT.WorkPointer) + AM.MaxTer);
1214 PRINTFBUF(
"PF_Deferred (Term) ",term,*term);
1215 PRINTFBUF(
"PF_Deferred (Bracket)",PF_CurrentBracket,*PF_CurrentBracket);
1217 bra = bstop = PF_CurrentBracket;
1220 bstop -= ABS(bstop[-1]);
1223 while ( *bra != HAAKJE && bra < bstop ) bra += bra[1];
1224 if ( bra >= bstop ) {
1225 AT.WorkPointer = term + *term;
1226 if (
Generator(BHEAD term,level) )
goto DefCall;
1228 AT.WorkPointer = oldwork;
1232 tstart = bra + bra[1];
1233 bra = PF_CurrentBracket;
1235 *tstart = bra + *bra - tstart;
1244 if (
InsertTerm(BHEAD term,0,AM.rbufnum,tstart,termout,0) < 0 ) {
1250 AT.WorkPointer = termout + *termout;
1251 if (
Generator(BHEAD termout,level) )
goto DefCall;
1252 AT.WorkPointer = termout;
1254 if ( tstart >= fi->POfull )
goto ThatsIt;
1259 while ( bra <= bstop ) {
1260 if ( *bra != *tstart )
goto ThatsIt;
1267 if ( *bra != HAAKJE || *tstart != HAAKJE )
goto ThatsIt;
1268 tstart += tstart[1];
1270 *tstart = next - tstart;
1271 bra = PF_CurrentBracket + 1;
1281 MesCall(
"PF_Deferred");
1290static LONG **PF_W4Sstats = 0;
1298static int PF_Wait4Slave(
int src)
1302 tag = PF_ANY_MSGTAG;
1303 PF_CatchErrorMessages(&src, &tag);
1306 if ( tag != PF_READY_MSGTAG ) {
1307 MesPrint(
"[%d] PF_Wait4Slave: received MSGTAG %d",(WORD)PF.me,(WORD)tag);
1310 if ( PF_W4Sstats == 0 ) {
1311 PF_W4Sstats = (LONG**)Malloc1(
sizeof(LONG*),
"");
1312 PF_W4Sstats[0] = (LONG*)Malloc1(PF_STATS_SIZE*
sizeof(LONG),
"");
1314 PF_Unpack(PF_W4Sstats[0],PF_STATS_SIZE,PF_LONG);
1315 PF_Statistics(PF_W4Sstats,next);
1336static WORD *partodoexr=NULL;
1345static int PF_Wait4SlaveIP(
int *src)
1349 tag = PF_ANY_MSGTAG;
1350 PF_CatchErrorMessages(src, &tag);
1353 if ( PF_W4Sstats == 0 ) {
1354 PF_W4Sstats = (LONG**)Malloc1(
sizeof(LONG*),
"");
1355 PF_W4Sstats[0] = (LONG*)Malloc1(PF_STATS_SIZE*
sizeof(LONG),
"");
1358 PF_Unpack(PF_W4Sstats[0],PF_STATS_SIZE,PF_LONG);
1359 if ( tag == PF_DATA_MSGTAG )
1360 AR.CurExpr = partodoexr[next];
1361 PF_Statistics(PF_W4Sstats,next);
1384static int PF_WaitAllSlaves(
void)
1386 int i, readySlaves, tag, next = PF_ANY_SOURCE;
1387 UBYTE *has_sent = 0;
1389 has_sent = (UBYTE*)Malloc1(
sizeof(UBYTE)*(PF.numtasks + 1),
"PF_WaitAllSlaves");
1390 for ( i = 0; i < PF.numtasks; i++ ) has_sent[i] = 0;
1392 for ( readySlaves = 1; readySlaves < PF.numtasks; ) {
1393 if ( next != PF_ANY_SOURCE) {
1395 if ( ++next >= PF.numtasks ) next = 1;
1396 }
while ( has_sent[next] == 1 );
1401 tag = PF_ProbeWithCatchingErrorMessages(&next);
1406 case PF_BUFFER_MSGTAG:
1407 case PF_ENDBUFFER_MSGTAG:
1411 if ( has_sent[next] == 0 ) {
1416 fprintf(stderr,
"ERROR next=%d tag=%d\n",next,tag);
1430#ifdef PF_WITH_SCHED_YIELD
1437 case PF_DATA_MSGTAG:
1439 next=PF_Wait4SlaveIP(&tag);
1443 PF_Statistics(PF_stats,0);
1444 PF_Slave2MasterIP(next);
1445 PF_Master2SlaveIP(next,NULL);
1446 if ( has_sent[next] == 0 ) {
1451 fprintf(stderr,
"ERROR next=%d tag=%d\n",next,tag);
1454 case PF_EMPTY_MSGTAG:
1456 next=PF_Wait4SlaveIP(&tag);
1460 PF_Master2SlaveIP(next,NULL);
1461 if ( has_sent[next] == 0 ) {
1466 fprintf(stderr,
"ERROR next=%d tag=%d\n",next,tag);
1469 case PF_READY_MSGTAG:
1474 next = PF_Wait4Slave(next);
1475 if ( next == -1 )
return(next);
1476 if ( has_sent[0] == 0 ) {
1477 PF.sbuf->active = 0;
1485 PACK_LONG(PF.sbuf->fill[next], AN.ninterms);
1489 *(PF.sbuf->fill[next])++ = 0;
1490 PF.sbuf->active = next;
1502 MesPrint(
"!!!Unexpected MPI message src=%d tag=%d.", next, tag);
1503 readySlaves = PF.numtasks+1;
1508 if ( has_sent ) M_free(has_sent,
"PF_WaitAllSlaves");
1513 return(PF.numtasks-readySlaves);
1536 WORD *term = AT.WorkPointer;
1545 if ( ( (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer ) ) > AT.WorkTop ) {
1550 if ( AC.numpfirstnum > 0 ) {
1551 for ( j = 0; j < AC.numpfirstnum; j++ ) {
1552 AC.inputnumbers[j] = -1;
1556 if ( AC.mparallelflag != PARALLELFLAG )
return(0);
1560 if ( PF.me == MASTER ) {
1565 WORD oldBracketOn = AR.BracketOn;
1566 WORD *oldBrackBuf = AT.BrackBuf;
1567 WORD oldbracketindexflag = AT.bracketindexflag;
1572 LONG ProcessBucketSize = AC.mProcessBucketSize;
1574 if ( PF.log && AC.CModule >= PF.log )
1575 MesPrint(
"[%d] working on expression %s in module %l",PF.me,EXPRNAME(i),AC.CModule);
1576 if ( GetTerm(BHEAD term) <= 0 ) {
1577 MesPrint(
"[%d] Expression %d has problems in scratchfile",PF.me,i);
1581 if ( AR.outtohide ) {
1582 SeekScratch(AR.hidefile,&position);
1583 e->onfile = position;
1584 if (
PutOut(BHEAD term,&position,AR.hidefile,0) < 0 )
return(-1);
1587 SeekScratch(AR.outfile,&position);
1588 e->onfile = position;
1589 if (
PutOut(BHEAD term,&position,AR.outfile,0) < 0 )
return(-1);
1593 if ( ( e->vflags & ISFACTORIZED ) != 0 ) {
1595 AT.BrackBuf = AM.BracketFactors;
1596 AT.bracketindexflag = 1;
1598 if ( AT.bracketindexflag > 0 ) OpenBracketIndex(i);
1609 if ( sb == 0 || sb->buff[0] != AT.SS->lBuffer ) {
1610 size = (LONG)((AT.SS->sTop2 - AT.SS->lBuffer)/(PF.numtasks));
1611 if ( size > (LONG)(AR.infile->POsize/
sizeof(WORD) - 1) )
1612 size = AR.infile->POsize/
sizeof(WORD) - 1;
1614 if ( ( sb = PF_AllocBuf(PF.numtasks,size*
sizeof(WORD),PF.numtasks) ) == NULL )
1617 sb->buff[0] = AT.SS->lBuffer;
1618 sb->full[0] = sb->fill[0] = sb->buff[0];
1619 for ( j = 1; j < PF.numtasks; j++ ) {
1620 sb->stop[j-1] = sb->buff[j] = sb->buff[j-1] + size;
1622 sb->stop[PF.numtasks-1] = sb->buff[PF.numtasks-1] + size;
1625 for ( j = 0; j < PF.numtasks; j++ ) {
1626 sb->full[j] = sb->fill[j] = sb->buff[j];
1638 maxinterms = ProcessBucketSize / 100;
1639 if ( maxinterms > e->counter / (PF.numtasks - 1) / 4 )
1640 maxinterms = e->counter / (PF.numtasks - 1) / 4;
1641 if ( maxinterms < 1 ) maxinterms = 1;
1652 while ( GetTerm(BHEAD term) ) {
1653 AN.ninterms++; dd = AN.deferskipped;
1654 if ( AC.CollectFun && *term <= (LONG)(AM.MaxTer/(2*
sizeof(WORD))) ) {
1655 if ( GetMoreTerms(term) < 0 ) {
1659 PRINTFBUF(
"PF_Processor gets",term,*term);
1660 if ( termsinbucket >= maxinterms || sb->fill[0] + *term >= sb->stop[0] ) {
1661 next = PF_Wait4Slave(PF_ANY_SOURCE);
1663 sb->fill[next] = sb->fill[0];
1664 sb->full[next] = sb->full[0];
1665 SWAP(sb->stop[next], sb->stop[0]);
1666 SWAP(sb->buff[next], sb->buff[0]);
1667 sb->fill[0] = sb->full[0] = sb->buff[0];
1679 if ( cmaxinterms >= PF.numtasks - 2 ) {
1681 if ( maxinterms >= ProcessBucketSize ) {
1683 maxinterms = ProcessBucketSize;
1686 else if ( cmaxinterms >= 0 ) {
1691 NCOPY(sb->fill[0], s, j);
1701 if ( LastExpression ) {
1703 if ( AR.infile->handle >= 0 ) {
1704 CloseFile(AR.infile->handle);
1705 AR.infile->handle = -1;
1706 remove(AR.infile->name);
1707 PUTZERO(AR.infile->POposition);
1709 AR.infile->POfill = AR.infile->POfull = AR.infile->PObuffer;
1711 if ( AR.outtohide ) AR.outfile = AR.hidefile;
1713 if (
EndSort(BHEAD AM.S0->sBuffer,0) < 0 )
return(-1);
1715 if ( AR.outtohide ) {
1716 AR.outfile = oldoutfile;
1717 AR.hidefile->POfull = AR.hidefile->POfill;
1720 AR.BracketOn = oldBracketOn;
1721 AT.BrackBuf = oldBrackBuf;
1722 if ( ( e->vflags & TOBEFACTORED ) != 0 )
1724 else if ( ( ( e->vflags & TOBEUNFACTORED ) != 0 )
1725 && ( ( e->vflags & ISFACTORIZED ) != 0 ) )
1727 AT.bracketindexflag = oldbracketindexflag;
1738 PF_PostEndSortBarrier();
1740 DBGOUT_NINTERMS(1, (
"PF.me=%d AN.ninterms=%d ENDSORT\n", (
int)PF.me, (
int)AN.ninterms));
1741 PF_CatchErrorMessagesForAll();
1743 for ( k = 1; k < PF.numtasks; k++ ) {
1747 WORD numdummies, expchanged;
1750 if ( e->numdummies < numdummies ) e->numdummies = numdummies;
1751 AR.expchanged |= expchanged;
1754 if ( AC.numpfirstnum > 0 ) PF_UnpackRedefinedPreVars();
1757 if ( AC.numpfirstnum > 0 ) {
1759 if ( RetCode )
return RetCode;
1761 if ( ! AC.OldParallelStats ) {
1764 for ( k = 1; k < PF.numtasks; k++ ) {
1765 genterms += PF_stats[k][3];
1767 AT.SS->GenTerms = genterms;
1768 WriteStats(&PF_exprsize, STATSPOSTSORT, NOCHECKLOGTYPE);
1769 Expressions[AR.CurExpr].size = PF_exprsize;
1771 PF_Statistics(PF_stats,0);
1776 if ( AM.S0->TermsLeft ) e->vflags &= ~ISZERO;
1777 else e->vflags |= ISZERO;
1778 if ( AR.expchanged == 0 ) e->vflags |= ISUNMODIFIED;
1779 if ( AM.S0->TermsLeft ) AR.expflags |= ISZERO;
1780 if ( AR.expchanged ) AR.expflags |= ISUNMODIFIED;
1796 WORD oldBracketOn = AR.BracketOn;
1797 WORD *oldBrackBuf = AT.BrackBuf;
1798 WORD oldbracketindexflag = AT.bracketindexflag;
1801 if ( AC.numpfirstnum > 0 ) {
1802 for ( j = 0; j < AC.numpfirstnum; j++ ) {
1803 AC.inputnumbers[j] = -1;
1807 SeekScratch(AR.outfile,&position);
1808 e->onfile = position;
1809 AR.DeferFlag = AC.ComDefer;
1811 if ( ( e->vflags & ISFACTORIZED ) != 0 ) {
1813 AT.BrackBuf = AM.BracketFactors;
1814 AT.bracketindexflag = 1;
1817 AR.MaxDum = AM.IndDum;
1822 FILEHANDLE *fi = AC.RhsExprInModuleFlag && PF.rhsInParallel ? &PF.slavebuf : AR.infile;
1823 fi->POfull = fi->POfill = fi->PObuffer;
1827 while ( PF_GetTerm(term) ) {
1828 PF_linterms++; AN.ninterms++; dd = AN.deferskipped;
1829 AT.WorkPointer = term + *term;
1830 AN.RepPoint = AT.RepCount + 1;
1831 if ( ( e->vflags & ISFACTORIZED ) != 0 && term[1] == HAAKJE ) {
1835 if ( AR.DeferFlag ) {
1836 AR.CurDum = AN.IndDum = Expressions[AR.CurExpr].numdummies + AM.IndDum;
1839 AN.IndDum = AM.IndDum;
1840 AR.CurDum = ReNumber(BHEAD term);
1842 if ( AC.SymChangeFlag ) MarkDirty(term,DIRTYSYMFLAG);
1844 if ( ( AC.modmode & ALSOFUNARGS ) != 0 ) MarkDirty(term,DIRTYFLAG);
1845 else if ( AR.PolyFun ) PolyFunDirty(BHEAD term);
1847 else if ( AC.PolyRatFunChanged ) PolyFunDirty(BHEAD term);
1848 if ( ( AR.PolyFunType == 2 ) && ( AC.PolyRatFunChanged == 0 )
1849 && ( e->status == LOCALEXPRESSION || e->status == GLOBALEXPRESSION ) ) {
1850 PolyFunClean(BHEAD term);
1856 PF_linterms += dd; AN.ninterms += dd;
1858 PF_linterms += dd; AN.ninterms += dd;
1874 WORD *oldbuff = fout->PObuffer;
1875 WORD *oldstop = fout->POstop;
1876 LONG oldsize = fout->POsize;
1877 if (
EndSort(BHEAD AM.S0->sBuffer, 0) < 0 )
return -1;
1878 fout->PObuffer = oldbuff;
1879 fout->POstop = oldstop;
1880 fout->POsize = oldsize;
1881 fout->POfill = fout->POfull = fout->PObuffer;
1883 AR.BracketOn = oldBracketOn;
1884 AT.BrackBuf = oldBrackBuf;
1885 AT.bracketindexflag = oldbracketindexflag;
1890 PF_PostEndSortBarrier();
1892 DBGOUT_NINTERMS(1, (
"PF.me=%d AN.ninterms=%d PF_linterms=%d ENDSORT\n", (
int)PF.me, (
int)AN.ninterms, (
int)PF_linterms));
1902 WORD numdummies = AR.MaxDum - AM.IndDum;
1907 if ( AC.numpfirstnum > 0 ) PF_PackRedefinedPreVars();
1910 if ( AC.numpfirstnum > 0 ) {
1912 if ( RetCode )
return RetCode;
1924 NumToStr(lbuf,AC.CModule);
1925 fprintf(stderr,
"[%d|%s] Endsort,Collect,Broadcast done\n",PF.me,lbuf);
1952 PF_CurrentBracket = 0;
1959 if (ret) {
return ret; }
1964 PF_statsinterval = 10;
1966 PF.exprbufsize=4096;
1969 if ( PF.me == MASTER ) {
1974 if ( ( c = getenv(
"PF_LOG") ) != 0 ) {
1975 if ( *c ) PF.log = (int)atoi(c);
1977 fprintf(stderr,
"[%d] changing PF.log to %d\n",PF.me,PF.log);
1980 if ( ( c = (
char*)getenv(
"PF_RBUFS") ) != 0 ) {
1981 PF.numrbufs = (int)atoi(c);
1982 fprintf(stderr,
"[%d] changing numrbufs to: %d\n",PF.me,PF.numrbufs);
1985 if ( ( c = (
char*)getenv(
"PF_SBUFS") ) != 0 ) {
1986 PF.numsbufs = (int)atoi(c);
1987 fprintf(stderr,
"[%d] changing numsbufs to: %d\n",PF.me,PF.numsbufs);
1990 if ( PF.numsbufs > 10 ) PF.numsbufs = 10;
1991 if ( PF.numsbufs < 1 ) PF.numsbufs = 1;
1992 if ( PF.numrbufs > 2 ) PF.numrbufs = 2;
1993 if ( PF.numrbufs < 1 ) PF.numrbufs = 1;
1995 if ( ( c = getenv(
"PF_STATS") ) ) {
1997 PF_statsinterval = (int)atoi(c);
1998 NumToStr(lbuf,PF_statsinterval);
1999 fprintf(stderr,
"[%d] changing PF_statsinterval to %s\n",PF.me,lbuf);
2001 if ( PF_statsinterval < 1 ) PF_statsinterval = 10;
2008 if ( PF.me == MASTER ) {
2011 PF_Pack(&PF.numrbufs,1,PF_WORD);
2012 PF_Pack(&PF.numsbufs,1,PF_WORD);
2015 if ( PF.me != MASTER ) {
2020 fprintf(stderr,
"[%d] log=%d rbufs=%d sbufs=%d\n",
2021 PF.me, PF.log, PF.numrbufs, PF.numsbufs);
2043 if ( errorcode != 0 && PF_processing ) {
2045 PF_RaiseRuntimeError();
2079 LONG slavetimes = 0;
2080 LONG t = PF.me == MASTER ? 0 : AM.SumTime +
TimeCPU(1);
2081 int ret =
PF_Reduce(&t, &slavetimes, 1, PF_LONG, MPI_SUM, MASTER);
2100#ifdef PF_DEBUG_BCAST_LONG
2101 if ( PF.me == MASTER ) {
2102 MesPrint(
">> Broadcast LONG: %l", x);
2129#ifdef PF_DEBUG_BCAST_BUF
2130 if ( PF.me == MASTER ) {
2131 MesPrint(
">> Broadcast Buffer: length=%l", *length);
2135 if ( PF.me != MASTER ) {
2140 if ( *length <= 0 )
return;
2142 if ( PF.me != MASTER ) {
2143 *buffer = (WORD *)Malloc1(*length *
sizeof(WORD),
"PF_BroadcastBuffer");
2148 while ( rest > 0 ) {
2149 int l = rest < (LONG)PF.exprbufsize ? (
int)rest : PF.exprbufsize;
2182 if ( MASTER == PF.me ) {
2191 if ( MASTER != PF.me ) {
2198 }
while ( cstr[clength-1] !=
'\0' );
2230 if ( MASTER == PF.me ) {
2236 LONG n = ((*newsize)+1)/PF_maxDollarChunkSize;
2248 WORD *thechunk = *dbuffer;
2251 err |=
PF_Pack(numterms,1,PF_INT);
2252 err |=
PF_Pack(newsize,1,PF_LONG);
2257 for ( i = 0; i < n; i++ ) {
2258 err |=
PF_Pack(thechunk,PF_maxDollarChunkSize,PF_WORD);
2260 thechunk +=PF_maxDollarChunkSize;
2266 if ( ( n = ( (*newsize)+1)%PF_maxDollarChunkSize ) != 0 ) {
2267 err |=
PF_Pack(thechunk,n,PF_WORD);
2270#ifdef PF_DEBUG_BCAST_PREDOLLAR
2271 MesPrint(
">> Broadcast PreDollar: newsize=%d numterms=%d", (
int)*newsize, *numterms);
2274 if ( MASTER != PF.me ) {
2276 LONG n, therest, thesize;
2283 thesize = (*newsize)+1;
2288 n = (thesize/PF_maxDollarChunkSize) - 1;
2292 therest = thesize % PF_maxDollarChunkSize;
2293 thechunk = *dbuffer =
2294 (WORD*)Malloc1( thesize *
sizeof(WORD),
"$-buffer slave");
2295 if ( thechunk == NULL )
return(err|4);
2300 for ( i = 0; i < n; i++ ) {
2301 err |=
PF_Unpack(thechunk,PF_maxDollarChunkSize,PF_WORD);
2302 thechunk += PF_maxDollarChunkSize;
2309 err |=
PF_Unpack(thechunk,PF_maxDollarChunkSize,PF_WORD);
2310 thechunk += PF_maxDollarChunkSize;
2316 if ( therest != 0 ) err |=
PF_Unpack(thechunk,therest,PF_WORD);
2331static inline LONG dollarlen(
const WORD *terms)
2333 const WORD *p = terms;
2334 while ( *p ) p += *p;
2347static inline WORD dollar_mod_type(WORD index)
2350 for ( i = 0; i < NumModOptdollars; i++ )
2351 if ( ModOptdollars[i].number == index )
break;
2352 if ( i >= NumModOptdollars )
return -1;
2353 return ModOptdollars[i].type;
2371static inline int dollar_to_be_collected(WORD index)
2373 switch ( dollar_mod_type(index) ) {
2392static inline void copy_dollar(WORD index, WORD type,
const WORD *where, LONG size)
2396 CleanDollarFactors(d);
2398 if ( type != DOLZERO && where != NULL && where != &AM.dollarzero && where[0] != 0 && size > 0 ) {
2399 if ( size > d->size || size < d->size / 4 ) {
2400 if ( d->where && d->where != &AM.dollarzero )
2401 M_free(d->where,
"old content of dollar");
2402 d->where = Malloc1(
sizeof(WORD) * size,
"copy buffer to dollar");
2406 WCOPY(d->where, where, size);
2409 if ( d->where && d->where != &AM.dollarzero )
2410 M_free(d->where,
"old content of dollar");
2412 d->where = &AM.dollarzero;
2426static inline int compare_two_expressions(
const WORD *e1,
const WORD *e2)
2445 else if ( e2[e2[0]] == 0 && e2[0] == ABS(e2[e2[0] - 1]) + 1 ) {
2446 if ( e2[e2[0] - 1] > 0 )
2452 else if ( e1[e1[0]] == 0 && e1[0] == ABS(e1[e1[0] - 1]) + 1 ) {
2454 if ( e1[e1[0] - 1] > 0 )
2459 else if ( e2[e2[0]] == 0 && e2[0] == ABS(e2[e2[0] - 1]) + 1 ) {
2460 return(
CompCoef((WORD *)e1, (WORD *)e2));
2464 while ( e1[0] && e2[0] ) {
2465 int c = CompareTerms(BHEAD (WORD *)e1, (WORD *)e2, 1);
2473 if ( e1[0] )
return(+1);
2474 if ( e2[0] )
return(-1);
2516 if ( AC.mparallelflag != PARALLELFLAG && !AC.partodoflag )
return 0;
2522 for ( i = 0; i < NumPotModdollars; i++ ) {
2523 WORD index = PotModdollars[i];
2524 if ( dollar_to_be_collected(index) ) ndollars++;
2526 if ( ndollars == 0 )
return 0;
2528 if ( PF.me == MASTER ) {
2534 int nbufs = ndollars * (PF.numtasks - 1);
2536 for ( i =
VectorSize(dollar_slave_bufs); i < nbufs; i++ ) {
2541 for ( nslaves = 1; nslaves < PF.numtasks; nslaves++ ) {
2545 for ( i = 0; i < NumPotModdollars; i++ ) {
2546 WORD index = PotModdollars[i];
2548 if ( !dollar_to_be_collected(index) )
continue;
2549 b = &
VectorPtr(dollar_slave_bufs)[(PF.numtasks - 1) * nvars + (src - 1)];
2551 if ( b->type != DOLZERO ) {
2577 for ( i = 0; i < NumPotModdollars; i++ ) {
2578 WORD index = PotModdollars[i];
2582 if ( !dollar_to_be_collected(index) )
continue;
2583 d = Dollars + index;
2584 b = &
VectorPtr(dollar_slave_bufs)[(PF.numtasks - 1) * nvars];
2585 dtype = dollar_mod_type(index);
2593 for ( j = 1; j < PF.numtasks - 1; j++ ) {
2595 if ( (dtype == MODMAX && c > 0) || (dtype == MODMIN && c < 0) )
2599 copy_dollar(index, b->type,
VectorPtr(b->buf), b->size);
2612 CBUF *C = cbuf + AM.rbufnum;
2613 WORD *oldwork = AT.WorkPointer, *oldcterm = AN.cTerm;
2614 WORD olddefer = AR.DeferFlag, oldnumlhs = AR.Cnumlhs, oldnumrhs = C->numrhs;
2622 if ( ((WORD *)((UBYTE *)AT.WorkPointer + AM.MaxTer)) > AT.WorkTop ) {
2643 for ( j = 0; j < PF.numtasks; j++ ) {
2645 for ( r = j == 0 ? Dollars[index].where :
VectorPtr(b[j - 1].buf); *r; r += *r ) {
2646 WCOPY(AT.WorkPointer, r, *r);
2647 AT.WorkPointer += *r;
2654 AT.WorkPointer = oldwork;
2658 size =
EndSort(BHEAD (WORD *)&dbuf, 2);
2668 if ( dbuf[0] == 0 ) {
2671 else if ( dbuf[dbuf[0]] == 0 ) {
2672 const WORD *t = dbuf, *w;
2676 if ( nsize < 0 ) nsize = -nsize;
2677 if ( nsize == n - 1 ) {
2678 nsize = (nsize - 1) / 2;
2681 w++;
while ( w < t + n - 1 ) {
if ( *w )
break; w++; }
2682 if ( w >= t + n - 1 ) type = DOLNUMBER;
2684 else if ( n == 7 && t[6] == 3 && t[5] == 1 && t[4] == 1 && t[1] == INDEX && t[2] == 3 ) {
2690 copy_dollar(index, type, dbuf, dollarlen(dbuf) + 1);
2691 M_free(dbuf,
"temporary dollar buffer");
2693 AR.Cnumlhs = oldnumlhs;
2694 C->numrhs = oldnumrhs;
2695 AR.DeferFlag = olddefer;
2696 AN.cTerm = oldcterm;
2697 AT.WorkPointer = oldwork;
2699 if ( err )
return err;
2706 if ( d->type == DOLTERMS )
2707 cbuf[AM.dbufnum].CanCommu[index] = numcommute(d->where, &cbuf[AM.dbufnum].NumTerms[index]);
2708 cbuf[AM.dbufnum].rhs[index] = d->where;
2710#ifdef PF_DEBUG_REDUCE_DOLLAR
2711 MesPrint(
"<< Reduce $-var: %s", AC.dollarnames->namebuffer + d->name);
2724 for ( i = 0; i < NumPotModdollars; i++ ) {
2725 WORD index = PotModdollars[i];
2727 if ( !dollar_to_be_collected(index) )
continue;
2728 d = Dollars + index;
2730 if ( d->type != DOLZERO ) {
2735 LONG size = dollarlen(d->where);
2762static inline int dollar_to_be_broadcast(WORD index)
2764 switch ( dollar_mod_type(index) ) {
2796 for ( i = 0; i < NumPotModdollars; i++ ) {
2797 WORD index = PotModdollars[i];
2798 if ( dollar_to_be_broadcast(index) ) ndollars++;
2800 if ( ndollars == 0 )
return 0;
2802 if ( PF.me == MASTER ) {
2808 for ( i = 0; i < NumPotModdollars; i++ ) {
2809 WORD index = PotModdollars[i];
2811 if ( !dollar_to_be_broadcast(index) )
continue;
2812 d = Dollars + index;
2813 PF_LongMultiPack(&d->type, 1, PF_WORD);
2814 if ( d->type != DOLZERO ) {
2819 LONG size = dollarlen(d->where);
2820 PF_LongMultiPack(&size, 1, PF_LONG);
2821 PF_LongMultiPack(d->where, size, PF_WORD);
2823 PF_LongMultiPack(&d->nfactors, 1, PF_WORD);
2824 if ( d->nfactors > 1 ) {
2825 for ( j = 0; j < d->nfactors; j++ ) {
2827 PF_LongMultiPack(&f->type, 1, PF_WORD);
2828 PF_LongMultiPack(&f->size, 1, PF_LONG);
2830 PF_LongMultiPack(f->where, f->size, PF_WORD);
2832 PF_LongMultiPack(&f->value, 1, PF_WORD);
2836#ifdef PF_DEBUG_BCAST_DOLLAR
2837 MesPrint(
">> Broadcast $-var: %s", AC.dollarnames->namebuffer + d->name);
2845 if ( PF.me != MASTER ) {
2849 for ( i = 0; i < NumPotModdollars; i++ ) {
2850 WORD index = PotModdollars[i];
2852 if ( !dollar_to_be_broadcast(index) )
continue;
2853 d = Dollars + index;
2855 if ( d->where && d->where != &AM.dollarzero )
2856 M_free(d->where,
"old content of dollar");
2857 d->where = &AM.dollarzero;
2859 CleanDollarFactors(d);
2861 PF_LongMultiUnpack(&d->type, 1, PF_WORD);
2862 if ( d->type != DOLZERO ) {
2864 PF_LongMultiUnpack(&size, 1, PF_LONG);
2866 d->where = (WORD *)Malloc1(
sizeof(WORD) * d->size,
"dollar content");
2867 PF_LongMultiUnpack(d->where, size, PF_WORD);
2870 PF_LongMultiUnpack(&d->nfactors, 1, PF_WORD);
2871 if ( d->nfactors > 1 ) {
2872 d->factors = (
FACDOLLAR *)Malloc1(
sizeof(
FACDOLLAR) * d->nfactors,
"dollar factored stuff");
2873 for ( j = 0; j < d->nfactors; j++ ) {
2875 PF_LongMultiUnpack(&f->type, 1, PF_WORD);
2876 PF_LongMultiUnpack(&f->size, 1, PF_LONG);
2877 if ( f->size > 0 ) {
2878 f->where = (WORD *)Malloc1(
sizeof(WORD) * (f->size + 1),
"dollar factor content");
2879 PF_LongMultiUnpack(f->where, f->size, PF_WORD);
2880 f->where[f->size] = 0;
2885 PF_LongMultiUnpack(&f->value, 1, PF_WORD);
2890 if ( d->type == DOLTERMS )
2891 cbuf[AM.dbufnum].CanCommu[index] = numcommute(d->where, &cbuf[AM.dbufnum].NumTerms[index]);
2892 cbuf[AM.dbufnum].rhs[index] = d->where;
2909static Vector(UBYTE, prevarbuf);
2924static void PF_PackRedefinedPreVars(
void)
2929 for ( i = 0; i < AC.numpfirstnum; i++ )
2930 if ( AC.inputnumbers[i] >= 0 ) nredefs++;
2933 for ( i = 0; i < AC.numpfirstnum; i++ )
2934 if ( AC.inputnumbers[i] >= 0) {
2935 WORD index = AC.pfirstnum[i];
2936 UBYTE *value = PreVar[index].value;
2937 int bytes = strlen((
char *)value);
2959static void PF_UnpackRedefinedPreVars(
void)
2965 if ( nredefs > 0 ) {
2967 for ( i = 0; i < nredefs; i++ ) {
2977 value[bytes] =
'\0';
2980 for ( j = 0; j < AC.numpfirstnum; j++ )
2981 if ( AC.pfirstnum[j] == index )
break;
2982 if ( AC.inputnumbers[j] < inputnumber ) {
2983 AC.inputnumbers[j] = inputnumber;
2984 PutPreVar(PreVar[index].name, value, NULL, 1);
3012 if ( PF.me == MASTER ) {
3020 for ( i = 0; i < AC.numpfirstnum; i++ )
3021 if ( AC.inputnumbers[i] >= 0 ) nredefs++;
3022 PF_LongMultiPack(&nredefs, 1, PF_INT);
3024 for ( i = 0; i < AC.numpfirstnum; i++ )
3025 if ( AC.inputnumbers[i] >= 0) {
3026 WORD index = AC.pfirstnum[i];
3027 UBYTE *value = PreVar[index].value;
3028 int bytes = strlen((
char *)value);
3029 PF_LongMultiPack(&index, 1, PF_WORD);
3030 PF_LongMultiPack(&bytes, 1, PF_INT);
3031 PF_LongMultiPack(value, bytes, PF_BYTE);
3032#ifdef PF_DEBUG_BCAST_PREVAR
3033 MesPrint(
">> Broadcast PreVar: %s = \"%s\"", PreVar[index].name, value);
3041 if ( PF.me != MASTER ) {
3047 PF_LongMultiUnpack(&nredefs, 1, PF_INT);
3048 if ( nredefs > 0 ) {
3050 for ( i = 0; i < nredefs; i++ ) {
3054 PF_LongMultiUnpack(&index, 1, PF_WORD);
3055 PF_LongMultiUnpack(&bytes, 1, PF_INT);
3058 PF_LongMultiUnpack(value, bytes, PF_BYTE);
3059 value[bytes] =
'\0';
3060 PutPreVar(PreVar[index].name, value, NULL, 1);
3078static WORD oldRhsExprInModuleFlag;
3079static Vector(WORD, oldPotModdollars);
3080static Vector(WORD, oldpfirstnum);
3095int PF_StoreInsideInfo(
void)
3098 oldRhsExprInModuleFlag = AC.RhsExprInModuleFlag;
3100 for ( i = 0; i < NumPotModdollars; i++ )
3103 for ( i = 0; i < AC.numpfirstnum; i++ )
3121int PF_RestoreInsideInfo(
void)
3124 AC.RhsExprInModuleFlag = oldRhsExprInModuleFlag;
3125 NumPotModdollars =
VectorSize(oldPotModdollars);
3126 for ( i = 0; i < NumPotModdollars; i++ )
3127 PotModdollars[i] =
VectorPtr(oldPotModdollars)[i];
3129 for ( i = 0; i < AC.numpfirstnum; i++ )
3130 AC.pfirstnum[i] =
VectorPtr(oldpfirstnum)[i];
3149 CBUF *C = cbuf + bufnum;
3152 if ( PF.me == MASTER ) {
3158 PF_LongMultiPack(&C->
BufferSize, 1, PF_LONG);
3159 PF_LongMultiPack(&C->numlhs, 1, PF_INT);
3160 PF_LongMultiPack(&C->numrhs, 1, PF_INT);
3161 PF_LongMultiPack(&C->maxlhs, 1, PF_INT);
3162 PF_LongMultiPack(&C->maxrhs, 1, PF_INT);
3163 PF_LongMultiPack(&C->mnumlhs, 1, PF_INT);
3164 PF_LongMultiPack(&C->mnumrhs, 1, PF_INT);
3165 PF_LongMultiPack(&C->numtree, 1, PF_INT);
3166 PF_LongMultiPack(&C->rootnum, 1, PF_INT);
3167 PF_LongMultiPack(&C->MaxTreeSize, 1, PF_INT);
3170 PF_LongMultiPack(&l, 1, PF_LONG);
3171 PF_LongMultiPack(C->
Buffer, l, PF_WORD);
3172 for ( i = 0; i < C->numlhs + 1; i++ ) {
3174 PF_LongMultiPack(&l, 1, PF_LONG);
3176 for ( i = 0; i < C->numrhs + 1; i++ ) {
3178 PF_LongMultiPack(&l, 1, PF_LONG);
3180 PF_LongMultiPack(C->
CanCommu, C->numrhs + 1, PF_LONG);
3181 PF_LongMultiPack(C->
NumTerms, C->numrhs + 1, PF_LONG);
3182 PF_LongMultiPack(C->
numdum, C->numrhs + 1, PF_WORD);
3183 PF_LongMultiPack(C->
dimension, C->numrhs + 1, PF_WORD);
3184 if ( C->MaxTreeSize > 0 )
3185 PF_LongMultiPack(C->
boomlijst, (C->numtree + 1) * (
sizeof(
COMPTREE) /
sizeof(
int)), PF_INT);
3186#ifdef PF_DEBUG_BCAST_CBUF
3187 MesPrint(
">> Broadcast CBuf %d", bufnum);
3194 if ( PF.me != MASTER ) {
3201 PF_LongMultiUnpack(&C->
BufferSize, 1, PF_LONG);
3202 PF_LongMultiUnpack(&C->numlhs, 1, PF_INT);
3203 PF_LongMultiUnpack(&C->numrhs, 1, PF_INT);
3204 PF_LongMultiUnpack(&C->maxlhs, 1, PF_INT);
3205 PF_LongMultiUnpack(&C->maxrhs, 1, PF_INT);
3206 PF_LongMultiUnpack(&C->mnumlhs, 1, PF_INT);
3207 PF_LongMultiUnpack(&C->mnumrhs, 1, PF_INT);
3208 PF_LongMultiUnpack(&C->numtree, 1, PF_INT);
3209 PF_LongMultiUnpack(&C->rootnum, 1, PF_INT);
3210 PF_LongMultiUnpack(&C->MaxTreeSize, 1, PF_INT);
3212 C->
Buffer = (WORD *)Malloc1(C->
BufferSize *
sizeof(WORD),
"compiler buffer");
3214 C->
lhs = (WORD **)Malloc1(C->maxlhs *
sizeof(WORD *),
"compiler buffer");
3215 C->
rhs = (WORD **)Malloc1(C->maxrhs * (
sizeof(WORD *) + 2 *
sizeof(LONG) + 2 *
sizeof(WORD)),
"compiler buffer");
3220 if ( C->MaxTreeSize > 0 )
3223 PF_LongMultiUnpack(&l, 1, PF_LONG);
3224 PF_LongMultiUnpack(C->
Buffer, l, PF_WORD);
3226 for ( i = 0; i < C->numlhs + 1; i++ ) {
3227 PF_LongMultiUnpack(&l, 1, PF_LONG);
3230 for ( i = 0; i < C->numrhs + 1; i++ ) {
3231 PF_LongMultiUnpack(&l, 1, PF_LONG);
3234 PF_LongMultiUnpack(C->
CanCommu, C->numrhs + 1, PF_LONG);
3235 PF_LongMultiUnpack(C->
NumTerms, C->numrhs + 1, PF_LONG);
3236 PF_LongMultiUnpack(C->
numdum, C->numrhs + 1, PF_WORD);
3237 PF_LongMultiUnpack(C->
dimension, C->numrhs + 1, PF_WORD);
3238 if ( C->MaxTreeSize > 0 )
3239 PF_LongMultiUnpack(C->
boomlijst, (C->numtree + 1) * (
sizeof(
COMPTREE) /
sizeof(
int)), PF_INT);
3262 if ( PF.me == MASTER ) {
3267 PF_LongMultiPack(&AR.expflags, 1, PF_WORD);
3268 for ( i = 0; i < NumExpressions; i++ ) {
3269 e = &Expressions[i];
3270 PF_LongMultiPack(&e->counter, 1, PF_WORD);
3271 PF_LongMultiPack(&e->vflags, 1, PF_WORD);
3272 PF_LongMultiPack(&e->uflags, 1, PF_WORD);
3273 PF_LongMultiPack(&e->numdummies, 1, PF_WORD);
3274 PF_LongMultiPack(&e->numfactors, 1, PF_WORD);
3275#ifdef PF_DEBUG_BCAST_EXPRFLAGS
3276 MesPrint(
">> Broadcast ExprFlags: %s", AC.exprnames->namebuffer + e->name);
3284 if ( PF.me != MASTER ) {
3288 PF_LongMultiUnpack(&AR.expflags, 1, PF_WORD);
3289 for ( i = 0; i < NumExpressions; i++ ) {
3290 e = &Expressions[i];
3291 PF_LongMultiUnpack(&e->counter, 1, PF_WORD);
3292 PF_LongMultiUnpack(&e->vflags, 1, PF_WORD);
3293 PF_LongMultiUnpack(&e->uflags, 1, PF_WORD);
3294 PF_LongMultiUnpack(&e->numdummies, 1, PF_WORD);
3295 PF_LongMultiUnpack(&e->numfactors, 1, PF_WORD);
3318 ( f->
handle >= 0) && ISGEPOS(*position,f->POposition) &&
3319 ( ISGEPOSINC(*position,f->POposition,(f->POfull-f->PObuffer)*
sizeof(WORD)) ==0 )
3321 f->POfull=f->PObuffer;
3322 SetScratch(f,position);
3341 if ( ( RetCode = CreateFile(f->name) ) >= 0 ) {
3342 f->
handle = (WORD)RetCode;
3343 PUTZERO(f->filesize);
3344 PUTZERO(f->POposition);
3347 MesPrint(
"Cannot create scratch file %s",f->name);
3351 size = (f->POfill-f->PObuffer)*
sizeof(WORD);
3353 SeekFile(f->
handle,&(f->POposition),SEEK_SET);
3354 if ( WriteFile(f->
handle,(UBYTE *)(f->PObuffer),size) != size ){
3355 MesPrint(
"Error while writing to disk. Disk full?");
3358 ADDPOS(f->filesize,size);
3359 ADDPOS(f->POposition,size);
3360 f->POfill = f->POfull=f->PObuffer;
3372static int PF_WalkThroughExprMaster(
FILEHANDLE *curfile,
int dl)
3376 if(curfile->POfull-curfile->POfill < dl){
3378 SeekScratch(curfile,&pos);
3379 PF_SetScratch(curfile,&pos);
3381 curfile->POfill+=dl;
3383 if( l >= PF.exprbufsize){
3384 if( l == PF.exprbufsize){
3385 if( *(curfile->POfill) == 0)
3393 dl=*(curfile->POfill);
3395 return l-PF.exprbufsize;
3398 if(curfile->POfull-curfile->POfill < 1){
3400 SeekScratch(curfile,&pos);
3401 PF_SetScratch(curfile,&pos);
3403 dl=*(curfile->POfill+1)+2;
3414static int PF_WalkThroughExprSlave(
FILEHANDLE *curfile, LONG *counter,
int dl)
3418 if(curfile->POstop-curfile->POfill < dl){
3419 if(PF_pushScratch(curfile))
3420 return(-PF.exprbufsize-1);
3422 curfile->POfill+=dl;
3423 curfile->POfull=curfile->POfill;
3425 if( l >= PF.exprbufsize){
3426 if( l == PF.exprbufsize){
3431 if( *(curfile->POfill) == 0)
3436 curfile->POfull=curfile->POfill;
3440 dl=*(curfile->POfill);
3442 return l-PF.exprbufsize;
3445 if(curfile->POstop-curfile->POfill < 1){
3446 if(PF_pushScratch(curfile))
3447 return(-PF.exprbufsize-1);
3453 dl=*(curfile->POfill+1)+2;
3473 SetScratch(curfile,&(e->onfile));
3480 if ( curfile->POfull - curfile->POfill < PF.exprbufsize + 1 ) {
3482 SeekScratch(curfile,&pos);
3483 PF_SetScratch(curfile,&pos);
3485 if (
PF_Bcast(curfile->POfill, (PF.exprbufsize + 1) *
sizeof(WORD)) )
3487 l=PF_WalkThroughExprMaster(curfile,l-1);
3517 if ( curfile->POstop - curfile->POfill < PF.exprbufsize + 1 ) {
3518 if(PF_pushScratch(curfile))
3521 if (
PF_Bcast(curfile->POfill, (PF.exprbufsize + 1) *
sizeof(WORD)) )
3523 l = PF_WalkThroughExprSlave(curfile, &counter, l - 1);
3526 if(l<-PF.exprbufsize)
3530 if ( curfile->
handle >= 0 ) {
3531 if ( PF_pushScratch(curfile) )
return -1;
3533 curfile->POfull=curfile->POfill;
3534 if ( curfile != AR.hidefile ) AR.InInBuf = curfile->POfull-curfile->PObuffer;
3535 else AR.InHiBuf = curfile->POfull-curfile->PObuffer;
3536 CHECK(counter == e->counter + 1);
3554 if ( PF.me == MASTER ) {
3555 if ( PF_rhsBCastMaster(file, e) )
return -1;
3556#ifdef PF_DEBUG_BCAST_RHSEXPR
3557 MesPrint(
">> Broadcast RhsExpr: %s", AC.exprnames->namebuffer + e->name);
3562 SetEndHScratch(file, &pos);
3564 if ( PF_rhsBCastSlave(file, e) )
return -1;
3583 for ( i = 0; i < NumExpressions; i++ ) {
3585 if ( !(e->vflags & ISINRHS) )
continue;
3586 switch ( e->status ) {
3587 case LOCALEXPRESSION:
3588 case SKIPLEXPRESSION:
3589 case DROPLEXPRESSION:
3590 case GLOBALEXPRESSION:
3591 case SKIPGEXPRESSION:
3592 case DROPGEXPRESSION:
3593 case HIDELEXPRESSION:
3594 case HIDEGEXPRESSION:
3595 case INTOHIDELEXPRESSION:
3596 case INTOHIDEGEXPRESSION:
3599 case HIDDENLEXPRESSION:
3600 case HIDDENGEXPRESSION:
3601 case DROPHLEXPRESSION:
3602 case DROPHGEXPRESSION:
3603 case UNHIDELEXPRESSION:
3604 case UNHIDEGEXPRESSION:
3609 if ( PF.me != MASTER )
3636 if ( PF.numtasks >= 3 ) {
3637 for ( i = 0; i < NumExpressions; i++ ) {
3638 e = Expressions + i;
3639 if ( e->partodo > 0 && e->counter == 0 ) {
3645 if(PF.me == MASTER){
3646 if ( PF.numtasks >= 3 ) {
3647 partodoexr = (WORD*)Malloc1(
sizeof(WORD)*(PF.numtasks+1),
"PF_InParallelProcessor");
3648 for ( i = 0; i < NumExpressions; i++ ) {
3650 if ( e->partodo <= 0 )
continue;
3652 case LOCALEXPRESSION:
3653 case GLOBALEXPRESSION:
3654 case UNHIDELEXPRESSION:
3655 case UNHIDEGEXPRESSION:
3656 case INTOHIDELEXPRESSION:
3657 case INTOHIDEGEXPRESSION:
3659 next=PF_Wait4SlaveIP(&tag);
3662 if(tag == PF_DATA_MSGTAG){
3663 PF_Statistics(PF_stats,0);
3664 if(PF_Slave2MasterIP(next))
3667 if(PF_Master2SlaveIP(next,e))
3680 if(PF_WaitAllSlaves())
3683 if ( AC.CollectFun ) AR.DeferFlag = 0;
3685 M_free(partodoexr,
"PF_InParallelProcessor");
3690 for ( i = 0; i < NumExpressions; i++ ) {
3691 Expressions[i].partodo = 0;
3694 PF_PostEndSortBarrier();
3698 if(PF_Wait4MasterIP(PF_EMPTY_MSGTAG))
3702 WORD *oldwork= AT.WorkPointer;
3703 tag=PF_ReadMaster();
3706 if(tag == PF_DATA_MSGTAG){
3707 oldwork = AT.WorkPointer;
3710 if ( AC.numpfirstnum > 0 ) {
3712 for ( j = 0; j < AC.numpfirstnum; j++ ) {
3713 AC.inputnumbers[j] = -1;
3719 if(PF_Wait4MasterIP(PF_DATA_MSGTAG))
3721 if(PF_Slave2MasterIP(PF.me))
3723 AT.WorkPointer=oldwork;
3725 }
while(tag!=PF_EMPTY_MSGTAG);
3727 PF_PostEndSortBarrier();
3736static int PF_Wait4MasterIP(
int tag)
3742 fprintf(stderr,
"[%d] Starting to send to Master\n",PF.me);
3750 PF_Pack(&PF_linterms ,1,PF_LONG);
3751 PF_Pack(&(AM.S0->GenTerms) ,1,PF_LONG);
3752 PF_Pack(&(AM.S0->TermsLeft),1,PF_LONG);
3756 fprintf(stderr,
"[%d] Now sending with tag = %d\n",PF.me,tag);
3763 fprintf(stderr,
"[%d] returning from send\n",PF.me);
3780static int PF_DoOneExpr(
void)
3789 WORD oldBracketOn = AR.BracketOn;
3790 WORD *oldBrackBuf = AT.BrackBuf;
3791 WORD oldbracketindexflag = AT.bracketindexflag;
3792 e = Expressions + PF.exprtodo;
3795 AR.SortType = AC.SortType;
3797 if ( ( e->vflags & ISFACTORIZED ) != 0 ) {
3799 AT.BrackBuf = AM.BracketFactors;
3800 AT.bracketindexflag = 1;
3803 position = AS.OldOnFile[i];
3804 if ( e->status == HIDDENLEXPRESSION || e->status == HIDDENGEXPRESSION
3805 || e->status == UNHIDELEXPRESSION || e->status == UNHIDEGEXPRESSION ) {
3806 AR.GetFile = 2; fi = AR.hidefile;
3809 AR.GetFile = 0; fi = AR.infile;
3817 SetScratch(fi,&position);
3818 term = AT.WorkPointer;
3819 AR.CompressPointer = AR.CompressBuffer;
3820 AR.CompressPointer[0] = 0;
3822 if ( GetTerm(BHEAD term) <= 0 ) {
3823 MesPrint(
"Expression %d has problems in scratchfile",i);
3826 if ( AT.bracketindexflag > 0 ) OpenBracketIndex(i);
3828 PUTZERO(outposition);
3830 fout->POfill = fout->POfull = fout->PObuffer;
3831 fout->POposition = outposition;
3832 if ( fout->
handle >= 0 ) {
3833 fout->POposition = outposition;
3845 if (
PutOut(BHEAD term,&outposition,fout,0) < 0 )
return -1;
3847 AR.DeferFlag = AC.ComDefer;
3850 term = AT.WorkPointer;
3852 AR.MaxDum = AM.IndDum;
3854 while ( GetTerm(BHEAD term) ) {
3855 SeekScratch(fi,&position);
3856 AN.ninterms++; dd = AN.deferskipped;
3857 if ( ( e->vflags & ISFACTORIZED ) != 0 && term[1] == HAAKJE ) {
3861 if ( AC.CollectFun && *term <= (AM.MaxTer/(2*(LONG)
sizeof(WORD))) ) {
3862 if ( GetMoreTerms(term) < 0 ) {
3865 SeekScratch(fi,&position);
3867 AT.WorkPointer = term + *term;
3868 AN.RepPoint = AT.RepCount + 1;
3869 if ( AR.DeferFlag ) {
3870 AR.CurDum = AN.IndDum = Expressions[PF.exprtodo].numdummies;
3873 AN.IndDum = AM.IndDum;
3874 AR.CurDum = ReNumber(BHEAD term);
3876 if ( AC.SymChangeFlag ) MarkDirty(term,DIRTYSYMFLAG);
3878 if ( ( AC.modmode & ALSOFUNARGS ) != 0 ) MarkDirty(term,DIRTYFLAG);
3879 else if ( AR.PolyFun ) PolyFunDirty(BHEAD term);
3881 else if ( AC.PolyRatFunChanged ) PolyFunDirty(BHEAD term);
3882 if ( ( AR.PolyFunType == 2 ) && ( AC.PolyRatFunChanged == 0 )
3883 && ( e->status == LOCALEXPRESSION || e->status == GLOBALEXPRESSION ) ) {
3884 PolyFunClean(BHEAD term);
3891 SetScratch(fi,&position);
3892 if ( fi == AR.hidefile ) {
3893 AR.InHiBuf = (fi->POfull-fi->PObuffer)
3894 -DIFBASE(position,fi->POposition)/
sizeof(WORD);
3897 AR.InInBuf = (fi->POfull-fi->PObuffer)
3898 -DIFBASE(position,fi->POposition)/
sizeof(WORD);
3902 if (
EndSort(BHEAD AM.S0->sBuffer,0) < 0 )
return(-1);
3903 e->numdummies = AR.MaxDum - AM.IndDum;
3904 AR.BracketOn = oldBracketOn;
3905 AT.BrackBuf = oldBrackBuf;
3906 if ( ( e->vflags & TOBEFACTORED ) != 0 )
3908 else if ( ( ( e->vflags & TOBEUNFACTORED ) != 0 )
3909 && ( ( e->vflags & ISFACTORIZED ) != 0 ) )
3911 if ( AM.S0->TermsLeft ) e->vflags &= ~ISZERO;
3912 else e->vflags |= ISZERO;
3913 if ( AR.expchanged == 0 ) e->vflags |= ISUNMODIFIED;
3917 AT.bracketindexflag = oldbracketindexflag;
3919 fout->POfull = fout->POfill;
3933static int PF_Slave2MasterIP(
int src)
3943 if(PF.me != MASTER){
3944 e = Expressions + PF.exprtodo;
3946 memcpy(&(exprData.e), e,
sizeof(
struct ExPrEsSiOn));
3947 SeekScratch(fout,&pos);
3948 exprData.i=BASEPOSITION(pos);
3953 SETBASEPOSITION(pos,0);
3955 int blen=PF.exprbufsize*
sizeof(WORD);
3958 l=PF_SendChunkIP(fout,&pos, MASTER, blen);
3965 if ( fout->
handle >= 0 ) {
3969 PUTZERO(fout->POposition);
3970 PUTZERO(fout->filesize);
3971 fout->POfill = fout->POfull = fout->PObuffer;
3974 if ( AC.numpfirstnum > 0 ) {
3976 PF_PackRedefinedPreVars();
3983 e = Expressions +partodoexr[src];
3986 PF_CatchErrorMessages(&src, &i);
3991 e->counter = exprData.e.counter;
3992 e->vflags = exprData.e.vflags;
3993 e->uflags = exprData.e.uflags;
3994 e->numdummies = exprData.e.numdummies;
3995 e->numfactors = exprData.e.numfactors;
3996 if ( !(e->vflags & ISZERO) ) AR.expflags |= ISZERO;
3997 if ( !(e->vflags & ISUNMODIFIED) ) AR.expflags |= ISUNMODIFIED;
3998 SeekScratch(fout,&pos);
4002 int blen=PF.exprbufsize*
sizeof(WORD);
4005 l=PF_RecvChunkIP(fout,src,blen);
4012 if ( AC.numpfirstnum > 0 ) {
4014 PF_UnpackRedefinedPreVars();
4024static int PF_Master2SlaveIP(
int dest,
EXPRESSIONS e)
4037 memcpy(&(exprData.e), e,
sizeof(
struct ExPrEsSiOn));
4038 exprData.i=e-Expressions;
4039 if ( AC.StatsFlag && AC.OldParallelStats ) {
4041 MesPrint(
" Sending expression %s to slave %d",EXPRNAME(exprData.i),dest);
4045 if ( e->status == HIDDENLEXPRESSION || e->status == HIDDENGEXPRESSION
4046 || e->status == UNHIDELEXPRESSION || e->status == UNHIDEGEXPRESSION )
4051 SetScratch(fi,&pos);
4053 l=PF_SendChunkIP(fi, &pos, dest, PF.exprbufsize*
sizeof(WORD));
4056 t=fi->PObuffer+ (DIFBASE(pos,fi->POposition))/
sizeof(WORD);
4057 ll=PF_WalkThrough(t,ll,l/
sizeof(WORD),&count);
4068static int PF_ReadMaster(
void)
4080 tag = PF_ANY_MSGTAG;
4081 PF_CatchErrorMessages(&m, &tag);
4085 if(tag == PF_EMPTY_MSGTAG)
4089 PF.exprtodo=exprData.i;
4090 e=Expressions + PF.exprtodo;
4093 if ( e->status == HIDDENLEXPRESSION || e->status == HIDDENGEXPRESSION
4094 || e->status == UNHIDELEXPRESSION || e->status == UNHIDEGEXPRESSION )
4098 SetEndHScratch(fi,&pos);
4099 e->onfile=AS.OldOnFile[PF.exprtodo]=pos;
4102 l=PF_RecvChunkIP(fi,MASTER,PF.exprbufsize*
sizeof(WORD));
4105 t=fi->POfull-l/
sizeof(WORD);
4106 ll=PF_WalkThrough(t,ll,l/
sizeof(WORD),&count);
4110 fi->POfill=fi->POfull;
4111 return(PF_DATA_MSGTAG);
4120static int PF_SendChunkIP(
FILEHANDLE *curfile,
POSITION *position,
int to, LONG thesize)
4124 ISLESSPOS(*position,curfile->POposition) ||
4125 ISGEPOSINC(*position,curfile->POposition,
4126 ((curfile->POfull-curfile->PObuffer)*
sizeof(WORD)-thesize) )
4129 l=(curfile->POfull-curfile->PObuffer)*
sizeof(WORD) - (LONG)(position->p1);
4131 PF_SetScratch(curfile,position);
4133 ISGEPOSINC(*position,curfile->POposition,
4134 ((curfile->POfull-curfile->PObuffer)*
sizeof(WORD)-thesize) )
4136 l=(curfile->POfull-curfile->PObuffer)*
sizeof(WORD) - (LONG)position->p1;
4141 if(
PF_RawSend(to,curfile->PObuffer+ (DIFBASE(*position,curfile->POposition))/
sizeof(WORD),l,0))
4152static int PF_RecvChunkIP(
FILEHANDLE *curfile,
int from, LONG thesize)
4156 if( (LONG)((curfile->POstop - curfile->POfull)*
sizeof(WORD)) < thesize )
4157 if(PF_pushScratch(curfile))
4162 receivedBytes=
PF_RawRecv(&from,curfile->POfull,thesize,&tag);
4164 if(receivedBytes >= 0 ){
4165 curfile->POfull+=receivedBytes/
sizeof(WORD);
4166 curfile->POfill=curfile->POfull;
4168 return(receivedBytes);
4184static int PF_WalkThrough(WORD *t, LONG l, LONG chunk, LONG *count)
4225#define PF_SNDFILEBUFSIZE 4096
4238 if(
PF_RawSend(to,&to,
sizeof(
int),PF_EMPTY_MSGTAG))
4243 char buf[PF_SNDFILEBUFSIZE];
4245 l=fread(buf, 1, PF_SNDFILEBUFSIZE, fd);
4247 if(l==PF_SNDFILEBUFSIZE){
4248 if(
PF_RawSend(to,buf,PF_SNDFILEBUFSIZE,PF_BUFFER_MSGTAG))
4277 char buf[PF_SNDFILEBUFSIZE];
4279 l=
PF_RawRecv(&from,buf,PF_SNDFILEBUFSIZE,&tag);
4282 if(tag == PF_EMPTY_MSGTAG)
4285 if( fwrite(buf,l,1,fd)!=1 )
4288 }
while(tag!=PF_ENDBUFFER_MSGTAG);
4330static int errorMessageLock = 0;
4331static Vector(UBYTE, stdoutBuffer);
4332static Vector(UBYTE, logBuffer);
4333#define recvBuffer logBuffer
4339#ifndef PF_ENABLE_STDOUT_BUFFERING
4341#define PF_ENABLE_STDOUT_BUFFERING
4355 assert(PF.me != MASTER);
4356 if ( errorMessageLock++ > 0 )
return;
4371 assert(PF.me != MASTER);
4372 if ( --errorMessageLock > 0 )
return;
4400 if ( PF.me != MASTER && errorMessageLock > 0 ) {
4401 if ( handle == AM.StdOut ) {
4405 else if ( handle == AC.LogHandle ) {
4410#ifdef PF_ENABLE_STDOUT_BUFFERING
4456 if ( PF.me == MASTER && handle == AM.StdOut ) {
4459 if ( (size > 0 && buffer[size - 1] != LINEFEED) || (size == 1 && buffer[0] == LINEFEED) ) {
4463 if ( (oldsize =
VectorSize(stdoutBuffer)) > 0 ) {
4471 else if ( ret < (LONG)oldsize ) {
4475 return ret - (LONG)oldsize;
4480 return WriteFileToFile(handle, buffer, size);
4494#ifdef PF_ENABLE_STDOUT_BUFFERING
4495 if ( PF.me == MASTER &&
VectorSize(stdoutBuffer) > 0 ) {
4515static void PF_ReceiveErrorMessage(
int src,
int tag)
4517 assert(PF.me == MASTER);
4522 case PF_STDOUT_MSGTAG:
4528 int handle = (tag == PF_STDOUT_MSGTAG) ? AM.StdOut : AC.LogHandle;
4529#ifdef PF_ENABLE_STDOUT_BUFFERING
4533 WriteFileToFile(handle,
VectorPtr(recvBuffer), size);
4553static void PF_CatchErrorMessages(
int *src,
int *tag)
4560 if ( atag == PF_STDOUT_MSGTAG || atag == PF_LOG_MSGTAG ) {
4561 assert(PF.me == MASTER);
4562 PF_ReceiveErrorMessage(asrc, atag);
4565 if ( atag == PF_RUNTIME_ERROR_MSGTAG ) {
4583static void PF_CatchErrorMessagesForAll(
void)
4585 assert(PF.me == MASTER);
4587 for ( i = 1; i < PF.numtasks; i++ ) {
4589 int tag = PF_ANY_MSGTAG;
4590 PF_CatchErrorMessages(&src, &tag);
4609static int PF_ProbeWithCatchingErrorMessages(
int *src)
4614 if ( tag == PF_STDOUT_MSGTAG || tag == PF_LOG_MSGTAG ) {
4615 assert(PF.me == MASTER);
4616 PF_ReceiveErrorMessage(newsrc, tag);
4619 if ( tag == PF_RUNTIME_ERROR_MSGTAG ) {
4655static void PF_RaiseRuntimeError(
void)
4657 if ( PF.me == MASTER ) {
4658 PF_BroadcastRuntimeError();
4662 ret =
PF_RawSend(MASTER, &dummy, 0, PF_RUNTIME_ERROR_MSGTAG);
4665 int tag = PF_RUNTIME_ERROR_MSGTAG;
4680static void PF_BroadcastRuntimeError(
void)
4682 assert(PF.me == MASTER);
4685 MPI_Request requests[PF.numtasks - 1];
4692 for (
int i = 1; i < PF.numtasks; i++ ) {
4693 ret =
PF_RawIsend(i, &dummy, 0, PF_BYTE, PF_RUNTIME_ERROR_MSGTAG, &requests[i - 1]);
4701 for (
int i = 1; i < PF.numtasks; i++ ) {
4703 int asrc = PF_ANY_SOURCE;
4706 assert(1 <= asrc && asrc < PF.numtasks);
4708 case PF_STDOUT_MSGTAG:
4710 PF_ReceiveErrorMessage(asrc, tag);
4712 case PF_RUNTIME_ERROR_MSGTAG:
4713 case PF_RUNTIME_SYNC_MSGTAG:
4724 ret =
PF_RawWaitAll(PF.numtasks - 1, requests, MPI_STATUSES_IGNORE);
4737void PF_PostEndSortBarrier(
void)
4739 assert(PF_processing);
4756 if ( PF.me == MASTER ) {
4758 for (
int i = 1; i < PF.numtasks; i++ ) {
4760 int asrc = PF_ANY_SOURCE;
4763 assert(1 <= asrc && asrc < PF.numtasks);
4765 case PF_STDOUT_MSGTAG:
4767 PF_ReceiveErrorMessage(asrc, tag);
4769 case PF_RUNTIME_ERROR_MSGTAG:
4774 case PF_RUNTIME_SYNC_MSGTAG:
4779 MesPrint(
"!!!Unexpected MPI message src=%d tag=%d.", asrc, tag);
4785 for (
int i = 1; i < PF.numtasks; i++ ) {
4786 ret =
PF_RawSend(i, &dummy, 0, error ? PF_RUNTIME_ERROR_MSGTAG : PF_RUNTIME_SYNC_MSGTAG);
4792 ret =
PF_RawSend(MASTER, &dummy, 0, PF_RUNTIME_SYNC_MSGTAG);
4798 case PF_RUNTIME_SYNC_MSGTAG:
4800 case PF_RUNTIME_ERROR_MSGTAG:
4805 MesPrint(
"!!!Unexpected MPI message src=%d tag=%d.", src, tag);
4824 if ( PF.me == MASTER ) {
4825 PF_BroadcastRuntimeError();
4830 int tag = PF_RUNTIME_ERROR_MSGTAG;
4833 ret =
PF_RawSend(MASTER, &dummy, 0, PF_RUNTIME_ERROR_MSGTAG);
void AddArgs(PHEAD WORD *, WORD *, WORD *)
WORD * poly_ratfun_add(PHEAD WORD *, WORD *)
int poly_unfactorize_expression(EXPRESSIONS)
WORD CompCoef(WORD *, WORD *)
WORD PutOut(PHEAD WORD *, POSITION *, FILEHANDLE *, WORD)
LONG EndSort(PHEAD WORD *, int)
int Generator(PHEAD WORD *, WORD)
void LowerSortLevel(void)
int StoreTerm(PHEAD WORD *)
int poly_factorize_expression(EXPRESSIONS)
void WriteStats(POSITION *, WORD, WORD)
int NormalModulus(UWORD *, WORD *)
int InsertTerm(PHEAD WORD *, WORD, WORD, WORD *, WORD *, WORD)
int FlushOut(POSITION *, FILEHANDLE *, int)
int PutPreVar(UBYTE *, UBYTE *, UBYTE *, int)
int PF_LongSingleReceive(int src, int tag, int *psrc, int *ptag)
int PF_PackString(const UBYTE *str)
int PF_LongSingleSend(int to, int tag)
int PF_PrepareLongSinglePack(void)
int PF_Unpack(void *buffer, size_t count, MPI_Datatype type)
int PF_Receive(int src, int tag, int *psrc, int *ptag)
int PF_Send(int to, int tag)
int PF_Reduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype type, MPI_Op op, int root)
int PF_LongSingleUnpack(void *buffer, size_t count, MPI_Datatype type)
int PF_Pack(const void *buffer, size_t count, MPI_Datatype type)
int PF_PrepareLongMultiPack(void)
int PF_LongMultiBroadcast(void)
int PF_UnpackString(UBYTE *str)
int PF_LongSinglePack(const void *buffer, size_t count, MPI_Datatype type)
int PF_Bcast(void *buffer, int count)
int PF_ISendSbuf(int to, int tag)
int PF_WaitRbuf(PF_BUFFER *, int, LONG *)
void PF_FreeErrorMessageBuffers(void)
WORD PF_Deferred(WORD *term, WORD level)
int PF_BroadcastRedefinedPreVars(void)
int PF_Init(int *argc, char ***argv)
int PF_BroadcastRHS(void)
LONG PF_GetSlaveTimes(void)
int PF_BroadcastExpFlags(void)
int PF_BroadcastPreDollar(WORD **dbuffer, LONG *newsize, int *numterms)
int PF_BroadcastModifiedDollars(void)
int PF_RawIsend(int dest, const void *buf, int count, MPI_Datatype type, int tag, MPI_Request *request)
void PF_PreTerminate(int errorcode)
int PF_RecvWbuf(WORD *, LONG *, int *)
int PF_BroadcastCBuf(int bufnum)
void PF_BroadcastBuffer(WORD **buffer, LONG *length)
LONG PF_RawRecv(int *src, void *buf, LONG thesize, int *tag)
int PF_CollectModifiedDollars(void)
int PF_RawWaitAll(int count, MPI_Request *request, MPI_Status *status)
LONG PF_WriteFileToFile(int handle, UBYTE *buffer, LONG size)
int PF_RawProbe(int *src, int *tag, int *bytesize)
int PF_RecvFile(int from, FILE *fd)
int PF_IRecvRbuf(PF_BUFFER *, int, int)
int PF_BroadcastString(UBYTE *str)
int PF_Terminate(int errorcode)
int PF_Discard(int *src, int *tag)
void PF_FlushStdOutBuffer(void)
int PF_RawSend(int dest, void *buf, LONG l, int tag)
LONG PF_BroadcastNumber(LONG x)
int PF_LibInit(int *, char ***)
int PF_SendFile(int to, FILE *fd)
int PF_Processor(EXPRESSIONS e, WORD i, WORD LastExpression)
int PF_BroadcastExpr(EXPRESSIONS e, FILEHANDLE *file)
#define UNPACK_LONG(p, n)
void PF_ReceiveRuntimeError(void)
int PF_InParallelProcessor(void)
#define VectorReserve(X, newcapacity)
#define VectorPushBack(X, x)
#define VectorPushBacks(X, src, n)