Skip to content

Commit 18ebd2d

Browse files
committed
bindings/go/blst.go: deduplicate numThreads calculations.
1 parent 05e9841 commit 18ebd2d

File tree

4 files changed

+56
-108
lines changed

4 files changed

+56
-108
lines changed

bindings/go/blst.go

Lines changed: 32 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,22 @@ func SetMaxProcs(max int) {
212212
maxProcs = max
213213
}
214214

215+
func numThreads(maxThreads int) int {
216+
numThreads := maxProcs
217+
218+
// take into consideration the possility that application reduced
219+
// GOMAXPROCS after |maxProcs| was initialized
220+
numProcs := runtime.GOMAXPROCS(0)
221+
if maxProcs > numProcs {
222+
numThreads = numProcs
223+
}
224+
225+
if maxThreads > 0 && numThreads > maxThreads {
226+
return maxThreads
227+
}
228+
return numThreads
229+
}
230+
215231
var cgo_pairingSizeOf = C.blst_pairing_sizeof()
216232
var cgo_p1Generator = *C.blst_p1_generator()
217233
var cgo_p2Generator = *C.blst_p2_generator()
@@ -633,13 +649,8 @@ func coreAggregateVerifyPkInG1(sigFn sigGetterP2, sigGroupcheck bool,
633649
}
634650

635651
numCores := runtime.GOMAXPROCS(0)
636-
numThreads := maxProcs
637-
if numThreads > numCores {
638-
numThreads = numCores
639-
}
640-
if numThreads > n {
641-
numThreads = n
642-
}
652+
numThreads := numThreads(n)
653+
643654
// Each thread will determine next message to process by atomically
644655
// incrementing curItem, process corresponding pk,msg[,aug] tuple and
645656
// repeat until n is exceeded. The resulting accumulations will be
@@ -830,14 +841,8 @@ func multipleAggregateVerifyPkInG1(paramsFn mulAggGetterPkInG1,
830841
useHash = optional[0]
831842
}
832843

833-
numCores := runtime.GOMAXPROCS(0)
834-
numThreads := maxProcs
835-
if numThreads > numCores {
836-
numThreads = numCores
837-
}
838-
if numThreads > n {
839-
numThreads = n
840-
}
844+
numThreads := numThreads(n)
845+
841846
// Each thread will determine next message to process by atomically
842847
// incrementing curItem, process corresponding pk,msg[,aug] tuple and
843848
// repeat until n is exceeded. The resulting accumulations will be
@@ -1235,13 +1240,8 @@ func coreAggregateVerifyPkInG2(sigFn sigGetterP1, sigGroupcheck bool,
12351240
}
12361241

12371242
numCores := runtime.GOMAXPROCS(0)
1238-
numThreads := maxProcs
1239-
if numThreads > numCores {
1240-
numThreads = numCores
1241-
}
1242-
if numThreads > n {
1243-
numThreads = n
1244-
}
1243+
numThreads := numThreads(n)
1244+
12451245
// Each thread will determine next message to process by atomically
12461246
// incrementing curItem, process corresponding pk,msg[,aug] tuple and
12471247
// repeat until n is exceeded. The resulting accumulations will be
@@ -1432,14 +1432,8 @@ func multipleAggregateVerifyPkInG2(paramsFn mulAggGetterPkInG2,
14321432
useHash = optional[0]
14331433
}
14341434

