所以我正在翻译我在 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/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为