[go-nuts] Re: parallel array processing question

2018-04-20 Thread Ankit Gupta
There could have been a data race in your code with the usage of allMax 
variable which is updated and checked by multiple goroutines. But you are 
saved by the use of mutex. All shared variables, in general, are subject to 
data races when there is a write involved. The local variables you create 
and params passed by value to goroutines are not subject to data race. Your 
usage of mutex is correct, but you can have it block only these lines --

mutex.Lock()

*if* sliceMax > allMax {

allMax = sliceMax

}

mutex.Unlock()


Now, whenever you have doubt, just remove mutex and run your code with 
-race directive. Go runtime will provide details if any racy code is 
detected.

Also, your slice range will omit last element, so do this - 

endIndex = len(numbers)


instead of 


endIndex = len(numbers) - 1



On Thursday, April 19, 2018 at 5:05:20 PM UTC+5:30, l vic wrote:
>
> I have a program that calculates max value in integer array by breaking 
> the array into number of slices and calculating max in every slice inside 
> of go-routine.
> Do I still need to lock/unlock each slice with mutex inside of go-routine? 
> The code seems to be working but are any apparent problems with it?
>
> package main
>
>
> import (
>
> "fmt"
>
> "os"
>
> "strconv"
>
> "sync"
>
> )
>
>
> //returns maximum number found in provided slice
>
> func maxInSlice(numbers []uint) (uint, error) {
>
> if numbers == nil {
>
> return 0, fmt.Errorf("nil  numbers")
>
> }
>
>
> var max uint = 0
>
> for _, n := range numbers {
>
> for _, m := range numbers {
>
> if n > m {
>
> max = n
>
> }
>
> }
>
> }
>
>
> return max, nil
>
> }
>
>
> // finds  maximum  number  in  numbers  array  by   breaking  work  into  N  
> pieces
>
> // (where  N  is provided  as a command  line  argument)  and processing  the
>
> // pieces  in  parallel  goroutines func  main()
>
> func main() {
>
> parallelism, _ := strconv.Atoi(os.Args[1])
>
> fmt.Printf("ok,  i'll  use %d  goroutines\n", parallelism)
>
>
> numbers := []uint{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
>
> wg := sync.WaitGroup{}
>
> mutex := sync.Mutex{}
>
> var allMax uint = 0
>
> perGoRoutine := len(numbers) / parallelism
>
> fmt.Printf("perGoRoutine=%d\n", perGoRoutine)
>
>
> for i := 0; i < parallelism; i++ {
>
> wg.Add(1)
>
> go func(i int) {
>
> defer wg.Done()
>
> fmt.Printf("==>index i = %d perGoRoutine in go func=%d\n", i, 
> perGoRoutine)
>
> startIndex := perGoRoutine * i
>
> endIndex := startIndex + perGoRoutine - 1
>
> //include last element
>
> if i == parallelism-1 {
>
> endIndex = len(numbers) - 1
>
>
> }
>
>
> fmt.Printf("startIndex=%d endIndex=%d\n", startIndex, endIndex)
>
> sliceMax, err := maxInSlice(numbers[startIndex:endIndex])
>
> mutex.Lock()
>
> if err != nil {
>
> fmt.Printf("error  finding  max  for  slice  %d  to  %d, 
> skipping  this  slice:  %s\n", err)
>
> return
>
> }
>
>
> fmt.Printf("goroutine  %d  (slice  %d  to  %d)  found  max  
> %d\n", i, startIndex, endIndex, sliceMax)
>
> if sliceMax > allMax {
>
> allMax = sliceMax
>
> }
>
> mutex.Unlock()
>
>
> }(i)
>
>
> }
>
> wg.Wait()
>
>
> fmt.Printf("maximum:  %d\n", allMax)
>
>
> }
>
>
>
>
>
-- 
*::DISCLAIMER::




The contents of this e-mail and any attachments are confidential and 
intended for the named recipient(s) only.E-mail transmission is not 
guaranteed to be secure or error-free as information could be intercepted, 
corrupted,lost, destroyed, arrive late or incomplete, or may contain 
viruses in transmission. The e mail and its contents(with or without 
referred errors) shall therefore not attach any liability on the originator 
or redBus.com. Views or opinions, if any, presented in this email are 
solely those of the author and may not necessarily reflect the views or 
opinions of redBus.com. Any form of reproduction, dissemination, copying, 
disclosure, modification,distribution and / or publication of this message 
without the prior written consent of authorized representative of redbus. 
com is strictly prohibited. If you have received this 
email in error please delete it and notify the sender immediately.Before 
opening any email and/or attachments, please check them for viruses and 
other defects.*

-- 
*::DISCLAIMER::




The contents of this e-mail and any 

[go-nuts] Re: parallel array processing question

2018-04-20 Thread Stefan Nilsson
Reading only is safe, but you will have a data race if two goroutines 
access the same variable concurrently and at least one of the accesses is a 
write. Here is an article that gives some rules of thumb on how to 
efficiently schedule parallel computation on separate CPUs: 
http://yourbasic.org/golang/efficient-parallel-computation/

On Thursday, April 19, 2018 at 1:35:20 PM UTC+2, l vic wrote:
>
> I have a program that calculates max value in integer array by breaking 
> the array into number of slices and calculating max in every slice inside 
> of go-routine.
> Do I still need to lock/unlock each slice with mutex inside of go-routine? 
> The code seems to be working but are any apparent problems with it?
>
> package main
>
>
> import (
>
> "fmt"
>
> "os"
>
> "strconv"
>
> "sync"
>
> )
>
>
> //returns maximum number found in provided slice
>
> func maxInSlice(numbers []uint) (uint, error) {
>
> if numbers == nil {
>
> return 0, fmt.Errorf("nil  numbers")
>
> }
>
>
> var max uint = 0
>
> for _, n := range numbers {
>
> for _, m := range numbers {
>
> if n > m {
>
> max = n
>
> }
>
> }
>
> }
>
>
> return max, nil
>
> }
>
>
> // finds  maximum  number  in  numbers  array  by   breaking  work  into  N  
> pieces
>
> // (where  N  is provided  as a command  line  argument)  and processing  the
>
> // pieces  in  parallel  goroutines func  main()
>
> func main() {
>
> parallelism, _ := strconv.Atoi(os.Args[1])
>
> fmt.Printf("ok,  i'll  use %d  goroutines\n", parallelism)
>
>
> numbers := []uint{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
>
> wg := sync.WaitGroup{}
>
> mutex := sync.Mutex{}
>
> var allMax uint = 0
>
> perGoRoutine := len(numbers) / parallelism
>
> fmt.Printf("perGoRoutine=%d\n", perGoRoutine)
>
>
> for i := 0; i < parallelism; i++ {
>
> wg.Add(1)
>
> go func(i int) {
>
> defer wg.Done()
>
> fmt.Printf("==>index i = %d perGoRoutine in go func=%d\n", i, 
> perGoRoutine)
>
> startIndex := perGoRoutine * i
>
> endIndex := startIndex + perGoRoutine - 1
>
> //include last element
>
> if i == parallelism-1 {
>
> endIndex = len(numbers) - 1
>
>
> }
>
>
> fmt.Printf("startIndex=%d endIndex=%d\n", startIndex, endIndex)
>
> sliceMax, err := maxInSlice(numbers[startIndex:endIndex])
>
> mutex.Lock()
>
> if err != nil {
>
> fmt.Printf("error  finding  max  for  slice  %d  to  %d, 
> skipping  this  slice:  %s\n", err)
>
> return
>
> }
>
>
> fmt.Printf("goroutine  %d  (slice  %d  to  %d)  found  max  
> %d\n", i, startIndex, endIndex, sliceMax)
>
> if sliceMax > allMax {
>
> allMax = sliceMax
>
> }
>
> mutex.Unlock()
>
>
> }(i)
>
>
> }
>
> wg.Wait()
>
>
> fmt.Printf("maximum:  %d\n", allMax)
>
>
> }
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: parallel array processing question

2018-04-19 Thread Egon
When you have such questions, then run your code with the race 
detector. https://golang.org/doc/articles/race_detector.html

Otherwise, you are only reading from the slice, there is no racing possible.

Also, your max function looks very weird and has a small bug:
https://play.golang.org/p/8K-0ze3RdDt

The slice calculations has bugs as well, e.g. try running with 64 
goroutines.

+ Egon

On Thursday, 19 April 2018 14:35:20 UTC+3, l vic wrote:
>
> I have a program that calculates max value in integer array by breaking 
> the array into number of slices and calculating max in every slice inside 
> of go-routine.
> Do I still need to lock/unlock each slice with mutex inside of go-routine? 
> The code seems to be working but are any apparent problems with it?
>
> package main
>
>
> import (
>
> "fmt"
>
> "os"
>
> "strconv"
>
> "sync"
>
> )
>
>
> //returns maximum number found in provided slice
>
> func maxInSlice(numbers []uint) (uint, error) {
>
> if numbers == nil {
>
> return 0, fmt.Errorf("nil  numbers")
>
> }
>
>
> var max uint = 0
>
> for _, n := range numbers {
>
> for _, m := range numbers {
>
> if n > m {
>
> max = n
>
> }
>
> }
>
> }
>
>
> return max, nil
>
> }
>
>
> // finds  maximum  number  in  numbers  array  by   breaking  work  into  N  
> pieces
>
> // (where  N  is provided  as a command  line  argument)  and processing  the
>
> // pieces  in  parallel  goroutines func  main()
>
> func main() {
>
> parallelism, _ := strconv.Atoi(os.Args[1])
>
> fmt.Printf("ok,  i'll  use %d  goroutines\n", parallelism)
>
>
> numbers := []uint{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
>
> wg := sync.WaitGroup{}
>
> mutex := sync.Mutex{}
>
> var allMax uint = 0
>
> perGoRoutine := len(numbers) / parallelism
>
> fmt.Printf("perGoRoutine=%d\n", perGoRoutine)
>
>
> for i := 0; i < parallelism; i++ {
>
> wg.Add(1)
>
> go func(i int) {
>
> defer wg.Done()
>
> fmt.Printf("==>index i = %d perGoRoutine in go func=%d\n", i, 
> perGoRoutine)
>
> startIndex := perGoRoutine * i
>
> endIndex := startIndex + perGoRoutine - 1
>
> //include last element
>
> if i == parallelism-1 {
>
> endIndex = len(numbers) - 1
>
>
> }
>
>
> fmt.Printf("startIndex=%d endIndex=%d\n", startIndex, endIndex)
>
> sliceMax, err := maxInSlice(numbers[startIndex:endIndex])
>
> mutex.Lock()
>
> if err != nil {
>
> fmt.Printf("error  finding  max  for  slice  %d  to  %d, 
> skipping  this  slice:  %s\n", err)
>
> return
>
> }
>
>
> fmt.Printf("goroutine  %d  (slice  %d  to  %d)  found  max  
> %d\n", i, startIndex, endIndex, sliceMax)
>
> if sliceMax > allMax {
>
> allMax = sliceMax
>
> }
>
> mutex.Unlock()
>
>
> }(i)
>
>
> }
>
> wg.Wait()
>
>
> fmt.Printf("maximum:  %d\n", allMax)
>
>
> }
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.