使用goroutines和通道的“矩阵乘法”

时间:2019-12-01 17:33:28

标签: go matrix-multiplication channel goroutine

我有一个大学项目,当我使用1个goroutine,2个goroutine,3个等等时,测试矩阵乘法的时间差。我必须使用频道。我的问题是,不管我添加多少go例程,编译时间几乎总是相同。也许有人可以说出问题所在。也许发送时间很长,而且发送时间很长。代码在下面给出

ArrayList

2 个答案:

答案 0 :(得分:0)

您的代码很难遵循(将变量dummy1 / dummy2调用时会感到困惑,尤其是当它们在Calc中获得不同的名称时),并且添加一些注释将使其更容易理解。

首先是一个错误。发送要计算的数据后,您dummy1 <- -1相信您会等待所有计算完成。但是,当您有多个goroutine时,不一定是这种情况。该通道将被其中一个goroutine耗尽,并打印出定时信息。其他goroutine仍将运行(并且可能尚未完成其计算)。

就时间安排而言,我怀疑将数据发送到go例程的方式会使速度变慢;您先发送行,然后发送列;因为未缓冲通道,所以goroutine将在等待列时阻塞(切换回主goroutine发送列)。这样反复来回会降低您的goroutine获取数据的速度,并且可以很好地解释为什么添加额外的goroutine影响有限(如果使用缓冲通道,也会变得很危险)。

我已经将您的代码(请注意可能存在错误,并且还远远不够完美!)重构为确实显示出差异的东西(在我的计算机上为1 goroutine = 10s; 5 = 7s):

package main

import (
    "fmt"
    "math/rand"
    "sync"
    "time"
)

const length = 1000

var start time.Time
var rez [length][length]int

// toMultiply will hold details of what the goroutine will be multiplying (one row and one column)
type toMultiply struct {
    rowNo    int
    columnNo int
    row      []int
    column   []int
}

func main() {
    const noOfGoRoutines = 5

    // Build up a matrix of dimensions (length) x (length)
    var a [length][length]int
    var b [length][length]int
    for i := 0; i < length; i++ {
        for j := 0; j < length; j++ {
            a[i][j] = rand.Intn(10)
            b[i][j] = rand.Intn(10)
        }
    }

    // Setup completed so start the clock...
    start = time.Now()

    // Start off threadlength go routines to multiply each row/column
    toCalc := make(chan toMultiply)
    var wg sync.WaitGroup
    wg.Add(noOfGoRoutines)
    for i := 0; i < noOfGoRoutines; i++ {
        go func() {
            Calc(toCalc)
            wg.Done()
        }()
    }

    // Begin the multiplication.
    start = time.Now()
    for i := 0; i < length; i++ {
        for j := 0; j < length; j++ {
            tm := toMultiply{
                rowNo:    i,
                columnNo: j,
                row:      make([]int, length),
                column:   make([]int, length),
            }

            for k := 0; k < length; k++ {
                tm.row[k] = a[i][j]
                tm.column[k] = b[i][k]
            }
            toCalc <- tm
        }
    }

    // All of the data has been sent to the chanel; now we need to wait for all of the
    // goroutines to complete
    close(toCalc)
    wg.Wait()

    fmt.Println("Binomial took ", time.Since(start))

    // The full result should be in tz
    for i := 0; i < length; i++ {
        for j := 0; j < length; j++ {
            //fmt.Print(rez[i][j])
            //fmt.Print(" ")
        }
        //fmt.Println(" ")
    }
}

// Calc - Multiply a row from one matrix with a column from another
func Calc(toCalc <-chan toMultiply) {
    for tc := range toCalc {
        var result int
        for i := 0; i < len(tc.row); i++ {
            result += tc.row[i] * tc.column[i]
        }
        // warning - the below should work in this case but be careful writing to global variables from goroutines
        rez[tc.rowNo][tc.columnNo] = result
    }
}

答案 1 :(得分:0)

您看不到任何区别,因为准备将数据传递给go例程是您的瓶颈。它比执行calc慢或快。

传递行和列的副本不是一个好策略。这会破坏性能。

go例程可以直接从输入矩阵中读取只读数据。这里没有可能的比赛条件。

与输出相同。如果go例程计算行和列的相乘,它将把结果写入不同的单元格中。这里也没有可能的比赛条件。

要做的事情如下。用两个字段定义一个结构,一个用于行,一个用于列以相乘。

用行和列的所有可能组合填充一个缓冲通道,以从(0,0)乘以(n-1,m-1)。

go例程,使用通道中的结构,执行计算并将结果直接写入输出矩阵。

然后,您还有一个完成的通道,向主go例程发信号通知计算已完成。当go例程完成对结构(n-1,m-1)的处理后,它将关闭完成的通道。

main go例程在编写完所有结构之后,在done通道上等待。关闭完工通道后,它将打印经过的时间。 我们可以使用一个等待组来等待所有go例程终止其计算。

然后,您可以开始执行一次go例程,并增加go例程的数量,以查看处理时间的影响。

查看代码:

package main

import (
    "fmt"
    "math/rand"
    "sync"
    "time"
)

type pair struct {
    row, col int
}

const length = 1000

var start time.Time
var rez [length][length]int

func main() {
    const threadlength = 1
    pairs := make(chan pair, 1000)
    var wg sync.WaitGroup
    var a [length][length]int
    var b [length][length]int
    for i := 0; i < length; i++ {
        for j := 0; j < length; j++ {
            a[i][j] = rand.Intn(10)
            b[i][j] = rand.Intn(10)
        }
    }
    wg.Add(threadlength)
    for i := 0; i < threadlength; i++ {
        go Calc(pairs, &a, &b, &rez, &wg)
    }
    start = time.Now()
    for i := 0; i < length; i++ {
        for j := 0; j < length; j++ {
            pairs <- pair{row: i, col: j}
        }
    }
    close(pairs)
    wg.Wait()
    elapsed := time.Since(start)
    fmt.Println("Binomial took ", elapsed)

    for i := 0; i < length; i++ {
        for j := 0; j < length; j++ {
            fmt.Print(rez[i][j])
            fmt.Print(" ")
        }
        fmt.Println(" ")
    }
}

func Calc(pairs chan pair, a, b, rez *[length][length]int, wg *sync.WaitGroup) {
    for {
        pair, ok := <-pairs
        if !ok {
            break
        }
        rez[pair.row][pair.col] = 0
        for i := 0; i < length; i++ {
            rez[pair.row][pair.col] += a[pair.row][i] * b[i][pair.col]
        }
    }
    wg.Done()
}
相关问题