Different parallel hashing (#12639)

* Paralellize hashing of large lists

* add unit test

* add file

* do not parallelize on low processor count

* revert minimal proc count

---------

Co-authored-by: Nishant Das <nishdas93@gmail.com>
This commit is contained in:
Potuz
2023-07-21 20:36:20 -04:00
committed by GitHub
parent 43378ae8d5
commit f97db3b738
9 changed files with 132 additions and 20 deletions

View File

@@ -1,4 +1,4 @@
load("@prysm//tools/go:def.bzl", "go_library")
load("@prysm//tools/go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
@@ -7,3 +7,11 @@ go_library(
visibility = ["//visibility:public"],
deps = ["@com_github_prysmaticlabs_gohashtree//:go_default_library"],
)
go_test(
name = "go_default_test",
size = "small",
srcs = ["hashtree_test.go"],
embed = [":go_default_library"],
deps = ["//testing/require:go_default_library"],
)

View File

@@ -1,17 +1,47 @@
package htr
import (
"runtime"
"sync"
"github.com/prysmaticlabs/gohashtree"
)
const minSliceSizeToParallelize = 5000
func hashParallel(inputList [][32]byte, outputList [][32]byte, wg *sync.WaitGroup) {
defer wg.Done()
err := gohashtree.Hash(outputList, inputList)
if err != nil {
panic(err)
}
}
// VectorizedSha256 takes a list of roots and hashes them using CPU
// specific vector instructions. Depending on host machine's specific
// hardware configuration, using this routine can lead to a significant
// performance improvement compared to the default method of hashing
// lists.
func VectorizedSha256(inputList [][32]byte, outputList [][32]byte) {
err := gohashtree.Hash(outputList, inputList)
func VectorizedSha256(inputList [][32]byte) [][32]byte {
outputList := make([][32]byte, len(inputList)/2)
if len(inputList) < minSliceSizeToParallelize {
err := gohashtree.Hash(outputList, inputList)
if err != nil {
panic(err)
}
return outputList
}
n := runtime.GOMAXPROCS(0) - 1
wg := sync.WaitGroup{}
wg.Add(n)
groupSize := len(inputList) / (2 * (n + 1))
for j := 0; j < n; j++ {
go hashParallel(inputList[j*2*groupSize:(j+1)*2*groupSize], outputList[j*groupSize:], &wg)
}
err := gohashtree.Hash(outputList[n*groupSize:], inputList[n*2*groupSize:])
if err != nil {
panic(err)
}
wg.Wait()
return outputList
}

View File

@@ -0,0 +1,27 @@
package htr
import (
"sync"
"testing"
"github.com/prysmaticlabs/prysm/v4/testing/require"
)
func Test_VectorizedSha256(t *testing.T) {
largeSlice := make([][32]byte, 32*minSliceSizeToParallelize)
secondLargeSlice := make([][32]byte, 32*minSliceSizeToParallelize)
hash1 := make([][32]byte, 16*minSliceSizeToParallelize)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
tempHash := VectorizedSha256(largeSlice)
copy(hash1, tempHash)
}()
wg.Wait()
hash2 := VectorizedSha256(secondLargeSlice)
require.Equal(t, len(hash1), len(hash2))
for i, r := range hash1 {
require.Equal(t, r, hash2[i])
}
}