1435-
numCores := runtime.GOMAXPROCS(0)
1436-
numThreads := maxProcs
1437-
if numThreads > numCores {
1438-
numThreads = numCores
1439-
}
1440-
if numThreads > n {
1441-
numThreads = n
1442-
}
1435+
numThreads := numThreads(n)
1436+
14431437
// Each thread will determine next message to process by atomically
14441438
// incrementing curItem, process corresponding pk,msg[,aug] tuple and
14451439
// repeat until n is exceeded. The resulting accumulations will be
@@ -1742,14 +1736,8 @@ func (_ *P1Affine) BatchUncompress(in [][]byte) []*P1Affine {
17421736
points := make([]P1Affine, n)
17431737
pointsPtrs := make([]*P1Affine, n)
17441738

1745-
numCores := runtime.GOMAXPROCS(0)
1746-
numThreads := maxProcs
1747-
if numThreads > numCores {
1748-
numThreads = numCores
1749-
}
1750-
if numThreads > n {
1751-
numThreads = n
1752-
}
1739+
numThreads := numThreads(n)
1740+
17531741
// Each thread will determine next message to process by atomically
17541742
// incrementing curItem, process corresponding point, and
17551743
// repeat until n is exceeded. Each thread will send a result (true for
@@ -2122,11 +2110,7 @@ func P1AffinesMult(pointsIf interface{}, scalarsIf interface{}, nbits int) *P1 {
21222110
panic(fmt.Sprintf("unsupported type %T", val))
21232111
}
21242112

2125-
numThreads := maxProcs
2126-
numCores := runtime.GOMAXPROCS(0)
2127-
if numCores < maxProcs {
2128-
numThreads = numCores
2129-
}
2113+
numThreads := numThreads(0)
21302114

21312115
if numThreads < 2 || npoints < 32 {
21322116
sz := int(C.blst_p1s_mult_pippenger_scratch_sizeof(C.size_t(npoints))) / 8
@@ -2338,14 +2322,7 @@ func P1AffinesValidate(pointsIf interface{}) bool {
23382322
panic(fmt.Sprintf("unsupported type %T", val))
23392323
}
23402324

2341-
numCores := runtime.GOMAXPROCS(0)
2342-
numThreads := maxProcs
2343-
if numThreads > numCores {
2344-
numThreads = numCores
2345-
}
2346-
if numThreads > npoints {
2347-
numThreads = npoints
2348-
}
2325+
numThreads := numThreads(npoints)
23492326

23502327
if numThreads < 2 {
23512328
for i := 0; i < npoints; i++ {
@@ -2499,14 +2476,8 @@ func (_ *P2Affine) BatchUncompress(in [][]byte) []*P2Affine {
24992476
points := make([]P2Affine, n)
25002477
pointsPtrs := make([]*P2Affine, n)
25012478

2502-
numCores := runtime.GOMAXPROCS(0)
2503-
numThreads := maxProcs
2504-
if numThreads > numCores {
2505-
numThreads = numCores
2506-
}
2507-
if numThreads > n {
2508-
numThreads = n
2509-
}
2479+
numThreads := numThreads(n)
2480+
25102481
// Each thread will determine next message to process by atomically
25112482
// incrementing curItem, process corresponding point, and
25122483
// repeat until n is exceeded. Each thread will send a result (true for
@@ -2879,11 +2850,7 @@ func P2AffinesMult(pointsIf interface{}, scalarsIf interface{}, nbits int) *P2 {
28792850
panic(fmt.Sprintf("unsupported type %T", val))
28802851
}
28812852

2882-
numThreads := maxProcs
2883-
numCores := runtime.GOMAXPROCS(0)
2884-
if numCores < maxProcs {
2885-
numThreads = numCores
2886-
}
2853+
numThreads := numThreads(0)
28872854

28882855
if numThreads < 2 || npoints < 32 {
28892856
sz := int(C.blst_p2s_mult_pippenger_scratch_sizeof(C.size_t(npoints))) / 8
@@ -3095,14 +3062,7 @@ func P2AffinesValidate(pointsIf interface{}) bool {
30953062
panic(fmt.Sprintf("unsupported type %T", val))
30963063
}
30973064

3098-
numCores := runtime.GOMAXPROCS(0)
3099-
numThreads := maxProcs
3100-
if numThreads > numCores {
3101-
numThreads = numCores
3102-
}
3103-
if numThreads > npoints {
3104-
numThreads = npoints
3105-
}
3065+
numThreads := numThreads(npoints)
31063066

31073067
if numThreads < 2 {
31083068
for i := 0; i < npoints; i++ {

bindings/go/blst.tgo

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ func SetMaxProcs(max int) {
202202
maxProcs = max
203203
}
204204

205+
func numThreads(maxThreads int) int {
206+
numThreads := maxProcs
207+
208+
// take into consideration the possility that application reduced
209+
// GOMAXPROCS after |maxProcs| was initialized
210+
numProcs := runtime.GOMAXPROCS(0)
211+
if maxProcs > numProcs {
212+
numThreads = numProcs
213+
}
214+
215+
if maxThreads > 0 && numThreads > maxThreads {
216+
return maxThreads
217+
}
218+
return numThreads
219+
}
220+
205221
var cgo_pairingSizeOf = C.blst_pairing_sizeof()
206222
var cgo_p1Generator = *C.blst_p1_generator()
207223
var cgo_p2Generator = *C.blst_p2_generator()

bindings/go/blst_minpk.tgo

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,8 @@ func coreAggregateVerifyPkInG1(sigFn sigGetterP2, sigGroupcheck bool,
179179
}
180180

181181
numCores := runtime.GOMAXPROCS(0)
182-
numThreads := maxProcs
183-
if numThreads > numCores {
184-
numThreads = numCores
185-
}
186-
if numThreads > n {
187-
numThreads = n
188-
}
182+
numThreads := numThreads(n)
183+
189184
// Each thread will determine next message to process by atomically
190185
// incrementing curItem, process corresponding pk,msg[,aug] tuple and
191186
// repeat until n is exceeded. The resulting accumulations will be
@@ -376,14 +371,8 @@ func multipleAggregateVerifyPkInG1(paramsFn mulAggGetterPkInG1,
376371
useHash = optional[0]
377372
}
378373

379-
numCores := runtime.GOMAXPROCS(0)
380-
numThreads := maxProcs
381-
if numThreads > numCores {
382-
numThreads = numCores
383-
}
384-
if numThreads > n {
385-
numThreads = n
386-
}
374+
numThreads := numThreads(n)
375+
387376
// Each thread will determine next message to process by atomically
388377
// incrementing curItem, process corresponding pk,msg[,aug] tuple and
389378
// repeat until n is exceeded. The resulting accumulations will be

bindings/go/blst_px.tgo

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,8 @@ func (_ *P1Affine) BatchUncompress(in [][]byte) []*P1Affine {
8282
points := make([]P1Affine, n)
8383
pointsPtrs := make([]*P1Affine, n)
8484

85-
numCores := runtime.GOMAXPROCS(0)
86-
numThreads := maxProcs
87-
if numThreads > numCores {
88-
numThreads = numCores
89-
}
90-
if numThreads > n {
91-
numThreads = n
92-
}
85+
numThreads := numThreads(n)
86+
9387
// Each thread will determine next message to process by atomically
9488
// incrementing curItem, process corresponding point, and
9589
// repeat until n is exceeded. Each thread will send a result (true for
@@ -463,11 +457,7 @@ func P1AffinesMult(pointsIf interface{}, scalarsIf interface{}, nbits int) *P1 {
463457
panic(fmt.Sprintf("unsupported type %T",val))
464458
}
465459

466-
numThreads := maxProcs
467-
numCores := runtime.GOMAXPROCS(0)
468-
if numCores < maxProcs {
469-
numThreads = numCores
470-
}
460+
numThreads := numThreads(0)
471461

472462
if numThreads < 2 || npoints < 32 {
473463
sz := int(C.blst_p1s_mult_pippenger_scratch_sizeof(C.size_t(npoints)))/8
@@ -677,14 +667,7 @@ func P1AffinesValidate(pointsIf interface{}) bool {
677667
panic(fmt.Sprintf("unsupported type %T", val))
678668
}
679669

680-
numCores := runtime.GOMAXPROCS(0)
681-
numThreads := maxProcs
682-
if numThreads > numCores {
683-
numThreads = numCores
684-
}
685-
if numThreads > npoints {
686-
numThreads = npoints
687-
}
670+
numThreads := numThreads(npoints)
688671

689672
if numThreads < 2 {
690673
for i := 0; i < npoints; i++ {

0 commit comments

Comments
 (0)