swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: InFile


From: Marcus G. Daniels
Subject: Re: InFile
Date: 11 Sep 1999 16:01:22 -0700
User-agent: Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.4

>>>>> "PT" == Pietro Terna <address@hidden> writes:

PT>     Reading the documentation I see that InFile and OutFile
PT> protocol are now deprecated.

PT>     What is the easy alternative? My need is to read and write the
PT> contents of some arrays of float.

I suggest you use either the Lisp or HDF5 archivers.  (FYI, InFile and OutFile
won't go away any time soon for Objective C users.)

Suppose you have a floating point matrix in a file like below that you
want to load into a Swarm object.

$ cat ary.txt
1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 100.0

The first thing to do it to convert this data into a format Swarm can load. 
Swarm currently knows about two formats: a Scheme-like format and HDF5.

To create HDF5 for Swarm, the procedure to load this file involves running 
a script in R like this:

$ cat saveary.R
v <- scan("ary.txt") # load vector of data
m <- matrix(v, nrow=2,byrow=TRUE) # convert vector to array
myObject <- list(ary=m) # wrap array in an object
attr(myObject,"type") <- "MyObject" # identify as a MyObject class (for Swarm)
hdf5save("ary.hdf",myObject) # save it to HDF5

Which you can run like this:

$ R BATCH saveary.R

The result is the file ary.hdf (not itself human-readable).  Note that
HDF5 is a standalone library that has a well-defined API; it's not
necessary to use R if you have another tool or application that makes
calls to the HDF5 library.

Here is what the text file above would look like converted to Scheme:¹

$ cat ary.scm
(list
 (cons 'myObject
       (make-instance 'MyObject 
                      #:ary
                      #2((1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0) 
                         (10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 100.0))
                      )))

This format is list of pairs where the first member of the pair
(a.k.a. a "cons") is a key the the second is a value.  The key,
"myObject" is the name you give to the object (an instance of MyObject
that contains an array).

Once you've created either ary.hdf or ary.scm, then you can use either
the HDF5Archiver or LispArchiver to load it.  The crucial thing to
look at in the program below are the "#ifdef USE_HDF5" alternatives and
the call to getObject: that follows.

#import <simtools.h>
#import <defobj/Create.h>
#import <defobj.h>

#define ROWS 2
#define COLUMNS 10

@interface MyObject: CreateDrop
{
  double ary[ROWS][COLUMNS];
}
@end

@implementation MyObject
- (void)describe: outputCharStream
{
  unsigned i;

  for (i = 0; i < ROWS; i++)
    {
      unsigned j;

      for (j = 0; j < COLUMNS; j++)
        printf (" %f", ary[i][j]);
      putchar ('\n');
    }
}
@end

@interface Controller: CreateDrop
{
  id ary;
}
+ createBegin: aZone;
@end

@implementation Controller

+ createBegin: aZone
{
  Controller *obj = [super createBegin: aZone];

  obj->ary = [MyObject create: globalZone];
  return obj;
}

@end

int
main (int argc, const char **argv) 
{

  initSwarmBatch (argc, argv);

  {
    id archiver;
    id controller;

#ifdef USE_HDF5
    archiver = [[[HDF5Archiver createBegin: globalZone]
                  setPath: "ary.hdf"]
                 createEnd];
#else
    archiver = [[[LispArchiver createBegin: globalZone]
                  setPath: "ary.scm"]
                 createEnd];
#endif
    
    xprint ([archiver getObject: "myObject"]);
    controller = [Controller create: globalZone];
  }
  return 0;
}

/*
Local Variables:
compile-command: "$SWARMHOME/bin/libtool-swarm --mode=link gcc -DUSE_HDF5 -o 
ary -g -Wno-import -I$SWARMHOME/include -L$SWARMHOME/lib ary.m -lswarm -lobjc"
End:
*/



¹ Instead reformatting by hand, you can also use program to do it for you.
Here's a Scheme program that converts a text file to the right Scheme format.

$ cat convert-ary.sc
(define (load-text text-file)
    (with-input-from-file text-file
      (lambda ()
        (let join-strings ((expr ""))
             (let ((line (read-line)))
               (if (eof-object? line)
                   (call-with-input-string (string-append "#2(" expr ")")
                                           read)
                   (join-strings (string-append expr "(" line ")"))))))))

(define (wrap filename ary)
    (with-output-to-file filename
      (lambda ()
        (write `(list
                 (cons 'myObject
                  (make-instance 'MyObject #:ary ,ary)))))))

$ guile -l convert-ary.scm -c '(wrap "ary.scm" (load-text "ary.txt"))'


                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]