Example #1
0
void *random_io_reader(void *p)
{
  thread_arg_t *arg = (thread_arg_t *) p;
  char *bp; 
  posix_memalign((void **)&bp, 512, access_size);

  int num_reqs_per_thread = num_requests / num_threads;
  double t1 = gettimeofday_sec();
  //for (int i = 0; i < num_reqs_per_thread; ++i) {
  while (true) {
    off_t block_number = random_range(0, ionum-1) + access_from_block;
    //std::cout << arg->id << ": " << block_number << std::endl;
    if (!_pread(arg->fd, bp, access_size, block_number * access_size)) {
      perror("pread");
    }
    pthread_mutex_lock(&mutex);
    ++counter;
    pthread_mutex_unlock(&mutex);
    if (counter >= num_requests) {
      return NULL;
    }
  }
  double t2 = gettimeofday_sec();
  iops[arg->id] = num_reqs_per_thread / (t2-t1);
  mbps[arg->id] = (double) num_reqs_per_thread * access_size / (t2-t1);
  //std::cout << arg->id << ": " << iops[arg->id] << std::endl;
  return NULL;
}
int main(int argc, char** argv)
{
  omp_internal_data data;
  int retval;
  double t1, t2;
  data.retval = &retval;

  if (argc != 2)
    {
      fprintf(stderr, "ARGS: <N for fib(N)>\n");
      exit(1);
    }
  data.arg = atoi(argv[1]);

  gsoc_get_env(&_num_workers, &_gsoc_cutoff_depth);

  t1 = gettimeofday_sec();
  gsoc_run_workers(&data);
  t2 = gettimeofday_sec();

  fprintf(stdout, "fib(%d):%d\n", data.arg, *data.retval);
  fprintf(stdout, "exectime:%f\n", t2 - t1);

  return 0;
}
Example #3
0
int main(){
  double t1, t2;
  int i, j, l, m;
  FILE *fp;

  int size = (WIDTH - N) * (HEIGHT - N);
  int *ref = (int*)malloc(sizeof(int)*N*N);
  int *im  = (int*)malloc(sizeof(int)*WIDTH*HEIGHT);
  int *dif = (int*)malloc(sizeof(int)*size);
  int min = 0;

  fp = fopen("template.dat","r");
  for(i = 0; i < N * N; i++) {
    fscanf(fp, "%d", &ref[i]);
  }
  fclose(fp);
  fp = fopen("image.dat","r");
  for(i = 0; i < size; i++) {
    fscanf(fp, "%d", &im[i]);
  }
  fclose(fp);
  
  t1 = gettimeofday_sec();
  for(i = 0; i < HEIGHT - N; i++) {
    for(j = 0; j < WIDTH - N; j++) {
      int tmp = 0;
      int diff = 0;
      for(l = 0; l < N; l++) {
        for(m = 0; m < N; m++) {
          diff = im[(i+m)*WIDTH+j+l] - ref[m*N+l];
          tmp += diff * diff;
        }
      }
      //printf("%d\n", i*(WIDTH-N)+j);
      dif[i*(WIDTH-N)+j] = tmp;
    }
  }
  t2 = gettimeofday_sec();

  for(i = 0; i < size; i++) {
    if(dif[min] > dif[i]) {
      min = i;
    }
  }
  printf("x = %d, y = %d\n", min % (WIDTH - HEIGHT),
         min / (WIDTH - HEIGHT));
  printf("Run Time: %.3le[s]", t2 - t1);

  fp = fopen("tmpout.dat","w");
  for(i = 0; i < size; i++) {
    fprintf(fp, "%d\n", dif[i]);
  }
  fclose(fp);

  free(ref);
  free(im);
  free(dif);

  return 0;
}
Example #4
0
int main(int argc, char **argv){

  if (argc != 3) {
    std::cerr << "Usage: " << argv[0] << " dbname record_num" << std::endl; 
    exit(1);
  }

  double t1, t2;
  int rnum = atoi(argv[2]);
  int ecode;
  char *key, *value;

  t1 = gettimeofday_sec();
  TCBDB *bdb = tcbdbnew();
  tcbdbtune(bdb, 5, -1, -1, -1, -1, 0);
  tcbdbsetcache(bdb, 300, 100);

  if (!tcbdbopen(bdb, argv[1], BDBOWRITER | BDBOCREAT)) {
    ecode = tcbdbecode(bdb);
    fprintf(stderr, "open error: %s\n", tcbdberrmsg(ecode));
  }

  for (int i = 0; i < rnum; ++i) {
    char key[9];
    memset(key, 0, 9);
    sprintf(key,"%08d", i);
    char *val = new char[307201]; // 100K
    memset(val, 0, 307201);
    sprintf(val, "%0102400d", i);
    sprintf(val + 102400, "%0102400d", i);
    sprintf(val + 102400*2, "%0102400d", i);

    int size;
    void *data = tcbdbget(bdb, key, strlen(key), &size);
    if (data != NULL) {
      if (strcmp(val, (char *) data) != 0) {
        std::cout << "[error] value incorrect." << std::endl;
      }
      free(data);
    } else {
      std::cout << "[error] entry not found." << std::endl;
    } 
    delete [] val;
  }

  if (!tcbdbclose(bdb)) {
    ecode = tcbdbecode(bdb);
    fprintf(stderr, "close error: %s\n", tcbdberrmsg(ecode));
  }
  tcbdbdel(bdb);
  t2 = gettimeofday_sec();
  std::cout << "get time: " << t2 - t1 << std::endl;

  return 0;
}
Example #5
0
void FPSCount(DWORD* fps){

    static double before=gettimeofday_sec();
    double now=gettimeofday_sec();
    static DWORD fps_ctr=0;

    if(now-before>=1.000){
	before=now;
	*fps=fps_ctr;
	fps_ctr=0;

    }
    fps_ctr++;
}
Example #6
0
/*!
 * @brief ブロックを生成する
 *
 * ゲームオーバー判定も行う
 */
static void create_block(void) {
  uint i;
  int block_type;  /*ブロックの種類用。0 ~ 6の乱数を入れる */
  /* まずブロックの座標を初期位置にリセット */
  y = 0;
  x = 4;

  /* 乱数を発生させ、その乱数を7で割った余り(0 ~ 6まで)でブロックの種類を決定 */
  block_type = rand() % N_BLOCK;
  /* ブロックデータの中から block_type に応じた種類のブロックを読み込む */
  for (i = 0; i < BLOCK_HEIGHT; i++) {
    uint j;
    for (j = 0; j < BLOCK_WIDTH; j++) {
      block[i][j] = block_list[block_type][i][j];
    }
  }
  /* 壁+ブロックをフィールドへ */
  for (i = 0; i < BLOCK_HEIGHT; i++) {
    uint j;
    for (j = 0; j < BLOCK_WIDTH; j++) {
      field[i][j + 4] = stage[i][j + 4] + block[i][j];
      /* 初期位置に置いたブロックが既に固定ブロックに重なっていればゲームオーバー */
      if (field[i][j + 4] > 1) {
        gameover = TRUE;
        return;
      }
    }
  }
  srand(gettimeofday_sec());
}
Example #7
0
/*!
 * @brief 様々な初期化設定を行う
 *
 * シグナルハンドラを設定し、画面を初期化する。<br>
 * また、ブロックを生成する。
 */
