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