Skip to content
This repository was archived by the owner on Mar 24, 2026. It is now read-only.

Commit 90a08f5

Browse files
bibikaranton-malakhov
authored andcommitted
Add .clang-format and format RNG C source (#8)
* Add .clang-format and format RNG C source * Add format rules to rng, linalg makefiles
1 parent f6ddc50 commit 90a08f5

4 files changed

Lines changed: 96 additions & 93 deletions

File tree

‎.clang-format‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
BasedOnStyle: LLVM
2+
IndentWidth: 4
3+
AlignAfterOpenBracket: true
4+
AllowShortFunctionsOnASingleLine: None
5+
SpaceAfterCStyleCast: true
6+
Standard: Cpp11

‎numpy/linalg/Makefile‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@>
1111
BENCHES = cholesky det dot inv lu qr svd
1212
SOURCES = $(addsuffix .cc,$(BENCHES)) linalg.cc
1313

14+
CLANG_FORMAT = clang-format
15+
1416
$(TARGET): $(SOURCES:.cc=.o)
1517
$(CXX) $^ -o $@ $(LDFLAGS)
1618

1719
clean:
1820
rm -f $(TARGET) *.o
1921

22+
format:
23+
$(CLANG_FORMAT) -i $(SOURCES)
24+
25+
.PHONY: clean format
26+

‎numpy/random/Makefile‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
BENCHMARKS = rng
66
SOURCES = $(addsuffix .c,$(BENCHMARKS))
77
CC = icc
8+
CLANG_FORMAT = clang-format
89
CFLAGS += -m64 -fPIC -fomit-frame-pointer -xSSE4.2 -axCORE-AVX2,CORE-AVX512 \
910
-O3 -fp-model fast=2 -fimf-precision=high -prec-sqrt -prec-div \
1011
-fprotect-parens
@@ -19,6 +20,9 @@ $(BENCHMARKS): $(SOURCES)
1920
clean:
2021
rm -f $(BENCHMARKS)
2122

22-
.PHONY: clean run
23+
format:
24+
$(CLANG_FORMAT) -i $(SOURCES)
25+
26+
.PHONY: clean run format
2327

2428

‎numpy/random/rng.c‎

Lines changed: 78 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -18,79 +18,77 @@ extern void sample_uniform(VSLStreamStatePtr stream, MKL_INT sample_size) {
1818
int err;
1919
double *x;
2020
double a = -1.0, b = 1.0;
21-
x = (double *) mkl_malloc(sizeof(double)*sample_size, 64);
21+
x = (double *) mkl_malloc(sizeof(double) * sample_size, 64);
2222

23-
err = vdRngUniform(VSL_RNG_METHOD_UNIFORM_STD_ACCURATE, stream, sample_size, x, a, b);
23+
err = vdRngUniform(VSL_RNG_METHOD_UNIFORM_STD_ACCURATE, stream, sample_size,
24+
x, a, b);
2425
if (err != VSL_STATUS_OK) {
25-
printf("Uniform RNG error code: %d\n", err);
26+
printf("Uniform RNG error code: %d\n", err);
2627
}
2728

2829
mkl_free(x);
2930
}
3031

31-
3232
/* mkl_random.standard_normal */
3333
extern void sample_normal(VSLStreamStatePtr stream, MKL_INT sample_size) {
3434
int err;
3535
double *x;
3636
double mu_zero = 0.0, sigma_one = 1.0;
3737

38-
x = (double *) mkl_malloc(sizeof(double)*sample_size, 64);
38+
x = (double *) mkl_malloc(sizeof(double) * sample_size, 64);
3939
err = vdRngGaussian(VSL_RNG_METHOD_GAUSSIAN_ICDF, stream, sample_size, x,
40-
mu_zero, sigma_one);
40+
mu_zero, sigma_one);
4141
if (err != VSL_STATUS_OK) {
42-
printf("Normal RNG error code: %d\n", err);
42+
printf("Normal RNG error code: %d\n", err);
4343
}
4444

4545
mkl_free(x);
4646
}
4747

