[Swift-user] How to determine length of an array?

Michael Wilde wilde at mcs.anl.gov
Thu Aug 2 12:29:00 CDT 2012


Hi Tom,

This should work. Its hopefully fixed in trunk, but if not, we should fix it there, quickly.

In the meantime, for moderate sized arrays you may be able to use the hack below as a temporary solution.

- Mike

---

type file;


app (file o) countArgs (int v[])
{
  sh "-c" "echo $#" v stdout=@o;
}

(int s) sizeOfFileArray (file fa[])
{
  int ia[];
  foreach f,i in fa {
    ia[i] = 1;
  }
  s = readData(countArgs(ia));
}

file input_files[] <filesys_mapper; suffix=".log">;

# trace( @length( input_files ) ) ;

trace("size", sizeOfFileArray(input_files));



----- Original Message -----
> From: "Thomas Uram" <turam at mcs.anl.gov>
> To: "swift user" <swift-user at ci.uchicago.edu>
> Sent: Thursday, August 2, 2012 11:23:30 AM
> Subject: [Swift-user] How to determine length of an array?
> Some functionality of my script is conditional based on the contents
> of an array, so I need to determine the array length. The path
> described by the documentation leads to an exception. How should I
> determine array length? Or, a lesser requirement: how can I determine
> if an array is empty?
> 
> 
> The user guide (
> http://www.ci.uchicago.edu/swift/guides/trunk/userguide/userguide.html
> ) says:
> 
> 
> @length(array) will return the length of an array in Swift. This
> function will wait for all elements in the array to be written before
> returning the length.
> 
> 
> But the following Swift script:
> 
> 
> 
> type file;
> file input_files[] <filesys_mapper; suffix=".log">;
> trace( @length( input_files ) ) ;
> 
> 
> Produces this output:
> 
> 
> 
> Swift 0.93 swift-r5483 cog-r3339
> 
> 
> RunID: 20120802-1218-5dfj571d
> (input): found 255 files
> Progress: time: Thu, 02 Aug 2012 12:18:51 -0400
> Execution failed:
> java.lang.ClassCastException:
> org.griphyn.vdl.mapping.RootArrayDataNode cannot be cast to
> java.util.Map
> at
> org.griphyn.vdl.karajan.lib.swiftscript.Misc.swiftscript_length(Misc.java:399)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at
> org.globus.cog.karajan.workflow.nodes.functions.FunctionsCollection.function(FunctionsCollection.java:82)
> at
> org.globus.cog.karajan.workflow.nodes.functions.AbstractFunction.post(AbstractFunction.java:27)
> at
> org.globus.cog.karajan.workflow.nodes.AbstractSequentialWithArguments.completed(AbstractSequentialWithArguments.java:194)
> at
> org.globus.cog.karajan.workflow.nodes.FlowNode.complete(FlowNode.java:214)
> at
> org.globus.cog.karajan.workflow.nodes.FlowContainer.post(FlowContainer.java:58)
> at
> org.globus.cog.karajan.workflow.nodes.functions.AbstractFunction.post(AbstractFunction.java:28)
> at
> org.globus.cog.karajan.workflow.nodes.Sequential.startNext(Sequential.java:29)
> at
> org.globus.cog.karajan.workflow.nodes.Sequential.executeChildren(Sequential.java:20)
> at
> org.globus.cog.karajan.workflow.nodes.FlowContainer.execute(FlowContainer.java:63)
> at
> org.globus.cog.karajan.workflow.nodes.FlowNode.restart(FlowNode.java:139)
> at
> org.globus.cog.karajan.workflow.nodes.FlowNode.start(FlowNode.java:197)
> at
> org.globus.cog.karajan.workflow.FlowElementWrapper.start(FlowElementWrapper.java:227)
> at
> org.globus.cog.karajan.workflow.events.EventBus.start(EventBus.java:104)
> at
> org.globus.cog.karajan.workflow.events.EventTargetPair.run(EventTargetPair.java:40)
> at
> java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
> at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
> at java.util.concurrent.FutureTask.run(FutureTask.java:138)
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
> at java.lang.Thread.run(Thread.java:619)
> 
> 
> _______________________________________________
> Swift-user mailing list
> Swift-user at ci.uchicago.edu
> https://lists.ci.uchicago.edu/cgi-bin/mailman/listinfo/swift-user

-- 
Michael Wilde
Computation Institute, University of Chicago
Mathematics and Computer Science Division
Argonne National Laboratory




More information about the Swift-user mailing list