static void initialize(int sock) {
  uint i;

  sig_sock = sock;                   /* シグナルハンドラが参照できるように、グローバル変数にソケットを記憶 */
  signal(SIGINT, sig_handler);       /* シグナルハンドラの設定 */
  srand(gettimeofday_sec());         /* 乱数の種の設定 */
  initscr();                         /* 画面を初期化する */
  clear();
  cbreak();                          /* 入力をバッファに溜め込まないようにする */
  noecho();                          /* エコーバックを行わないようにする */
  fcntl(sock, F_SETFL, O_NONBLOCK);  /* sockからのread()をノンブロッキングに(リアルタイム処理を可能にする) */
  nodelay(stdscr, TRUE);             /* getch()をノンブロッキングに */
  print_labels();                    /* ラベルを描画する */

  /* 画面と壁を初期設定 */
  for (i = 0; i < STAGE_HEIGHT; i++) {
    uint j;
    for (j = 0; j < STAGE_WIDTH; j++) {
      if ((j == 0) || (j == STAGE_WIDTH - 1) || (i == STAGE_HEIGHT - 1)) {
        field[i][j] = stage[i][j] = WALL;
      } else {
        field[i][j] = stage[i][j] = SPACE;
      }
    }
  }
  create_block();                 /* 最初のブロック発生させる */
  show_field(field, MY_FIELD_X);  /* ゲーム直後の画面を描画 */
}
Example #8
0
int main(int argc, char *argv[])
{
  if (argc != 3) {
    std::cerr << "Usage: " << argv[0] << " dbname record_num" << std::endl; 
    exit(1);
  }
  int rnum = atoi(argv[2]);

  double t1, t2;
  t1 = gettimeofday_sec();
  Lux::IO::Btree *bt = new Lux::IO::Btree(Lux::IO::NONCLUSTER);
  if (!bt->open(argv[1], Lux::DB_RDONLY)) {
    std::cerr << "open failed" << std::endl;
    exit(1);
  }

  for (int i = 0; i < rnum; ++i) {
    char key[9];
    memset(key, 0, 9);
    sprintf(key,"%08d", i);
    char *val = new char[307201];
    sprintf(val, "%0102400d", i);
    sprintf(val + 102400, "%0102400d", i);
    sprintf(val + 102400*2, "%0102400d", i);

    Lux::IO::data_t key_data = {key, strlen(key)};
    Lux::IO::data_t *val_data = bt->get(&key_data);
    if (val_data != NULL) {
      if (strcmp(val, (char *) val_data->data) != 0) {
        std::cout << "[error] value incorrect." << (char *) val_data->data << " expected [" << val << "]" << std::endl;
      }
      bt->clean_data(val_data);
    } else {
      std::cout << "[error] entry not found for [" << key << "]" << std::endl;
    } 
    delete [] val;
  }
  bt->close();
  delete bt;

  t2 = gettimeofday_sec();
  std::cout << "get time: " << t2 - t1 << std::endl;

  return 0;
}
Example #9
0
int main(int argc, char **argv)
{
  if (argc != 5) {
    std::cerr << argv[0] << " device-name access-size access-fraction num-requests" << std::endl;
    exit(1);
  }
  char dev[128];
  sprintf(dev, "/dev/%s", argv[1]);
  int access_size = atoi(argv[2]);
  double access_fraction = atof(argv[3]);
  int num_requests = atoi(argv[4]);

  int fd = open(dev, O_RDONLY | O_DIRECT);
  if (fd < 0) {
    perror("open");
  }
  struct stat sbuf;
  if (fstat(fd, &sbuf) < 0) {
    perror("fstat");
    exit(1);
  }

  int res, t;
  res = ioctl(fd, BLKSSZGET, &t);
  off_t sz;
  res = ioctl(fd, BLKGETSIZE64, &sz);
#ifdef DEBUG
  printf("sector size: %d\n", t);
  printf("total size :%llu\n", sz);
#endif

  sz = (off_t) (sz * access_fraction);
  off_t ionum = sz / access_size;

  srand((unsigned) time(NULL));

  double t1 = gettimeofday_sec();
  random_io(fd, ionum, access_size, num_requests);
  double t2 = gettimeofday_sec();
  double iops = num_requests / (t2-t1);
  double mbps = num_requests * access_size / (t2-t1) /1024/1024;
  std::cout << access_size << " IOPS: " << iops << " MB/s: " << mbps << std::endl;

  return 0;
}
Example #10
0
Result test(const char *top, size_t n, size_t count)
{
	double begin = gettimeofday_sec();
	size_t ret = 0;
	int hit = 0;
	for (size_t i = 0; i < count; i++) {
		const char *p = top;
		int remain = n;
		while (remain > 0) {
			const char *q = Func::find(p, remain);
			if (q == 0) break;
			ret += q - p;
			hit++;
			remain -= q - p + 1;
			p = q + 1;
		}
	}
	return Result(hit, ret, (gettimeofday_sec() - begin) * 1e6 / count);
}
Example #11
0
void merge0(pos_lists_t &Z0)
{
    double t1 = gettimeofday_sec();
    Lux::IO::Btree *btZ1 = new Lux::IO::Btree(Lux::IO::NONCLUSTER);
    btZ1->set_noncluster_params(Lux::IO::Padded, Lux::IO::NOPADDING);
    if (!btZ1->open("Z1", Lux::IO::DB_CREAT)) {
        std::cerr << "open error for Z1" << std::endl;
        perror("Z1");
    }
    btZ1->set_bulk_loading(true);

    Lux::IO::Btree *btI0 = new Lux::IO::Btree(Lux::IO::NONCLUSTER);
    if (!btI0->open("I0", Lux::IO::DB_RDONLY)) {
        std::cerr << "open error for I0" << std::endl;
        perror("I0");
    }

    pos_lists_itr itr = Z0.begin();
    pos_lists_itr itr_end = Z0.end();
    Lux::IO::cursor_t *c = btI0->cursor_init();
    if (!btI0->first(c)) {
        std::cerr << "cursor first failed" << std::endl;
    }
    int i = 0;
    while (1) {
        bool is_I0_all_read = false;
        bool is_Z0_all_read = false;
        Lux::IO::data_t *key;
        Lux::IO::data_t *val;
        if (!btI0->cursor_get(c, &key, &val, Lux::IO::SYSTEM)) {
            std::cerr << "cursor_get failed" << std::endl;
        }

        int n = strncmp(itr->first.c_str(), (char *) key->data, key->size);
        if (n > 0) {
            /*
            std::cout << "write I0: ";
            std::cout.write((char *) key->data, key->size);
            std::cout << std::endl;
            */
            // put disk side
            btZ1->put(key, val);
            if (!btI0->next(c)) {
                is_I0_all_read = true;
            }
        } else if (n == 0) {
            /*
            std::cout << "write I0+Z0 : ";
            std::cout.write((char *) key->data, key->size);
            std::cout << std::endl;
            */
            // combine disk + memory
            char *p;
            size_t val_size;
            serialize_pos_list(itr->second, &p, &val_size);
            size_t new_val_size = val->size + val_size;
            char *new_val = new char[new_val_size];
            memcpy(new_val, val->data, val->size);
            memcpy(new_val + val->size, p, val_size);

            Lux::IO::data_t key = {itr->first.c_str(), itr->first.length()};
            Lux::IO::data_t val = {new_val, new_val_size};
            btZ1->put(&key, &val);
            delete [] p;
            delete [] new_val;

            if (!btI0->next(c)) {
                is_I0_all_read = true;
            }
            if (++itr == itr_end) {
                is_Z0_all_read = true;
            }

        } else {
            /*
            std::cout << "write Z0 : ";
            std::cout << itr->first;
            std::cout << std::endl;
            */
            // put memory side
            char *p;
            size_t val_size;
            serialize_pos_list(itr->second, &p, &val_size);

            Lux::IO::data_t key = {itr->first.c_str(), itr->first.length()};
            Lux::IO::data_t val = {p, val_size};
            btZ1->put(&key, &val);
            delete [] p;

            if (++itr == itr_end) {
                is_Z0_all_read = true;
            }
        }
        ++i;
        btI0->clean_data(key);
        btI0->clean_data(val);

        // if either list is read up
        if (is_I0_all_read) {
            while (itr != itr_end) {
                char *p;
                size_t val_size;
                serialize_pos_list(itr->second, &p, &val_size);

                Lux::IO::data_t key = {itr->first.c_str(), itr->first.length()};
                Lux::IO::data_t val = {p, val_size};
                btZ1->put(&key, &val);
                //std::cout << "Z0 flush: " << itr->first << std::endl;
                delete [] p;
                ++itr;
            }
            break;
        } else if (is_Z0_all_read) {
            do {
                Lux::IO::data_t *key;
                Lux::IO::data_t *val;
                if (!btI0->cursor_get(c, &key, &val, Lux::IO::SYSTEM)) {
                    std::cerr << "cursor_get failed" << std::endl;
                }
                btZ1->put(key, val);
                /*
                std::cout << "I0 flush: ";
                std::cout.write((char *) key->data, key->size);
                std::cout << std::endl;
                */

                btI0->clean_data(key);
                btI0->clean_data(val);
            } while (btI0->next(c));
            break;
        }
    }

    btI0->cursor_fin(c);
    if (!btI0->close()) {
        std::cerr << "close failed" << std::endl;
    }
    if (!btZ1->close()) {
        std::cerr << "close failed" << std::endl;
    }

    delete btI0;
    delete btZ1;
    double t2 = gettimeofday_sec();
    std::cerr << "2-way merge (I0 and Z0): " << t2 - t1 << " (s)" << std::endl;
}
Example #12
0
void pipeline_merge(pos_lists_t &Z0, uint32_t level)
{
    std::cerr << "pipelining merge - level: " << level << std::endl;
    double t1 = gettimeofday_sec();
    std::cout << "Z0 size" << Z0.size() << std::endl;

    int counter[level];
    bool cursor_finished[level];
    for (int i = 0; i < level; ++i) {
        counter[i] = 0;
        cursor_finished[i] = false;
    }

    // open index I1 - Ilevel
    std::vector<Lux::IO::Btree *> bts;
    std::vector<Lux::IO::cursor_t *> btc;

    for (int i = 0; i < level; ++i) {
        Lux::IO::Btree *bt = new Lux::IO::Btree(Lux::IO::NONCLUSTER);
        char fname[256];
        memset(fname, 0, 256);
        sprintf(fname, "I%d", i);
        if (!bt->open(fname, Lux::IO::DB_RDONLY)) {
            std::cerr << "open error for " << fname << std::endl;
            perror(fname);
        }
        Lux::IO::cursor_t *c = bt->cursor_init();
        if (!bt->first(c)) {
            std::cerr << "cursor first failed" << std::endl;
            cursor_finished[i] = true;
        }
        bts.push_back(bt);
        btc.push_back(c);

        std::cout << "I" << i << " size" << std::endl;
        bt->show_db_header();
    }

    // create I(level+1)
    Lux::IO::Btree *btI = new Lux::IO::Btree(Lux::IO::NONCLUSTER);
    btI->set_noncluster_params(Lux::IO::Padded, Lux::IO::NOPADDING);
    char fname[256];
    memset(fname, 0, 256);
    sprintf(fname, "I%d", level);
    if (!btI->open(fname, Lux::IO::DB_CREAT)) {
        std::cerr << "open error for " << fname << std::endl;
        perror(fname);
    }
    btI->set_bulk_loading(true);

    // pipiline proceeded by the specified number
    uint32_t pipeline_by_num = 10;
    pos_lists_itr itr_end = Z0.end();
    for (pos_lists_itr itr = Z0.begin(); itr != itr_end; ++itr) {

        data_lists_t dlist;
        char *base_str;
        int k = 0;
        do {
            std::pair<std::string, Lux::IO::data_t *> list;
            list.first = itr->first;
            char *p;
            size_t val_size;
            serialize_pos_list(itr->second, &p, &val_size);
            // FIXME: this must be allocated in heap (free store)
            Lux::IO::data_t *v = new Lux::IO::data_t;
            v->data = p;
            v->size = val_size;
            list.second = v;
            dlist.insert(list);
            base_str = (char *) itr->first.c_str();
            ++itr;
            ++k;
        } while ((itr != itr_end) && (k < pipeline_by_num));
        --itr;

        //char *base_str = (char *) itr->first.c_str();
        for (int i = 0; i < level; ++i) {
            Lux::IO::data_t *key;
            Lux::IO::data_t *val;
            while (!cursor_finished[i]) {
                if (!bts[i]->cursor_get(btc[i], &key, &val, Lux::IO::SYSTEM)) {
                    std::cerr << "cursor_get failed" << std::endl;
                }
                int n = strcmp(base_str, (char *) key->data);
                if (n < 0) {
                    bts[i]->clean_data(key);
                    bts[i]->clean_data(val);
                    break;
                } else {
                    // add
                    std::pair<std::string, Lux::IO::data_t *> list;
                    list.first = std::string((char *) key->data);
                    list.second = val;

                    data_lists_t::iterator d_itr = dlist.find(list.first);
                    if (d_itr != dlist.end()) {
                        // concatenate
                        uint32_t newsize = d_itr->second->size + val->size;
                        char *newlist = new char[newsize];
                        char *p = newlist;
                        memcpy(p, d_itr->second->data, d_itr->second->size);
                        memcpy(p + d_itr->second->size, val->data, val->size);
                        Lux::IO::data_t *newval = new Lux::IO::data_t;
                        newval->data = newlist;
                        newval->size = newsize;
                        // old second must be deleted (the pointer and the pointing data also)
                        delete [] (char *) (d_itr->second->data);
                        delete d_itr->second;
                        d_itr->second = newval;

                        // deleting in this case only
                        bts[i]->clean_data(val);
                    } else {
                        // just add
                        dlist.insert(list);
                    }
                    bts[i]->clean_data(key);
                }
                ++counter[i];
                if (!bts[i]->next(btc[i])) {
                    cursor_finished[i] = true;
                }
            }
        }

        // write to I(level+1)
        data_lists_itr d_itr_end = dlist.end();
        for (data_lists_itr d_itr = dlist.begin(); d_itr != d_itr_end; ++d_itr) {
            Lux::IO::data_t key = {d_itr->first.c_str(), d_itr->first.length()};
            Lux::IO::data_t *val = d_itr->second;
            btI->put(&key, val);
            delete [] (char *) (d_itr->second->data);
            delete d_itr->second;
        }
        dlist.clear();
    }

    /* TODO: when Z0 list is finished first, then remaining lists from another Ii is not indexed. */
    // FIXME: bad code. basic procedure is very similar to the above one. better to integrate.
    // Ii is the base
    for (int i = 0; i < level; ++i) {
        while (!cursor_finished[i]) {
            Lux::IO::data_t *key;
            Lux::IO::data_t *val;
            if (!bts[i]->cursor_get(btc[i], &key, &val, Lux::IO::SYSTEM)) {
                std::cerr << "cursor_get failed" << std::endl;
            }

            data_lists_t dlist;
            std::pair<std::string, Lux::IO::data_t *> list;
            list.first = std::string((char *) key->data);
            list.second = val;
            dlist.insert(list);

            char *base_str = (char *) key->data;
            for (int j = i + 1; j < level; ++j) {

                while (!cursor_finished[j]) {
                    Lux::IO::data_t *k;
                    Lux::IO::data_t *v;
                    if (!bts[j]->cursor_get(btc[j], &k, &v, Lux::IO::SYSTEM)) {
                        std::cerr << "cursor_get failed" << std::endl;
                    }
                    int n = strcmp(base_str, (char *) k->data);
                    if (n < 0) {
                        break;
                    } else {
                        // add
                        std::pair<std::string, Lux::IO::data_t *> list;
                        list.first = std::string((char *) k->data);
                        list.second = v;

                        data_lists_t::iterator d_itr = dlist.find(list.first);
                        if (d_itr != dlist.end()) {
                            // concatenate
                            uint32_t newsize = d_itr->second->size + v->size;
                            char *newlist = new char[newsize];
                            char *p = newlist;
                            memcpy(p, d_itr->second->data, d_itr->second->size);
                            memcpy(p + d_itr->second->size, v->data, v->size);
                            Lux::IO::data_t *newval = new Lux::IO::data_t;
                            newval->data = newlist;
                            newval->size = newsize;
                            d_itr->second = newval;
                        } else {
                            // just add
                            dlist.insert(list);
                        }
                    }
                    if (!bts[j]->next(btc[j])) {
                        cursor_finished[j] = true;
                    }
                }
            }
            // write to I(level+1)
            data_lists_itr d_itr_end = dlist.end();
            for (data_lists_itr d_itr = dlist.begin(); d_itr != d_itr_end; ++d_itr) {
                Lux::IO::data_t key = {d_itr->first.c_str(), d_itr->first.length()};
                Lux::IO::data_t *val = d_itr->second;
                btI->put(&key, val);
            }
            dlist.clear();

            if (!bts[i]->next(btc[i])) {
                cursor_finished[i] = true;
            }
        }
    }

    for (int i = 0; i < level; ++i) {
        if (cursor_finished[i]) {
            std::cout << "true ";
        } else {
            std::cout << "false ";
        }
    }
    std::cout << std::endl;

    for (int i = 0; i < bts.size(); ++i) {
        bts[i]->cursor_fin(btc[i]);
        bts[i]->close();
        delete bts[i];
    }

    if (!btI->close()) {
        perror("btI");
    }
    delete btI;
    double t2 = gettimeofday_sec();
    std::cerr << level+1 << "-way merge: " << t2 - t1 << " (s)" << std::endl;
}
Example #13
0
void pipeline_merge_linear(pos_lists_t &Z0, uint32_t level)
{
    std::cerr << "pipelining merge - level: " << level << std::endl;
    double t1 = gettimeofday_sec();

    // +1: for Z0, I0NUM-1: I0 level index exists (I0NUM-1) more.
    int kway = level + 1 + I0NUM - 1;

    // couter[0] and cursor_finished[0] for Z0
    int counter[kway];
    bool cursor_finished[kway];
    for (int i = 0; i < kway; ++i) {
        counter[i] = 0;
        cursor_finished[i] = false;
    }

    std::vector<Lux::IO::Btree *> bts;
    std::vector<Lux::IO::cursor_t *> btc;
    //str_list current_key;
    std::string current_key[kway];
    //data_list current_data;
    Lux::IO::data_t *current_data[kway];
    //current_key.reserve(level+1);
    //current_data.reserve(level+1);

    for (int i = 1; i < kway; ++i) {
        char fname[256];
        memset(fname, 0, 256);
        if (i >= 1 && i <= (1 + I0NUM - 1)) { // for I0 level
            sprintf(fname, "I0-%d", i-1);
        } else { // for other levels (I1 ... IN)
            sprintf(fname, "I%d", i-I0NUM);
        }

        Lux::IO::Btree *bt = new Lux::IO::Btree(Lux::IO::NONCLUSTER);
        if (!bt->open(fname, Lux::IO::DB_RDONLY)) {
            std::cerr << "open error for " << fname << std::endl;
            perror(fname);
            exit(1);
        }
        Lux::IO::cursor_t *c = bt->cursor_init();
        if (!bt->first(c)) {
            std::cerr << "cursor first failed" << std::endl;
            cursor_finished[i] = true;
        } else {
            Lux::IO::data_t *key;
            Lux::IO::data_t *val;
            if (!bt->cursor_get(c, &key, &val, Lux::IO::SYSTEM)) {
                std::cerr << "cursor_get failed" << std::endl;
            } else {
                current_key[i] = std::string((char *) key->data);
                current_data[i] = val;
                bt->clean_data(key);
            }
        }
        bts.push_back(bt);
        btc.push_back(c);
    }

    // create I(level+1)
    Lux::IO::Btree *btI = new Lux::IO::Btree(Lux::IO::NONCLUSTER);
    btI->set_noncluster_params(Lux::IO::Padded, Lux::IO::NOPADDING);
    char fname[256];
    memset(fname, 0, 256);
    sprintf(fname, "I%d", level);
    if (!btI->open(fname, Lux::IO::DB_CREAT)) {
        std::cerr << "open error for " << fname << std::endl;
        perror(fname);
    }
    btI->set_bulk_loading(true);

// tmporary
#define Z0NEXT() \
  if (itr == itr_end) { \
    cursor_finished[0] = true; \
  } else { \
    current_key[0] = itr->first; \
    char *p; \
    size_t val_size; \
    serialize_pos_list(itr->second, &p, &val_size); \
    Lux::IO::data_t *dt = new Lux::IO::data_t; \
    dt->data = p; \
    dt->size = val_size; \
    current_data[0] = dt; \
  }

#define IiNEXT(idx) \
  if (!bts[idx-1]->next(btc[idx-1])) { \
    cursor_finished[idx] = true; \
  } else { \
    Lux::IO::data_t *key; \
    Lux::IO::data_t *val; \
    if (!bts[idx-1]->cursor_get(btc[idx-1], &key, &val, Lux::IO::SYSTEM)) { \
      std::cerr << "cursor_get failed" << std::endl; \
    } \
    current_key[idx] = std::string((char *) key->data); \
    current_data[idx] = val; \
    bts[idx-1]->clean_data(key); \
  }

    // Z1 iterator
    pos_lists_itr itr = Z0.begin();
    pos_lists_itr itr_end = Z0.end();
    Z0NEXT();

    while (1) {
        bool is_all_done = true;
        for (int i = 0; i < kway; ++i) {
            if (!cursor_finished[i]) {
                is_all_done = false;
                break;
            }
        }
        if (is_all_done) {
            break;
        }

        std::string smallest_key;
        std::vector<int> smallest_i;
        int num_smallest = 0;

        bool is_set = false;
        for (int i = 0; i < kway; ++i) {
            if (cursor_finished[i]) {
                continue;
            }
            if (!is_set) {
                // set first entry
                smallest_key = current_key[i];
                smallest_i.push_back(i);
                is_set = true;
                num_smallest = 1;
            } else {
                if (current_key[i] < smallest_key) {
                    smallest_key = current_key[i];
                    smallest_i.push_back(i);
                    num_smallest = 1;
                } else if (current_key[i] == smallest_key) {
                    smallest_i.push_back(i);
                    ++num_smallest;
                }
            }
        }

        // comparison for smallest
        if (is_set) {
            // num_smallest from the end
            if (num_smallest > 1) {
                // combine
                size_t size = smallest_i.size();
                size_t total = 0;
                for (int i = size - num_smallest; i < size; ++i) {
                    int idx = smallest_i[i];
                    total += current_data[idx]->size;
                }
                Lux::IO::data_t new_data;
                new_data.data = new char[total];
                new_data.size = total;
                char *p = (char *) new_data.data;
                for (int i = size - num_smallest; i < size; ++i) {
                    int idx = smallest_i[i];
                    memcpy(p, current_data[idx]->data, current_data[idx]->size);
                    p += current_data[idx]->size;
                    btI->clean_data(current_data[idx]);
                    if (idx == 0) {
                        ++itr;
                        Z0NEXT();
                    } else {
                        IiNEXT(idx);
                    }
                }

                Lux::IO::data_t key = {smallest_key.c_str(), smallest_key.length()};
                Lux::IO::data_t *val = &new_data;
                btI->put(&key, val);
                delete [] (char *) (new_data.data);

            } else {
                // output the key and value
                int idx = smallest_i[smallest_i.size()-1];
                Lux::IO::data_t key = {smallest_key.c_str(), smallest_key.length()};
                Lux::IO::data_t *val = current_data[idx];
                btI->put(&key, val);
                btI->clean_data(val);

                // forward the cursor to the next
                if (idx == 0) { // Z0
                    ++itr;
                    Z0NEXT();
                } else {
                    IiNEXT(idx);
                }
            }
        }

    }

    for (int i = 0; i < bts.size(); ++i) {
        bts[i]->cursor_fin(btc[i]);
        bts[i]->close();
        delete bts[i];
    }

    if (!btI->close()) {
        perror("btI");
    }
    delete btI;

    double t2 = gettimeofday_sec();
    std::cerr << level+1 << "-way merge: " << t2 - t1 << " (s)" << std::endl;
}
int main()
{
  int i;

  /* for sequential use */
  gsoc_taskqueue* q;
  gsoc_task *tasks;

  /* for parallel use */
  long num_cpu = sysconf(_SC_NPROCESSORS_CONF);
  gsoc_taskqueue *taskqs[num_cpu];
  cpu_set_t cpuset;
  pthread_t tids[num_cpu];
  worker workers[num_cpu];
  int logged_worker = random() % num_cpu;
  double t1, t2;

  /* initializations for test */
  tasks = malloc(sizeof(gsoc_task) * GSOC_TASKQUEUE_INIT_SIZE * TESTVAL_EXTENDS);
  for (i = 0; i < GSOC_TASKQUEUE_INIT_SIZE * TESTVAL_EXTENDS; ++i)
    tasks[i].threadid = i;
  q = gsoc_taskqueue_new();
  for (i = 0; i < num_cpu; ++i)
    taskqs[i] = gsoc_taskqueue_new();

  /* push+pop */
  for (i = 0; i < GSOC_TASKQUEUE_INIT_SIZE * TESTVAL_EXTENDS; ++i)
    gsoc_taskqueue_push(q, &tasks[i]);
  for (i = GSOC_TASKQUEUE_INIT_SIZE * TESTVAL_EXTENDS - 1; i >= 0 ; --i)
    assert(gsoc_taskqueue_pop(q)->threadid == i);
  /* pop for empty deque */
  assert(gsoc_taskqueue_pop(q) == NULL);

  /* push+take */
  for (i = 0; i < GSOC_TASKQUEUE_INIT_SIZE * TESTVAL_EXTENDS; ++i)
    gsoc_taskqueue_push(q, &tasks[i]);
  for (i = 0; i < GSOC_TASKQUEUE_INIT_SIZE * TESTVAL_EXTENDS; ++i)
    assert(gsoc_taskqueue_take(q)->threadid == i);
  /* take for empty deque */
  assert(gsoc_taskqueue_take(q) == NULL);


  /* Emulate workers */
  fprintf(stderr, "==========\nWith %d CPUs\n===========\n", (int)num_cpu);

  t1 = gettimeofday_sec();

  for (i = 0; i < num_cpu; ++i) {
    CPU_ZERO(&cpuset);
    CPU_SET(i, &cpuset);
    workers[i].cpu = cpuset;
    workers[i].id = i;
    workers[i].my_taskq = taskqs[i];
    workers[i].tasks = tasks;
    workers[i].workers = workers;
    workers[i].num_workers = num_cpu;
    workers[i].logged_worker = logged_worker;

    pthread_create(&tids[i], NULL, parallel_push_pop_take, &workers[i]);
  }
  for (i = 0; i < num_cpu; ++i)
    pthread_join(tids[i], NULL);

  t2 = gettimeofday_sec();

  fprintf(stderr, "%f sec elaplsed for parallel_push_pop_take()\n", t2-t1);

  /* finalization for test */
  for (i = 0; i < num_cpu; ++i)
    gsoc_taskqueue_delete(taskqs[i]);
  gsoc_taskqueue_delete(q);
  free(tasks);

  return 0;
}
Example #15
0
void Z0_flush(std::vector<bool> &indexes, pos_lists_t &Z0)
{
  std::cout << "flushing ..." << std::endl;
  double t1 = gettimeofday_sec();

  int i = 0;
  indexes.push_back(false);
  while (1) {
    // pipelining test
    uint32_t pipeline_level = 0;
    if (is_pipelining) {
      for (int j = 0; j < indexes.size(); ++j) {
        if (indexes[j]) {
          ++pipeline_level;
        } else {
          break;
        }
      }
      if (pipeline_level >= 1) {
        std::cout << "multiway-merge" << std::endl;
        // do pipelining
        //pipeline_merge(Z0, pipeline_level);
        pipeline_merge_linear(Z0, pipeline_level);
        if (unlink("Z0.bidx") < 0) { perror("Z0.bidx"); }
        if (unlink("Z0.data") < 0) { perror("Z0.data"); }
        for (int k = 0; k < pipeline_level; ++k) {
          indexes[k] = false;
          char fname1[256];
          char fname2[256];
          sprintf(fname1, "I%d.bidx", k);
          sprintf(fname2, "I%d.data", k);
          if (unlink(fname1) < 0) { perror(fname1); }
          if (unlink(fname2) < 0) { perror(fname2); }
        }
        indexes[pipeline_level] = true;
        break;
      }
    }
    
    if (indexes[i]) {
      merge(Z0, i);
      indexes[i] = false;
      // [NOTICE] they shouldn't be removed here.
      char fname1[256];
      char fname2[256];
      sprintf(fname1, "I%d.bidx", i);
      sprintf(fname2, "I%d.data", i);
      if (unlink(fname1) < 0) { perror("unlink failed"); }
      if (unlink(fname2) < 0) { perror("unlink failed"); }
    } else {
      dump(Z0, i);
      indexes[i] = true;
      break;
    }
    ++i;
    indexes.push_back(false);
  }
  //pos_lists_t().swap(Z0);
  double t2 = gettimeofday_sec();
  std::cout << "flush(merge+I/O) time: " << t2 - t1 << std::endl;
}
Example #16
0
int main (int argc, const char * argv[])
{
    if (false) {
        double t0, t1, t2, t3;
        
        t0 = gettimeofday_sec();
        
        int width = 200;
        int height = 200;
        int n = 2000000;
        //  n= 2 millionで12secほどaddにかかる( nlogn ) 
        QuadTree<Location> *tree = new QuadTree<Location>(Region(0,0,width,height));
        for ( int i = 0; i < n; i++ ) {
            double x = width * ((double)(rand() % n) - (n/2.0)) / n;
            double y = height * ((double)(rand() % n) - (n/2.0)) / n;
            Location *l = new Location(x,y);
            tree->add(l);
        }
        t1 = gettimeofday_sec();
        
        int count = 0;
        std::vector<QuadTree<Location> *> foundTree = tree->findRegion(Region(70,-30,1,1));
        for ( int i = 0 ; i < foundTree.size(); i++ ) {
            count += foundTree[i]->getObjects().size();
        }
        t2 = gettimeofday_sec();
        
        Location *l = tree->findFirstObjectInRegion(Region(700, 45.23, 1, 0.4));
        t3 = gettimeofday_sec();
        
        printf("get all objects -> %d\n", (int)tree->getObjects().size());
        printf("find region objects - > %d\n", count);
        if ( l != NULL ) {
            printf("find first (%f, %f)\n", l->x, l->y);
        }
        
        printf("%d add time: %g\n", n, t1-t0);
        printf("findRegion time: %g\n", t2-t1);
        printf("findFirst time: %g\n", t3-t2);
    }
    
    
    //  findFirst() test
    if (true) {
        int m = 20;
        int n = 20;
        QuadTree<Location> *tree = new QuadTree<Location>(Region(140,40, m, n));
        LadyBug *ladybug = new LadyBug("", "/Users/asai/Desktop/gps.txt");
        std::vector<Location *> *locs = ladybug->getLocations();
        for ( unsigned int i = 0; i < locs->size(); i++ ) {
            if ( !locs->at(i)->isError ) {
                tree->add(locs->at(i));
            }
        }
        tree->dump();
        
        for ( int i = 0; i < m; i++ ) {
            for ( int j = 0; j < n; j++ ) {
                Region r = Region(140 - m/2 + i, 40 - n/2 + j, 2, 2);
                Location *l = tree->findFirstObjectInRegion(r);
                if ( l != NULL ) {
                    printf("found!");
                    r.dump();
                } else {
                    int count = 0;
                    std::vector<QuadTree<Location> *> foundTree = tree->findRegion(r);
                    for ( int k = 0 ; k < foundTree.size(); k++ ) {
                        count += foundTree[k]->getObjects().size();
                    }
                    printf("not found but %d found by getObjects()\n", count);
                }
                
            }
        }
    }
    
    
    return 0;
}
Example #17
0
int main(int argc, char *argv[])
{
  FILE *fp, *fp2;
  char buf[100000][100];	/*命令 読み込み用バッファ */
  char fpt[FPT_MAX_COUNT][20] = { 0 };
  char ofname[1000] = "result";
  int i = 0, j, k;
  //メモリ上の浮動小数点テーブルの位置
  int fpmemoffset = 0;
  int result;
  bool doprintregs = false, doprintmem = false;
  program *answer;
  program2 *answer2;
  double t2, t1;
  int ll = 0;

  while ((result = getopt(argc, argv, "rmo:")) != -1) {
    switch (result) {

      /* 値をとらないオプション */
    case 'r':
      /* getoptの返り値は見付けたオプションである. */
      doprintregs = true;
      break;
    case 'm':
      doprintmem = true;
      break;

    case 'o':
      /* 値を取る引数の場合は外部変数optargにその値を格納する. */
      strcpy(ofname, optarg);
      break;
    }
  }
  argc -= optind;
  argv += optind;


  /*assembler-file open and read */
  fp = fopen(argv[0], "r");
  if (fp == NULL) {
    printf("%sが開けませんaa\n", argv[0]);
    return 1;
  }

  if (argc > 1) {
    fp2 = fopen(argv[1], "r");
    if (fp2 == NULL) {
      printf("%sが開けません\n", argv[1]);
      return 1;
    }
    for (i = 0; i < FPT_MAX_COUNT; ++i) {
      if (fgets(fpt[i], 20, fp2) == NULL) {	/* 1行読み込み */
	break;		/* 末尾まで完了したか、エラー発生で終了 */
      }
    }

    for (j = 0; j < i; ++j) {
      //エラーが起こっても関係ない
      //printf("%s\n", fpt[j]);
      memory[j + fpmemoffset].i = (int) strtoll(fpt[j], NULL, 16);
    }
    //print_memory();
  }

  i = 0;
  while (1) {
    if (fgets(buf[i], 81, fp) == NULL) {	/* 1行読み込み */
      break;		/* 末尾まで完了したか、エラー発生で終了 */
    }
    i++;
  }

  out_fp = fopen(ofname, "w");

  answer = parse_all(buf, i);
  answer2 = parse_all2(parse_all(buf, i));
  
  //print_program(answer);

  t1 = gettimeofday_sec();
  do_assemble2(answer, answer2);
  t2 = gettimeofday_sec();
  
  printf("%lf\n", t2 - t1);
  printf("%lld\n", cnt);

  if (doprintregs)
    print_register();
  if (doprintmem)
    print_memory();

  fclose(fp);
  fclose(fp2);
  fclose(out_fp);
    
  return 0;
}
Example #18
0
int main(int argc, char *argv[])
{
  if (argc < 6 || argc > 7) {
    std::cerr << argv[0] << " device-name access-size access-from-loc access-range num-requests [num-threads]" << std::endl;
    exit(1);
  }
  char dev[128];
  //sprintf(dev, "/dev/%s", argv[1]);
  sprintf(dev, "%s", argv[1]);
  access_size = atoi(argv[2]);
  access_from_loc = atoll(argv[3]);
  access_range = atoll(argv[4]);
  num_requests = atoi(argv[5]);
  num_threads = NUM_THREAD;
  if (argc == 7) {
    num_threads = atoi(argv[6]);
  }
  iops = new double[num_threads];
  mbps = new double[num_threads];
  int fd;
  struct stat sbuf;
  if (stat(dev, &sbuf) < 0) {
    perror("fstat");
    exit(1);
  }

  int res, t;
  u64 sz;
  if (S_ISBLK(sbuf.st_mode)) {
    fd = open(dev, O_RDONLY | O_DIRECT);
    res = ioctl(fd, BLKSSZGET, &t);
    res = ioctl(fd, BLKGETSIZE64, &sz);
  } else {
    fd = open(dev, O_RDONLY);
    sz = sbuf.st_size;
  }
  if (fd < 0) {
    perror("open");
  } 

#ifdef DEBUG
  printf("sector size: %d\n", t);
  printf("total size :%llu\n", sz);
#endif

  //sz *= access_fraction;
  ionum = access_range / access_size;
  access_from_block = access_from_loc / access_size;
  printf("ionum: %llu\n", ionum);
  printf("access_from_block :%llu\n", access_from_block);

  srand((unsigned) time(NULL));
  pthread_t tid[num_threads];
  thread_arg_t arg[num_threads];
  double t1 = gettimeofday_sec();
  for (int i = 0; i < num_threads; ++i) {
    arg[i].id = i;
    arg[i].fd = fd;
    if (pthread_create(&tid[i], NULL, random_io_reader, (void *) &arg[i]) != 0) {
      perror("pthread_create");
      exit(1);
    }
  }

  void *ret = NULL;
  for (int i = 0; i < num_threads; ++i) {
    if (pthread_join(tid[i], &ret)) {
      perror("pthread_join");
    }
  }
  double t2 = gettimeofday_sec();
  double iops_total = 0.0;
  double mbps_total = 0.0;
  for (int i = 0; i < num_threads; ++i) {
    iops_total += iops[i];
    mbps_total += mbps[i];
  }
  //std::cout << access_size << " IOPS: " << iops_total << " MB/s: " << mbps_total/1024/1024 << std::endl;
  //std::cout << access_size << " IOPS: " << num_requests/(t2-t1) << " MB/s: " << (num_requests*access_size)/(t2-t1)/1024/1024 << std::endl;
  std::cout << access_size << " IOPS: " << counter/(t2-t1) << " MB/s: " << (counter*access_size)/(t2-t1)/1024/1024 << std::endl;
  delete iops;
  delete mbps;

  return 0;
}
Example #19
0
void Wait(double wait_time){
    double start_time=gettimeofday_sec();
    while(gettimeofday_sec()<wait_time+start_time)
	if(wait_time>0)sleep(1);
}
Example #20
0
void MainLoop()
{
    //QueryPerformanceFrequency(&nFreq);
    //QueryPerformanceCounter(&nBefore);
    
    DWORD StartTime,EndTime,PassTime;
    double l_StartTime,l_EndTime,l_PassTime;
#ifdef _WIN32
    StartTime=timeGetTime();
#else
    l_StartTime=gettimeofday_sec();
#endif
    ARUint8		*image;
    ARMarkerInfo	*marker_info;
    int			 marker_num;
    int			 j,k;

    if( (image = (ARUint8*)arVideoGetImage() )==NULL){
	arUtilSleep(2);
	return;
    }

    argDrawMode2D();
    argDispImage(image, 0, 0);


    if(arDetectMarker(image, thresh, &marker_info, &marker_num) < 0){
	CleanUp();
	exit(0);
    }
    arVideoCapNext();

    k=-1;
    for(j=0;j<marker_num;j++){   
	if(patt_id==marker_info[j].id){
	    k = (k==-1)  ?   j : k;
	    k = (marker_info[k].cf < marker_info[j].cf)   ?  j: k;
	}
    }

    if(k!=-1) {
	if(isFirst==true)
	    nyar_NyARTransMat_O2_transMat(nyobj,&marker_info[k],patt_center,patt_width,patt_trans);
	else
	    nyar_NyARTransMat_O2_transMatCont(nyobj,&marker_info[k],patt_trans,patt_center,patt_width,patt_trans);

	isFirst=false;

	if(GameOver==false){
	    if(arUtilTimer()>1.0){
		MovePiece(3,f,p); 
		score+=f.ShiftPiece(f.deletePiece());
		arUtilTimerReset();
		GameOver=GameOverCheck(f,p);
	    }
	}
	else{
	    if(arUtilTimer()>15.0)
		InitGame();
	}
	DrawObject();
    }

    argSwapBuffers();
#ifdef _WIN32
    EndTime=timeGetTime();
    PassTime=EndTime-StartTime;
    (1000/FPS>PassTime)?Wait(1000/FPS-PassTime):Wait(0);
    FPSCount(&fps);
    printf("FPS=%d\n",fps);
#else
l_EndTime=gettimeofday_sec();
l_PassTime=l_EndTime-l_StartTime;
    ((double)(1000/FPS)>l_PassTime)?Wait((double)1000/FPS-l_PassTime):Wait(0);
    FPSCount(&fps);
    printf("FPS=%d\n",fps);
#endif
   
}
Example #21
0
void mergei(int i)
{
    double t1 = gettimeofday_sec();
    // merge Ii with temporary index Zi
    Lux::IO::Btree *btZi1 = new Lux::IO::Btree(Lux::IO::NONCLUSTER);
    btZi1->set_noncluster_params(Lux::IO::Padded, Lux::IO::NOPADDING);
    char fname[256];
    sprintf(fname, "Z%d", i+1);
    if (!btZi1->open(fname, Lux::IO::DB_CREAT)) {
        std::cerr << "open error for " << fname << std::endl;
        perror("Zi1");
    }
    btZi1->set_bulk_loading(true);

    memset(fname, 0, 256);
    sprintf(fname, "Z%d", i);
    Lux::IO::Btree *btZi = new Lux::IO::Btree(Lux::IO::NONCLUSTER);
    if (!btZi->open(fname, Lux::IO::DB_RDONLY)) {
        std::cerr << "open error for " << fname << std::endl;
        perror("Zi");
    }
    memset(fname, 0, 256);
    sprintf(fname, "I%d", i);
    Lux::IO::Btree *btIi = new Lux::IO::Btree(Lux::IO::NONCLUSTER);
    if (!btIi->open(fname, Lux::IO::DB_RDONLY)) {
        std::cerr << "open error for " << fname << std::endl;
        perror("Ii");
    }

    Lux::IO::cursor_t *cI = btIi->cursor_init();
    Lux::IO::cursor_t *cZ = btZi->cursor_init();
    if (!btIi->first(cI)) {
        std::cerr << "first cursor error for btIi" << std::endl;
    }
    if (!btZi->first(cZ)) {
        std::cerr << "first cursor error for btZi" << std::endl;
    }
    int j = 0;
    while (1) {
        bool is_Ii_all_read = false;
        bool is_Zi_all_read = false;

        Lux::IO::data_t *keyI;
        Lux::IO::data_t *valI;
        if (!btIi->cursor_get(cI, &keyI, &valI, Lux::IO::SYSTEM)) {
            std::cerr << "cursor_get failed" << std::endl;
        }
        Lux::IO::data_t *keyZ;
        Lux::IO::data_t *valZ;
        if (!btZi->cursor_get(cZ, &keyZ, &valZ, Lux::IO::SYSTEM)) {
            std::cerr << "cursor_get failed" << std::endl;
        }

        int n = strncmp((char *) keyI->data, (char *) keyZ->data, keyI->size > keyZ->size ? keyZ->size : keyI->size);
        if (n > 0 || (n == 0 && keyI->size > keyZ->size)) {
            // put disk side
            btZi1->put(keyZ, valZ);
            if (!btZi->next(cZ)) {
                is_Zi_all_read = true;
            }
        } else if (n < 0 || (n == 0 && keyZ->size > keyI->size)) {
            // put disk side
            btZi1->put(keyI, valI);
            if (!btIi->next(cI)) {
                is_Ii_all_read = true;
            }
        } else {
            size_t new_val_size = valI->size + valZ->size;
            char *new_val = new char[new_val_size];
            memcpy(new_val, valI->data, valI->size);
            memcpy(new_val + valI->size, valZ->data, valZ->size);

            Lux::IO::data_t val = {new_val, new_val_size};
            btZi1->put(keyI, &val);
            delete [] new_val;
            if (!btIi->next(cI)) {
                is_Ii_all_read = true;
            }
            if (!btZi->next(cZ)) {
                is_Zi_all_read = true;
            }
        }
        btIi->clean_data(keyI);
        btIi->clean_data(valI);
        btZi->clean_data(keyZ);
        btZi->clean_data(valZ);

        if (is_Zi_all_read && !is_Ii_all_read) {
            do {
                Lux::IO::data_t *key;
                Lux::IO::data_t *val;
                if (!btIi->cursor_get(cI, &key, &val, Lux::IO::SYSTEM)) {
                    std::cerr << "cursor_get failed" << std::endl;
                }
                btZi1->put(key, val);
                /*
                std::cout << "Ii flush remainings: ";
                std::cout.write((char *) key->data, key->size);
                std::cout << std::endl;
                */
                btIi->clean_data(key);
                btIi->clean_data(val);
            } while (btIi->next(cI));
            break;
        } else if (is_Ii_all_read && !is_Zi_all_read) {
            do {
                Lux::IO::data_t *key;
                Lux::IO::data_t *val;
                if (!btZi->cursor_get(cZ, &key, &val, Lux::IO::SYSTEM)) {
                    std::cerr << "cursor_get failed" << std::endl;
                }
                btZi1->put(key, val);
                /*
                std::cout << "Zi flush remainings: ";
                std::cout.write((char *) key->data, key->size);
                std::cout << std::endl;
                */
                btZi->clean_data(key);
                btZi->clean_data(val);
            } while (btZi->next(cZ));
            break;
        } else if (is_Ii_all_read && is_Zi_all_read) {
            break;
        }
        ++j;
        //std::cout << j << std::endl;
    }

    btIi->cursor_fin(cI);
    btZi->cursor_fin(cZ);
    if (!btIi->close()) {
        std::cerr << "close failed" << std::endl;
    }
    delete btIi;
    if (!btZi->close()) {
        std::cerr << "close failed" << std::endl;
    }
    delete btZi;
    if (!btZi1->close()) {
        std::cerr << "close failed" << std::endl;
    }
    delete btZi1;

    char fname1[256];
    char fname2[256];
    sprintf(fname1, "Z%d.bidx", i);
    sprintf(fname2, "Z%d.data", i);
    if (unlink(fname1) < 0) {
        perror("unlink failed");
    }
    if (unlink(fname2) < 0) {
        perror("unlink failed");
    }
    double t2 = gettimeofday_sec();
    std::cerr << "2-way merge (I" << i << " and Z" << i << "): " << t2 - t1 << " (s)" << std::endl;
}
Example #22
0
/**
 * 学習処理
 * const char * path:保存先フォルダ
 */
