Annotation of OpenXM_contrib2/asir2018/io/ox.c, Revision 1.3
1.1 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@sec.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.
1.3 ! fujimoto 47: * $OpenXM: OpenXM_contrib2/asir2018/io/ox.c,v 1.2 2019/07/25 05:57:55 noro Exp $
1.1 noro 48: */
49: #include "ca.h"
50: #include "parse.h"
51: #include "signal.h"
52: #include "wsio.h"
53: #include "ox.h"
54:
55: #define ISIZ sizeof(int)
56:
57: extern Obj VOIDobj;
58:
59: extern int nserver_102, myrank_102;
60: extern int ox_need_conv;
61: int ox_usr1_sent, ox_int_received, critical_when_signal;
62: unsigned int ox_serial;
63: int ox_flushing;
64: int ox_batch;
65: int ox_check=1;
66: int ox_exchange_mathcap=1;
67: JMP_BUF ox_env;
68:
69: MATHCAP my_mathcap;
70:
71: struct oxcap {
72: unsigned int ox;
73: int ncap;
74: int *cap;
75: };
76:
77: struct mathcap {
78: LIST mc;
79: unsigned int version;
80: char *servername;
81: int nsmcap;
82: unsigned int *smcap;
83: int noxcap;
84: struct oxcap *oxcap;
85: int no_ox_reset;
86: };
87:
88: struct oxcap *my_oxcap;
89:
90: static struct mathcap my_mc;
91: static struct mathcap *remote_mc;
92: static int remote_mc_len;
93:
94: void mclist_to_mc(LIST mclist,struct mathcap *mc);
95: Obj asir_pop_one();
96: void asir_push_one(Obj);
97:
98: #if defined(VISUAL) || defined(__MINGW32__)
99: /* XXX : mainly used in engine2000/io.c, but declared here */
100: typedef void *HANDLE;
101: HANDLE hStreamNotify,hStreamNotify_Ack;
102:
103: void cleanup_events()
104: {
105: /* ox_watch_stream may be waiting for hStreamNotify_Ack to be set */
106:
107: ResetEvent(hStreamNotify);
108: SetEvent(hStreamNotify_Ack);
109: }
110: #endif
111:
112: /* 1 if no_ox_reset, 0 if ox_reset OK, -1 if invalid */
113: int no_ox_reset(int s)
114: {
115: if ( remote_mc && s >= 0 && s < remote_mc_len )
116: return remote_mc[s].no_ox_reset;
117: else
118: return -1;
119: }
120:
121: void ox_resetenv(char *s)
122: {
123: #if defined(VISUAL) || defined(__MINGW32__)
124: cleanup_events();
125: #endif
126: fprintf(stderr,"%s\n",s);
127: LONGJMP(ox_env,1);
128: }
129:
130: static int available_cmo[] = {
131: CMO_NULL, CMO_INT32, CMO_DATUM, CMO_STRING, CMO_MATHCAP,
132: CMO_ERROR, CMO_ERROR2, CMO_LIST, CMO_MONOMIAL32,
133: CMO_ZZ, CMO_QQ, CMO_ZERO,
134: CMO_DMS_GENERIC, CMO_DMS_OF_N_VARIABLES,
135: CMO_RING_BY_NAME, CMO_DISTRIBUTED_POLYNOMIAL,
136: CMO_RATIONAL,
137: CMO_RECURSIVE_POLYNOMIAL, CMO_UNIVARIATE_POLYNOMIAL,
138: CMO_INDETERMINATE,
139: CMO_TREE, CMO_BIGFLOAT32, CMO_COMPLEX,
140: 0
141: };
142:
143: static int asir_available_sm[] = {
144: SM_dupErrors, SM_getsp, SM_popSerializedLocalObject,
145: SM_popCMO, SM_popString, SM_pushCMOtag, SM_setName,
146: SM_evalName, SM_executeStringByLocalParser,
147: SM_executeStringByLocalParserInBatchMode,
148: SM_executeFunction, SM_shutdown, SM_pops,
149: SM_mathcap, SM_setMathcap, SM_nop,
150: SM_beginBlock, SM_endBlock,
151: 0
152: };
153:
154: static int ox_asir_available_sm[] = {
155: SM_dupErrors, SM_getsp, SM_popSerializedLocalObject,
156: SM_popCMO, SM_popString, SM_pushCMOtag, SM_setName,
157: SM_evalName, SM_executeStringByLocalParser,
158: SM_executeStringByLocalParserInBatchMode,
159: SM_executeFunction, SM_shutdown, SM_pops,
160: SM_mathcap, SM_setMathcap, SM_nop,
161: SM_beginBlock, SM_endBlock, SM_executeFunctionSync,
162: SM_set_rank_102, SM_tcp_accept_102, SM_tcp_connect_102, SM_reset_102,
163: SM_bcast_102, SM_reduce_102,
164: 0
165: };
166:
167: static int ox_plot_available_sm[] = {
168: SM_dupErrors, SM_getsp, SM_popSerializedLocalObject,
169: SM_popCMO, SM_popString, SM_setName,
170: SM_evalName, SM_executeStringByLocalParser,
171: SM_executeFunction, SM_shutdown, SM_pops,
172: SM_mathcap, SM_setMathcap, SM_nop,
173: 0
174: };
175:
176: /*
177: mathcap =
178: [
179: version list,
180: SMlist,
181: [
182: [OX tag,CMO tag list],
183: [OX tag,CMO tag list],
184: ...
185: ]
186: ]
187: */
188:
189: void create_my_mathcap(char *system)
190: {
191: NODE n,n0;
192: int i,k;
193: STRING str;
194: LIST sname,smlist,oxlist,cmolist,asirlist,oxasir,r;
195: USINT tag,t,t1;
196:
197: if ( my_mathcap )
198: return;
199: /* version */
200: MKSTR(str,system);
201: MKUSINT(t,OX_VERSION);
202: n0 = mknode(2,t,str); MKLIST(sname,n0);
203:
204: /* sm tag */
205: n0 = 0;
206: if ( !strcmp(system,"asir") ) {
207: for ( i = 0; k = asir_available_sm[i]; i++ ) {
208: NEXTNODE(n0,n); MKUSINT(t,k); BDY(n) = (pointer)t;
209: }
210: } else if ( !strcmp(system,"ox_asir") ) {
211: for ( i = 0; k = ox_asir_available_sm[i]; i++ ) {
212: NEXTNODE(n0,n); MKUSINT(t,k); BDY(n) = (pointer)t;
213: }
214: NEXT(n) = 0;
215: } else if ( !strcmp(system,"ox_plot") ) {
216: for ( i = 0; k = ox_plot_available_sm[i]; i++ ) {
217: NEXTNODE(n0,n); MKUSINT(t,k); BDY(n) = (pointer)t;
218: }
219: NEXT(n) = 0;
220: }
221: MKLIST(smlist,n0);
222:
223: /* creation of [OX_DATA,CMO list] */
224: /* ox tag */
225: MKUSINT(tag,OX_DATA);
226: /* cmo tag */
227: for ( n0 = 0, i = 0; k = available_cmo[i]; i++ ) {
228: NEXTNODE(n0,n); MKUSINT(t,k); BDY(n) = (pointer)t;
229: }
230: NEXT(n) = 0; MKLIST(cmolist,n0);
231: /* [ox tag, cmo list] */
232: n0 = mknode(2,tag,cmolist);
233: MKLIST(oxlist,n0);
234:
235: /* creation of [OX_LOCAL_OBJECT_ASIR,ASIR tag] */
236: /* ox tag */
237: MKUSINT(tag,OX_LOCAL_OBJECT_ASIR);
238: /* local tag */
239: MKUSINT(t,ASIR_VL);
240: MKUSINT(t1,ASIR_OBJ);
241: n0 = mknode(2,t,t1); MKLIST(cmolist,n0);
242: /* [ox tag, local list] */
243: n0 = mknode(2,tag,cmolist);
244: MKLIST(asirlist,n0);
245:
246: /* [oxlist,asirlist] */
247: n0 = mknode(2,oxlist,asirlist); MKLIST(oxasir,n0);
248:
249: /* [version,sm,oxasir] */
250: n0 = mknode(3,sname,smlist,oxasir); MKLIST(r,n0);
251:
252: MKMATHCAP(my_mathcap,r);
253: mclist_to_mc(r,&my_mc);
254: my_oxcap = my_mc.oxcap;
255: }
256:
257: void store_remote_mathcap(int s,MATHCAP mc)
258: {
259: if ( !remote_mc ) {
260: remote_mc_len = 16;
261: remote_mc = (struct mathcap *)
262: CALLOC(remote_mc_len,sizeof(struct mathcap));
263: }
264: if ( s >= remote_mc_len ) {
265: remote_mc_len *= 2;
266: remote_mc = (struct mathcap *)REALLOC(remote_mc,
267: remote_mc_len*sizeof(struct mathcap));
268: }
269: mclist_to_mc(BDY(mc),&remote_mc[s]);
270: }
271:
272: /*
273: mathcap =
274: [
275: version list,
276: SMlist,
277: [
278: [OX tag,CMO tag list],
279: [OX tag,CMO tag list],
280: ...
281: ]
282: ]
283:
284: ===>
285:
286: mathcap
287: | version | &servername | nsmcap | &smcap | noxcap | &oxcap |
288: smcap
289: | SM_xxx | SM_yyy | ... |
290: oxcap
291: | oxcap[0] | oxcap[1] | ... |
292: oxcap[i]
293: | ox | ncap | &cap |
294: cap
295: | CMO_xxx | CMO_yyy | ... |
296: */
297:
298: void mclist_to_mc(LIST mclist,struct mathcap *mc)
299: {
300: int l,i,j;
301: NODE n,t,oxcmo,cap;
302: int *ptr;
303:
304: /*
305: [
306: [ version,servername ]
307: [sm1,sm2,...],
308: [
309: [o1,[n11,n12,...]],
310: [o2,[n21,n22,...]],
311: ...
312: ]
313: (optional)[s1,s2,...] (no_ox_reset)
314: ]
315: */
316: n = BDY(mclist);
317: mc->mc = mclist;
318: mc->version = BDY((USINT)BDY(BDY((LIST)BDY(n))));
319: mc->servername = BDY((STRING)BDY(NEXT(BDY((LIST)BDY(n)))));
320:
321: /* smcap */
322: n = NEXT(n);
323: t = BDY((LIST)BDY(n));
324: mc->nsmcap = length(t);
325: mc->smcap = (int *)MALLOC_ATOMIC(mc->nsmcap*sizeof(int));
326: for ( j = 0, ptr = mc->smcap; j < mc->nsmcap; j++, t = NEXT(t) )
327: ptr[j] = BDY((USINT)BDY(t));
328:
329: n = NEXT(n);
330: n = BDY((LIST)BDY(n));
331: /* n -> BDY([[OX1,CMOlist1], [OX2,CMOlist2], ...]) */
332: mc->noxcap = length(n);
333: mc->oxcap = (struct oxcap *)MALLOC(mc->noxcap*sizeof(struct oxcap));
334: for ( j = 0; j < mc->noxcap; j++, n = NEXT(n) ) {
335: oxcmo = BDY((LIST)BDY(n));
336: /* oxcmo = BDY([OXj,CMOlistj]) */
337: mc->oxcap[j].ox = BDY((USINT)BDY(oxcmo));
338: cap = BDY((LIST)BDY(NEXT(oxcmo)));
339: /* cap ->BDY(CMOlistj) */
340: l = length(cap);
341: mc->oxcap[j].ncap = l;
342: mc->oxcap[j].cap = (unsigned int *)CALLOC(l+1,sizeof(unsigned int));
343: for ( t = cap, ptr = mc->oxcap[j].cap, i = 0; i < l; t = NEXT(t), i++ )
344: ptr[i] = BDY((USINT)BDY(t));
345: }
346: /* check of no_ox_reset */
347: mc->no_ox_reset = 0;
348: n = BDY(mclist);
349: if ( length(n) >= 4 ) {
350: t = BDY((LIST)ARG3(n));
351: for ( ; t; t = NEXT(t) )
352: if ( !strcmp(BDY((STRING)BDY(t)),"no_ox_reset") )
353: mc->no_ox_reset = 1;
354: }
355: }
356:
357: int check_sm_by_mc(int s,unsigned int smtag)
358: {
359: struct mathcap *rmc;
360: int nsmcap,i;
361: unsigned int *smcap;
362:
363: /* XXX : return 1 if remote_mc is not available. */
364: if ( !remote_mc )
365: return 1;
366: rmc = &remote_mc[s];
367: nsmcap = rmc->nsmcap;
368: smcap = rmc->smcap;
369: if ( !smcap )
370: return 1;
371: for ( i = 0; i < nsmcap; i++ )
372: if ( smcap[i] == smtag )
373: break;
374: if ( i == nsmcap )
375: return 0;
376: else
377: return 1;
378: }
379:
380: int check_by_mc(int s,unsigned int oxtag,unsigned int cmotag)
381: {
382: struct mathcap *rmc;
383: int noxcap,ncap,i,j;
384: struct oxcap *oxcap;
385: unsigned int *cap;
386:
387: /* XXX : return 1 if remote_mc is not available. */
388: if ( !remote_mc )
389: return 1;
390: rmc = &remote_mc[s];
391: noxcap = rmc->noxcap;
392: oxcap = rmc->oxcap;
393: if ( !oxcap )
394: return 1;
395: for ( i = 0; i < noxcap; i++ )
396: if ( oxcap[i].ox == oxtag )
397: break;
398: if ( i == noxcap )
399: return 0;
400: ncap = oxcap[i].ncap;
401: cap = oxcap[i].cap;
402: for ( j = 0; j < ncap; j++ )
403: if ( cap[j] == cmotag )
404: break;
405: if ( j == ncap )
406: return 0;
407: else
408: return 1;
409: }
410:
1.2 noro 411: extern int Im_ox_plot;
1.3 ! fujimoto 412: #if defined(ANDROID)
! 413: int Im_ox_plot = 0;
! 414: #endif
1.2 noro 415:
1.1 noro 416: void begin_critical() {
417: critical_when_signal = 1;
418: }
419:
420: void end_critical() {
421: critical_when_signal = 0;
422: if ( ox_usr1_sent ) {
423: ox_usr1_sent = 0;
424: #if !defined(VISUAL) && !defined(__MINGW32__)
425: ox_usr1_handler(SIGUSR1);
426: #else
427: ox_usr1_handler(0);
428: #endif
429: }
430: if ( ox_int_received ) {
431: ox_int_received = 0; int_handler(SIGINT);
432: }
433: }
434:
435: extern NODE user_int_handler;
436: extern int caught_intr,in_gc;
437:
438: void ox_usr1_handler(int sig)
439: {
440: NODE t;
441:
442: #if !defined(VISUAL) && !defined(__MINGW32__)
443: set_signal_for_restart(SIGUSR1,ox_usr1_handler);
444: #endif
1.2 noro 445: if ( Im_ox_plot ) {
446: ox_flushing = 1;
447: ox_send_sync(0);
448: return;
449: }
1.1 noro 450: if ( critical_when_signal ) {
451: fprintf(stderr,"usr1 : critical\n");
452: ox_usr1_sent = 1;
453: } else if ( in_gc ) {
454: fprintf(stderr,"usr1 : in_gc\n");
455: caught_intr = 2;
456: } else {
457: ox_flushing = 1;
458: if ( user_int_handler ) {
459: fprintf(stderr,
460: "usr1 : calling the registered exception handlers...");
461: for ( t = user_int_handler; t; t = NEXT(t) )
462: bevalf((FUNC)BDY(t),0);
463: fprintf(stderr, "done.\n");
464: }
465: LEAVE_SIGNAL_CS_ALL;
466: ox_resetenv("usr1 : return to toplevel by SIGUSR1");
467: }
468: }
469:
470: void clear_readbuffer()
471: {
1.3 ! fujimoto 472: #if defined(ANDROID)
! 473: fpurge(iofp[0].in);
! 474: #elif defined(linux)
1.1 noro 475: iofp[0].in->_IO_read_ptr = iofp[0].in->_IO_read_end;
476: #elif defined(__FreeBSD__)
477: fpurge(iofp[0].in);
478: #endif
479: /*
480: sock = fileno(iofp[0].in);
481: interval.tv_sec = (int)0;
482: interval.tv_usec = (int)0;
483:
484: FD_ZERO(&r); FD_ZERO(&w); FD_ZERO(&e);
485: FD_SET(sock,&r);
486: while ( 1 ) {
487: n = select(FD_SETSIZE,&r,&w,&e,&interval);
488: if ( !n )
489: break;
490: read(sock,&c,1);
491: }
492: */
493: }
494:
495: #if MPI
496: int ox_data_is_available(int s)
497: {
498: return 1;
499: }
500:
501: void wait_for_data(int s)
502: {
503: return;
504: }
505:
506: void wait_for_data_102(int rank)
507: {
508: return;
509: }
510: #else
511: int ox_data_is_available(int s)
512: {
513: return FP_DATA_IS_AVAILABLE(iofp[s].in);
514: }
515:
516: void wait_for_data(int s)
517: {
518: fd_set r;
519: int sock;
520:
521: if ( !FP_DATA_IS_AVAILABLE(iofp[s].in) ) {
522: #if defined(VISUAL) || defined(__MINGW32__)
523: sock = iofp[s].in->fildes;
524: FD_ZERO(&r);
525: FD_SET((unsigned int)sock,&r);
526: select(0,&r,NULL,NULL,NULL);
527: #else
528: sock = fileno(iofp[s].in);
529: FD_ZERO(&r);
530: FD_SET(sock,&r);
531: select(FD_SETSIZE,&r,NULL,NULL,NULL);
532: #endif
533: }
534: }
535:
536: void wait_for_data_102(int rank)
537: {
538: fd_set r;
539: int sock;
540:
541: if ( !FP_DATA_IS_AVAILABLE(iofp_102[rank].in) ) {
542: #if defined(VISUAL) || defined(__MINGW32__)
543: sock = iofp_102[rank].in->fildes;
544: FD_ZERO(&r);
545: FD_SET((unsigned int)sock,&r);
546: select(0,&r,NULL,NULL,NULL);
547: #else
548: sock = fileno(iofp_102[rank].in);
549: FD_ZERO(&r);
550: FD_SET(sock,&r);
551: select(FD_SETSIZE,&r,NULL,NULL,NULL);
552: #endif
553: }
554: }
555: #endif
556:
557: void ox_send_data(int s,pointer p)
558: {
559: ERR err;
560: Obj p0;
561:
562: p0 = (Obj)p;
563: if ( ox_check && !ox_check_cmo(s,(Obj)p) ) {
564: create_error(&err,ox_serial,"ox_send_data : Mathcap violation",0);
565: p = (pointer)err;
566: }
567: begin_critical();
568: ox_write_int(s,OX_DATA);
569: ox_write_int(s,ox_serial++);
570: ox_write_cmo(s,p);
571: ox_flush_stream(s);
572: end_critical();
573: }
574:
575: void ox_send_data_102(int rank,pointer p)
576: {
577: ERR err;
578:
579: begin_critical();
580: ox_write_int_102(rank,OX_DATA);
581: ox_write_int_102(rank,ox_serial++);
582: ox_write_cmo_102(rank,p);
583: ox_flush_stream_102(rank);
584: end_critical();
585: }
586:
587: void ox_bcast_102(int root)
588: {
589: Obj data;
590: int r,mask,id,src,dst;
591:
592: r = myrank_102-root;
593: if ( r == 0 )
594: data = (Obj)asir_pop_one();
595:
596: if ( r < 0 ) r += nserver_102;
597: for ( mask = 1; mask < nserver_102; mask <<= 1 )
598: if ( r&mask ) {
599: src = myrank_102-mask;
600: if ( src < 0 ) src += nserver_102;
601: ox_recv_102(src,&id,&data);
602: break;
603: }
604: for ( mask >>= 1; mask > 0; mask >>= 1 )
605: if ( (r+mask) < nserver_102 ) {
606: dst = myrank_102+mask;
607: if ( dst >= nserver_102 ) dst -= nserver_102;
608: ox_send_data_102(dst,data);
609: }
610: asir_push_one(data);
611: }
612:
613: /* func : an arithmetic funcion func(vl,a,b,*c) */
614:
615: void ox_reduce_102(int root,void (*func)())
616: {
617: Obj data,data0,t;
618: int r,mask,id,src,dst;
619:
620: r = myrank_102-root;
621: if ( r < 0 ) r += nserver_102;
622: data = (Obj)asir_pop_one();
623: for ( mask = 1; mask < nserver_102; mask <<= 1 )
624: if ( r&mask ) {
625: dst = (r-mask)+root;
626: if ( dst >= nserver_102 ) dst -= nserver_102;
627: ox_send_data_102(dst,data);
628: break;
629: } else {
630: src = r+mask;
631: if ( src < nserver_102 ) {
632: src += root;
633: if ( src >= nserver_102 ) src -= nserver_102;
634: ox_recv_102(src,&id,&data0);
635: (*func)(CO,data,data0,&t); data = t;
636: }
637: }
638: asir_push_one(r?0:data);
639: }
640:
641: void ox_send_cmd(int s,int id)
642: {
643: if ( ox_check && !check_sm_by_mc(s,id) )
644: error("ox_send_cmd : Mathcap violation");
645: begin_critical();
646: ox_write_int(s,OX_COMMAND);
647: ox_write_int(s,ox_serial++);
648: ox_write_int(s,id);
649: ox_flush_stream(s);
650: end_critical();
651: }
652:
653: void ox_send_sync(int s)
654: {
655: begin_critical();
656: ox_write_int(s,OX_SYNC_BALL);
657: ox_write_int(s,ox_serial++);
658: ox_flush_stream(s);
659: end_critical();
660: }
661:
662: void ox_send_sync_102(int rank)
663: {
664: begin_critical();
665: ox_write_int_102(rank,OX_SYNC_BALL);
666: ox_write_int_102(rank,ox_serial++);
667: ox_flush_stream_102(rank);
668: end_critical();
669: }
670:
671: void ox_send_local_data(int s,Obj p)
672: {
673: begin_critical();
674: ox_write_int(s,OX_LOCAL_OBJECT_ASIR);
675: ox_write_int(s,ox_serial++);
676: ox_write_int(s,ASIR_OBJ);
677: saveobj((FILE *)iofp[s].out,p);
678: ox_flush_stream(s);
679: end_critical();
680: }
681:
682: void ox_send_local_data_102(int rank,Obj p)
683: {
684: begin_critical();
685: ox_write_int_102(rank,OX_LOCAL_OBJECT_ASIR);
686: ox_write_int_102(rank,ox_serial++);
687: ox_write_int_102(rank,ASIR_OBJ);
688: saveobj((FILE *)iofp_102[rank].out,p);
689: ox_flush_stream_102(rank);
690: end_critical();
691: }
692:
693: void ox_send_local_ring(int s,VL vl)
694: {
695: begin_critical();
696: ox_write_int(s,OX_LOCAL_OBJECT_ASIR);
697: ox_write_int(s,ox_serial++);
698: ox_write_int(s,ASIR_VL);
699: savevl((FILE *)iofp[s].out,vl);
700: ox_flush_stream(s);
701: end_critical();
702: }
703:
704: void ox_send_local_ring_102(int rank,VL vl)
705: {
706: begin_critical();
707: ox_write_int_102(rank,OX_LOCAL_OBJECT_ASIR);
708: ox_write_int_102(rank,ox_serial++);
709: ox_write_int_102(rank,ASIR_VL);
710: savevl((FILE *)iofp_102[rank].out,vl);
711: ox_flush_stream_102(rank);
712: end_critical();
713: }
714:
715: unsigned int ox_recv(int s, int *id, Obj *p)
716: {
717: unsigned int cmd,serial;
718: USINT ui;
719:
720: wait_for_data(s);
721: begin_critical();
722: ox_read_int(s,id);
723: ox_read_int(s,&serial);
724: switch ( *id ) {
725: case OX_COMMAND:
726: ox_read_int(s,&cmd);
727: MKUSINT(ui,cmd);
728: *p = (Obj)ui;
729: break;
730: case OX_DATA:
731: ox_read_cmo(s,p);
732: break;
733: case OX_LOCAL_OBJECT_ASIR:
734: ox_read_local(s,p);
735: break;
736: default:
737: *p = 0;
738: break;
739: }
740: end_critical();
741: return serial;
742: }
743:
744: unsigned int ox_recv_102(int rank, int *id, Obj *p)
745: {
746: unsigned int cmd,serial;
747: USINT ui;
748:
749: wait_for_data_102(rank);
750: begin_critical();
751: ox_read_int_102(rank,id);
752: ox_read_int_102(rank,&serial);
753: switch ( *id ) {
754: case OX_COMMAND:
755: ox_read_int_102(rank,&cmd);
756: MKUSINT(ui,cmd);
757: *p = (Obj)ui;
758: break;
759: case OX_DATA:
760: ox_read_cmo_102(rank,p);
761: break;
762: case OX_LOCAL_OBJECT_ASIR:
763: ox_read_local_102(rank,p);
764: break;
765: default:
766: *p = 0;
767: break;
768: }
769: end_critical();
770: return serial;
771: }
772:
773: void ox_get_result(int s,Obj *rp)
774: {
775: int id;
776: Obj obj,r;
777: int level;
778:
779: level = 0;
780: r = 0;
781: do {
782: ox_recv(s,&id,&obj);
783: if ( id == OX_COMMAND ) {
784: switch ( ((USINT)obj)->body ) {
785: case SM_beginBlock:
786: level++;
787: break;
788: case SM_endBlock:
789: level--;
790: }
791: } else
792: r = obj;
793: } while ( level );
794: *rp = r;
795: }
796:
797: void ox_read_int(int s, int *n)
798: {
799: ox_need_conv = iofp[s].conv;
800: read_int((FILE *)iofp[s].in,n);
801: }
802:
803: void ox_read_int_102(int rank, int *n)
804: {
805: ox_need_conv = iofp_102[rank].conv;
806: read_int((FILE *)iofp_102[rank].in,n);
807: }
808:
809: void ox_read_cmo(int s, Obj *rp)
810: {
811: ox_need_conv = iofp[s].conv;
812: read_cmo((FILE *)iofp[s].in,rp);
813: }
814:
815: void ox_read_cmo_102(int rank, Obj *rp)
816: {
817: ox_need_conv = iofp_102[rank].conv;
818: read_cmo((FILE *)iofp_102[rank].in,rp);
819: }
820:
821:
822: void ox_read_local(int s, Obj *rp)
823: {
824: int id;
825:
826: ox_need_conv = iofp[s].conv;
827: read_int((FILE *)iofp[s].in,&id);
828: switch ( id ) {
829: case ASIR_VL:
830: loadvl((FILE *)iofp[s].in);
831: *rp = VOIDobj;
832: break;
833: case ASIR_OBJ:
834: loadobj((FILE *)iofp[s].in,rp);
835: break;
836: default:
837: error("ox_read_local : unsupported id");
838: break;
839: }
840: }
841:
842: void ox_read_local_102(int rank, Obj *rp)
843: {
844: int id;
845:
846: ox_need_conv = iofp_102[rank].conv;
847: read_int((FILE *)iofp_102[rank].in,&id);
848: switch ( id ) {
849: case ASIR_VL:
850: loadvl((FILE *)iofp_102[rank].in);
851: *rp = VOIDobj;
852: break;
853: case ASIR_OBJ:
854: loadobj((FILE *)iofp_102[rank].in,rp);
855: break;
856: default:
857: error("ox_read_local_102 : unsupported id");
858: break;
859: }
860: }
861:
862: void ox_write_int(int s, int n)
863: {
864: ox_need_conv = iofp[s].conv;
865: write_int((FILE *)iofp[s].out,&n);
866: }
867:
868: void ox_write_int_102(int rank, int n)
869: {
870: ox_need_conv = iofp_102[rank].conv;
871: write_int((FILE *)iofp_102[rank].out,&n);
872: }
873:
874: void ox_write_cmo(int s, Obj obj)
875: {
876: ox_need_conv = iofp[s].conv;
877: write_cmo((FILE *)iofp[s].out,obj);
878: }
879:
880: void ox_write_cmo_102(int rank, Obj obj)
881: {
882: ox_need_conv = iofp_102[rank].conv;
883: write_cmo((FILE *)iofp_102[rank].out,obj);
884: }
885:
886: int ox_check_cmo(int s, Obj obj)
887: {
888: NODE m;
889:
890: if ( !obj )
891: return 1;
892: switch ( OID(obj) ) {
893: case O_MATHCAP: case O_STR: case O_ERR: case O_USINT: case O_VOID:
894: case O_BYTEARRAY:
895: return 1;
896: case O_P:
897: if ( !check_by_mc(s,OX_DATA,CMO_RECURSIVE_POLYNOMIAL) )
898: return 0;
899: else
900: return ox_check_cmo_p(s,(P)obj);
901: case O_R:
902: if ( !check_by_mc(s,OX_DATA,CMO_RATIONAL) )
903: return 0;
904: else if ( !check_by_mc(s,OX_DATA,CMO_RECURSIVE_POLYNOMIAL) )
905: return 0;
906: else
907: return ox_check_cmo_p(s,NM((R)obj)) && ox_check_cmo_p(s,DN((R)obj));
908: case O_DP:
909: return ox_check_cmo_dp(s,(DP)obj);
910: case O_N:
911: switch ( NID((Num)obj) ) {
912: case N_Q:
913: if ( INT((Q)obj) )
914: return check_by_mc(s,OX_DATA,CMO_ZZ);
915: else
916: return check_by_mc(s,OX_DATA,CMO_QQ);
917: case N_R: case N_B: case N_C:
918: return 1;
919: default:
920: return 0;
921: }
922: break;
923: case O_LIST:
924: for ( m = BDY((LIST)obj); m; m = NEXT(m) )
925: if ( !ox_check_cmo(s,(BDY(m))) )
926: return 0;
927: return 1;
928: case O_QUOTE: /* XXX */
929: return 1;
930: case O_MAT: /* MAT is sent as a list */
931: return 1;
932: default:
933: return 0;
934: }
935: }
936:
937: void ox_get_serverinfo(int s, LIST *rp)
938: {
939: if ( remote_mc )
940: *rp = remote_mc[s].mc;
941: else {
942: MKLIST(*rp,0);
943: }
944: }
945:
946: char *ox_get_servername(int s)
947: {
948: return (remote_mc && remote_mc[s].servername)?remote_mc[s].servername:0;
949: }
950:
951:
952: int ox_check_cmo_p(int s, P p)
953: {
954: DCP dc;
955:
956: if ( NUM(p) )
957: return ox_check_cmo(s,(Obj)p);
958: else {
959: for ( dc = DC(p); dc; dc = NEXT(dc) )
960: if ( !ox_check_cmo_p(s,COEF(dc)) )
961: return 0;
962: return 1;
963: }
964: }
965:
966: int ox_check_cmo_dp(int s, DP p)
967: {
968: MP m;
969:
970: for ( m = BDY(p); m; m = NEXT(m) )
971: if ( !ox_check_cmo(s,(Obj)m->c) )
972: return 0;
973: return 1;
974: }
975:
976: void ox_flush_stream(int s)
977: {
978: if ( ox_batch )
979: return;
980: #if defined(VISUAL) || defined(__MINGW32__) || defined(MPI)
981: if ( WSIO_fileno(iofp[s].out) < 0 )
982: cflush(iofp[s].out);
983: else
984: #endif
985: fflush((FILE *)iofp[s].out);
986: }
987:
988: void ox_flush_stream_force(int s)
989: {
990: #if defined(VISUAL) || defined(__MINGW32__) || defined(MPI)
991: if ( WSIO_fileno(iofp[s].out) < 0 )
992: cflush(iofp[s].out);
993: else
994: #endif
995: fflush((FILE *)iofp[s].out);
996: }
997:
998: void ox_flush_stream_102(int rank)
999: {
1000: if ( !ox_batch )
1001: ox_flush_stream_force_102(rank);
1002: }
1003:
1004: void ox_flush_stream_force_102(int rank)
1005: {
1006: if ( iofp_102[rank].out )
1007: #if defined(VISUAL) || defined(__MINGW32__)
1008: cflush(iofp_102[rank].out);
1009: #elif MPI
1010: cflush(iofp_102[rank].out);
1011: #else
1012: fflush(iofp_102[rank].out);
1013: #endif
1014: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>