Beispiel #1
0
/* code to escape ' '*From' ' at the beginning of a line */
int
appendfiletombox(int in, int out)
{
	int addspace, n, sol;
	char *p;
	Inbuf *b;

	seek(out, 0, 2);

	b = allocinbuf(in, out);
	addspace = 0;
	sol = 1;

	for(;;){
		if(b->wp - b->rp < Fromlen){
			/*
			 * not enough unread bytes in buffer to match "From ",
			 * so get some more.  We must only inject a space at
			 * the start of a line (one that begins with "From ").
			 */
			if (b->rp == b->buf || b->rp[-1] == '\n') {
				n = fill(b, addspace);
				addspace = 0;
			} else
				n = fill(b, 0);
			if(n < 0)
				goto error;
			if(n == 0)
				break;
			if(n < Fromlen){	/* still can't match? */
				b->rp = b->wp;
				continue;
			}
		}

		/* state machine looking for ' '*From' ' */
		if(!sol){
			p = memchr(b->rp, '\n', b->wp - b->rp);
			if(p == nil)
				b->rp = b->wp;
			else{
				b->rp = p+1;
				sol = 1;
			}
			continue;
		} else {
			if(*b->rp == ' ' || strncmp(b->rp, "From ", Fromlen) != 0){
				b->rp++;
				continue;
			}
			addspace = 1;
			sol = 0;
		}
	}

	/* mailbox entries always terminate with two newlines */
	n = b->last == '\n' ? 1 : 2;
	if(write(out, "\n\n", n) != n)
		goto error;
	n += b->bytes;
	free(b);
	return n;
error:
	free(b);
	return -1;
}
Beispiel #2
0
int main( int argc, char **argv )
{
    int done = 0, myid, numprocs, i;
	int from, to;
    int  namelen;
    char processor_name[MPI_MAX_PROCESSOR_NAME];
	double seconds, Mflop_s;;
	
	int root_process = 0;
	int n_iterations = 1, iter = 0;
	int n = 1600; 

   	double *A = (double*) malloc( n * n * sizeof(double) );
   	double *B = (double*) malloc( n * n * sizeof(double) );
   	double *C = (double*) malloc( n * n * sizeof(double) );
    
	MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);
    MPI_Get_processor_name(processor_name,&namelen);
	
	/* These sizes should highlight performance dips at multiples of certain powers-of-two */
	/*Craete and fill 3 random matrices A,B,C*/
	from = myid * n/numprocs;
	to = (myid+1) * n/numprocs;
	if(myid == root_process){
    	printf ("Description:\t%s\n\n", dgemm_desc);
		n_iterations = 1;
	}

START:	
	if (myid == root_process){
		

    	fill( A, n * n );
    	fill( B, n * n );
    	//fill( C, n * n );
        memset( C, 0, sizeof( double ) * n * n );

	}
	MPI_Bcast(A, n * n, MPI_DOUBLE, 0,MPI_COMM_WORLD);
	MPI_Bcast(B, n * n, MPI_DOUBLE, 0,MPI_COMM_WORLD);
	MPI_Bcast(C, n * n, MPI_DOUBLE, 0,MPI_COMM_WORLD);


	if(myid == root_process){
		iter = 0;
	}
	
	double *T = (double*) malloc( n * n * sizeof(double) );
	
ITERATION:
   	if(myid == root_process){
   		seconds = MPI_Wtime();
   	}
   	

	square_dgemm(n, from, to, A, B, C, T);
//	MPI_Barrier(MPI_COMM_WORLD);

	MPI_Gather(T + from * n,
               		n * (n / numprocs),
			        MPI_DOUBLE,
					C + from * n,
				    n * (n / numprocs),
				    MPI_DOUBLE,
				    0,
				    MPI_COMM_WORLD);




/*		
		if (iter < n_iterations){
			iter++;
			goto ITERATION;
		}

        seconds = MPI_Wtime() - seconds;
        if (seconds < 0.1){
			n_iterations *= 2;
			goto START;
		}
*/
        seconds = MPI_Wtime() - seconds;
		Mflop_s = 1e-6 * n_iterations * n * n * n / seconds;
		printf("Mflops: %g time: %g \n", Mflop_s, seconds);
        
		cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n,n,n, -1, A,n, B,n, 1, C,n );
		/*Subtract the maximum allowed roundoff from each element of C*/
        absolute_value( A, n * n );
        absolute_value( B, n * n );
        absolute_value( C, n * n );
        //dgemm( 'N','N', n,n,n, -3.0*DBL_EPSILON*n, A,n, B,n, 1, C,n );
        cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n,n,n, -3.0*DBL_EPSILON*n, A,n, B,n, 1, C,n );
		/*After this test if any element in C is still positive something went wrong in square_dgemm*/
        for( int i = 0; i < n * n; i++ )
            if( C[i] > 0 ) {
                printf( "FAILURE: error in matrix multiply exceeds an acceptable margin\n" );
                exit(-1);
            }

/*		
		if (iter < n_iterations){
			iter++;
			goto ITERATION;
		}

        seconds = MPI_Wtime() - seconds;
        if (seconds < 0.1){
			n_iterations *= 2;
			goto START;
		}
*/

    
	}
Beispiel #3
0
	// Custom method for drawing the object
	void display() 
	{
		fill(255);
		ellipse(xOffset + x, yOffset + y, 6, 6);
	}
Beispiel #4
0
 explicit random_values(std::size_t count, test::random_generator const& generator =
     test::default_generator)
 {
     fill(count, generator);
 }
KJS::Value Pixmap::call( KJS::ExecState *exec, KJS::Object &self, const KJS::List &args ) {

    if( !JSProxy::checkType( self, JSProxy::ValueProxy, "QPixmap") )
        return KJS::Value();

    JSValueProxy *op = JSProxy::toValueProxy( self.imp() );
    pix = op->toVariant().toPixmap();

    KJS::Value retValue = KJS::Value();
    switch ( mid ) {
    case Methodwidth:
        retValue = KJS::Number(width());
        break;
    case Methodheight:
        retValue = KJS::Number(height());
        break;
    case Methoddepth:
        retValue = KJS::Number(depth());
        break;
    case MethodisNull:
        retValue = KJS::Boolean(isNull());
        break;
    case Methodsize:
        retValue = convertToValue(exec, size());
        break;
    case Methodrect:
        retValue = convertToValue(exec, rect());
        break;
    case Methodresize:
    {
        if( args.size() == 2)
		resize(extractInt(exec, args, 0), extractInt(exec, args, 1));
	else if( args.size() == 1)
	    resize(extractQSize(exec, args, 0) );
	break;
    }
    case Methodfill:
        fill( extractQColor(exec, args, 0));
        break;
    case Methodmask:
    {
        retValue = convertToValue(exec, mask() );
        break;
    }
    case MethodsetMask:
    {
       setMask(extractQPixmap(exec, args, 0));
       break;
    }
    case MethodcreateHeuristicMask:
    {
       retValue = convertToValue(exec, createHeuristicMask(extractBool(exec, args, 0)));
       break;
    }
    case MethodgrabWindow:
    {
    	int winid = extractInt(exec, args,0);
	int x = extractInt(exec, args,1);
	int y = extractInt(exec, args,2);
	int w = extractInt(exec, args,3);
	int h = extractInt(exec, args,4);
    	grabWindow(winid,x,y,w,h);
	break;
    }
    default:
        kdWarning() << "Image has no method " << mid << endl;
        break;
    }

    op->setValue(pix);
    return retValue;
}
/* ****************************************************************************
*
* StatusCode::fill -
*/
void StatusCode::fill(StatusCode* scP)
{
  fill(scP->code, scP->details);
}
Beispiel #7
0
//------------------------------------------------------------------------------
void ostream::do_fill(unsigned len) {
  for (; len < width(); len++) {
    putch(fill());
  }
  width(0);
}
/*
 * This function implements the API defined in <linux/decompress/generic.h>.
 *
 * This wrapper will automatically choose single-call or multi-call mode
 * of the native XZ decoder API. The single-call mode can be used only when
 * both input and output buffers are available as a single chunk, i.e. when
 * fill() and flush() won't be used.
 */
