How do you declare global variables? 
Author Message
 How do you declare global variables?

Hi all,

I'm just getting into using fortran and now I have a need that neither I nor
my materials-engineer boss with years&years of Fortran 77 (MS PowerStation)
experience can answer.

I'm building a static library of Fortran routines that take in parameters
and build arrays of REAL*4's and INTEGER*4's for consumption by a C++ class.
I plan to return integer POINTER's to the array elements for C++ to use.

We're using Fortran because the object code is 6 to 100 times as fast as C++
of VB would be. Integrating them is trickier than I'd like it to be.

In C, I would do this in a program module, and then link it into my other
modules. How does this stuff look in Fortran? (DVF 6 if you can...):
//////
static float *floatArray;
static long int floatArrayElems;

void AllocateFloatArray(long numElements)
{
    floatArray = malloc(sizeof(float)*numElements);
    floatArrayElems=numElements;

Quote:
}

void ComputeAndFillFloatArray(float parm1, float parm2, float parm3, long
*arrayLoc)
{
    long elementCount;
    // use the parameters to compute the array contents
    // set up the element count in elementCount
    // and so forth...
    if (floatArray != NULL) { free(floatArray); }
    AllocateFloatArray(elementCount);
    // fill the array here....
    // and return the array pointer thusly
    *arrayLoc = (long) floatArray;

Quote:
}

Rob


Wed, 18 Jun 1902 08:00:00 GMT  
 How do you declare global variables?

Quote:
>From: "Rob Perkins" <NO SPAM>

>years&years of Fortran 77
>In C, I would do this in a program module, and then link it into my other
>modules. How does this stuff look in Fortran? (DVF 6 if you can...):
>//////

For years and years, the standard ways in which C and f77 shared data were in
function arguments and results, and labeled COMMON, which appears as extern
struct in C on most systems where C and f77 are linkable.   Are you saying you
want something more modern?  Isn't it written up in the DVF documents?
Tim Prince



Wed, 18 Jun 1902 08:00:00 GMT  
 How do you declare global variables?

Quote:

> Hi all,

> I'm just getting into using Fortran and now I have a need that neither I nor
> my materials-engineer boss with years&years of Fortran 77 (MS PowerStation)
> experience can answer.

> I'm building a static library of Fortran routines that take in parameters
> and build arrays of REAL*4's and INTEGER*4's for consumption by a C++ class.
> I plan to return integer POINTER's to the array elements for C++ to use.

I must apologize for not dealing with your problem, but there are a couple of
comments that need to made.  REAL*n and INTEGER*n declarations were never
standard.  Your implementation may support them, but if you can use a standard
type you will be better off.

Quote:

> We're using Fortran because the object code is 6 to 100 times as fast as C++
> of VB would be.

And this makes no sense.  Fortran may be a very good choice for your problem,
and I will certainly not discourage that choice.  As distasteful as I find C++
(and even more VB) to be, this difference in efficiency indicates either that
you have an extremely bad C++ compiler or your C++ coding needs serious tuning.
For most applications you will find differences rarely exceeding 30% or so, and
that can go either way, depending on the application and implementation.

--




Wed, 18 Jun 1902 08:00:00 GMT  
 How do you declare global variables?
Well, the best I've come up with is to declare the static globals I want in
a MODULE subprogram, and then USE the module in the subroutines I want to
access, thusly. Is it correct? Will I get any memory leaks? Any comments?

Rob

!***************** Begin source *************
MODULE gArrays
     REAL*4, POINTER :: THEARRAY(:)
END MODULE gArrays

SUBROUTINE FOO(ARG)
USE gArrays
IMPLICIT NONE
!DEC ATTRIBUTES STDCALL :: FOO
!DEC ATTRIBUTES REFERENCE :: ARG
INTEGER*4 :: ARG
INTENT(INOUT) :: ARG

REAL*4 ARRAYPOINTER
POINTER(P,ARRAYPOINTER)

     NULLIFY(THEARRAY) ! initializes the pointer array declared in the
gArrays module; this is kind of a "safe" DEALLOCATE for POINTER type
variables.
     ALLOCATE(THEARRAY(3)) ! or some such, the point is to execute ALLOCATE
     P=LOC(THEARRAY) ! I'm not completely sure this is necessary, but it
works.
     ARG = P

RETURN
END SUBROUTINE FOO
!***************** End source ***************

Inline...

Quote:

>I must apologize for not dealing with your problem, but there are a couple
of
>comments that need to made.  REAL*n and INTEGER*n declarations were never
>standard.  Your implementation may support them, but if you can use a
standard
>type you will be better off.

They're the types my boss has been using in his MS Powerstation code. It's
more economical to stay in sync with him than with any other standard (I get
to keep my job, etc)

Quote:

>> We're using Fortran because the object code is 6 to 100 times as fast as
C++
>> of VB would be.

>And this makes no sense.  Fortran may be a very good choice for your
problem,
>and I will certainly not discourage that choice.  As distasteful as I find
C++
>(and even more VB) to be, this difference in efficiency indicates either
that
>you have an extremely bad C++ compiler or your C++ coding needs serious
tuning.
>For most applications you will find differences rarely exceeding 30% or so,
and
>that can go either way, depending on the application and implementation.

We're not "most applications". You'll find that if you implement finite
difference analysis code, or iso-surface shape generation code, or anything
on several million elements of a 3-D array, then that 30% difference can
shave hours off of an analysis.