int learningNeuralNetworkPP(const char * path) {
	if (state < 1) {
		printf("Not create neural network.\n");
		return -1;
	}
	/*処理開始*/
	start_time = gettimeofday_sec();
	int i, j;
	x = (double *) malloc(sizeof(double) * input.pattern * input.num);
	d = (double *) malloc(sizeof(double) * input.pattern * output.num);

	/*ログ用変数*/
	int log_count = 0;/*ログをテキストに書きだした回数*/
	int save_count = 0;/*ログを書きだすべきかどうか(0ならば書き出し、それ以外は無視)*/
	/*ログユニットのメモリを確保(隠れ層)*/
	logerH.num = getIntRandom(0, hidden[0].num - 1);
	logerH.bias = (double *) malloc(sizeof(double) * array_size);
	logerH.d = (double *) malloc(sizeof(double) * array_size);
	logerH.w = (double *) malloc(sizeof(double) * input.num * array_size);
	logerH.z = (double *) malloc(sizeof(double) * array_size);
	/*ログユニットのメモリを確保(出力層)*/
	logerO.num = getIntRandom(0, output.num - 1);
	logerO.bias = (double *) malloc(sizeof(double) * array_size);
	logerO.d = (double *) malloc(sizeof(double) * array_size);
	logerO.w = (double *) malloc(sizeof(double) * hidden[numLayer - 3].num * array_size);
	logerO.z = (double *) malloc(sizeof(double) * array_size);

	/*ニューラルネットワークの処理*/
	int num = 0;/*入力するパターン番号*/
	int start;/*読み込み開始ライン*/
	for (i = 0; i < times; i++) {
		printf("learning times %d-th.\n", i);
		/*ログ処理*/
		if (i % logFre == 0) {
			/*データをログに格納*/
			logerH.bias[save_count] = hidden[0].bias[logerH.num];
			logerH.d[save_count] = 0.0;
			logerH.z[save_count] = hidden[0].z[logerH.num];
			for (j = 0; j < input.num; j++) {
				logerH.w[input.num * save_count + j] = hidden[0].w[input.num * logerH.num + j];
			}
			logerO.bias[save_count] = output.bias[logerO.num];
			logerO.d[save_count] = input.d[output.num * num + logerO.num];
			logerO.z[save_count] = output.z[logerO.num];
			for (j = 0; j < hidden[numLayer - 3].num; j++) {
				logerO.w[hidden[numLayer - 3].num * save_count + j] =
						output.w[hidden[numLayer - 3].num * logerO.num + j];
			}
			save_count++;
			if (save_count == array_size && i != 0) {
				/*書き出し処理*/
				saveLogUnit(path, "hidden.csv", array_size, &logerH, log_count * array_size, array_size, input.num);
				saveLogUnit(path, "output.csv", array_size, &logerO, log_count * array_size, array_size,
						hidden[numLayer - 3].num);
				log_count++;
				save_count = 0;
			}
		}

		/*画像の学習が一定数終わったら新しい画像へ入れ替え*/
		if (data_type == 1 && array_size < NUM_IMAGES && i != 0 && i % (array_size / 2) == 0) {
			loadLineReset();
			DataSetTrainMNIST(input.pattern, x, d);
			setInput(output.num, &input, x, d);
		} else if (data_type == 0 && array_size < image_list.imageNum && i != 0 && i % (array_size / 2) == 0) {
			start = getIntRandom(0, image_list.imageNum - array_size);
			inputToImageData(&image_list, start, input.pattern, x, d);
			setInput(output.num, &input, x, d);
		}

		/*入力値を決定*/
		num = getIntRandom(0, input.pattern - 1);

		/*dropoutの判定*/
		if (dropout > 0) {
			setInputLayerDropout(&input);
			for (j = 0; j < numLayer - 2; j++) {
				setDropout(&hidden[j]);
			}
		}

		/*出力処理(中間層)*/
		for (j = 0; j < numLayer - 2; j++) {
			if (j > 0) {
				hiddenToOtherLayer(&hidden[j - 1], &hidden[j], 1.0);
			} else {
				inputToHiddenLayer(num, &input, &hidden[j], 1.0, 1.0);
			}
		}
		/*出力処理(出力層)*/
		hiddenToOtherLayer(&hidden[numLayer - 3], &output, 1.0);

		/*学習処理(出力層)*/
		learningOutputLayer(num, &input, &hidden[numLayer - 3], &output);
		/*学習処理(中間層)*/
		for (j = (numLayer - 3); j > -1; j--) {
			if (numLayer == 3) {
				learningFirstHiddenLayer(num, &input, &hidden[j], &output);
			} else if (j == (numLayer - 3)) {
				learningHiddenLayer(&hidden[j - 1], &hidden[j], &output);
			} else if (j > 0) {
				learningHiddenLayer(&hidden[j - 1], &hidden[j], &hidden[j + 1]);
			} else {
				learningFirstHiddenLayer(num, &input, &hidden[j], &hidden[j + 1]);
			}
		}

		/*dropoutを初期化*/
		if (dropout > 0) {
			resetInputLayerDropout(&input);
			for (j = 0; j < numLayer - 2; j++) {
				resetDropout(&hidden[j]);
			}
		}
	}

	/*残りのログデータを保存*/
	if (save_count != 0 || times == 1) {
		saveLogUnit(path, "hidden.csv", array_size, &logerH, log_count * array_size, save_count, input.num);
		saveLogUnit(path, "output.csv", array_size, &logerO, log_count * array_size, save_count,
				hidden[numLayer - 3].num);
	}

	/*ログユニットのメモリを解放(隠れ層)*/
	free(logerH.bias);
	free(logerH.d);
	free(logerH.w);
	free(logerH.z);
	/*ログユニットのメモリを解放(出力層)*/
	free(logerO.bias);
	free(logerO.d);
	free(logerO.w);
	free(logerO.z);

	/*ニューラルネットワークを保存*/
	printf("->Writing Neural Network\n");
	if (saveNeuralNetwork(path, numLayer, &input, &hidden, &output, dropout) < 0) {
		printf("ERROR:Not writed save data in %s.\n", path);
		/*エンターを押して終了を後で追加*/
		return -1;
	}

	/*処理終了*/
	free(x);
	free(d);
	end_time = gettimeofday_sec();
	return 1;
}
Example #23
0
void q3(void)
{
  char *l1_shipdate_rle = "l1_shipdate_rle";
  char *l1_extendedprice_uncompressed = "l1_extendedprice_uncompressed";
  char *l3_orderdate_rle = "l3_orderdate_rle";
  char *l3_custkey_uncompressed = "l3_custkey_uncompressed";
  char *l3_orderkey_uncompressed = "l3_orderkey_uncompressed";
  char *l5_customer_key_rle = "l5_customer_key_rle";
  char *l5_nationkey_uncompressed = "l5_nationkey_uncompressed";
  char *l6_orderkey_rle = "l6_orderkey_rle";
  char *l6_extendedprice_uncompressed = "l6_extendedprice_uncompressed";
  char *l6_shipdate_uncompressed = "l6_shipdate_uncompressed";
  char *ji_l3_l1 = "ji_l3-l1";

  char cstore_l1_shipdate_rle[64];
  char cstore_l1_extendedprice_uncompressed[64];
  char cstore_l3_orderdate_rle[64];
  char cstore_l3_custkey_uncompressed[64];
  char cstore_l3_orderkey_uncompressed[64];
  char cstore_l5_customer_key_rle[64];
  char cstore_l5_nationkey_uncompressed[64];
  char cstore_l6_orderkey_rle[64];
  char cstore_l6_extendedprice_uncompressed[64];
  char cstore_l6_shipdate_uncompressed[64];
  char cstore_ji_l3_l1[64];
  sprintf(cstore_l1_shipdate_rle, "%s/%s", db_name, l1_shipdate_rle);
  sprintf(cstore_l1_extendedprice_uncompressed, "%s/%s", db_name, l1_extendedprice_uncompressed);
  sprintf(cstore_l3_orderdate_rle, "%s/%s", db_name, l3_orderdate_rle);
  sprintf(cstore_l3_custkey_uncompressed, "%s/%s", db_name, l3_custkey_uncompressed);
  sprintf(cstore_l3_orderkey_uncompressed, "%s/%s", db_name, l3_orderkey_uncompressed);
  sprintf(cstore_l5_customer_key_rle, "%s/%s", db_name, l5_customer_key_rle);
  sprintf(cstore_l5_nationkey_uncompressed, "%s/%s", db_name, l5_nationkey_uncompressed);
  sprintf(cstore_l6_orderkey_rle, "%s/%s", db_name, l6_orderkey_rle);
  sprintf(cstore_l6_extendedprice_uncompressed, "%s/%s", db_name, l6_extendedprice_uncompressed);
  sprintf(cstore_l6_shipdate_uncompressed, "%s/%s", db_name, l6_shipdate_uncompressed);
  sprintf(cstore_ji_l3_l1, "%s/%s", db_name, ji_l3_l1);

  double t0 = gettimeofday_sec();
  ib_err_t err;
  //ib_crsr_t crsr_l1_s, crsr_l1_e, crsr_l3_o, crsr_l3_c, crsr_l5_c, crsr_l5_n, crsr_ji;

  ib_trx_t ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ);
  err = ib_cursor_open_table(cstore_l1_shipdate_rle, ib_trx, &crsr_l1_s);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l1_shipdate_rle << std::endl; }
  err = ib_cursor_open_table(cstore_l1_extendedprice_uncompressed, ib_trx, &crsr_l1_e);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l1_extendedprice_uncompressed << std::endl; }
  err = ib_cursor_open_table(cstore_l3_orderdate_rle, ib_trx, &crsr_l3_o);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l3_orderdate_rle << std::endl; }
  err = ib_cursor_open_table(cstore_l3_custkey_uncompressed, ib_trx, &crsr_l3_c);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l3_custkey_uncompressed << std::endl; }
  err = ib_cursor_open_table(cstore_l3_orderkey_uncompressed, ib_trx, &crsr_l3_ok);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l3_orderkey_uncompressed << std::endl; }
  err = ib_cursor_open_table(cstore_l5_customer_key_rle, ib_trx, &crsr_l5_c);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l5_customer_key_rle << std::endl; }
  err = ib_cursor_open_table(cstore_l5_nationkey_uncompressed, ib_trx, &crsr_l5_n);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l5_nationkey_uncompressed << std::endl; }
  err = ib_cursor_open_table(cstore_l6_orderkey_rle, ib_trx, &crsr_l6_o);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l6_orderkey_rle << std::endl; }
  err = ib_cursor_open_table(cstore_l6_extendedprice_uncompressed, ib_trx, &crsr_l6_e);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l6_extendedprice_uncompressed << std::endl; }
  err = ib_cursor_open_table(cstore_l6_shipdate_uncompressed, ib_trx, &crsr_l6_s);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_l6_shipdate_uncompressed << std::endl; }
  err = ib_cursor_open_table(cstore_ji_l3_l1, ib_trx, &crsr_ji);
  if (err != DB_SUCCESS) { std::cout << ib_strerror(err) << " " << cstore_ji_l3_l1 << std::endl; }

  tpl_l1_s = ib_clust_read_tuple_create(crsr_l1_s);
  tpl_l1_e = ib_clust_read_tuple_create(crsr_l1_e);
  tpl_l3_o = ib_clust_read_tuple_create(crsr_l3_o);
  tpl_l3_c = ib_clust_read_tuple_create(crsr_l3_c);
  tpl_l3_ok = ib_clust_read_tuple_create(crsr_l3_ok);
  tpl_l5_c = ib_clust_read_tuple_create(crsr_l5_c);
  tpl_l5_n = ib_clust_read_tuple_create(crsr_l5_n);
  tpl_l6_o = ib_clust_read_tuple_create(crsr_l6_o);
  tpl_l6_e = ib_clust_read_tuple_create(crsr_l6_e);
  tpl_l6_s = ib_clust_read_tuple_create(crsr_l6_s);
  tpl_ji = ib_clust_read_tuple_create(crsr_ji);

  ib_tpl_t key = ib_clust_search_tuple_create(crsr_l3_o);
  ib_tuple_write_u32(key, 0, pred_o_orderdate);
  int res;
  // move cursor
  //err = ib_cursor_moveto(l3_o, key, IB_CUR_L, &res);
  err = ib_cursor_first(crsr_l3_o);
  ib_tuple_delete(key);
  if (err == DB_SUCCESS) {
    //std::cout << "success" << std::endl;
  } else {
    std::cout << "failed" << std::endl;
    ib_cursor_close(crsr_l3_o); 
    return;
  }

  while (err == DB_SUCCESS) {
    err = ib_cursor_read_row(crsr_l3_o, tpl_l3_o);

    /* Possible handle locking and timeout errors too in multi-threaded applications. */
    if (err == DB_RECORD_NOT_FOUND || err == DB_END_OF_INDEX) {
      break;
    }
    ib_u32_t orderdate, pos, freq;
    ib_tuple_read_u32(tpl_l3_o, 0, &orderdate);
    if (orderdate >= pred_o_orderdate) { break; }
    ib_tuple_read_u32(tpl_l3_o, 1, &pos);
    ib_tuple_read_u32(tpl_l3_o, 2, &freq);

    double t1 = gettimeofday_sec();
    // get L3 o_custkey from position filtering
    std::vector<im_val_t> pos_custkey_array = get_l3_custkeys_from_sequences(pos, freq);
    std::sort(pos_custkey_array.begin(), pos_custkey_array.end(), val_asc);
    /*
    for (int i = 0; i < pos_custkey_array.size(); ++i) {
      std::cout << pos_custkey_array[i].pos << ": " << pos_custkey_array[i].val << std::endl;
    }
    */
    double t2 = gettimeofday_sec();
    std::cout << "orderdate: " << pred_o_orderdate << " get_l3_custkeys_from_sequences: " << t2 - t1 << std::endl;

    // join with L5 and apply c_nationkey predicate
    //std::vector<uint32_t> pos_array = apply_l5_predicate(pos_custkey_array);
    std::vector<uint32_t> pos_array = apply_l5_predicate_scan(pos_custkey_array);
    /*
    for (int i = 0; i < pos_array.size(); ++i) {
      std::cout << "pos: " << pos_array[i] << std::endl;
    }
    */
    double t3 = gettimeofday_sec();
    std::cout << "orderdate: " << pred_o_orderdate << " apply_l5_predicate: " << t3 - t2 << std::endl;

    // get L3 o_orderkey from position filtring
    std::vector<im_val_t> pos_orderkey_array = get_l3_orderkeys_from_positions(pos_array);
    /*
    for (int i = 0; i < pos_orderkey_array.size(); ++i) {
      std::cout << pos_orderkey_array[i].pos << ": " << pos_orderkey_array[i].val << std::endl;
    }
    */
    double t4 = gettimeofday_sec();
    std::cout << "orderdate: " << pred_o_orderdate << " get_l3_orderkeys_from_positions: " << t4 - t3 << std::endl;
    
    // join with L1 apply L1 l_shipdate predicate
    // TODO: we don't need orderkey if we have L3->L1 join-index (we only need positions)
    if (use_ji) {
      std::vector<im_agg_t> extendedprice_array = apply_l1_predicate_via_ji(pos_orderkey_array);
      double t5 = gettimeofday_sec();
      std::cout << "orderdate: " << pred_o_orderdate << " apply_l1_predicate(JI): " << t5 - t4 << std::endl;
    } else {
      std::sort(pos_orderkey_array.begin(), pos_orderkey_array.end(), val_asc);
      std::vector<im_agg_t> extendedprice_array = apply_l6_predicate(pos_orderkey_array);
      double t5 = gettimeofday_sec();
      std::cout << "orderdate: " << pred_o_orderdate << " apply_l6_predicate: " << t5 - t4 << std::endl;
    }

    /*
    for (int i = 0; i < extendedprice_array.size(); ++i) {
      std::cout << "orderdate: " << orderdate << ", orderkey: " << pos_orderkey_array[i].val << ", SUM(extendedprice): " << extendedprice_array[i].agg << std::endl;
    }
    */

    // here we have L3.o_orderdate, L1.l_extendedprice
    // aggratation if needed (omitted)

    err = ib_cursor_next(crsr_l3_o);

    /* Possible handle locking and timeout errors too
    *  in multi-threaded applications. */
    if (err == DB_RECORD_NOT_FOUND || err == DB_END_OF_INDEX) {
      break;
    }
    tpl_l3_o = ib_tuple_clear(tpl_l3_o);
  }
  ib_tuple_delete(tpl_l3_o);
  ib_cursor_close(crsr_l1_s);
  ib_cursor_close(crsr_l1_e);
  ib_cursor_close(crsr_l3_o); 
  ib_cursor_close(crsr_l3_c); 
  ib_cursor_close(crsr_l3_ok); 
  ib_cursor_close(crsr_l5_c); 
  ib_cursor_close(crsr_l5_n); 
  ib_cursor_close(crsr_l6_o); 
  ib_cursor_close(crsr_l6_e); 
  ib_cursor_close(crsr_l6_s); 
  ib_cursor_close(crsr_ji); 
  ib_trx_commit(ib_trx);
  double t6 = gettimeofday_sec();
  std::cout << "orderdate: " << pred_o_orderdate << " total:" << t6-t0 << std::endl;
}
Example #24
0
void *index_memory(void *p)
{
  pos_lists_t *Z0 = new pos_lists_t;

  uint32_t used_buffer_size = 0;

  thread_arg_t *arg = (thread_arg_t *) p;
  int id = arg->id;
  double t1 = gettimeofday_sec();
  std::cout << "index_memory starts for Z0-" << id << std::endl;

  MeCab::Tagger *tagger = MeCab::createTagger(0, NULL);

  uint32_t doc_id = 1;
  std::string line;
  while (1) {
    pthread_mutex_lock(&fin_mutex);
    if (!getline(fin, line)) {
      pthread_mutex_unlock(&fin_mutex);
      break;
    }
    pthread_mutex_unlock(&fin_mutex);

    std::vector<std::string> items;
    boost::split(items, line, boost::is_any_of(""));

    /*
    if (items.size() != 6) { continue; }
    line = items[4] + " " + items[5];
    */
    if (items.size() != 3) { continue; }
    line = items[1] + " " + items[2];

    unsigned int offset = 0;
    pos_lists_tmp_t Ztmp;
    const MeCab::Node *node_ = tagger->parseToNode(line.c_str());
    for (; node_; node_ = node_->next) {
      if (node_->stat == MECAB_BOS_NODE ||
        node_->stat == MECAB_EOS_NODE) {
        continue; 
      }   
      std::string token(node_->surface, node_->length);
      unsigned int length = get_length_of((const unsigned char *) token.c_str());
      // mecab ignores spaces in default 
      // but they must be counted as offset from the beginning
      int head_space_len =  node_->rlength - node_->length;
      offset += head_space_len > 0 ? head_space_len : 0;

      pos_lists_tmp_t::iterator itr = Ztmp.find(token);
      if (itr != Ztmp.end()) {
        itr->second.push_back(offset);
        ++(itr->second.at(1));
      } else {
        pos_list_t p;
        p.push_back(doc_id);
        p.push_back(1);
        p.push_back(offset);
        std::pair<std::string, pos_list_t> pos;
        pos.first = token;
        pos.second = p;
        Ztmp.insert(pos);
      }
      //tokens_.push_back(Term(token, length, offset));
      offset += length;
    }

    pos_lists_tmp_t::iterator itr_end = Ztmp.end();
    for (pos_lists_tmp_t::iterator itr = Ztmp.begin();
         itr != itr_end; ++itr) {

      std::pair<std::string, pos_list_t> pos = *itr;
      LMergeAddToken(indexes, *Z0, pos, used_buffer_size);
    }
    //std::cout << "doc: " << doc_id << std::endl;
    ++doc_id;
    Ztmp.clear();
    pos_lists_tmp_t().swap(Ztmp);

    if (used_buffer_size > FLUSH_THRESHOLD) {
      pthread_mutex_lock(&flush_buffer_queue_mutex);
      ++flush_buffer_queue_size;
      push_or_merge(Z0);
      //flush_buffer_queue.push_back(Z0);
      if (flush_buffer_queue_size == NUM_FLUSH_BUFFER) {
        // flushing (Z0 is cleard inside Z0_flush)
        Z0_flush(indexes);
        flush_buffer_queue_size = 0;
      }
      Z0 = new pos_lists_t;
      pthread_mutex_unlock(&flush_buffer_queue_mutex);
      used_buffer_size = 0;
      // [TODO] clear doc_id
    }
  }
  if (used_buffer_size > 0) {
    pthread_mutex_lock(&flush_buffer_queue_mutex);
    //flush_buffer_queue.push_back(Z0);
    ++flush_buffer_queue_size;
    push_or_merge(Z0);
    if (flush_buffer_queue_size == NUM_FLUSH_BUFFER) {
      Z0_flush(indexes);
    }
    pthread_mutex_unlock(&flush_buffer_queue_mutex);
  }
  delete tagger;
  double t2 = gettimeofday_sec();
  std::cout << "index_memory ends for Z0-" << id << " - time: " << t2 -t1 << std::endl;

  return NULL;
}
Example #25
0
int main(int argc, char **argv){

  if (argc != 3) {
    std::cerr << "Usage: " << argv[0] << " file dbname" << std::endl; 
    exit(1);
  }

  double t1, t2;
  FILE *fp = fopen(argv[1], "r");
  if (fp == NULL) {
    std::cerr << "failed to open " << argv[1] << std::endl;
    exit(1);
  }

  int ecode;
  char *key, *value;

  t1 = gettimeofday_sec();
  TCBDB *bdb = tcbdbnew();
  tcbdbtune(bdb, 5, -1, -1, -1, -1, 0);
  tcbdbsetcache(bdb, 300, 100);

  if (!tcbdbopen(bdb, argv[2], BDBOWRITER | BDBOCREAT)) {
    ecode = tcbdbecode(bdb);
    fprintf(stderr, "open error: %s\n", tcbdberrmsg(ecode));
  }

  char buf[256];
  while (fgets(buf, 256, fp)) {
    char *key = strchr(buf, ' ');
    if (key == NULL) { continue; }
    *key++ = '\0';
    char *valp = strchr(key, ' ');
    if (valp == NULL) { continue; }
    *valp++ = '\0';
    char *val = new char[102401]; // 100K
    sprintf(val, "%0102400d", atoi(valp));

    int size;
    void *data = tcbdbget(bdb, key, strlen(key), &size);
    if (data != NULL) {
      if (strcmp(val, (char *) data) != 0) {
        std::cout << "[error] value incorrect." << std::endl;
      }
      free(data);
    } else {
      std::cout << "[error] entry not found." << std::endl;
    } 
    delete [] val;
  }

  if (!tcbdbclose(bdb)) {
    ecode = tcbdbecode(bdb);
    fprintf(stderr, "close error: %s\n", tcbdberrmsg(ecode));
  }
  tcbdbdel(bdb);
  t2 = gettimeofday_sec();
  std::cout << "get time: " << t2 - t1 << std::endl;

  fclose(fp);

  return 0;
}
Example #26
0
void Z0_flush(std::vector<bool> &indexes)
{
  std::cout << "Z0_flush" << std::endl;

  double t1 = gettimeofday_sec();

  int i = 0;
  if (indexes.empty()) {
    indexes.push_back(false);
  }
  while (1) {
    // pipelining test
    uint32_t pipeline_level = 0;
    for (int j = 0; j < indexes.size(); ++j) {
      if (indexes[j]) {
        ++pipeline_level;
      } else {
        break;
      }
    }

    pipeline_merge_linear(pipeline_level);
    for (int k = 0; k < pipeline_level; ++k) {
      indexes[k] = false;
      char fname1[256];
      char fname2[256];
      sprintf(fname1, "I%d.bidx", k);
      sprintf(fname2, "I%d.data", k);
      if (unlink(fname1) < 0) { perror(fname1); }
      if (unlink(fname2) < 0) { perror(fname2); }
    }
    if (indexes.size() != pipeline_level + 1) {
      indexes.push_back(false);
    }
    indexes[pipeline_level] = true;
    break;
    
    /*
    if (indexes[i]) {
      assert(0);
      merge(Z0, i);
      indexes[i] = false;
      // [NOTICE] they shouldn't be removed here.
      char fname1[256];
      char fname2[256];
      sprintf(fname1, "I%d.bidx", i);
      sprintf(fname2, "I%d.data", i);
      if (unlink(fname1) < 0) { perror("unlink failed"); }
      if (unlink(fname2) < 0) { perror("unlink failed"); }
    } else {
      dump(Z0, i);
      indexes[i] = true;
      break;
    }
    assert(0);
    ++i;
    indexes.push_back(false);
    */
  }
  std::vector<pos_lists_t *>::iterator itr_end = flush_buffer_queue.end();
  for (std::vector<pos_lists_t *>::iterator itr = flush_buffer_queue.begin(); itr != itr_end; ++itr) {
    (*itr)->clear();
    delete *itr;
  }
  flush_buffer_queue.clear();
  double t2 = gettimeofday_sec();
  std::cout << "flush(merge+I/O) time: " << t2 - t1 << std::endl;
}
Example #27
0
int main(int argc, char *argv[])
{
  FILE *fp, *fp2;
  char buf[100000][100];	/*命令 読み込み用バッファ */
  char fpt[FPT_MAX_COUNT][20] = { 0 };
  char ofname[1000] = "result";
  int i = 0, j, k;
  //メモリ上の浮動小数点テーブルの位置
  int fpmemoffset = 0;
  int result;
  bool doprintregs = false, doprintmem = false;
  program *answer;
  program2 *answer2;
  double t2, t1;
  int ll = 0;

  while ((result = getopt(argc, argv, "rmo:")) != -1) {
    switch (result) {

      /* 値をとらないオプション */
    case 'r':
      /* getoptの返り値は見付けたオプションである. */
      doprintregs = true;
      break;
    case 'm':
      doprintmem = true;
      break;

    case 'o':
      /* 値を取る引数の場合は外部変数optargにその値を格納する. */
      strcpy(ofname, optarg);
      break;

      /* 以下二つのcaseは意味がないようだ.
	 getoptが直接エラーを出力してくれるから.
	 プログラムを終了するなら意味があるかも知れない */
    case ':':
      /* 値を取る引数に値がなかった場合:を返す. */
      fprintf(stdout, "%c needs value\n", result);
      break;

      /* getoptの引数で指定されなかったオプションを受け取ると?を返す. */
    case '?':
      fprintf(stdout, "unknown\n");
      break;
    }
  }
  argc -= optind;
  argv += optind;
    

  /*
    if (argc < 3) {
    fprintf(stderr, "<Useage>:%s assembler-file fp-table-file\n",
    argv[0]);
    return 1;
    }
  */


  /*assembler-file open and read */
  fp = fopen(argv[0], "r");
  if (fp == NULL) {
    printf("%sが開けませんaa\n", argv[0]);
    return 1;
  }

  if (argc > 1) {
    fp2 = fopen(argv[1], "r");
    if (fp2 == NULL) {
      printf("%sが開けません\n", argv[1]);
      return 1;
    }
    for (i = 0; i < FPT_MAX_COUNT; ++i) {
      if (fgets(fpt[i], 20, fp2) == NULL) {	/* 1行読み込み */
	break;		/* 末尾まで完了したか、エラー発生で終了 */
      }
    }

    for (j = 0; j < i; ++j) {
      //エラーが起こっても関係ない
      //printf("%s\n", fpt[j]);
      memory[j + fpmemoffset].i = (int) strtoll(fpt[j], NULL, 16);
    }
    //print_memory();
  }

  i = 0;
  while (1) {
    if (fgets(buf[i], 81, fp) == NULL) {	/* 1行読み込み */
      break;		/* 末尾まで完了したか、エラー発生で終了 */
    }
    i++;
  }

  out_fp = fopen(ofname, "w");

  answer = parse_all(buf, i);

  answer2 = parse_all2(answer);
  //print_program(answer);

  t1 = gettimeofday_sec();
  //do_assemble(answer);
  do_assemble2(answer, answer2);
  t2 = gettimeofday_sec();
  printf("%lf\n", t2 - t1);

  if (doprintregs)
    print_register();
  if (doprintmem)
    print_memory();

  fclose(fp);
  fclose(fp2);
  fclose(out_fp);

  printf("%f\n", ppp);
  printf("%f\n", nnn);
  /*
    fprintf(fp, "P3\n%d %d %d\n", memory[8193].i, memory[8194].i,
    memory[8195].i);
    for (i = 8196; i < memory[8192].i; i = i + 3) {
    fprintf(fp, "%d %d %d\n", (int) memory[i].d,
    (int) memory[i + 1].d, (int) memory[i + 2].d);
    }
  */

    
  return 0;
}