STATIC int INIT unxz(unsigned char *in, int in_size,
                     int (*fill)(void *dest, unsigned int size),
                     int (*flush)(void *src, unsigned int size),
                     unsigned char *out, int *in_used,
                     void (*error)(char *x))
{
    struct xz_buf b;
    struct xz_dec *s;
    enum xz_ret ret;
    bool must_free_in = false;

#if XZ_INTERNAL_CRC32
    xz_crc32_init();
#endif

    if (in_used != NULL)
        *in_used = 0;

    if (fill == NULL && flush == NULL)
        s = xz_dec_init(XZ_SINGLE, 0);
    else
        s = xz_dec_init(XZ_DYNALLOC, (uint32_t)-1);

    if (s == NULL)
        goto error_alloc_state;

    if (flush == NULL) {
        b.out = out;
        b.out_size = (size_t)-1;
    } else {
        b.out_size = XZ_IOBUF_SIZE;
        b.out = malloc(XZ_IOBUF_SIZE);
        if (b.out == NULL)
            goto error_alloc_out;
    }

    if (in == NULL) {
        must_free_in = true;
        in = malloc(XZ_IOBUF_SIZE);
        if (in == NULL)
            goto error_alloc_in;
    }

    b.in = in;
    b.in_pos = 0;
    b.in_size = in_size;
    b.out_pos = 0;

    if (fill == NULL && flush == NULL) {
        ret = xz_dec_run(s, &b);
    } else {
        do {
            if (b.in_pos == b.in_size && fill != NULL) {
                if (in_used != NULL)
                    *in_used += b.in_pos;

                b.in_pos = 0;

                in_size = fill(in, XZ_IOBUF_SIZE);
                if (in_size < 0) {
                    /*
                     * This isn't an optimal error code
                     * but it probably isn't worth making
                     * a new one either.
                     */
                    ret = XZ_BUF_ERROR;
                    break;
                }

                b.in_size = in_size;
            }

            ret = xz_dec_run(s, &b);

            if (flush != NULL && (b.out_pos == b.out_size
                                  || (ret != XZ_OK && b.out_pos > 0))) {
                /*
                 * Setting ret here may hide an error
                 * returned by xz_dec_run(), but probably
                 * it's not too bad.
                 */
                if (flush(b.out, b.out_pos) != (int)b.out_pos)
                    ret = XZ_BUF_ERROR;

                b.out_pos = 0;
            }
        } while (ret == XZ_OK);

        if (must_free_in)
            free(in);

        if (flush != NULL)
            free(b.out);
    }

    if (in_used != NULL)
        *in_used += b.in_pos;

    xz_dec_end(s);

    switch (ret) {
    case XZ_STREAM_END:
        return 0;

    case XZ_MEM_ERROR:
        /* This can occur only in multi-call mode. */
        error("XZ decompressor ran out of memory");
        break;

    case XZ_FORMAT_ERROR:
        error("Input is not in the XZ format (wrong magic bytes)");
        break;

    case XZ_OPTIONS_ERROR:
        error("Input was encoded with settings that are not "
              "supported by this XZ decoder");
        break;

    case XZ_DATA_ERROR:
    case XZ_BUF_ERROR:
        error("XZ-compressed data is corrupt");
        break;

    default:
        error("Bug in the XZ decompressor");
        break;
    }

    return -1;

error_alloc_in:
    if (flush != NULL)
        free(b.out);

error_alloc_out:
    xz_dec_end(s);

error_alloc_state:
    error("XZ decompressor ran out of memory");
    return -1;
}
Beispiel #9
0
void fill( ChannelT<T> *channel, T value )
{
	fill( channel, value, channel->getBounds() );
}
Beispiel #10
0
 Surface::Data::Data(Pixel pixel, int w, int h)
    : pixels(w * h)
 {
    fill(begin(pixels), end(pixels), pixel);
 }
