Ejemplo n.º 1
0
static VALUE rb_sp_add (VALUE self, VALUE eid, VALUE fd)
{
	int _eid = FIX2INT (eid);
	int _fd = FIX2INT (fd);

	return INT2FIX (sp_add (_eid, _fd) );
}
Ejemplo n.º 2
0
static int
start_socket(struct socket_server *ss, struct request_start *request, struct socket_message *result) {
	int id = request->id;
	result->id = id;
	result->opaque = request->opaque;
	result->ud = 0;
	result->data = NULL;
	struct socket *s = &ss->slot[HASH_ID(id)];
	if (s->type == SOCKET_TYPE_INVALID || s->id !=id) {
		return SOCKET_ERROR;
	}
	if (s->type == SOCKET_TYPE_PACCEPT || s->type == SOCKET_TYPE_PLISTEN) {
		if (sp_add(ss->event_fd, s->fd, s)) {
			s->type = SOCKET_TYPE_INVALID;
			return SOCKET_ERROR;
		}
		s->type = (s->type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_CONNECTED : SOCKET_TYPE_LISTEN;
		s->opaque = request->opaque;
		result->data = "start";
		return SOCKET_OPEN;
	} else if (s->type == SOCKET_TYPE_CONNECTED) {
		s->opaque = request->opaque;
		result->data = "transfer";
		return SOCKET_OPEN;
	}
	return -1;
}
Ejemplo n.º 3
0
// 创建新的socket实例
// id,通过reserve_id分配的alloc_id
// fd, 通过socket创建的套接字
// add, true则加入事件循环
static struct socket *
new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, bool add) {
	struct socket * s = &ss->slot[HASH_ID(id)];
	// 正常情况下,socket已在reserve_id中赋值为保留类型
	assert(s->type == SOCKET_TYPE_RESERVE);

	if (add) {
		// 加入事件循环
		if (sp_add(ss->event_fd, fd, s)) {
			// 如果加入失败,socket重置为初始类型	
			s->type = SOCKET_TYPE_INVALID;
			return NULL;
		}
	}

	// 初始化变量
	s->id = id;
	s->fd = fd;
	s->protocol = protocol;
	s->p.size = MIN_READ_BUFFER;
	s->opaque = opaque;
	s->wb_size = 0;
	check_wb_list(&s->high);
	check_wb_list(&s->low);
	return s;
}
Ejemplo n.º 4
0
static struct socket *
newsocket(struct silly_socket *ss, struct socket *s, int fd, enum stype type, void (* report)(struct silly_socket *ss, struct socket *s, int err))
{
        int err;
        if (s == NULL)
                s = allocsocket(ss, type, PROTOCOL_TCP);
        if (s == NULL) {
                close(fd);
                return NULL;
        }
        assert(s->type == type || s->type == STYPE_ALLOCED);
        assert(s->presize == MIN_READBUFF_LEN);
        assert(fd >= 0);
        s->fd = fd;
        s->type = type;
        err = sp_add(ss->spfd, fd, s);
        if (err < 0) {
                if (report)
                        report(ss, s, errno);
                perror("newsocket");
                close(fd);
                freesocket(ss, s);
                return NULL;
        }
        return s;
}
Ejemplo n.º 5
0
void
mpzspv_to_ntt (mpzspv_t x, spv_size_t offset, spv_size_t len,
    spv_size_t ntt_size, int monic, mpzspm_t mpzspm)
{
  unsigned int i;
  spv_size_t j, log2_ntt_size;
  spm_t spm;
  spv_t spv;
  
  ASSERT (mpzspv_verify (x, offset, len, mpzspm));
  ASSERT (mpzspv_verify (x, offset + ntt_size, 0, mpzspm));
  
  log2_ntt_size = ceil_log_2 (ntt_size);

  for (i = 0; i < mpzspm->sp_num; i++)
    {
      spm = mpzspm->spm[i];
      spv = x[i] + offset;
      
      if (ntt_size < len)
        {
	  for (j = ntt_size; j < len; j += ntt_size)
	    spv_add (spv, spv, spv + j, ntt_size, spm->sp);
	}
      if (ntt_size > len)
	spv_set_zero (spv + len, ntt_size - len);

      if (monic)
	spv[len % ntt_size] = sp_add (spv[len % ntt_size], 1, spm->sp);
      
      spv_ntt_gfp_dif (spv, log2_ntt_size, spm);
    }
}
Ejemplo n.º 6
0
// 启动socket,加入事件循环
// 成功时返回SOCKET_OPEN
static int
start_socket(struct socket_server *ss, struct request_start *request, struct socket_message *result) {
	int id = request->id;
	result->id = id;
	result->opaque = request->opaque;
	result->ud = 0;
	result->data = NULL;
	struct socket *s = &ss->slot[HASH_ID(id)];
	if (s->type == SOCKET_TYPE_INVALID || s->id !=id) {
		result->data = "invalid socket";
		return SOCKET_ERROR;
	}
	if (s->type == SOCKET_TYPE_PACCEPT || s->type == SOCKET_TYPE_PLISTEN) {
		// 套接字加入事件循环
		if (sp_add(ss->event_fd, s->fd, s)) {
			force_close(ss, s, result);
			result->data = strerror(errno);
			return SOCKET_ERROR;
		}
		// SOCKET_TYPE_PACCEPT -> SOCKET_TYPE_CONNECTED
		// SOCKET_TYPE_PLISTEN -> SOCKET_TYPE_LISTEN
		s->type = (s->type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_CONNECTED : SOCKET_TYPE_LISTEN;
		s->opaque = request->opaque;
		result->data = "start";
		return SOCKET_OPEN;
	} else if (s->type == SOCKET_TYPE_CONNECTED) {
		// todo: maybe we should send a message SOCKET_TRANSFER to s->opaque
		s->opaque = request->opaque;
		result->data = "transfer";
		return SOCKET_OPEN;
	}
	// if s->type == SOCKET_TYPE_HALFCLOSE , SOCKET_CLOSE message will send later
	return -1;
}
Ejemplo n.º 7
0
// 创建socket_server                                                                                                                                                                       
struct socket_server * 
socket_server_create() {
	int i;
	int fd[2];
	// 创建事件循环句柄 epoll/kqueue
	poll_fd efd = sp_create();
	if (sp_invalid(efd)) {
		fprintf(stderr, "socket-server: create event pool failed.\n");
		return NULL;
	}
	// 创建管道,这里用于进程内读写指令
	if (pipe(fd)) {
		sp_release(efd);
		fprintf(stderr, "socket-server: create socket pair failed.\n");
		return NULL;
	}
	// 把管道的读端加入事件循环
	if (sp_add(efd, fd[0], NULL)) {
		// add recvctrl_fd to event poll
		fprintf(stderr, "socket-server: can't add server fd to event pool.\n");
		close(fd[0]);
		close(fd[1]);
		sp_release(efd);
		return NULL;
	}

	// 创建并初始化socket_server
	struct socket_server *ss = MALLOC(sizeof(*ss));
	ss->event_fd = efd;
	ss->recvctrl_fd = fd[0];
	ss->sendctrl_fd = fd[1];
	ss->checkctrl = 1;

	for (i=0;i<MAX_SOCKET;i++) {
		struct socket *s = &ss->slot[i];
		s->type = SOCKET_TYPE_INVALID;
		clear_wb_list(&s->high);
		clear_wb_list(&s->low);
	}
	ss->alloc_id = 0;
	ss->event_n = 0;
	ss->event_index = 0;
	memset(&ss->soi, 0, sizeof(ss->soi));
	// 清空可读文件描述符集,为select做准备
	FD_ZERO(&ss->rfds);
	// 判断管道读套接字描述符小于系统描述符集大小常量,一般为1024
	// has_cmd中会调用FD_SET把管道读套接字加入可读文件描述符集管理
	// 提前判断避免FD_SET执行错误
	assert(ss->recvctrl_fd < FD_SETSIZE);

	return ss;
}
Ejemplo n.º 8
0
struct socket_server *
socket_server_create()
{
	int i;
	int fd[2];
	poll_fd efd = sp_create();
	if (sp_invalid(efd))
	{
		fprintf(stderr, "socket-server: create event pool failed.\n");
		return NULL;
	}
	if (pipe(fd))
	{
		sp_release(efd);
		fprintf(stderr, "socket-server: create socket pair failed.\n");
		return NULL;
	}
	if (sp_add(efd, fd[0], NULL))
	{
		// add recvctrl_fd to event poll
		fprintf(stderr, "socket-server: can't add server fd to event pool.\n");
		close(fd[0]);
		close(fd[1]);
		sp_release(efd);
		return NULL;
	}

	struct socket_server *ss = MALLOC(sizeof(*ss));
	ss->event_fd = efd;
	ss->recvctrl_fd = fd[0];
	ss->sendctrl_fd = fd[1];
	ss->checkctrl = 1;

	for (i = 0; i < MAX_SOCKET; i++)
	{
		struct socket *s = &ss->slot[i];
		s->type = SOCKET_TYPE_INVALID;
		clear_wb_list(&s->high);
		clear_wb_list(&s->low);
	}
	ss->alloc_id = 0;
	ss->event_n = 0;
	ss->event_index = 0;
	FD_ZERO(&ss->rfds);
	assert(ss->recvctrl_fd < FD_SETSIZE);

	return ss;
}
Ejemplo n.º 9
0
struct socket_server * 
socket_server_create() {
	int i;
	int fd[2];
	poll_fd efd = sp_create(); // epoll_create
	if (sp_invalid(efd)) {
		fprintf(stderr, "socket-server: create event pool failed.\n");
		return NULL;
	}

	// 创建一个管道
	if (pipe(fd)) {
		sp_release(efd);
		fprintf(stderr, "socket-server: create socket pair failed.\n");
		return NULL;
	}

	// epoll关注管道读端的可读事件
	if (sp_add(efd, fd[0], NULL)) {
		// add recvctrl_fd to event poll
		fprintf(stderr, "socket-server: can't add server fd to event pool.\n");
		close(fd[0]);
		close(fd[1]);
		sp_release(efd);
		return NULL;
	}

	struct socket_server *ss = MALLOC(sizeof(*ss));
	ss->event_fd = efd;
	ss->recvctrl_fd = fd[0]; // 管道读端
	ss->sendctrl_fd = fd[1]; // 管道写端
	ss->checkctrl = 1;

	// 初始化64K个socket
	for (i=0;i<MAX_SOCKET;i++) {
		struct socket *s = &ss->slot[i];
		s->type = SOCKET_TYPE_INVALID;
		s->head = NULL;
		s->tail = NULL;
	}
	ss->alloc_id = 0;
	ss->event_n = 0;
	ss->event_index = 0;
	FD_ZERO(&ss->rfds); // 用于select的fd置为空 主要是用于命令通道
	assert(ss->recvctrl_fd < FD_SETSIZE);

	return ss;
}
Ejemplo n.º 10
0
int
silly_socket_init()
{
        int err;
        sp_t spfd = SP_INVALID;
        int fd[2] = {-1, -1};
        struct socket *s = NULL;
        struct silly_socket *ss = silly_malloc(sizeof(*ss));
        memset(ss, 0, sizeof(*ss));
        socketpool_init(ss);
        spfd = sp_create(EVENT_SIZE);
        if (spfd == SP_INVALID)
                goto end;
        s = allocsocket(ss, STYPE_CTRL, PROTOCOL_PIPE);
        assert(s);
        err = pipe(fd); //use the pipe and not the socketpair because the pipe will be automatic when the data size small than BUFF_SIZE
        if (err < 0)
                goto end;
        err = sp_add(spfd, fd[0], s);
        if (err < 0)
                goto end;
        ss->spfd = spfd;
        ss->ctrlsendfd = fd[1];
        ss->ctrlrecvfd = fd[0];
        ss->eventindex = 0;
        ss->eventcount = 0;
        resize_eventbuff(ss, EVENT_SIZE);
        FD_ZERO(&ss->ctrlfdset);
        SSOCKET = ss;
        return 0;
end:
        if (s)
                freesocket(ss, s);
        if (spfd != SP_INVALID)
                sp_free(spfd);
        if (fd[0] >= 0)
                close(fd[0]);
        if (fd[1] >= 0)
                close(fd[0]);
        if (ss)
                silly_free(ss);

        return -errno;
}
Ejemplo n.º 11
0
static int
trylisten(struct silly_socket *ss, struct cmdpacket *cmd)
{
        int err;
        int sid = cmd->u.listen.sid;
        struct socket *s = &ss->socketpool[HASH(sid)];
        assert(s->sid == sid);
        assert(s->type == STYPE_ALLOCED);
        err = sp_add(ss->spfd, s->fd, s);
        if (err < 0) {
                perror("trylisten");
                report_close(ss, s, errno);
                close(s->fd);
                freesocket(ss, s);
                return err;
        }
        s->type = STYPE_LISTEN;
        return err;
}
Ejemplo n.º 12
0
static struct socket *
new_fd(struct socket_server *ss, int id, int fd, uintptr_t opaque, bool add) {
	struct socket * s = &ss->slot[id % MAX_SOCKET];
	assert(s->type == SOCKET_TYPE_RESERVE);

	if (add) {
		if (sp_add(ss->event_fd, fd, s)) {
			s->type = SOCKET_TYPE_INVALID;
			return NULL;
		}
	}

	s->id = id;
	s->fd = fd;
	s->size = MIN_READ_BUFFER;
	s->opaque = opaque;
	assert(s->head == NULL);
	assert(s->tail == NULL);
	return s;
}
Ejemplo n.º 13
0
struct socket_server * 
socket_server_create() {
	int i;
	int fd[2];
	poll_fd efd = sp_create();
	if (sp_invalid(efd)) {
		fprintf(stderr, "socket-server: create event pool failed.\n");
		return NULL;
	}
	if (socketpair(AF_UNIX,SOCK_STREAM,0,fd)) {
		sp_release(efd);
		fprintf(stderr, "socket-server: create socket pair failed.\n");
		return NULL;
	}
	if (sp_add(efd, fd[0], NULL)) {
		// add server_fd to event poll
		fprintf(stderr, "socket-server: can't add server fd to event pool.\n");
		close(fd[0]);
		close(fd[1]);
		sp_release(efd);
		return NULL;
	}

	struct socket_server *ss = MALLOC(sizeof(*ss));
	ss->event_fd = efd;
	ss->server_fd = fd[0];
	ss->client_fd = fd[1];

	for (i=0;i<MAX_SOCKET;i++) {
		struct socket *s = &ss->slot[i];
		s->type = SOCKET_TYPE_INVALID;
		s->head = NULL;
		s->tail = NULL;
	}
	ss->alloc_id = 0;
	ss->event_n = 0;
	ss->event_index = 0;
	ss->session_id = 0;

	return ss;
}
Ejemplo n.º 14
0
static struct socket *
new_fd(struct socket_server *ss, int id, int fd, uintptr_t opaque, bool add) {
	struct socket * s = &ss->slot[HASH_ID(id)];
	assert(s->type == SOCKET_TYPE_RESERVE);

	if (add) {
		if (sp_add(ss->event_fd, fd, s)) {
			s->type = SOCKET_TYPE_INVALID;
			return NULL;
		}
	}

	s->id = id;
	s->fd = fd;
	s->size = MIN_READ_BUFFER;
	s->opaque = opaque;
	s->wb_size = 0;
	check_wb_list(&s->high);
	check_wb_list(&s->low);
	return s;
}
Ejemplo n.º 15
0
static int
tryudpbind(struct silly_socket *ss, struct cmdpacket *cmd)
{
        int err;
        int sid = cmd->u.listen.sid;
        struct socket *s = &ss->socketpool[HASH(sid)];
        assert(s->sid == sid);
        assert(s->type = STYPE_ALLOCED);
        err = sp_add(ss->spfd, s->fd, s);
        if (err < 0) {
                perror("tryudpbind");
                report_close(ss, s, errno);
                close(s->fd);
                freesocket(ss, s);
                return err;
        }
        assert(s->protocol == PROTOCOL_UDP);
        s->type = STYPE_UDPBIND;
        assert(err == 0);
        return err;
}
Ejemplo n.º 16
0
static struct socket *
new_fd(struct socket_server *ss, int fd) {
	int i;
	for (i=0;i<MAX_SOCKET;i++) {
		int id = ss->alloc_id + i + 1;
		struct socket * s = &ss->slot[id % MAX_SOCKET];
		if (s->type == SOCKET_TYPE_INVALID) {
			if (sp_add(ss->event_fd, fd, s)) {
				return NULL;
			}
			s->id = id;
			s->fd = fd;
			s->size = MIN_READ_BUFFER;
			s->session = 0;
			assert(s->head == NULL);
			assert(s->tail == NULL);
			ss->alloc_id = id;
			return s;
		}
	}
	return NULL;
}
Ejemplo n.º 17
0
//--------------------------------------------------------------------------
void Hqp_IpRedSpBKP::init(const Hqp_Program *qp)
{
  IVEC *degree, *neigh_start, *neighs;
  SPMAT *QCTC;
  SPROW *r1, *r2;
  int i, j;
  int len, dim;
  Real sum;

  _n = qp->c->dim;
  _me = qp->b->dim;
  _m = qp->d->dim;
  dim = _n + _me;

  // reallocations

  _pivot = px_resize(_pivot, dim);
  _blocks = px_resize(_blocks, dim);
  _zw = v_resize(_zw, _m);
  _scale = v_resize(_scale, _n);
  _r12 = v_resize(_r12, dim);
  _xy = v_resize(_xy, dim);

  // store C' for further computations
  // analyze structure of C'*C

  _CT = sp_transp(qp->C, _CT);
  sp_ones(_CT);
  v_ones(_zw);
  QCTC = sp_get(_n, _n, 10);
  r1 = _CT->row;
  for (i=0; i<_n; i++, r1++) {
    r2 = r1;
    for (j=i; j<_n; j++, r2++) {
      sum = sprow_inprod(r1, _zw, r2);
      if (sum != 0.0) {
	sp_set_val(QCTC, i, j, sum);
	if (i != j)
	  sp_set_val(QCTC, j, i, sum);
      }
    }
  }
  _CTC_degree = iv_resize(_CTC_degree, _n);
  _CTC_neigh_start = iv_resize(_CTC_neigh_start, _n + 1);
  _CTC_neighs = sp_rcm_scan(QCTC, SMNULL, SMNULL,
			    _CTC_degree, _CTC_neigh_start, _CTC_neighs);

  // initialize structure of reduced qp

  QCTC = sp_add(qp->Q, QCTC, QCTC);

  // determine RCM ordering

  degree = iv_get(dim);
  neigh_start = iv_get(dim + 1);
  neighs = sp_rcm_scan(QCTC, qp->A, SMNULL, degree, neigh_start, IVNULL);

  _QP2J = sp_rcm_order(degree, neigh_start, neighs, _QP2J);
  _sbw = sp_rcm_sbw(neigh_start, neighs, _QP2J);
  _J2QP = px_inv(_QP2J, _J2QP);

  iv_free(degree);
  iv_free(neigh_start);
  iv_free(neighs);

  len = 1 + (int)(log((double)dim) / log(2.0));
  sp_free(_J);
  sp_free(_J_raw);
  _J_raw = sp_get(dim, dim, len);
  _J = SMNULL;

  // fill up data (to allocate _J_raw)
  sp_into_symsp(QCTC, -1.0, _J_raw, _QP2J, 0, 0);
  spT_into_symsp(qp->A, 1.0, _J_raw, _QP2J, 0, _n);
  sp_into_symsp(qp->A, 1.0, _J_raw, _QP2J, _n, 0);

  sp_free(QCTC);

  // prepare iterations

  update(qp);
}
Ejemplo n.º 18
0
void
mpzspv_mul_ntt (mpzspv_t r, const spv_size_t offsetr, 
    mpzspv_t x, const spv_size_t offsetx, const spv_size_t lenx,
    mpzspv_t y, const spv_size_t offsety, const spv_size_t leny,
    const spv_size_t ntt_size, const int monic, const spv_size_t monic_pos, 
    mpzspm_t mpzspm, const int steps)
{
  spv_size_t log2_ntt_size;
  int i;
  
  ASSERT (mpzspv_verify (x, offsetx, lenx, mpzspm));
  ASSERT (mpzspv_verify (y, offsety, leny, mpzspm));
  ASSERT (mpzspv_verify (x, offsetx + ntt_size, 0, mpzspm));
  ASSERT (mpzspv_verify (y, offsety + ntt_size, 0, mpzspm));
  ASSERT (mpzspv_verify (r, offsetr + ntt_size, 0, mpzspm));
  
  log2_ntt_size = ceil_log_2 (ntt_size);

  /* Need parallelization at higher level (e.g., handling a branch of the 
     product tree in one thread) to make this worthwhile for ECM */
#define MPZSPV_MUL_NTT_OPENMP 0

#if defined(_OPENMP) && MPZSPV_MUL_NTT_OPENMP
#pragma omp parallel if (ntt_size > 16384)
  {
#pragma omp for
#endif
  for (i = 0; i < (int) mpzspm->sp_num; i++)
    {
      spv_size_t j;
      spm_t spm = mpzspm->spm[i];
      spv_t spvr = r[i] + offsetr;
      spv_t spvx = x[i] + offsetx;
      spv_t spvy = y[i] + offsety;

      if ((steps & NTT_MUL_STEP_FFT1) != 0) {
        if (ntt_size < lenx)
          {
            for (j = ntt_size; j < lenx; j += ntt_size)
              spv_add (spvx, spvx, spvx + j, ntt_size, spm->sp);
          }
        if (ntt_size > lenx)
          spv_set_zero (spvx + lenx, ntt_size - lenx);

        if (monic)
          spvx[lenx % ntt_size] = sp_add (spvx[lenx % ntt_size], 1, spm->sp);

        spv_ntt_gfp_dif (spvx, log2_ntt_size, spm);
      }

      if ((steps & NTT_MUL_STEP_FFT2) != 0) {
        if (ntt_size < leny)
          {
            for (j = ntt_size; j < leny; j += ntt_size)
              spv_add (spvy, spvy, spvy + j, ntt_size, spm->sp);
          }
        if (ntt_size > leny)
          spv_set_zero (spvy + leny, ntt_size - leny);

        if (monic)
          spvy[leny % ntt_size] = sp_add (spvy[leny % ntt_size], 1, spm->sp);

        spv_ntt_gfp_dif (spvy, log2_ntt_size, spm);
      }

      if ((steps & NTT_MUL_STEP_MUL) != 0) {
        spv_pwmul (spvr, spvx, spvy, ntt_size, spm->sp, spm->mul_c);
      }

      if ((steps & NTT_MUL_STEP_IFFT) != 0) {
        ASSERT (sizeof (mp_limb_t) >= sizeof (sp_t));

        spv_ntt_gfp_dit (spvr, log2_ntt_size, spm);

        /* spm->sp - (spm->sp - 1) / ntt_size is the inverse of ntt_size */
        spv_mul_sp (spvr, spvr, spm->sp - (spm->sp - 1) / ntt_size,
            ntt_size, spm->sp, spm->mul_c);

        if (monic_pos)
          spvr[monic_pos % ntt_size] = sp_sub (spvr[monic_pos % ntt_size],
              1, spm->sp);
      }
    }
#if defined(_OPENMP) && MPZSPV_MUL_NTT_OPENMP
  }
#endif
}
Ejemplo n.º 19
0
void 
mpzspv_sqr_reciprocal (mpzspv_t dft, const spv_size_t n, 
                       const mpzspm_t mpzspm)
{
  const spv_size_t log2_n = ceil_log_2 (n);
  const spv_size_t len = ((spv_size_t) 2) << log2_n;
  const spv_size_t log2_len = 1 + log2_n;
  int j;

  ASSERT(mpzspm->max_ntt_size % 3UL == 0UL);
  ASSERT(len % 3UL != 0UL);
  ASSERT(mpzspm->max_ntt_size % len == 0UL);

#ifdef _OPENMP
#pragma omp parallel
  {
#pragma omp for
#endif
    for (j = 0; j < (int) (mpzspm->sp_num); j++)
      {
        const spm_t spm = mpzspm->spm[j];
        const spv_t spv = dft[j];
        sp_t w1, w2, invlen;
        const sp_t sp = spm->sp, mul_c = spm->mul_c;
        spv_size_t i;

        /* Zero out NTT elements [n .. len-n] */
        spv_set_sp (spv + n, (sp_t) 0, len - 2*n + 1);

#ifdef TRACE_ntt_sqr_reciprocal
        if (j == 0)
          {
            printf ("ntt_sqr_reciprocal: NTT vector mod %lu\n", sp);
            ntt_print_vec ("ntt_sqr_reciprocal: before weighting:", spv, len);
          }
#endif

        /* Compute the root for the weight signal, a 3rd primitive root 
           of unity */
        w1 = sp_pow (spm->prim_root, mpzspm->max_ntt_size / 3UL, sp, 
                     mul_c);
        /* Compute iw= 1/w */
        w2 = sp_pow (spm->inv_prim_root, mpzspm->max_ntt_size / 3UL, sp, 
                     mul_c);
#ifdef TRACE_ntt_sqr_reciprocal
        if (j == 0)
          printf ("w1 = %lu ,w2 = %lu\n", w1, w2);
#endif
        ASSERT(sp_mul(w1, w2, sp, mul_c) == (sp_t) 1);
        ASSERT(w1 != (sp_t) 1);
        ASSERT(sp_pow (w1, 3UL, sp, mul_c) == (sp_t) 1);
        ASSERT(w2 != (sp_t) 1);
        ASSERT(sp_pow (w2, 3UL, sp, mul_c) == (sp_t) 1);

        /* Fill NTT elements spv[len-n+1 .. len-1] with coefficients and
           apply weight signal to spv[i] and spv[l-i] for 0 <= i < n
           Use the fact that w^i + w^{-i} = -1 if i != 0 (mod 3). */
        for (i = 0; i + 2 < n; i += 3)
          {
            sp_t t, u;
            
	    if (i > 0)
	      spv[len - i] = spv[i];
            
            t = spv[i + 1];
            u = sp_mul (t, w1, sp, mul_c);
            spv[i + 1] = u;
            spv[len - i - 1] = sp_neg (sp_add (t, u, sp), sp);

            t = spv[i + 2];
            u = sp_mul (t, w2, sp, mul_c);
            spv[i + 2] = u;
            spv[len - i - 2] = sp_neg (sp_add (t, u, sp), sp);
          }
        if (i < n && i > 0)
          {
            spv[len - i] = spv[i];
          }
        if (i + 1 < n)
          {
            sp_t t, u;
            t = spv[i + 1];
            u = sp_mul (t, w1, sp, mul_c);
            spv[i + 1] = u;
            spv[len - i - 1] = sp_neg (sp_add (t, u, sp), sp);
          }

#ifdef TRACE_ntt_sqr_reciprocal
      if (j == 0)
        ntt_print_vec ("ntt_sqr_reciprocal: after weighting:", spv, len);
#endif

        /* Forward DFT of dft[j] */
        spv_ntt_gfp_dif (spv, log2_len, spm);

#ifdef TRACE_ntt_sqr_reciprocal
        if (j == 0)
          ntt_print_vec ("ntt_sqr_reciprocal: after forward transform:", 
			 spv, len);
#endif

        /* Square the transformed vector point-wise */
        spv_pwmul (spv, spv, spv, len, sp, mul_c);
      
#ifdef TRACE_ntt_sqr_reciprocal
        if (j == 0)
          ntt_print_vec ("ntt_sqr_reciprocal: after point-wise squaring:", 
			 spv, len);
#endif

        /* Inverse transform of dft[j] */
        spv_ntt_gfp_dit (spv, log2_len, spm);
      
#ifdef TRACE_ntt_sqr_reciprocal
        if (j == 0)
          ntt_print_vec ("ntt_sqr_reciprocal: after inverse transform:", 
			 spv, len);
#endif

        /* Un-weight and divide by transform length */
        invlen = sp - (sp - (sp_t) 1) / len; /* invlen = 1/len (mod sp) */
        w1 = sp_mul (invlen, w1, sp, mul_c);
        w2 = sp_mul (invlen, w2, sp, mul_c);
        for (i = 0; i < 2 * n - 3; i += 3)
          {
            spv[i] = sp_mul (spv[i], invlen, sp, mul_c);
            spv[i + 1] = sp_mul (spv[i + 1], w2, sp, mul_c);
            spv[i + 2] = sp_mul (spv[i + 2], w1, sp, mul_c);
          }
        if (i < 2 * n - 1)
          spv[i] = sp_mul (spv[i], invlen, sp, mul_c);
        if (i < 2 * n - 2)
          spv[i + 1] = sp_mul (spv[i + 1], w2, sp, mul_c);
        
#ifdef TRACE_ntt_sqr_reciprocal
        if (j == 0)
          ntt_print_vec ("ntt_sqr_reciprocal: after un-weighting:", spv, len);
#endif

        /* Separate the coefficients of R in the wrapped-around product. */

        /* Set w1 = cuberoot(1)^l where cuberoot(1) is the same primitive
           3rd root of unity we used for the weight signal */
        w1 = sp_pow (spm->prim_root, mpzspm->max_ntt_size / 3UL, sp, 
                     mul_c);
        w1 = sp_pow (w1, len % 3UL, sp, mul_c);
        
        /* Set w2 = 1/(w1 - 1/w1). Incidentally, w2 = 1/sqrt(-3) */
        w2 = sp_inv (w1, sp, mul_c);
        w2 = sp_sub (w1, w2, sp);
        w2 = sp_inv (w2, sp, mul_c);
#ifdef TRACE_ntt_sqr_reciprocal
        if (j == 0)
          printf ("For separating: w1 = %lu, w2 = %lu\n", w1, w2);
#endif
        
        for (i = len - (2*n - 2); i <= len / 2; i++)
          {
            sp_t t, u;
            /* spv[i] = s_i + w^{-l} s_{l-i}. 
               spv[l-i] = s_{l-i} + w^{-l} s_i */
            t = sp_mul (spv[i], w1, sp, mul_c); /* t = w^l s_i + s_{l-i} */
            t = sp_sub (t, spv[len - i], sp);   /* t = w^l s_i + w^{-l} s_i */
            t = sp_mul (t, w2, sp, mul_c);      /* t = s_1 */

            u = sp_sub (spv[i], t, sp);         /* u = w^{-l} s_{l-i} */
            u = sp_mul (u, w1, sp, mul_c);      /* u = s_{l-i} */
            spv[i] = t;
            spv[len - i] = u;
            ASSERT(i < len / 2 || t == u);
          }

#ifdef TRACE_ntt_sqr_reciprocal
        if (j == 0)
          ntt_print_vec ("ntt_sqr_reciprocal: after un-wrapping:", spv, len);
#endif
      }
#ifdef _OPENMP
    }
#endif
}
Ejemplo n.º 20
0
int
set0(Evas *e){
	Evas_Object *img, *proxy;
	bool rv;
	int w,h;
	struct imageupdate *iu;
	//int minw,minh,maxw,maxh;

	label_add(e,10,0,"The Source",false);
	img = evas_object_image_filled_add(e);
	evas_object_image_file_set(img, images[0], NULL);
	evas_object_image_size_get(img, &w, &h);
	w = w/2; h = h/2;
	evas_object_resize(img, w, h);
	evas_object_move(img, 10,10);
	evas_object_show(img);
	iu = calloc(1,sizeof(struct imageupdate));
	iu->cur = 0;
	iu->max = N_IMAGES;
	iu->obj = img;
	iu->imagelist = images;
	ecore_timer_add(1.4, image_next, iu);

	label_add(e,20+w,0,"Normal Proxy",false);
	proxy = evas_object_image_filled_add(e);
	if (!proxy){
		printf("Unable to create proxy object\n");
		return 1;
	}
	rv = evas_object_image_source_set(proxy, img);
	if (rv != true){
		printf("Error setting proxy source\n");
		return 1;
	}
	evas_object_resize(proxy, w, h);
	evas_object_move(proxy, 20 + w, 10);
	/* If this is uncommented: Moves proxy evyer second (swap x/y) */
	//iu->proxy = proxy;
	evas_object_show(proxy);
	label_add(e,10,h + 20, "Reflected Proxy",false);
	proxy = evas_object_image_filled_add(e);
	evas_object_image_source_set(proxy, img);
	evas_object_resize(proxy, w, h);
	evas_object_move(proxy, 10, 30+h);
	evas_object_show(proxy);
	flip_map(proxy);


	label_add(e,20+w,h+20,"Squish Proxy",false);
	proxy = evas_object_image_add(e);
	evas_object_image_source_set(proxy, img);
	evas_object_resize(proxy, w, h / 2);
	evas_object_image_fill_set(proxy, 0,0, w, h/2);
	evas_object_move(proxy, 20+w, 30+h);
	evas_object_show(proxy);

	/* Proxy a label */
	img = label_add(e, 300, 10, "Label Source ",true);
	evas_object_geometry_get(img, NULL, NULL, &w, &h);
	proxy = evas_object_image_filled_add(e);
	evas_object_image_source_set(proxy, img);
	evas_object_resize(proxy, w, h);
	evas_object_move(proxy, 300, 10 + h + 3);
	evas_object_show(proxy);
	flip_map(proxy);

	label_add(e, 440, 10, "Squish Label",false);
	proxy = evas_object_image_add(e);
	evas_object_image_source_set(proxy, img);
	evas_object_resize(proxy, w, h / 2);
	evas_object_image_fill_set(proxy, 0,0,w,h/2);
	evas_object_move(proxy, 440, 10 + h + 3);
	evas_object_show(proxy);

	label_add(e, 440, 60, "Stretch Label",false);
	proxy = evas_object_image_add(e);
	evas_object_image_source_set(proxy, img);
	evas_object_resize(proxy, w, h);
	evas_object_image_fill_set(proxy, 0,0,w, h);
	evas_object_move(proxy, 440, 60 + h + 3);
	evas_object_show(proxy);

	label_add(e, 240, 60, "Highlight", false);
	proxy = evas_object_image_add(e);
	evas_object_image_source_set(proxy, img);
	evas_object_resize(proxy, 50,50);
	evas_object_image_fill_set(proxy, -w/2,-h/2,w*3,h*3);
	evas_object_move(proxy, 250, 60 + h + 3);
	evas_object_show(proxy);


	img = label_add(e, 400, 120, "Zoomy Text!", false);
	proxy = evas_object_image_filled_add(e);
	evas_object_image_source_set(proxy, img);
	evas_object_resize(proxy, w, h);
	evas_object_move(proxy, 350, 150);
	zoom_map(proxy);
	evas_object_show(proxy);

	/* Proxy a text block */
	img = textblock_add(e, 10, 200);
	evas_object_geometry_get(img, NULL, NULL, &w, &h);

	proxy = evas_object_image_filled_add(e);
	evas_object_image_source_set(proxy, img);
	evas_object_resize(proxy, w, h);
	evas_object_move(proxy, 10, 320);
	evas_object_show(proxy);
	flip_map(proxy);

	proxy = evas_object_image_filled_add(e);
	evas_object_image_source_set(proxy, img);
	evas_object_resize(proxy, w/2, h/2);
	evas_object_move(proxy, 10 + w, 320);
	evas_object_show(proxy);

	/* The 'smart' object */
	img = sp_add(e);
	evas_object_move(img, 300,200);
	evas_object_resize(img, 100, 20);
	ecore_timer_add(0.05, smart_animate, img);
	w = 100;
	h = 20;


	proxy = evas_object_image_filled_add(e);
	evas_object_image_source_set(proxy, img);
	evas_object_resize(proxy, w, h);
	evas_object_move(proxy, 300, 240);
	evas_object_show(proxy);
	flip_map(proxy);

	proxy = evas_object_image_filled_add(e);
	evas_object_image_source_set(proxy, img);
	evas_object_resize(proxy, w * 2, h / 3);
	evas_object_image_fill_set(proxy, 0, 0, w * 2, h /3 );
	evas_object_move(proxy, 420, 240);
	evas_object_show(proxy);


	img = evas_object_image_filled_add(e);
	proxy = evas_object_image_filled_add(e);
	evas_object_move(img, 500, 300);
	evas_object_move(proxy, 600, 300);
	evas_object_resize(img, 100, 100);
	evas_object_resize(proxy, 100, 100);
	evas_object_show(img);
	evas_object_show(proxy);
	evas_object_image_source_set(img, proxy);
	evas_object_image_source_set(proxy, img);

#if 0
	label_add(e, 300,90, "Edje File", false);
	img = edje_object_add(e);
	if (!_edje_load_or_show_error(img, "basic.edj", "proxytest")){
		  evas_object_del(img);
	}

	evas_object_resize(img,220,200);
	evas_object_move(img,300,100);
	evas_object_show(img);
	edje_object_size_max_get(img, &maxw, &maxh);
	edje_object_size_min_get(img, &minw, &minh);
	if ((minw <= 0) && (minh <= 0))
		edje_object_size_min_calc(img, &minw, &minh);
	evas_object_size_hint_max_set(img, maxw, maxh);
	evas_object_size_hint_min_set(img, minw, minh);
	evas_object_size_hint_weight_set(img,
			EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	evas_object_size_hint_align_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
#endif /* The edje file */

	return 0;
}
Ejemplo n.º 21
0
static int
set1(Evas *e){
	Evas_Object *img, *proxy, *lbl;
	int w,h;

	/* create an image,, but don't show */
	label_add(e,10,0,"Hidden Source",false);
	img = evas_object_image_add(e);
	evas_object_image_file_set(img, images[0], NULL);
	/* don't do anything that may cause it to load */
	w = 96; h = 98;
	evas_object_resize(img, w, h);
	evas_object_image_fill_set(img, 0, 0, w, h);
	evas_object_move(img, 10,10);

	label_add(e,w + 90,0,"Proxy",false);
	proxy = evas_object_image_filled_add(e);
	evas_object_image_source_set(proxy, img);
	evas_object_resize(proxy, w, h);
	evas_object_image_fill_set(proxy, 0, 0, w, h);
	evas_object_move(proxy, w + 90, 10);
	evas_object_show(proxy);

	/*
	 * Test a hidden label
	 */
	label_add(e, 10, 120, "Hidden Text", false);
	lbl = label_add(e, 10, 120, "Can't See ME!", false);
	evas_object_hide(lbl);

	label_add(e, 200, 120, "Proxy Text", false);
	w = 200; h = 200;
	proxy =  evas_object_image_filled_add(e);
	evas_object_image_source_set(proxy, lbl);
	evas_object_resize(proxy, w, h);
	evas_object_image_fill_set(proxy, 0, 0, w, h);
	evas_object_move(proxy, 200, 120);
	evas_object_show(proxy);

	/*
	 * Invisible text block
	 */
	img = textblock_add(e, 600, 200);
	evas_object_hide(img);
	evas_object_geometry_get(img, NULL, NULL, &w, &h);

	proxy = evas_object_image_filled_add(e);
	evas_object_image_source_set(proxy, img);
	evas_object_resize(proxy, w, h);
	evas_object_move(proxy, 10, 320);
	evas_object_show(proxy);
	flip_map(proxy);


	/*
	 * Test an offscreen 'widget'
	 */
	img = sp_add(e);
	evas_object_move(img, -300,200);
	evas_object_resize(img, 100, 20);
	ecore_timer_add(0.05, smart_animate, img);
	w = 100;
	h = 20;

	proxy = evas_object_image_filled_add(e);
	evas_object_image_source_set(proxy, img);
	evas_object_resize(proxy, w, h);
	evas_object_move(proxy, 300, 300);
	evas_object_show(proxy);
	flip_map(proxy);





	return 0;
}
Ejemplo n.º 22
0
static int interp_lmop(const sp_mat *T, const sp_mat_c *A, const double *u,
                        const sp_log_mat_c *X_skel)
{
  mwSize nf = X_skel->m, nc = X_skel->n;
  mwSize max_nz=0, max_Q;
  mwIndex j;
  
  double *sqv1, *sqv2;
  double *Q, *QQt;
  
  for(j=0;j<nc;++j) {
    mwSize nz=X_skel->jc[j+1]-X_skel->jc[j];
    if(nz>max_nz) max_nz=nz;
  }
  max_Q = (max_nz*(max_nz+1))/2;
  
  if(!(sqv1=mem_alloc((2*max_nz + max_Q + max_nz*max_nz)*sizeof(double))))
    return 0;
  sqv2 = sqv1+max_nz, Q = sqv2+max_nz, QQt = Q+max_Q;

  { mwIndex nz=T->jc[nf]; for(j=0;j<nz;++j) T->pr[j]=0; }
  for(j=0;j<nc;++j) {
    const mwIndex *Qi = &X_skel->ir[X_skel->jc[j]];
    mwSize nz = X_skel->jc[j+1]-X_skel->jc[j];
    mwIndex m,k;
    double *qk = Q;
    double uj = u[j];
    for(k=0;k<nz*nz;++k) QQt[k]=0;
    for(k=0;k<nz;++k,qk+=k) {
      double alpha;
      mwIndex s = Qi[k];
      /* sqv1 := R_(k+1) A e_s */
      sp_restrict_sorted(sqv1, k+1,Qi, A->jc[s+1]-A->jc[s],
        &A->ir[A->jc[s]], &A->pr[A->jc[s]]);
      /* sqv2 := Q^t A e_s */
      mv_utt(sqv2, k,Q, sqv1);
      /* qk := Q Q^t A e_s */ 
      mv_ut(qk, k,Q, sqv2);
      /* alpha := ||(I-Q Q^t A)e_s||_A^2 = (A e_s)^t (I-Q Q^t A)e_s */
      alpha = sqv1[k];
      for(m=0;m<k;++m) alpha -= sqv1[m] * qk[m];
      /* qk := Q e_(k+1) = alpha^{-1/2} (I-Q Q^t A)e_s */
      alpha = -1.0 / sqrt(alpha);
      for(m=0;m<k;++m) qk[m] *= alpha;
      qk[k] = -alpha;
      /* QQt := QQt + qk qk^t */
      for(m=0;m<=k;++m) {
        mwIndex i, mnz = m*nz; double qkm = qk[m];
        for(i=0;i<=k;++i) QQt[mnz+i] += qkm * qk[i];
      }
    }
    /* T := T + u_j QQt */
    qk=QQt;
    for(k=0;k<nz;++k,qk+=nz) {
      mwIndex i = Qi[k], ti = T->jc[i];
      sp_add(T->jc[i+1]-ti,&T->ir[ti],&T->pr[ti], uj, nz,Qi,qk);
    }
  }
  mem_free(sqv1);
  return 1;
}