[BACK]Return to ox.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / io

Annotation of OpenXM_contrib2/asir2000/io/ox.c, Revision 1.25

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>