Beispiel #11
0
bool StringCell::setCharAt(CharLengthType offset, UnicodeChar unicodeChar)
{
	assert(unicodeChar.isValid());
	return fill(unicodeChar, offset, offset + 1);
}
/* Included from initramfs et al code */
STATIC int INIT gunzip(unsigned char *buf, long len,
		       long (*fill)(void*, unsigned long),
		       long (*flush)(void*, unsigned long),
		       unsigned char *out_buf,
		       long *pos,
		       void(*error)(char *x)) {
	u8 *zbuf;
	struct z_stream_s *strm;
	int rc;
	size_t out_len;

	rc = -1;
	if (flush) {
		out_len = 0x8000; /* 32 K */
		out_buf = malloc(out_len);
	} else {
		out_len = 0x7fffffff; /* no limit */
	}
	if (!out_buf) {
		error("Out of memory while allocating output buffer");
		goto gunzip_nomem1;
	}

	if (buf)
		zbuf = buf;
	else {
		zbuf = malloc(GZIP_IOBUF_SIZE);
		len = 0;
	}
	if (!zbuf) {
		error("Out of memory while allocating input buffer");
		goto gunzip_nomem2;
	}

	strm = malloc(sizeof(*strm));
	if (strm == NULL) {
		error("Out of memory while allocating z_stream");
		goto gunzip_nomem3;
	}

	strm->workspace = malloc(flush ? zlib_inflate_workspacesize() :
				 sizeof(struct inflate_state));
	if (strm->workspace == NULL) {
		error("Out of memory while allocating workspace");
		goto gunzip_nomem4;
	}

	if (!fill)
		fill = nofill;

	if (len == 0)
		len = fill(zbuf, GZIP_IOBUF_SIZE);

	/* verify the gzip header */
	if (len < 10 ||
	   zbuf[0] != 0x1f || zbuf[1] != 0x8b || zbuf[2] != 0x08) {
		if (pos)
			*pos = 0;
		error("Not a gzip file");
		goto gunzip_5;
	}

	/* skip over gzip header (1f,8b,08... 10 bytes total +
	 * possible asciz filename)
	 */
	strm->next_in = zbuf + 10;
	strm->avail_in = len - 10;
	/* skip over asciz filename */
	if (zbuf[3] & 0x8) {
		do {
			/*
			 * If the filename doesn't fit into the buffer,
			 * the file is very probably corrupt. Don't try
			 * to read more data.
			 */
			if (strm->avail_in == 0) {
				error("header error");
				goto gunzip_5;
			}
			--strm->avail_in;
		} while (*strm->next_in++);
	}

	strm->next_out = out_buf;
	strm->avail_out = out_len;

	rc = zlib_inflateInit2(strm, -MAX_WBITS);

	if (!flush) {
		WS(strm)->inflate_state.wsize = 0;
		WS(strm)->inflate_state.window = NULL;
	}

	while (rc == Z_OK) {
		if (strm->avail_in == 0) {
			/* TODO: handle case where both pos and fill are set */
			len = fill(zbuf, GZIP_IOBUF_SIZE);
			if (len < 0) {
				rc = -1;
				error("read error");
				break;
			}
			strm->next_in = zbuf;
			strm->avail_in = len;
		}
		rc = zlib_inflate(strm, 0);

		/* Write any data generated */
		if (flush && strm->next_out > out_buf) {
			long l = strm->next_out - out_buf;
			if (l != flush(out_buf, l)) {
				rc = -1;
				error("write error");
				break;
			}
			strm->next_out = out_buf;
			strm->avail_out = out_len;
		}

		/* after Z_FINISH, only Z_STREAM_END is "we unpacked it all" */
		if (rc == Z_STREAM_END) {
			rc = 0;
			break;
		} else if (rc != Z_OK) {
			error("uncompression error");
			rc = -1;
		}
	}

	zlib_inflateEnd(strm);
	if (pos)
		/* add + 8 to skip over trailer */
		*pos = strm->next_in - zbuf+8;

gunzip_5:
	free(strm->workspace);
gunzip_nomem4:
	free(strm);
gunzip_nomem3:
	if (!buf)
		free(zbuf);
gunzip_nomem2:
	if (flush)
		free(out_buf);
gunzip_nomem1:
	return rc; /* returns Z_OK (0) if successful */
}
Beispiel #13
0
void VectorMatrix::clear()
{
	fill(Vector3d(0.0, 0.0, 0.0));
}
Beispiel #14
0
void scan(Scanner *s)
{
	s->tok = s->cur;

	switch (s->state) {
	default: goto yy0;
	case 0: goto yyFillLabel0;
	case 1: goto yyFillLabel1;
	case 2: goto yyFillLabel2;
	case 3: goto yyFillLabel3;
	}
	for(;;)
	{
		s->tok = s->cur;


yy0:
		switch (s->cond) {
		case EStateNormal: goto yyc_Normal;
		case EStateComment: goto yyc_Comment;
		case EStateSkiptoeol: goto yyc_Skiptoeol;
		case EStateString: goto yyc_String;
		}
/* *********************************** */
yyc_Comment:
		s->state = 0;(0);
		if ((s->lim - s->cur) < 2) if (fill(s, 2) == ~0) break;
yyFillLabel0:
		s->yych = *s->cur;
		switch (s->yych) {
		case '*':	goto yy5;
		default:	goto yy3;
		}
yy3:
		++s->cur;
yy4:
		continue;
yy5:
		s->yych = *++s->cur;
		switch (s->yych) {
		case '/':	goto yy6;
		default:	goto yy4;
		}
yy6:
		++s->cur;
		s->cond = EStateNormal;(EStateNormal);
		continue;
/* *********************************** */
yyc_Normal:
		s->state = 1;(1);
		if ((s->lim - s->cur) < 4) if (fill(s, 4) == ~0) break;
yyFillLabel1:
		s->yych = *s->cur;
		switch (s->yych) {
		case '"':	goto yy12;
		case '\'':	goto yy14;
		case '/':	goto yy15;
		case '?':	goto yy16;
		default:	goto yy10;
		}
yy10:
		++s->cur;
yy11:
		fputc(s->cur[-1], stdout);
		continue;
yy12:
		++s->cur;
		s->cond = EStateString;(EStateString);
		fputc(s->cur[-1], stdout);
		continue;
yy14:
		s->yych = *(s->tok = ++s->cur);
		switch (s->yych) {
		case '"':	goto yy17;
		case '\\':	goto yy19;
		default:	goto yy11;
		}
yy15:
		s->yych = *++s->cur;
		switch (s->yych) {
		case '*':	goto yy20;
		case '/':	goto yy22;
		default:	goto yy11;
		}
yy16:
		s->yych = *(s->tok = ++s->cur);
		switch (s->yych) {
		case '?':	goto yy24;
		default:	goto yy11;
		}
yy17:
		s->yych = *++s->cur;
		switch (s->yych) {
		case '\'':	goto yy25;
		default:	goto yy18;
		}
yy18:
		s->cur = s->tok;
		goto yy11;
yy19:
		s->yych = *++s->cur;
		switch (s->yych) {
		case '"':	goto yy17;
		default:	goto yy18;
		}
yy20:
		++s->cur;
		s->cond = EStateComment;(EStateComment);
		continue;
yy22:
		++s->cur;
		s->cond = EStateSkiptoeol;(EStateSkiptoeol);
		continue;
yy24:
		s->yych = *++s->cur;
		switch (s->yych) {
		case '!':	goto yy27;
		case '\'':	goto yy29;
		case '(':	goto yy31;
		case ')':	goto yy33;
		case '-':	goto yy35;
		case '/':	goto yy37;
		case '<':	goto yy39;
		case '=':	goto yy41;
		case '>':	goto yy43;
		default:	goto yy18;
		}
yy25:
		++s->cur;
		fputl("'\"'", 3, stdout);
		continue;
yy27:
		++s->cur;
		fputc('|', stdout);
		continue;
yy29:
		++s->cur;
		fputc('^', stdout);
		continue;
yy31:
		++s->cur;
		fputc('[', stdout);
		continue;
yy33:
		++s->cur;
		fputc(']', stdout);
		continue;
yy35:
		++s->cur;
		fputc('~', stdout);
		continue;
yy37:
		++s->cur;
		fputc('\\', stdout);
		continue;
yy39:
		++s->cur;
		fputc('{', stdout);
		continue;
yy41:
		++s->cur;
		fputc('#', stdout);
		continue;
yy43:
		++s->cur;
		fputc('}', stdout);
		continue;
/* *********************************** */
yyc_Skiptoeol:
		s->state = 2;(2);
		if ((s->lim - s->cur) < 5) if (fill(s, 5) == ~0) break;
yyFillLabel2:
		s->yych = *s->cur;
		switch (s->yych) {
		case '\n':	goto yy49;
		case '\r':	goto yy51;
		case '?':	goto yy52;
		case '\\':	goto yy53;
		default:	goto yy47;
		}
yy47:
		++s->cur;
yy48:
		continue;
yy49:
		++s->cur;
		s->cond = EStateNormal;(EStateNormal);
		fputc('\n', stdout);
		continue;
yy51:
		s->yych = *++s->cur;
		switch (s->yych) {
		case '\n':	goto yy54;
		default:	goto yy48;
		}
yy52:
		s->yych = *(s->tok = ++s->cur);
		switch (s->yych) {
		case '?':	goto yy56;
		default:	goto yy48;
		}
yy53:
		s->yych = *(s->tok = ++s->cur);
		switch (s->yych) {
		case '\n':	goto yy58;
		case '\r':	goto yy60;
		default:	goto yy48;
		}
yy54:
		++s->cur;
		s->cond = EStateNormal;(EStateNormal);
		fputc('\r', stdout);
		fputc('\n', stdout);
		continue;
yy56:
		s->yych = *++s->cur;
		switch (s->yych) {
		case '/':	goto yy61;
		default:	goto yy57;
		}
yy57:
		s->cur = s->tok;
		goto yy48;
yy58:
		++s->cur;
		continue;
yy60:
		s->yych = *++s->cur;
		switch (s->yych) {
		case '\n':	goto yy58;
		default:	goto yy57;
		}
yy61:
		s->yych = *++s->cur;
		switch (s->yych) {
		case '\n':	goto yy62;
		case '\r':	goto yy64;
		default:	goto yy57;
		}
yy62:
		++s->cur;
		continue;
yy64:
		++s->cur;
		switch ((s->yych = *s->cur)) {
		case '\n':	goto yy62;
		default:	goto yy57;
		}
/* *********************************** */
yyc_String:
		s->state = 3;(3);
		if ((s->lim - s->cur) < 2) if (fill(s, 2) == ~0) break;
yyFillLabel3:
		s->yych = *s->cur;
		switch (s->yych) {
		case '"':	goto yy69;
		case '\\':	goto yy71;
		default:	goto yy67;
		}
yy67:
		++s->cur;
yy68:
		fputc(s->cur[-1], stdout);
		continue;
yy69:
		++s->cur;
		s->cond = EStateNormal;(EStateNormal);
		fputc(s->cur[-1], stdout);
		continue;
yy71:
		s->yych = *++s->cur;
		switch (s->yych) {
		case '\n':	goto yy68;
		default:	goto yy72;
		}
yy72:
		++s->cur;
		fputl((const char*)s->cur-2, 2, stdout);
		continue;

	}
}
Beispiel #15
0
  // Custom method for drawing the object
