pi_riemann.c ダウンロード/コピー
pi_riemann.c
pi_riemann.c
1"""
2概要:
3 中点リーマン和積分を用いて円周率を計算するC言語のベンチマークプログラムです。
4詳細説明:
5 指定された分割数と繰り返し回数で計算を実行し、実行時間の中央値や最小値を計測して出力します。
6関連リンク:
7 pi_riemann_usage
8"""
9
10/*
11 * Independent C benchmark for midpoint-Riemann integration of sqrt(1-x*x).
12 * Build (Linux): cc -O3 -std=c11 -D_POSIX_C_SOURCE=200809L pi_riemann.c -lm -o pi_riemann
13 * Build (Windows): create a Visual Studio C/C++ console project and add this file.
14 * Run: ./pi_riemann --nmax 10000000 --repeat 5
15 */
16
17#include <math.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <time.h>
22
23#if defined(_WIN32)
24#include <windows.h>
25#define NOINLINE __declspec(noinline)
26#else
27#define NOINLINE __attribute__((noinline))
28#endif
29
30static double now_seconds(void) {
31 """
32 概要:
33 高精度な現在時刻を秒単位で取得します。
34 詳細説明:
35 Windows環境ではQueryPerformanceCounterを、その他の環境ではclock_gettimeを使用します。
36 戻り値:
37 :returns: 現在の時刻の秒数。
38 :rtype: double
39 """
40#if defined(_WIN32)
41 LARGE_INTEGER counter, frequency;
42 QueryPerformanceFrequency(&frequency);
43 QueryPerformanceCounter(&counter);
44 return (double)counter.QuadPart / (double)frequency.QuadPart;
45#else
46 struct timespec ts;
47 clock_gettime(CLOCK_MONOTONIC, &ts);
48 return (double)ts.tv_sec + 1.0e-9 * (double)ts.tv_nsec;
49#endif
50}
51
52/* noinline plus the volatile function pointer below prevent Release builds
53 from moving this loop out of the benchmark's repeat loop. */
54static NOINLINE double pi_midpoint(long long nmax) {
55 """
56 概要:
57 中点リーマン和積分を使用して円周率の近似値を計算します。
58 詳細説明:
59 指定された分割数で積分を計算します。コンパイラの最適化による影響を防ぐためnoinline属性を使用しています。
60 引数:
61 :param nmax: 積分の分割数。
62 :type nmax: long long
63 戻り値:
64 :returns: 計算された円周率の近似値。
65 :rtype: double
66 """
67 const double dx = 1.0 / (double)nmax;
68 double total = 0.0;
69 for (long long i = 0; i < nmax; ++i) {
70 const double x = ((double)i + 0.5) * dx;
71 total += sqrt(1.0 - x * x);
72 }
73 return 4.0 * dx * total;
74}
75
76static double (* volatile pi_function)(long long) = pi_midpoint;
77
78static int compare_double(const void* a, const void* b) {
79 """
80 概要:
81 2つのdouble型の値を比較します。
82 詳細説明:
83 qsort関数で使用するための比較関数です。
84 引数:
85 :param a: 比較する1つ目の値へのポインタ。
86 :type a: const void*
87 :param b: 比較する2つ目の値へのポインタ。
88 :type b: const void*
89 戻り値:
90 :returns: 1つ目の値が大きい場合は正の値、小さい場合は負の値、等しい場合は0。
91 :rtype: int
92 """
93 const double da = *(const double*)a;
94 const double db = *(const double*)b;
95 return (da > db) - (da < db);
96}
97
98static void usage(const char* program) {
99 """
100 概要:
101 プログラムの使用方法を標準エラー出力に表示します。
102 引数:
103 :param program: プログラムの実行ファイル名。
104 :type program: const char*
105 """
106 fprintf(stderr, "Usage: %s [--nmax positive-integer] [--repeat positive-integer]\n", program);
107}
108
109int main(int argc, char** argv) {
110 """
111 概要:
112 ベンチマークプログラムのメイン関数です。
113 詳細説明:
114 コマンドライン引数を解析し、指定された条件で円周率計算のベンチマークを実行して結果を出力します。
115 引数:
116 :param argc: コマンドライン引数の数。
117 :type argc: int
118 :param argv: コマンドライン引数の文字列配列。
119 :type argv: char**
120 戻り値:
121 :returns: 正常終了時は0、エラー発生時や不正な引数の場合は0以外の終了コード。
122 :rtype: int
123 """
124 long long nmax = 1000000;
125 int repeat = 3;
126
127 for (int i = 1; i < argc; ++i) {
128 if (strcmp(argv[i], "--nmax") == 0 && i + 1 < argc) {
129 nmax = strtoll(argv[++i], NULL, 10);
130 }
131 else if (strcmp(argv[i], "--repeat") == 0 && i + 1 < argc) {
132 repeat = (int)strtol(argv[++i], NULL, 10);
133 }
134 else if (strcmp(argv[i], "--help") == 0) {
135 usage(argv[0]);
136 return 0;
137 }
138 else {
139 usage(argv[0]);
140 return 2;
141 }
142 }
143 if (nmax < 1 || repeat < 1) {
144 usage(argv[0]);
145 return 2;
146 }
147
148 double* times = malloc((size_t)repeat * sizeof(*times));
149 if (times == NULL) {
150 fprintf(stderr, "Unable to allocate timing array.\n");
151 return 1;
152 }
153
154 double value = 0.0;
155 for (int r = 0; r < repeat; ++r) {
156 const double t0 = now_seconds();
157 value = pi_function(nmax);
158 times[r] = now_seconds() - t0;
159 }
160 qsort(times, (size_t)repeat, sizeof(*times), compare_double);
161 const double median = (repeat % 2) ? times[repeat / 2]
162 : 0.5 * (times[repeat / 2 - 1] + times[repeat / 2]);
163
164 printf("Midpoint Riemann sum: nmax=%lld, repeat=%d\n", nmax, repeat);
165 printf("C pi=%.15f abs.error=%.3e median=%.6f s min=%.6f s\n",
166 value, fabs(value - acos(-1.0)), median, times[0]);
167 free(times);
168 return 0;
169}