48-
4948
/* mkl_random.gamma(5.2, 1) */
5049
extern void sample_gamma(VSLStreamStatePtr stream, MKL_INT sample_size) {
5150
int err;
5251
double *x;
5352
double shape_par = 5.2, scale_one = 1.0, loc_zero = 0.0;
5453

55-
x = (double *) mkl_malloc(sizeof(double)*sample_size, 64);
56-
err = vdRngGamma(VSL_RNG_METHOD_GAMMA_GNORM_ACCURATE, stream, sample_size, x,
57-
shape_par, loc_zero, scale_one);
54+
x = (double *) mkl_malloc(sizeof(double) * sample_size, 64);
55+
err = vdRngGamma(VSL_RNG_METHOD_GAMMA_GNORM_ACCURATE, stream, sample_size,
56+
x, shape_par, loc_zero, scale_one);
5857
if (err != VSL_STATUS_OK) {
59-
printf("Gamma RNG error code: %d\n", err);
58+
printf("Gamma RNG error code: %d\n", err);
6059
}
6160

6261
mkl_free(x);
6362
}
6463

65-
6664
/* mkl_random.beta(0.7, 2.5) */
6765
extern void sample_beta(VSLStreamStatePtr stream, MKL_INT sample_size) {
6866
int err;
6967
double *x;
7068
double shape_par1 = 0.7, shape_par2 = 2.5;
7169
double loc_zero = 0.0, scale_one = 1.0;
7270

73-
x = (double *) mkl_malloc(sizeof(double)*sample_size, 64);
71+
x = (double *) mkl_malloc(sizeof(double) * sample_size, 64);
7472
err = vdRngBeta(VSL_RNG_METHOD_BETA_CJA_ACCURATE, stream, sample_size, x,
75-
shape_par1, shape_par2, loc_zero, scale_one);
73+
shape_par1, shape_par2, loc_zero, scale_one);
7674
if (err != VSL_STATUS_OK) {
77-
printf("Beta RNG error code: %d\n", err);
75+
printf("Beta RNG error code: %d\n", err);
7876
}
7977

8078
mkl_free(x);
8179
}
8280

