/* Send a batch of work until to a stealer batch: info about the batch. This function will free memory finish: true if we should notify target that this is last to send */ static adlb_code send_steal_batch(steal_cb_state *batch, bool finish) { int count = (int)batch->size; struct packed_steal_resp hdr = { .count = count, .last = finish }; SEND(&hdr, sizeof(hdr), MPI_BYTE, batch->stealer_rank, ADLB_TAG_RESPONSE_STEAL_COUNT); if (count == 0) return ADLB_SUCCESS; struct packed_steal_work packed[count]; for (int i = 0; i < count; i++) { xlb_pack_steal_work(&(packed[i]), batch->work_units[i]); } // Store requests for wait MPI_Request reqs[count + 1]; DEBUG("[%i] sending batch size %zu", xlb_s.layout.rank, batch->size); ISEND(packed, (int)sizeof(packed[0]) * count, MPI_BYTE, batch->stealer_rank, ADLB_TAG_RESPONSE_STEAL, &reqs[0]); for (int i = 0; i < count; i++) { DEBUG("stolen payload: %s", (char*) batch->work_units[i]->payload); xlb_work_unit *unit = batch->work_units[i]; ISEND(unit->payload, unit->length, MPI_BYTE, batch->stealer_rank, ADLB_TAG_RESPONSE_STEAL, &reqs[i+1]); } // Wait until MPI confirms sends have completed int rc = MPI_Waitall(count + 1, reqs, MPI_STATUSES_IGNORE); MPI_CHECK(rc); for (int i = 0; i < count; i++) { xlb_work_unit_free(batch->work_units[i]); } batch->size = 0; return ADLB_SUCCESS; }
/* A slightly modified version of MCW_get_intlist, which can scan for label matching instead of just numbers. If labels == NULL, then it functions just like MCW_get_intlist ZSS Dec 09 */ int * MCW_get_labels_intlist (char **labels, int nvals, char *str) { int *subv = NULL ; int ii , ipos , nout , slen ; int ibot,itop,istep , nused ; char *cpt ; static int show_labs = -1; /* Meaningless input? */ if( nvals < 1 ) return NULL ; /* No selection list? */ if( str == NULL || str[0] == '\0' ) return NULL ; if( show_labs == -1 ) show_labs = AFNI_yesenv("AFNI_SHOW_LABEL_TO_INDEX"); /* skip initial '[' or '{' or '#'*/ subv = (int *) malloc( sizeof(int) * 2 ) ; subv[0] = nout = 0 ; ipos = 0 ; if( str[ipos] == '[' || str[ipos] == '{' || str[ipos] == '#') ipos++ ; /* do we have a 1dcat string in there ZSS ? */ if (strstr(str,"1dcat ")) { return(get_1dcat_intlist ( str, &ii, nvals-1 )); } /* do we have a count string in there ZSS ? */ if (strstr(str,"count ")) { return(get_count_intlist ( str, &ii, nvals-1 )); } /*** loop through each sub-selector until end of input ***/ slen = strlen(str) ; while( ipos < slen && !ISEND(str[ipos]) ){ while( isspace(str[ipos]) ) ipos++ ; /* skip blanks */ if( ISEND(str[ipos]) ) break ; /* done */ /** get starting value **/ if( str[ipos] == '$' ){ /* special case */ ibot = nvals-1 ; ipos++ ; } else if ( !labels || !(nused = is_in_labels(str+ipos, labels, nvals, &ibot))){ /* decode integer */ ibot = strtol( str+ipos , &cpt , 10 ) ; if( ibot < 0 && !allow_negative ){ fprintf(stderr, "** ERROR: selector index %d is out of range 0..%d\n", ibot,nvals-1) ; free(subv) ; return NULL ; } if( ibot >= nvals ){ fprintf(stderr, "** ERROR: selector index %d is out of range 0..%d\n", ibot,nvals-1) ; free(subv) ; return NULL ; } nused = (cpt-(str+ipos)) ; if( ibot == 0 && nused == 0 ){ fprintf(stderr, "** ERROR: selector syntax error 5 '%s'\n",str+ipos) ; free(subv) ; return NULL ; } ipos += nused ; } else { if( show_labs ) fprintf(stderr,"-- label select: sub-brick %d is from label %s\n", ibot, labels[ibot]); ipos+=nused; } while( isspace(str[ipos]) ) ipos++ ; /* skip blanks */ /** if that's it for this sub-selector, add one value to list **/ if( str[ipos] == ',' || ISEND(str[ipos]) ){ nout++ ; subv = (int *) realloc( (char *)subv , sizeof(int) * (nout+1) ) ; subv[0] = nout ; subv[nout] = ibot ; if( ISEND(str[ipos]) ) break ; /* done */ ipos++ ; continue ; /* re-start loop at next sub-selector */ } /** otherwise, must have '..' or '-' as next inputs **/ if( str[ipos] == '-' || str[ipos] == ':' ){ ipos++ ; } else if( str[ipos] == '.' && str[ipos+1] == '.' ){ ipos++ ; ipos++ ; } else { fprintf(stderr,"** ERROR: selector selector syntax is bad: '%s'\n", str+ipos) ; free(subv) ; return NULL ; } /** get ending value for loop now **/ if( str[ipos] == '$' ){ /* special case */ itop = nvals-1 ; ipos++ ; } else if ( !labels || !(nused = is_in_labels(str+ipos,labels, nvals, &itop))){ /* decode integer */ itop = strtol( str+ipos , &cpt , 10 ) ; if( itop < 0 && !allow_negative ){ fprintf(stderr,"** ERROR: selector index %d is out of range 0..%d\n", itop,nvals-1) ; free(subv) ; return NULL ; } if( itop >= nvals ){ fprintf(stderr,"** ERROR: selector index %d is out of range 0..%d\n", itop,nvals-1) ; free(subv) ; return NULL ; } nused = (cpt-(str+ipos)) ; if( itop == 0 && nused == 0 ){ fprintf(stderr,"** ERROR: selector syntax error 6 '%s'\n",str+ipos) ; free(subv) ; return NULL ; } ipos += nused ; } else { /* have a label */ if( show_labs ) fprintf(stderr,"-- label select: sub-brick %d is from label %s\n", itop, labels[itop]); ipos+=nused; } /** set default loop step **/ istep = (ibot <= itop) ? 1 : -1 ; while( isspace(str[ipos]) ) ipos++ ; /* skip blanks */ /** check if we have a non-default loop step **/ if( str[ipos] == '(' ){ /* decode an integer */ ipos++ ; istep = strtol( str+ipos , &cpt , 10 ) ; if( istep == 0 ){ fprintf(stderr,"** ERROR: selector loop step is 0!\n") ; free(subv) ; return NULL ; } nused = (cpt-(str+ipos)) ; ipos += nused ; if( str[ipos] == ')' ) ipos++ ; if( (ibot-itop)*istep > 0 ){ fprintf( stderr, "** WARNING: selector count '%d..%d(%d)' means nothing!\n", ibot,itop,istep ) ; } } /** add values to output **/ for( ii=ibot ; (ii-itop)*istep <= 0 ; ii += istep ){ nout++ ; subv = (int *) realloc( (char *)subv , sizeof(int) * (nout+1) ) ; subv[0] = nout ; subv[nout] = ii ; } /** check if we have a comma to skip over **/ while( isspace(str[ipos]) ) ipos++ ; /* skip blanks */ if( str[ipos] == ',' ) ipos++ ; /* skip commas */ } /* end of loop through selector string */ if( subv[0] == 0 ){ free(subv); subv = NULL; } return subv ; }
static int fnmatch_helper( const char **pcur, /* pattern */ const char **scur, /* string */ int flags) { const int period = !(flags & FNM_DOTMATCH); const int pathname = flags & FNM_PATHNAME; const int escape = !(flags & FNM_NOESCAPE); const int nocase = flags & FNM_CASEFOLD; const char *ptmp = 0; const char *stmp = 0; const char *p = *pcur; const char *s = *scur; if (period && *s == '.' && *UNESCAPE(p) != '.') /* leading period */ RETURN(FNM_NOMATCH); while (1) { switch (*p) { case '*': do { p++; } while (*p == '*'); if (ISEND(UNESCAPE(p))) { p = UNESCAPE(p); RETURN(0); } if (ISEND(s)) RETURN(FNM_NOMATCH); ptmp = p; stmp = s; continue; case '?': if (ISEND(s)) RETURN(FNM_NOMATCH); p++; Inc(s); continue; case '[': { const char *t; if (ISEND(s)) RETURN(FNM_NOMATCH); if ((t = bracket(p + 1, s, flags)) != 0) { p = t; Inc(s); continue; } goto failed; } } /* ordinary */ p = UNESCAPE(p); if (ISEND(s)) RETURN(ISEND(p) ? 0 : FNM_NOMATCH); if (ISEND(p)) goto failed; if (Compare(p, s) != 0) goto failed; Inc(p); Inc(s); continue; failed: /* try next '*' position */ if (ptmp && stmp) { p = ptmp; Inc(stmp); /* !ISEND(*stmp) */ s = stmp; continue; } RETURN(FNM_NOMATCH); } }
int * get_count_intlist ( char *str , int *nret, int maxval ) { int *subv = NULL, *ret = NULL ; int ii , ipos , nout , slen, shuffle, step, itmp; int ibot,itop,istep , nused , nuni; long int seed=0; char *cpt ; *nret = -1; if (!str || !strstr(str,"count ") || strlen (str) < 8) { fprintf(stderr, "NULL input or string does not have 'count '" " or at least 2 values are not present after 'count '\n"); return (NULL); } /* move past count */ slen = strlen(str) ; ipos = strlen("count "); /* see if you have seed */ if (strstr(str, "-seed ")) { ipos = (strstr(str, "-seed ")-str)+strlen("-seed "); seed = strtol( str+ipos , &cpt , 10 ) ; nused = (cpt-(str+ipos)) ; ipos += nused ; } /* get first value */ while( isspace(str[ipos]) ) ipos++ ; /* skip blanks */ if( ISEND(str[ipos]) ) return NULL ; /* bad */ ibot = strtol( str+ipos , &cpt , 10 ) ; if( ibot < 0 && !allow_negative ){ fprintf(stderr,"** ERROR: bot selector index %d cannot be < 0\n", ibot) ; return NULL ; /* added return 4 Jan 2016 [rickr] */ } else if( maxval >= 0 && ibot > maxval ){ /* 4 Jan 2016 [rickr] */ fprintf(stderr,"** ERROR: count selector index %d exceeds max %d\n", ibot, maxval) ; return NULL ; } nused = (cpt-(str+ipos)) ; if( ibot == 0 && nused == 0 ){ fprintf(stderr,"** ERROR: selector syntax error 1 '%s'\n",str+ipos) ; return NULL ; } ipos += nused ; while( isspace(str[ipos]) ) ipos++ ; /* skip blanks */ if( ISEND(str[ipos]) ) return NULL ; /* Bad */ itop = strtol( str+ipos , &cpt , 10 ) ; if( itop < 0 && !allow_negative ){ fprintf(stderr,"** ERROR: top selector index %d cannot be < 0\n", itop) ; return NULL ; } else if( maxval >= 0 && itop > maxval ){ /* 4 Jan 2016 [rickr] */ fprintf(stderr,"** ERROR: count top selector index %d exceeds max %d\n", itop, maxval) ; return NULL ; } if( itop == 0 && nused == 0 ){ fprintf(stderr,"** ERROR: selector syntax error 2 '%s'\n",str+ipos) ; return NULL ; } nused = (cpt-(str+ipos)) ; ipos += nused ; shuffle = 0; step = 0; while( isspace(str[ipos]) ) ipos++ ; /* skip blanks */ if( !ISEND(str[ipos]) ) { /* have step */ if ( (str[ipos] >= 'A' && str[ipos]<='Z') || (str[ipos] >= 'a' && str[ipos]<='z') ) { /* only S is acceptable here */ if (str[ipos] == 'S' || str[ipos] == 's') { shuffle = 1; ++ipos; }else { fprintf(stderr, "** No qualifiers allowed for step, other than 'S'. " "Have %c.\n", str[ipos]); return NULL ; } } if( !ISEND(str[ipos]) ) { step = strtol( str+ipos , &cpt , 10 ) ; } } nused = (cpt-(str+ipos)) ; ipos += nused ; if (step < 0) { fprintf(stderr,"** step must be > 0. Have %d.\n", step); return NULL ; } /* fprintf(stderr,"Have count parameters: %d to %d with step %d and shuffle = %d; seed = %ld\n", ibot, itop, step, shuffle, seed); */ if (itop < ibot) {nuni = ibot - itop + 1;} else { nuni = itop - ibot + 1; } if (shuffle) { subv = z_rand_order(ibot, itop, seed); if (step > 0) { *nret = step; } else { *nret = nuni; } } else { *nret = nuni; subv = (int *)malloc(sizeof(int)*(*nret)); if (!step) { if (itop < ibot) step = -1; else step = 1; } else { if (itop < ibot) step *= -1; else step *= 1; } itmp = 0; if (itop < ibot) for (ii=ibot; ii>=itop;ii=ii+step) { subv[itmp] = ii; ++itmp; } else for (ii=ibot; ii<=itop;ii=ii+step) { subv[itmp] = ii; ++itmp; } *nret = itmp; } /* fprintf(stderr,"Have %d ints: %d to %d with step %d and shuffle = %d\n", *nret, ibot, itop, step, shuffle); */ ret = (int *)malloc(sizeof(int)*(*nret+1)); ret[0] = *nret; for (ii=1; ii<=ret[0]; ++ii) ret[ii] = subv[(ii-1)%(nuni)]; free(subv); subv = ret; /* for (ii=0; ii<=ret[0]; ++ii) fprintf(stderr,"%d: %d\n", ii, subv[ii]); */ return(subv); }