void Module::draw() {
  fill(255);
  ellipse(xOffset + x, yOffset + y, 6, 6);
}
Beispiel #16
0
void scan(Scanner *s)
{
	s->tok = s->cur;

	switch (s->state) {
	default: goto yy0;
	case 0: goto yyFillLabel0;
	case 1: goto yyFillLabel1;
	case 2: goto yyFillLabel2;
	case 3: goto yyFillLabel3;
	}
	for(;;)
	{
		s->tok = s->cur;

		{

			static void *yyctable[4] = {
				&&yyc_Normal,
				&&yyc_Comment,
				&&yyc_Skiptoeol,
				&&yyc_String,
			};
yy0:
			goto *yyctable[s->cond];
/* *********************************** */
yyc_Comment:
			s->state = 0;(0);
			if ((s->lim - s->cur) < 2) if(fill(s, 2) == ~0) break;
yyFillLabel0:
			s->yych = *s->cur;
			if (s->yych == '*') goto yy5;
			++s->cur;
yy4:
			{
				goto yyc_Comment;
			}
yy5:
			s->yych = *++s->cur;
			if (s->yych != '/') goto yy4;
			++s->cur;
			s->cond = EStateNormal;(EStateNormal);
			{
				continue;
			}
/* *********************************** */
yyc_Normal:
			s->state = 1;(1);
			if ((s->lim - s->cur) < 4) if(fill(s, 4) == ~0) break;
yyFillLabel1:
			s->yych = *s->cur;
			{
				static void *yytarget[256] = {
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy12, &&yy10, &&yy10, &&yy10, &&yy10, &&yy14,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy15,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy16,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10,
					&&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10, &&yy10
				};
				goto *yytarget[s->yych];
			}
yy10:
			++s->cur;
yy11:
			{
				fputc(s->cur[-1], stdout);
				continue;
			}
yy12:
			++s->cur;
			s->cond = EStateString;(EStateString);
			{
				fputc(s->cur[-1], stdout);
				continue;
			}
yy14:
			s->yych = *(s->tok = ++s->cur);
			if (s->yych == '"') goto yy17;
			if (s->yych == '\\') goto yy19;
			goto yy11;
yy15:
			s->yych = *++s->cur;
			if (s->yych == '*') goto yy20;
			if (s->yych == '/') goto yy22;
			goto yy11;
yy16:
			s->yych = *(s->tok = ++s->cur);
			if (s->yych == '?') goto yy24;
			goto yy11;
yy17:
			s->yych = *++s->cur;
			if (s->yych == '\'') goto yy25;
yy18:
			s->cur = s->tok;
			goto yy11;
yy19:
			s->yych = *++s->cur;
			if (s->yych == '"') goto yy17;
			goto yy18;
yy20:
			++s->cur;
			s->cond = EStateComment;(EStateComment);
			{
				goto yyc_Comment;
			}
yy22:
			++s->cur;
			s->cond = EStateSkiptoeol;(EStateSkiptoeol);
			{
				goto yyc_Skiptoeol;
			}
yy24:
			s->yych = *++s->cur;
			{
				static void *yytarget[256] = {
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy27, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy29,
					&&yy31, &&yy33, &&yy18, &&yy18, &&yy18, &&yy35, &&yy18, &&yy37,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy39, &&yy41, &&yy43, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18,
					&&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18, &&yy18
				};
				goto *yytarget[s->yych];
			}
yy25:
			++s->cur;
			{
				fputl("'\"'", 3, stdout);
				continue;
			}
yy27:
			++s->cur;
			{
				fputc('|', stdout);
				continue;
			}
yy29:
			++s->cur;
			{
				fputc('^', stdout);
				continue;
			}
yy31:
			++s->cur;
			{
				fputc('[', stdout);
				continue;
			}
yy33:
			++s->cur;
			{
				fputc(']', stdout);
				continue;
			}
yy35:
			++s->cur;
			{
				fputc('~', stdout);
				continue;
			}
yy37:
			++s->cur;
			{
				fputc('\\', stdout);
				continue;
			}
yy39:
			++s->cur;
			{
				fputc('{', stdout);
				continue;
			}
yy41:
			++s->cur;
			{
				fputc('#', stdout);
				continue;
			}
yy43:
			++s->cur;
			{
				fputc('}', stdout);
				continue;
			}
/* *********************************** */
yyc_Skiptoeol:
			s->state = 2;(2);
			if ((s->lim - s->cur) < 5) if(fill(s, 5) == ~0) break;
yyFillLabel2:
			s->yych = *s->cur;
			{
				static void *yytarget[256] = {
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy49, &&yy47, &&yy47, &&yy51, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy52,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy53, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47,
					&&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47, &&yy47
				};
				goto *yytarget[s->yych];
			}
yy47:
			++s->cur;
yy48:
			{
				goto yyc_Skiptoeol;
			}
yy49:
			++s->cur;
			s->cond = EStateNormal;(EStateNormal);
			{
				fputc('\n', stdout);
				continue;
			}
yy51:
			s->yych = *++s->cur;
			if (s->yych == '\n') goto yy54;
			goto yy48;
yy52:
			s->yych = *(s->tok = ++s->cur);
			if (s->yych == '?') goto yy56;
			goto yy48;
yy53:
			s->yych = *(s->tok = ++s->cur);
			if (s->yych == '\n') goto yy58;
			if (s->yych == '\r') goto yy60;
			goto yy48;
yy54:
			++s->cur;
			s->cond = EStateNormal;(EStateNormal);
			{
				fputc('\r', stdout);
				fputc('\n', stdout);
				continue;
			}
yy56:
			s->yych = *++s->cur;
			if (s->yych == '/') goto yy61;
yy57:
			s->cur = s->tok;
			goto yy48;
yy58:
			++s->cur;
			{
				goto yyc_Skiptoeol;
			}
yy60:
			s->yych = *++s->cur;
			if (s->yych == '\n') goto yy58;
			goto yy57;
yy61:
			s->yych = *++s->cur;
			if (s->yych == '\n') goto yy62;
			if (s->yych == '\r') goto yy64;
			goto yy57;
yy62:
			++s->cur;
			{
				goto yyc_Skiptoeol;
			}
yy64:
			++s->cur;
			if ((s->yych = *s->cur) == '\n') goto yy62;
			goto yy57;
/* *********************************** */
yyc_String:
			s->state = 3;(3);
			if ((s->lim - s->cur) < 2) if(fill(s, 2) == ~0) break;
yyFillLabel3:
			s->yych = *s->cur;
			if (s->yych == '"') goto yy69;
			if (s->yych == '\\') goto yy71;
			++s->cur;
yy68:
			{
				fputc(s->cur[-1], stdout);
				continue;
			}
yy69:
			++s->cur;
			s->cond = EStateNormal;(EStateNormal);
			{
				fputc(s->cur[-1], stdout);
				continue;
			}
yy71:
			s->yych = *++s->cur;
			if (s->yych == '\n') goto yy68;
			++s->cur;
			{
				fputl((const char*)s->cur-2, 2, stdout);
				continue;
			}
		}

	}
}