83-
8481
/* mkl_random.randint(0,100) */
8582
extern void sample_randint(VSLStreamStatePtr stream, MKL_INT sample_size) {
8683
MKL_INT *x;
8784
int err;
8885
MKL_INT a = 0, b = 100;
8986

9087
x = (MKL_INT *) mkl_malloc(sizeof(MKL_INT) * sample_size, 64);
91-
err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, stream, sample_size, x, a, b);
88+
err =
89+
viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, stream, sample_size, x, a, b);
9290
if (err != VSL_STATUS_OK) {
93-
printf("RandInt RNG error code: %d\n", err);
91+
printf("RandInt RNG error code: %d\n", err);
9492
}
9593
mkl_free(x);
9694
}
@@ -102,9 +100,10 @@ extern void sample_poisson(VSLStreamStatePtr stream, MKL_INT sample_size) {
102100
double rate = 7.2;
103101

104102
x = (MKL_INT *) mkl_malloc(sizeof(MKL_INT) * sample_size, 64);
105-
err = viRngPoisson(VSL_RNG_METHOD_POISSON_POISNORM, stream, sample_size, x, rate);
103+
err = viRngPoisson(VSL_RNG_METHOD_POISSON_POISNORM, stream, sample_size, x,
104+
rate);
106105
if (err != VSL_STATUS_OK) {
107-
printf("Poisson RNG error code: %d\n", err);
106+
printf("Poisson RNG error code: %d\n", err);
108107
}
109108
mkl_free(x);
110109
}
@@ -113,98 +112,85 @@ extern void sample_poisson(VSLStreamStatePtr stream, MKL_INT sample_size) {
113112
extern void sample_hypergeom(VSLStreamStatePtr stream, MKL_INT sample_size) {
114113
MKL_INT *x;
115114
int err;
116-
MKL_INT el=214 + 97, es = 83, em = 214;
115+
MKL_INT el = 214 + 97, es = 83, em = 214;
117116

118117
x = (MKL_INT *) mkl_malloc(sizeof(MKL_INT) * sample_size, 64);
119-
err = viRngHypergeometric(VSL_RNG_METHOD_HYPERGEOMETRIC_H2PE, stream, sample_size, x, el, es, em);
118+
err = viRngHypergeometric(VSL_RNG_METHOD_HYPERGEOMETRIC_H2PE, stream,
119+
sample_size, x, el, es, em);
120120
if (err != VSL_STATUS_OK) {
121-
printf("RandInt RNG error code: %d\n", err);
121+
printf("RandInt RNG error code: %d\n", err);
122122
}
123123

124124
mkl_free(x);
125125
}
126126

127127
#define BRNGS_LEN 9
128128
const MKL_INT brngs[BRNGS_LEN] = {
129-
VSL_BRNG_WH,
130-
VSL_BRNG_MT19937,
131-
VSL_BRNG_SFMT19937,
132-
VSL_BRNG_MT2203,
133-
VSL_BRNG_R250,
134-
VSL_BRNG_MCG31,
135-
VSL_BRNG_MCG59,
136-
VSL_BRNG_MRG32K3A,
137-
VSL_BRNG_PHILOX4X32X10
138-
};
139-
const char* brng_names[BRNGS_LEN] = {
140-
"WH", "MT19937", "SFMT19937", "MT2203", "R250", "MCG31", "MCG59", "MRG32K3A", "PHILOX4X32X10"
141-
};
142-
143-
typedef void (*DistributionSampler)(VSLStreamStatePtr stream, MKL_INT sample_size);
129+
VSL_BRNG_WH, VSL_BRNG_MT19937, VSL_BRNG_SFMT19937,
130+
VSL_BRNG_MT2203, VSL_BRNG_R250, VSL_BRNG_MCG31,
131+
VSL_BRNG_MCG59, VSL_BRNG_MRG32K3A, VSL_BRNG_PHILOX4X32X10};
132+
const char *brng_names[BRNGS_LEN] = {"WH", "MT19937", "SFMT19937",
133+
"MT2203", "R250", "MCG31",
134+
"MCG59", "MRG32K3A", "PHILOX4X32X10"};
135+
136+
typedef void (*DistributionSampler)(VSLStreamStatePtr stream,
137+
MKL_INT sample_size);
144138

145139
#define FN_LEN 7
146140
const DistributionSampler fns[FN_LEN] = {
147-
&sample_uniform,
148-
&sample_normal,
149-
&sample_gamma,
150-
&sample_beta,
151-
&sample_randint,
152-
&sample_poisson,
153-
&sample_hypergeom
154-
};
155-
156-
const char* dist_names[FN_LEN] = {
157-
"uniform", "normal", "gamma", "beta", "randint", "poisson", "hypergeom"
158-
};
141+
&sample_uniform, &sample_normal, &sample_gamma, &sample_beta,
142+
&sample_randint, &sample_poisson, &sample_hypergeom};
143+
144+
const char *dist_names[FN_LEN] = {"uniform", "normal", "gamma", "beta",
145+
"randint", "poisson", "hypergeom"};
159146

