Commit dd89057c authored by thomas.forbriger's avatar thomas.forbriger Committed by thomas.forbriger
Browse files

now we can read and write blitz arrays!

This is a legacy commit from before 2015-03-01.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/trunk
SVN Revision: 1135
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent 79bc3501
# this is <Makefile>
# ----------------------------------------------------------------------------
# $Id: Makefile,v 1.10 2002-11-27 14:58:52 forbrig Exp $
# $Id: Makefile,v 1.11 2002-11-28 21:34:06 forbrig Exp $
#
# Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt)
#
......@@ -26,7 +26,8 @@
#
HEADERS=$(shell find . -name \*.h)
SRC=misc/fortranio.cc misc/commandline.cc misc/ioswap.cc misc/error.cc
SRC=misc/fortranio.cc misc/commandline.cc misc/ioswap.cc misc/error.cc \
misc/blitzfortranio.cc
TESTS=$(wildcard tests/*.cc)
FTESTS=$(wildcard tests/*.f)
README=$(shell find . -name README) README.xtra
......@@ -52,7 +53,7 @@ FLAGS=
CXXFLAGS=-fhonor-std -Wall $(FLAGS)
CXXFLAGS=-Wall $(FLAGS)
LDFLAGS=-L$(LOCLIBDIR)
CPPFLAGS=-I$(LOCINCLUDEDIR) $(FLAGS)
CPPFLAGS=-I$(LOCINCLUDEDIR) $(FLAGS) -I$(BLITZINCLUDE)
all: install-include libtfxx.a doxydoc
......
......@@ -121,7 +121,8 @@ EXPAND_ONLY_PREDEF = NO
SEARCH_INCLUDES = YES
INCLUDE_PATH =
INCLUDE_FILE_PATTERNS =
PREDEFINED =
PREDEFINED = DOXYGEN_MUST_SKIP_THIS \
TF_COMPLEX_ARRAY
EXPAND_AS_DEFINED =
#---------------------------------------------------------------------------
# Configuration::addtions related to external references
......
/*! \file blitzfortranio.cc
* \brief instantiate our magic numbers (implementation)
*
* ----------------------------------------------------------------------------
*
* $Id: blitzfortranio.cc,v 1.1 2002-11-28 21:34:07 forbrig Exp $
* \author Thomas Forbriger
* \date 28/11/2002
*
* instantiate our magic numbers (implementation)
*
* Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt)
*
* REVISIONS and CHANGES
* - 28/11/2002 V1.0 Thomas Forbriger
*
* ============================================================================
*/
#define TF_BLITZFORTRANIO_CC_VERSION \
"TF_BLITZFORTRANIO_CC V1.0 "
#define TF_BLITZFORTRANIO_CC_CVSID \
"$Id: blitzfortranio.cc,v 1.1 2002-11-28 21:34:07 forbrig Exp $"
#define TF_COMPLEX_ARRAY
#include <tfxx/blitzfortranio.h>
namespace tfxx {
namespace fortranio {
const char blitz_magic<float, 1>::magic[];
const char blitz_magic<float, 2>::magic[];
const char blitz_magic<float, 3>::magic[];
const char blitz_magic<float, 4>::magic[];
const char blitz_magic<float, 5>::magic[];
const char blitz_magic<double, 1>::magic[];
const char blitz_magic<double, 2>::magic[];
const char blitz_magic<double, 3>::magic[];
const char blitz_magic<double, 4>::magic[];
const char blitz_magic<double, 5>::magic[];
const char blitz_magic<int, 1>::magic[];
const char blitz_magic<int, 2>::magic[];
const char blitz_magic<int, 3>::magic[];
const char blitz_magic<int, 4>::magic[];
const char blitz_magic<int, 5>::magic[];
const char blitz_magic<std::complex<float>, 1>::magic[];
const char blitz_magic<std::complex<float>, 2>::magic[];
const char blitz_magic<std::complex<float>, 3>::magic[];
const char blitz_magic<std::complex<float>, 4>::magic[];
const char blitz_magic<std::complex<float>, 5>::magic[];
const char blitz_magic<std::complex<double>, 1>::magic[];
const char blitz_magic<std::complex<double>, 2>::magic[];
const char blitz_magic<std::complex<double>, 3>::magic[];
const char blitz_magic<std::complex<double>, 4>::magic[];
const char blitz_magic<std::complex<double>, 5>::magic[];
} // namespace fortranio
} // namespace tfxx
/* ----- END OF blitzfortranio.cc ----- */
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: blitzfortranio.h,v 1.3 2002-11-28 14:54:53 forbrig Exp $
* $Id: blitzfortranio.h,v 1.4 2002-11-28 21:34:07 forbrig Exp $
* \author Thomas Forbriger
* \date 28/11/2002
*
......@@ -23,11 +23,13 @@
#define TF_BLITZFORTRANIO_H_VERSION \
"TF_BLITZFORTRANIO_H V1.0 "
#define TF_BLITZFORTRANIO_H_CVSID \
"$Id: blitzfortranio.h,v 1.3 2002-11-28 14:54:53 forbrig Exp $"
"$Id: blitzfortranio.h,v 1.4 2002-11-28 21:34:07 forbrig Exp $"
#include<fstream>
#include<blitz/array.h>
#include<tfxx/fortranio.h>
#include<tfxx/ioswap.h>
#include<tfxx/misc.h>
#include<tfxx/error.h>
namespace tfxx {
namespace fortranio {
......@@ -48,43 +50,43 @@ struct blitz_magic {
}; // blitz_magic
//! partial specializations
template<> struct blitz_magic<float, 1> { static const magic[]="ZZf1"; };
template<> struct blitz_magic<float, 2> { static const magic[]="ZZf2"; };
template<> struct blitz_magic<float, 3> { static const magic[]="ZZf3"; };
template<> struct blitz_magic<float, 4> { static const magic[]="ZZf4"; };
template<> struct blitz_magic<float, 5> { static const magic[]="ZZf5"; };
template<> struct blitz_magic<double, 1> { static const magic[]="ZZd1"; };
template<> struct blitz_magic<double, 2> { static const magic[]="ZZd2"; };
template<> struct blitz_magic<double, 3> { static const magic[]="ZZd3"; };
template<> struct blitz_magic<double, 4> { static const magic[]="ZZd4"; };
template<> struct blitz_magic<double, 5> { static const magic[]="ZZd5"; };
template<> struct blitz_magic<int, 1> { static const magic[]="ZZi1"; };
template<> struct blitz_magic<int, 2> { static const magic[]="ZZi2"; };
template<> struct blitz_magic<int, 3> { static const magic[]="ZZi3"; };
template<> struct blitz_magic<int, 4> { static const magic[]="ZZi4"; };
template<> struct blitz_magic<int, 5> { static const magic[]="ZZi5"; };
template<> struct blitz_magic<float, 1> { static const char magic[]="ZZf1"; };
template<> struct blitz_magic<float, 2> { static const char magic[]="ZZf2"; };
template<> struct blitz_magic<float, 3> { static const char magic[]="ZZf3"; };
template<> struct blitz_magic<float, 4> { static const char magic[]="ZZf4"; };
template<> struct blitz_magic<float, 5> { static const char magic[]="ZZf5"; };
template<> struct blitz_magic<double, 1> { static const char magic[]="ZZd1"; };
template<> struct blitz_magic<double, 2> { static const char magic[]="ZZd2"; };
template<> struct blitz_magic<double, 3> { static const char magic[]="ZZd3"; };
template<> struct blitz_magic<double, 4> { static const char magic[]="ZZd4"; };
template<> struct blitz_magic<double, 5> { static const char magic[]="ZZd5"; };
template<> struct blitz_magic<int, 1> { static const char magic[]="ZZi1"; };
template<> struct blitz_magic<int, 2> { static const char magic[]="ZZi2"; };
template<> struct blitz_magic<int, 3> { static const char magic[]="ZZi3"; };
template<> struct blitz_magic<int, 4> { static const char magic[]="ZZi4"; };
template<> struct blitz_magic<int, 5> { static const char magic[]="ZZi5"; };
#ifdef TF_COMPLEX_ARRAY
#include<complex>
template<> struct blitz_magic<std::complex<float>, 1>
{ static const magic[]="ZZc1"; };
{ static const char magic[]="ZZc1"; };
template<> struct blitz_magic<std::complex<float>, 2>
{ static const magic[]="ZZc2"; };
{ static const char magic[]="ZZc2"; };
template<> struct blitz_magic<std::complex<float>, 3>
{ static const magic[]="ZZc3"; };
{ static const char magic[]="ZZc3"; };
template<> struct blitz_magic<std::complex<float>, 4>
{ static const magic[]="ZZc4"; };
{ static const char magic[]="ZZc4"; };
template<> struct blitz_magic<std::complex<float>, 5>
{ static const magic[]="ZZc5"; };
{ static const char magic[]="ZZc5"; };
template<> struct blitz_magic<std::complex<double>, 1>
{ static const magic[]="ZZz1"; };
{ static const char magic[]="ZZz1"; };
template<> struct blitz_magic<std::complex<double>, 2>
{ static const magic[]="ZZz2"; };
{ static const char magic[]="ZZz2"; };
template<> struct blitz_magic<std::complex<double>, 3>
{ static const magic[]="ZZz3"; };
{ static const char magic[]="ZZz3"; };
template<> struct blitz_magic<std::complex<double>, 4>
{ static const magic[]="ZZz4"; };
{ static const char magic[]="ZZz4"; };
template<> struct blitz_magic<std::complex<double>, 5>
{ static const magic[]="ZZz5"; };
{ static const char magic[]="ZZz5"; };
#endif
/*----------------------------------------------------------------------*/
......@@ -103,8 +105,16 @@ template<class T, int N>
operator << (tfxx::fortranio::FortranBinOutput& fo,
const blitz::Array<T, N>& a)
{
fo.write_magic(blitz_magic<T,N>::magic);
fo.put(N);
fo.put(value);
fo.end_block();
for (int i=0; i<N; i++) fo.put(a.lbound(i));
fo.end_block();
for (int i=0; i<N; i++) fo.put(a.extent(i));
fo.end_block();
for (blitz::Array<T, N>::const_iterator i=a.begin(); i!=a.end(); ++i)
{ fo.put(*i); }
fo.end_block();
return(fo);
}
......@@ -124,9 +134,19 @@ template<class T, int N>
operator >> (tfxx::fortranio::FortranBinInput& fi,
blitz::Array<T, N>& a)
{
TFXX_assert((fi.match_magic(blitz_magic<T,N>::magic)),
"ERROR (blitz array operator>>): input has no matching magic number!");
int n;
fi.get(n)
fi.get(value);
fi.get(n);
TFXX_assert((N==n), "ERROR (blitz array operator>>): illegal rank!");
blitz::TinyVector<int, N> lbound, extent;
for (int i=0; i<N; i++) fi.get(lbound[i]);
for (int i=0; i<N; i++) fi.get(extent[i]);
blitz::Array<T, N> result(lbound, extent, blitz::fortranArray);
result.dumpStructureInformation();
for (blitz::Array<T, N>::iterator i=result.begin(); i!=result.end(); ++i)
{ fi.get(*i); }
a.reference(result);
return(fi);
}
......
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: fortranio.cc,v 1.7 2002-11-28 19:47:41 forbrig Exp $
* $Id: fortranio.cc,v 1.8 2002-11-28 21:34:07 forbrig Exp $
* \author Thomas Forbriger
* \date 13/01/2002
*
......@@ -24,18 +24,18 @@
* - 28/11/2002 V1.7
* - flush buffer only if filled
* - support magic numbers
* - needs a cleaning destructor
*
* ============================================================================
*/
#define TF_FORTRANIO_CC_VERSION \
"TF_FORTRANIO_CC V1.7 "
#define TF_FORTRANIO_CC_CVSID \
"$Id: fortranio.cc,v 1.7 2002-11-28 19:47:41 forbrig Exp $"
"$Id: fortranio.cc,v 1.8 2002-11-28 21:34:07 forbrig Exp $"
#include <iostream>
#include <string>
#include <tfxx/fortranio.h>
#include <tfxx/ioswap.h>
#include <tfxx/misc.h>
#include <tfxx/error.h>
......@@ -75,6 +75,27 @@ namespace fortranio {
"FortranBinInput: stream is not good after reading block size");
}
/*----------------------------------------------------------------------*/
//! finish open block
void FortranBinInput::finish_block()
{
// do we have to read any more from current block?
while (Mnremain>0) { this->extract_next_char(); }
// had this block finite size? Then read end count and compare
if (Mnbytes>0) {
tfxx::ioswap::IOUnion<int> buf;
Mistream.read(buf.bytes, tfxx::ioswap::IOTsize<int>::Msize);
int endcount=buf.value;
if (Mswap) { endcount=tfxx::ioswap::swap(buf.value); }
TFXX_assert((Mnbytes == endcount),
"FortranBinInput (finish_block): "
"bount count at end of block does not match!");
}
// we do not open a new block - indicate by zero blocksize
Mnbytes=0;
}
/*----------------------------------------------------------------------*/
//! return next character from buffer
......@@ -103,8 +124,10 @@ namespace fortranio {
//! look out for expected magic number and adjust swapping flag
bool FortranBinInput::match_magic(const char* cmagic)
{
// skip rest of previous block
this->finish_block();
tfxx::ioswap::Emagic_type result
=tfxx::ioswap::file_magic_test(Mistream, cmagic,true);
=tfxx::ioswap::file_magic_test(Mistream, cmagic, true);
if (result==tfxx::ioswap::magic_nomatch) return(false);
if (result==tfxx::ioswap::magic_swap) { Mswap=true; }
else { Mswap=false; }
......
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: fortranio.h,v 1.15 2002-11-28 19:47:49 forbrig Exp $
* $Id: fortranio.h,v 1.16 2002-11-28 21:34:07 forbrig Exp $
* \author Thomas Forbriger
* \date 13/01/2002
*
......@@ -18,7 +18,9 @@
* - 15/11/2002 V1.3 no arrays for buffering
* - 19/11/2002 V1.4 remove all access functions
* rather use I/O operator
* - 28/11/2002 V1.5 support reading and writing of magic numbers
* - 28/11/2002 V1.5
* - support reading and writing of magic numbers
* - input needs a cleaning destructor
*
* ============================================================================
*/
......@@ -27,9 +29,9 @@
#ifndef TF_FORTRANIO_H_VERSION
#define TF_FORTRANIO_H_VERSION \
"TF_ V1.0 "
"TF_ V1.5 "
#define TF_FORTRANIO_H_CVSID \
"$Id: fortranio.h,v 1.15 2002-11-28 19:47:49 forbrig Exp $"
"$Id: fortranio.h,v 1.16 2002-11-28 21:34:07 forbrig Exp $"
#include<tfxx/misc.h>
#include<fstream>
......@@ -121,6 +123,8 @@ class FortranBinInput {
//! start reading from input stream \c is
FortranBinInput(std::istream& is, const bool& swap=false):
Mistream(is), Mswap(swap), Mnbytes(0), Mnremain(0) { }
//! needs a cleaning destructor
~FortranBinInput() { this->finish_block(); }
//! are there more data expected?
bool more() const
{ return((Mnbytes>0) || Mistream.good()); }
......@@ -142,6 +146,8 @@ class FortranBinInput {
void extract_chars(char* buf, const int& n);
//! read next block size
void read_block_size();
//! finish open block
void finish_block();
//! The input stream to read from
std::istream& Mistream;
......@@ -181,7 +187,7 @@ class FortranBinOutput {
FortranBinOutput(std::ostream& os):
Mostream(os), Mbuffer(std::string()) { }
//! we need a flushing destructor
~FortranBinOutput() { end_block(); }
~FortranBinOutput() { this->end_block(); }
//! put a value to the output
template<typename T>
void put(const T& value);
......
# this is <Makefile>
# ----------------------------------------------------------------------------
# $Id: Makefile,v 1.6 2002-11-20 14:57:51 forbrig Exp $
# $Id: Makefile,v 1.7 2002-11-28 21:34:08 forbrig Exp $
#
# Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt)
#
......@@ -9,14 +9,15 @@
# REVISIONS and CHANGES
# - 15/11/2002 V1.0 Thomas Forbriger
# - 19/11/2002 V1.1 provide f2c rules too for tests on AIX
# - 28/11/2002 V1.2 added blitztest
#
# ============================================================================
#
CPPFLAGS=-I$(LOCINCLUDEDIR) -I$(SERVERINCLUDEDIR)
LDFLAGS=-L$(SERVERLIBDIR) -L$(LOCLIBDIR)
CPPFLAGS=-I$(LOCINCLUDEDIR) -I$(SERVERINCLUDEDIR) -I$(BLITZINCLUDE)
LDFLAGS=-L$(SERVERLIBDIR) -L$(LOCLIBDIR) -L$(BLITZLIB)
CXXFLAGS=-fhonor-std -Wall $(FLAGS)
CXXFLAGS=-Wall $(FLAGS)
CXXFLAGS=$(FLAGS)
FC=g77
FCFLAGS=-Wall -ffixed-line-length-0 -fno-backslash
......@@ -44,7 +45,7 @@ clean: ;
# all that only need templates from the library
STANDARDTEST=
# all that need the binary library
BINARYTEST=commandlinetest fortraniotest
BINARYTEST=commandlinetest fortraniotest blitztest
# F77 part of the I/O test
FORTRANTEST=fortranF77
......@@ -63,7 +64,7 @@ $(addsuffix .o,$(CXXTESTS)): %.o: %.cc
$(CXX) -c -o $@ $< $(CXXFLAGS) $(CPPFLAGS) $(FLAGS)
$(STANDARDTEST): %: %.o; $(CXX) -o $@ $< $(LDFLAGS)
$(BINARYTEST): %: %.o; $(CXX) -o $@ $< $(LDFLAGS) -ltfxx
$(BINARYTEST): %: %.o; $(CXX) -o $@ $< $(LDFLAGS) -ltfxx -lblitz
$(FORTRANTEST): %: %.o; $(CC) -o $@ $< $(LDFLAGS) -ltf -lf2c -lm
$(addsuffix .g77,$(FORTRANTEST)): %.g77: %.o77
$(FC) -o $@ $< $(LDFLAGS) -ltf77
......
/*! \file blitztest.cc
* \brief test all blitz support modules
*
* ----------------------------------------------------------------------------
*
* $Id: blitztest.cc,v 1.1 2002-11-28 21:34:08 forbrig Exp $
* \author Thomas Forbriger
* \date 28/11/2002
*
* test all blitz support modules
*
* Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt)
*
* REVISIONS and CHANGES
* - 28/11/2002 V1.0 Thomas Forbriger
*
* ============================================================================
*/
#define BLITZTEST_VERSION \
"BLITZTEST V1.0 test all blitz support modules"
#define BLITZTEST_CVSID \
"$Id: blitztest.cc,v 1.1 2002-11-28 21:34:08 forbrig Exp $"
#include <iostream>
#include <blitz/array.h>
#include <tfxx/commandline.h>
#include <tfxx/blitzfortranio.h>
using std::cout;
using std::cerr;
using std::endl;
//! test blitz binary I/O operators
int main(int iargc, char* argv[])
{
// define usage information
char usage_text[]=
{
BLITZTEST_VERSION "\n"
"usage: blitztest [-v] [-if file] [-of file]" "\n"
" or: blitztest --help|-h" "\n"
};
// define full help text
char help_text[]=
{
"-v be verbose" "\n"
"-of file write output to \"file\"" "\n"
"-if file read input from \"file\"" "\n"
BLITZTEST_CVSID
};
// define commandline options
using namespace tfxx::cmdline;
static Declare options[]=
{
// 0: print help
{"help",arg_no,"-"},
// 1: verbose mode
{"v",arg_no,"-"},
// 2: output file
{"of",arg_yes,"junk"},
// 3: input file
{"if",arg_yes,"junk"},
{NULL}
};
// no arguments? print usage...
if (iargc<1)
{
cerr << usage_text << endl;
exit(0);
}
// collect options from commandline
Commandline cmdline(iargc, argv, options);
// help requested? print full help text...
if (cmdline.optset(0))
{
cerr << usage_text << endl;
cerr << help_text << endl;
exit(0);
}
bool verbose=cmdline.optset(1);
std::string outfile=cmdline.string_arg(2);
std::string infile=cmdline.string_arg(3);
if (verbose)
{
cout << BLITZTEST_VERSION << endl << endl
<< "Creating arrays:" << endl;
}
blitz::Array<float, 2> A(blitz::Range(3,6),blitz::Range(1,3),
blitz::fortranArray);
blitz::Array<float, 2> B(blitz::Range(3,6),blitz::Range(1,3));
{
blitz::firstIndex i;
blitz::secondIndex j;
A=100+10*i+j;
B=200+10*i+j;
}
cout << endl << "Fortran array: " << endl
<< "==============" << endl << A << endl;
A.dumpStructureInformation();
cout << endl << "C array: " << endl
<< "========" << endl << B << endl;
B.dumpStructureInformation();
if (verbose) cout << endl << "writing to " << outfile << "..." << endl;
{
std::ofstream os(outfile.c_str());
tfxx::fortranio::FortranBinOutput fo(os);
fo << A << B;
}
blitz::Array<float, 2> C,D,E(blitz::fortranArray),F(blitz::fortranArray);
if (verbose) cout << endl << "reading from " << infile
<< " into C arrays..." << endl;
try
{
std::ifstream is(infile.c_str());
tfxx::fortranio::FortranBinInput fi(is);
fi >> C >> D;
}
catch (tfxx::error::Exception)
{
cout.flush();
TFXX_abort("sorry...");
}
if (verbose) cout << endl << "reading from " << infile
<< " into Fortran arrays..." << endl;
{
std::ifstream is(infile.c_str());
tfxx::fortranio::FortranBinInput fi(is);
fi >> E >> F;
}
cout << endl << "C array from Fortran array:" << endl
<< "===========================" << endl << C << endl;
C.dumpStructureInformation();
cout << endl << "C array from C array:" << endl
<< "=====================" << endl << D << endl;
D.dumpStructureInformation();
cout << endl << "Fortran array from Fortran array:" << endl
<< "=================================" << endl << E << endl;
E.dumpStructureInformation();
cout << endl << "Fortran array from C array:" << endl
<< "===========================" << endl << F << endl;
F.dumpStructureInformation();
}
/* ----- END OF blitztest.cc ----- */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment