Example #1
0
File: asfds.c Project: SKKachaev/OS
int main(int argc, char **argv) 
{
    if (argc < 2) error_and_exit("not enough arguments");
    char* argv_exec[argc];
    argv_exec[0] = argv[1];
    argv_exec[1] = argv[2];

    for (int i = 2; i < argc-1; ++i)
    {
        argv_exec[i] = wrap_malloc(FILDES_LEN);
        memset(argv_exec[i], 0, FILDES_LEN);
        int fildes = wrap_open(argv[i+1], O_RDONLY);
        sprintf(argv_exec[i], "%d", fildes);
    }

    argv_exec[argc-1] = NULL;
    pid_t pid = wrap_fork();
    if (pid != 0)
    {
        int stat;
        wrap_waitpid(pid, &stat, 0);
        if (!(WIFEXITED(stat) && WEXITSTATUS(stat) == 0))
            error_and_exit("exit status of executed program");
    } else
    {
        wrap_execvp(argv_exec[0], argv_exec);
        _exit(EXIT_SUCCESS);
    }

    for (int i = 2; i < argc-1; ++i) free(argv_exec[i]);
    return EXIT_SUCCESS;
}
Example #2
0
/*** do reweighted coordinate descent optimization.
 * The weight for L1 norm is updated by calling reweighting function
 * cd->path->func using beta of the previous iteration. ***/
