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