It's much worse in VB. I'm sorry you find C++ and VB so distasteful. I'm
kind of locked into using them, mostly because that's the fastest way to get
an app out the door for Windows and WinNT. That, and this is my second week
of exposure to Fortran.

- Show quoted text -

Quote:

>--





Wed, 18 Jun 1902 08:00:00 GMT  
 How do you declare global variables?

Quote:

>For years and years, the standard ways in which C and f77 shared data were
in
>function arguments and results, and labeled COMMON, which appears as extern
>struct in C on most systems where C and f77 are linkable.   Are you saying
you
>want something more modern?  Isn't it written up in the DVF documents?
>Tim Prince


No, what I'm saying is I want the Fortran 90 equivalent of the "static"
module variable, which is functionally the opposite of an "extern"-declared
variable or function. "static" implies a module-scope variable invisible to
other linked modules in the program. I had planned to pass pointers to data
between C++ and Fortran code subroutines.

COMMON blocks cannot contain ALLOCATE-able arrays and such, which I need. I
imagine it's written up in the DVF documents, but the documentation doesn't
lend itself well to a C++ programmer transitioning in. Do I want to use the
MODULE construct, coupled with USE in a SUBROUTINE declaration?

Rob



Wed, 18 Jun 1902 08:00:00 GMT  
 How do you declare global variables?

...

Quote:
>No, what I'm saying is I want the Fortran 90 equivalent of the "static"
>module variable, which is functionally the opposite of an "extern"-declared
>variable or function. "static" implies a module-scope variable invisible to
>other linked modules in the program. I had planned to pass pointers to data
>between C++ and Fortran code subroutines.

C (and C++) overuse the keyword 'static' to mean several different things
depending on context.  In this case, I believe that what you want is a variable
that's visible to all the procedures within a MODULE but is not visible
outside that MODULE.  The Fortran keyword for that concept is PRIVATE.
Note that in C (and C++) a 'static' variable is also preserved from one
invocation of a module to another.  To achieve this in Fortran, you must use
the SAVE attribute.

Quote:
>COMMON blocks cannot contain ALLOCATE-able arrays and such, which I need. I
>imagine it's written up in the DVF documents, but the documentation doesn't
>lend itself well to a C++ programmer transitioning in. Do I want to use the
>MODULE construct, coupled with USE in a SUBROUTINE declaration?

Yes.  The PRIVATE attribute is only meaningful within MODULEs.  Another
way is to build a MODULE with no PRIVATE attributes and then reference
it with the USE ... ONLY form in the subroutines that need the MODULE.
This doesn't actually prevent someone from referencing the variable outside
the MODULE like the PRIVATE attribute would, but it allows you not to
see that variable if you choose not to.  In any case, look up these concepts
in the manual and you'll be on the right track.

--
J. Giles



Wed, 18 Jun 1902 08:00:00 GMT  
 How do you declare global variables?

Quote:
>Another way is to build a MODULE with no PRIVATE attributes and then
reference
>it with the USE ... ONLY form in the subroutines that need the MODULE.
>J. Giles

Thanks. That's what I ended up doing. Are there any memory leaks in this
code?

MODULE gVars
   real*4 facets,AngleIncr,pi,arrayPointerVar
      real*4, ALLOCATABLE :: VERTICES(:)
   integer*4 NumVertices
   integer*4 ArraySize
   integer*4 ArrayCounter
   POINTER(P,arrayPointerVar)
END MODULE gVars

SUBROUTINE VERTEXARRAY(arrayPointer, arrayElems, vertexCount)
USE gVars
INTEGER*4 :: arrayPointer
INTEGER*4 :: arrayElems
INTEGER*4 :: vertexCount
INTENT(INOUT) :: arrayPointer
INTENT(INOUT) :: arrayElems
INTENT(INOUT) :: vertexCount

 P=LOC(VERTICES)
 arrayPointer=P
 arrayElems = arraySize
 vertexCount = NumVertices

END SUBROUTINE VERTEXARRAY

SUBROUTINE CYLINDERX(x0,y0,z0,r,len,xc,yc,zc,xrot,yrot,zrot)
USE gVars
REAL*4 :: x0,y0,z0,r,len,xc,yc,zc,xrot,yrot,zrot

REAL*4 :: xbot,ybot,zbot,xtop,ytop,ztop

facets = 36 ! This makes a facet every 10 degrees
pi = 3.141592627
AngleIncr = 2.*pi/facets
ArraySize = facets*4*3*3 ! 4 triangles per facet
    ! 3 vertices per triangle
    ! 3 REAL*4's per vertex

! OK, DEALLOCATE and ALLOCATE the array to the proper size.
 IF (ALLOCATED(VERTICES)) DEALLOCATE(VERTICES)
 ALLOCATE(VERTICES(ArraySize))
 ArrayCounter = 1

! do other stuff with the array

END SUBROUTINE CYLINDERX



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Where declaring global variables when using exe + dll's with different data-dictionaries

2. Strange behavior in C-declared global variable?

3. ?Accessing Global variables (declared in C) in ASM subroutine

4. How to declare a global variable

5. declaring global variables

6. Declaring global variable

7. Class variables / global variables / Init variables

8. Automatically declaring a global object

9. Declaring global data for subapplications

10. Dll Declared Global Class

11. Declaring global and static

12. global declare

 

 
Powered by phpBB® Forum Software