Annotation of OpenXM_contrib2/asir2000/io/ox.c, Revision 1.1.1.1
1.1 noro 1: /* $OpenXM: OpenXM/src/asir99/io/ox.c,v 1.7 1999/11/19 04:15:49 noro Exp $ */
2: #include "ca.h"
3: #include "parse.h"
4: #include "wsio.h"
5: #include "ox.h"
6:
7: #define ISIZ sizeof(int)
8:
9: void ox_flush_stream(),ox_write_int(),ox_write_cmo();
10: void ox_read_int(),ox_read_cmo();
11: void mclist_to_mc();
12: void ox_read_local();
13:
14: extern Obj VOIDobj;
15:
16: extern int ox_need_conv;
17: extern int ox_usr1_sent, ox_int_received, critical_when_signal;
18: unsigned int ox_serial;
19: int ox_flushing;
20: int ox_batch;
21: int ox_check=1;
22: jmp_buf ox_env;
23:
24: MATHCAP my_mathcap;
25:
26: struct oxcap {
27: unsigned int ox;
28: int ncap;
29: int *cap;
30: };
31:
32: struct mathcap {
33: LIST mc;
34: unsigned int version;
35: char *servername;
36: int nsmcap;
37: unsigned int *smcap;
38: int noxcap;
39: struct oxcap *oxcap;
40: };
41:
42: struct oxcap *my_oxcap;
43:
44: static struct mathcap my_mc;
45: static struct mathcap *remote_mc;
46: static int remote_mc_len;
47:
48: void ox_resetenv(s)
49: char *s;
50: {
51: fprintf(stderr,"%s\n",s);
52: longjmp(ox_env,1);
53: }
54:
55: static int available_cmo[] = {
56: CMO_NULL, CMO_INT32, CMO_DATUM, CMO_STRING, CMO_MATHCAP,
57: CMO_ERROR, CMO_ERROR2, CMO_LIST, CMO_MONOMIAL32,
58: CMO_ZZ, CMO_QQ, CMO_ZERO,
59: CMO_DMS_GENERIC, CMO_DMS_OF_N_VARIABLES,
60: CMO_RING_BY_NAME, CMO_DISTRIBUTED_POLYNOMIAL,
61: CMO_RECURSIVE_POLYNOMIAL, CMO_UNIVARIATE_POLYNOMIAL,
62: CMO_INDETERMINATE,
63: 0
64: };
65:
66: static int available_sm[] = {
67: SM_dupErrors, SM_getsp, SM_popSerializedLocalObject,
68: SM_popCMO, SM_popString, SM_setName,
69: SM_evalName, SM_executeStringByLocalParser,
70: SM_executeStringByLocalParserInBatchMode,
71: SM_executeFunction, SM_shutdown, SM_pops,
72: SM_mathcap, SM_setMathcap, SM_nop,
73: SM_beginBlock, SM_endBlock,
74: 0
75: };
76:
77: /*
78: mathcap =
79: [
80: version list,
81: SMlist,
82: [
83: [OX tag,CMO tag list],
84: [OX tag,CMO tag list],
85: ...
86: ]
87: ]
88: */
89:
90: void create_my_mathcap(char *system)
91: {
92: NODE n,n0;
93: int i,k;
94: STRING str;
95: LIST sname,smlist,oxlist,cmolist,asirlist,oxtag,oxasir,r;
96: USINT tag,t,t1;
97:
98: if ( my_mathcap )
99: return;
100: /* version */
101: MKSTR(str,system);
102: MKUSINT(t,OX_VERSION);
103: n0 = mknode(2,t,str); MKLIST(sname,n0);
104:
105: /* cmo tag */
106: for ( n0 = 0, i = 0; k = available_sm[i]; i++ ) {
107: NEXTNODE(n0,n); MKUSINT(t,k); BDY(n) = (pointer)t;
108: }
109: NEXT(n) = 0; MKLIST(smlist,n0);
110:
111: /* creation of [OX_DATA,CMO list] */
112: /* ox tag */
113: MKUSINT(tag,OX_DATA);
114: /* cmo tag */
115: for ( n0 = 0, i = 0; k = available_cmo[i]; i++ ) {
116: NEXTNODE(n0,n); MKUSINT(t,k); BDY(n) = (pointer)t;
117: }
118: NEXT(n) = 0; MKLIST(cmolist,n0);
119: /* [ox tag, cmo list] */
120: n0 = mknode(2,tag,cmolist);
121: MKLIST(oxlist,n0);
122:
123: /* creation of [OX_LOCAL_OBJECT_ASIR,ASIR tag] */
124: /* ox tag */
125: MKUSINT(tag,OX_LOCAL_OBJECT_ASIR);
126: /* local tag */
127: MKUSINT(t,ASIR_VL);
128: MKUSINT(t1,ASIR_OBJ);
129: n0 = mknode(2,t,t1); MKLIST(cmolist,n0);
130: /* [ox tag, local list] */
131: n0 = mknode(2,tag,cmolist);
132: MKLIST(asirlist,n0);
133:
134: /* [oxlist,asirlist] */
135: n0 = mknode(2,oxlist,asirlist); MKLIST(oxasir,n0);
136:
137: /* [version,sm,oxasir] */
138: n0 = mknode(3,sname,smlist,oxasir); MKLIST(r,n0);
139:
140: MKMATHCAP(my_mathcap,r);
141: mclist_to_mc(r,&my_mc);
142: my_oxcap = my_mc.oxcap;
143: }
144:
145: void store_remote_mathcap(int s,MATHCAP mc)
146: {
147: if ( !remote_mc ) {
148: remote_mc_len = 16;
149: remote_mc = (struct mathcap *)
150: CALLOC(remote_mc_len,sizeof(struct mathcap));
151: }
152: if ( s >= remote_mc_len ) {
153: remote_mc_len *= 2;
154: remote_mc = (struct mathcap *)REALLOC(remote_mc,
155: remote_mc_len*sizeof(struct mathcap));
156: }
157: mclist_to_mc(BDY(mc),&remote_mc[s]);
158: }
159:
160: /*
161: mathcap =
162: [
163: version list,
164: SMlist,
165: [
166: [OX tag,CMO tag list],
167: [OX tag,CMO tag list],
168: ...
169: ]
170: ]
171:
172: ===>
173:
174: mathcap
175: | version | &servername | nsmcap | &smcap | noxcap | &oxcap |
176: smcap
177: | SM_xxx | SM_yyy | ... |
178: oxcap
179: | oxcap[0] | oxcap[1] | ... |
180: oxcap[i]
181: | ox | ncap | &cap |
182: cap
183: | CMO_xxx | CMO_yyy | ... |
184: */
185:
186: void mclist_to_mc(LIST mclist,struct mathcap *mc)
187: {
188: int id,l,i,j;
189: NODE n,t,oxcmo,ox,cap;
190: int *ptr;
191:
192: /*
193: [
194: [ version,servername ]
195: [sm1,sm2,...],
196: [
197: [o1,[n11,n12,...]],
198: [o2,[n21,n22,...]],
199: ...
200: ]
201: ]
202: */
203: n = BDY(mclist);
204: mc->mc = mclist;
205: mc->version = BDY((USINT)BDY(BDY((LIST)BDY(n))));
206: mc->servername = BDY((STRING)BDY(NEXT(BDY((LIST)BDY(n)))));
207:
208: /* smcap */
209: n = NEXT(n);
210: t = BDY((LIST)BDY(n));
211: mc->nsmcap = length(t);
212: mc->smcap = (int *)MALLOC_ATOMIC(mc->nsmcap*sizeof(int));
213: for ( j = 0, ptr = mc->smcap; j < mc->nsmcap; j++, t = NEXT(t) )
214: ptr[j] = BDY((USINT)BDY(t));
215:
216: n = NEXT(n);
217: n = BDY((LIST)BDY(n));
218: /* n -> BDY([[OX1,CMOlist1], [OX2,CMOlist2], ...]) */
219: mc->noxcap = length(n);
220: mc->oxcap = (struct oxcap *)MALLOC(mc->noxcap*sizeof(struct oxcap));
221: for ( j = 0; j < mc->noxcap; j++, n = NEXT(n) ) {
222: oxcmo = BDY((LIST)BDY(n));
223: /* oxcmo = BDY([OXj,CMOlistj]) */
224: mc->oxcap[j].ox = BDY((USINT)BDY(oxcmo));
225: cap = BDY((LIST)BDY(NEXT(oxcmo)));
226: /* cap ->BDY(CMOlistj) */
227: l = length(cap);
228: mc->oxcap[j].ncap = l;
229: mc->oxcap[j].cap = (unsigned int *)CALLOC(l+1,sizeof(unsigned int));
230: for ( t = cap, ptr = mc->oxcap[j].cap, i = 0; i < l; t = NEXT(t), i++ )
231: ptr[i] = BDY((USINT)BDY(t));
232: }
233: }
234:
235: int check_sm_by_mc(s,smtag)
236: int s;
237: unsigned int smtag;
238: {
239: struct mathcap *rmc;
240: int nsmcap,i;
241: unsigned int *smcap;
242:
243: /* XXX : return 1 if remote_mc is not available. */
244: if ( !remote_mc )
245: return 1;
246: rmc = &remote_mc[s];
247: nsmcap = rmc->nsmcap;
248: smcap = rmc->smcap;
249: if ( !smcap )
250: return 1;
251: for ( i = 0; i < nsmcap; i++ )
252: if ( smcap[i] == smtag )
253: break;
254: if ( i == nsmcap )
255: return 0;
256: else
257: return 1;
258: }
259:
260: int check_by_mc(s,oxtag,cmotag)
261: int s;
262: unsigned int oxtag,cmotag;
263: {
264: struct mathcap *rmc;
265: int noxcap,ncap,i,j;
266: struct oxcap *oxcap;
267: unsigned int *cap;
268:
269: /* XXX : return 1 if remote_mc is not available. */
270: if ( !remote_mc )
271: return 1;
272: rmc = &remote_mc[s];
273: noxcap = rmc->noxcap;
274: oxcap = rmc->oxcap;
275: if ( !oxcap )
276: return 1;
277: for ( i = 0; i < noxcap; i++ )
278: if ( oxcap[i].ox == oxtag )
279: break;
280: if ( i == noxcap )
281: return 0;
282: ncap = oxcap[i].ncap;
283: cap = oxcap[i].cap;
284: for ( j = 0; j < ncap; j++ )
285: if ( cap[j] == cmotag )
286: break;
287: if ( j == ncap )
288: return 0;
289: else
290: return 1;
291: }
292:
293: void begin_critical() {
294: critical_when_signal = 1;
295: }
296:
297: void end_critical() {
298: critical_when_signal = 0;
299: if ( ox_usr1_sent ) {
300: ox_usr1_sent = 0; ox_usr1_handler();
301: }
302: if ( ox_int_received ) {
303: ox_int_received = 0; int_handler(SIGINT);
304: }
305: }
306:
307: void ox_usr1_handler(sig)
308: int sig;
309: {
310: extern jmp_buf env;
311: unsigned int cmd;
312:
313: #if !defined(VISUAL)
314: signal(SIGUSR1,ox_usr1_handler);
315: #endif
316: if ( critical_when_signal ) {
317: fprintf(stderr,"usr1 : critical\n");
318: ox_usr1_sent = 1;
319: } else {
320: ox_flushing = 1;
321: ox_resetenv("usr1 : return to toplevel by SIGUSR1");
322: }
323: }
324:
325: void clear_readbuffer()
326: {
327: char c;
328: fd_set r,w,e;
329: struct timeval interval;
330: int n,sock;
331:
332: #if defined(linux)
333: iofp[0].in->_IO_read_ptr = iofp[0].in->_IO_read_end;
334: #elif defined(__FreeBSD__)
335: fpurge(iofp[0].in);
336: #endif
337: /*
338: sock = fileno(iofp[0].in);
339: interval.tv_sec = (int)0;
340: interval.tv_usec = (int)0;
341:
342: FD_ZERO(&r); FD_ZERO(&w); FD_ZERO(&e);
343: FD_SET(sock,&r);
344: while ( 1 ) {
345: n = select(FD_SETSIZE,&r,&w,&e,&interval);
346: if ( !n )
347: break;
348: read(sock,&c,1);
349: }
350: */
351: }
352:
353: #if MPI
354: int ox_data_is_available(int s)
355: {
356: return 1;
357: }
358:
359: void wait_for_data(int s)
360: {
361: return;
362: }
363: #else
364: int ox_data_is_available(int s)
365: {
366: return FP_DATA_IS_AVAILABLE(iofp[s].in);
367: }
368:
369: void wait_for_data(int s)
370: {
371: fd_set r;
372: int sock;
373:
374: if ( !FP_DATA_IS_AVAILABLE(iofp[s].in) ) {
375: #if defined(VISUAL)
376: sock = iofp[s].in->fildes;
377: FD_ZERO(&r);
378: FD_SET(sock,&r);
379: select(0,&r,NULL,NULL,NULL);
380: #else
381: sock = fileno(iofp[s].in);
382: FD_ZERO(&r);
383: FD_SET(sock,&r);
384: select(FD_SETSIZE,&r,NULL,NULL,NULL);
385: #endif
386: }
387: }
388: #endif
389:
390: void ox_send_data(int s,pointer p)
391: {
392: if ( ox_check && !ox_check_cmo(s,(Obj)p) )
393: error("ox_send_data : Mathcap violation");
394: begin_critical();
395: ox_write_int(s,OX_DATA);
396: ox_write_int(s,ox_serial++);
397: ox_write_cmo(s,p);
398: ox_flush_stream(s);
399: end_critical();
400: }
401:
402: void ox_send_cmd(int s,int id)
403: {
404: if ( ox_check && !check_sm_by_mc(s,id) )
405: error("ox_send_cmd : Mathcap violation");
406: begin_critical();
407: ox_write_int(s,OX_COMMAND);
408: ox_write_int(s,ox_serial++);
409: ox_write_int(s,id);
410: ox_flush_stream(s);
411: end_critical();
412: }
413:
414: void ox_send_sync(int s)
415: {
416: begin_critical();
417: ox_write_int(s,OX_SYNC_BALL);
418: ox_write_int(s,ox_serial++);
419: ox_flush_stream(s);
420: end_critical();
421: }
422:
423: void ox_send_local_data(int s,Obj p)
424: {
425: begin_critical();
426: ox_write_int(s,OX_LOCAL_OBJECT_ASIR);
427: ox_write_int(s,ox_serial++);
428: ox_write_int(s,ASIR_OBJ);
429: saveobj(iofp[s].out,p);
430: ox_flush_stream(s);
431: end_critical();
432: }
433:
434: void ox_send_local_ring(int s,VL vl)
435: {
436: begin_critical();
437: ox_write_int(s,OX_LOCAL_OBJECT_ASIR);
438: ox_write_int(s,ox_serial++);
439: ox_write_int(s,ASIR_VL);
440: savevl(iofp[s].out,vl);
441: ox_flush_stream(s);
442: end_critical();
443: }
444:
445: unsigned int ox_recv(int s, int *id, pointer *p)
446: {
447: unsigned int cmd,serial;
448: USINT ui;
449:
450: wait_for_data(s);
451: begin_critical();
452: ox_read_int(s,id);
453: ox_read_int(s,&serial);
454: switch ( *id ) {
455: case OX_COMMAND:
456: ox_read_int(s,&cmd);
457: MKUSINT(ui,cmd);
458: *p = (pointer)ui;
459: break;
460: case OX_DATA:
461: ox_read_cmo(s,p);
462: break;
463: case OX_LOCAL_OBJECT_ASIR:
464: ox_read_local(s,p);
465: break;
466: default:
467: *p = 0;
468: break;
469: }
470: end_critical();
471: return serial;
472: }
473:
474: void ox_get_result(s,rp)
475: int s;
476: Obj *rp;
477: {
478: int id;
479: Obj obj,r;
480: int level;
481:
482: level = 0;
483: r = 0;
484: do {
485: ox_recv(s,&id,(pointer *)&obj);
486: if ( id == OX_COMMAND ) {
487: switch ( ((USINT)obj)->body ) {
488: case SM_beginBlock:
489: level++;
490: break;
491: case SM_endBlock:
492: level--;
493: }
494: } else
495: r = obj;
496: } while ( level );
497: *rp = r;
498: }
499:
500: void ox_read_int(int s, int *n)
501: {
502: ox_need_conv = iofp[s].conv;
503: read_int(iofp[s].in,n);
504: }
505:
506: void ox_read_cmo(int s, Obj *rp)
507: {
508: ox_need_conv = iofp[s].conv;
509: read_cmo(iofp[s].in,rp);
510: }
511:
512: void ox_read_local(int s, Obj *rp)
513: {
514: int id;
515:
516: ox_need_conv = iofp[s].conv;
517: read_int(iofp[s].in,&id);
518: switch ( id ) {
519: case ASIR_VL:
520: loadvl(iofp[s].in);
521: *rp = VOIDobj;
522: break;
523: case ASIR_OBJ:
524: loadobj(iofp[s].in,rp);
525: break;
526: default:
527: error("ox_read_local : unsupported id");
528: break;
529: }
530: }
531:
532: void ox_write_int(int s, int n)
533: {
534: ox_need_conv = iofp[s].conv;
535: write_int(iofp[s].out,&n);
536: }
537:
538: void ox_write_cmo(int s, Obj obj)
539: {
540: ox_need_conv = iofp[s].conv;
541: write_cmo(iofp[s].out,obj);
542: }
543:
544: int ox_check_cmo(int s, Obj obj)
545: {
546: NODE m;
547:
548: if ( !obj )
549: return 1;
550: switch ( OID(obj) ) {
551: case O_MATHCAP: case O_STR: case O_ERR: case O_USINT: case O_VOID:
552: return 1;
553: case O_P:
554: if ( !check_by_mc(s,OX_DATA,CMO_RECURSIVE_POLYNOMIAL) )
555: return 0;
556: else
557: return ox_check_cmo_p(s,(P)obj);
558: case O_R:
559: if ( !check_by_mc(s,OX_DATA,CMO_RATIONAL) )
560: return 0;
561: else if ( !check_by_mc(s,OX_DATA,CMO_RECURSIVE_POLYNOMIAL) )
562: return 0;
563: else
564: return ox_check_cmo_p(s,NM((R)obj)) && ox_check_cmo_p(s,DN((R)obj));
565: case O_DP:
566: return ox_check_cmo_dp(s,(DP)obj);
567: case O_N:
568: if ( NID((Num)obj) == N_Q ) {
569: if ( INT((Q)obj) )
570: return check_by_mc(s,OX_DATA,CMO_ZZ);
571: else
572: return check_by_mc(s,OX_DATA,CMO_QQ);
573: } else
574: return 0;
575: case O_LIST:
576: for ( m = BDY((LIST)obj); m; m = NEXT(m) )
577: if ( !ox_check_cmo(s,(BDY(m))) )
578: return 0;
579: return 1;
580: default:
581: return 0;
582: }
583: }
584:
585: void ox_get_serverinfo(int s, LIST *rp)
586: {
587: if ( remote_mc )
588: *rp = remote_mc[s].mc;
589: else {
590: MKLIST(*rp,0);
591: }
592: }
593:
594: int ox_check_cmo_p(int s, P p)
595: {
596: DCP dc;
597:
598: if ( NUM(p) )
599: return ox_check_cmo(s,(Obj)p);
600: else {
601: for ( dc = DC(p); dc; dc = NEXT(dc) )
602: if ( !ox_check_cmo_p(s,COEF(dc)) )
603: return 0;
604: return 1;
605: }
606: }
607:
608: int ox_check_cmo_dp(int s, DP p)
609: {
610: MP m;
611:
612: for ( m = BDY(p); m; m = NEXT(m) )
613: if ( !ox_check_cmo(s,(Obj)m->c) )
614: return 0;
615: return 1;
616: }
617:
618: void ox_flush_stream(s)
619: int s;
620: {
621: if ( ox_batch )
622: return;
623: #if defined(VISUAL)
624: if ( _fileno(&iofp[s].out->fp) < 0 )
625: cflush(iofp[s].out);
626: else
627: #elif MPI
628: if ( (char)fileno(&iofp[s].out->fp) < 0 )
629: cflush(iofp[s].out);
630: else
631: #endif
632: fflush(iofp[s].out);
633: }
634:
635: void ox_flush_stream_force(s)
636: int s;
637: {
638: #if defined(VISUAL)
639: if ( _fileno(&iofp[s].out->fp) < 0 )
640: cflush(iofp[s].out);
641: else
642: #elif MPI
643: if ( (char)fileno(&iofp[s].out->fp) < 0 )
644: cflush(iofp[s].out);
645: else
646: #endif
647: fflush(iofp[s].out);
648: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>