Beispiel #1
0
static struct table *
init() {
	struct table * t = stable_create();
	stable_settable(t, TKEY("number"));
	stable_settable(t, TKEY("string"));
	stable_setnumber(t,TKEY("count"),0);

	return t;
}
int main(int argc, const char** argv)
{
	double alfa = 2;
	double beta = 0.7;
	size_t batches[] = { 100, 1000, 10000, 1000000 };
	size_t num_batches = sizeof(batches) / sizeof(size_t);
	double* cpu_rands = NULL;
	double* gpu_rands = NULL;
	double start, end, tdiff;
	size_t i, batch_size;

	StableDist *dist = stable_create(alfa, beta, 1, 0, 0);

	if (!dist) {
		fprintf(stderr, "StableDist creation failure. Aborting.\n");
		return 1;
	}

	if (stable_activate_gpu(dist)) {
		fprintf(stderr, "Couldn't initialize GPU.\n");
		return 1;
	}

	stable_set_absTOL(1e-20);
	stable_set_relTOL(1.2e-10);
	stable_rnd_seed(dist, time(NULL));

	fprintf(stderr, "Count\tCPU-ms\tGPU-ms\n");

	for (i = 0; i < num_batches; i++) {
		batch_size = batches[i];
		cpu_rands = calloc(batch_size, sizeof(double));
		gpu_rands = calloc(batch_size, sizeof(double));

		start = get_ms_time();
		stable_rnd(dist, cpu_rands, batch_size);
		end = get_ms_time();
		tdiff = end - start;

		fprintf(stderr, "%zu\t%.3lf\t", batch_size, tdiff);

		start = get_ms_time();
		stable_rnd_gpu(dist, gpu_rands, batch_size);
		end = get_ms_time();
		tdiff = end - start;

		fprintf(stderr, "%.3lf\n", tdiff);
	}

	for (i = 0; i < batch_size; i++)
		printf("%.6lf\n", gpu_rands[i]);

	stable_free(dist);
	return 0;
}
int main(int argc, char *argv[])
{
	double alfa, beta, sigma, mu_0;
	double *data;
	int N, Nexp;
	int seed;
	double total_duration, start, end;
	double *pdf;
	double ms_duration;

	StableDist *dist = NULL;

	alfa = 1.5;
	beta = 0.75;
	sigma = 5.0;
	mu_0 = 15.0;
	N = 400;
	Nexp = 1;
	seed = -1;

	printf("Parameters for the random data generated:\n");
	printf("α\t%lf\n", alfa);
	printf("β\t%lf\n", beta);
	printf("σ\t%lf\n", sigma);
	printf("μ\t%lf\n", mu_0);
	printf("Size\t%d\n", N);
	printf("\nWill perform %d experiments.\n\n", Nexp);

	if ((dist = stable_create(alfa, beta, sigma, mu_0, 0)) == NULL) {
		printf("Error when creating the distribution");
		exit(1);
	}

	stable_set_THREADS(1);
	stable_set_absTOL(1e-16);
	stable_set_relTOL(1e-8);
	stable_set_FLOG("errlog.txt");

	if (seed < 0)
		stable_rnd_seed(dist, time(NULL));
	else
		stable_rnd_seed(dist, seed);

	/* Random sample generation */
	data = (double *)malloc(N * sizeof(double));
	pdf = (double *) calloc(N, sizeof(double));

	stable_rnd(dist, data, N);

	total_duration = 0;

	stable_fit_init(dist, data, N, NULL, NULL);

	stable_activate_gpu(dist);

	start = get_ms_time();
	stable_fit_grid(dist, data, N);
	end = get_ms_time();

	ms_duration = end - start;


	printf("time = %lf\nα = %lf\nβ = %.2lf\nμ = %.2lf\nσ = %.2lf\n",
		   ms_duration, dist->alfa, dist->beta, dist->sigma, dist->mu_0);

	free(data);
	stable_free(dist);

	fclose(stable_get_FLOG());

	return 0;
}
int main(int argc, const char** argv)
{
	double alfas[] = { 0.25, 0.5, 0.75, 1.25, 1.5 };
	double betas[] = { 0, 0.5, 1 };
	double intervals[] = { -10, 10 };
	int points_per_interval = 20;
	double cdf_vals[points_per_interval];
	double guesses[points_per_interval];
	short use_all_gpu = 1;

	stable_clinteg_printinfo();

	StableDist *dist = stable_create(0.5, 0, 1, 0, 0);

	if (!dist) {
		fprintf(stderr, "StableDist creation failure. Aborting.\n");
		return 1;
	}

	if (stable_activate_gpu(dist)) {
		fprintf(stderr, "Couldn't initialize GPU.\n");
		return 1;
	}

	if (argc > 1)
		use_all_gpu = 0;

	stable_set_absTOL(1e-20);
	stable_set_relTOL(1.2e-10);

	size_t ai, bi, i, j;
	double points[points_per_interval];
	size_t interval_count = (sizeof(intervals) / sizeof(double)) - 1;
	size_t alfa_count = sizeof(alfas) / sizeof(double);
	size_t beta_count = sizeof(betas) / sizeof(double);
	double total_relerr = 0, total_abserr = 0;
	size_t valid_points;

	double abs_diff_sum, rel_diff_sum, gpu_err_sum, cpu_err_sum;

	for (i = 0; i < interval_count; i++) {
		double begin = intervals[i];
		double end = intervals[i + 1];
		double step = (end - begin) / points_per_interval;

		printf("\n=== Interval (%.0lf, %.0lf)\n", begin, end);
		printf("alfa  beta   abserr  relerr\n");

		for (j = 0; j < points_per_interval; j++)
			points[j] = j * step + begin;

		for (ai = 0; ai < alfa_count; ai++) {
			for (bi = 0; bi < beta_count; bi++) {
				stable_setparams(dist, alfas[ai], betas[bi], 1, 0, 0);
				stable_cdf_gpu(dist, points, points_per_interval, cdf_vals, NULL);

				abs_diff_sum = 0;
				rel_diff_sum = 0;
				gpu_err_sum = 0;
				cpu_err_sum = 0;
				valid_points = 0;

				if (use_all_gpu)
					stable_inv_gpu(dist, cdf_vals, points_per_interval, guesses, NULL);

				for (j = 0; j < points_per_interval; j++) {
					if (cdf_vals[j] >= 0.1 && cdf_vals[j] <= 0.9) {
						double guess;

						if (use_all_gpu)
							guess = guesses[j];
						else
							guess = stable_inv_point_gpu(dist, cdf_vals[j], NULL);

						if (isnan(guess))
							continue;

						double diff = fabs(guess - points[j]);

						abs_diff_sum += diff;

						if (points[j] != 0)
							rel_diff_sum += diff / points[j];

						valid_points++;
					}
				}

				total_relerr += rel_diff_sum;
				total_abserr += abs_diff_sum;

				if (valid_points != 0) {
					abs_diff_sum /= valid_points;
					rel_diff_sum /= valid_points;
				}

				printf("%.3lf %.3lf  %8.3g %8.3g\n",
					   alfas[ai], betas[bi],
					   abs_diff_sum, rel_diff_sum);
			}
		}
	}


	stable_free(dist);
	return 0;
}
Beispiel #5
0
RpcService *rpc_offer(char *svcName) {
    SRecord *s = stable_create(svcName);
    return (RpcService) s;
}
Beispiel #6
0
int main(int argc, char *argv[])
{
	double alfa, beta, sigma, mu_0;
	double alfa_init = 0, beta_init = 0, sigma_init = 0, mu_0_init = 0;
	double *data;
	int i = 1, iexp, N, Nexp;
	int seed;
	double acc_pdev;
	double total_duration, start, end;
	struct fittest tests[] = {
		//{ stable_fit_mle, 0, "MLE" },
		//{ stable_fit_mle2d, 0, "M2D"},
		//{ stable_fit_koutrouvelis, 0, "KTR"},
		{ stable_fit_mle, 1, "MLE" },
		{ stable_fit_mle2d, 1, "M2D"},
		{ stable_fit_koutrouvelis, 1, "KTR"},
		{ stable_fit_grid, 1, "GRD" },
		{ stable_fit_grid, 0, "GRD" }
	};
	struct fittest *test;
	size_t num_tests = sizeof tests / sizeof(struct fittest);
	struct fitresult* results;
	struct fitresult* result;

	results = calloc(num_tests, sizeof(struct fitresult));

	StableDist *dist = NULL;

	alfa = 1.5;
	beta = 0.75;
	sigma = 5.0;
	mu_0 = 15.0;
	N = 400;
	Nexp = 10;
	seed = -1;

	printf("Parameters for the random data generated:\n");
	printf("α\t%lf\n", alfa);
	printf("β\t%lf\n", beta);
	printf("σ\t%lf\n", sigma);
	printf("μ\t%lf\n", mu_0);
	printf("Size\t%d\n", N);
	printf("\nWill perform %d experiments for each fitter.\n\n", Nexp);

	if ((dist = stable_create(alfa, beta, sigma, mu_0, 0)) == NULL) {
		printf("Error when creating the distribution");
		exit(1);
	}

	stable_set_THREADS(1);
	stable_set_absTOL(1e-16);
	stable_set_relTOL(1e-8);
	stable_set_FLOG("errlog.txt");

	if (seed < 0)
		stable_rnd_seed(dist, time(NULL));
	else
		stable_rnd_seed(dist, seed);

	/* Random sample generation */
	data = (double *)malloc(N * sizeof(double));

	stable_rnd(dist, data, N);

	printf("Fitter\tms/fit\t\tα\t\tβ\t\tμ\t\tσ\n");

	for (i = 0; i < num_tests; i++) {
		test = tests + i;
		result = results + i;
		total_duration = 0;

		for (iexp = 0; iexp < Nexp; iexp++) {
			stable_fit_init(dist, data, N, NULL, NULL);

			add_initial_estimations(alfa);
			add_initial_estimations(beta);
			add_initial_estimations(sigma);
			add_initial_estimations(mu_0);

			if (test->gpu_enabled)
				stable_activate_gpu(dist);
			else
				stable_deactivate_gpu(dist);

			dist->parallel_gridfit = test->gpu_enabled; // Temporary.

			start = get_ms_time();
			test->func(dist, data, N);
			end = get_ms_time();

			add_avg_err(alfa);
			add_avg_err(beta);
			add_avg_err(sigma);
			add_avg_err(mu_0);

			result->ms_duration += end - start;
		}

		calc_avg_err(alfa);
		calc_avg_err(beta);
		calc_avg_err(sigma);
		calc_avg_err(mu_0);
		result->ms_duration /= Nexp;

		printf("%s", test->name);

		if (test->gpu_enabled)
			printf("_GPU");

		printf("\t%lf\t%.2lf ± %.2lf\t%.2lf ± %.2lf\t%.2lf ± %.2lf\t%.2lf ± %.2lf\n",
			   result->ms_duration,
			   result->alfa, result->alfa_err,
			   result->beta, result->beta_err,
			   result->sigma, result->sigma_err,
			   result->mu_0, result->mu_0_err);
	}

	alfa_init /= Nexp * num_tests;
	beta_init /= Nexp * num_tests;
	sigma_init /= Nexp * num_tests;
	mu_0_init /= Nexp * num_tests;

	printf("\n\nInitial estimations: \n");
	printf("α = %lf\nβ = %.2lf\nμ = %.2lf\nσ = %.2lf\n",
		   alfa_init, beta_init, sigma_init, mu_0_init);

	printf("\n\nComparison of actual vs. expected results:\n");
	printf("Fitter\tα error\t\tβ error\t\tμ error\t\tσ error\t\tAverage %% error\n");

	for (i = 0; i < num_tests; i++) {
		result = results + i;
		test = tests + i;
		acc_pdev = 0;

		printf("%s", test->name);

		if (test->gpu_enabled)
			printf("_GPU");

		print_deviation(alfa);
		print_deviation(beta);
		print_deviation(mu_0);
		print_deviation(sigma);

		printf("\t%.1lf %%\n", acc_pdev / 4);
	}


	free(data);
	free(results);
	stable_free(dist);

	fclose(stable_get_FLOG());

	return 0;
}
int main(int argc, char *argv[])
{
	int n = 2, // 1 para pdf, 2 para cdf
		P = 0; // parametrizacion

	double gamma = 1,
		   delta = 0;

	int    THREADS = 16;
	double tol = 1.2e-14;
	double atol = 1e-50;

	int Na = 14,
		Nb = 5,
		Nx = 5417;

	double  alfa[] = {0.1, 0.25, 0.5, 0.75, 0.9, 0.99, 1, 1.01, 1.1, 1.25, 1.5, 1.75, 1.95, 2.0};
	double  beta[] = {0.0, 0.25, 0.5, 0.75, 1.0};
	double * x = (double*)malloc(Nx * sizeof(double));
	double x0[] = { -1000,  -100,      -10,          -1,        1,    10, 100};
	double Ndiv = 7, Nxdiv[] = {900,   720,      576,        1024,      576,   720, 901};
	unsigned int Nx0[]   = {  0,   900,     1620,        2196,     3220,  3796, 4516};
	double xD[] = {     1, 0.125, 0.015625, 0.001953125, 0.015625, 0.125,   1};

	double *pdf, *err;
	StableDist *dist = NULL;
	int i = 1, j, k;
	void(*func)(StableDist *, const double *, const int,
				double *, double *);
	struct timeval t_1, t_2/*,tp_1,tp_2*/;
	double t, tpdf;

	char name[256], nameerr[256]/*,nametiempos[256]*/;
	FILE * f;
	FILE * ferr;
	//  FILE * ftiempos;
	FILE * flog;
	FILE * finteg;

	if (argc != 3) {
		printf("Uso: stable_precision FUNCION RELTOL\n");
		exit(1);
	} else {
		tol = atof(argv[2]);
		n   = atoi(argv[1]);
	}

	for (i = 0; i < Ndiv; i++) {
		for (j = 0; j < Nxdiv[i]; j++)
			x[Nx0[i] + j] = x0[i] + xD[i] * j;
	}

	/*
	  for(i=0;i<Nx;i++)
	   {
	    printf("%1.9lf",x[i]);
	    if ((j+1)%10) printf("\t");
	    else printf("\n");
	   }

	  getchar();
	*/
	pdf = (double*)malloc(Nx * Nb * sizeof(double));
	err = (double*)malloc(Nx * Nb * sizeof(double));

	if (n == 1) {
		func = &stable_pdf;
		sprintf(name, "stable_precision_pdf_%1.1ea_%1.1er.txt", atol, tol);
		sprintf(nameerr, "stable_precision_pdf_err_%1.1ea_%1.1er.txt", atol, tol);
	} else if (n == 2) {
		func = &stable_cdf;
		sprintf(name, "stable_precision_cdf_%1.1ea_%1.1er.txt", atol, tol);
		sprintf(nameerr, "stable_precision_cdf_err_%1.1ea_%1.1er.txt", atol, tol);
	} else {
		printf("ERROR");
		exit(2);
	}

	if ((f = fopen(name, "wt")) == NULL) {
		printf("Error al crear archivo.");
		exit(2);
	}

	if ((ferr = fopen(nameerr, "wt")) == NULL) {
		printf("Error al crear archivo.");
		exit(2);
	}

	// Creacion de una distribucion estable
	if ((dist = stable_create(alfa[0], beta[0], gamma, delta, P)) == NULL) {
		printf("Error en la creacion de la distribucion");
		exit(EXIT_FAILURE);
	}

	finteg = stable_set_FINTEG("data_integrando.txt");
	flog = stable_set_FLOG("errlog.txt");

	stable_set_THREADS(THREADS);
	stable_set_relTOL(tol);
	stable_set_absTOL(atol);
	stable_set_METHOD(STABLE_QNG);
	stable_set_METHOD2(STABLE_QAG2);

	printf("FUNCTION=%d ALFAPOINTS=%d BETAPOINTS=%d XPOINTS=%d\n", n, Na, Nb, Nx);

	fprintf(f, "FUNCTION=%d ALFAPOINTS=%d BETAPOINTS=%d XPOINTS=%d\n", n, Na, Nb, Nx);
	fprintf(ferr, "FUNCTION=%d ALFAPOINTS=%d BETAPOINTS=%d XPOINTS=%d\n", n, Na, Nb, Nx);
	tpdf = 0;
	gettimeofday(&t_1, NULL);

	for (j = 0; j < Na; j++) {
		for (i = 0; i < Nb; i++) {
			stable_setparams(dist, alfa[j], beta[i], gamma, delta, P);
			//          gettimeofday(&tp_1,NULL);

			(func)(dist, (const double *)x, Nx, pdf + i * Nx, err + i * Nx);
			printf("        -> Alfa = %lf, Beta = %lf, %d points OK <-\n",
				   alfa[j], beta[i], Nx);
			//          gettimeofday(&tp_2,NULL);
			//          tpdf=tp_2.tv_sec-tp_1.tv_sec+(tp_2.tv_usec-tp_1.tv_usec)/1000000.0;
			//          fprintf(ftiempos,"%1.2lf %1.2lf %1.16lf %1.16lf\n",alfa[j],beta[i],tpdf,(double)Nx/tpdf);
			//          getchar();
		}

		//Volcado al archivo de texto
		fprintf(f, "\nAlfa = %lf\n________x\\beta________\t", alfa[j]);
		fprintf(ferr, "\nAlfa = %lf\n________x\\beta________\t", alfa[j]);

		for (i = 0; i < Nb; i++) {
			fprintf(f, "%1.16e\t", beta[i]);
			fprintf(ferr, "%1.16e\t", beta[i]);
		}

		fprintf(f, "\n");
		fprintf(ferr, "\n");

		for (k = 0; k < Nx; k++) {
			fprintf(f, "%1.16e\t", x[k]);
			fprintf(ferr, "%1.16e\t", x[k]);

			for (i = 0; i < Nb; i++) {
				fprintf(f, "%1.16e\t", *(pdf + i * Nx + k));
				fprintf(ferr, "%1.16e\t", *(err + i * Nx + k));
			}

			fprintf(f, "\n");
			fprintf(ferr, "\n");
		}

	}

	gettimeofday(&t_2, NULL);
	t = t_2.tv_sec - t_1.tv_sec + (t_2.tv_usec - t_1.tv_usec) / 1000000.0;

	printf("\n-----> Calculos completados en %3.3lf segundos <-----\n", t);

	fclose(f);
	fclose(ferr);
	//  fclose(ftiempos);
	fclose(finteg);
	fclose(flog);

	return 0;
}
Beispiel #8
0
int main(int argc, char *argv[])
{
	double alfa, beta, sigma, mu_0;
	double *data;
	int i = 1, iexp, N, Nexp;
	int seed;
	char testname[100];
	size_t test_count = 0;
	double total_duration, start, end;
	struct fittest tests[] = {
		//{ stable_fit_mle, 0, "MLE" },
		//{ stable_fit_mle2d, 0, "M2D"},
		//{ stable_fit_koutrouvelis, 0, "KTR"},
		//{ stable_fit_koutrouvelis, 1, "KTR"},
		//{ stable_fit_mle, 1, "MLE" },
		// { stable_fit_mle2d, 1, "M2D"},
		{ stable_fit_grid, 1, "GRD" },
		// { stable_fit_grid, 0, "GRD" }
	};
	struct fittest *test;
	size_t num_tests = sizeof tests / sizeof(struct fittest);

	StableDist *dist = NULL;

	alfa = 1.5;
	beta = 0.75;
	sigma = 5.0;
	mu_0 = 15.0;
	N = 1000;
	Nexp = 20;
	seed = -1;

	install_stop_handlers();

	if ((dist = stable_create(alfa, beta, sigma, mu_0, 0)) == NULL) {
		printf("Error when creating the distribution");
		exit(1);
	}

	stable_set_THREADS(1);
	stable_set_absTOL(1e-16);
	stable_set_relTOL(1e-8);

	if (seed < 0)
		stable_rnd_seed(dist, time(NULL));
	else
		stable_rnd_seed(dist, seed);

	/* Random sample generation */
	data = (double *) malloc(Nexp * N * sizeof(double));

	for (i = 0; i < num_tests; i++) {
		test = tests + i;
		total_duration = 0;

		char out_fname[100];
		char* gpu_marker;

		if (test->gpu_enabled)
			gpu_marker = "_GPU";
		else
			gpu_marker = "";


		snprintf(out_fname, 100, "%s%s.dat", test->name, gpu_marker);
		snprintf(testname, 100, "%s%s", test->name, gpu_marker);

		FILE* out = fopen(out_fname, "w");

		if (!out) {
			perror("fopen");
			return 1;
		}

		printf("Estimation evaluation for %s...\n", testname);

		if (test->gpu_enabled)
			stable_activate_gpu(dist);
		else
			stable_deactivate_gpu(dist);

		for (alfa = ALFA_START; alfa <= ALFA_END + 2 * DBL_EPSILON; alfa += ALPHA_INCR) {
			for (beta = BETA_START; beta <= BETA_END + 2 * DBL_EPSILON; beta += BETA_INCR) {
				mu_0 = 0;
				sigma = 1;

				for (mu_0 = MU_START; mu_0 <= MU_END + 2 * DBL_EPSILON; mu_0 += MU_INCR) {
					for (sigma = SIGMA_START; sigma <= SIGMA_END + 2 * DBL_EPSILON; sigma += SIGMA_INCR) {
						double alfa_est = 0, beta_est = 0, mu_0_est = 0, sigma_est = 0;
						double alfa_est_err = 0, beta_est_err = 0, mu_0_est_err = 0, sigma_est_err = 0;
						stable_setparams(dist, alfa, beta, sigma, mu_0, 0);

						printf("Testing %s %.2lf/%.2lf/%.2lf/%.2lf\n", testname, alfa, beta, mu_0, sigma);

						stable_rnd(dist, data, N * Nexp);

						double ms_duration = 0;

						dist->parallel_gridfit = test->gpu_enabled; // Temporary.

						for (iexp = 0; iexp < Nexp; iexp++) {
							if (stable_fit_init(dist, data + iexp * N, N, NULL, NULL) != 0) {
								printf("Warning: couldn't init distribution\n");
								continue;
							}

							printf("Eval %d... ", iexp);
							fflush(stdout);

							start = get_ms_time();
							test->func(dist, data + iexp * N, N);
							end = get_ms_time();

							add_avg_err(alfa);
							add_avg_err(beta);
							add_avg_err(sigma);
							add_avg_err(mu_0);

							ms_duration += end - start;
							printf("Done\n");
							fflush(stdout);
						}

						calc_avg_err(alfa);
						calc_avg_err(beta);
						calc_avg_err(sigma);
						calc_avg_err(mu_0);

						ms_duration /= Nexp;

						fprintf(out, "%lf %lf %lf %lf %lf ", alfa, beta, mu_0, sigma, ms_duration);
						fprintf(out, "%lf %lf %lf %lf %lf %lf %lf %lf\n",
								alfa_est, alfa_est_err,
								beta_est, beta_est_err,
								sigma_est, sigma_est_err,
								mu_0_est, mu_0_est_err);

						fflush(out);
						fflush(stdout);

						test_count++;
					}

				}
			}
		}

		fclose(out);
		printf("Eval finished: %zu sample points.\n", test_count);
		test_count = 0;
	}

	printf("Done\n");


	free(data);
	stable_free(dist);

	return 0;
}