示例#1
0
SEXP get_pi (SEXP Rpostmat,
	     SEXP Rfun,
	     SEXP Rr,
	     SEXP Rr_low,
	     SEXP Rinds,
	     SEXP Rxcol,
	     SEXP Rycol) {

  SEXP rc = R_NilValue;
  int i,j,k;
  double dist;
  int num_cnt, denom_cnt; /*counters for those filling conditions*/
  int f_ans; /*used to hold the result of the function*/


  /*turn all of the R stuff passed in to the type of stuff we can
   referene in C*/
  double *r = REAL(Rr);
  double *r_low = REAL(Rr_low);
  int *inds = INTEGER(Rinds);
  int xcol = INTEGER(Rxcol)[0]-1;
  int ycol = INTEGER(Rycol)[0]-1;

  SEXP postmat_dim = getAttrib(Rpostmat, R_DimSymbol);
  double *postmat = REAL(Rpostmat);
  int rows = INTEGER(postmat_dim)[0];

  /*some sanity checking*/
  if (!isFunction(Rfun)) error("Rfun must be a function");

  /*prepare the return information*/
  PROTECT(rc=allocVector(REALSXP, length(Rr)));

  /*repeat calculation for all r*/
  for (i=0;i<length(Rr);i++) {
    //Rprintf("%1.1f,%1.1f\n", r_low[i],r[i]); //DEBUG
    /*zero out counts*/
    num_cnt = 0;
    denom_cnt = 0;

    /*might be faster to have some scraning criteria, but
      we will loop throufh every pair...j is from */
    for (j=0;j<rows;j++) {

      /*k is to*/
      for (k=0; k<rows;k++) {
  	/*do not compare someone with themself*/
  	if (inds[k] == inds[j]) continue;

  	/*calculate the distance*/
  	dist = sqrt(pow(postmat[j+xcol*rows] - postmat[k+xcol*rows],2) +
  		    pow(postmat[j+ycol*rows] - postmat[k+ycol*rows],2));

  	if ((dist>r[i]) | (dist<r_low[i])) continue;

  	/*call the user supplied function*/
  	f_ans = (int)run_fun(Rfun,
  			     extract_row(Rpostmat,j),
  			     extract_row(Rpostmat,k));

  	/*update the counts appropriately*/
  	if (f_ans==1) {
  	  denom_cnt++;
  	  num_cnt++;
  	} else if (f_ans==2) {
  	  denom_cnt++;
  	}
      }
    }
    //Rprintf("%d/%d\n",num_cnt,denom_cnt); // DEBUG
    REAL(rc)[i] = (double)num_cnt/denom_cnt;
  }

  UNPROTECT(1);

  return(rc);

}
示例#2
0
文件: eventl.c 项目: jajm/pazpar2
static int event_loop(iochan_man_t man, IOCHAN *iochans) {
    do /* loop as long as there are active associations to process */
    {
        IOCHAN p, *nextp;
        IOCHAN start;
        IOCHAN inv_start;
        fd_set in, out, except;
        int res, max;
        static struct timeval to;
        struct timeval *timeout;

//        struct yaz_poll_fd *fds;
        int no_fds = 0;
        FD_ZERO(&in);
        FD_ZERO(&out);
        FD_ZERO(&except);
        timeout = &to; /* hang on select */
        to.tv_sec = 300;
        to.tv_usec = 0;

        // INV: start must no change through the loop

        yaz_mutex_enter(man->iochan_mutex);
        start = man->channel_list;
        yaz_mutex_leave(man->iochan_mutex);
        inv_start = start;
        for (p = start; p; p = p->next) {
            no_fds++;
        }
//        fds = (struct yaz_poll_fd *) xmalloc(no_fds * sizeof(*fds));

        max = 0;
        for (p = start; p; p = p->next) {
            if (p->thread_users > 0)
                continue;
            if (p->max_idle && p->max_idle < to.tv_sec)
                to.tv_sec = p->max_idle;
            if (p->fd < 0)
                continue;
            if (p->flags & EVENT_INPUT)
                FD_SET(p->fd, &in);
            if (p->flags & EVENT_OUTPUT)
                FD_SET(p->fd, &out);
            if (p->flags & EVENT_EXCEPT)
                FD_SET(p->fd, &except);
            if (p->fd > max)
                max = p->fd;
        }
        yaz_log(man->log_level, "max=%d sel_fd=%d", max, man->sel_fd);

        if (man->sel_fd != -1) {
            if (man->sel_fd > max)
                max = man->sel_fd;
            FD_SET(man->sel_fd, &in);
        }
        yaz_log(man->log_level, "select begin nofds=%d", max);
        res = select(max + 1, &in, &out, &except, timeout);
        yaz_log(man->log_level, "select returned res=%d", res);
        if (res < 0) {
            if (errno == EINTR)
                continue;
            else {
                yaz_log(YLOG_ERRNO | YLOG_WARN, "select");
                return 0;
            }
        }
        if (man->sel_fd != -1) {
            if (FD_ISSET(man->sel_fd, &in)) {
                IOCHAN chan;

                yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
                        man->sel_fd);
                while ((chan = sel_thread_result(man->sel_thread))) {
                    yaz_log(man->log_level,
                            "eventl: got thread result chan=%p name=%s", chan,
                            chan->name ? chan->name : "");
                    chan->thread_users--;
                }
            }
        }
        if (man->log_level) {
            int no = 0;
            for (p = start; p; p = p->next) {
                no++;
            }
            yaz_log(man->log_level, "%d channels", no);
        }
        for (p = start; p; p = p->next) {
            time_t now = time(0);

            if (p->destroyed) {
                yaz_log(man->log_level,
                        "eventl: skip destroyed chan=%p name=%s", p,
                        p->name ? p->name : "");
                continue;
            }
            if (p->thread_users > 0) {
                yaz_log(man->log_level,
                        "eventl: skip chan=%p name=%s users=%d", p,
                        p->name ? p->name : "", p->thread_users);
                continue;
            }
            p->this_event = 0;

            if (p->max_idle && now - p->last_event > p->max_idle) {
                p->last_event = now;
                p->this_event |= EVENT_TIMEOUT;
            }
            if (p->fd >= 0) {
                if (FD_ISSET(p->fd, &in)) {
                    p->last_event = now;
                    p->this_event |= EVENT_INPUT;
                }
                if (FD_ISSET(p->fd, &out)) {
                    p->last_event = now;
                    p->this_event |= EVENT_OUTPUT;
                }
                if (FD_ISSET(p->fd, &except)) {
                    p->last_event = now;
                    p->this_event |= EVENT_EXCEPT;
                }
            }
            run_fun(man, p);
        }
        assert(inv_start == start);
        yaz_mutex_enter(man->iochan_mutex);
        for (nextp = iochans; *nextp;) {
            IOCHAN p = *nextp;
            if (p->destroyed && p->thread_users == 0) {
                *nextp = iochan_destroy_real(p);
            } else
                nextp = &p->next;
        }
        yaz_mutex_leave(man->iochan_mutex);
    } while (*iochans);
    return 0;
}
示例#3
0
int main(int argc, char **argv) {
    int num_benchs = 0;
    time_t rtime = 0, start_t;
    char *benchlet_prefixes[1024] = { "", NULL };

    if (argc < 2) {
        fprintf(stderr,
                "ctrbenchmark, Copyright (C)2011, "
                "Johannes Weiß <*****@*****.**>\n");
        fprintf(stderr,
                "This program comes with ABSOLUTELY NO WARRANTY; "
                "for details type `show w'.\n"
                "This is free software, and you are welcome to redistribute it"
                "\nunder certain conditions; type `show c' for details.\n\n");
        errx(1, "Usage: %s RUNNING-TIME [BENCHLETS...]", argv[0]);
    }

    rtime = atoi(argv[1]);
    if (argc > 2) {
        for (int i = 2; i < argc; i++, num_benchs++) {
            benchlet_prefixes[num_benchs] = argv[i];
            benchlet_prefixes[num_benchs+1] = NULL;
        }
    } else {
        num_benchs = 1;
    }
    printf("numbenchs: %d\n", num_benchs);

    assert(SECTION_SIZE(__benchlet_calls) ==
           SECTION_SIZE(__benchlet_init_calls));

    for (int i = 0; i < SECTION_SIZE(__benchlet_calls); i++) {
        const char **counters;
        const char *counter;
        benchlet_config_t cfg;
        benchlet_info_t info;
        benchlet_init_call_t init_fun = __start___benchlet_init_calls[i];
        benchlet_call_t run_fun = __start___benchlet_calls[i];

        init_fun(&info);
        printf("- benchlet '%s':\n\t* desc: '%s'\n\t* counters: ",
               info.name, info.description);
        counters = info.penetrated_counters;
        while(NULL != (counter = *counters++)) {
            printf("%s, ", counter);
        }
        printf("\b\b  \n");

        cfg.desired_running_time = (int)rtime;
        for (int j = 0; j < num_benchs; j++) {
            if (info.name == strstr(info.name, benchlet_prefixes[j])) {
                printf("- Running '%s' for %ds in 1s...\n",
                       info.name, (int)cfg.desired_running_time);
                sleep(1);
                start_t = time(NULL);
                run_fun(&cfg);
                printf("- OK (%ds)\n", (int)(time(NULL)-start_t));
            } else {
                printf("- not running '%s'\n", info.name);
            }
        }
    }
    return 0;
}