douliandan7340 2019-02-12 12:47
浏览 86

如何在并行for循环中使用通道

func parallelSum (c chan int){
  sum := 0
  for i :=1 ; i< 100;i++{
    go func(i int){
        sum += i
    }(i)
  }
    time.Sleep(1*time.Second)
    c <- sum
}

I'm trying to learn the parallel ability to speed up things like OpenMP. And here is an example of the intended summing up parallel loop in Go, this function runs as a goroutine.

Note that the variable sum is not a channel here, so does this mean the variable sum access inside the for loop is a blocked operation? Is it now efficient enough? Is there a better solution?

I knew the channel feature was designed for this, my obviously wrong implement below can compile, but with 100 runtime errors like following.

goroutine 4 [chan receive]:
main.parallelSumError(0xc0000180c0)
    /home/tom/src/goland_newproject/main.go:58 +0xb4 //line 58 : temp := <-sum
created by main.main
    /home/tom/src/goland_newproject/main.go:128 +0x2ca //line 128: go parallelSumError(pcr), the calling function

So what's the problem here? it seems summing is not a good example for paralleled for-loop, but actually I wish to know how to use channel inside paralleled for-loop.

func parallelSum (c chan int){
    sum := make(chan int)
    for i :=1 ; i< 100;i++{
        go func(i int){
            temp := <- sum //error here why?
            temp += i
            sum <- temp
        }(i)
    }
    time.Sleep(1*time.Second)
    temp := <-sum
    c <- temp
}

both with the same main function

func main(){
    pc := make(chan int)
    go parallelSum(pc) 
    result = <- pc
    fmt.Println("parallel result:", result)
}
  • 写回答

2条回答 默认 最新

  • dro60731 2019-02-12 13:24
    关注

    I don't like the idea of summing numbers through channels. I'd rather use something classical like sync.Mutex or atomic.AddUint64. But, at least, I made your code working. We aren't able to pass a value from one channel to another (I added temp variable). Also, there is sync.WaitGroup and other stuff. But I still don't like the idea of the code.

    package main
    
    import (
    "fmt"
    "sync"
    )
    
    func main() {
        pc := make(chan int)
        go parallelSum(pc)
        result := <- pc
        fmt.Println("parallel result:", result)
    }
    
    
    func parallelSum (c chan int){
        sum := make(chan int)
    
    
        wg := sync.WaitGroup{}
        wg.Add(100)
    
        for i :=1 ; i <= 100;i++{
            go func(i int){
                temp := <- sum
                temp += i
                wg.Done()
    
                sum <- temp
            }(i)
        }
    
        sum <- 0
    
        wg.Wait()
        temp := <- sum
        c <- temp
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败