将c数组作为可变大小的矩阵传递给fortran [英] Passing c arrays into fortran as a variable sized matrix

查看:126
本文介绍了将c数组作为可变大小的矩阵传递给fortran的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我被委托将一些fortran子程序翻译成C语言。这些子程序被称为主要基于C语言的大型语句的控制流程的一部分。



我正在面对的问题是手部问题从C到fortran的数组数据



假设我们在c中声明了一个数组

  int * someCArray =(int *)malloc(50 * 4 * sizeof(int)); 

现在,这个数组需要被传递到Fortran子程序中以填充数据

  someFortranFunc(someCArray,someOtherParams); 

当数组到达fortran land时,它被声明为一个可变大小的矩阵: (b,b)

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ >

问题在于fortran似乎没有正确确定数组的大小,因为程序seg-faults。当我调试程序时,我发现索引到

  somefarray(1,2)

报告这是一个无效索引。任何对第一列中任何项目的引用都可以正常工作,但是当它到达fortran时,数组中只有一列可用。



我无法真正改变事实上,这是一个fortran中可变大小的数组。任何人都可以解释这里发生了什么,有没有一种方法可以缓解C方面的问题?



[edit]



顺便说一下,fortran子程序被替换的fortran代码调用为

 整数somedatastorage(plentybignumber)
integer someindex
...
call somefarray(somedatastorage(someindex))

其中数据存储是一个很大的1d阵列。超出数据存储的大小没有问题。不过,不管怎样,传递C数组和Fortran(子)数组之间的差异导致fortran子例程中的差异。



谢谢!

解决方案

它不应该是别人建议的:
1. int的大小与Integer的大小。由于第一列有正确的值。
2.行列排序。只会以错误的顺序获取值而不会分段错误。
3.参考值与值传递。由于第一列有正确的值。除非编译器在你背后做了一些邪恶的事情。



你确定你不以某种秘密方式做这件事:

  someCArray ++ 

打印出在你通过它之后,就在你刚刚通过它之后立即使用someCArray指针。您还应该使用Fortran代码中的调试器将其打印出来,以验证编译器是否生成一些临时副本来帮助您。


So, i've been commissioned to translate some fortran subroutines into C. These subroutines are being called as part of the control flow of a large porgram based primarily in C.

I am translating the functions one at a time, starting with the functions that are found at the top of call stacks.

The problem I am facing is the hand-off of array data from C to fortran.

Suppose we have declared an array in c as

int* someCArray = (int*)malloc( 50 * 4 * sizeof(int) );

Now, this array needs to be passed down into a fortran subroutine to be filled with data

someFortranFunc( someCArray, someOtherParams );

when the array arrives in fortran land, it is declared as a variable sized matrix as such:

subroutine somefortranfunc(somecarray,someotherparams)
integer somefarray(50,*)

The problem is that fortran doesn't seem to size the array correctly, becuase the program seg-faults. When I debug the program, I find that indexing to

somefarray(1,2)

reports that this is an invalid index. Any references to any items in the first column work fine, but there is only one available column in the array when it arrives in fortran.

I can't really change the fact that this is a variable sized array in fortran. Can anyone explain what is happening here, and is there a way that I can mitigate the problem from the C side of things?

[edit]

By the way, the fortran subroutine is being called from the replaced fortran code as

integer somedatastorage(plentybignumber)
integer someindex
...
call somefarray(somedatastorage(someindex))

where the data storage is a large 1d array. There isn't a problem with overrunning the size of the data storage. Somehow, though, the difference between passing the C array and the fortran (sub)array is causing a difference in the fortran subroutine.

Thanks!

解决方案

What it shouldn't be that others suggested: 1. Size of int vs. size of Integer. Since the first column has the right values. 2. Row vs. column ordering. Would just get values in wrong order not segmentation faulted. 3. Reference vs value passing. Since the first column has the right values. Unless the compiler is doing something evil behind your back.

Are you sure you don't do this in some secret way?:

   someCArray++

print out the value of the someCArray pointer right after you make it and right before you pass it. You also should print it out using the debugger in the fortran code just to verify that the compiler is not generating some temporary copies to help you.

这篇关于将c数组作为可变大小的矩阵传递给fortran的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