[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.39

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.39    ! ohara      47:  * $OpenXM: OpenXM_contrib2/asir2000/io/ox.c,v 1.38 2016/08/24 00:52:50 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;
1.38      noro       85:   int no_ox_reset;
1.1       noro       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:
1.12      noro       94: void mclist_to_mc(LIST mclist,struct mathcap *mc);
1.30      ohara      95: Obj asir_pop_one();
                     96: void asir_push_one(Obj);
1.12      noro       97:
1.35      fujimoto   98: #if defined(VISUAL) || defined(__MINGW32__)
1.9       noro       99: /* XXX : mainly used in engine2000/io.c, but declared here */
1.33      fujimoto  100: typedef void *HANDLE;
1.9       noro      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:
1.38      noro      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:
1.12      noro      121: void ox_resetenv(char *s)
1.1       noro      122: {
1.35      fujimoto  123: #if defined(VISUAL) || defined(__MINGW32__)
1.9       noro      124:        cleanup_events();
                    125: #endif
1.1       noro      126:        fprintf(stderr,"%s\n",s);
1.13      noro      127:        LONGJMP(ox_env,1);
1.1       noro      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,
1.25      takayama  136:     CMO_RATIONAL,
1.1       noro      137:        CMO_RECURSIVE_POLYNOMIAL, CMO_UNIVARIATE_POLYNOMIAL,
                    138:        CMO_INDETERMINATE,
1.37      ohara     139:        CMO_TREE, CMO_BIGFLOAT32, CMO_COMPLEX,
1.1       noro      140:        0
                    141: };
                    142:
1.17      noro      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:
1.15      noro      154: static int ox_asir_available_sm[] = {
1.1       noro      155:        SM_dupErrors, SM_getsp, SM_popSerializedLocalObject,
1.7       noro      156:        SM_popCMO, SM_popString, SM_pushCMOtag, SM_setName,
1.1       noro      157:        SM_evalName, SM_executeStringByLocalParser,
                    158:        SM_executeStringByLocalParserInBatchMode,
                    159:        SM_executeFunction, SM_shutdown, SM_pops,
                    160:        SM_mathcap, SM_setMathcap, SM_nop,
1.29      noro      161:        SM_beginBlock, SM_endBlock,  SM_executeFunctionSync,
1.21      noro      162:        SM_set_rank_102, SM_tcp_accept_102, SM_tcp_connect_102, SM_reset_102,
1.24      noro      163:        SM_bcast_102, SM_reduce_102,
1.1       noro      164:        0
                    165: };
                    166:
1.15      noro      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,
1.16      noro      172:        SM_mathcap, SM_setMathcap, SM_nop,
1.15      noro      173:        0
                    174: };
                    175:
1.1       noro      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;
1.12      noro      194:        LIST sname,smlist,oxlist,cmolist,asirlist,oxasir,r;
1.1       noro      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:
1.15      noro      204:        /* sm tag */
                    205:        n0 = 0;
