/** * Check to see if the buffer size for the current schedule is greater than the previous size. If * so, set the buffer size to the current buffer size needed. * * @exception IllegalActionException If thrown while getting helper or buffer size. */ protected void _updatePortBufferSize() throws IllegalActionException { ptolemy.domains.sdf.kernel.SDFDirector director = (ptolemy.domains.sdf.kernel.SDFDirector) getComponent(); CompositeActor container = (CompositeActor) director.getContainer(); ptolemy.codegen.c.actor.TypedCompositeActor containerHelper = (ptolemy.codegen.c.actor.TypedCompositeActor) _getHelper(container); Iterator actors = container.deepEntityList().iterator(); while (actors.hasNext()) { Actor actor = (Actor) actors.next(); CodeGeneratorHelper actorHelper = (CodeGeneratorHelper) _getHelper((NamedObj) actor); Iterator inputPorts = actor.inputPortList().iterator(); while (inputPorts.hasNext()) { IOPort inputPort = (IOPort) inputPorts.next(); for (int k = 0; k < inputPort.getWidth(); k++) { int newCapacity = getBufferSize(inputPort, k); int oldCapacity = actorHelper.getBufferSize(inputPort, k); if (newCapacity > oldCapacity) { actorHelper.setBufferSize(inputPort, k, newCapacity); } } } } Iterator outputPorts = container.outputPortList().iterator(); while (outputPorts.hasNext()) { IOPort outputPort = (IOPort) outputPorts.next(); for (int k = 0; k < outputPort.getWidthInside(); k++) { int newCapacity = getBufferSize(outputPort, k); int oldCapacity = containerHelper.getBufferSize(outputPort, k); if (newCapacity > oldCapacity) { containerHelper.setBufferSize(outputPort, k, newCapacity); } } } }
/** * Generate code for transferring enough tokens to fulfill the output production rate. * * @param outputPort The port to transfer tokens. * @param code The string buffer that the generated code is appended to. * @exception IllegalActionException If thrown while transferring tokens. */ public void generateTransferOutputsCode(IOPort outputPort, StringBuffer code) throws IllegalActionException { code.append( CodeStream.indent( _codeGenerator.comment("SDFDirector: " + "Transfer tokens to the outside."))); int rate = DFUtilities.getTokenProductionRate(outputPort); CompositeActor container = (CompositeActor) getComponent().getContainer(); ptolemy.codegen.c.actor.TypedCompositeActor compositeActorHelper = (ptolemy.codegen.c.actor.TypedCompositeActor) _getHelper(container); if (container instanceof TypedCompositeActorWithCoSimulation) { if (_portNumber == 0) { int numberOfOutputPorts = container.outputPortList().size(); code.append(_INDENT2 + "jobjectArray tokensToAllOutputPorts;" + _eol); code.append( _INDENT2 + "jclass objClass = (*env)->FindClass(env, " + "\"Ljava/lang/Object;\");" + _eol); code.append( _INDENT2 + "tokensToAllOutputPorts = " + "(*env)->NewObjectArray(env, " + numberOfOutputPorts + ", objClass, NULL);" + _eol); } String portName = outputPort.getName(); String tokensToThisPort = "tokensTo" + portName; ; Type type = ((TypedIOPort) outputPort).getType(); int numberOfChannels = outputPort.getWidthInside(); code.append(_INDENT2 + "jobjectArray " + tokensToThisPort + ";" + _eol); if (type == BaseType.INT) { if (!_intFlag) { code.append(_INDENT2 + "jclass objClassI = (*env)->FindClass(env, " + "\"[I\");" + _eol); _intFlag = true; } code.append( _INDENT2 + tokensToThisPort + " = (*env)->NewObjectArray(env, " + numberOfChannels + ", objClassI, NULL);" + _eol); } else if (type == BaseType.DOUBLE) { if (!_doubleFlag) { code.append(_INDENT2 + "jclass objClassD = (*env)->FindClass(env, " + "\"[D\");" + _eol); _doubleFlag = true; } code.append( _INDENT2 + tokensToThisPort + " = (*env)->NewObjectArray(env, " + numberOfChannels + ", objClassD, NULL);" + _eol); } else { // FIXME: need to deal with other types } for (int i = 0; i < outputPort.getWidthInside(); i++) { String tokensToOneChannel = "tokensToOneChannelOf" + portName; if (i == 0) { if (type == BaseType.INT) { code.append(_INDENT2 + "jint " + tokensToOneChannel + "[" + rate + "];" + _eol); } else if (type == BaseType.DOUBLE) { code.append(_INDENT2 + "jdouble " + tokensToOneChannel + "[" + rate + "];" + _eol); } else { // FIXME: need to deal with other types } } String portNameWithChannelNumber = portName; if (outputPort.isMultiport()) { portNameWithChannelNumber = portName + '#' + i; } for (int k = 0; k < rate; k++) { code.append( _INDENT2 + tokensToOneChannel + "[" + k + "] = " + compositeActorHelper.getReference("@" + portNameWithChannelNumber + "," + k) + ";" + _eol); } String tokensToOneChannelArray = "arr" + portName + i; if (type == BaseType.INT) { code.append( _INDENT2 + "jintArray " + tokensToOneChannelArray + " = " + "(*env)->NewIntArray(env, " + rate + ");" + _eol); code.append( _INDENT2 + "(*env)->SetIntArrayRegion" + "(env, " + tokensToOneChannelArray + ", 0, " + rate + ", " + tokensToOneChannel + ");" + _eol); } else if (type == BaseType.DOUBLE) { code.append( _INDENT2 + "jdoubleArray " + tokensToOneChannelArray + " = " + "(*env)->NewDoubleArray(env, " + rate + ");" + _eol); code.append( _INDENT2 + "(*env)->SetDoubleArrayRegion" + "(env, " + tokensToOneChannelArray + ", 0, " + rate + ", " + tokensToOneChannel + ");" + _eol); } else { // FIXME: need to deal with other types } code.append( _INDENT2 + "(*env)->SetObjectArrayElement" + "(env, " + tokensToThisPort + ", " + i + ", " + tokensToOneChannelArray + ");" + _eol); code.append( _INDENT2 + "(*env)->DeleteLocalRef(env, " + tokensToOneChannelArray + ");" + _eol); } code.append( _INDENT2 + "(*env)->SetObjectArrayElement" + "(env, tokensToAllOutputPorts, " + _portNumber + ", " + tokensToThisPort + ");" + _eol); code.append(_INDENT2 + "(*env)->DeleteLocalRef(env, " + tokensToThisPort + ");" + _eol); _portNumber++; } else { for (int i = 0; i < outputPort.getWidthInside(); i++) { if (i < outputPort.getWidth()) { String name = outputPort.getName(); if (outputPort.isMultiport()) { name = name + '#' + i; } for (int k = 0; k < rate; k++) { code.append(CodeStream.indent(compositeActorHelper.getReference(name + "," + k))); code.append(" =" + _eol); code.append( CodeStream.indent( _INDENT2 + compositeActorHelper.getReference("@" + name + "," + k))); code.append(";" + _eol); } } } } // The offset of the ports connected to the output port is // updated by outside director. _updatePortOffset(outputPort, code, rate); }
/** * Generate code for transferring enough tokens to complete an internal iteration. * * @param inputPort The port to transfer tokens. * @param code The string buffer that the generated code is appended to. * @exception IllegalActionException If thrown while transferring tokens. */ public void generateTransferInputsCode(IOPort inputPort, StringBuffer code) throws IllegalActionException { code.append( CodeStream.indent( _codeGenerator.comment("SDFDirector: " + "Transfer tokens to the inside."))); int rate = DFUtilities.getTokenConsumptionRate(inputPort); CompositeActor container = (CompositeActor) getComponent().getContainer(); ptolemy.codegen.c.actor.TypedCompositeActor compositeActorHelper = (ptolemy.codegen.c.actor.TypedCompositeActor) _getHelper(container); if (container instanceof TypedCompositeActorWithCoSimulation) { Type type = ((TypedIOPort) inputPort).getType(); String portName = inputPort.getName(); for (int i = 0; i < inputPort.getWidth(); i++) { if (i < inputPort.getWidthInside()) { String tokensFromOneChannel = "tokensFromOneChannelOf" + portName + i; String pointerToTokensFromOneChannel = "pointerTo" + tokensFromOneChannel; code.append(_INDENT2); code.append( "jobject " + tokensFromOneChannel + " = (*env)->GetObjectArrayElement(env, " + portName + ", " + i + ");" + _eol); code.append(_INDENT2); if (type == BaseType.INT) { code.append( "jint * " + pointerToTokensFromOneChannel + " = (*env)->GetIntArrayElements" + "(env, " + tokensFromOneChannel + ", NULL);" + _eol); } else if (type == BaseType.DOUBLE) { code.append( "jdouble * " + pointerToTokensFromOneChannel + " = (*env)->GetDoubleArrayElements" + "(env, " + tokensFromOneChannel + ", NULL);" + _eol); } else { // FIXME: need to deal with other types } String portNameWithChannelNumber = portName; if (inputPort.isMultiport()) { portNameWithChannelNumber = portName + '#' + i; } for (int k = 0; k < rate; k++) { code.append(_INDENT2); code.append( compositeActorHelper.getReference("@" + portNameWithChannelNumber + "," + k)); code.append(" = " + pointerToTokensFromOneChannel + "[" + k + "];" + _eol); } code.append(_INDENT2); if (type == BaseType.INT) { code.append( "(*env)->ReleaseIntArrayElements(env, " + tokensFromOneChannel + ", " + pointerToTokensFromOneChannel + ", 0);" + _eol); } else if (type == BaseType.DOUBLE) { code.append( "(*env)->ReleaseDoubleArrayElements(env, " + tokensFromOneChannel + ", " + pointerToTokensFromOneChannel + ", 0);" + _eol); } else { // FIXME: need to deal with other types } } } } else { for (int i = 0; i < inputPort.getWidth(); i++) { if (i < inputPort.getWidthInside()) { String name = inputPort.getName(); if (inputPort.isMultiport()) { name = name + '#' + i; } for (int k = 0; k < rate; k++) { code.append(CodeStream.indent(compositeActorHelper.getReference("@" + name + "," + k))); code.append(" =" + _eol); code.append( CodeStream.indent(_INDENT2 + compositeActorHelper.getReference(name + "," + k))); code.append(";" + _eol); } } } } // The offset of the input port itself is updated by outside director. _updateConnectedPortsOffset(inputPort, code, rate); }