bool
cdescent_do_reweighting (cdescent *cd)
{
	int			iter;
	double		dnrm1;
	bool		converged = true;

	if (!cd->rwt) error_and_exit ("cdescent_do_reweighting", "cd->rwt is empty.", __FILE__, __LINE__);
	if (!cd->rwt->func) error_and_exit ("cdescent_do_reweighting", "cd->rwt->func is empty.", __FILE__, __LINE__);

	iter = 0;
	do {
		double		nrm1_prev = cd->nrm1;
		mm_dense	*w = cd->rwt->func->function (cd, cd->rwt->func->data);
		cdescent_set_penalty_factor (cd, w, cd->rwt->func->tau);
		mm_real_free (w);

		if (!(converged = cdescent_do_update_one_cycle (cd))) break;

		if (++iter > cd->rwt->maxiter) {
			printf_warning ("cdescent_do_reweighting", "reaching max number of iterations.", __FILE__, __LINE__);
			break;
		}

		dnrm1 = fabs (cd->nrm1 - nrm1_prev);
	} while (dnrm1 > cd->rwt->tolerance);

	return converged;
}
Example #3
0
File: twatc.c Project: vrld/twat
void receive_tweet(char* buffer, struct sockaddr_in* server)
{
    struct sockaddr_in client;
    int transfered = 0, timeoutcount = 0;

    /* Get message from message server */
    if (!udp_send(sock, "Gimmeh!", server, sizeof(struct sockaddr_in)))
        error_and_exit("Cannot send message to server", __FILE__, __LINE__);

    do {
        transfered = udp_receive(sock, buffer, BUFSIZE, &client, sizeof(client));
        if (transfered == -1) 
        {
            if (errno == EAGAIN || errno == EWOULDBLOCK) 
            {
                ++timeoutcount;
                if (timeoutcount >= MAX_TIMEOUTS) 
                {
                    printf("Maximum timeouts exceeded. Server is considered dead.");
                    exit(0);
                }
                printf("Server timed out (%d).\n", timeoutcount);
            }

            error_and_exit("Cannot read from server", __FILE__, __LINE__);
        } 
        else if (transfered == 0) 
        {
            error_and_exit("Server has shut down", __FILE__, __LINE__);
        }
    } while (transfered <= 0); 
    if (server->sin_addr.s_addr != client.sin_addr.s_addr)
        error_and_exit("Received message from unexpected server", __FILE__, __LINE__);
}
Example #4
0
/*** x(:,j)' * y(:,k) ***/
double
mm_real_xj_trans_dot_yk (const mm_real *x, const int j, const mm_dense *y, const int k)
{
	if (j < 0 || x->n <= j) error_and_exit ("mm_real_xj_trans_dot_yk", "first index out of range.", __FILE__, __LINE__);
	if (k < 0 || y->n <= k) error_and_exit ("mm_real_xj_trans_dot_yk", "second index out of range.", __FILE__, __LINE__);
	if (!mm_real_is_dense (y)) error_and_exit ("mm_real_xj_trans_dot_yk", "y must be dense.", __FILE__, __LINE__);
	if (mm_real_is_symmetric (y)) error_and_exit ("mm_real_xj_trans_dot_yk", "y must be general.", __FILE__, __LINE__);
	if (x->m != y->m) error_and_exit ("mm_real_xj_trans_dot_yk", "matrix dimensions do not match.", __FILE__, __LINE__);

	return (mm_real_is_sparse (x)) ? mm_real_sj_trans_dot_yk (x, j, y, k) : mm_real_dj_trans_dot_yk (x, j, y, k);
}
Example #5
0
/*** y = alpha * x(:,j) + y, atomic ***/
void
mm_real_axjpy_atomic (const double alpha, const mm_real *x, const int j, mm_dense *y)
{
	if (j < 0 || x->n <= j) error_and_exit ("mm_real_axjpy_atomic", "index out of range.", __FILE__, __LINE__);
	if (!mm_real_is_dense (y)) error_and_exit ("mm_real_axjpy_atomic", "y must be dense.", __FILE__, __LINE__);
	if (mm_real_is_symmetric (y)) error_and_exit ("mm_real_axjpy_atomic", "y must be general.", __FILE__, __LINE__);
	if (y->n != 1) error_and_exit ("mm_real_axjpy_atomic", "y must be vector.", __FILE__, __LINE__);
	if (x->m != y->m) error_and_exit ("mm_real_axjpy_atomic", "vector and matrix dimensions do not match.", __FILE__, __LINE__);

	return (mm_real_is_sparse (x)) ? mm_real_asjpy_atomic (alpha, x, j, y) : mm_real_adjpy_atomic (alpha, x, j, y);
}
Example #6
0
/*** x = [x1, x2] ***/
mm_real *
mm_real_holzcat (const mm_real *x1, const mm_real *x2)
{
	if ((mm_real_is_sparse (x1) && mm_real_is_dense (x1)) || (mm_real_is_dense (x1) && mm_real_is_sparse (x1)))
		error_and_exit ("mm_real_holzcat", "format of matrix x1 and x2 are incompatible.", __FILE__, __LINE__);
	if (mm_real_is_symmetric (x1) || mm_real_is_symmetric (x2))
		error_and_exit ("mm_real_holzcat", "matrix must be general.", __FILE__, __LINE__);
	if (x1->m != x2->m) error_and_exit ("mm_real_holzcat", "matrix size is incompatible.", __FILE__, __LINE__);

	return (mm_real_is_sparse (x1)) ? mm_real_holzcat_sparse (x1, x2) : mm_real_holzcat_dense (x1, x2);
}
Example #7
0
/*** memcpy mm_real ***/
void
mm_real_memcpy (mm_real *dest, const mm_real *src)
{
	if (mm_real_is_sparse (src)) {
		if (!mm_real_is_sparse (dest)) error_and_exit ("mm_real_memcpy", "destination matrix format does not match source matrix format.", __FILE__, __LINE__);
		mm_real_memcpy_sparse (dest, src);
	} else {
		if (!mm_real_is_dense (dest)) error_and_exit ("mm_real_memcpy", "destination matrix format does not match source matrix format.", __FILE__, __LINE__);
		mm_real_memcpy_dense (dest, src);
	}
	return;
}
Example #8
0
File: twats.c Project: vrld/twat
int main(int argc, char** argv)
{
    struct sockaddr_in server;
    struct sockaddr_in client;
    char buffer[BUFSIZE];
    socklen_t len_client;
    int transfered = 0;
    char* msg;
    pthread_t thread_tweet_fetcher;

    if (argc < 2) {
        printf("USAGE: %s [port]\n", argv[0]);
        return 0;
    }

    atexit(at_exit);
    set_signal_handlers();

    T = twitter_new();
    printf("Reading tweets...\n");
    fetch_tweets(T, 15);

    pthread_create(&thread_tweet_fetcher, NULL, thread_fetch_tweets, NULL);

    printf("Opening server...\n");
    sock = udp_create_socket(&server, sizeof(server), htonl(INADDR_ANY), atoi(argv[1]), 0);
    if (sock < 0)
        error_and_exit("cannot create socket", __FILE__, __LINE__);

    if (bind(sock, (struct sockaddr*)&server, sizeof(server)) < 0)
        error_and_exit("Cannot bind to socket", __FILE__, __LINE__);

    len_client = sizeof(client);
    for (;;) {
        transfered = udp_receive(sock, buffer, BUFSIZE, &client, len_client);
        switch (transfered) {
            case -1: 
                error_and_exit("Failed to receive message", __FILE__, __LINE__);
            case 0:
                error_and_exit("Client has shut down...", __FILE__, __LINE__);
        }

        msg = get_tweet(T);
        transfered = udp_send(sock, msg, &client, sizeof(client));
        free(msg);

        if (!transfered)
            error_and_exit("Sending has failed!", __FILE__, __LINE__);
    }

    close(sock);
    return 0;
}
Example #9
0
int main(void) {
    FILE *fh = NULL;
    cmp_ctx_t cmp;
    uint32_t array_size = 0;
    uint32_t str_size = 0;
    char hello[6] = {0, 0, 0, 0, 0, 0};
    char message_pack[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    fh = fopen("cmp_data.dat", "w+b");

    if (fh == NULL)
        error_and_exit("Error opening data.dat");

    cmp_init(&cmp, fh, file_reader, file_writer);

    if (!cmp_write_array(&cmp, 2))
        error_and_exit(cmp_strerror(&cmp));

    if (!cmp_write_str(&cmp, "Hello", 5))
        error_and_exit(cmp_strerror(&cmp));

    if (!cmp_write_str(&cmp, "MessagePack", 11))
        error_and_exit(cmp_strerror(&cmp));

    rewind(fh);

    if (!cmp_read_array(&cmp, &array_size))
        error_and_exit(cmp_strerror(&cmp));

    /* You can read the str byte size and then read str bytes... */

    if (!cmp_read_str_size(&cmp, &str_size))
        error_and_exit(cmp_strerror(&cmp));

    if (str_size > (sizeof(hello) - 1))
        error_and_exit("Packed 'hello' length too long\n");

    if (!read_bytes(hello, str_size, fh))
        error_and_exit(cmp_strerror(&cmp));

    /*
     * ...or you can set the maximum number of bytes to read and do it all in
     * one call
     */

    str_size = sizeof(message_pack);
    if (!cmp_read_str(&cmp, message_pack, &str_size))
        error_and_exit(cmp_strerror(&cmp));

    printf("Array Length: %zu.\n", array_size);
    printf("[\"%s\", \"%s\"]\n", hello, message_pack);

    fclose(fh);

    return EXIT_SUCCESS;
}
Example #10
0
/*** x(:,j)' * y ***/
mm_dense *
mm_real_xj_trans_dot_y (const mm_real *x, const int j, const mm_dense *y)
{
	int			k;
	mm_dense	*z;
	if (j < 0 || x->n <= j) error_and_exit ("mm_real_xj_trans_dot_y", "first index out of range.", __FILE__, __LINE__);
	if (!mm_real_is_dense (y)) error_and_exit ("mm_real_xj_trans_dot_y", "y must be dense.", __FILE__, __LINE__);
	if (mm_real_is_symmetric (y)) error_and_exit ("mm_real_xj_trans_dot_y", "y must be general.", __FILE__, __LINE__);
	if (x->m != y->m) error_and_exit ("mm_real_xj_trans_dot_y", "matrix dimensions do not match.", __FILE__, __LINE__);
	z = mm_real_new (MM_REAL_DENSE, MM_REAL_GENERAL, 1, y->n, y->n);
#pragma omp parallel for
	for (k = 0; k < y->n; k++) z->data[k] = mm_real_xj_trans_dot_yk (x, j, y, k);
	return z;
}
Example #11
0
/*** fread MatrixMarket format file ***/
mm_real *
mm_real_fread (FILE *fp)
{
	MM_typecode	typecode;
	mm_real		*x;
	if (mm_read_banner (fp, &typecode) != 0) error_and_exit ("mm_real_fread", "failed to read mm_real.", __FILE__, __LINE__);
	if (!is_type_supported (typecode)) {
		char	msg[128];
		sprintf (msg, "matrix type does not supported :[%s].", mm_typecode_to_str (typecode));
		error_and_exit ("mm_real_fread", msg, __FILE__, __LINE__);
	}
	x = (mm_is_sparse (typecode)) ? mm_real_fread_sparse (fp, typecode) : mm_real_fread_dense (fp, typecode);
	if (!x) error_and_exit ("mm_real_fread", "failed to read mm_real.", __FILE__, __LINE__);
	if (mm_real_is_symmetric (x) && x->m != x->n) error_and_exit ("mm_real_fread", "symmetric matrix must be square.", __FILE__, __LINE__);
	return x;
}
Example #12
0
/*** do cyclic coordinate descent optimization for fixed lambda1
 * repeat coordinate descent algorithm until solution is converged ***/
bool
cdescent_do_update_one_cycle (cdescent *cd)
{
	int					ccd_iter = 0;
	bool				converged = false;

	update_one_cycle	update_func;

	update_func = (cd->rule == CDESCENT_SELECTION_RULE_STOCHASTIC) ?
			cdescent_do_update_once_cycle_stochastic : cdescent_do_update_once_cycle_cyclic;


	if (!cd) error_and_exit ("cdescent_do_cyclic_update", "cdescent *cd is empty.", __FILE__, __LINE__);

	while (!converged) {

		converged = update_func (cd);

		if (++ccd_iter >= cd->maxiter) {
			printf_warning ("cdescent_do_cyclic_update", "reaching max number of iterations.", __FILE__, __LINE__);
			break;
		}

	}
	cd->total_iter += ccd_iter;
	return converged;
}
Example #13
0
void choose_server(rcComm_t **cn, char *host, rodsEnv *env, int verb) {

    if (verb) {
	fprintf(stderr, "Chosen server is: %s\n", host);
    }

    if (host && strcmp(host, THIS_ADDRESS)) {
    	int       stat;
    	rErrMsg_t err_msg;
	rcComm_t  *new_cn = NULL;
	
	new_cn = rcConnect(host, env->rodsPort, env->rodsUserName,
	    	    	   env->rodsZone, 0, &err_msg);
			   
	if (!new_cn) {
    	    fprintf(stderr, "Error: rcReconnect failed with status %d.  Continuing with original server.\n", err_msg.status);
	    return;
	}
	    
	#if IRODS_VERSION_INTEGER && IRODS_VERSION_INTEGER >= 4001008
	    stat = clientLogin(new_cn, "", "");
	#else
	    stat = clientLogin(new_cn);
	#endif
	
	if (stat < 0) {
	    rcDisconnect(new_cn);
    	    error_and_exit(*cn, "Error: clientLogin failed with status %d:%s\n", stat, get_irods_error_name(stat, verb));
	} else {
	    rcDisconnect(*cn);
	    *cn = new_cn;
	}
    }
}
Example #14
0
/*** norm2 x(:,j) ***/
double
mm_real_xj_nrm2 (const mm_real *x, const int j)
{
	double	ssq;
	if (j < 0 || x->n <= j) error_and_exit ("mm_real_xj_nrm2", "index out of range.", __FILE__, __LINE__);
	ssq = mm_real_xj_ssq (x, j);
	return sqrt (ssq);
}
Example #15
0
int randrange(const int start, const int end)
{
	const int range = end - start;
	const int offset = start;

	if (range <= 0)
		error_and_exit("range is negative! (start = %d, end = %d)\n", start, end);

	return random() % range + offset;
}
Example #16
0
/*** x(:,j) += alpha ***/
void
mm_real_xj_add_const (mm_real *x, const int j, const double alpha)
{
	int		k;
	int		n;
	double	*data;
	if (mm_real_is_symmetric (x)) error_and_exit ("mm_real_xj_add_const", "matrix must be general.", __FILE__, __LINE__);
	if (j < 0 || x->n <= j) error_and_exit ("mm_real_xj_add_const", "index out of range.", __FILE__, __LINE__);

	if (mm_real_is_sparse (x)) {
		n = x->p[j + 1] - x->p[j];
		data = x->data + x->p[j];
	} else {
		n = x->m;
		data = x->data + j * x->m;
	}
	for (k = 0; k < n; k++) data[k] += alpha;

	return;
}
Example #17
0
/*** x(:,j) *= alpha ***/
void
mm_real_xj_scale (mm_real *x, const int j, const double alpha)
{
	int		n;
	double	*data;
	if (mm_real_is_symmetric (x)) error_and_exit ("mm_real_xj_scale", "matrix must be general.", __FILE__, __LINE__);
	if (j < 0 || x->n <= j) error_and_exit ("mm_real_xj_scale", "index out of range.", __FILE__, __LINE__);

	if (mm_real_is_sparse (x)) {
		int		p = x->p[j];
		n = x->p[j + 1] - p;
		data = x->data + p;
	} else {
		n = x->m;
		data = x->data + j * x->m;
	}
	dscal_ (&n, &alpha, data, &ione);

	return;
}
Example #18
0
/* 设置网卡模式成混帐模式,这样的话可以截获以太网帧数据 */
int set_card_promisc( char * interface_name, int sock )  
{  
	/* 用于套接口ioctl的接口请求结构体 	*/
	struct ifreq ifr; 
                
	/* 复制网卡名称进入请求结构体的名称元素 */
	strncpy(ifr.ifr_name, interface_name ,strlen( interface_name )+1);  

	/* 通过ioctl获得相应信息	        */
	if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) {       	
	    error_and_exit("ioctl", 2);  
	}  

	/* 设置网卡模式标志为混杂模式	       */
    	ifr.ifr_flags |= IFF_PROMISC;  		            

	/* 通过ioctl把参数传递给网卡	       */  
	if(ioctl(sock, SIOCSIFFLAGS, &ifr) == -1 )  	      
   	    error_and_exit("ioctl", 3);  
}  
Example #19
0
/*** create new mm_real object
 * MMRealFormat	format: MM_REAL_DENSE or MM_REAL_SPARSE
 * MMRealSymm		symm  : MM_REAL_GENERAL, MM_REAL_SYMMETRIC_UPPER or MM_REAL_SYMMETRIC_LOWER
 * int				m, n  : rows and columns of the matrix
 * int				nnz   : number of nonzero elements of the matrix ***/
mm_real *
mm_real_new (MMRealFormat format, MMRealSymm symm, const int m, const int n, const int nnz)
{
	mm_real	*x;
	bool	symmetric;

	if (!is_format_valid (format))
		error_and_exit ("mm_real_new", "invalid MMRealFormat format.", __FILE__, __LINE__);
	if (!is_symm_valid (symm))
		error_and_exit ("mm_real_new", "invalid MMRealSymm symm.", __FILE__, __LINE__);

	symmetric = symm & MM_SYMMETRIC;
	if (symmetric && m != n)
		error_and_exit ("mm_real_new", "symmetric matrix must be square.", __FILE__, __LINE__);

	x = mm_real_alloc ();
	if (x == NULL) error_and_exit ("mm_real_new", "failed to allocate object.", __FILE__, __LINE__);
	x->m = m;
	x->n = n;
	x->nnz = nnz;

	// typecode[1] = 'C' or 'A'
	if (format == MM_REAL_SPARSE) {
		mm_set_coordinate (&x->typecode);
		x->i = (int *) malloc (x->nnz * sizeof (int));
		x->p = (int *) malloc ((x->n + 1) * sizeof (int));
		if (x->i == NULL || x->p == NULL) error_and_exit ("mm_real_new", "cannot allocate memory.", __FILE__, __LINE__);
		// initialize x->p[0]
		x->p[0] = 0;
	} else mm_set_array (&x->typecode);

	x->symm = symm;
	// typecode[3] = 'G' -> 'S'
	if (symmetric) mm_set_symmetric (&x->typecode);

	if (!is_type_supported (x->typecode)) {
		char	msg[128];
		sprintf (msg, "matrix type does not supported :[%s].", mm_typecode_to_str (x->typecode));
		error_and_exit ("mm_real_new", msg, __FILE__, __LINE__);
	}

	// allocate arrays
	x->data = (double *) malloc (x->nnz * sizeof (double));
	if (x->data == NULL) error_and_exit ("mm_real_new", "cannot allocate memory.", __FILE__, __LINE__);

	return x;
}
Example #20
0
/*** alpha * x * y, where x is sparse/dense matrix and y is dense matrix ***/
void
mm_real_x_dot_y (const bool trans, const double alpha, const mm_real *x, const mm_dense *y, const double beta, mm_dense *z)
{
	int		k;
	if (!mm_real_is_dense (y)) error_and_exit ("mm_real_x_dot_y", "y must be dense.", __FILE__, __LINE__);
	if (mm_real_is_symmetric (y)) error_and_exit ("mm_real_x_dot_y", "y must be general.", __FILE__, __LINE__);
	if ((trans && x->m != y->m) || (!trans && x->n != y->m))
		error_and_exit ("mm_real_x_dot_y", "dimensions of x and y do not match.", __FILE__, __LINE__);
	if (!mm_real_is_dense (z)) error_and_exit ("mm_real_x_dot_y", "z must be dense.", __FILE__, __LINE__);
	if (mm_real_is_symmetric (z)) error_and_exit ("mm_real_x_dot_y", "z must be general.", __FILE__, __LINE__);
	if ((trans && x->n != z->m) || (!trans && x->m != z->m))
		error_and_exit ("mm_real_x_dot_y", "dimensions of x and z do not match.", __FILE__, __LINE__);
#pragma omp parallel for
	for (k = 0; k < y->n; k++) mm_real_x_dot_yk (trans, alpha, x, y, k, beta, z);
	return;
}
Example #21
0
File: twatc.c Project: vrld/twat
int main(int argc, char** argv)
{
    struct sockaddr_in server;
    int chan;
    PaStream *stream;
    PaStreamParameters output_parameters;
    PaError err;

    voice = NULL;
    srand(time(NULL));
    atexit(at_exit);
    set_signal_handlers();
    init();

    if (argc != 4)
        return usage(argv[0]);

    set_output_parameters(&output_parameters, atoi(argv[3]));

    /* connect to server */
    sock = udp_create_socket(&server, sizeof(server), inet_addr(argv[1]), atoi(argv[2]), WAIT_FOR_RECEIVE);
    if (sock < 0)
        error_and_exit("Cannot create socket", __FILE__, __LINE__);
    
    /* create speech thingies */
    si.w          = malloc(sizeof(cst_wave) * si.channel_count);
    si.pos        = malloc(sizeof(long) * si.channel_count);
    si.done       = malloc(sizeof(int) * si.channel_count);
    si.cur_delay  = malloc(sizeof(double) * si.channel_count);
    si.rate_delay = malloc(sizeof(double) * si.channel_count);
    for (chan = 0; chan < si.channel_count; ++chan) {
        si.w[chan] = NULL;
        next_tweet(&server, chan);
    }

    err = Pa_OpenStream(&stream, NULL, &output_parameters,
            44100., 0, paNoFlag, say_text_callback, &si);
    if (paNoError != err) {
        fprintf(stderr, "Cannot open stream: %s\n", Pa_GetErrorText(err));
        exit(-1);
    }
/*        Pa_OpenDefaultStream(&stream, 0, si.channel_count, paInt16,
            si.w->sample_rate, 0, say_text_callback, &si); */
    err = Pa_StartStream(stream);
    if (paNoError != err) {
        fprintf(stderr, "Cannot start stream: %s\n", Pa_GetErrorText(err));
        exit(-1);
    }
    
    while (1)
    {
        usleep(100);
        for (chan = 0; chan < si.channel_count; ++chan) 
        {
            if (si.done[chan])
                next_tweet(&server, chan);
        }
    }
    //Pa_StopStream(stream);

    return 0;
}
Example #22
0
/*** n x n identity matrix ***/
mm_real *
mm_real_eye (MMRealFormat format, const int n)
{
	if (n <= 0) error_and_exit ("mm_real_eye", "invalid size.", __FILE__, __LINE__);
	return (format == MM_REAL_SPARSE) ? mm_real_seye (n) : mm_real_deye (n);
}
Example #23
0
/*** do pathwise cyclic coordinate descent optimization.
 * The regression is starting at the smallest value lambda1_max for which
 * the entire vector beta = 0, and decreasing sequence of values for lambda1
 * while log10(lambda1) >= path->log10_lambda1_lower.
 * log10(lambda1_max) is identical with log10 ( max ( abs(X' * y) ) ), where this
 * value is stored in cd->lreg->log10camax.
 * The interval of decreasing sequence on the log10 scale is path->dlog10_lambda1. ***/
