Annotation of OpenXM_contrib2/asir2000/io/ox.c, Revision 1.8
1.5 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.6 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.5 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
1.8 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/ox.c,v 1.7 2000/09/07 23:59:55 noro Exp $
1.5 noro 48: */
1.1 noro 49: #include "ca.h"
50: #include "parse.h"
1.3 noro 51: #include "signal.h"
1.1 noro 52: #include "wsio.h"
53: #include "ox.h"
54:
55: #define ISIZ sizeof(int)
56:
57: void ox_flush_stream(),ox_write_int(),ox_write_cmo();
58: void ox_read_int(),ox_read_cmo();
59: void mclist_to_mc();
60: void ox_read_local();
61:
62: extern Obj VOIDobj;
63:
64: extern int ox_need_conv;
65: extern int ox_usr1_sent, ox_int_received, critical_when_signal;
66: unsigned int ox_serial;
67: int ox_flushing;
68: int ox_batch;
69: int ox_check=1;
1.2 noro 70: int ox_exchange_mathcap=1;
1.1 noro 71: jmp_buf ox_env;
72:
73: MATHCAP my_mathcap;
74:
75: struct oxcap {
76: unsigned int ox;
77: int ncap;
78: int *cap;
79: };
80:
81: struct mathcap {
82: LIST mc;
83: unsigned int version;
84: char *servername;
85: int nsmcap;
86: unsigned int *smcap;
87: int noxcap;
88: struct oxcap *oxcap;
89: };
90:
91: struct oxcap *my_oxcap;
92:
93: static struct mathcap my_mc;
94: static struct mathcap *remote_mc;
95: static int remote_mc_len;
96:
97: void ox_resetenv(s)
98: char *s;
99: {
100: fprintf(stderr,"%s\n",s);
101: longjmp(ox_env,1);
102: }
103:
104: static int available_cmo[] = {
105: CMO_NULL, CMO_INT32, CMO_DATUM, CMO_STRING, CMO_MATHCAP,
106: CMO_ERROR, CMO_ERROR2, CMO_LIST, CMO_MONOMIAL32,
107: CMO_ZZ, CMO_QQ, CMO_ZERO,
108: CMO_DMS_GENERIC, CMO_DMS_OF_N_VARIABLES,
109: CMO_RING_BY_NAME, CMO_DISTRIBUTED_POLYNOMIAL,
110: CMO_RECURSIVE_POLYNOMIAL, CMO_UNIVARIATE_POLYNOMIAL,
111: CMO_INDETERMINATE,
112: 0
113: };
114:
115: static int available_sm[] = {
116: SM_dupErrors, SM_getsp, SM_popSerializedLocalObject,
1.7 noro 117: SM_popCMO, SM_popString, SM_pushCMOtag, SM_setName,
1.1 noro 118: SM_evalName, SM_executeStringByLocalParser,
119: SM_executeStringByLocalParserInBatchMode,
120: SM_executeFunction, SM_shutdown, SM_pops,
121: SM_mathcap, SM_setMathcap, SM_nop,
122: SM_beginBlock, SM_endBlock,
123: 0
124: };
125:
126: /*
127: mathcap =
128: [
129: version list,
130: SMlist,
131: [
132: [OX tag,CMO tag list],
133: [OX tag,CMO tag list],
134: ...
135: ]
136: ]
137: */
138:
139: void create_my_mathcap(char *system)
140: {
141: NODE n,n0;
142: int i,k;
143: STRING str;
144: LIST sname,smlist,oxlist,cmolist,asirlist,oxtag,oxasir,r;
145: USINT tag,t,t1;
146:
147: if ( my_mathcap )
148: return;
149: /* version */
150: MKSTR(str,system);
151: MKUSINT(t,OX_VERSION);
152: n0 = mknode(2,t,str); MKLIST(sname,n0);
153:
154: /* cmo tag */
155: for ( n0 = 0, i = 0; k = available_sm[i]; i++ ) {
156: NEXTNODE(n0,n); MKUSINT(t,k); BDY(n) = (pointer)t;
157: }
158: NEXT(n) = 0; MKLIST(smlist,n0);
159:
160: /* creation of [OX_DATA,CMO list] */
161: /* ox tag */
162: MKUSINT(tag,OX_DATA);
163: /* cmo tag */
164: for ( n0 = 0, i = 0; k = available_cmo[i]; i++ ) {
165: NEXTNODE(n0,n); MKUSINT(t,k); BDY(n) = (pointer)t;
166: }
167: NEXT(n) = 0; MKLIST(cmolist,n0);
168: /* [ox tag, cmo list] */
169: n0 = mknode(2,tag,cmolist);
170: MKLIST(oxlist,n0);
171:
172: /* creation of [OX_LOCAL_OBJECT_ASIR,ASIR tag] */
173: /* ox tag */
174: MKUSINT(tag,OX_LOCAL_OBJECT_ASIR);
175: /* local tag */
176: MKUSINT(t,ASIR_VL);
177: MKUSINT(t1,ASIR_OBJ);
178: n0 = mknode(2,t,t1); MKLIST(cmolist,n0);
179: /* [ox tag, local list] */
180: n0 = mknode(2,tag,cmolist);
181: MKLIST(asirlist,n0);
182:
183: /* [oxlist,asirlist] */
184: n0 = mknode(2,oxlist,asirlist); MKLIST(oxasir,n0);
185:
186: /* [version,sm,oxasir] */
187: n0 = mknode(3,sname,smlist,oxasir); MKLIST(r,n0);
188:
189: MKMATHCAP(my_mathcap,r);
190: mclist_to_mc(r,&my_mc);
191: my_oxcap = my_mc.oxcap;
192: }
193:
194: void store_remote_mathcap(int s,MATHCAP mc)
195: {
196: if ( !remote_mc ) {
197: remote_mc_len = 16;
198: remote_mc = (struct mathcap *)
199: CALLOC(remote_mc_len,sizeof(struct mathcap));
200: }
201: if ( s >= remote_mc_len ) {
202: remote_mc_len *= 2;
203: remote_mc = (struct mathcap *)REALLOC(remote_mc,
204: remote_mc_len*sizeof(struct mathcap));
205: }
206: mclist_to_mc(BDY(mc),&remote_mc[s]);
207: }
208:
209: /*
210: mathcap =
211: [
212: version list,
213: SMlist,
214: [
215: [OX tag,CMO tag list],
216: [OX tag,CMO tag list],
217: ...
218: ]
219: ]
220:
221: ===>
222:
223: mathcap
224: | version | &servername | nsmcap | &smcap | noxcap | &oxcap |
225: smcap
226: | SM_xxx | SM_yyy | ... |
227: oxcap
228: | oxcap[0] | oxcap[1] | ... |
229: oxcap[i]
230: | ox | ncap | &cap |
231: cap
232: | CMO_xxx | CMO_yyy | ... |
233: */
234:
235: void mclist_to_mc(LIST mclist,struct mathcap *mc)
236: {
237: int id,l,i,j;
238: NODE n,t,oxcmo,ox,cap;
239: int *ptr;
240:
241: /*
242: [
243: [ version,servername ]
244: [sm1,sm2,...],
245: [
246: [o1,[n11,n12,...]],
247: [o2,[n21,n22,...]],
248: ...
249: ]
250: ]
251: */
252: n = BDY(mclist);
253: mc->mc = mclist;
254: mc->version = BDY((USINT)BDY(BDY((LIST)BDY(n))));
255: mc->servername = BDY((STRING)BDY(NEXT(BDY((LIST)BDY(n)))));
256:
257: /* smcap */
258: n = NEXT(n);
259: t = BDY((LIST)BDY(n));
260: mc->nsmcap = length(t);
261: mc->smcap = (int *)MALLOC_ATOMIC(mc->nsmcap*sizeof(int));
262: for ( j = 0, ptr = mc->smcap; j < mc->nsmcap; j++, t = NEXT(t) )
263: ptr[j] = BDY((USINT)BDY(t));
264:
265: n = NEXT(n);
266: n = BDY((LIST)BDY(n));
267: /* n -> BDY([[OX1,CMOlist1], [OX2,CMOlist2], ...]) */
268: mc->noxcap = length(n);
269: mc->oxcap = (struct oxcap *)MALLOC(mc->noxcap*sizeof(struct oxcap));
270: for ( j = 0; j < mc->noxcap; j++, n = NEXT(n) ) {
271: oxcmo = BDY((LIST)BDY(n));
272: /* oxcmo = BDY([OXj,CMOlistj]) */
273: mc->oxcap[j].ox = BDY((USINT)BDY(oxcmo));
274: cap = BDY((LIST)BDY(NEXT(oxcmo)));
275: /* cap ->BDY(CMOlistj) */
276: l = length(cap);
277: mc->oxcap[j].ncap = l;
278: mc->oxcap[j].cap = (unsigned int *)CALLOC(l+1,sizeof(unsigned int));
279: for ( t = cap, ptr = mc->oxcap[j].cap, i = 0; i < l; t = NEXT(t), i++ )
280: ptr[i] = BDY((USINT)BDY(t));
281: }
282: }
283:
284: int check_sm_by_mc(s,smtag)
285: int s;
286: unsigned int smtag;
287: {
288: struct mathcap *rmc;
289: int nsmcap,i;
290: unsigned int *smcap;
291:
292: /* XXX : return 1 if remote_mc is not available. */
293: if ( !remote_mc )
294: return 1;
295: rmc = &remote_mc[s];
296: nsmcap = rmc->nsmcap;
297: smcap = rmc->smcap;
298: if ( !smcap )
299: return 1;
300: for ( i = 0; i < nsmcap; i++ )
301: if ( smcap[i] == smtag )
302: break;
303: if ( i == nsmcap )
304: return 0;
305: else
306: return 1;
307: }
308:
309: int check_by_mc(s,oxtag,cmotag)
310: int s;
311: unsigned int oxtag,cmotag;
312: {
313: struct mathcap *rmc;
314: int noxcap,ncap,i,j;
315: struct oxcap *oxcap;
316: unsigned int *cap;
317:
318: /* XXX : return 1 if remote_mc is not available. */
319: if ( !remote_mc )
320: return 1;
321: rmc = &remote_mc[s];
322: noxcap = rmc->noxcap;
323: oxcap = rmc->oxcap;
324: if ( !oxcap )
325: return 1;
326: for ( i = 0; i < noxcap; i++ )
327: if ( oxcap[i].ox == oxtag )
328: break;
329: if ( i == noxcap )
330: return 0;
331: ncap = oxcap[i].ncap;
332: cap = oxcap[i].cap;
333: for ( j = 0; j < ncap; j++ )
334: if ( cap[j] == cmotag )
335: break;
336: if ( j == ncap )
337: return 0;
338: else
339: return 1;
340: }
341:
342: void begin_critical() {
343: critical_when_signal = 1;
344: }
345:
346: void end_critical() {
347: critical_when_signal = 0;
348: if ( ox_usr1_sent ) {
349: ox_usr1_sent = 0; ox_usr1_handler();
350: }
351: if ( ox_int_received ) {
352: ox_int_received = 0; int_handler(SIGINT);
353: }
354: }
355:
356: void ox_usr1_handler(sig)
357: int sig;
358: {
359: extern jmp_buf env;
360: unsigned int cmd;
361:
362: #if !defined(VISUAL)
363: signal(SIGUSR1,ox_usr1_handler);
364: #endif
365: if ( critical_when_signal ) {
366: fprintf(stderr,"usr1 : critical\n");
367: ox_usr1_sent = 1;
368: } else {
369: ox_flushing = 1;
370: ox_resetenv("usr1 : return to toplevel by SIGUSR1");
371: }
372: }
373:
374: void clear_readbuffer()
375: {
376: char c;
377: fd_set r,w,e;
378: struct timeval interval;
379: int n,sock;
380:
381: #if defined(linux)
382: iofp[0].in->_IO_read_ptr = iofp[0].in->_IO_read_end;
383: #elif defined(__FreeBSD__)
384: fpurge(iofp[0].in);
385: #endif
386: /*
387: sock = fileno(iofp[0].in);
388: interval.tv_sec = (int)0;
389: interval.tv_usec = (int)0;
390:
391: FD_ZERO(&r); FD_ZERO(&w); FD_ZERO(&e);
392: FD_SET(sock,&r);
393: while ( 1 ) {
394: n = select(FD_SETSIZE,&r,&w,&e,&interval);
395: if ( !n )
396: break;
397: read(sock,&c,1);
398: }
399: */
400: }
401:
402: #if MPI
403: int ox_data_is_available(int s)
404: {
405: return 1;
406: }
407:
408: void wait_for_data(int s)
409: {
410: return;
411: }
412: #else
413: int ox_data_is_available(int s)
414: {
415: return FP_DATA_IS_AVAILABLE(iofp[s].in);
416: }
417:
418: void wait_for_data(int s)
419: {
420: fd_set r;
421: int sock;
422:
423: if ( !FP_DATA_IS_AVAILABLE(iofp[s].in) ) {
424: #if defined(VISUAL)
425: sock = iofp[s].in->fildes;
426: FD_ZERO(&r);
427: FD_SET(sock,&r);
428: select(0,&r,NULL,NULL,NULL);
429: #else
430: sock = fileno(iofp[s].in);
431: FD_ZERO(&r);
432: FD_SET(sock,&r);
433: select(FD_SETSIZE,&r,NULL,NULL,NULL);
434: #endif
435: }
436: }
437: #endif
438:
439: void ox_send_data(int s,pointer p)
440: {
1.8 ! noro 441: ERR err;
! 442:
! 443: if ( ox_check && !ox_check_cmo(s,(Obj)p) ) {
! 444: create_error(&err,ox_serial,"ox_send_data : Mathcap violation");
! 445: p = (pointer)err;
! 446: }
1.1 noro 447: begin_critical();
448: ox_write_int(s,OX_DATA);
449: ox_write_int(s,ox_serial++);
450: ox_write_cmo(s,p);
451: ox_flush_stream(s);
452: end_critical();
453: }
454:
455: void ox_send_cmd(int s,int id)
456: {
457: if ( ox_check && !check_sm_by_mc(s,id) )
458: error("ox_send_cmd : Mathcap violation");
459: begin_critical();
460: ox_write_int(s,OX_COMMAND);
461: ox_write_int(s,ox_serial++);
462: ox_write_int(s,id);
463: ox_flush_stream(s);
464: end_critical();
465: }
466:
467: void ox_send_sync(int s)
468: {
469: begin_critical();
470: ox_write_int(s,OX_SYNC_BALL);
471: ox_write_int(s,ox_serial++);
472: ox_flush_stream(s);
473: end_critical();
474: }
475:
476: void ox_send_local_data(int s,Obj p)
477: {
478: begin_critical();
479: ox_write_int(s,OX_LOCAL_OBJECT_ASIR);
480: ox_write_int(s,ox_serial++);
481: ox_write_int(s,ASIR_OBJ);
482: saveobj(iofp[s].out,p);
483: ox_flush_stream(s);
484: end_critical();
485: }
486:
487: void ox_send_local_ring(int s,VL vl)
488: {
489: begin_critical();
490: ox_write_int(s,OX_LOCAL_OBJECT_ASIR);
491: ox_write_int(s,ox_serial++);
492: ox_write_int(s,ASIR_VL);
493: savevl(iofp[s].out,vl);
494: ox_flush_stream(s);
495: end_critical();
496: }
497:
498: unsigned int ox_recv(int s, int *id, pointer *p)
499: {
500: unsigned int cmd,serial;
501: USINT ui;
502:
503: wait_for_data(s);
504: begin_critical();
505: ox_read_int(s,id);
506: ox_read_int(s,&serial);
507: switch ( *id ) {
508: case OX_COMMAND:
509: ox_read_int(s,&cmd);
510: MKUSINT(ui,cmd);
511: *p = (pointer)ui;
512: break;
513: case OX_DATA:
514: ox_read_cmo(s,p);
515: break;
516: case OX_LOCAL_OBJECT_ASIR:
517: ox_read_local(s,p);
518: break;
519: default:
520: *p = 0;
521: break;
522: }
523: end_critical();
524: return serial;
525: }
526:
527: void ox_get_result(s,rp)
528: int s;
529: Obj *rp;
530: {
531: int id;
532: Obj obj,r;
533: int level;
534:
535: level = 0;
536: r = 0;
537: do {
538: ox_recv(s,&id,(pointer *)&obj);
539: if ( id == OX_COMMAND ) {
540: switch ( ((USINT)obj)->body ) {
541: case SM_beginBlock:
542: level++;
543: break;
544: case SM_endBlock:
545: level--;
546: }
547: } else
548: r = obj;
549: } while ( level );
550: *rp = r;
551: }
552:
553: void ox_read_int(int s, int *n)
554: {
555: ox_need_conv = iofp[s].conv;
556: read_int(iofp[s].in,n);
557: }
558:
559: void ox_read_cmo(int s, Obj *rp)
560: {
561: ox_need_conv = iofp[s].conv;
562: read_cmo(iofp[s].in,rp);
563: }
564:
565: void ox_read_local(int s, Obj *rp)
566: {
567: int id;
568:
569: ox_need_conv = iofp[s].conv;
570: read_int(iofp[s].in,&id);
571: switch ( id ) {
572: case ASIR_VL:
573: loadvl(iofp[s].in);
574: *rp = VOIDobj;
575: break;
576: case ASIR_OBJ:
577: loadobj(iofp[s].in,rp);
578: break;
579: default:
580: error("ox_read_local : unsupported id");
581: break;
582: }
583: }
584:
585: void ox_write_int(int s, int n)
586: {
587: ox_need_conv = iofp[s].conv;
588: write_int(iofp[s].out,&n);
589: }
590:
591: void ox_write_cmo(int s, Obj obj)
592: {
593: ox_need_conv = iofp[s].conv;
594: write_cmo(iofp[s].out,obj);
595: }
596:
597: int ox_check_cmo(int s, Obj obj)
598: {
599: NODE m;
600:
601: if ( !obj )
602: return 1;
603: switch ( OID(obj) ) {
604: case O_MATHCAP: case O_STR: case O_ERR: case O_USINT: case O_VOID:
605: return 1;
606: case O_P:
607: if ( !check_by_mc(s,OX_DATA,CMO_RECURSIVE_POLYNOMIAL) )
608: return 0;
609: else
610: return ox_check_cmo_p(s,(P)obj);
611: case O_R:
612: if ( !check_by_mc(s,OX_DATA,CMO_RATIONAL) )
613: return 0;
614: else if ( !check_by_mc(s,OX_DATA,CMO_RECURSIVE_POLYNOMIAL) )
615: return 0;
616: else
617: return ox_check_cmo_p(s,NM((R)obj)) && ox_check_cmo_p(s,DN((R)obj));
618: case O_DP:
619: return ox_check_cmo_dp(s,(DP)obj);
620: case O_N:
1.4 noro 621: switch ( NID((Num)obj) ) {
622: case N_Q:
623: if ( INT((Q)obj) )
624: return check_by_mc(s,OX_DATA,CMO_ZZ);
625: else
626: return check_by_mc(s,OX_DATA,CMO_QQ);
627: case N_R:
628: return 1;
629: default:
630: return 0;
631: }
632: break;
1.1 noro 633: case O_LIST:
634: for ( m = BDY((LIST)obj); m; m = NEXT(m) )
635: if ( !ox_check_cmo(s,(BDY(m))) )
636: return 0;
637: return 1;
638: default:
639: return 0;
640: }
641: }
642:
643: void ox_get_serverinfo(int s, LIST *rp)
644: {
645: if ( remote_mc )
646: *rp = remote_mc[s].mc;
647: else {
648: MKLIST(*rp,0);
649: }
650: }
651:
652: int ox_check_cmo_p(int s, P p)
653: {
654: DCP dc;
655:
656: if ( NUM(p) )
657: return ox_check_cmo(s,(Obj)p);
658: else {
659: for ( dc = DC(p); dc; dc = NEXT(dc) )
660: if ( !ox_check_cmo_p(s,COEF(dc)) )
661: return 0;
662: return 1;
663: }
664: }
665:
666: int ox_check_cmo_dp(int s, DP p)
667: {
668: MP m;
669:
670: for ( m = BDY(p); m; m = NEXT(m) )
671: if ( !ox_check_cmo(s,(Obj)m->c) )
672: return 0;
673: return 1;
674: }
675:
676: void ox_flush_stream(s)
677: int s;
678: {
679: if ( ox_batch )
680: return;
681: #if defined(VISUAL)
682: if ( _fileno(&iofp[s].out->fp) < 0 )
683: cflush(iofp[s].out);
684: else
685: #elif MPI
686: if ( (char)fileno(&iofp[s].out->fp) < 0 )
687: cflush(iofp[s].out);
688: else
689: #endif
690: fflush(iofp[s].out);
691: }
692:
693: void ox_flush_stream_force(s)
694: int s;
695: {
696: #if defined(VISUAL)
697: if ( _fileno(&iofp[s].out->fp) < 0 )
698: cflush(iofp[s].out);
699: else
700: #elif MPI
701: if ( (char)fileno(&iofp[s].out->fp) < 0 )
702: cflush(iofp[s].out);
703: else
704: #endif
705: fflush(iofp[s].out);
706: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>