int main(int argc, char **argv)
{
	Scanner in;

	if (argc != 2)
	{
		fprintf(stderr, "%s <file>\n", argv[0]);
		return 1;;
	}

	memset((char*) &in, 0, sizeof(in));

	if (!strcmp(argv[1], "-"))
	{
		in.fp = stdin;
	}
	else if ((in.fp = fopen(argv[1], "r")) == NULL)
	{
		fprintf(stderr, "Cannot open file '%s'\n", argv[1]);
		return 1;
	}

	if (init(&in) > 0)
	{
 		scan(&in);
 	}

	if (in.fp != stdin)
	{
		fclose(in.fp);
	}
	return 0;
}
Beispiel #17
0
void common_options(int ch)
{
	switch(ch) {
	case 'a':
		options |= F_AUDIBLE;
		break;
	case 'A':
		options |= F_ADAPTIVE;
		break;
	case 'c':
		npackets = atoi(optarg);
		if (npackets <= 0) {
			fprintf(stderr, "ping: bad number of packets to transmit.\n");
			exit(2);
		}
		break;
	case 'd':
		options |= F_SO_DEBUG;
		break;
	case 'f':
		options |= F_FLOOD;
		setbuf(stdout, (char *)NULL);
		break;
	case 'i':		/* wait between sending packets */
	{
		if (strchr(optarg, '.')) {
			float t;
			if (sscanf(optarg, "%f", &t) != 1) {
				fprintf(stderr, "ping: bad timing interval.\n");
				exit(2);
			}
			interval = (int)(t*1000);
		} else if (sscanf(optarg, "%d", &interval) == 1) {
			interval *= 1000;
		} else {
			fprintf(stderr, "ping: bad timing interval.\n");
			exit(2);
		}

		if (interval < 0) {
			fprintf(stderr, "ping: bad timing interval.\n");
			exit(2);
		}
		options |= F_INTERVAL;
		break;
	}
	case 'w':
		deadline = atoi(optarg);
		if (deadline < 0) {
			fprintf(stderr, "ping: bad wait time.\n");
			exit(2);
		}
		break;
	case 'l':
		preload = atoi(optarg);
		if (preload <= 0) {
			fprintf(stderr, "ping: bad preload value, should be 1..%d\n", mx_dup_ck);
			exit(2);
		}
		if (preload > mx_dup_ck)
			preload = mx_dup_ck;
		if (uid && preload > 3) {
			fprintf(stderr, "ping: cannot set preload to value > 3\n");
			exit(2);
		}
		break;
	case 'S':
		sndbuf = atoi(optarg);
		if (sndbuf <= 0) {
			fprintf(stderr, "ping: bad sndbuf value.\n");
			exit(2);
		}
		break;
	case 'n':
		options |= F_NUMERIC;
		break;
	case 'p':		/* fill buffer with user pattern */
		options |= F_PINGFILLED;
		fill(optarg);
		break;
	case 'q':
		options |= F_QUIET;
		break;
	case 'r':
		options |= F_SO_DONTROUTE;
		break;
	case 's':		/* size of packet to send */
		datalen = atoi(optarg);
		if (datalen < 0) {
			fprintf(stderr, "ping: illegal negative packet size %d.\n", datalen);
			exit(2);
		}
		break;
	case 'v':
		options |= F_VERBOSE;
		break;
	case 'L':
		options |= F_NOLOOP;
		break;
	case 't':
		options |= F_TTL;
		ttl = atoi(optarg);
		if (ttl < 0 || ttl > 255) {
			fprintf(stderr, "ping: ttl %u out of range\n", ttl);
			exit(2);
		}
		break;
	case 'U':
		options |= F_LATENCY;
		break;
	case 'B':
		options |= F_STRICTSOURCE;
		break;
	case 'W':
		lingertime = atoi(optarg);
		if (lingertime < 0 || lingertime > INT_MAX/1000000) {
			fprintf(stderr, "ping: bad linger time.\n");
			exit(2);
		}
		lingertime *= 1000;
		break;
	case 'V':
		printf("ping utility, iputils-ss%s\n", SNAPSHOT);
		exit(0);
	default:
		abort();
	}
}
Beispiel #18
0
// Constructor for quadtree with particular size and parent -- build the tree, too!
QuadTree::QuadTree(double* inp_data, int N, double inp_x, double inp_y, double inp_hw, double inp_hh)
{
    init(NULL, inp_data, inp_x, inp_y, inp_hw, inp_hh);
    fill(N);
}
/* ****************************************************************************
*
* StatusCode::fill -
*/
void StatusCode::fill(const StatusCode& sc)
{
  fill(sc.code, sc.details);
}
Beispiel #20
0
// Constructor for quadtree with particular size and parent -- build the tree, too!
QuadTree::QuadTree(QuadTree* inp_parent, double* inp_data, int N, double inp_x, double inp_y, double inp_hw, double inp_hh)
{
    init(inp_parent, inp_data, inp_x, inp_y, inp_hw, inp_hh);
    fill(N);
}
	AlphaReal BanditSingleSparseStump::run()
	{

		if ( ! this->_banditAlgo->isInitialized() ) {
			init();
		}

		const int numClasses = _pTrainingData->getNumClasses();
		const int numColumns = _pTrainingData->getNumAttributes();

		// set the smoothing value to avoid numerical problem
		// when theta=0.
		setSmoothingVal( (AlphaReal) 1.0 / (AlphaReal)_pTrainingData->getNumExamples() * (AlphaReal)0.01 );

		vector<sRates> mu(numClasses); // The class-wise rates. See BaseLearner::sRates for more info.
		vector<AlphaReal> tmpV(numClasses); // The class-wise votes/abstentions

		FeatureReal tmpThreshold;
		AlphaReal tmpAlpha;

		AlphaReal bestEnergy = numeric_limits<AlphaReal>::max();
		AlphaReal tmpEnergy;

		StumpAlgorithmLSHTC<FeatureReal> sAlgo(numClasses);
		sAlgo.initSearchLoop(_pTrainingData);

		AlphaReal halfTheta;
		if ( _abstention == ABST_REAL || _abstention == ABST_CLASSWISE )
			halfTheta = _theta/(AlphaReal)2.0;
		else
			halfTheta = 0;
		
		AlphaReal bestReward = 0.0;


		_banditAlgo->getKBestAction( _K, _armsForPulling );
		_rewards.resize( _armsForPulling.size() );

		if ( this->_armsForPulling.size() == 0 )
		{
			cout << "error" << endl;
		}

		for( int i = 0; i < (int)_armsForPulling.size(); i++ ) {
			//columnIndices[i] = p.second;			


			const pair<vpReverseIterator,vpReverseIterator> dataBeginEnd = 
				static_cast<SortedData*>(_pTrainingData)->getFileteredReverseBeginEnd( _armsForPulling[i] );

			/*
			const pair<vpIterator,vpIterator> dataBeginEnd = 
			static_cast<SortedData*>(_pTrainingData)->getFileteredBeginEnd( _armsForPulling[i] );
			*/

			const vpReverseIterator dataBegin = dataBeginEnd.first;
			const vpReverseIterator dataEnd = dataBeginEnd.second;

			/*
			const vpIterator dataBegin = dataBeginEnd.first;
			const vpIterator dataEnd = dataBeginEnd.second;
			*/

			// also sets mu, tmpV, and bestHalfEdge
			tmpThreshold = sAlgo.findSingleThresholdWithInit(dataBegin, dataEnd, _pTrainingData, 
				halfTheta, &mu, &tmpV);

			tmpEnergy = getEnergy(mu, tmpAlpha, tmpV);
			//update the weights in the UCT tree

			AlphaReal edge = 0.0;
			for ( vector<sRates>::iterator itR = mu.begin(); itR != mu.end(); itR++ ) edge += ( itR->rPls - itR->rMin );
			AlphaReal reward = this->getRewardFromEdge( edge );
			_rewards[i] = reward;

			if ( _verbose > 3 ) {
				//cout << "\tK = " <<i << endl;
				cout << "\tTempAlpha: " << tmpAlpha << endl;
				cout << "\tTempEnergy: " << tmpEnergy << endl;
				cout << "\tUpdate weight: " << reward << endl;
			}


			if ( (i==0) || (tmpEnergy < bestEnergy && tmpAlpha > 0) )
			{
				// Store it in the current weak hypothesis.
				// note: I don't really like having so many temp variables
				// but the alternative would be a structure, which would need
				// to be inheritable to make things more consistent. But this would
				// make it less flexible. Therefore, I am still undecided. This
				// might change!

				_alpha = tmpAlpha;
				_v = tmpV;
				_selectedColumn = _armsForPulling[i];
				_threshold = tmpThreshold;

				bestEnergy = tmpEnergy;
				bestReward = reward;
			}
		}

		if ( _banditAlgoName == BA_EXP3G2 )
		{
			vector<AlphaReal> ePayoffs( numColumns );			
			fill( ePayoffs.begin(), ePayoffs.end(), 0.0 );

			for( int i=0; i<_armsForPulling.size(); i++ )
			{
				ePayoffs[_armsForPulling[i]] = _rewards[i];
			}		
			estimatePayoffs( ePayoffs );

			(dynamic_cast<Exp3G2*>(_banditAlgo))->receiveReward( ePayoffs );
		} else {
			for( int i=0; i<_armsForPulling.size(); i++ )
			{
				_banditAlgo->receiveReward( _armsForPulling[i], _rewards[i] );
			}		
		}

		if ( _verbose > 2 ) cout << "Column has been selected: " << _selectedColumn << endl;

		stringstream thresholdString;
		thresholdString << _threshold;
		_id = _pTrainingData->getAttributeNameMap().getNameFromIdx(_selectedColumn) + thresholdString.str();

		_reward = bestReward;

		return bestEnergy;
	}
Beispiel #22
0
	float SelectorLearner::run()
	{
		const int numClasses = _pTrainingData->getNumClasses();
		const int numColumns = _pTrainingData->getNumAttributes();
		const int numExamples = _pTrainingData->getNumExamples();
		
		// set the smoothing value to avoid numerical problem
		// when theta=0.
		setSmoothingVal( 1.0 / (float)_pTrainingData->getNumExamples() * 0.01 );
		
		vector<sRates> vMu(numClasses); // The class-wise rates. See BaseLearner::sRates for more info.
		vector<float> tmpV(numClasses); // The class-wise votes/abstentions
		
		float tmpAlpha, tmpEnergy;
		float bestEnergy = numeric_limits<float>::max();
		
		int numOfDimensions = _maxNumOfDimensions;
		for (int j = 0; j < numColumns; ++j)
		{
			// Tricky way to select numOfDimensions columns randomly out of numColumns
			int rest = numColumns - j;
			float r = rand()/static_cast<float>(RAND_MAX);
			
			if ( static_cast<float>(numOfDimensions) / rest > r ) 
			{
				--numOfDimensions;
				/*
				 if (_verbose > 2)
				 cout << "    --> trying attribute = "
				 <<_pTrainingData->getAttributeNameMap().getNameFromIdx(j)
				 << endl << flush;
				 */
				const int numIdxs = _pTrainingData->getEnumMap(j).getNumNames();
				
				// Create and initialize the numIdxs x numClasses gamma matrix
				vector<vector<float> > tmpGammasPls(numIdxs);
				vector<vector<float> > tmpGammasMin(numIdxs);
				for (int io = 0; io < numIdxs; ++io) {
					vector<float> tmpGammaPls(numClasses);
					vector<float> tmpGammaMin(numClasses);
					fill(tmpGammaPls.begin(), tmpGammaPls.end(), 0.0);
					fill(tmpGammaMin.begin(), tmpGammaMin.end(), 0.0);
					tmpGammasPls[io] = tmpGammaPls;
					tmpGammasMin[io] = tmpGammaMin;
				}
				
				// Compute the elements of the gamma plus and minus matrices
				float entry;
				for (int i = 0; i < numExamples; ++i) {
					const vector<Label>& labels = _pTrainingData->getLabels(i);
					int io = static_cast<int>(_pTrainingData->getValue(i,j));	    
					for (int l = 0; l < numClasses; ++l) {
						entry = labels[l].weight * labels[l].y;
						if (entry > 0)
							tmpGammasPls[io][l] += entry;
						else if (entry < 0)
							tmpGammasMin[io][l] += -entry;
					}
				}
				
				// Initialize the u vector to random +-1
				vector<sRates> uMu(numIdxs); // The idx-wise rates
				vector<float> tmpU(numIdxs);// The idx-wise votes/abstentions
				vector<float> previousTmpU(numIdxs);// The idx-wise votes/abstentions
				
				for (int io = 0; io < numIdxs; ++io) {
					uMu[io].classIdx = io;	
				}
				
				for (int io = 0; io < numIdxs; ++io) {					
					// initializing u as it has only one positive element
					fill( tmpU.begin(), tmpU.end(), -1 );
					tmpU[io] = +1;
					
					vector<sRates> vMu(numClasses); // The label-wise rates
					for (int l = 0; l < numClasses; ++l)
						vMu[l].classIdx = l;
					vector<float> tmpV(numClasses); // The label-wise votes/abstentions
					
					float tmpVal;
					tmpAlpha = 0.0;
					
					//filling out tmpV and vMu
					for (int l = 0; l < numClasses; ++l) {
						vMu[l].rPls = vMu[l].rMin = vMu[l].rZero = 0; 
						for (int io = 0; io < numIdxs; ++io) {
							if (tmpU[io] > 0) {
								vMu[l].rPls += tmpGammasPls[io][l];
								vMu[l].rMin += tmpGammasMin[io][l];
							}
							else if (tmpU[io] < 0) {
								vMu[l].rPls += tmpGammasMin[io][l];
								vMu[l].rMin += tmpGammasPls[io][l];
							}
						}
						if (vMu[l].rPls >= vMu[l].rMin) {
							tmpV[l] = +1;
						}
						else {
							tmpV[l] = -1;
							tmpVal = vMu[l].rPls;
							vMu[l].rPls = vMu[l].rMin;
							vMu[l].rMin = tmpVal;
						}
					}
					
					tmpEnergy = AbstainableLearner::getEnergy(vMu, tmpAlpha, tmpV);
					
					if ( tmpEnergy < bestEnergy && tmpAlpha > 0 ) {
						_alpha = tmpAlpha;
						_v = tmpV;
						//_u = tmpU;
						_positiveIdxOfArrayU = io;
						_selectedColumn = j;
						bestEnergy = tmpEnergy;
					}
				}
			}
		}
		
		
		if (_selectedColumn>-1)
		{
			_id = _pTrainingData->getAttributeNameMap().getNameFromIdx(_selectedColumn);
			return bestEnergy;
		} else {
			return bestEnergy = numeric_limits<float>::signaling_NaN();
		}				
	}
/**
 * @note ERR is a preprocessor definition which determines convergence
 */
void* thread_pagerank_power(void* arg)
{
  const int tid = *(int*)arg;
  uint chunk = N / NTHREADS;
  if (chunk == 0) chunk = 1;
  uint start = tid * chunk;
  uint end;
  if (tid != NTHREADS - 1)
    end = (tid + 1) * chunk;
  else
    end = N;

  const FLOAT p = 0.85;
  const FLOAT delta = (1 - p) / N;

  fill(zPtr, 1 / (FLOAT)N, start, end);
  fill(xPtr, 0, start, end);

  /* printf("%d: C\n", tid); */
  uint well_c, i, j;
  for (i = well_c = start; i < end; ++i)
  {
    if (C[i] == 0) wells[well_c++] = i;
  }

  /* printf("%d: begin iters\n", tid); */
  FLOAT total_wells_prob, total_maximum;
  do
  {
    /* printf("%d: filling normals\n", tid); */
    if (cnt != 0)
      fill(xPtr, 0, start, end);
    for (i = start; i < end; ++i)
    {
      for (j = 0; j < RC[i]; ++j)
      {
        uint coming = R[i][j];
        xPtr[i] += zPtr[coming] / C[coming];
      }
    }
    /* printf("%d: adding local wells\n", tid); */
    total_wells_prob = 0;
    for (i = start; i < well_c; ++i)
      total_wells_prob += zPtr[wells[i]];
    wells_prob[tid] = total_wells_prob;
    /* printf("%d: finding global wells\n", tid); */
    pthread_barrier_wait(barrierPtr);
    total_wells_prob = 0;
    for (j = 0; j < NTHREADS; ++j)
      total_wells_prob += wells_prob[j];
    add(xPtr, total_wells_prob / N, start, end);
    multiply(xPtr, p, start, end);
    add(xPtr, delta, start, end);
    /* printf("%d: finding local maximum\n", tid); */
    maximum[tid] = max_abs_diff(xPtr, zPtr, start, end);
    // find global maximum and share knowledge across threads
    pthread_barrier_wait(barrierPtr);
    /* printf("%d: finding global maximum\n", tid); */
    total_maximum = 0;
    for (j = 0; j < NTHREADS; ++j)
      total_maximum = (total_maximum < maximum[j]) ? maximum[j] : total_maximum;
    if (tid == 0)
    {
      ++cnt;
      swap(&xPtr, &zPtr);
    }
    pthread_barrier_wait(barrierPtr);
  } while (total_maximum >= ERR);

  pthread_exit(NULL);
}
void scan(Scanner *s)
{
	fill(s, 0);

	for(;;)
	{
		s->tok = s->cur;

		if (s->cond < 2) {
			if (s->cond < 1) {
				goto yyc_Normal;
			} else {
				goto yyc_Comment;
			}
		} else {
			if (s->cond < 3) {
				goto yyc_Skiptoeol;
			} else {
				goto yyc_String;
			}
		}
/* *********************************** */
yyc_Comment:
		if ((s->lim - s->cur) < 2) if(fill(s, 2) >= 0) break;
		s->yych = *s->cur;
		if (s->yych != '*') goto yy5;
		++s->cur;
		if ((s->yych = *s->cur) == '/') goto yy6;
yy4:
		{
				goto yyc_Comment;
			}
yy5:
		s->yych = *++s->cur;
		goto yy4;
yy6:
		++s->cur;
		{
				s->cond = EStateNormal;
				continue;
			}
/* *********************************** */
yyc_Normal:
		if ((s->lim - s->cur) < 4) if(fill(s, 4) >= 0) break;
		s->yych = *s->cur;
		if (s->yych <= '\'') {
			if (s->yych == '"') goto yy14;
			if (s->yych <= '&') goto yy16;
			goto yy13;
		} else {
			if (s->yych <= '/') {
				if (s->yych <= '.') goto yy16;
				goto yy12;
			} else {
				if (s->yych != '?') goto yy16;
			}
		}
		s->yych = *(s->tok = ++s->cur);
		if (s->yych == '?') goto yy27;
yy11:
		{
				fputc(*s->tok, stdout);
				continue;
			}
yy12:
		s->yych = *++s->cur;
		if (s->yych == '*') goto yy25;
		if (s->yych == '/') goto yy23;
		goto yy11;
yy13:
		s->yych = *(s->tok = ++s->cur);
		if (s->yych == '"') goto yy17;
		if (s->yych == '\\') goto yy19;
		goto yy11;
yy14:
		++s->cur;
		{
				fputc(*s->tok, stdout);
				s->state = EStateString;
				continue;
			}
yy16:
		s->yych = *++s->cur;
		goto yy11;
yy17:
		s->yych = *++s->cur;
		if (s->yych == '\'') goto yy21;
yy18:
		s->cur = s->tok;
		goto yy11;
yy19:
		s->yych = *++s->cur;
		if (s->yych != '"') goto yy18;
		s->yych = *++s->cur;
		if (s->yych != '\'') goto yy18;
yy21:
		++s->cur;
		{
				fputl("'\"'", 3, stdout);
				continue;
			}
yy23:
		++s->cur;
		{
				s->cond = EStateSkiptoeol;
				goto yyc_Skiptoeol;
			}
yy25:
		++s->cur;
		{
				s->cond = EStateComment;
				goto yyc_Comment;
			}
yy27:
		s->yych = *++s->cur;
		switch (s->yych) {
		case '!':	goto yy42;
		case '\'':	goto yy40;
		case '(':	goto yy28;
		case ')':	goto yy30;
		case '-':	goto yy44;
		case '/':	goto yy38;
		case '<':	goto yy32;
		case '=':	goto yy36;
		case '>':	goto yy34;
		default:	goto yy18;
		}
yy28:
		++s->cur;
		{
				fputc('[', stdout);
				continue;
			}
yy30:
		++s->cur;
		{
				fputc(']', stdout);
				continue;
			}
yy32:
		++s->cur;
		{
				fputc('{', stdout);
				continue;
			}
yy34:
		++s->cur;
		{
				fputc('}', stdout);
				continue;
			}
yy36:
		++s->cur;
		{
				fputc('#', stdout);
				continue;
			}
yy38:
		++s->cur;
		{
				fputc('\\', stdout);
				continue;
			}
yy40:
		++s->cur;
		{
				fputc('^', stdout);
				continue;
			}
yy42:
		++s->cur;
		{
				fputc('|', stdout);
				continue;
			}
yy44:
		++s->cur;
		{
				fputc('~', stdout);
				continue;
			}
/* *********************************** */
yyc_Skiptoeol:
		if ((s->lim - s->cur) < 5) if(fill(s, 5) >= 0) break;
		s->yych = *s->cur;
		if (s->yych <= '>') {
			if (s->yych == '\n') goto yy51;
			goto yy53;
		} else {
			if (s->yych <= '?') goto yy48;
			if (s->yych == '\\') goto yy50;
			goto yy53;
		}
yy48:
		s->yych = *(s->tok = ++s->cur);
		if (s->yych == '?') goto yy58;
yy49:
		{
				goto yyc_Skiptoeol;
			}
yy50:
		s->yych = *(s->tok = ++s->cur);
		if (s->yych == '\n') goto yy56;
		if (s->yych == '\r') goto yy54;
		goto yy49;
yy51:
		++s->cur;
		{
				s->cond = EStateNormal;
				continue;
			}
yy53:
		s->yych = *++s->cur;
		goto yy49;
yy54:
		s->yych = *++s->cur;
		if (s->yych == '\n') goto yy56;
yy55:
		s->cur = s->tok;
		goto yy49;
yy56:
		++s->cur;
		{
				goto yyc_Skiptoeol;
			}
yy58:
		s->yych = *++s->cur;
		if (s->yych != '/') goto yy55;
		s->yych = *++s->cur;
		if (s->yych == '\n') goto yy61;
		if (s->yych != '\r') goto yy55;
		s->yych = *++s->cur;
		if (s->yych != '\n') goto yy55;
yy61:
		++s->cur;
		{
				goto yyc_Skiptoeol;
			}
/* *********************************** */
yyc_String:
		if ((s->lim - s->cur) < 2) if(fill(s, 2) >= 0) break;
		s->yych = *s->cur;
		if (s->yych == '"') goto yy67;
		if (s->yych != '\\') goto yy69;
		++s->cur;
		if ((s->yych = *s->cur) != '\n') goto yy70;
yy66:
		{
				fputc(*s->tok, stdout);
				continue;
			}
yy67:
		++s->cur;
		{
				fputc(*s->tok, stdout);
				s->cond = EStateNormal;
				continue;
			}
yy69:
		s->yych = *++s->cur;
		goto yy66;
yy70:
		++s->cur;
		{
				fputl((const char*)s->tok, 2, stdout);
				continue;
			}

	}
}
CRSSparsity sp_triplet(int nrow, int ncol, const std::vector<int>& row, const std::vector<int>& col, std::vector<int>& mapping, bool invert_mapping){
  // Assert dimensions
  casadi_assert_message(row.size()==col.size(),"inconsistent lengths");

  // Create the return sparsity pattern and access vectors
  CRSSparsity ret(nrow,ncol);
  vector<int> &r_rowind = ret.rowindRef();
  vector<int> &r_col = ret.colRef();
  r_col.reserve(col.size());

  // Consistency check and check if elements are already perfectly ordered with no duplicates
  int last_row=-1, last_col=-1;
  bool perfectly_ordered=true;
  for(int k=0; k<row.size(); ++k){
    // Consistency check
    casadi_assert_message(row[k]>=0 && row[k]<nrow,"Row index out of bounds");
    casadi_assert_message(col[k]>=0 && col[k]<ncol,"Col index out of bounds");
    
    // Check if ordering is already perfect
    perfectly_ordered = perfectly_ordered && (row[k]<last_row || (row[k]==last_row && col[k]<=last_col));
    last_row = row[k];
    last_col = col[k];
  }
  
  // Quick return if perfectly ordered
  if(perfectly_ordered){
    // Save columns
    r_col.resize(col.size());
    copy(col.begin(),col.end(),r_col.begin());
    
    // Find offset index
    int el=0;
    for(int i=0; i<nrow; ++i){
      while(el<row.size() && row[el]==i) el++; 
      r_rowind[i+1] = el;
    }
    
    // Identity mapping
    mapping.resize(col.size());
    for(int k=0; k<col.size(); ++k)
      mapping[k] = k;
    
    // Quick return
    return ret;
  }
    
  // Reuse data
  vector<int>& mapping1 = invert_mapping ? r_col : mapping;
  vector<int>& mapping2 = invert_mapping ? mapping : r_col;
  
  // Make sure that enough memory is allocated to use as a work vector
  mapping1.reserve(std::max(ncol+1,int(row.size())));
  
  // Number of elements in each column
  vector<int>& colcount = mapping1; // reuse memory
  colcount.resize(ncol+1);
  fill(colcount.begin(),colcount.end(),0);
  for(vector<int>::const_iterator it=col.begin(); it!=col.end(); ++it){
    colcount[*it+1]++;
  }
  
  // Cumsum to get index offset for each column
  for(int i=0; i<ncol; ++i){
    colcount[i+1] += colcount[i];
  }
  
  // New column for each old column
  mapping2.resize(col.size());
  for(int k=0; k<col.size(); ++k){
    mapping2[colcount[col[k]]++] = k;
  }
  
  // Number of elements in each row
  vector<int>& rowcount = r_rowind; // reuse memory, r_rowind is already the right size and is filled with zeros
  for(vector<int>::const_iterator it=mapping2.begin(); it!=mapping2.end(); ++it){
    rowcount[row[*it]+1]++;
  }
  
  // Cumsum to get index offset for each row
  for(int i=0; i<nrow; ++i){
    rowcount[i+1] += rowcount[i];
  }

  // New row for each old row
  mapping1.resize(row.size());
  for(vector<int>::const_iterator it=mapping2.begin(); it!=mapping2.end(); ++it){
    mapping1[rowcount[row[*it]]++] = *it;
  }

  // Current element in the return matrix
  int r_el = 0;
  r_col.resize(row.size());

  // Current nonzero
  vector<int>::const_iterator it=mapping1.begin();

  // Loop over rows
  r_rowind[0] = 0;
  for(int i=0; i<nrow; ++i){
    
    // Previous column (to detect duplicates)
    int j_prev = -1;
    
    // Loop over nonzero elements of the row
    while(it!=mapping1.end() && row[*it]==i){

      // Get the element
      int el = *it;
      it++;

      // Get the column
      int j = col[el];
      
      // If not a duplicate, save to return matrix
      if(j!=j_prev)
        r_col[r_el++] = j;
      
      if(invert_mapping){
        // Save to the inverse mapping
        mapping2[el] = r_el-1;        
      } else {
        // If not a duplicate, save to the mapping vector
        if(j!=j_prev)
          mapping1[r_el-1] = el;
      }
      
      // Save column
      j_prev = j;
    }
    
    // Update row offset
    r_rowind[i+1] = r_el;
  }

  // Resize the column vector
  r_col.resize(r_el);

  // Resize mapping matrix
  if(!invert_mapping){
    mapping1.resize(r_el);
  }
  
  return ret;
}
Beispiel #26
0
int main()
{
  int  m,n,p,i,j,comb,max;
  printf("Enter number of equations and variables\n");
  scanf("%d %d",&m,&n);
  p=n+1;
  float *optim=(float *)malloc(n*sizeof(float));
  float *blank=(float *)malloc(n*sizeof(float));
  float **mat = allocate(m,p);
  printf("Enter the coefficients of the equations\n");
  scanf("%*c");
  for(i=0;i<m;i++)
    {
      printf("Enter equation %d \n",i+1);
      for(j=0;j<p;j++)
	{
	  scanf("%f",(j+*(mat+i)));
	}
    }
  printf("The coefficient matrix is \n");
  printmatrix(mat,m,p);
  printf("\n");
  printf("Enter the function to be optimized \n");
  for(j=0;j<n;j++)
    {
      scanf("%f",optim+j);
    }
  printf("Enter 1 to maximize, 0 to minimize \n");
  scanf("%d",&max);
  
  comb =ncr(n,n-m);
  float **sols = allocate(comb,n);
  float **store = allocate(comb,n);
  float **temp = allocate(m,m+1);
  int *type=(int *)malloc(comb*sizeof(int));
  fill(blank,0,m,n,store);
  for( i=0;i<comb;i++)
    {
      int k=0,l;
      for(j=0;j<n;j++)
	{
	  if(fabs(store[i][j] - 1) < 0.001)
	    {
	     
	      for(l=0;l<m;l++)
		{
		  temp[l][k]=mat[l][j];
		}
	      k++;
	    }
	}
      for(l=0;l<m;l++)
	{
	  temp[l][k]=mat[l][n];
	}
      //printmatrix(temp,m,m+1);
      //  printf("\n");
      float * fs = (float *)malloc(m*sizeof(float));
     type[i]= gauss(temp,m,fs);
      k =0;
      for(j=0;j<n;j++)
	{
	  if(store[i][j] < 0.001)
	    {
	      sols[i][j]=0;
	    }
	  else
	    {
	      sols[i][j]=fs[k];
	      k++;
	    }
	}
    }
  //  printmatrix(sols,comb,n);
  printsolution(sols,m,n,type,optim,max);
  return 0;
}
Beispiel #27
0
int main()
{
    // geometries container
    map geometries;

    // create some geometries
    for ( unsigned i = 0 ; i < 10 ; ++i )
    {
        unsigned c = rand() % 3;

        if ( 0 == c )
        {
            // create polygon
            polygon p;
            fill(i, p.outer());
            geometries.insert(std::make_pair(i, geometry(p)));
        }
        else if ( 1 == c )
        {
            // create ring
            ring r;
            fill(i, r);
            geometries.insert(std::make_pair(i, geometry(r)));
        }
        else if ( 2 == c )
        {
            // create linestring
            linestring l;
            fill(i, l);
            geometries.insert(std::make_pair(i, geometry(l)));
        }
    }

    // display geometries
    std::cout << "generated geometries:" << std::endl;
    BOOST_FOREACH(map::value_type const& p, geometries)
        boost::apply_visitor(print_visitor(), p.second);

    // create the rtree using default constructor
    bgi::rtree< value, bgi::quadratic<16, 4> > rtree;

    // fill the spatial index
    for ( map::iterator it = geometries.begin() ; it != geometries.end() ; ++it )
    {
        // calculate polygon bounding box
        box b = boost::apply_visitor(envelope_visitor(), it->second);
        // insert new value
        rtree.insert(std::make_pair(b, it));
    }

    // find values intersecting some area defined by a box
    box query_box(point(0, 0), point(5, 5));
    std::vector<value> result_s;
    rtree.query(bgi::intersects(query_box), std::back_inserter(result_s));

    // find 5 nearest values to a point
    std::vector<value> result_n;
    rtree.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n));

    // note: in Boost.Geometry the WKT representation of a box is polygon

    // note: the values store the bounding boxes of geometries
    // the geometries aren't used for querying but are printed

    // display results
    std::cout << "spatial query box:" << std::endl;
    std::cout << bg::wkt<box>(query_box) << std::endl;
    std::cout << "spatial query result:" << std::endl;
    BOOST_FOREACH(value const& v, result_s)
        boost::apply_visitor(print_visitor(), v.second->second);

    std::cout << "knn query point:" << std::endl;
    std::cout << bg::wkt<point>(point(0, 0)) << std::endl;
    std::cout << "knn query result:" << std::endl;
    BOOST_FOREACH(value const& v, result_n)
        boost::apply_visitor(print_visitor(), v.second->second);

    return 0;
}
Beispiel #28
0
int main(int argc, char *argv[])
{
    char msg[65500];
    struct addrinfo hints, *res;
    const char *source_host = NULL;
    const char *target_host = NULL;
    size_t len;
    int kindy;
    int fodder;
    
    while ((fodder = getopt(argc, argv, "s:t:")) != -1) {
        switch (fodder) {
        case 's' :
            if ((source_host = strdup(optarg)) == NULL) {
                perror("strdup");
                return 1;
            }
            break;
        case 't' :
            if ((target_host = strdup(optarg)) == NULL) {
                perror("strdup");
                return 1;
            }
            break;
        default :
            usage(argv[0]);
        }
    }
    if (source_host == NULL || target_host == NULL) {
        usage(argv[0]);
    }
    fill(msg, &len);
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_addr = NULL;
    if ((fodder = getaddrinfo(source_host, NULL, &hints, &res)) != 0) {
        fprintf(stderr, "getaddrinfo : %s\n", gai_strerror(fodder));
        return 1;
    }    
    if ((kindy = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
        perror("socket");
        return 1;
    }
    if (bind(kindy, (struct sockaddr *) res->ai_addr,
             res->ai_addrlen) != 0) {
        perror("bind");
        return 1;
    }
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_addr = NULL;
    if ((fodder = getaddrinfo(target_host, "domain", &hints, &res)) != 0) {
        fprintf(stderr, "getaddrinfo : %s\n", gai_strerror(fodder));
        return 1;
    }
    if (sendto(kindy, msg, len, 0, (const struct sockaddr *) res->ai_addr,
               (socklen_t) res->ai_addrlen) <= (ssize_t) 0) {
        perror("sendto");
        return 1;
    }
    (void) close(kindy);
    
    return 0;
}
Beispiel #29
0
int main(int argc, char *argv[])
{
    double *old, *current, *next;
    int t_max, i_max, num_threads;
    double time;

    /* Parse commandline args: i_max t_max num_threads */
    if (argc < 4) {
        printf("Usage: %s i_max t_max num_threads [initial_data]\n", argv[0]);
        printf(" - i_max: number of discrete amplitude points, should be >2\n");
        printf(" - t_max: number of discrete timesteps, should be >=1\n");
        printf(" - num_threads: number of threads to use for simulation, "
                "should be >=1\n");
        printf(" - initial_data: select what data should be used for the first "
                "two generation.\n");
        printf("   Available options are:\n");
        printf("    * sin: one period of the sinus function at the start.\n");
        printf("    * sinfull: entire data is filled with the sinus.\n");
        printf("    * gauss: a single gauss-function at the start.\n");
        printf("    * file <2 filenames>: allows you to specify a file with on "
                "each line a float for both generations.\n");

        return EXIT_FAILURE;
    }

    i_max = atoi(argv[1]);
    t_max = atoi(argv[2]);
    num_threads = atoi(argv[3]);

    if (i_max < 3) {
        printf("argument error: i_max should be >2.\n");
        return EXIT_FAILURE;
    }
    if (t_max < 1) {
        printf("argument error: t_max should be >=1.\n");
        return EXIT_FAILURE;
    }
    if (num_threads < 1) {
        printf("argument error: num_threads should be >=1.\n");
        return EXIT_FAILURE;
    }

    /* Allocate and initialize buffers. */
    old = malloc(i_max * sizeof(double));
    current = malloc(i_max * sizeof(double));
    next = malloc(i_max * sizeof(double));

    if (old == NULL || current == NULL || next == NULL) {
        fprintf(stderr, "Could not allocate enough memory, aborting.\n");
        return EXIT_FAILURE;
    }

    memset(old, 0, i_max * sizeof(double));
    memset(current, 0, i_max * sizeof(double));
    memset(next, 0, i_max * sizeof(double));

    /* How should we will our first two generations? */
    if (argc > 4) {
        if (strcmp(argv[4], "sin") == 0) {
            fill(old, 1, i_max/4, 0, 2*3.14, sin);
            fill(current, 2, i_max/4, 0, 2*3.14, sin);
        } else if (strcmp(argv[4], "sinfull") == 0) {
            fill(old, 1, i_max-2, 0, 10*3.14, sin);
            fill(current, 2, i_max-3, 0, 10*3.14, sin);
        } else if (strcmp(argv[4], "gauss") == 0) {
            fill(old, 1, i_max/4, -3, 3, gauss);
            fill(current, 2, i_max/4, -3, 3, gauss);
        } else if (strcmp(argv[4], "file") == 0) {
            if (argc < 7) {
                printf("No files specified!\n");
                return EXIT_FAILURE;
            }
            file_read_double_array(argv[5], old, i_max);
            file_read_double_array(argv[6], current, i_max);
        } else {
            printf("Unknown initial mode: %s.\n", argv[4]);
            return EXIT_FAILURE;
        }
    } else {
        /* Default to sinus. */
        fill(old, 1, i_max/4, 0, 2*3.14, sin);
        fill(current, 2, i_max/4, 0, 2*3.14, sin);
    }

    timer_start();

    /* Call the actual simulation that should be implemented in simulate.c. */
    simulate(i_max, t_max, num_threads, old, current, next);

    time = timer_end();
    printf("Took %g seconds\n", time);
    printf("Normalized: %g seconds\n", time / (i_max * t_max));

    file_write_double_array("result.txt", current, i_max);

    free(old);
    free(current);
    free(next);

    return EXIT_SUCCESS;
}
Beispiel #30
0
 void ensure(int num) {
     if (end - start < num) {
         fill();
     }
 }