Ejemplo n.º 1
0
func multiplyFast(A, B *etumag1matrix.Matrix) (*etumag1matrix.Matrix, error) {
	if A.CountCols() != B.CountRows() {
		return nil, errors.New("number of columns in A is not equal to the number of rows in B")
	}

	elements := make([]float64, A.Rows*B.Cols)
	stopch := make(chan struct{})

	proc := func(n int) {
		for m := n; m < len(elements); m += goroutines {
			i, j := (m-m%B.Cols)/B.Cols, m%B.Cols
			row := A.GetRow(i)
			col := B.GetCol(j)
			if len(row) == len(col) {
				for k := 0; k < len(row); k++ {
					elements[m] += row[k] * col[k]
				}
			}
		}
		stopch <- struct{}{}
	}

	for n := 0; n < goroutines; n++ {
		go proc(n)
	}

	for i := 0; i < goroutines; i++ {
		<-stopch
	}
	close(stopch)

	return etumag1matrix.NewMatrixFromSlice(A.CountRows(), B.CountCols(), elements), nil
}
Ejemplo n.º 2
0
func main() {
	var cols, rows int
	var addr string

	flag.IntVar(&cols, "cols", COLS, "cols num")
	flag.IntVar(&rows, "rows", ROWS, "rows num")
	flag.StringVar(&addr, "addr", ":8080", "address")

	flag.Parse()

	if len(os.Args) < 2 {
		fmt.Println("enter http addresses")
		os.Exit(1)
	}

	rand.Seed(time.Now().UTC().UnixNano())

	elements := make([]float64, cols*rows)
	for i := 0; i < cols*rows; i++ {
		elements[i] = rand.Float64()
	}

	matrix := etumag1matrix.NewMatrixFromSlice(cols, rows, elements)
	client, err := etumag1matrix.NewClient2(addr)

	fmt.Println("try to multiply matrix")

	start := time.Now()
	matrix2, err := client.MultiplyMatrix(matrix, matrix)
	fmt.Println("request time:", time.Since(start))

	if err != nil {
		fmt.Println("error:", err)
		os.Exit(1)
	}

	if matrix2.CountRows()*matrix2.CountCols() <= 100 {
		fmt.Println(matrix2)
	}

	// matrix, err = client.MultiplyMatrix(matrixFromSlice5, matrixFromSlice6)
	// if !matrix.Equals(res3) {
	// 	fmt.Println("ERROR")
	// } else {
	// 	fmt.Println("SUCCESS =)")
	// }
}
Ejemplo n.º 3
0
func main() {
	if len(os.Args) < 2 {
		fmt.Println("enter http addresses")
		os.Exit(1)
	}

	rand.Seed(time.Now().UTC().UnixNano())

	elements := make([]float64, COLS*ROWS)
	for i := 0; i < COLS*ROWS; i++ {
		elements[i] = rand.Float64()
	}

	matrix := etumag1matrix.NewMatrixFromSlice(COLS, ROWS, elements)
	client, err := etumag1matrix.NewClient(os.Args[1:]...)

	fmt.Println("try to multiply matrix")

	start := time.Now()
	matrix2, err := client.MultiplyMatrix(matrix, matrix)
	fmt.Println("time:", time.Since(start))

	if err != nil {
		fmt.Println("error:", err)
		os.Exit(1)
	}

	if matrix2.CountRows()*matrix2.CountCols() <= 100 {
		fmt.Println(matrix2)
	}

	matrix, err = client.MultiplyMatrix(matrixFromSlice5, matrixFromSlice6)
	if !matrix.Equals(res3) {
		fmt.Println("ERROR")
	} else {
		fmt.Println("SUCCESS ^_^")
	}
}
Ejemplo n.º 4
0
package main

import (
	"testing"

	"github.com/ivan1993spb/etumag1matrix"
)

var matrixFromSlice1 = etumag1matrix.NewMatrixFromSlice(3, 3, []float64{
	1, 2, 3,
	4, 5, 6,
	7, 8, 9,
})

var matrixFromSlice2 = etumag1matrix.NewMatrixFromSlice(3, 3, []float64{
	7, 8, 9,
	4, 5, 6,
	1, 2, 3,
})

var matrixFromSlice3 = etumag1matrix.NewMatrixFromSlice(3, 4, []float64{
	1, 2, 3,
	4, 5, 6,
	7, 8, 9,
	10, 11, 12,
})

var matrixFromSlice4 = etumag1matrix.NewMatrixFromSlice(4, 3, []float64{
	1, 4, 7, 10,
	2, 5, 8, 11,
	3, 6, 9, 12,
Ejemplo n.º 5
0
	"fmt"
	"math/rand"
	"os"
	"time"

	"github.com/ivan1993spb/etumag1matrix"
)

const (
	COLS = 100
	ROWS = 100
)

var matrixFromSlice5 = etumag1matrix.NewMatrixFromSlice(3, 6, []float64{
	1, 2, 3, 4, 5, 6,
	0, 0, 0, 0, 0, 0,
	7, 8, 9, 10, 11, 12,
})

var matrixFromSlice6 = etumag1matrix.NewMatrixFromSlice(6, 2, []float64{
	1, 1,
	2, 2,
	3, 3,
	3, 3,
	2, 2,
	1, 1,
})

var res3 = etumag1matrix.NewMatrixFromSlice(3, 2, []float64{
	42, 42,
	0, 0,