bool
cdescent_do_pathwise_optimization (cdescent *cd)
{
	int			iter = 0;
	double		logt;
	bool		stop_flag = false;

	bool		converged;

	FILE		*fp_path = NULL;
	FILE		*fp_bic = NULL;

	if (!cd) error_and_exit ("cdescent_do_pathwise_optimization", "cdescent *cd is empty.", __FILE__, __LINE__);
	if (!cd->path) error_and_exit ("cdescent_do_pathwise_optimization", "cd->path is empty.", __FILE__, __LINE__);

	// reset cdescent object if need
	if (cd->was_modified) cdescent_reset (cd);
	// reset pathwise object if need
	if (cd->path->was_modified) pathwise_reset (cd->path);

	/* warm start */
	stop_flag = set_logt (cd->path->log10_lambda_lower, cd->path->log10_lambda_upper, &logt);

	if (cd->path->output_fullpath) {
		if (!(fp_path = fopen (cd->path->fn_path, "w"))) {
			char	msg[80];
			sprintf (msg, "cannot open file %s.", cd->path->fn_path);
			printf_warning ("cdescent_do_pathwise_optimization", msg, __FILE__, __LINE__);
		}
		if (fp_path) fprintf_solutionpath (fp_path, iter, cd->beta);
	}
	if (cd->path->output_bic_info) {
		if (!(fp_bic = fopen (cd->path->fn_bic, "w"))) {
			char	msg[80];
			sprintf (msg, "cannot open file %s.", cd->path->fn_bic);
			printf_warning ("cdescent_do_pathwise_optimization", msg, __FILE__, __LINE__);
		}
		// output BIC info headers
		if (fp_bic) fprintf (fp_bic, "# nrm1\t\tBIC\t\tRSS\t\tdf\t\tnrm2\t\tlambda1\tlambda2\n");
	}

	if (cd->path->verbos) fprintf (stderr, "starting pathwise optimization.\n");

	while (1) {
		bic_info	*info;

		iter++;

		cdescent_set_log10_lambda (cd, logt);
		if (cd->path->verbos) fprintf (stderr, "%d-th iteration lambda1 = %.4e, lamba2 = %.4e ", iter, cd->lambda1, cd->lambda2);


		if (!(converged = cdescent_do_update_one_cycle (cd))) break;

		// reweighting
		if (cd->rwt && !(converged = cdescent_do_reweighting (cd))) break;

		// output solution path
		if (fp_path) fprintf_solutionpath (fp_path, iter, cd->beta);

		info = cdescent_eval_bic (cd);
		// if bic_val < min_bic_val, update min_bic_val, lambda1_opt, nrm1_opt and beta_opt
		if (info->bic_val < cd->path->min_bic_val) {
			store_optimal (cd, iter);
			cd->path->min_bic_val = info->bic_val;
			if (!cd->path->was_modified) cd->path->was_modified = true;
		}
		if (cd->path->verbos) fprintf (stderr, "bic = %.4e ... ", info->bic_val);
		// output BIC info
		if (fp_bic) {
			// |beta|  BIC  RSS  df  ||beta||^2 lambda2
			fprintf (fp_bic, "%.8e\t%.8e\t%.8e\t%.8e\t%.8e\t%.8e\t%.8e\n", cd->nrm1, info->bic_val, info->rss, info->df, mm_real_xj_ssq (cd->beta, 0), cd->lambda1, cd->lambda2);
			fflush (fp_bic);
		}
		free (info);

		if (stop_flag) break;

		/* if logt - dlog10_lambda1 < log10_lambda1, logt = log10_lambda1 and stop_flag is set to true
		 * else logt -= dlog10_lambda1 */
		stop_flag = set_logt (cd->path->log10_lambda_lower, logt - cd->path->dlog10_lambda, &logt);

		if (cd->path->verbos) fprintf (stderr, "done.\n");

	}

	if (fp_path) fclose (fp_path);
	if (fp_bic) fclose (fp_bic);

	return converged;
}
Example #24
0
int main( int argc, char ** argv )  
{  
	/* 用于存储套接口文件描述符 */
	int sockfd;

	/* 初始化全局变量 	  */
	init_global( &global );

	if( argc == 1 ) {       		/* 表示打印所有包头信息 */
		global.print_flag_frame = 1;
		global.print_flag_arp = 1;
		global.print_flag_ip = 1;
		global.print_flag_rarp = 1;
		global.print_flag_tcp = 1;
		global.print_flag_udp = 1;
		global.print_flag_icmp = 1;
		global.print_flag_igmp = 1;
	} else {		       /* 帮助 或者 通过指定协议名称只打印某层些协议 */
		if( !strcasecmp( argv[1], "-h" ) ){
			help();
			exit( 0 );
		} else { 
			int i;
			for( i=1; i < argc; i++ ){
				if( !strcasecmp( argv[i], "frame" ) )
					global.print_flag_frame = 1;
				else if( !strcasecmp( argv[i], "arp" ) )
					global.print_flag_arp = 1;
				else if( !strcasecmp( argv[i], "rarp" ) )
					global.print_flag_rarp = 1;
				else if( !strcasecmp( argv[i], "ip" ) )
					global.print_flag_ip = 1;
				else if( !strcasecmp( argv[i], "tcp" ) )
					global.print_flag_tcp = 1;
				else if( !strcasecmp( argv[i], "udp" ) )
					global.print_flag_udp = 1;
				else if( !strcasecmp( argv[i], "icmp" ) )
					global.print_flag_icmp = 1;
				else if( !strcasecmp( argv[i], "igmp" ) )
					global.print_flag_igmp = 1;
			}
		}
	}
	
	/* 通过协议族AF_PACKET类信SOCK_RAW, 类型SOCK_RAW创建一个用于可以接受网卡帧数据的套接口,同时返回套就口文件描述符 */
	if( (sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)) ) == -1 ) 
	        error_and_exit( "socket", 1 );  /* 如果发生错误,返回错误值, 并退出 */

	
	/* 设定网卡eth0成混杂模式 */
	set_card_promisc( "eth0", sockfd ); 

	/* 设定信号处理函数, 下面是设置当我们按下ctrl-c时所调用的处理函数 */	
	signal( SIGINT, sig_int );

	/* 无限循环接收以太网卡数据帧, 并进行数据分用,直到你按下ctrl-c */
	while( 1 ){
		do_frame( sockfd );
	}  

	return 0;
}
Example #25
0
int main(void) {
    FILE *fh = NULL;
    cmp_ctx_t cmp;
    uint16_t year = 1983;
    uint8_t month = 5;
    uint8_t day = 28;
    int64_t sint = 0;
    uint64_t uint = 0;
    float flt = 0.0f;
    double dbl = 0.0;
    bool boolean = false;
    uint8_t fake_bool = 0;
    uint32_t string_size = 0;
    uint32_t array_size = 0;
    uint32_t binary_size = 0;
    uint32_t map_size = 0;
    int8_t ext_type = 0;
    uint32_t ext_size = 0;
    char sbuf[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    fh = fopen("cmp_data.dat", "w+b");

    if (fh == NULL)
        error_and_exit("Error opening data.dat");

    cmp_init(&cmp, fh, file_reader, file_skipper, file_writer);

    /*
     * When you write an array, you first specify the number of array
     * elements, then you write that many elements.
     */
    if (!cmp_write_array(&cmp, 9))
        error_and_exit(cmp_strerror(&cmp));

    if (!cmp_write_sint(&cmp, -14))
        error_and_exit(cmp_strerror(&cmp));

    if (!cmp_write_uint(&cmp, 38))
        error_and_exit(cmp_strerror(&cmp));

    if (!cmp_write_float(&cmp, 1.8f))
        error_and_exit(cmp_strerror(&cmp));

    if (!cmp_write_double(&cmp, 300.4))
        error_and_exit(cmp_strerror(&cmp));

    if (!cmp_write_nil(&cmp))
        error_and_exit(cmp_strerror(&cmp));

    if (!cmp_write_true(&cmp))
        error_and_exit(cmp_strerror(&cmp));

    if (!cmp_write_false(&cmp))
        error_and_exit(cmp_strerror(&cmp));

    if (!cmp_write_bool(&cmp, false))
        error_and_exit(cmp_strerror(&cmp));

    if (!cmp_write_u8_as_bool(&cmp, 1))
        error_and_exit(cmp_strerror(&cmp));

    /* Array full */

    /*
     * Maps work similar to arrays, but the length is in "pairs", so this
     * writes 2 pairs to the map.  Subsequently, pairs are written in key,
     * value order.
     */

    if (!cmp_write_map(&cmp, 2))
        error_and_exit(cmp_strerror(&cmp));

    /* You can write string data all at once... */

    if (!cmp_write_str(&cmp, "Greeting", 8))
        error_and_exit(cmp_strerror(&cmp));

    if (!cmp_write_str(&cmp, "Hello", 5))
        error_and_exit(cmp_strerror(&cmp));

    if (!cmp_write_str(&cmp, "Name", 4))
        error_and_exit(cmp_strerror(&cmp));

    /* ...or in chunks */

    if (!cmp_write_str_marker(&cmp, 5))
        error_and_exit(cmp_strerror(&cmp));

    if (file_writer(&cmp, "Li", 2) != 2)
        error_and_exit(strerror(errno));

    if (file_writer(&cmp, "nus", 3) != 3)
        error_and_exit(strerror(errno));

    /* Map full */

    /* Binary data functions the same as string data */

    if (!cmp_write_bin(&cmp, "MessagePack", 11))
        error_and_exit(cmp_strerror(&cmp));

    if (!cmp_write_bin_marker(&cmp, 8))
        error_and_exit(cmp_strerror(&cmp));

    if (file_writer(&cmp, "is ", 3) != 3)
        error_and_exit(strerror(errno));

    if (file_writer(&cmp, "great", 5) != 5)
        error_and_exit(strerror(errno));

    /*
     * With extended types, you can create your own custom types.  Here we
     * create a simple date type.
     */

    /* cmp_write_ext_marker(type, size) */
    if (!cmp_write_ext_marker(&cmp, 1, 4))
        error_and_exit(cmp_strerror(&cmp));

    file_writer(&cmp, &year, sizeof(uint16_t));
    file_writer(&cmp, &month, sizeof(uint8_t));
    file_writer(&cmp, &day, sizeof(uint8_t));

    /* Now we can read the data back just as easily */

    rewind(fh);

    if (!cmp_read_array(&cmp, &array_size))
        error_and_exit(cmp_strerror(&cmp));

    if (array_size != 9)
        error_and_exit("Array size was not 9");

    if (!cmp_read_sinteger(&cmp, &sint))
        error_and_exit(cmp_strerror(&cmp));

    if (sint != -14)
        error_and_exit("Signed int was not -14");

    if (!cmp_read_uinteger(&cmp, &uint))
        error_and_exit(cmp_strerror(&cmp));

    if (uint != 38)
        error_and_exit("Unsigned int was not 38");

    if (!cmp_read_float(&cmp, &flt))
        error_and_exit(cmp_strerror(&cmp));

    if (flt != 1.8f)
        error_and_exit("Float was not 1.8f");

    if (!cmp_read_double(&cmp, &dbl))
        error_and_exit(cmp_strerror(&cmp));

    if (dbl != 300.4)
        error_and_exit("Double was not 300.f");

    if (!cmp_read_nil(&cmp))
        error_and_exit(cmp_strerror(&cmp));

    if (!cmp_read_bool(&cmp, &boolean))
        error_and_exit(cmp_strerror(&cmp));

    if (boolean != true)
        error_and_exit("First boolean was not true");

    if (!cmp_read_bool(&cmp, &boolean))
        error_and_exit(cmp_strerror(&cmp));

    if (boolean != false)
        error_and_exit("Second boolean was not false");

    if (!cmp_read_bool(&cmp, &boolean))
        error_and_exit(cmp_strerror(&cmp));

    if (boolean != false)
        error_and_exit("Third boolean was not false");

    if (!cmp_read_bool_as_u8(&cmp, &fake_bool))
        error_and_exit(cmp_strerror(&cmp));

    if (fake_bool != 1) {
        fprintf(stderr, "%u.\n", fake_bool);
        error_and_exit("Third boolean (u8) was not 1");
    }

    if (!cmp_read_map(&cmp, &map_size))
        error_and_exit(cmp_strerror(&cmp));

    if (map_size != 2)
        error_and_exit("Map size was not 2");

    /*
     * String reading here.  Note that normally strings are encoded using
     * UTF-8.  I have cleverly restricted this example to ASCII, which overlaps
     * UTF-8 encoding, but this must not be assumed in real-world code.
     *
     * You can read strings in two ways.  Either you can read the string's size
     * in bytes and then read the bytes manually...
     */

    if (!cmp_read_str_size(&cmp, &string_size))
        error_and_exit(cmp_strerror(&cmp));

    if (string_size != 8)
        error_and_exit("Greeting string key size was not 8");

    if (!read_bytes(sbuf, 8, fh))
        error_and_exit(strerror(errno));

    sbuf[string_size] = 0;

    if (strncmp(sbuf, "Greeting", 8) != 0)
        error_and_exit("Greeting string key name was not 'Greeting'");

    /*
     * ...or you can set the maximum number of bytes to read and do it all in
     * one call.  cmp_read_str will write no more than "size" bytes, including
     * the terminating NULL, to the passed buffer.  If the string's size
     * exceeds the passed buffer size, the "size" input is set to the number of
     * bytes necessary, not including the terminating NULL.  Otherwise, the
     * "size" input is set to the number of bytes written, not including the
     * terminating NULL.
     */

    string_size = sizeof(sbuf);
    if (!cmp_read_str(&cmp, sbuf, &string_size))
        error_and_exit(cmp_strerror(&cmp));

    if (strncmp(sbuf, "Hello", 5) != 0)
        error_and_exit("Greeting string value was not 'Hello'");

    string_size = sizeof(sbuf);
    if (!cmp_read_str(&cmp, sbuf, &string_size))
        error_and_exit(cmp_strerror(&cmp));

    if (strncmp(sbuf, "Name", 4) != 0)
        error_and_exit("Name key name was not 'Name'");

    string_size = sizeof(sbuf);
    if (!cmp_read_str(&cmp, sbuf, &string_size))
        error_and_exit(cmp_strerror(&cmp));

    if (strncmp(sbuf, "Linus", 5) != 0)
        error_and_exit("Name key value was not 'Linus'");

    memset(sbuf, 0, sizeof(sbuf));
    binary_size = sizeof(sbuf);
    if (!cmp_read_bin(&cmp, &sbuf, &binary_size))
        error_and_exit(cmp_strerror(&cmp));

    if (memcmp(sbuf, "MessagePack", 11) != 0)
        error_and_exit("1st binary value was not 'MessagePack'");

    memset(sbuf, 0, sizeof(sbuf));
    binary_size = sizeof(sbuf);
    if (!cmp_read_bin(&cmp, &sbuf, &binary_size))
        error_and_exit(cmp_strerror(&cmp));

    if (memcmp(sbuf, "is great", 8) != 0)
        error_and_exit("2nd binary value was not 'is great'");

    if (!cmp_read_ext_marker(&cmp, &ext_type, &ext_size))
        error_and_exit(cmp_strerror(&cmp));

    if (!read_bytes(&year, sizeof(uint16_t), fh))
        error_and_exit(strerror(errno));

    if (!read_bytes(&month, sizeof(uint8_t), fh))
        error_and_exit(strerror(errno));

    if (!read_bytes(&day, sizeof(uint8_t), fh))
        error_and_exit(strerror(errno));

    if (year != 1983)
        error_and_exit("Year was not 1983");

    if (month != 5)
        error_and_exit("Month was not 5");

    if (day != 28)
        error_and_exit("Day was not 28");

    rewind(fh);

    /* Alternately, you can read objects until the stream is empty */
    while (1) {
        cmp_object_t obj;

        if (!cmp_read_object(&cmp, &obj)) {
            if (feof(fh))
                break;

            error_and_exit(cmp_strerror(&cmp));
        }

        switch (obj.type) {
            case CMP_TYPE_POSITIVE_FIXNUM:
            case CMP_TYPE_UINT8:
                printf("Unsigned Integer: %u\n", obj.as.u8);
                break;
            case CMP_TYPE_FIXMAP:
            case CMP_TYPE_MAP16:
            case CMP_TYPE_MAP32:
                printf("Map: %u\n", obj.as.map_size);
                break;
            case CMP_TYPE_FIXARRAY:
            case CMP_TYPE_ARRAY16:
            case CMP_TYPE_ARRAY32:
                printf("Array: %u\n", obj.as.array_size);
                break;
            case CMP_TYPE_FIXSTR:
            case CMP_TYPE_STR8:
            case CMP_TYPE_STR16:
            case CMP_TYPE_STR32:
                if (!read_bytes(sbuf, obj.as.str_size, fh))
                    error_and_exit(strerror(errno));
                sbuf[obj.as.str_size] = 0;
                printf("String: %s\n", sbuf);
                break;
            case CMP_TYPE_BIN8:
            case CMP_TYPE_BIN16:
            case CMP_TYPE_BIN32:
                memset(sbuf, 0, sizeof(sbuf));
                if (!read_bytes(sbuf, obj.as.bin_size, fh))
                    error_and_exit(strerror(errno));
                printf("Binary: %s\n", sbuf);
                break;
            case CMP_TYPE_NIL:
                printf("NULL\n");
                break;
            case CMP_TYPE_BOOLEAN:
                if (obj.as.boolean)
                    printf("Boolean: true\n");
                else
                    printf("Boolean: false\n");
                break;
            case CMP_TYPE_EXT8:
            case CMP_TYPE_EXT16:
            case CMP_TYPE_EXT32:
            case CMP_TYPE_FIXEXT1:
            case CMP_TYPE_FIXEXT2:
            case CMP_TYPE_FIXEXT4:
            case CMP_TYPE_FIXEXT8:
            case CMP_TYPE_FIXEXT16:
                if (obj.as.ext.type == 1) { /* Date object */
                    if (!read_bytes(&year, sizeof(uint16_t), fh))
                        error_and_exit(strerror(errno));

                    if (!read_bytes(&month, sizeof(uint8_t), fh))
                        error_and_exit(strerror(errno));

                    if (!read_bytes(&day, sizeof(uint8_t), fh))
                        error_and_exit(strerror(errno));

                    printf("Date: %u/%u/%u\n", year, month, day);
                }
                else {
                    printf("Extended type {%d, %u}: ",
                        obj.as.ext.type, obj.as.ext.size
                    );
                    while (obj.as.ext.size--) {
                        read_bytes(sbuf, sizeof(uint8_t), fh);
                        printf("%02x ", sbuf[0]);
                    }
                    printf("\n");
                }
                break;
            case CMP_TYPE_FLOAT:
                printf("Float: %f\n", obj.as.flt);
                break;
            case CMP_TYPE_DOUBLE:
                printf("Double: %f\n", obj.as.dbl);
                break;
            case CMP_TYPE_UINT16:
                printf("Unsigned Integer: %u\n", obj.as.u16);
                break;
            case CMP_TYPE_UINT32:
                printf("Unsigned Integer: %u\n", obj.as.u32);
                break;
            case CMP_TYPE_UINT64:
                printf("Unsigned Integer: %" PRIu64 "\n", obj.as.u64);
                break;
            case CMP_TYPE_NEGATIVE_FIXNUM:
            case CMP_TYPE_SINT8:
                printf("Signed Integer: %d\n", obj.as.s8);
                break;
            case CMP_TYPE_SINT16:
                printf("Signed Integer: %d\n", obj.as.s16);
                break;
            case CMP_TYPE_SINT32:
                printf("Signed Integer: %d\n", obj.as.s32);
                break;
            case CMP_TYPE_SINT64:
                printf("Signed Integer: %" PRId64 "\n", obj.as.s64);
                break;
            default:
                printf("Unrecognized object type %u\n", obj.type);
                break;
        }
    }

    fclose(fh);

    return EXIT_SUCCESS;
}
Example #26
0
int main (int argc, char **argv) {
    rcComm_t           *conn = NULL;
    rodsEnv            irods_env;
    rErrMsg_t          err_msg;
    dataObjInp_t       data_obj;
    openedDataObjInp_t open_obj;
    int                open_fd;
    char    	       *new_host = NULL;
    
    int status;
    char *obj_name = NULL;
    char *buffer;
    char prog_name[255];
    size_t buf_size = DEFAULT_BUFFER_SIZE;
    int verbose = 0;
    int opt;
    unsigned long total_written = 0;
    int write_to_irods = 1;
    int server_set = 0;
    
    while ((opt = getopt(argc, argv, "b:vhrdw")) != -1) {
    	switch (opt) {
	    case 'b':
	    	buf_size = atoi(optarg);
		
		if (buf_size <= 0) {
		    error_and_exit(conn, "Error: buffer size must be greater than 0.\n");
		}
		
		break;
		
	    case 'v':
	    	verbose = 1;
		break;
		
	    case 'r':
	    	write_to_irods = 0;
		break;
		
	    case 'w':
	    	// dummy write option to be enforced later
		break;
		
	    case 'd':
	    	server_set = 1;
		break;
	    
	    case 'h':
	    	usage_and_exit(argv[0], EXIT_SUCCESS);
		break;
		
	    default:
	    	usage_and_exit(argv[0], EXIT_FAILURE);
		break;
	}
    }
    
    if (optind >= argc) {
    	fprintf(stderr, "Error: Missing iRODS file.\n");
	usage_and_exit(argv[0], EXIT_FAILURE);
    }
    
    obj_name = argv[optind];
    
    if ((buffer = malloc(buf_size)) == NULL) {
    	error_and_exit(conn, "Error: unable to set buffer to size %ld\n", buf_size);
    }
    
    // set the client name so iRODS knows what program is connecting to it
    sprintf(prog_name, "%s:%s", PACKAGE_NAME, PACKAGE_VERSION);
   
    if (verbose) {
    	fprintf(stderr, "Setting client name to: %s\n", prog_name);
    }
    
    setenv(SP_OPTION, prog_name, 1);
    
    // lets get the irods environment
    if ((status = getRodsEnv(&irods_env)) < 0) {
    	error_and_exit(conn, "Error: getRodsEnv failed with status %d:%s\n", status, get_irods_error_name(status, verbose));
    }
    
    if ((status = irods_uri_check(obj_name, &irods_env, verbose)) < 0) {
    	error_and_exit(conn, "Error: invalid uri: %s\n", obj_name);
    } else if (status > 0) {
    	server_set = 1;
    }
    
    if (verbose) {
    	fprintf(stderr, "host %s\nzone %s\nuser %s\nport %d\n",
	    irods_env.rodsHost, irods_env.rodsZone,
	    irods_env.rodsUserName, irods_env.rodsPort);
    }
    
    #if IRODS_VERSION_INTEGER && IRODS_VERSION_INTEGER >= 4001008
	init_client_api_table();
   #endif
    
    // make the irods connections
    conn = rcConnect(irods_env.rodsHost, irods_env.rodsPort,
    	    	     irods_env.rodsUserName, irods_env.rodsZone,
		     0, &err_msg);
		     
    if (!conn) {
    	print_irods_error("Error: rcConnect failed:", &err_msg);
	exit(EXIT_FAILURE);
    }
    
    #if IRODS_VERSION_INTEGER && IRODS_VERSION_INTEGER >= 4001008
	status = clientLogin(conn, "", "");
    #else
	status = clientLogin(conn);
    #endif

    if (status < 0) {
    	error_and_exit(conn, "Error: clientLogin failed with status %d:%s\n", status, get_irods_error_name(status, verbose));
    }
  
    // set up the data object
    memset(&data_obj, 0, sizeof(data_obj));
    strncpy(data_obj.objPath, obj_name, MAX_NAME_LEN);
    
    if (write_to_irods) {
    	data_obj.openFlags = O_WRONLY;
    } else {
    	data_obj.openFlags = O_RDONLY;
    }
    
    data_obj.dataSize = 0;

    // talk to server
    if (write_to_irods) {
    	if (!server_set) {
	    if ((status = rcGetHostForPut(conn, &data_obj, &new_host)) < 0) {
		error_and_exit(conn, "Error: rcGetHostForPut failed with status %d:%s\n", status, get_irods_error_name(status, verbose));
	    }

    	    choose_server(&conn, new_host, &irods_env, verbose);
	    free(new_host);
	}

	if ((open_fd = rcDataObjCreate(conn, &data_obj)) < 0) {
    	    error_and_exit(conn, "Error: rcDataObjCreate failed with status %d:%s\n", open_fd, get_irods_error_name(open_fd, verbose));
	}
    } else {
    	if (!server_set) {
	    if ((status = rcGetHostForGet(conn, &data_obj, &new_host)) < 0) {
		error_and_exit(conn, "Error: rcGetHostForGet failed with status %d:%s\n", status, get_irods_error_name(status, verbose));
	    }

    	    choose_server(&conn, new_host, &irods_env, verbose);
	    free(new_host);
	}

	if ((open_fd = rcDataObjOpen(conn, &data_obj)) < 0) {
    	    error_and_exit(conn, "Error: rcDataObjOpen failed with status %d:%s\n", open_fd, get_irods_error_name(open_fd, verbose));
	}
    }
    
    // the read/write loop    
    while (1) {
    	bytesBuf_t data_buffer;
	long read_in;
	long written_out;
	
	// set up common data elements
	memset(&open_obj, 0, sizeof(open_obj));
	open_obj.l1descInx = open_fd;
	data_buffer.buf = buffer;
	
	// time to read something
	if (write_to_irods) {
    	    read_in 	    = fread(buffer, 1, buf_size, stdin);
	    open_obj.len    = read_in;
	    data_buffer.len = open_obj.len;
	} else {
	    open_obj.len = buf_size;
	    data_buffer.len = open_obj.len;
	    
	    if ((read_in = rcDataObjRead(conn, &open_obj, &data_buffer)) < 0) {
    		error_and_exit(conn, "Error:  rcDataObjRead failed with status %ld:%s\n", read_in, get_irods_error_name(read_in, verbose));
	    }
	}
	
	if (verbose) {
	    fprintf(stderr, "%ld bytes read\n", read_in);
	}
	
	if (!read_in) break;
    
	// now try and write something
	if (write_to_irods) {
	    open_obj.len = read_in;
	    data_buffer.len = open_obj.len;

	    if ((written_out = rcDataObjWrite(conn, &open_obj, &data_buffer)) < 0) {
    		error_and_exit(conn, "Error:  rcDataObjWrite failed with status %ld\n", written_out, get_irods_error_name(written_out, verbose));
	    }
	} else {
	    written_out = fwrite(buffer, 1, read_in, stdout);
	}
	
	if (verbose) {
	    fprintf(stderr, "%ld bytes written\n", written_out);
	}
	
	total_written += written_out;
	
	if (read_in != written_out) {
	    error_and_exit(conn, "Error: write fail %ld written, should be %ld.\n", written_out, read_in);
	}
    };
    
    if (verbose) {
    	fprintf(stderr, "Total bytes written %ld\n", total_written);
    }
    
    if ((status = rcDataObjClose(conn, &open_obj)) < 0) {
    	error_and_exit(conn, "Error: rcDataObjClose failed with status %d:%s\n", status, get_irods_error_name(status, verbose));
    }
    
    rcDisconnect(conn);
    free(buffer);
    exit(EXIT_SUCCESS);
}
Example #27
0
int main(int argc, char** argv) {
	if( 1 >= argc){
		printf("usage: %s file offset length\n", basename(argv[0]));
		return 0;
	}
	FILE *fh = NULL;
	cmp_ctx_t cmp;
	uint16_t year = 1983;
	uint8_t month = 5;
	uint8_t day = 28;
	int64_t sint = 0;
	uint64_t uint = 0;
	float flt = 0.0f;
	double dbl = 0.0;
	bool boolean = false;
	uint8_t fake_bool = 0;
	uint32_t string_size = 0;
	uint32_t array_size = 0;
	uint32_t binary_size = 0;
	uint32_t map_size = 0;
	int8_t ext_type = 0;
	uint32_t ext_size = 0;
	char sbuf[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

	fh = fopen(argv[1], "rb");
	if (fh == NULL){
		error_and_exit(strerror(errno));
	}

	cmp_init(&cmp, fh, file_reader, file_writer);

	/* Alternately, you can read objects until the stream is empty */
	while (1) {
		cmp_object_t obj;

		if (!cmp_read_object(&cmp, &obj)) {
			if (feof(fh))
				break;

			error_and_exit(cmp_strerror(&cmp));
		}

		switch (obj.type) {
		case CMP_TYPE_POSITIVE_FIXNUM:
		case CMP_TYPE_UINT8:
			//printf("Unsigned Integer: %u\n", obj.as.u8);
			printf("Unsigned Integer: %u\n", obj.as.u8);
			break;
		case CMP_TYPE_FIXMAP:
		case CMP_TYPE_MAP16:
		case CMP_TYPE_MAP32:
			printf("Map: %u\n", obj.as.map_size);
			break;
		case CMP_TYPE_FIXARRAY:
		case CMP_TYPE_ARRAY16:
		case CMP_TYPE_ARRAY32:
			printf("Array: %u\n", obj.as.array_size);
			break;
		case CMP_TYPE_FIXSTR:
		case CMP_TYPE_STR8:
		case CMP_TYPE_STR16:
		case CMP_TYPE_STR32:
			if (!read_bytes(sbuf, obj.as.str_size, fh))
				error_and_exit(strerror(errno));
			sbuf[obj.as.str_size] = 0;
			printf("String: %s\n", sbuf);
			break;
		case CMP_TYPE_BIN8:
		case CMP_TYPE_BIN16:
		case CMP_TYPE_BIN32:
			memset(sbuf, 0, sizeof(sbuf));
			if (!read_bytes(sbuf, obj.as.bin_size, fh))
				error_and_exit(strerror(errno));
			printf("Binary: %s\n", sbuf);
			break;
		case CMP_TYPE_NIL:
			printf("NULL\n");
			break;
		case CMP_TYPE_BOOLEAN:
			if (obj.as.boolean)
				printf("Boolean: true\n");
			else
				printf("Boolean: false\n");
			break;
		case CMP_TYPE_EXT8:
		case CMP_TYPE_EXT16:
		case CMP_TYPE_EXT32:
		case CMP_TYPE_FIXEXT1:
		case CMP_TYPE_FIXEXT2:
		case CMP_TYPE_FIXEXT4:
		case CMP_TYPE_FIXEXT8:
		case CMP_TYPE_FIXEXT16:
			if (obj.as.ext.type == 1) { /* Date object */
				if (!read_bytes(&year, sizeof(uint16_t), fh))
					error_and_exit(strerror(errno));

				if (!read_bytes(&month, sizeof(uint8_t), fh))
					error_and_exit(strerror(errno));

				if (!read_bytes(&day, sizeof(uint8_t), fh))
					error_and_exit(strerror(errno));

				printf("Date: %u/%u/%u\n", year, month, day);
			} else {
				printf("Extended type {%d, %u}: ", obj.as.ext.type, obj.as.ext.size);
				while (obj.as.ext.size--) {
					read_bytes(sbuf, sizeof(uint8_t), fh);
					printf("%02x ", sbuf[0]);
				}
				printf("\n");
			}
			break;
		case CMP_TYPE_FLOAT:
			printf("Float: %f\n", obj.as.flt);
			break;
		case CMP_TYPE_DOUBLE:
			printf("Double: %f\n", obj.as.dbl);
			break;
		case CMP_TYPE_UINT16:
			printf("Unsigned Integer: %u\n", obj.as.u16);
			break;
		case CMP_TYPE_UINT32:
			printf("Unsigned Integer: %u\n", obj.as.u32);
			break;
		case CMP_TYPE_UINT64:
			printf("Unsigned Integer: %" PRIu64 "\n", obj.as.u64);
			break;
		case CMP_TYPE_NEGATIVE_FIXNUM:
		case CMP_TYPE_SINT8:
			printf("Signed Integer: %d\n", obj.as.s8);
			break;
		case CMP_TYPE_SINT16:
			printf("Signed Integer: %d\n", obj.as.s16);
			break;
		case CMP_TYPE_SINT32:
			printf("Signed Integer: %d\n", obj.as.s32);
			break;
		case CMP_TYPE_SINT64:
			printf("Signed Integer: %" PRId64 "\n", obj.as.s64);
			break;
		default:
			printf("Unrecognized object type %u\n", obj.type);
			break;
		}
	}

	fclose(fh);

	return EXIT_SUCCESS;
}
Example #28
0
/*** sum x(:,j) ***/
double
mm_real_xj_sum (const mm_real *x, const int j)
{
	if (j < 0 || x->n <= j) error_and_exit ("mm_real_xj_sum", "index out of range.", __FILE__, __LINE__);
	return (mm_real_is_sparse (x)) ? mm_real_sj_sum (x, j) : mm_real_dj_sum (x, j);
}
Example #29
0
/*** create new linregmodel object
 * INPUT:
 * mm_dense          *y: dense vector
 * mm_sparse         *x: sparse general / symmetric matrix
 * const double lambda2: regularization parameter
 * const mm_real     *d: general linear operator of penalty
 * const mm_real     *w: L1 penalty factor
 * PreProc         proc: specify pre-processing for y and x
 *                       DO_CENTERING_Y: do centering of y
 *                       DO_CENTERING_X: do centering of each column of x
 *                       DO_NORMALIZING_X: do normalizing of each column of x
 *                       DO_STANDARDIZING_X: do centering and normalizing of each column of x ***/
linregmodel *
linregmodel_new (mm_real *y, mm_real *x, const mm_real *d, PreProc proc)
{
	int			j;
	linregmodel	*lreg;

	/* check whether x and y are not empty */
	if (!y) error_and_exit ("linregmodel_new", "y is empty.", __FILE__, __LINE__);
	if (!x) error_and_exit ("linregmodel_new", "x is empty.", __FILE__, __LINE__);

	/* check whether y is vector */
	if (y->n != 1) error_and_exit ("linregmodel_new", "y must be vector.", __FILE__, __LINE__);

	/* check dimensions of x and y */
	if (y->m != x->m) error_and_exit ("linregmodel_new", "dimensions of x and y do not match.", __FILE__, __LINE__);

	/* check dimensions of x and d */
	if (d && x->n != d->n) error_and_exit ("linregmodel_new", "dimensions of x and d do not match.", __FILE__, __LINE__);

	lreg = linregmodel_alloc ();
	if (lreg == NULL) error_and_exit ("linregmodel_new", "failed to allocate memory for linregmodel object.", __FILE__, __LINE__);

	lreg->y = y;
	lreg->ycentered = calc_sum (lreg->y, &lreg->sy);
	/* if DO_CENTERING_Y is set and y is not already centered */
	if ((proc & DO_CENTERING_Y) && !lreg->ycentered) {
		/* if lreg->y is sparse, convert to dense vector */
		if (mm_real_is_sparse (lreg->y)) mm_real_sparse_to_dense (lreg->y);
		do_centering (lreg->y, lreg->sy);
		lreg->ycentered = true;
	}

	lreg->x = x;
	lreg->xcentered = calc_sum (lreg->x, &lreg->sx);
	/* if DO_CENTERING_X is set and x is not already centered */
	if ((proc & DO_CENTERING_X) && !lreg->xcentered) {
		/* if lreg->x is sparse, convert to dense matrix */
		if (mm_real_is_sparse (lreg->x)) mm_real_sparse_to_dense (lreg->x);
		/* if lreg->x is symmetric, convert to general matrix */
		if (mm_real_is_symmetric (lreg->x)) mm_real_symmetric_to_general (lreg->x);
		do_centering (lreg->x, lreg->sx);
		lreg->xcentered = true;
	};

	lreg->xnormalized = calc_ssq (lreg->x, &lreg->xtx);
	/* if DO_NORMALIZING_X is set and x is not already normalized */
	if ((proc & DO_NORMALIZING_X) && !lreg->xnormalized) {
		/* if lreg->x is symmetric, convert to general matrix */
		if (mm_real_is_symmetric (lreg->x)) mm_real_symmetric_to_general (lreg->x);
		do_normalizing (lreg->x, lreg->xtx);
		lreg->xnormalized = true;
	}

	/* copy d */
	if (d) {
		lreg->d = d;
		lreg->dtd = (double *) malloc (lreg->d->n * sizeof (double));
#pragma omp parallel for
		for (j = 0; j < lreg->d->n; j++) {
			lreg->dtd[j] = mm_real_xj_ssq (lreg->d, j);
		}
	}

	// c = X' * y
	lreg->c = mm_real_new (MM_REAL_DENSE, MM_REAL_GENERAL, lreg->x->n, 1, lreg->x->n);
#pragma omp parallel for
	for (j = 0; j < lreg->x->n; j++) {
		lreg->c->data[j] = mm_real_xj_trans_dot_yk (lreg->x, j, lreg->y, 0);
	}

	// camax = max ( abs (c) )
	lreg->camax = fabs (lreg->c->data[idamax_ (&lreg->c->nnz, lreg->c->data, &ione) - 1]);

	return lreg;
}