Annotation of OpenXM_contrib2/asir2000/io/ox.c, Revision 1.5
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
! 26: * e-mail at risa-admin@flab.fujitsu.co.jp of the detailed specification
! 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.
! 47: * $OpenXM: OpenXM_contrib2/asir2000/io/ox.c,v 1.4 2000/03/28 06:32:22 noro Exp $
! 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,
117: SM_popCMO, SM_popString, SM_setName,
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: {
441: if ( ox_check && !ox_check_cmo(s,(Obj)p) )
442: error("ox_send_data : Mathcap violation");
443: begin_critical();
444: ox_write_int(s,OX_DATA);
445: ox_write_int(s,ox_serial++);
446: ox_write_cmo(s,p);
447: ox_flush_stream(s);
448: end_critical();
449: }
450:
451: void ox_send_cmd(int s,int id)
452: {
453: if ( ox_check && !check_sm_by_mc(s,id) )
454: error("ox_send_cmd : Mathcap violation");
455: begin_critical();
456: ox_write_int(s,OX_COMMAND);
457: ox_write_int(s,ox_serial++);
458: ox_write_int(s,id);
459: ox_flush_stream(s);
460: end_critical();
461: }
462:
463: void ox_send_sync(int s)
464: {
465: begin_critical();
466: ox_write_int(s,OX_SYNC_BALL);
467: ox_write_int(s,ox_serial++);
468: ox_flush_stream(s);
469: end_critical();
470: }
471:
472: void ox_send_local_data(int s,Obj p)
473: {
474: begin_critical();
475: ox_write_int(s,OX_LOCAL_OBJECT_ASIR);
476: ox_write_int(s,ox_serial++);
477: ox_write_int(s,ASIR_OBJ);
478: saveobj(iofp[s].out,p);
479: ox_flush_stream(s);
480: end_critical();
481: }
482:
483: void ox_send_local_ring(int s,VL vl)
484: {
485: begin_critical();
486: ox_write_int(s,OX_LOCAL_OBJECT_ASIR);
487: ox_write_int(s,ox_serial++);
488: ox_write_int(s,ASIR_VL);
489: savevl(iofp[s].out,vl);
490: ox_flush_stream(s);
491: end_critical();
492: }
493:
494: unsigned int ox_recv(int s, int *id, pointer *p)
495: {
496: unsigned int cmd,serial;
497: USINT ui;
498:
499: wait_for_data(s);
500: begin_critical();
501: ox_read_int(s,id);
502: ox_read_int(s,&serial);
503: switch ( *id ) {
504: case OX_COMMAND:
505: ox_read_int(s,&cmd);
506: MKUSINT(ui,cmd);
507: *p = (pointer)ui;
508: break;
509: case OX_DATA:
510: ox_read_cmo(s,p);
511: break;
512: case OX_LOCAL_OBJECT_ASIR:
513: ox_read_local(s,p);
514: break;
515: default:
516: *p = 0;
517: break;
518: }
519: end_critical();
520: return serial;
521: }
522:
523: void ox_get_result(s,rp)
524: int s;
525: Obj *rp;
526: {
527: int id;
528: Obj obj,r;
529: int level;
530:
531: level = 0;
532: r = 0;
533: do {
534: ox_recv(s,&id,(pointer *)&obj);
535: if ( id == OX_COMMAND ) {
536: switch ( ((USINT)obj)->body ) {
537: case SM_beginBlock:
538: level++;
539: break;
540: case SM_endBlock:
541: level--;
542: }
543: } else
544: r = obj;
545: } while ( level );
546: *rp = r;
547: }
548:
549: void ox_read_int(int s, int *n)
550: {
551: ox_need_conv = iofp[s].conv;
552: read_int(iofp[s].in,n);
553: }
554:
555: void ox_read_cmo(int s, Obj *rp)
556: {
557: ox_need_conv = iofp[s].conv;
558: read_cmo(iofp[s].in,rp);
559: }
560:
561: void ox_read_local(int s, Obj *rp)
562: {
563: int id;
564:
565: ox_need_conv = iofp[s].conv;
566: read_int(iofp[s].in,&id);
567: switch ( id ) {
568: case ASIR_VL:
569: loadvl(iofp[s].in);
570: *rp = VOIDobj;
571: break;
572: case ASIR_OBJ:
573: loadobj(iofp[s].in,rp);
574: break;
575: default:
576: error("ox_read_local : unsupported id");
577: break;
578: }
579: }
580:
581: void ox_write_int(int s, int n)
582: {
583: ox_need_conv = iofp[s].conv;
584: write_int(iofp[s].out,&n);
585: }
586:
587: void ox_write_cmo(int s, Obj obj)
588: {
589: ox_need_conv = iofp[s].conv;
590: write_cmo(iofp[s].out,obj);
591: }
592:
593: int ox_check_cmo(int s, Obj obj)
594: {
595: NODE m;
596:
597: if ( !obj )
598: return 1;
599: switch ( OID(obj) ) {
600: case O_MATHCAP: case O_STR: case O_ERR: case O_USINT: case O_VOID:
601: return 1;
602: case O_P:
603: if ( !check_by_mc(s,OX_DATA,CMO_RECURSIVE_POLYNOMIAL) )
604: return 0;
605: else
606: return ox_check_cmo_p(s,(P)obj);
607: case O_R:
608: if ( !check_by_mc(s,OX_DATA,CMO_RATIONAL) )
609: return 0;
610: else if ( !check_by_mc(s,OX_DATA,CMO_RECURSIVE_POLYNOMIAL) )
611: return 0;
612: else
613: return ox_check_cmo_p(s,NM((R)obj)) && ox_check_cmo_p(s,DN((R)obj));
614: case O_DP:
615: return ox_check_cmo_dp(s,(DP)obj);
616: case O_N:
1.4 noro 617: switch ( NID((Num)obj) ) {
618: case N_Q:
619: if ( INT((Q)obj) )
620: return check_by_mc(s,OX_DATA,CMO_ZZ);
621: else
622: return check_by_mc(s,OX_DATA,CMO_QQ);
623: case N_R:
624: return 1;
625: default:
626: return 0;
627: }
628: break;
1.1 noro 629: case O_LIST:
630: for ( m = BDY((LIST)obj); m; m = NEXT(m) )
631: if ( !ox_check_cmo(s,(BDY(m))) )
632: return 0;
633: return 1;
634: default:
635: return 0;
636: }
637: }
638:
639: void ox_get_serverinfo(int s, LIST *rp)
640: {
641: if ( remote_mc )
642: *rp = remote_mc[s].mc;
643: else {
644: MKLIST(*rp,0);
645: }
646: }
647:
648: int ox_check_cmo_p(int s, P p)
649: {
650: DCP dc;
651:
652: if ( NUM(p) )
653: return ox_check_cmo(s,(Obj)p);
654: else {
655: for ( dc = DC(p); dc; dc = NEXT(dc) )
656: if ( !ox_check_cmo_p(s,COEF(dc)) )
657: return 0;
658: return 1;
659: }
660: }
661:
662: int ox_check_cmo_dp(int s, DP p)
663: {
664: MP m;
665:
666: for ( m = BDY(p); m; m = NEXT(m) )
667: if ( !ox_check_cmo(s,(Obj)m->c) )
668: return 0;
669: return 1;
670: }
671:
672: void ox_flush_stream(s)
673: int s;
674: {
675: if ( ox_batch )
676: return;
677: #if defined(VISUAL)
678: if ( _fileno(&iofp[s].out->fp) < 0 )
679: cflush(iofp[s].out);
680: else
681: #elif MPI
682: if ( (char)fileno(&iofp[s].out->fp) < 0 )
683: cflush(iofp[s].out);
684: else
685: #endif
686: fflush(iofp[s].out);
687: }
688:
689: void ox_flush_stream_force(s)
690: int s;
691: {
692: #if defined(VISUAL)
693: if ( _fileno(&iofp[s].out->fp) < 0 )
694: cflush(iofp[s].out);
695: else
696: #elif MPI
697: if ( (char)fileno(&iofp[s].out->fp) < 0 )
698: cflush(iofp[s].out);
699: else
700: #endif
701: fflush(iofp[s].out);
702: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>