1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // The race detector does not understand ParFor synchronization.
18 // Simple serial sanity test for parallelfor.
19 func TestParFor(t
*testing
.T
) {
22 data
:= make([]uint64, N
)
23 for i
:= uint64(0); i
< N
; i
++ {
27 // Avoid making func a closure: parfor cannot invoke them.
28 // Since it doesn't happen in the C code, it's not worth doing
31 ParForSetup(desc
, P
, N
, nil, true, func(desc
*ParFor
, i
uint32) {
33 data
[i
] = data
[i
]*data
[i
] + 1
36 for i
:= uint64(0); i
< N
; i
++ {
38 t
.Fatalf("Wrong element %d: %d", i
, data
[i
])
43 // Test that nonblocking parallelfor does not block.
44 func TestParFor2(t
*testing
.T
) {
47 data
:= make([]uint64, N
)
48 for i
:= uint64(0); i
< N
; i
++ {
52 ParForSetup(desc
, P
, N
, (*byte)(unsafe
.Pointer(&data
)), false, func(desc
*ParFor
, i
uint32) {
53 d
:= *(*[]uint64)(unsafe
.Pointer(desc
.Ctx
))
56 for p
:= 0; p
< P
; p
++ {
59 for i
:= uint64(0); i
< N
; i
++ {
61 t
.Fatalf("Wrong element %d: %d", i
, data
[i
])
66 // Test that iterations are properly distributed.
67 func TestParForSetup(t
*testing
.T
) {
71 for n
:= uint32(0); n
< N
; n
++ {
72 for p
:= uint32(1); p
<= P
; p
++ {
73 ParForSetup(desc
, p
, n
, nil, true, func(desc
*ParFor
, i
uint32) {})
77 for i
:= uint32(0); i
< p
; i
++ {
78 begin
, end
:= ParForIters(desc
, i
)
84 t
.Fatalf("incorrect begin: %d (n=%d, p=%d)", begin
, n
, p
)
87 if size
!= size0
&& size
!= size0
+1 {
88 t
.Fatalf("incorrect size: %d/%d (n=%d, p=%d)", size
, size0
, n
, p
)
91 t
.Fatalf("incorrect begin/end: %d/%d (n=%d, p=%d)", begin
, end0
, n
, p
)
97 t
.Fatalf("incorrect sum: %d/%d (p=%d)", sum
, n
, p
)
103 // Test parallel parallelfor.
104 func TestParForParallel(t
*testing
.T
) {
109 data
:= make([]uint64, N
)
110 for i
:= uint64(0); i
< N
; i
++ {
114 c
:= make(chan bool, P
)
115 desc
:= NewParFor(uint32(P
))
117 ParForSetup(desc
, uint32(P
), uint32(N
), nil, false, func(desc
*ParFor
, i
uint32) {
119 data
[i
] = data
[i
]*data
[i
] + 1
121 for p
:= 1; p
< P
; p
++ {
128 for p
:= 1; p
< P
; p
++ {
131 for i
:= uint64(0); i
< N
; i
++ {
132 if data
[i
] != i
*i
+1 {
133 t
.Fatalf("Wrong element %d: %d", i
, data
[i
])
137 data
, desc
= nil, nil