160147
const MKL_INT dist_sample_sizes[FN_LEN] = {
161-
10 * SAMPLE_SIZE, /* uniform */
162-
2 * SAMPLE_SIZE,
163-
SAMPLE_SIZE,
164-
SAMPLE_SIZE,
165-
10 * SAMPLE_SIZE, /* randint */
166-
5 * SAMPLE_SIZE, /* poisson */
167-
SAMPLE_SIZE
168-
};
148+
10 * SAMPLE_SIZE, /* uniform */
149+
2 * SAMPLE_SIZE, SAMPLE_SIZE, SAMPLE_SIZE, 10 * SAMPLE_SIZE, /* randint */
150+
5 * SAMPLE_SIZE, /* poisson */
151+
SAMPLE_SIZE};
169152

170153
int main(void) {
171154
VSLStreamStatePtr stream;
172155
int err, outer_it, inner_it, brng_idx, fn_idx;
173156
double times[OUTER_REPS];
174157

175-
for (brng_idx=0; brng_idx < BRNGS_LEN; brng_idx++) {
176-
for (fn_idx = 0; fn_idx < FN_LEN; fn_idx++) {
177-
DistributionSampler sampling_fn = fns[fn_idx];
178-
MKL_INT sz = dist_sample_sizes[fn_idx];
179-
180-
for(outer_it=0; outer_it < OUTER_REPS; outer_it++) {
181-
struct timespec ts_start, ts_finish;
182-
183-
err = vslNewStream(&stream, brngs[brng_idx], 123);
184-
if (err != VSL_STATUS_OK) {
185-
printf("PANIC: abandon ship... \n");
186-
}
187-
188-
clock_gettime(CLOCK_MONOTONIC, &ts_start);
189-
for(inner_it=0; inner_it < INNER_REPS; inner_it++) {
190-
(*sampling_fn)(stream, sz);
191-
}
192-
clock_gettime(CLOCK_MONOTONIC, &ts_finish);
193-
194-
times[outer_it] = (ts_finish.tv_sec - ts_start.tv_sec) + (ts_finish.tv_nsec - ts_start.tv_nsec) * (1e-9);
195-
vslDeleteStream(&stream);
196-
}
197-
198-
199-
{
200-
double min_time = times[0];
201-
int i;
202-
for (i=1; i < OUTER_REPS; i++)
203-
if (times[i] < min_time) min_time = times[i];
204-
205-
printf("Native-C,%s,%s,%.5f\n",brng_names[brng_idx], dist_names[fn_idx], min_time);
206-
}
207-
}
158+
for (brng_idx = 0; brng_idx < BRNGS_LEN; brng_idx++) {
159+
for (fn_idx = 0; fn_idx < FN_LEN; fn_idx++) {
160+
DistributionSampler sampling_fn = fns[fn_idx];
161+
MKL_INT sz = dist_sample_sizes[fn_idx];
162+
163+
for (outer_it = 0; outer_it < OUTER_REPS; outer_it++) {
164+
struct timespec ts_start, ts_finish;
165+
166+
err = vslNewStream(&stream, brngs[brng_idx], 123);
167+
if (err != VSL_STATUS_OK) {
168+
printf("PANIC: abandon ship... \n");
169+
}
170+
171+
clock_gettime(CLOCK_MONOTONIC, &ts_start);
172+
for (inner_it = 0; inner_it < INNER_REPS; inner_it++) {
173+
(*sampling_fn)(stream, sz);
174+
}
175+
clock_gettime(CLOCK_MONOTONIC, &ts_finish);
176+
177+
times[outer_it] =
178+
(ts_finish.tv_sec - ts_start.tv_sec) +
179+
(ts_finish.tv_nsec - ts_start.tv_nsec) * (1e-9);
180+
vslDeleteStream(&stream);
181+
}
182+
183+
{
184+
double min_time = times[0];
185+
int i;
186+
for (i = 1; i < OUTER_REPS; i++)
187+
if (times[i] < min_time)
188+
min_time = times[i];
189+
190+
printf("Native-C,%s,%s,%.5f\n", brng_names[brng_idx],
191+
dist_names[fn_idx], min_time);
192+
}
193+
}
208194
}
209195

210196
return 0;

0 commit comments

Comments
 (0)