For parallelizing code like this, you will probably see better performance by 
having each routine process a full row rather than a cell. You amortize the 
overhead better. 

Still the matrix is probably too small to make a difference. 

> On Feb 19, 2021, at 8:56 AM, 'Peter Weinberger ' via golang-nuts 
> <[email protected]> wrote:
> 
> 
> A 100-long vector is way too short to show any benefit. goroutines have 
> start-up, scheduling, and shut-down costs. What happens if you try to square 
> the largest matrix you can fit into memory (10,000 by 10000?)
> 
>> On Fri, Feb 19, 2021 at 9:46 AM Yuwen Dai <[email protected]> wrote:
>> Hi experts,
>> 
>> I'm a newbie to golang.  One of my first ideas to use goroutine is to write 
>> a matrix multiplying programme:  C = A*B.    I though the calculating of  
>> every element of C:  c[i][j] = row i of A  *  column j of B could be run by 
>> a goroutine.  This is the skeleton of the code:
>> 
>>     t1 := time.Now().UnixNano()  //benchmark
>>     rand.Seed(t1)
>>     for i := 0; i < n; i++ {
>>         ai,_ := get_row(a,i)
>>         for j := 0; j < t; j ++ {
>>             bj, _ := get_column(b,j)
>>             //        c[i][j],_ = dot_product(ai, bj)
>>             go func(element *int, ai,bj []int) {
>>                 *element,_ = dot_product(ai,bj)
>>                 wg.Done()
>>             }(&c[i][j], ai, bj)
>>         }
>>     }
>> 
>>     wg.Wait()  // waiting for all the elements have been calculated
>>     t2 := time.Now().UnixNano()
>>     fmt.Printf("the dot_product using goroutine costs %v\n", t2 - t1)
>> 
>> As the goroutines will run "concurrently" on my laptop with 8 CPU cores to 
>> calculate the element of matrix, I thought the code would ran faster than 
>> not using goroutine.  In fact, it ran slowlier than not using goroutine.  
>> Any explanation of this?    By the way,  the dimension of  matrix is 
>> 100x100.  
>> 
>> Best regards,
>> Yuwen
>> -- 
>> 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 [email protected].
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/52ef6c0f-7044-4b7e-968c-b894eb52d374n%40googlegroups.com.
> 
> -- 
> 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 [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/CAOUkXSpvBq9ctKGwXC4VHT-xfBm2j4vsODnYgUgeO_ivbG-uSw%40mail.gmail.com.

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/22427044-434A-4C82-9CF8-F9C9846C6B6E%40ix.netcom.com.

Reply via email to