1.17      noro      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") ) {
1.15      noro      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;
1.1       noro      220:        }
1.15      noro      221:        MKLIST(smlist,n0);
1.1       noro      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: {
1.12      noro      300:        int l,i,j;
                    301:        NODE n,t,oxcmo,cap;
1.1       noro      302:        int *ptr;
                    303:
                    304:        /*
                    305:                [
                    306:                        [ version,servername ]
                    307:                        [sm1,sm2,...],
                    308:                        [
                    309:                                [o1,[n11,n12,...]],
                    310:                                [o2,[n21,n22,...]],
                    311:                                ...
                    312:                        ]
1.38      noro      313:       (optional)[s1,s2,...] (no_ox_reset)
1.1       noro      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:        }
1.38      noro      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:   }
1.1       noro      355: }
                    356:
1.12      noro      357: int check_sm_by_mc(int s,unsigned int smtag)
1.1       noro      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:
1.12      noro      380: int check_by_mc(int s,unsigned int oxtag,unsigned int cmotag)
1.1       noro      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:
                    411: void begin_critical() {
                    412:        critical_when_signal = 1;
                    413: }
                    414:
                    415: void end_critical() {
                    416:        critical_when_signal = 0;
                    417:        if ( ox_usr1_sent ) {
1.12      noro      418:                ox_usr1_sent = 0;
1.35      fujimoto  419: #if !defined(VISUAL) && !defined(__MINGW32__)
1.12      noro      420:        ox_usr1_handler(SIGUSR1);
                    421: #else
                    422:        ox_usr1_handler(0);
                    423: #endif
1.1       noro      424:        }
                    425:        if ( ox_int_received ) {
                    426:                ox_int_received = 0; int_handler(SIGINT);
                    427:        }
                    428: }
                    429:
1.26      noro      430: extern NODE user_int_handler;
1.18      noro      431:
1.12      noro      432: void ox_usr1_handler(int sig)
1.1       noro      433: {
1.26      noro      434:        NODE t;
                    435:
1.35      fujimoto  436: #if !defined(VISUAL) && !defined(__MINGW32__)
1.39    ! ohara     437:        set_signal_for_restart(SIGUSR1,ox_usr1_handler);
1.1       noro      438: #endif
                    439:        if ( critical_when_signal ) {
                    440:                fprintf(stderr,"usr1 : critical\n");
                    441:                ox_usr1_sent = 1;
                    442:        } else {
                    443:                ox_flushing = 1;
1.26      noro      444:                if ( user_int_handler ) {
1.18      noro      445:                        fprintf(stderr,
1.26      noro      446:                                "usr1 : calling the registered exception handlers...");
                    447:                        for ( t = user_int_handler; t; t = NEXT(t) )
                    448:                                bevalf((FUNC)BDY(t),0);
1.18      noro      449:                        fprintf(stderr, "done.\n");
                    450:                }
1.1       noro      451:                ox_resetenv("usr1 : return to toplevel by SIGUSR1");
                    452:        }
                    453: }
                    454:
                    455: void clear_readbuffer()
                    456: {
                    457: #if defined(linux)
                    458:        iofp[0].in->_IO_read_ptr = iofp[0].in->_IO_read_end;
                    459: #elif defined(__FreeBSD__)
                    460:        fpurge(iofp[0].in);
                    461: #endif
                    462: /*
                    463:        sock = fileno(iofp[0].in);
                    464:        interval.tv_sec = (int)0;
                    465:        interval.tv_usec = (int)0;
                    466:
                    467:        FD_ZERO(&r); FD_ZERO(&w); FD_ZERO(&e);
                    468:        FD_SET(sock,&r);
                    469:        while ( 1 ) {
                    470:                n = select(FD_SETSIZE,&r,&w,&e,&interval);
                    471:                if ( !n )
                    472:                        break;
                    473:                read(sock,&c,1);
                    474:        }
                    475: */
                    476: }
                    477:
1.20      noro      478: #if MPI
1.1       noro      479: int ox_data_is_available(int s)
                    480: {
                    481:        return 1;
                    482: }
                    483:
                    484: void wait_for_data(int s)
                    485: {
                    486:        return;
                    487: }
1.28      ohara     488:
                    489: void wait_for_data_102(int rank)
                    490: {
                    491:        return;
                    492: }
1.1       noro      493: #else
                    494: int ox_data_is_available(int s)
                    495: {
                    496:        return FP_DATA_IS_AVAILABLE(iofp[s].in);
                    497: }
                    498:
                    499: void wait_for_data(int s)
                    500: {
                    501:        fd_set r;
                    502:        int sock;
                    503:
                    504:        if ( !FP_DATA_IS_AVAILABLE(iofp[s].in) ) {
1.35      fujimoto  505: #if defined(VISUAL) || defined(__MINGW32__)
1.1       noro      506:                sock = iofp[s].in->fildes;
                    507:                FD_ZERO(&r);
1.12      noro      508:                FD_SET((unsigned int)sock,&r);
1.1       noro      509:                select(0,&r,NULL,NULL,NULL);
                    510: #else
                    511:                sock = fileno(iofp[s].in);
                    512:                FD_ZERO(&r);
                    513:                FD_SET(sock,&r);
                    514:                select(FD_SETSIZE,&r,NULL,NULL,NULL);
                    515: #endif
                    516:        }
                    517: }
