AFF --- A container for numbers (array) by Friederich and Forbriger.

◆ main()

int main ( )

test array interface to Fortran 77

See also
Interfacing Fortran 77

Definition at line 97 of file f77test.cc.

References AFF_F77TEST_CC_VERSION, CODE, DUMP, aff::dump_array(), f77interface::fill(), f77interface::fillarray(), section(), aff::slice(), aff::subarray(), f77interface::sums(), and f77interface::viewcommon().

98 {
99  cout << AFF_F77TEST_CC_VERSION << endl;
100 
101  // First we test the shape class that should support passing arrays to
102  // Fortran 77 functions
103  section("FortranShape:", '=');
104  {
105  section("Full Layout");
106  // we create a test array with known Fortran layout
107  CODE(Strided strided(Shaper(1,10,20)(1,5,10)(1,30)(20)));
108  // dump this shape
109  DUMP(strided);
110  // create Fortran shape from this (should be identical to known)
111  CODE(aff::util::FortranShape fs1(strided));
112  // and dump this
113  DUMP(fs1.first());
114  DUMP(fs1.last());
115  DUMP(fs1.dimlast());
116  cout << "fs1.offset(): " << fs1.offset() << endl;
117 
118  section("Sliced Subshape");
119  // now take shape of a subarray
120  CODE(Strided subshape(strided));
121  CODE(subshape.shrink(0,2).shrink(1,3,5).shrink(3,10,20));
122  CODE(subshape.collapse(2,15));
123  // create Fortran shape from this
124  CODE(aff::util::FortranShape fs2(subshape));
125  // and dump this
126  DUMP(fs2.first());
127  DUMP(fs2.last());
128  DUMP(fs2.dimlast());
129  cout << "fs2.offset(): " << fs2.offset() << endl;
130  }
131 
132  /*----------------------------------------------------------------------*/
133 
134  section("Pass array to Fortran via subroutine arguments:", '=');
135  {
136  // create an array and fill it
137  CODE(Array<int> A(Shaper(-3,3)(9)(-1,1)));
138  CODE(A=-55);
139  // create a subarray view and fill this through Fortran
140  CODE(Array<int> B=subarray(A)(-2,2)(3,7)(0));
142  // dump the result
143  CODE(dump_array(A));
144  // do it again for a slice
145  CODE(f77interface::fill(slice(A)()(2)));
146  CODE(dump_array(A));
147  }
148 
149  /*----------------------------------------------------------------------*/
150 
151  section("Access to common block:", '=');
152  {
153  // prepare to vectors to pass to fillarray
154  CODE(Array<float> v1(5));
155  CODE(Array<float> v2(3));
156  CODE(for(int i=v1.f(0); i<=v1.l(0); i++) { v1(i)=2.*i; });
157  CODE(for(int i=v2.f(0); i<=v2.l(0); i++) { v2(i)=.5*i; });
158  // fill common block through Fortran 77 subroutine
159  CODE(f77interface::fillarray(v1, v2));
160  // get a view on the common block and dump it
162  CODE(dump_array(Z));
163  // call Fortran subroutine sum and dump result
165  CODE(typedef f77interface::Tzarray::Tvalue Tzvalue);
166  // write directly to common block through a subarray
167  CODE(subarray(Z)(2,4)=Tzvalue(-10.));
168  // and dump the effect
169  CODE(dump_array(Z));
171  }
172 
173  /*----------------------------------------------------------------------*/
174 
175  section("Size-checked casts:", '=');
176  {
177  CODE(typedef std::complex<int> Ticvalue);
178  CODE(typedef std::complex<float> Tcvalue);
179  CODE(Array<Ticvalue> v1(1));
180  CODE(ConstArray<Ticvalue> v2(v1));
181  CODE(FortranArray<Array<Ticvalue> > fv1(v1));
183  CODE(v1(1)=Ticvalue(3,7));
184  CODE(cout << v1(1) << ", " << v2(1) << endl);
185  CODE(Ticvalue *icp=fv1.castedpointer<Ticvalue>());
186  CODE(*icp=Ticvalue(35,60));
187  CODE(cout << v1(1) << ", " << v2(1) << endl);
188  CODE(const Ticvalue *cicp1=fv1.castedpointer<const Ticvalue>());
189  CODE(const Ticvalue *cicp2=fv2.castedpointer<const Ticvalue>());
190  CODE(cout << *cicp1 << ", " << *cicp2 << endl);
191  section("That's dangerous:",' ');
192  CODE(Tcvalue *cp=fv1.castedpointer<Tcvalue>());
193  CODE(*cp=Ticvalue(35,60));
194  CODE(cout << v1(1) << ", " << v2(1) << endl);
195  CODE(double *dp=fv1.castedpointer<double>());
196  CODE(*dp=35.e12);
197  CODE(cout << v1(1) << ", " << v2(1) << endl);
198 
199  section("Test illegal usage (only if activated through macro-definition):",
200  ' ');
201  CODE(Array<int> iv1(1));
202  CODE(ConstArray<int> iv2(iv1));
203  CODE(FortranArray<Array<int> > fiv1(iv1));
204  CODE(FortranArray<ConstArray<int> > fiv2(iv2));
205  CODE(iv1(1)=50);
206  CODE(cout << iv1(1) << ", " << iv2(1) << endl);
207  CODE(int *iv1p=fiv1.pointer());
208  CODE(const int *iv2p=fiv2.pointer());
209  CODE(cout << *iv1p << ", " << *iv2p << endl);
210 #ifdef ILLEGAL1
211 #warning intentionally compiling illegal code:
212 #warning direct discard of const qualifier in conversion from non-const
213  CODE(int *ip1=fiv1.castedpointer<const int>());
214 #endif
215 #ifdef ILLEGAL2
216 #warning intentionally compiling illegal code:
217 #warning direct discard of const qualifier in conversion from const array
218  CODE(int *ip2=fiv2.castedpointer<const int>());
219 #endif
220 #ifdef ILLEGAL3
221 #warning intentionally compiling illegal code:
222 #warning discards const in conversion (reinterpret_cast)
223  CODE(int *ip3=fiv2.castedpointer<int>());
224 #endif
225 #ifdef ILLEGAL4
226 #warning intentionally compiling illegal code:
227 #warning direct type mismatch
228  CODE(float *ip4=fiv1.castedpointer<int>());
229 #endif
230 #ifdef ILLEGAL5
231 #warning intentionally compiling illegal code:
232 #warning wrong type size in conversion through reinterpret_cast
233  CODE(double *ip5=fiv1.castedpointer<double>());
234 #endif
235  }
236 
237 } // main
Shape for a rectangular array layout.
Definition: strided.h:117
aff::util::Slice< C > slice(const C &c)
Wrapper function to select correct type.
Definition: slice.h:158
#define DUMP(A)
Dump any object through its dump function.
Definition: dump_macros.h:62
aff::util::Subarray< C > subarray(const C &c)
Wrapper function to select correct type.
Definition: subarray.h:163
int fillarray(const aff::Array< float > &v1, const aff::Array< float > &v2)
fill common block through Fortran subroutine
#define CODE(C)
Dump code and execute (works like echo)
Definition: dump_macros.h:82
void dump_array(const ConstArray< T > &array, const Tdim &i=(Strided::Mmax_dimen-1), std::ostream &os=std::cout)
Dump array values.
Definition: dump_array.h:157
Full multi-dimensional array functionality.This is the full array class template. It adds no addition...
Definition: array.h:151
#define AFF_F77TEST_CC_VERSION
Definition: f77test.cc:64
Tzarray viewcommon()
create view from common
Class to provide Fortran interface values.
Definition: fortranshape.h:107
find appropriate leading dimensions
Definition: fortranshape.h:64
T Tvalue
Element type.
Definition: array.h:403
Shaper class for Fortran layout.
Definition: shaper.h:66
void section(const char *s, const char l='-')
print headline
Definition: f77test.cc:83
int fill(const aff::Array< int > &a)
interface function to Fortran77 subroutine fill
Definition: f77interface.cc:89
Array base classThis is a multidimensional (array) container that uses a strided memory layout (Fortr...
Definition: array.h:172
Tcarray sums()
read from common block through Fortran subroutine
Here is the call graph for this function: