blob: 9d73ac44ab5d77f5916eab3e6596846c0ce09ffb [file] [log] [blame]
Yang Nicc1ca482015-03-16 15:53:18 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yang Ni281c3252014-10-24 08:52:24 -070017package android.renderscript;
18
19import android.util.Log;
20import android.util.Pair;
21import java.util.ArrayList;
22import java.util.HashMap;
23import java.util.List;
24import java.util.Map;
25
26/**
Yang Ni4470d68a2015-03-16 16:23:50 -070027 * ScriptGroup2 is a new, enhanced API for script groups.
28 * A script group is a collection of kernels or invocable functions, with
29 * data dependencies defined among them. A script group is launched for
30 * execution as a whole, rather than launching each kernel or invocable function
31 * separately. Once created, a script group can be repeatedly used with
32 * different inputs.
33 * <p>
34 * In the new ScriptGroup2 API, a script group is modeled using closures.
35 * A closure, in this context, is defined as a function call to a kernel or
36 * invocable function. Each function argument or global variable accessed inside
37 * the function is bound to 1) a known value, 2) a script group input, or 3) a
38 * future. A future is the output of a closure, i.e., the return value of the
39 * function or a global variable written by that function.
40 * <p>
41 * A script group is a directed acyclic graph (DAG), in which closures are the
42 * vertices and the dependencies among them are the edges.
43 * The way the ScriptGroup2 API is designed makes cycles impossible in a script
44 * group. For example, it is impossible to make forward references to futures,
45 * i.e., it is impossible to set as input to a closure the future from itself or
46 * a future from another closure that directly or indirectly depends on it.
47 * <p>
48 * Grouping kernels and invocable functions together allows to execute them more
49 * efficiently. Runtime and compiler optimizations are applied to script
50 * groups, to reduce computation or communication overhead, and to make more
51 * efficient use of the CPU and the GPU.
52 */
Yang Ni281c3252014-10-24 08:52:24 -070053
Yang Ni281c3252014-10-24 08:52:24 -070054public class ScriptGroup2 extends BaseObj {
55
Yang Ni4470d68a2015-03-16 16:23:50 -070056 /**
57 * An opaque class for closures
58 */
59
Yang Nicc1ca482015-03-16 15:53:18 -070060 public static class Closure extends BaseObj {
Yang Ni4c93c8c2015-03-26 14:35:22 -070061 private Object[] mArgs;
Yang Nicc1ca482015-03-16 15:53:18 -070062 private Allocation mReturnValue;
63 private Map<Script.FieldID, Object> mBindings;
Yang Ni281c3252014-10-24 08:52:24 -070064
Yang Nicc1ca482015-03-16 15:53:18 -070065 private Future mReturnFuture;
66 private Map<Script.FieldID, Future> mGlobalFuture;
Yang Ni281c3252014-10-24 08:52:24 -070067
Yang Nicc1ca482015-03-16 15:53:18 -070068 private FieldPacker mFP;
Yang Nibe392ad2015-01-23 17:16:02 -080069
Yang Nicc1ca482015-03-16 15:53:18 -070070 private static final String TAG = "Closure";
Yang Ni281c3252014-10-24 08:52:24 -070071
Yang Nicc1ca482015-03-16 15:53:18 -070072 public Closure(long id, RenderScript rs) {
73 super(id, rs);
Yang Ni281c3252014-10-24 08:52:24 -070074 }
Yang Ni281c3252014-10-24 08:52:24 -070075
Yang Nicc1ca482015-03-16 15:53:18 -070076 public Closure(RenderScript rs, Script.KernelID kernelID, Type returnType,
77 Object[] args, Map<Script.FieldID, Object> globals) {
78 super(0, rs);
79
Yang Ni4c93c8c2015-03-26 14:35:22 -070080 mArgs = args;
Yang Nicc1ca482015-03-16 15:53:18 -070081 mReturnValue = Allocation.createTyped(rs, returnType);
Yang Ni4c93c8c2015-03-26 14:35:22 -070082 mBindings = globals;
Yang Nicc1ca482015-03-16 15:53:18 -070083 mGlobalFuture = new HashMap<Script.FieldID, Future>();
84
85 int numValues = args.length + globals.size();
86
87 long[] fieldIDs = new long[numValues];
88 long[] values = new long[numValues];
89 int[] sizes = new int[numValues];
90 long[] depClosures = new long[numValues];
91 long[] depFieldIDs = new long[numValues];
92
93 int i;
94 for (i = 0; i < args.length; i++) {
95 Object obj = args[i];
96 fieldIDs[i] = 0;
97 if (obj instanceof UnboundValue) {
98 UnboundValue unbound = (UnboundValue)obj;
99 unbound.addReference(this, i);
100 } else {
101 retrieveValueAndDependenceInfo(rs, i, args[i], values, sizes,
102 depClosures, depFieldIDs);
103 }
104 }
105
106 for (Map.Entry<Script.FieldID, Object> entry : globals.entrySet()) {
107 Object obj = entry.getValue();
108 Script.FieldID fieldID = entry.getKey();
109 fieldIDs[i] = fieldID.getID(rs);
110 if (obj instanceof UnboundValue) {
111 UnboundValue unbound = (UnboundValue)obj;
112 unbound.addReference(this, fieldID);
113 } else {
114 retrieveValueAndDependenceInfo(rs, i, obj, values,
115 sizes, depClosures, depFieldIDs);
116 }
117 i++;
118 }
119
120 long id = rs.nClosureCreate(kernelID.getID(rs), mReturnValue.getID(rs),
121 fieldIDs, values, sizes, depClosures, depFieldIDs);
122
123 setID(id);
Yang Ni281c3252014-10-24 08:52:24 -0700124 }
Yang Ni281c3252014-10-24 08:52:24 -0700125
Yang Nicc1ca482015-03-16 15:53:18 -0700126 public Closure(RenderScript rs, Script.InvokeID invokeID,
127 Object[] args, Map<Script.FieldID, Object> globals) {
128 super(0, rs);
129 mFP = FieldPacker.createFieldPack(args);
Yang Ni281c3252014-10-24 08:52:24 -0700130
Yang Ni4c93c8c2015-03-26 14:35:22 -0700131 mArgs = args;
132 mBindings = globals;
Yang Nicc1ca482015-03-16 15:53:18 -0700133 mGlobalFuture = new HashMap<Script.FieldID, Future>();
Yang Ni281c3252014-10-24 08:52:24 -0700134
Yang Nicc1ca482015-03-16 15:53:18 -0700135 int numValues = globals.size();
Yang Nibe392ad2015-01-23 17:16:02 -0800136
Yang Nicc1ca482015-03-16 15:53:18 -0700137 long[] fieldIDs = new long[numValues];
138 long[] values = new long[numValues];
139 int[] sizes = new int[numValues];
140 long[] depClosures = new long[numValues];
141 long[] depFieldIDs = new long[numValues];
Yang Nibe392ad2015-01-23 17:16:02 -0800142
Yang Nicc1ca482015-03-16 15:53:18 -0700143 int i = 0;
144 for (Map.Entry<Script.FieldID, Object> entry : globals.entrySet()) {
145 Object obj = entry.getValue();
146 Script.FieldID fieldID = entry.getKey();
147 fieldIDs[i] = fieldID.getID(rs);
148 if (obj instanceof UnboundValue) {
149 UnboundValue unbound = (UnboundValue)obj;
150 unbound.addReference(this, fieldID);
151 } else {
Yang Nicc1ca482015-03-16 15:53:18 -0700152 retrieveValueAndDependenceInfo(rs, i, obj, values,
153 sizes, depClosures, depFieldIDs);
154 }
155 i++;
156 }
Yang Nibe392ad2015-01-23 17:16:02 -0800157
Yang Nicc1ca482015-03-16 15:53:18 -0700158 long id = rs.nInvokeClosureCreate(invokeID.getID(rs), mFP.getData(), fieldIDs,
159 values, sizes);
Yang Nibe392ad2015-01-23 17:16:02 -0800160
Yang Nicc1ca482015-03-16 15:53:18 -0700161 setID(id);
Yang Nibe392ad2015-01-23 17:16:02 -0800162 }
Yang Nibe392ad2015-01-23 17:16:02 -0800163
Yang Nicc1ca482015-03-16 15:53:18 -0700164 private static
165 void retrieveValueAndDependenceInfo(RenderScript rs,
166 int index, Object obj,
167 long[] values, int[] sizes,
168 long[] depClosures,
169 long[] depFieldIDs) {
Yang Nibe392ad2015-01-23 17:16:02 -0800170
Yang Nicc1ca482015-03-16 15:53:18 -0700171 if (obj instanceof Future) {
172 Future f = (Future)obj;
173 obj = f.getValue();
174 depClosures[index] = f.getClosure().getID(rs);
175 Script.FieldID fieldID = f.getFieldID();
176 depFieldIDs[index] = fieldID != null ? fieldID.getID(rs) : 0;
177 if (obj == null) {
178 // Value is originally created by the owner closure
179 values[index] = 0;
180 sizes[index] = 0;
181 return;
182 }
183 } else {
184 depClosures[index] = 0;
185 depFieldIDs[index] = 0;
186 }
Yang Nibe392ad2015-01-23 17:16:02 -0800187
Yang Nicc1ca482015-03-16 15:53:18 -0700188 ValueAndSize vs = new ValueAndSize(rs, obj);
189 values[index] = vs.value;
190 sizes[index] = vs.size;
Yang Nibe392ad2015-01-23 17:16:02 -0800191 }
Yang Ni281c3252014-10-24 08:52:24 -0700192
Yang Ni4470d68a2015-03-16 16:23:50 -0700193 /**
194 * Returns the future for the return value
195 *
196 * @return a future
197 */
198
Yang Nicc1ca482015-03-16 15:53:18 -0700199 public Future getReturn() {
200 if (mReturnFuture == null) {
201 mReturnFuture = new Future(this, null, mReturnValue);
202 }
Yang Ni281c3252014-10-24 08:52:24 -0700203
Yang Nicc1ca482015-03-16 15:53:18 -0700204 return mReturnFuture;
Yang Ni281c3252014-10-24 08:52:24 -0700205 }
Yang Ni281c3252014-10-24 08:52:24 -0700206
Yang Ni4470d68a2015-03-16 16:23:50 -0700207 /**
208 * Returns the future for a global variable
209 *
210 * @param field the field ID for the global variable
211 * @return a future
212 */
213
Yang Nicc1ca482015-03-16 15:53:18 -0700214 public Future getGlobal(Script.FieldID field) {
215 Future f = mGlobalFuture.get(field);
Yang Ni281c3252014-10-24 08:52:24 -0700216
Yang Nicc1ca482015-03-16 15:53:18 -0700217 if (f == null) {
218 // If the field is not bound to this closure, this will return a future
219 // without an associated value (reference). So this is not working for
220 // cross-module (cross-script) linking in this case where a field not
221 // explicitly bound.
222 f = new Future(this, field, mBindings.get(field));
223 mGlobalFuture.put(field, f);
224 }
225
226 return f;
227 }
228
229 void setArg(int index, Object obj) {
Yang Ni4c93c8c2015-03-26 14:35:22 -0700230 mArgs[index] = obj;
Yang Nicc1ca482015-03-16 15:53:18 -0700231 ValueAndSize vs = new ValueAndSize(mRS, obj);
232 mRS.nClosureSetArg(getID(mRS), index, vs.value, vs.size);
233 }
234
235 void setGlobal(Script.FieldID fieldID, Object obj) {
Yang Ni4c93c8c2015-03-26 14:35:22 -0700236 mBindings.put(fieldID, obj);
Yang Nicc1ca482015-03-16 15:53:18 -0700237 ValueAndSize vs = new ValueAndSize(mRS, obj);
238 mRS.nClosureSetGlobal(getID(mRS), fieldID.getID(mRS), vs.value, vs.size);
239 }
240
241 private static final class ValueAndSize {
242 public ValueAndSize(RenderScript rs, Object obj) {
243 if (obj instanceof Allocation) {
244 value = ((Allocation)obj).getID(rs);
245 size = -1;
246 } else if (obj instanceof Boolean) {
247 value = ((Boolean)obj).booleanValue() ? 1 : 0;
248 size = 4;
249 } else if (obj instanceof Integer) {
250 value = ((Integer)obj).longValue();
251 size = 4;
252 } else if (obj instanceof Long) {
253 value = ((Long)obj).longValue();
254 size = 8;
255 } else if (obj instanceof Float) {
256 value = ((Float)obj).longValue();
257 size = 4;
258 } else if (obj instanceof Double) {
259 value = ((Double)obj).longValue();
260 size = 8;
261 }
262 }
263 public long value;
264 public int size;
265 }
Yang Ni281c3252014-10-24 08:52:24 -0700266 }
267
Yang Ni4470d68a2015-03-16 16:23:50 -0700268 /**
269 * An opaque class for futures
270 */
271
Yang Nicc1ca482015-03-16 15:53:18 -0700272 public static class Future {
273 Closure mClosure;
274 Script.FieldID mFieldID;
275 Object mValue;
Yang Ni281c3252014-10-24 08:52:24 -0700276
Yang Nicc1ca482015-03-16 15:53:18 -0700277 Future(Closure closure, Script.FieldID fieldID, Object value) {
278 mClosure = closure;
279 mFieldID = fieldID;
280 mValue = value;
281 }
Yang Ni281c3252014-10-24 08:52:24 -0700282
Yang Nicc1ca482015-03-16 15:53:18 -0700283 Closure getClosure() { return mClosure; }
284 Script.FieldID getFieldID() { return mFieldID; }
285 Object getValue() { return mValue; }
Yang Ni281c3252014-10-24 08:52:24 -0700286 }
287
Yang Ni4470d68a2015-03-16 16:23:50 -0700288 /**
289 * An opaque class for unbound values (a.k.a. script group inputs)
290 */
291
Yang Nicc1ca482015-03-16 15:53:18 -0700292 public static class UnboundValue {
293 // Either mFieldID or mArgIndex should be set but not both.
294 List<Pair<Closure, Script.FieldID>> mFieldID;
295 // -1 means unset. Legal values are 0 .. n-1, where n is the number of
296 // arguments for the referencing closure.
297 List<Pair<Closure, Integer>> mArgIndex;
298
299 UnboundValue() {
300 mFieldID = new ArrayList<Pair<Closure, Script.FieldID>>();
301 mArgIndex = new ArrayList<Pair<Closure, Integer>>();
302 }
303
304 void addReference(Closure closure, int index) {
305 mArgIndex.add(Pair.create(closure, Integer.valueOf(index)));
306 }
307
308 void addReference(Closure closure, Script.FieldID fieldID) {
309 mFieldID.add(Pair.create(closure, fieldID));
310 }
311
312 void set(Object value) {
313 for (Pair<Closure, Integer> p : mArgIndex) {
314 Closure closure = p.first;
315 int index = p.second.intValue();
316 closure.setArg(index, value);
317 }
318 for (Pair<Closure, Script.FieldID> p : mFieldID) {
319 Closure closure = p.first;
320 Script.FieldID fieldID = p.second;
321 closure.setGlobal(fieldID, value);
322 }
323 }
Yang Ni281c3252014-10-24 08:52:24 -0700324 }
325
Yang Ni281c3252014-10-24 08:52:24 -0700326 List<Closure> mClosures;
327 List<UnboundValue> mInputs;
Yang Nicc1ca482015-03-16 15:53:18 -0700328 Future[] mOutputs;
Yang Ni281c3252014-10-24 08:52:24 -0700329
Yang Nicc1ca482015-03-16 15:53:18 -0700330 private static final String TAG = "ScriptGroup2";
Yang Ni281c3252014-10-24 08:52:24 -0700331
Yang Nicc1ca482015-03-16 15:53:18 -0700332 public ScriptGroup2(long id, RenderScript rs) {
333 super(id, rs);
Yang Ni281c3252014-10-24 08:52:24 -0700334 }
335
Yang Nicc1ca482015-03-16 15:53:18 -0700336 ScriptGroup2(RenderScript rs, List<Closure> closures,
337 List<UnboundValue> inputs, Future[] outputs) {
338 super(0, rs);
339 mClosures = closures;
340 mInputs = inputs;
341 mOutputs = outputs;
342
343 long[] closureIDs = new long[closures.size()];
344 for (int i = 0; i < closureIDs.length; i++) {
345 closureIDs[i] = closures.get(i).getID(rs);
346 }
347 long id = rs.nScriptGroup2Create(ScriptC.mCachePath, closureIDs);
348 setID(id);
Yang Ni281c3252014-10-24 08:52:24 -0700349 }
350
Yang Ni4470d68a2015-03-16 16:23:50 -0700351 /**
352 * Executes a script group
353 *
354 * @param inputs inputs to the script group
355 * @return outputs of the script group as an array of objects
356 */
357
Yang Nicc1ca482015-03-16 15:53:18 -0700358 public Object[] execute(Object... inputs) {
359 if (inputs.length < mInputs.size()) {
360 Log.e(TAG, this.toString() + " receives " + inputs.length + " inputs, " +
361 "less than expected " + mInputs.size());
362 return null;
363 }
364
365 if (inputs.length > mInputs.size()) {
366 Log.i(TAG, this.toString() + " receives " + inputs.length + " inputs, " +
367 "more than expected " + mInputs.size());
368 }
369
370 for (int i = 0; i < mInputs.size(); i++) {
371 Object obj = inputs[i];
372 if (obj instanceof Future || obj instanceof UnboundValue) {
373 Log.e(TAG, this.toString() + ": input " + i +
374 " is a future or unbound value");
375 return null;
376 }
377 UnboundValue unbound = mInputs.get(i);
378 unbound.set(obj);
379 }
380
381 mRS.nScriptGroup2Execute(getID(mRS));
382
383 Object[] outputObjs = new Object[mOutputs.length];
384 int i = 0;
385 for (Future f : mOutputs) {
386 outputObjs[i++] = f.getValue();
387 }
388 return outputObjs;
Yang Nibe392ad2015-01-23 17:16:02 -0800389 }
390
Yang Nicc1ca482015-03-16 15:53:18 -0700391 /**
Yang Ni4470d68a2015-03-16 16:23:50 -0700392 * A class representing a binding of a value to a global variable in a
393 * kernel or invocable function. Such a binding can be used to create a
394 * closure.
395 */
396
Yang Ni8ff29802015-03-11 16:25:37 -0700397 public static final class Binding {
Yang Ni4470d68a2015-03-16 16:23:50 -0700398 private Script.FieldID mField;
399 private Object mValue;
400
401 /**
402 * Returns a Binding object that binds value to field
403 *
404 * @param field the Script.FieldID of the global variable
405 * @param value the value
406 */
407
Yang Ni8ff29802015-03-11 16:25:37 -0700408 public Binding(Script.FieldID field, Object value) {
409 mField = field;
410 mValue = value;
411 }
Yang Ni4470d68a2015-03-16 16:23:50 -0700412
413 /**
414 * Returns the field ID
415 */
416
417 public Script.FieldID getField() { return mField; }
418
419 /**
420 * Returns the value
421 */
422
423 public Object getValue() { return mValue; }
Yang Ni8ff29802015-03-11 16:25:37 -0700424 }
425
426 /**
Yang Ni4470d68a2015-03-16 16:23:50 -0700427 * The builder class to create a script group.
428 * <p>
429 * Closures are created using the {@link #addKernel} or {@link #addInvoke}
430 * methods.
431 * When a closure is created, futures from previously created closures
432 * can be used as inputs.
433 * Unbound values can be used as inputs to create closures as well.
434 * An unbound value is created using the {@link #addInput} method.
435 * Unbound values become inputs to the script group to be created,
436 * in the order that they are added.
437 * A script group is created by a call to the {@link #create} method, which
438 * accepts an array of futures as the outputs for the script group.
439 * <p>
440 * Closures in a script group can be evaluated in any order as long as the
441 * following conditions are met.
442 * First, a closure must be evaluated before any other closures that take its
443 * futures as inputs.
444 * Second, all closures added before an invoke closure must be evaluated
445 * before it.
446 * Third, all closures added after an invoke closure must be evaluated after
447 * it.
448 * <p>
449 * As a special case, the order that the closures are added is a legal
450 * evaluation order. However, other evaluation orders are allowed, including
451 * concurrently evaluating independent closures.
452 */
453
Yang Nicc1ca482015-03-16 15:53:18 -0700454 public static final class Builder {
455 RenderScript mRS;
456 List<Closure> mClosures;
457 List<UnboundValue> mInputs;
Yang Nicc1ca482015-03-16 15:53:18 -0700458 private static final String TAG = "ScriptGroup2.Builder";
Yang Ni281c3252014-10-24 08:52:24 -0700459
Yang Ni4470d68a2015-03-16 16:23:50 -0700460 /**
461 * Returns a Builder object
462 *
463 * @param rs the RenderScript context
464 */
Yang Nicc1ca482015-03-16 15:53:18 -0700465 public Builder(RenderScript rs) {
466 mRS = rs;
467 mClosures = new ArrayList<Closure>();
468 mInputs = new ArrayList<UnboundValue>();
469 }
470
Yang Ni4470d68a2015-03-16 16:23:50 -0700471 /**
472 * Adds a closure for a kernel
473 *
474 * @param k Kernel ID for the kernel function
475 * @param returnType Allocation type for the return value
476 * @param args arguments to the kernel function
477 * @param globalBindings bindings for global variables
478 * @return a closure
479 */
480
Yang Nicc1ca482015-03-16 15:53:18 -0700481 public Closure addKernel(Script.KernelID k, Type returnType, Object[] args,
482 Map<Script.FieldID, Object> globalBindings) {
483 Closure c = new Closure(mRS, k, returnType, args, globalBindings);
484 mClosures.add(c);
485 return c;
486 }
487
Yang Ni4470d68a2015-03-16 16:23:50 -0700488 /**
489 * Adds a closure for an invocable function
490 *
491 * @param invoke Invoke ID for the invocable function
492 * @param args arguments to the invocable function
493 * @param globalBindings bindings for global variables
494 * @return a closure
495 */
496
Yang Nicc1ca482015-03-16 15:53:18 -0700497 public Closure addInvoke(Script.InvokeID invoke, Object[] args,
498 Map<Script.FieldID, Object> globalBindings) {
499 Closure c = new Closure(mRS, invoke, args, globalBindings);
500 mClosures.add(c);
501 return c;
502 }
503
Yang Ni4470d68a2015-03-16 16:23:50 -0700504 /**
505 * Adds a script group input
506 *
507 * @return a unbound value that can be used to create a closure
508 */
Yang Nicc1ca482015-03-16 15:53:18 -0700509 public UnboundValue addInput() {
510 UnboundValue unbound = new UnboundValue();
511 mInputs.add(unbound);
512 return unbound;
513 }
514
Yang Ni4470d68a2015-03-16 16:23:50 -0700515 /**
516 * Adds a closure for a kernel
517 *
518 * @param k Kernel ID for the kernel function
519 * @param argsAndBindings arguments followed by bindings for global variables
520 * @return a closure
521 */
522
Yang Ni8ff29802015-03-11 16:25:37 -0700523 public Closure addKernel(Script.KernelID k, Type returnType, Object... argsAndBindings) {
524 ArrayList<Object> args = new ArrayList<Object>();
525 Map<Script.FieldID, Object> bindingMap = new HashMap<Script.FieldID, Object>();
526 if (!seperateArgsAndBindings(argsAndBindings, args, bindingMap)) {
527 return null;
528 }
529 return addKernel(k, returnType, args.toArray(), bindingMap);
530 }
531
Yang Ni4470d68a2015-03-16 16:23:50 -0700532 /**
533 * Adds a closure for an invocable function
534 *
535 * @param invoke Invoke ID for the invocable function
536 * @param argsAndBindings arguments followed by bindings for global variables
537 * @return a closure
538 */
539
Yang Ni8ff29802015-03-11 16:25:37 -0700540 public Closure addInvoke(Script.InvokeID invoke, Object... argsAndBindings) {
541 ArrayList<Object> args = new ArrayList<Object>();
542 Map<Script.FieldID, Object> bindingMap = new HashMap<Script.FieldID, Object>();
543 if (!seperateArgsAndBindings(argsAndBindings, args, bindingMap)) {
544 return null;
545 }
546 return addInvoke(invoke, args.toArray(), bindingMap);
547 }
548
Yang Ni4470d68a2015-03-16 16:23:50 -0700549 /**
550 * Creates a script group
551 *
552 * @param outputs futures intended as outputs of the script group
553 * @return a script group
554 */
555
Yang Nicc1ca482015-03-16 15:53:18 -0700556 public ScriptGroup2 create(Future... outputs) {
557 ScriptGroup2 ret = new ScriptGroup2(mRS, mClosures, mInputs, outputs);
558 return ret;
559 }
560
Yang Ni8ff29802015-03-11 16:25:37 -0700561 private boolean seperateArgsAndBindings(Object[] argsAndBindings,
562 ArrayList<Object> args,
563 Map<Script.FieldID, Object> bindingMap) {
564 int i;
565 for (i = 0; i < argsAndBindings.length; i++) {
566 if (argsAndBindings[i] instanceof Binding) {
567 break;
568 }
569 args.add(argsAndBindings[i]);
570 }
571
572 for (; i < argsAndBindings.length; i++) {
573 if (!(argsAndBindings[i] instanceof Binding)) {
574 return false;
575 }
576 Binding b = (Binding)argsAndBindings[i];
Yang Ni4470d68a2015-03-16 16:23:50 -0700577 bindingMap.put(b.getField(), b.getValue());
Yang Ni8ff29802015-03-11 16:25:37 -0700578 }
579
580 return true;
581 }
582
Yang Nicc1ca482015-03-16 15:53:18 -0700583 }
Yang Ni281c3252014-10-24 08:52:24 -0700584}