1.21      noro      518:
                    519: void wait_for_data_102(int rank)
                    520: {
                    521:        fd_set r;
                    522:        int sock;
                    523:
                    524:        if ( !FP_DATA_IS_AVAILABLE(iofp_102[rank].in) ) {
1.35      fujimoto  525: #if defined(VISUAL) || defined(__MINGW32__)
1.21      noro      526:                sock = iofp_102[rank].in->fildes;
                    527:                FD_ZERO(&r);
                    528:                FD_SET((unsigned int)sock,&r);
                    529:                select(0,&r,NULL,NULL,NULL);
                    530: #else
                    531:                sock = fileno(iofp_102[rank].in);
                    532:                FD_ZERO(&r);
                    533:                FD_SET(sock,&r);
                    534:                select(FD_SETSIZE,&r,NULL,NULL,NULL);
                    535: #endif
                    536:        }
                    537: }
1.1       noro      538: #endif
                    539:
                    540: void ox_send_data(int s,pointer p)
                    541: {
1.8       noro      542:        ERR err;
                    543:
                    544:        if ( ox_check && !ox_check_cmo(s,(Obj)p) ) {
1.27      noro      545:                create_error(&err,ox_serial,"ox_send_data : Mathcap violation",0);
1.8       noro      546:                p = (pointer)err;
                    547:        }
1.1       noro      548:        begin_critical();
                    549:        ox_write_int(s,OX_DATA);
                    550:        ox_write_int(s,ox_serial++);
                    551:        ox_write_cmo(s,p);
                    552:        ox_flush_stream(s);
                    553:        end_critical();
                    554: }
                    555:
1.21      noro      556: void ox_send_data_102(int rank,pointer p)
                    557: {
                    558:        ERR err;
                    559:
                    560:        begin_critical();
                    561:        ox_write_int_102(rank,OX_DATA);
                    562:        ox_write_int_102(rank,ox_serial++);
                    563:        ox_write_cmo_102(rank,p);
                    564:        ox_flush_stream_102(rank);
                    565:        end_critical();
1.22      noro      566: }
                    567:
1.24      noro      568: void ox_bcast_102(int root)
1.22      noro      569: {
                    570:        Obj data;
1.23      noro      571:        int r,mask,id,src,dst;
1.22      noro      572:
1.23      noro      573:        r = myrank_102-root;
1.24      noro      574:        if ( r == 0 )
                    575:                data = (Obj)asir_pop_one();
                    576:
1.23      noro      577:        if ( r < 0 ) r += nserver_102;
                    578:        for ( mask = 1; mask < nserver_102; mask <<= 1 )
1.22      noro      579:                if ( r&mask ) {
1.23      noro      580:                        src = myrank_102-mask;
                    581:                        if ( src < 0 ) src += nserver_102;
1.22      noro      582:                        ox_recv_102(src,&id,&data);
                    583:                        break;
                    584:                }
                    585:        for ( mask >>= 1; mask > 0; mask >>= 1 )
1.23      noro      586:                if ( (r+mask) < nserver_102 ) {
                    587:                        dst = myrank_102+mask;
                    588:                        if ( dst >= nserver_102 ) dst -= nserver_102;
1.22      noro      589:                        ox_send_data_102(dst,data);
                    590:                }
1.24      noro      591:        asir_push_one(data);
1.23      noro      592: }
                    593:
                    594: /* func : an arithmetic funcion func(vl,a,b,*c) */
                    595:
1.24      noro      596: void ox_reduce_102(int root,void (*func)())
1.23      noro      597: {
1.24      noro      598:        Obj data,data0,t;
1.23      noro      599:        int r,mask,id,src,dst;
                    600:
                    601:        r = myrank_102-root;
                    602:        if ( r < 0 ) r += nserver_102;
1.24      noro      603:        data = (Obj)asir_pop_one();
1.23      noro      604:        for ( mask = 1; mask < nserver_102; mask <<= 1 )
                    605:                if ( r&mask ) {
                    606:                        dst = (r-mask)+root;
                    607:                        if ( dst >= nserver_102 ) dst -= nserver_102;
                    608:                        ox_send_data_102(dst,data);
                    609:                        break;
                    610:                } else {
                    611:                        src = r+mask;
                    612:                        if ( src < nserver_102 ) {
                    613:                                src += root;
                    614:                                if ( src >= nserver_102 ) src -= nserver_102;
                    615:                                ox_recv_102(src,&id,&data0);
                    616:                                (*func)(CO,data,data0,&t); data = t;
                    617:                        }
                    618:                }
1.24      noro      619:        asir_push_one(r?0:data);
1.21      noro      620: }
                    621:
1.1       noro      622: void ox_send_cmd(int s,int id)
                    623: {
                    624:        if ( ox_check && !check_sm_by_mc(s,id) )
                    625:                error("ox_send_cmd : Mathcap violation");
                    626:        begin_critical();
                    627:        ox_write_int(s,OX_COMMAND);
                    628:        ox_write_int(s,ox_serial++);
                    629:        ox_write_int(s,id);
                    630:        ox_flush_stream(s);
                    631:        end_critical();
                    632: }
                    633:
                    634: void ox_send_sync(int s)
                    635: {
                    636:        begin_critical();
                    637:        ox_write_int(s,OX_SYNC_BALL);
                    638:        ox_write_int(s,ox_serial++);
                    639:        ox_flush_stream(s);
                    640:        end_critical();
                    641: }
                    642:
1.21      noro      643: void ox_send_sync_102(int rank)
                    644: {
                    645:        begin_critical();
                    646:        ox_write_int_102(rank,OX_SYNC_BALL);
                    647:        ox_write_int_102(rank,ox_serial++);
                    648:        ox_flush_stream_102(rank);
                    649:        end_critical();
                    650: }
                    651:
1.1       noro      652: void ox_send_local_data(int s,Obj p)
                    653: {
                    654:        begin_critical();
                    655:        ox_write_int(s,OX_LOCAL_OBJECT_ASIR);
                    656:        ox_write_int(s,ox_serial++);
                    657:        ox_write_int(s,ASIR_OBJ);
1.12      noro      658:        saveobj((FILE *)iofp[s].out,p);
1.1       noro      659:        ox_flush_stream(s);
                    660:        end_critical();
                    661: }
                    662:
1.21      noro      663: void ox_send_local_data_102(int rank,Obj p)
                    664: {
                    665:        begin_critical();
                    666:        ox_write_int_102(rank,OX_LOCAL_OBJECT_ASIR);
                    667:        ox_write_int_102(rank,ox_serial++);
                    668:        ox_write_int_102(rank,ASIR_OBJ);
                    669:        saveobj((FILE *)iofp_102[rank].out,p);
                    670:        ox_flush_stream_102(rank);
                    671:        end_critical();
                    672: }
                    673:
1.1       noro      674: void ox_send_local_ring(int s,VL vl)
                    675: {
                    676:        begin_critical();
                    677:        ox_write_int(s,OX_LOCAL_OBJECT_ASIR);
                    678:        ox_write_int(s,ox_serial++);
                    679:        ox_write_int(s,ASIR_VL);
1.12      noro      680:        savevl((FILE *)iofp[s].out,vl);
1.1       noro      681:        ox_flush_stream(s);
                    682:        end_critical();
                    683: }
                    684:
1.21      noro      685: void ox_send_local_ring_102(int rank,VL vl)
                    686: {
                    687:        begin_critical();
                    688:        ox_write_int_102(rank,OX_LOCAL_OBJECT_ASIR);
                    689:        ox_write_int_102(rank,ox_serial++);
                    690:        ox_write_int_102(rank,ASIR_VL);
                    691:        savevl((FILE *)iofp_102[rank].out,vl);
                    692:        ox_flush_stream_102(rank);
                    693:        end_critical();
                    694: }
                    695:
1.12      noro      696: unsigned int ox_recv(int s, int *id, Obj *p)
1.1       noro      697: {
                    698:        unsigned int cmd,serial;
                    699:        USINT ui;
                    700:
                    701:        wait_for_data(s);
                    702:        begin_critical();
                    703:        ox_read_int(s,id);
                    704:        ox_read_int(s,&serial);
                    705:        switch ( *id ) {
                    706:                case OX_COMMAND:
                    707:                        ox_read_int(s,&cmd);
                    708:                        MKUSINT(ui,cmd);
1.12      noro      709:                        *p = (Obj)ui;
1.1       noro      710:                        break;
                    711:                case OX_DATA:
                    712:                        ox_read_cmo(s,p);
                    713:                        break;
                    714:                case OX_LOCAL_OBJECT_ASIR:
                    715:                        ox_read_local(s,p);
                    716:                        break;
                    717:                default:
                    718:                        *p = 0;
                    719:                        break;
                    720:        }
                    721:        end_critical();
                    722:        return serial;
                    723: }
                    724:
1.21      noro      725: unsigned int ox_recv_102(int rank, int *id, Obj *p)
                    726: {
                    727:        unsigned int cmd,serial;
                    728:        USINT ui;
                    729:
                    730:        wait_for_data_102(rank);
                    731:        begin_critical();
                    732:        ox_read_int_102(rank,id);
                    733:        ox_read_int_102(rank,&serial);
                    734:        switch ( *id ) {
                    735:                case OX_COMMAND:
                    736:                        ox_read_int_102(rank,&cmd);
                    737:                        MKUSINT(ui,cmd);
                    738:                        *p = (Obj)ui;
                    739:                        break;
                    740:                case OX_DATA:
                    741:                        ox_read_cmo_102(rank,p);
                    742:                        break;
                    743:                case OX_LOCAL_OBJECT_ASIR:
                    744:                        ox_read_local_102(rank,p);
                    745:                        break;
                    746:                default:
                    747:                        *p = 0;
                    748:                        break;
                    749:        }
                    750:        end_critical();
                    751:        return serial;
                    752: }
                    753:
1.12      noro      754: void ox_get_result(int s,Obj *rp)
1.1       noro      755: {
                    756:        int id;
                    757:        Obj obj,r;
                    758:        int level;
                    759:
                    760:        level = 0;
                    761:        r = 0;
                    762:        do {
1.12      noro      763:                ox_recv(s,&id,&obj);
1.1       noro      764:                if ( id == OX_COMMAND ) {
                    765:                        switch ( ((USINT)obj)->body ) {
                    766:                                case SM_beginBlock:
                    767:                                        level++;
                    768:                                        break;
                    769:                                case SM_endBlock:
                    770:                                        level--;
                    771:                        }
                    772:                } else
                    773:                        r = obj;
                    774:        } while ( level );
                    775:        *rp = r;
                    776: }
                    777:
                    778: void ox_read_int(int s, int *n)
                    779: {
                    780:        ox_need_conv = iofp[s].conv;
1.12      noro      781:        read_int((FILE *)iofp[s].in,n);
1.1       noro      782: }
                    783:
1.21      noro      784: void ox_read_int_102(int rank, int *n)
                    785: {
                    786:        ox_need_conv = iofp_102[rank].conv;
                    787:        read_int((FILE *)iofp_102[rank].in,n);
                    788: }
                    789:
1.1       noro      790: void ox_read_cmo(int s, Obj *rp)
                    791: {
                    792:        ox_need_conv = iofp[s].conv;
1.12      noro      793:        read_cmo((FILE *)iofp[s].in,rp);
1.1       noro      794: }
                    795:
1.21      noro      796: void ox_read_cmo_102(int rank, Obj *rp)
                    797: {
                    798:        ox_need_conv = iofp_102[rank].conv;
                    799:        read_cmo((FILE *)iofp_102[rank].in,rp);
                    800: }
                    801:
                    802:
1.1       noro      803: void ox_read_local(int s, Obj *rp)
                    804: {
                    805:        int id;
                    806:
                    807:        ox_need_conv = iofp[s].conv;
1.12      noro      808:        read_int((FILE *)iofp[s].in,&id);
1.1       noro      809:        switch ( id ) {
                    810:                case ASIR_VL:
1.12      noro      811:                        loadvl((FILE *)iofp[s].in);
1.1       noro      812:                        *rp = VOIDobj;
                    813:                        break;
                    814:                case ASIR_OBJ:
1.12      noro      815:                        loadobj((FILE *)iofp[s].in,rp);
1.1       noro      816:                        break;
                    817:                default:
                    818:                        error("ox_read_local : unsupported id");
                    819:                        break;
                    820:        }
                    821: }
                    822:
1.21      noro      823: void ox_read_local_102(int rank, Obj *rp)
                    824: {
                    825:        int id;
                    826:
                    827:        ox_need_conv = iofp_102[rank].conv;
                    828:        read_int((FILE *)iofp_102[rank].in,&id);
                    829:        switch ( id ) {
                    830:                case ASIR_VL:
                    831:                        loadvl((FILE *)iofp_102[rank].in);
                    832:                        *rp = VOIDobj;
                    833:                        break;
                    834:                case ASIR_OBJ:
                    835:                        loadobj((FILE *)iofp_102[rank].in,rp);
                    836:                        break;
                    837:                default:
                    838:                        error("ox_read_local_102 : unsupported id");
                    839:                        break;
                    840:        }
                    841: }
                    842:
1.1       noro      843: void ox_write_int(int s, int n)
                    844: {
                    845:        ox_need_conv = iofp[s].conv;
1.12      noro      846:        write_int((FILE *)iofp[s].out,&n);
1.1       noro      847: }
                    848:
1.21      noro      849: void ox_write_int_102(int rank, int n)
                    850: {
                    851:        ox_need_conv = iofp_102[rank].conv;
                    852:        write_int((FILE *)iofp_102[rank].out,&n);
                    853: }
                    854:
1.1       noro      855: void ox_write_cmo(int s, Obj obj)
                    856: {
                    857:        ox_need_conv = iofp[s].conv;
1.12      noro      858:        write_cmo((FILE *)iofp[s].out,obj);
1.1       noro      859: }
                    860:
1.21      noro      861: void ox_write_cmo_102(int rank, Obj obj)
                    862: {
                    863:        ox_need_conv = iofp_102[rank].conv;
                    864:        write_cmo((FILE *)iofp_102[rank].out,obj);
                    865: }
                    866:
1.1       noro      867: int ox_check_cmo(int s, Obj obj)
                    868: {
                    869:        NODE m;
                    870:
                    871:        if ( !obj )
                    872:                return 1;
                    873:        switch ( OID(obj) ) {
                    874:                case O_MATHCAP: case O_STR: case O_ERR: case O_USINT: case O_VOID:
1.10      noro      875:                case O_BYTEARRAY:
1.1       noro      876:                        return 1;
                    877:                case O_P:
                    878:                        if ( !check_by_mc(s,OX_DATA,CMO_RECURSIVE_POLYNOMIAL) )
                    879:                                return 0;
                    880:                        else
                    881:                                return ox_check_cmo_p(s,(P)obj);
                    882:                case O_R:
                    883:                        if ( !check_by_mc(s,OX_DATA,CMO_RATIONAL) )
                    884:                                return 0;
                    885:                        else if ( !check_by_mc(s,OX_DATA,CMO_RECURSIVE_POLYNOMIAL) )
                    886:                                return 0;
                    887:                        else
                    888:                                return ox_check_cmo_p(s,NM((R)obj)) && ox_check_cmo_p(s,DN((R)obj));
                    889:                case O_DP:
                    890:                        return ox_check_cmo_dp(s,(DP)obj);
                    891:                case O_N:
1.4       noro      892:                        switch ( NID((Num)obj) ) {
                    893:                                case N_Q:
                    894:                                        if ( INT((Q)obj) )
                    895:                                                return check_by_mc(s,OX_DATA,CMO_ZZ);
                    896:                                        else
                    897:                                                return check_by_mc(s,OX_DATA,CMO_QQ);
1.36      noro      898:                                case N_R: case N_B: case N_C:
1.4       noro      899:                                        return 1;
                    900:                                default:
                    901:                                        return 0;
                    902:                        }
                    903:                        break;
1.1       noro      904:                case O_LIST:
                    905:                        for ( m = BDY((LIST)obj); m; m = NEXT(m) )
                    906:                                if ( !ox_check_cmo(s,(BDY(m))) )
                    907:                                        return 0;
1.11      noro      908:                        return 1;
                    909:                case O_QUOTE: /* XXX */
1.1       noro      910:                        return 1;
1.32      noro      911:     case O_MAT: /* MAT is sent as a list */
                    912:                        return 1;
1.1       noro      913:                default:
                    914:                        return 0;
                    915:        }
                    916: }
                    917:
                    918: void ox_get_serverinfo(int s, LIST *rp)
                    919: {
                    920:        if ( remote_mc )
                    921:                *rp = remote_mc[s].mc;
                    922:        else {
                    923:                MKLIST(*rp,0);
                    924:        }
                    925: }
