Exemple #1
0
int main()
{
    const int digits = 10000000;

    std::cout.precision(digits + 10);
    //std::cout << std::fixed;

    mpz_class pi = chudnovsky(digits);
    //std::cout << pi << std::endl;

    return 0;
}
Exemple #2
0
int main(int argc, char *argv[])
{
	int nsec, res = 0, opt, digits = DEFAULT_DIGITS, threads = 0;
	double sec;
	double cpu_time, cpu_start, cpu_end;
	struct timespec start, end;

	const char* short_opts = "hd:t:";
	const struct option long_opts[] = {
		{ "help",	0, NULL, 'h' },
		{ "digits",	1, NULL, 'd' },
		{ "threads",	1, NULL, 't' },
	};

	printf("pi-calc version %s\n", VERSION);

	do {
		opt = getopt_long(argc, argv, short_opts, long_opts, NULL);
		switch(opt)
		{
		case 'h':
			print_usage();
			return 0;
		case 'd':
			digits = atoi(optarg);
			if (digits <= 0) {
				res = -EINVAL;
				print_err("Wrong digits value", res);
				return res;
			}
			break;
		case 't':
			threads = atoi(optarg);
			if (threads <= 0) {
				res = -EINVAL;
				print_err("Wrong threads count", res);
				return res;
			}
			break;
		case -1:
			break;
		default:
			print_usage();
			res = -EINVAL;
			return res;
		}
	} while (opt != -1);

	/* Get CPU and normal time */
	cpu_start = clock();
	res = clock_gettime(CLOCK_MONOTONIC, &start);
	if (res < 0) {
		res = -errno;
		print_err("Cannot obtain start time", res);
		return res;
	}

	/* Run the Chudnovsky algorithm */
	res = chudnovsky(digits, threads);
	if (res < 0) {
		print_err("Error during execution", res);
		return res;
	}

	/* Get the end time */
	cpu_end = clock();
	res = clock_gettime(CLOCK_MONOTONIC, &end);
	if (res < 0) {
		res = -errno;
		print_err("Cannot obtain stop time", res);
		return res;
	}

	sec = difftime(end.tv_sec, start.tv_sec);
	if (end.tv_nsec >= start.tv_nsec) {
		nsec = end.tv_nsec - start.tv_nsec;
	} else {
		nsec = start.tv_nsec - end.tv_nsec;
		sec--;
	}
	cpu_time = ((double)(cpu_end - cpu_start)) / CLOCKS_PER_SEC;

	printf("Run time: %.0f.%.9d s\n", sec, nsec);
	printf("CPU time: %.9f s\n", cpu_time);

	return 0;
}