gpt4 book ai didi

c - 将 C 翻译成 Golang。如何分配内存以匹配 C?

转载 作者:数据小太阳 更新时间:2023-10-29 03:23:05 24 4
gpt4 key购买 nike

所以我正在翻译我在 C 中创建的程序。这个程序的目标是简单地从文件中读取矩阵,以稀疏行格式压缩矩阵,然后计算矩阵 vector 乘积。

这是 C 语言的程序片段。

//Read the MatrixMarket file and initialize a CSR formatted matrix.
csr_load_matrix(fileName, &compressedSparseMatrix);
//Set the correct values to the struct and create the memory allocation.
double *x;
double *y;

x = malloc(compressedSparseMatrix.cols * sizeof(double));
y = malloc(compressedSparseMatrix.rows * sizeof(double));

//initialize the vector x
for(int i = 0; i < compressedSparseMatrix.cols; i++){
x[i]= i+1;
}

//Calculate how long it takes to find the Matrix-Vector Product for the CSR Matrix.
//Start the timer.
clock_t begin = clock();
for (int i = 0; i < iterations; i++) {
csrMVP(&compressedSparseMatrix, x, y);
}
clock_t end = clock();
double totalTimeSpent = (double) (end - begin) / CLOCKS_PER_SEC;

这是我的 Main.c,我的 csrMVP 函数附在下面:

int csrMVP(CSR_Matrix *funcMatrix, double *x, double *y){
unsigned int i, j;
unsigned int k = 0;
double *value = funcMatrix->val;
unsigned int *column = funcMatrix->col;
unsigned int *rowPointer = funcMatrix->ptr;
double hold;
for(i = 0; i < funcMatrix->rows; i++){
hold = 0.0;
j = k;
k = rowPointer[i+1];
for(j; j < k; j++){
y[i] = y[i] + value[j] * x[i];
}

}
}

我的程序在 C 中完美运行。这里的想法是我想计算 MVP 1000 次,看看我的编译器能做到多快/多慢。

所以现在我的下一个目标是在 GoLang 中使用并发来比较 Golang 执行 MVP 过程 1000 次的速度。

这是我的 Golang 程序的开始:(为了空间的缘故假装 []float64{...} 是一个大矩阵。)

func main(){
var interation int

m := mat64.NewDense(32, 32, []float64{...})

//csr_print_matrix(m)
//m.CSRPrint()

//var y []float64
y := make([]float64, 32)
x := make([]int, 32)
for i := range x {
x[i] = i + 1
}

interation = 2
start := time.Now()
for i := 0; i < interation; i ++ {
m.CSRMVP(x, y)
}
elapsed := time.Since(start)
log.Printf("Matrix took %s", elapsed)


}

我的 m.CSRMVP() 函数是:

func (m *Dense) CSRMVP(x []int, y []float64){
fmt.Println("Value of Y:")
var j,end, newY int
var values []float64
var col_indices, row_ptr []int
values = m.mat.Data
end = 0

for i := 0; i < m.capCols; i++{
y[i] = 0.0
newY = int(y[i])
j = end
end = row_ptr[i+1]
for ; j < end; j++ {
newY = newY + int(values[j]) * x[col_indices[j]]
fmt.Print(newY)
y[i] = float64(newY)
}
}
}

问题似乎是传递变量 x 和 y 的内存分配问题。

这是我的程序只运行一次 MVP 的输出。

panic: runtime error: index out of range

goroutine 1 [running]:
github.com/gonum/matrix/mat64.(*Dense).CSRMVP(0xc42001e100, 0xc420055d50, 0x20, 0x20, 0xc420055e50, 0x20, 0x20)
/Users/jeanmac/go/src/github.com/gonum/matrix/mat64/dense.go:573 +0x215
main.main()
/Users/jeanmac/go/src/matrices/main.go:75 +0x16c

我仍在学习 Go。我的目标只是看看什么更快。我相信我的错误是由于 x 和 y 的内存分配不正确造成的。在 C 中,我调用 Malloc 来为这些变量创建我需要的内存,但我不确定 Go 的替代方案是什么。

dense.go中第573行对应:end = row_ptr[i+1]main.go中第75行对应:m.CSRMVP(x, y)

最佳答案

您正在使用 github.com/gonum/matrix/mat64。它已过时:

https://github.com/gonum/matrix/

Gonum Matrix

This repository is no longer maintained. Development has moved to https://github.com/gonum/gonum.

关于c - 将 C 翻译成 Golang。如何分配内存以匹配 C?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49785854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com