1.15      noro      926:
                    927: char *ox_get_servername(int s)
                    928: {
                    929:        return (remote_mc && remote_mc[s].servername)?remote_mc[s].servername:0;
                    930: }
                    931:
1.1       noro      932:
                    933: int ox_check_cmo_p(int s, P p)
                    934: {
                    935:        DCP dc;
                    936:
                    937:        if ( NUM(p) )
                    938:                return ox_check_cmo(s,(Obj)p);
                    939:        else {
                    940:                for ( dc = DC(p); dc; dc = NEXT(dc) )
                    941:                        if ( !ox_check_cmo_p(s,COEF(dc)) )
                    942:                                return 0;
                    943:                return 1;
                    944:        }
                    945: }
                    946:
                    947: int ox_check_cmo_dp(int s, DP p)
                    948: {
                    949:        MP m;
                    950:
                    951:        for ( m = BDY(p); m; m = NEXT(m) )
                    952:                if ( !ox_check_cmo(s,(Obj)m->c) )
                    953:                        return 0;
                    954:        return 1;
                    955: }
                    956:
1.12      noro      957: void ox_flush_stream(int s)
1.1       noro      958: {
                    959:        if ( ox_batch )
                    960:                return;
1.35      fujimoto  961: #if defined(VISUAL) || defined(__MINGW32__)
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.1       noro      971: }
                    972:
1.12      noro      973: void ox_flush_stream_force(int s)
1.1       noro      974: {
1.35      fujimoto  975: #if defined(VISUAL) || defined(__MINGW32__)
1.1       noro      976:        if ( _fileno(&iofp[s].out->fp) < 0 )
                    977:                cflush(iofp[s].out);
                    978:        else
1.20      noro      979: #elif MPI
1.1       noro      980:        if ( (char)fileno(&iofp[s].out->fp) < 0 )
                    981:                cflush(iofp[s].out);
                    982:        else
                    983: #endif
1.12      noro      984:        fflush((FILE *)iofp[s].out);
1.21      noro      985: }
                    986:
                    987: void ox_flush_stream_102(int rank)
                    988: {
                    989:        if ( !ox_batch )
                    990:                ox_flush_stream_force_102(rank);
                    991: }
                    992:
                    993: void ox_flush_stream_force_102(int rank)
                    994: {
                    995:        if ( iofp_102[rank].out )
1.35      fujimoto  996: #if defined(VISUAL) || defined(__MINGW32__)
1.21      noro      997:                cflush(iofp_102[rank].out);
                    998: #elif MPI
                    999:                cflush(iofp_102[rank].out);
                   1000: #else
                   1001:                fflush(iofp_102[rank].out);
                   1002: #endif
1.1       noro     1003: }

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