mirror of
https://github.com/golang/go
synced 2024-11-14 15:00:27 -07:00
ebbdf2a14c
Prior to the conversion of the runtime to Go, this void* was necessary to get closure information in to C callbacks. There are no more C callbacks and parfor is perfectly capable of invoking a Go closure now, so eliminate ctx and all of its unsafe-ness. (Plus, the runtime currently doesn't use ctx for anything.) Change-Id: I39fc53b7dd3d7f660710abc76b0d831bfc6296d8 Reviewed-on: https://go-review.googlesource.com/3395 Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
129 lines
2.7 KiB
Go
129 lines
2.7 KiB
Go
// Copyright 2012 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// The race detector does not understand ParFor synchronization.
|
|
// +build !race
|
|
|
|
package runtime_test
|
|
|
|
import (
|
|
. "runtime"
|
|
"testing"
|
|
)
|
|
|
|
// Simple serial sanity test for parallelfor.
|
|
func TestParFor(t *testing.T) {
|
|
const P = 1
|
|
const N = 20
|
|
data := make([]uint64, N)
|
|
for i := uint64(0); i < N; i++ {
|
|
data[i] = i
|
|
}
|
|
desc := NewParFor(P)
|
|
ParForSetup(desc, P, N, true, func(desc *ParFor, i uint32) {
|
|
data[i] = data[i]*data[i] + 1
|
|
})
|
|
ParForDo(desc)
|
|
for i := uint64(0); i < N; i++ {
|
|
if data[i] != i*i+1 {
|
|
t.Fatalf("Wrong element %d: %d", i, data[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
// Test that nonblocking parallelfor does not block.
|
|
func TestParFor2(t *testing.T) {
|
|
const P = 7
|
|
const N = 1003
|
|
data := make([]uint64, N)
|
|
for i := uint64(0); i < N; i++ {
|
|
data[i] = i
|
|
}
|
|
desc := NewParFor(P)
|
|
ParForSetup(desc, P, N, false, func(desc *ParFor, i uint32) {
|
|
data[i] = data[i]*data[i] + 1
|
|
})
|
|
for p := 0; p < P; p++ {
|
|
ParForDo(desc)
|
|
}
|
|
for i := uint64(0); i < N; i++ {
|
|
if data[i] != i*i+1 {
|
|
t.Fatalf("Wrong element %d: %d", i, data[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
// Test that iterations are properly distributed.
|
|
func TestParForSetup(t *testing.T) {
|
|
const P = 11
|
|
const N = 101
|
|
desc := NewParFor(P)
|
|
for n := uint32(0); n < N; n++ {
|
|
for p := uint32(1); p <= P; p++ {
|
|
ParForSetup(desc, p, n, true, func(desc *ParFor, i uint32) {})
|
|
sum := uint32(0)
|
|
size0 := uint32(0)
|
|
end0 := uint32(0)
|
|
for i := uint32(0); i < p; i++ {
|
|
begin, end := ParForIters(desc, i)
|
|
size := end - begin
|
|
sum += size
|
|
if i == 0 {
|
|
size0 = size
|
|
if begin != 0 {
|
|
t.Fatalf("incorrect begin: %d (n=%d, p=%d)", begin, n, p)
|
|
}
|
|
} else {
|
|
if size != size0 && size != size0+1 {
|
|
t.Fatalf("incorrect size: %d/%d (n=%d, p=%d)", size, size0, n, p)
|
|
}
|
|
if begin != end0 {
|
|
t.Fatalf("incorrect begin/end: %d/%d (n=%d, p=%d)", begin, end0, n, p)
|
|
}
|
|
}
|
|
end0 = end
|
|
}
|
|
if sum != n {
|
|
t.Fatalf("incorrect sum: %d/%d (p=%d)", sum, n, p)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Test parallel parallelfor.
|
|
func TestParForParallel(t *testing.T) {
|
|
N := uint64(1e7)
|
|
if testing.Short() {
|
|
N /= 10
|
|
}
|
|
data := make([]uint64, N)
|
|
for i := uint64(0); i < N; i++ {
|
|
data[i] = i
|
|
}
|
|
P := GOMAXPROCS(-1)
|
|
c := make(chan bool, P)
|
|
desc := NewParFor(uint32(P))
|
|
ParForSetup(desc, uint32(P), uint32(N), false, func(desc *ParFor, i uint32) {
|
|
data[i] = data[i]*data[i] + 1
|
|
})
|
|
for p := 1; p < P; p++ {
|
|
go func() {
|
|
ParForDo(desc)
|
|
c <- true
|
|
}()
|
|
}
|
|
ParForDo(desc)
|
|
for p := 1; p < P; p++ {
|
|
<-c
|
|
}
|
|
for i := uint64(0); i < N; i++ {
|
|
if data[i] != i*i+1 {
|
|
t.Fatalf("Wrong element %d: %d", i, data[i])
|
|
}
|
|
}
|
|
|
|
data, desc = nil, nil
|
|
GC()
|
|
}
|