示例#1
0
void ml_populateFromMan(ManList** list, char* entry) {
	/* There *has* to be a better way of doing this... */

	/* First, build the command we're going to run */
	char* prefix = "man -aw '";
	char* postfix ="' 2>&1";
	
	char* command = NULL;
	unsigned int commandLen = strlen(entry) + strlen(prefix) + strlen(postfix);
	command = malloc(commandLen + 1);
	if (!command) {
		return;
	}

	*command = '\0';
	strncat(command, prefix, commandLen);
	strncat(command, entry, commandLen);
	strncat(command, postfix, commandLen);

	/* Clear the list */
	ml_free(list);

	/* Now, we can run the command. */
	FILE* pipe = popen(command, "r");
	/* If paths to man pages are > 256bytes long, you REALLY need to
	   reconsider your directory structure choices. */
	char filename[256];
	while (fgets(filename, 256, pipe) != NULL) {
		filename[strcspn(filename, "\n")] = '\0';
		if (*filename == '/') {
			ml_append(list, filename);
		}
	}
	pclose(pipe);
}
示例#2
0
void hdaf_filter_kernel ( complex<double > *& fft_ker, double L, int n, int m, double k )
{
    double * kernel = 0;
    int w = 0;
    
    hdaf_filter_kernel ( kernel, w, L, n, m, k );
    
    double * p = &(kernel[w]);
    double * ker_ext = 0;
    
    kernel_extension ( ker_ext, n, p, w, w );
    
    fft( ker_ext, fft_ker, n );
    
    ml_free (ker_ext);
    ml_free (kernel);
}
char *webspy_nodeview(httpd *server) {

	char *tmp, *nodelist;
	int i = 0, max, offset = 1;
	struct ONLINE clone, *tmpn;
	online_t *who;
	strlist_t *nlist = 0;
	time_t now;
	struct macro_list *ml = 0;
	strlist_iterator_t *si;

	who = (online_t*) ht_get(get_context(), ONLINE_ATTR);
	max = who_online_max(who);
	now = time(0);

	for (i = 0; i < max; i++) {

		tmpn = who_getnode(who, i);

		if (!tmpn)
			continue;

		memcpy(&clone, tmpn, sizeof(struct ONLINE));

		if (clone.procid == 0)
			continue;
		
		tmp = webspy_nodeview_item(&clone, offset++, now);

		nlist = str_add_last(nlist, tmp);

		free(tmp);
	}

	// concat list
	max = 0;
	si = str_iterator(nlist);
	while (str_iterator_hasnext(si))
		max += strlen(str_iterator_next(si)) + 1;
	free(si);

	tmp = malloc(max + 1);
	*tmp = 0;
	si = str_iterator(nlist);
	while (str_iterator_hasnext(si))
		strcat(tmp, str_iterator_next(si));

	free(si);
	ml = ml_addstring(ml, NODEVIEW_NODEVIEWITEMS, tmp);

	free(tmp);
	tmp = ml_replacebuf(ml, ht_get(get_config(), PROPERTY_HTML_NODEVIEW));

	ml_free(ml);

	return tmp;
}
示例#4
0
void ml_free( T** &p, int m )
{
	if (p)
    {
        for (int k=0; k<m; k++)
            ml_free ( p[k] );
    }
    delete [] p;
	p=0;
}
示例#5
0
void ml_free(struct macro_list *l) {
    if (l) {
        ml_free(l->next);

        if ((l->mac_type != ML_CHAR) && l->mac_rep)
            free(l->mac_rep);
        if (l->mac_key)
            free(l->mac_key);
    }
}
示例#6
0
void ml_free(ManList** list) {
	if (*list == NULL) {
		return;
	}

	if((*list)->next) {
		ml_free(&(*list)->next);
	}

	free((*list)->page);
	free(*list);
	*list = NULL;
}
char *webspy_nodeview_item(struct ONLINE *o, int pos, time_t now) {
	struct macro_list *ml = 0;
	char tbuf[1024], agebuf[30], *last, tfile[1024], *tmp;
	int rc;
	double speed;

	ml = ml_addint(ml, NODEVIEWITEM_POS, pos);
	ml = ml_addint(ml, NODEVIEWITEM_PID, o->procid);
	ml = ml_addstring(ml, NODEVIEWITEM_USERNAME, o->username);

	spy_makeage(o->tstart.tv_sec, now, agebuf);
	ml = ml_addstring(ml, NODEVIEWITEM_IDLE, agebuf);
					  
	sprintf(tbuf, o->status);
	last = (char*)&tbuf;
	while (*last)
		if ((*last == '\r') || (*last == '\n'))
			*last = 0;
		else
			last++;

	rc = who_transfer_direction(o);

	if (rc != TRANS_NONE) {

		who_transfer_file(o, tfile);
		speed = who_transfer_speed(o);
		last = strrchr(tfile, '/');
		if (last)
			last++;
		else
			last = (char*)&tfile;

		sprintf(tbuf, "%s: %s %5dk %.1fks", (rc == TRANS_UP)?"UL":"DL", last, o->bytes_xfer/1024, speed);

	}

	ml = ml_addstring(ml, NODEVIEWITEM_ACTION, tbuf);

	tmp = ml_replacebuf(ml, ht_get(get_config(), PROPERTY_HTML_NODEVIEW_ITEM));

	ml_free(ml);

	return tmp;
}
示例#8
0
void hdaf_filter_kernel ( double *& kernel, int & w, double L, int n, int m, double k )
{
    mp_real sigma = sqrt( mp_2*m+ mp_1)/k;
    
    ml_poly<mp_real > P;
    make_hdaf_ml_poly(P,m );
    
    w = (int)ceil(hdaf_truncate_point (1E-16, 4E-16, m, dble(sigma), 0 )/(L/n));
    
    if (kernel != 0) ml_free( kernel );
    kernel = ml_alloc<double > (2*w+1);
    double * p = &(kernel[w]);
    
    mp_real h = ((mp_real)L)/n;
    mp_real s = h/(mp_sqrt2*sigma);
    mp_real ss = s*s;
    mp_real f = pow(mp_sqrt2*sigma,-1)*(h/n);
    
    for (int k=-w; k<=w; k++ )
        p[k] = dble( exp(-ss*(k*k)) *P( dble(s*k) ) *f );
    
}
示例#9
0
int main()
{
	std_setup();
    
    int n_steps = 500;
    int n_runs = 1000;
    
    double X0 = 0.0;
    double stop_time = 2.0;
    
    double dt = stop_time/n_steps;
    
    double * time = ml_alloc<double > ( n_steps );
    for (int k=0; k<n_steps; k++ )
        time[k] = dt*k;
    
    double **W=0, *X_true=0;
    double **X = ml_alloc<double> ( n_runs, n_steps );
    double * X_mean = ml_alloc<double> (n_steps);
    
    double **M = ml_alloc<double> ( n_runs, n_steps );
    
    gen_BM( dt, n_steps, W, n_runs, BM_mode_2 );
    
    for ( int run=0; run<n_runs; run++ )
    for ( int k=0; k<n_steps-1; k++ )
    {
        double sum1 = 0;
        for (int j=0; j<=k; j++)
            sum1 += drift(dt*j)*drift(dt*j)*dt;
        
        double sum2 = 0;
        for (int j=0; j<=k; j++)
            sum2 += drift(dt*j)*(W[run][j+1]-W[run][j]);
        
        M[run][k] = exp(-sum2 -0.5*sum1);
    }
    
    for ( int run=0; run<n_runs; run++ )
    {
        X[run][0] = X0;
			
        for ( int j=1; j<n_steps; j++ )
        {
			X[run][j] = X[run][j-1] + ( drift(dt*j) + drift(dt*(j-1)) )*dt/2 + volatility(X[run][j-1], dt*(j-1))*(W[run][j]-W[run][j-1]);
        }
    }
    
    for ( int step=0; step<n_steps; step++ )
        X_mean[step] = 0;
    
    for ( int run=0; run<n_runs; run++ )
    for ( int step=0; step<n_steps; step++ )
        X_mean[step] += X[run][step]/n_runs;
    
    output( X, max(n_runs,min(100,n_runs)), n_steps, "/workspace/output/SDE/test/X" );
    output( X_mean, n_steps, "/workspace/output/SDE/test/X_mean" );
    output( time, n_steps, "/workspace/output/SDE/test/time" );
    output( M, max(n_runs,min(100,n_runs)), n_steps, "/workspace/output/SDE/test/M" );
    
    for ( int run=0; run<n_runs; run++ )
    for ( int step=0; step<n_steps; step++ )
        X[run][step] *= M[run][step];
    
    for ( int step=0; step<n_steps; step++ )
        X_mean[step] = 0;
    
    for ( int run=0; run<n_runs; run++ )
    for ( int step=0; step<n_steps; step++ )
        X_mean[step] += X[run][step]/n_runs;
    
    
    output( X, max(n_runs,min(100,n_runs)), n_steps, "/workspace/output/SDE/test/XM" );
    output( X_mean, n_steps, "/workspace/output/SDE/test/QX" );
    
    ml_free( X, n_runs );
    ml_free( X_mean );
    ml_free( W, n_runs );
    ml_free( time );
    ml_free( X_mean );
    
    std_exit();
}
示例#10
0
int main()
{
    std_setup();
    
    int n_runs = 1E5;
    int n_steps = 30;
    int **M=0;
    
    walk_test(M,n_steps,n_runs);
    
    for ( int k=1; k<n_steps; k++ )
        cout << k << "\t" << M[k][0]  << "\t" << M[k][n_runs-1] << endl;
        
    
    for ( int k=1; k<n_steps; k++ )
    {        
        int max_ = max(M[k], n_runs );
        int min_ = min(M[k], n_runs );
        int n = abs(max_) + abs(min_) + 1;
        
        if ( max_>1 && min_<-1 )
        {
            int * c_ = ml_alloc<int > ( n );
            int *c = &c_[ -min_ ];
            
            for (int j=min_; j<=max_; j++)
                c[j] = 0;
            
            for (int j=0; j<n_runs; j++)
                c[ M[k][j] ] ++;
            
            float * p_ = ml_alloc<float > ( n );            
            float * p = &p_[ -min_ ];
            
            for (int j=min_; j<=max_; j++)
                p[j] = ((float)c[j])/n_runs;
            
            float mu = 0;
            
            for (int j=min_; j<=max_; j++)
                mu += p[k]*j;
            
            float sig2 = 0;
           
            for (int j=min_; j<=max_; j++)
                sig2 += p[k]*j*j;
            
            sig2 -= mu*mu;
            
            float sig = sqrt(sig2);
            
            clean_print(k); cout << "\t";
            clean_print(mu); cout << "\t";
            clean_print(sig); cout << "\t";
            clean_print(sig2); cout << endl;
            
            
            sprintf(fname, "/workspace/output/out_%d", k);
            output( p_ , n, fname );
            
            p=0;
            ml_free(p_);
            ml_free(c_);
        }
    }    
    
    
    
    
    std_exit();
}
示例#11
0
int main()
{
	std_setup();
    
    
    // setup:
    
    int n_steps = 500;
    int n_runs = 10000;
    
    double X0 = 0.0;
    double stop_time = 2.0;
    
    double dt = stop_time/n_steps;
    
    double * time = ml_alloc<double > ( n_steps );
    for (int k=0; k<n_steps; k++ )
        time[k] = dt*k;
    
    
    
    double **W=0, *X_true=0;
    double **X = ml_alloc<double> ( n_runs, n_steps );
    double * X_mean = ml_alloc<double> (n_steps);
    double **M = ml_alloc<double> ( n_runs, n_steps );
    
    
    
    // generate the BMs:
    
    gen_BM( dt, n_steps, W, n_runs, BM_mode_2 );
    
    
    // integrate the SDE
    
    for ( int run=0; run<n_runs; run++ )
    {
        X[run][0] = X0;
			
        for ( int j=1; j<n_steps; j++ )
        {
			X[run][j] = X[run][j-1] + ( drift(dt*j) + drift(dt*(j-1)) )*dt/2 + volatility(X[run][j-1], dt*(j-1))*(W[run][j]-W[run][j-1]);
        }
    }
    
    // compute the Girsanov change of measure at every t
    
    for ( int run=0; run<n_runs; run++ )
    for ( int k=0; k<n_steps-1; k++ )
    {
        double sum1 = 0;
        for (int j=0; j<=k; j++)
            sum1 += drift(dt*j)*drift(dt*j)*dt;
        
        double sum2 = 0;
        for (int j=0; j<=k; j++)
            sum2 += drift(dt*j)*(W[run][j+1]-W[run][j]);
        
        M[run][k] = exp(-sum2 -0.5*sum1);
    }
    
    // compute expectation of X, output results so far.
    
    for ( int step=0; step<n_steps; step++ )
        X_mean[step] = 0;
    
    for ( int run=0; run<n_runs; run++ )
    for ( int step=0; step<n_steps; step++ )
        X_mean[step] += X[run][step]/n_runs;
    
    output( X, max(n_runs,min(100,n_runs)), n_steps, "/workspace/output/SDE/test/X" );
    output( X_mean, n_steps, "/workspace/output/SDE/test/X_mean" );
    output( time, n_steps, "/workspace/output/SDE/test/time" );
    output( M, max(n_runs,min(100,n_runs)), n_steps, "/workspace/output/SDE/test/M" );
    
    
    // compute expectation of X, with respect to new measure, output results.
    
    for ( int run=0; run<n_runs; run++ )
    for ( int step=0; step<n_steps; step++ )
        X[run][step] *= M[run][step];
    
    for ( int step=0; step<n_steps; step++ )
        X_mean[step] = 0;
    
    for ( int run=0; run<n_runs; run++ )
    for ( int step=0; step<n_steps; step++ )
        X_mean[step] += X[run][step]/n_runs;
    
    
    output( X, max(n_runs,min(100,n_runs)), n_steps, "/workspace/output/SDE/test/XM" );
    output( X_mean, n_steps, "/workspace/output/SDE/test/QX" );
    
    
    
    
    
    
    ml_free( X, n_runs );
    ml_free( X_mean );
    ml_free( W, n_runs );
    ml_free( time );
    ml_free( X_mean );
    
    std_exit();
}
示例#12
0
void mp_free(MessagePool *mp) {
  hv_free(mp->buffer);
  for (int i = 0; i < MP_NUM_MESSAGE_LISTS; i++) {
    ml_free(&mp->lists[i]);
  }
}
示例#13
0
int main()
{
    std_setup();
    
    mp::mp_init(100);
    
    int n = pow(2,12);
    double a = 0.0;
    double b = ml_2pi;
    
    int m = 4;
    double gamma = 0.75;
    
    vector<double > frequency;
    vector<double > error;
    vector<double > measured_response;
    vector<double > response;
    vector<double > expected_error;
    
    for (int k=1; k<=n/2; k += max(n/100,1) )
    {
        double K = (ml_2pi/(b-a))*k;
        
        double * signal = ml_alloc<double > (n);
        
        for (int i=0; i<n; i++)
        {
            double x = ((b-a)*i)/n+a;
            signal[i] = cos( x*K );
        }
        
        complex<double > * fft_ker=0;
        hdaf_filter_kernel ( fft_ker, b-a, n, m, (ml_2pi/(b-a))*(gamma*n/2) );
        
        double * signal_filtered=0;
        complex<double > * workspace=0;
        
        fft( signal, workspace, n );
        
        for (int i=0; i<n/2+1; i++)
            workspace[i] *= fft_ker[i];
        
        ifft( workspace, signal_filtered, n );
        
        
        //---------------
        
        cout << K << "\t" << l2_error( signal, signal_filtered, n ) << endl;
        
        hdaf_delta_hat delta( m, sqrt(2*m+1)/( (ml_2pi/(b-a))*(gamma*n/2) ) );
        
        frequency.push_back( K );
        measured_response.push_back( rms(signal_filtered,n)/rms(signal,n ) );
        response.push_back ( delta(K) );
        
        expected_error.push_back ( log10( fabs(delta(K)-1.0)+1E-18 ) );
        error.push_back( log10(fabs(l2_error( signal, signal_filtered, n )) +1E-18) );
        
        ml_free(fft_ker);
        ml_free(signal);
    }
    
    
    sprintf( fname, "/workspace/output/temp/frequency" );
    output ( frequency,  fname );
    sprintf( fname, "/workspace/output/temp/measured_response" );
    output ( measured_response,  fname );
    sprintf( fname, "/workspace/output/temp/error" );
    output ( error,  fname );
    sprintf( fname, "/workspace/output/temp/expected_error" );
    output ( expected_error,  fname );
    sprintf( fname, "/workspace/output/temp/response" );
    output ( response,  fname );
    
    std_exit();
}
示例#14
0
void apply_hdaf_reg(
	int m,
	double sigma,
	int diff_order,
	double h,
	double eps_min,
	double eps_max,
    double * in,
	double * & out,
    int length,
    int in_stride,
    int out_stride)
{
	//if (out == 0) out = ml_alloc<double> (length*out_stride);
	
	static vector<int > m_list;
	static vector<double > sigma_list;
	static vector<int > diff_order_list;
	static vector<int > length_list;
	static vector<double > h_list;
	
	static vector<complex<double> * > kernel_fft_list;
	
	// not dealing with eps_range for now, fix later !!
	
	static int I = 0;
	bool found = 0;
	
	// look for existing parameter combination
	if (I)
	if (m_list[I] == m && sigma_list[I] == sigma && diff_order_list[I] == diff_order && length_list[I] == length && h_list[I] == h )
	    found = true;
	
	if (!found)
	for ( I = 0; I<m_list.size(); I++)
		if ( m_list[I] == m && sigma_list[I] == sigma && diff_order_list[I] == diff_order && length_list[I] == length && h_list[I] == h  )
		{
			found = true;
			break;
		}
	
	if (!found)
	{
		// new parameter combination
		
		I = m_list.size();
		
		m_list.push_back( m );
		sigma_list.push_back( sigma );
		diff_order_list.push_back( diff_order );
		length_list.push_back( length );
		h_list.push_back( h );
		
		complex<double > *kernel_fft = 0;		
		double * kernel = ml_alloc<double > (length);
		for (int k=0; k<length; k++)
			kernel[k] = 0.0;
		
		double x_max = hdaf_truncate_point (eps_min, eps_max, m, sigma, diff_order );
		int k_max = (int)ceil(x_max/h);
		if ( k_max >= length )
		{
			std::cout << "error: bad combination of hdaf parameters; truncate point exceeds data length in apply_hdaf_reg:\n";
            std::cout << "(eps1,eps2) = (" << eps_min << ", " << eps_max << "),\tm= " << m << ",\tsigma:" << sigma << ",\tdiff_order: " << diff_order << ",\tdata_length: " << length << ",\tx_max: " << x_max << ",\tk_max: " << k_max << std::endl;
			std_exit();
		}
		
		mp::mp_init(30);
		ml_poly<mp_real > P;
		make_hdaf_ml_poly( P, m );
		differentiate_hdaf_poly( P, diff_order );
        static mp_real sqrt2 = pow((mp_real)2.0,0.5);
     
        mp_real p = (pow(sqrt2*sigma,-diff_order)/(sqrt2*sigma))*h;
        
		for (int k=0; k<=k_max; k++)
        {
            mp_real r = (((mp_real)k)*h)/(sqrt2*sigma);
            kernel[k] = dble(exp(-r*r)*P(r)*p);
        }
        
		for (int k=1; k<=k_max; k++)
        {
            mp_real r = -(((mp_real)k)*h)/(sqrt2*sigma);        
            kernel[length-k] = dble(exp(-r*r)*P(r)*p);
        }
            
		FFT(kernel,kernel_fft, length, 1,1 );	
		kernel_fft_list.push_back(kernel_fft);
        
		ml_free( kernel );
	}
	
	// run
	complex<double> * q=0;
	complex<double> * ker_fft = kernel_fft_list[I];
	
	FFT(in, q, length, in_stride,1);
    
	for (int k=0; k<div_up(length,2); k++)
		q[k] *= ker_fft[k]/((double)length);
    
	IFFT(q,out,length,1,out_stride);
}