示例#1
0
文件: hilbert.c 项目: blynn/pbc
static void precision_init(int prec) {
  int i;
  mpf_t f0;

  mpf_set_default_prec(prec);
  mpf_init2(epsilon, 2);
  mpf_init2(negepsilon, 2);
  mpf_init(recipeulere);
  mpf_init(pi);
  mpf_init(eulere);

  mpf_set_ui(epsilon, 1);
  mpf_div_2exp(epsilon, epsilon, prec);
  mpf_neg(negepsilon, epsilon);

  mpf_init(f0);
  mpf_set_ui(eulere, 1);
  mpf_set_ui(f0, 1);
  for (i=1;; i++) {
    mpf_div_ui(f0, f0, i);
    if (mpf_cmp(f0, epsilon) < 0) {
      break;
    }
    mpf_add(eulere, eulere, f0);
  }
  mpf_clear(f0);

  mpf_ui_div(recipeulere, 1, eulere);

  compute_pi(prec);
}
示例#2
0
文件: main.c 项目: mimihalo/SIMDpi
int main()
{
	uint64_t begin,end;
	unsigned long long int dt;
    double pi,times;
	double rpi=acos(-1.0);
	double err;
	FILE *fp=fopen("result.txt","w");
	
	dt=0x100000;
	for(dt=0x100000;dt<=0x40000000;dt<<=1)
	{
		begin=clock();
		pi=compute_pi(dt);
		end=clock();
		times = (double)(end - begin) / (CLOCKS_PER_SEC/1000);
		err = (pi-rpi)/rpi;
		printf("dt = %lluM\n",(dt/(1<<20)));
		printf("pi = %.10f\n",pi);
		printf("time = %f\n",times);
		printf("error rate = %.10e\n",err);
		fprintf(fp,"%llu\t%.10f\t%f\t%.16f\n",(dt/(1<<20)),pi,times,err);
	}
	
	return 0;
}
int main(void)
{
    double time_elapsed[DT_SIZE];
    int ds;
    double pi;
    double start, finish;
    pthread_barrier_init(&barrier, NULL, (unsigned) THREAD_NUM);
    
    for (ds = 0; ds < DT_SIZE; ds++) {
        start = clock();
        compute_pi(ds*1000000);
        finish = clock();
        time_elapsed[ds] = (double)(finish-start)/CLOCKS_PER_SEC;
        pi = 0.0;
        //printf("pi= %lf\tdelta_size= %d\ttime= %lf\n", pi, ds, (double)(finish-start)/CLOCKS_PER_SEC);
    }
    
    FILE *fp;
    fp = fopen("baseline_pthread.txt", "w+");
    int i;
    fprintf(fp, "#delta_size\ttime\n");
    for (i = 0; i < DT_SIZE; i++) {
        fprintf(fp, "%d\t%lf\n", i, time_elapsed[i]);
    }
    fclose(fp);
    return 0;
}
示例#4
0
int main(int argc, char *argv[])
{
    struct timespec start, end;
    double cpu_time;
    
    double pi;
	
    assert(compute_pi(128*1024*1024) &&"Did you implement compute_pi()");
    
    clock_gettime(CLOCK_REALTIME, &start);
    pi=compute_pi((size_t)128*1024*1024);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time = diff_in_second(start, end);
    printf("pi: %lf\n",pi);
    printf("execution time of compute_pi() : %lf sec\n", cpu_time);

    return 0;
}
示例#5
0
int main(void)
{
	printf("pid: %d\n", getpid());
	clock_t start_time, end_time;
	double pi;
	start_time = clock();		
	pi = compute_pi(128000000);
	end_time = clock();
	printf("%f\nTime : %f\n", pi, (double)(end_time-start_time)/CLOCKS_PER_SEC);
	return 0;
}
示例#6
0
int main(int argc, char* argv[])
{
    int N = atoi(argv[1]);
    clock_t begin = clock();
    double result = compute_pi(N*1024*1024);
    clock_t end = clock();
    double time = (double)(end-begin)/CLOCKS_PER_SEC;

    printf("%lf \n",time);

    return 0;
}
示例#7
0
文件: SIMD.c 项目: giranntu/hw1Ext
int main(void)
{
	clock_t start_time, end_time;
	double pi;
	double total_time;
	start_time = clock();
	pi = compute_pi(128000000);
	end_time = clock();
	total_time =(double) (end_time-start_time)/CLOCKS_PER_SEC;
	printf("%f\nTime : %f\n", pi,total_time);

	return 0;
}
示例#8
0
文件: main.cpp 项目: keitee/kb
int main()
{
   std::random_device rd;
   auto seed_data = std::array<int, std::mt19937::state_size> {};
   std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd));
   std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
   auto eng = std::mt19937{ seq };
   auto dist = std::uniform_real_distribution<>{ 0, 1 };

   for (auto j = 0; j < 10; j++)
   {
      std::cout << compute_pi(eng, dist) << std::endl;
   }
}
示例#9
0
文件: kmp.c 项目: skysbird/alg
void kmp(char *t,char *p){
	int *pi;
	int m = strlen(p);
	pi = (int *)malloc(m*sizeof(int));	
	compute_pi(pi,p);
	
	int i = 0;
	int q = pi[1];
	int length_t = strlen(t);
	int length_p = m;
	
	for (i=0; i < length_t; ++i){
		while ( q > 0 && t[i] != p[q]){
			q = pi[q];
		}	
		if (t[i] == p[q]){
			q = q + 1;
		}
		if (q == length_p){
			printf("result is %d\n",(i-(length_p-1)));
			q = pi[q];
		}
	}
}
示例#10
0
int main(int argc, char* argv[])
{
	unsigned int operation = atoi(argv[1]);
	size_t n = atoi(argv[2]);

	clock_t begin, end;
	double time_spent[SAMPLE_SIZE];
	double min, max;

	double (*compute_pi)(size_t);
	char method_name[32];
	char time_filename[32];
	char error_filename[32];
	switch(operation) {
		case 0:
			compute_pi = &compute_pi_baseline;
			strcpy(method_name, "compute_pi_baseline");
			strcpy(time_filename, "time_baseline.txt");
			strcpy(error_filename, "error_baseline.txt");
			break;
		case 1:
			compute_pi = &compute_pi_avx;
			strcpy(method_name, "compute_pi_avx");
			strcpy(time_filename, "time_avx.txt");
			strcpy(error_filename, "error_avx.txt");
			break;
		case 2:
			compute_pi = &compute_pi_leibniz;
			strcpy(method_name, "compute_pi_leibniz");
			strcpy(time_filename, "time_leibniz.txt");
			strcpy(error_filename, "error_leibniz.txt");
			break;
		case 3:
			compute_pi = &compute_pi_leibniz_avx;
			strcpy(method_name, "compute_pi_leibniz_avx");
			strcpy(time_filename, "time_leibniz_avx.txt");
			strcpy(error_filename, "error_leibniz_avx.txt");
			break;
		case 4:
			compute_pi = &compute_pi_euler;
			strcpy(method_name, "compute_pi_euler");
			strcpy(time_filename, "time_euler.txt");
			strcpy(error_filename, "error_euler.txt");
			break;
		case 5:
			compute_pi = &compute_pi_euler_avx;
			strcpy(method_name, "compute_pi_euler_avx");
			strcpy(time_filename, "time_euler_avx.txt");
			strcpy(error_filename, "error_euler_avx.txt");
			break;
		case 6:
			compute_pi = &compute_pi_euler2;
			strcpy(method_name, "compute_pi_euler2_avx");
			strcpy(time_filename, "time_euler2.txt");
			strcpy(error_filename, "error_euler2.txt");
			break;
		default:
			break;
	}

	for (int i = 0; i < SAMPLE_SIZE; i++) {
		begin = clock();
		compute_pi(n * 1000000);
		end = clock();
		time_spent[i] = (double)(end - begin) / CLOCKS_PER_SEC;
	}
	double mean_time = compute_ci(&min, &max, time_spent);

	double pi = compute_pi(n * 1000000);
	double diff = pi - M_PI > 0 ? pi - M_PI : M_PI - pi;
	double error = diff / M_PI;

	printf("%s(%zuM) needs time in 95%% confidence interval[%lf, %lf]\n",
	       method_name, n, min, max);
	printf("error rate = %.15lf\n", error);

	FILE *fw = fopen(time_filename, "a");
	fprintf(fw, "%zu %lf\n", n, mean_time);
	fclose(fw);

	fw = fopen(error_filename, "a");
	fprintf(fw, "%zu %.15lf\n", n, error);
	fclose(fw);

	return 0;
}
示例#11
0
int main(int argc, char* argv[])
{
	char operation[32];
	size_t n = atoi(argv[2]);
	strncpy(operation, argv[1], 32);

	clock_t begin, end;
	double time_spent[SAMPLE_SIZE];
	double min, max;

	double (*compute_pi)(size_t);
	char method_name[64];
	char time_filename[64];
	char error_filename[64];

	if (!strcmp(operation, "baseline")) {
		compute_pi = &compute_pi_baseline;
		strcpy(method_name, "compute_pi_baseline");
		strcpy(time_filename, "time_baseline.txt");
		strcpy(error_filename, "error_baseline.txt");
	} else if (!strcmp(operation, "baseline_avx")) {
		compute_pi = &compute_pi_baseline_avx;
		strcpy(method_name, "compute_pi_baseline_avx");
		strcpy(time_filename, "time_baseline_avx.txt");
		strcpy(error_filename, "error_baseline_avx.txt");
	} else if (!strcmp(operation, "leibniz")) {
		compute_pi = &compute_pi_leibniz;
		strcpy(method_name, "compute_pi_leibniz");
		strcpy(time_filename, "time_leibniz.txt");
		strcpy(error_filename, "error_leibniz.txt");
	} else if (!strcmp(operation, "leibniz_avx")) {
		compute_pi = &compute_pi_leibniz_avx;
		strcpy(method_name, "compute_pi_leibniz_avx");
		strcpy(time_filename, "time_leibniz_avx.txt");
		strcpy(error_filename, "error_leibniz_avx.txt");
	} else if (!strcmp(operation, "leibniz_avx_opt")) {
		compute_pi = &compute_pi_leibniz_avx_opt;
		strcpy(method_name, "compute_pi_leibniz_avx_opt");
		strcpy(time_filename, "time_leibniz_avx_opt.txt");
		strcpy(error_filename, "error_leibniz_avx_opt.txt");
	} else if (!strcmp(operation, "leibniz_avx_opt_single")) {
		compute_pi = &compute_pi_leibniz_avx_opt_single;
		strcpy(method_name, "compute_pi_leibniz_avx_opt_single");
		strcpy(time_filename, "time_leibniz_avx_opt_single.txt");
		strcpy(error_filename, "error_leibniz_avx_opt_single.txt");
	} else if (!strcmp(operation, "leibniz_fma")) {
		compute_pi = &compute_pi_leibniz_fma;
		strcpy(method_name, "compute_pi_leibniz_fma");
		strcpy(time_filename, "time_leibniz_fma.txt");
		strcpy(error_filename, "error_leibniz_fma.txt");
	} else if (!strcmp(operation, "recursion")) {
		compute_pi = &compute_pi_recursion;
		strcpy(method_name, "compute_pi_recursion");
		strcpy(time_filename, "time_recursion.txt");
		strcpy(error_filename, "error_recursion.txt");
	} else if (!strcmp(operation, "leibniz_JM")) {
		compute_pi = &compute_pi_leibniz_JM;
		strcpy(method_name, "compute_pi_leibniz_JM");
		strcpy(time_filename, "time_leibniz_JM.txt");
		strcpy(error_filename, "error_leibniz_JM.txt");
	}



	for (int i = 0; i < SAMPLE_SIZE; i++) {
		begin = clock();
		compute_pi(n * 1024*1024);
		end = clock();
		time_spent[i] = (double)(end - begin) / CLOCKS_PER_SEC;
	}
	double mean_time = compute_ci(&min, &max, time_spent);

	double pi = 0.0;
	pi = compute_pi(n * 1024*1024);
	double diff = pi - M_PI > 0 ? pi - M_PI : M_PI - pi;
	double error = diff / M_PI;

	printf("%s(%zuM) needs time in 95%% confidence interval[%lf, %lf]\n",
	       method_name, n, min, max);
	printf("error rate = %.15lf\n", error);

	FILE *fw = fopen(time_filename, "a");
	fprintf(fw, "%zu %lf\n", n, mean_time);
	fclose(fw);

	fw = fopen(error_filename, "a");
	fprintf(fw, "%zu %.15lf\n", n, error);
	fclose(fw);

	return 0;
}
示例#12
0
int main()
{
	int selection;;
	//Loop with switch statement drives the user interface
	//User keeps entering options until they quit
	do
	{
	printf("Please Select an Option\n");
	printf("1-Computing pi\n");
	printf("2-Computing Square Root\n");
	printf("3-Displaying Primes\n");
	printf("4-Processing Grades\n");
	printf("5-Computing Tax\n");
	printf("6-Solving Quadratic\n");
	printf("7-Computing Factorials\n");
	printf("8-Counting File\n");
	printf("9-Sorting File\n");
	printf("10-Student File\n");
	printf("11-Quit\n");
	scanf("%d",&selection);
	
	switch(selection)
	{
		case 1: //Compute pi
		{
			int n;
			printf("How many terms of pi do you wish to compute: ");
			scanf("%d",&n);
			printf("pi to %d terms is %lf\n",n,compute_pi(n));
			break;
		}
		case 2: //Compute square root
		{
			double n;
			printf("Which square root do you wish to compute: ");
			scanf("%lf",&n);
			printf("the square root of %lf is %lf\n",n,compute_sqrt(n));
			break;
		}
		case 3: //Display primes
		{
			int n;
			printf("Enter an number to calculate primes up to: ");
			scanf("%d",&n);
			display_primes(n);
			break;
		}
		case 4: //Process grades
		{
			process_scores();
			break;
		}
		case 5: //Compute tax
		{
			int income;
			char status[10];
			char state;
			printf("What is your income: ");
			scanf("%d",&income);
			printf("In state or out of state: ");
			scanf("\n%c",&state);
			printf("Are you single or married: ");
			scanf("%s",status);
			printf("Your taxes are: %lf\n",compute_tax(income,status,state));
			break;
		}
		case 6: //Solving quadratic
		{
			double solution1;
			double solution2;
			double a;
			double b;
			double c;
			int solutions;
			printf("enter a: ");
			scanf("%lf",&a);
			printf("enter b: ");
			scanf("%lf",&b);
			printf("enter c: ");
			scanf("%lf",&c);
			solutions = quadratic(a,b,c,&solution1,&solution2);
			if (solutions)
			{
				printf("Solution 1: %lf\n",solution1);
				printf("Solution 2: %lf\n",solution2);
			}
			else
			{
				printf("There were no solutions\n");
			}
			break;
		}
		case 7: //Computing factorial
		{
			int n;
			printf("Enter the factorial to compute: ");
			scanf("%d",&n);
			printf("%d! is %d\n",n, factorial(n));
			break;
		}
		case 8: //counting file
		{
			char input_file[50];
			int characters;
			int lines;
			printf("Enter the file name to open: ");
			scanf("%s",input_file);
			file_count(input_file,&characters,&lines);
			printf("Number of lines: %d\n",lines);
			printf("Number of characters including EOF: %d\n",characters);
			break;
		}
		case 9: //sorting file
		{
			char input_file[50];
			char output_file[50];
			printf("Enter the input file name: ");
			scanf("%s",input_file);
			printf("Enter the output file name: ");
			scanf("%s",output_file);
			file_sort(input_file,output_file);
			printf("Operation Complete\n");
			break;
		}
		case 10: //student file
		{
			char input_file[50];
			printf("Enter the input file name: ");
			scanf("%s",input_file);
			file_student(input_file);
			printf("Operation Complete\n");
			break;
		}
		case 11: //quit
		{
			break;
		}
		
		default: //Error for incorrect entries
			printf("Your entry is invalid. Please try again\n");
	}
	}while(selection != 11); //do until user quits
	return 0;
}