Re: using dot_product from c++



utab wrote:
Dear all,

I tried to write a very simple subroutine where two vector are dot
producted.

Compiles and links fine but, then which a bad_alloc exception in from c
++ side.

Could someone comment on this?

gfortran -c vecpro.f90
g++ -c vecprod.cc
g++ -o test vecpro.o vecprod.o -lg2c

nm vecpro.o

results in

vectorproduct_

and execution results in

./test

terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc
Aborted

Codes:

subroutine vectorProduct(res,vec1,vec2,n)
implicit none
integer ::n ! dimension of the
vector
real, dimension(:) ::vec1,vec2 ! dimension of the vectors are

Assumed shape arrays are incompatible with mixed language use unless you code explicitly for the descriptor on the other side, which will be highly nonportable (even from one version of a compiler to another, let alone from one compiler to another).

The correct declaration would be:
REAL, DIMENSION(n) :: vec1, vec2

(as it is, you're not using n at all.)

real res ! result of the
operation
res=dot_product(vec1,vec2) ! dot_product is used to compute the
! result
end subroutine vectorProduct
#include <iostream>
#include <vector>
#include <string>

using namespace std;

extern "C" {
void vectorproduct_(double &,std::vector<double> &,
std::vector<double> &, int &);
}

A Fortran array is *highly* unlikely to be compatible with a C++ std::vector. Furthermore, Fortran parameters are much more likely to be compatible with C/C++ pointers than with C references. The correct prototype would be:

extern "C" void vectorproduct_(double*, double*, double*, int*);

int main(){

int sz;
vector<double> a(5),b(5);

sz=a.size();
int i=1,j=4;
double result;

for(;i!=sz+1;++i){
a.push_back(i);
++i;
}
for(;j!=sz+1;++j){
b.push_back(j);
++j;
}

This code is not doing what I believe you think it's doing. At the end of it, a and b are both length 10, containing;
0 0 0 0 0 1 2 3 4 5

while the value of sz is still 5.

You should either not initialize the vectors (leaving them in the default size of 0) or you should not use push_back but instead reference the location you wish to access directly. (Or, in the third alternative, there may be a standard algorithm that will allow you to perform the initialization on declaration, but a) it will ultimately just reduce to the loop structure, and b) it will be much more opaque to the unfamiliar reader.)

Don't set sz until you're done modifying the size of a and b.

vectorproduct_(result,a,b,sz);

Then, the correct call is:
vectorproduct_(&result, &a[0], &b[0], &sz);

std::cout << result << std::endl;
return 0;
}

I know that vectors are aligned in memort continuously, but could not
figure the problem out, I guess it is sth related to the memory since
I am getting an error related to bad_alloc.

The bad_alloc is a little surprising, I would have expected a simple segfault. I guess maybe it arises because the misunderstandings between the codes multiply to put the memory manager out of whack.
.



Relevant Pages

  • Re: DPROD issues
    ... a switch like that typically ... makes a compiler nonstandard in that mode. ... treatment of specific intrinsics is one ... subroutine sub1a ...
    (comp.lang.fortran)
  • Re: Jumping into block of an if construct
    ... (For that matter a clever enough compiler could replace this PUT DATA ... routine which itself executes the loops around element handling. ... Either way I think the cost of element handling will usually ... So locally based on subroutine arguments, but not on, for example, ...
    (comp.lang.fortran)
  • Re: Bus error/ segmentation fault--help?
    ... When I compile with the intel fortran compiler, ... This subroutine integrates the function y3 up one step, ... implicit double precision (a-h, o-z) ...
    (comp.lang.fortran)
  • Re: Question about name conflicts in Fortran
    ... I am hacking the g95 compiler so that it dumps the structure ... Subroutine foo ... The para after the 2nd numbered list in 16.2 covers the case where the ... One of the exceptions is "a generic name may be the ...
    (comp.lang.fortran)
  • Re: abstract sub programs overriding
    ... >>compiler can determine that Object is never referenced before it is ... >>implicit Finalize. ... >>the Initialize is user defined. ... > if that initial value is an aggregate, it has to be built directly in the ...
    (comp.lang.ada)