Timing of expect() #Chisel3 Tester - chisel

I would like to confirm that timing of the iotester of chisel3. I have long time did not touch the iotester, and now I do the testing. Then I confused the timing of the output on expect().
For example;
val reg = RegInit(Bool(), false.B)
...
reg = !io.input
io.output = reg
This can be tested by iotester as follows;
poke(c.io.input, 0)
step(1)
expect(c.io.output, 0)//Latch the input on reg
step(1)
expect(c.io.output, 1)//Output the reg
Is my understanding correct?
--
NaN

Not quite. Here's a filled out example
import chisel3._
import chiseltest._
import chiseltest.ChiselScalatestTester
import org.scalatest.FreeSpec
class Toggle extends MultiIOModule {
val in = IO(Input(Bool()))
val out = IO(Output(Bool()))
val reg = RegInit(false.B)
reg := ! in
out := reg
}
class ToggleTester extends FreeSpec with ChiselScalatestTester {
"test toggle" in {
test(new Toggle()) { c =>
c.in.poke(false.B) // clock is low
c.out.expect(false.B) // clock stil low, reg still at initial value
c.clock.step() // clock goes high
c.out.expect(true.B) // register has toggled
}
}
}
pokes take place when the clock has just gone low.
BTW, this example is using the new chiseltest test harness. I recommend using it over the older chisel-iotesters.

Related

Output var not calculating

This is a change counter program. The change is counting correctly, but the remainder is not (remChange()). After watching a few videos, and w3schools tutorials, I'm still coming up blank as to why this is. I've added a returnChange at the bottom of each function and was able to get that last 2 functions rounded (from the float). Did I do the floating point wrong in the println? Are my return tags in the wrong place? I'm just trying to get it to work by counting the coins and giving me back the change.
Here is my code: let me know, ill be researching in the meantime.
fun main(args: Array<String>) {
var change = 10.88
getDollars(change)
getQuarters(change)
getDimes(change)
getNickles(change)
}
fun getDollars(change: Double): Double{
val numofdolla = change/(1.00).toFloat();
println(numofdolla.toInt());
val remChange = change - numofdolla * 1.00;
println(remChange)
return change
}
fun getQuarters(change: Double): Double{
val numofqtr = change/(0.25).toFloat();
println(numofqtr.toInt());
val remChange = change - numofqtr * 0.25;
println(remChange)
return change
}
fun getDimes(change: Double): Double{
val numofdime = change/(0.10).toFloat();
println(numofdime.toInt());
val remChange = change - numofdime * 0.10;
println("%.2f".format(remChange))
return change
}
fun getNickles(change: Double): Double{
val numofnick = change/(0.05).toFloat();
println(numofnick.toInt());
val remChange = change - numofnick * 0.05;
println("%.2f".format(remChange))
return change
}
I'm not looking for anyone to write my code, as an explanation would be just perfect.
I'm not sure what you're expecting to happen here, but the title of your question seems to suggest that you expect the change variable to change when calling those helper functions.
You cannot mutate a function argument within a function in Kotlin, but you can indeed return a new value from the function. It seems you tried to do that, but here your main function is not using the return values of the getXxxx functions. So your change variable is never updated.
You can update the change variable based on the result of those functions:
fun main(args: Array<String>) {
var change = 10.88
change = getDollars(change)
change = getQuarters(change)
change = getDimes(change)
change = getNickles(change)
}
That said, those functions currently return the original change variable (which is unchanged), so if you want the value to change, you probably want to return remChange instead.

ffmpeg azure function consumption plan low CPU availability for high volume requests

I am running an azure queue function on a consumption plan; my function starts an FFMpeg process and accordingly is very CPU intensive. When I run the function with less than 100 items in the queue at once it works perfectly, azure scales up and gives me plenty of servers and all of the tasks complete very quickly. My problem is once I start doing more than 300 or 400 items at once, it starts fine but after a while the CPU slowly goes from 80% utilisation to only around 10% utilisation - my functions cant finish in time with only 10% CPU. This can be seen in the image shown below.
Does anyone know why the CPU useage is going lower the more instances my function creates? Thanks in advance Cuan
edit: the function is set to only run one at a time per instance, but the problem exists when set to 2 or 3 concurrent processes per instance in the host.json
edit: the CPU drops get noticeable at 15-20 servers, and start causing failures at around 60. After that the CPU bottoms out at an average of 8-10% with individuals reaching 0-3%, and the server count seems to increase without limit (which would be more helpful if I got some CPU with the servers)
Thanks again, Cuan.
I've also added the function code to the bottom of this post in case it helps.
using System.Net;
using System;
using System.Diagnostics;
using System.ComponentModel;
public static void Run(string myQueueItem, TraceWriter log)
{
log.Info($"C# Queue trigger function processed a request: {myQueueItem}");
//Basic Parameters
string ffmpegFile = #"D:\home\site\wwwroot\CommonResources\ffmpeg.exe";
string outputpath = #"D:\home\site\wwwroot\queue-ffmpeg-test\output\";
string reloutputpath = "output/";
string relinputpath = "input/";
string outputfile = "video2.mp4";
string dir = #"D:\home\site\wwwroot\queue-ffmpeg-test\";
//Special Parameters
string videoFile = "1 minute basic.mp4";
string sub = "1 minute sub.ass";
//guid tmp files
// Guid g1=Guid.NewGuid();
// Guid g2=Guid.NewGuid();
// string f1 = g1 + ".mp4";
// string f2 = g2 + ".ass";
string f1 = videoFile;
string f2 = sub;
//guid output - we will now do this at the caller level
string g3 = myQueueItem;
string outputGuid = g3+".mp4";
//get input files
//argument
string tmp = subArg(f1, f2, outputGuid );
//String.Format("-i \"" + #"input/tmp.mp4" + "\" -vf \"ass = '" + sub + "'\" \"" + reloutputpath +outputfile + "\" -y");
log.Info("ffmpeg argument is: "+tmp);
//startprocess parameters
Process process = new Process();
process.StartInfo.FileName = ffmpegFile;
process.StartInfo.Arguments = tmp;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WorkingDirectory = dir;
//output handler
process.OutputDataReceived += new DataReceivedEventHandler(
(s, e) =>
{
log.Info("O: "+e.Data);
}
);
process.ErrorDataReceived += new DataReceivedEventHandler(
(s, e) =>
{
log.Info("E: "+e.Data);
}
);
//start process
process.Start();
log.Info("process started");
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
public static void getFile(string link, string fileName, string dir, string relInputPath){
using (var client = new WebClient()){
client.DownloadFile(link, dir + relInputPath+ fileName);
}
}
public static string subArg(string input1, string input2, string output1){
return String.Format("-i \"" + #"input/" +input1+ "\" -vf \"ass = '" + #"input/"+input2 + "'\" \"" + #"output/" +output1 + "\" -y");
}
When you use the D:\home directory you are writing to the virtual function, which means each instance has to continually try to write to the same spot as the functions run which causes the massive I/O block. Instead writing to D:\local and then sending the finished file somewhere else solves that issue, this way rather than each instance constantly writing to a location they only write when completed, and write to a location designed to handle high throughput.
The easiest way I could find to manage the input and output after writing to D:\local was just to hook up the function to an azure storage container and handle the ins and outs that way. Doing so made the average CPU stay at 90-100% for upwards of 70 concurrent Instances.

Python Tkinter - How can you use a value from one function to another?

I am learning Python and Tkinter on my own and I have some questions regarding this code that I am writing.
I am trying to get the program to execute two functions that are part of a class.
So when I try to execute the below code tkinter executes the first function but does not execute function 2 and I don't understand why.
Can someone please help me with this.
And in function2 the value of j needs to equal the value of j from function1.
Thank you.
from tkinter import *
myapp=Tk()
myapp.geometry('1100x700+100+50')
myapp.title('List Generator')
input1=IntVar()
input2=IntVar()
class Myclass:
def __init__(self):
text1=input1.get()
text2=input2.get()
ex1=float(text1)
ex2=float(text2)
totbtu=float(ex1*ex2)
realbtu=totbtu+(totbtu*0.15)
j= float(totbtu + 100)
Label(myapp, text=totbtu).place(x=600,y=20)
Label(myapp, text=realbtu).place(x=600,y=60)
Label(myapp, text=j).place(x=600,y=100)
def function2(self):
h=j+33
Label(myapp, text=h).place(x=600,y=140)
label1 = Label(myapp, text='Enter Area').place(x=10,y=10)
area_entry=Entry(myapp,textvariable=input1 ).place(x=140,y=10)
label11 = Label(myapp,text='SQ FT',).place(x=270,y=10)
label2 = Label(myapp, text='Enter Load').place(x=10,y=35)
area_entry=Entry(myapp,textvariable=input2 ).place(x=140,y=35)
label22 = Label(myapp,text="BTU's/SQ FT",).place(x=270,y=35)
button1 = Button(myapp, text = 'Generate',padx=5,pady=5,command=Myclass).place(x=10,y=70)
myapp.mainloop()
If I understand your question correctly, you need to call 2 functions with one button command and you also need to use one value in both functions (I'm assuming).
Take a look at my solution:
from Tkinter import *
myapp=Tk()
myapp.geometry('1100x700+100+50')
myapp.title('List Generator')
input1=IntVar()
input2=IntVar()
j = 0 #needed for the value to be referenced in both function1 and function2
def function1():
global j
text1=input1.get()
text2=input2.get()
ex1=float(text1)
ex2=float(text2)
totbtu=float(ex1*ex2)
realbtu=totbtu+(totbtu*0.15)
j= float(totbtu + 100)
Label(myapp, text=totbtu).place(x=600,y=20)
Label(myapp, text=realbtu).place(x=600,y=60)
Label(myapp, text=j).place(x=600,y=100)
print(j)
def function2():
global j
h=j+33
Label(myapp, text=h).place(x=600,y=140)
print(j)
def wombocombo():
function1()
function2()
label1 = Label(myapp, text='Enter Area').place(x=10,y=10)
area_entry=Entry(myapp,textvariable=input1 ).place(x=140,y=10)
label11 = Label(myapp,text='SQ FT',).place(x=270,y=10)
label2 = Label(myapp, text='Enter Load').place(x=10,y=35)
area_entry=Entry(myapp,textvariable=input2 ).place(x=140,y=35)
label22 = Label(myapp,text="BTU's/SQ FT",).place(x=270,y=35)
button1 = Button(myapp, text = 'Generate',padx=5,pady=5,command=wombocombo).place(x=10,y=70)
myapp.mainloop()
When you dictate a function to be called when a button is pressed, you use command=myFunction myFunction is a function, not a class. And as such, when a class like Myclass is the command instead of a function, problems will occur.
To execute 2 functions with 1 command, simply combine both functions into 1. If you take a look at the wombocombo function, it will call both function1 and function2, hitting 2 birds with 1 stone.
As for sharing a variable between both functions, I always find it easiest to go broader one step on the scope ladder and add my variable there.
In this example, we needed the same j variable in both function1 and function2. Going broader in scope, we define j in a place that both function1 and function2 can access.

Is it possible to use absolute component positioning in Scala/Swing?

I am using Swing in Scala "org.scala-lang" % "scala-swing" % "2.11.0-M7".
I want to set position for my components explicitly. It is possible to do in Swing API for Java.
Question: is it possible to set absolute position for components in Swing Scala API?
Swing API for Scala example:
import scala.swing._
object PositionAbsolute extends SimpleSwingApplication {
lazy val top = new MainFrame() {
title = "PositionAbsolute"
val label = new Label("I want to be at (0, 0)")
val panel = new FlowPanel()
panel.preferredSize = new swing.Dimension(300, 400)
panel.contents += label
contents = panel
}
}
I know it's a little late for a response - and I am not at all an expert in these things, so please bear with me.
If you absolutely want or need to do absolute positioning of controls with swing in scala, here is a way to do it:
import scala.swing.{Button, Dimension, MainFrame}
object Main extends App {
val b1 = new Button {
text = "one"
preferredSize = new Dimension(60, 30)
}
val b2 = new Button {
text = "two"
preferredSize = new Dimension(80, 40)
}
val b3 = new Button("three")
b1.peer.setBounds(25, 5, b1.peer.getPreferredSize.width, b1.peer.getPreferredSize.height)
b2.peer.setBounds(55, 50, b2.peer.getPreferredSize.width, b2.peer.getPreferredSize.height)
b3.peer.setBounds(150, 15, b3.peer.getPreferredSize.width, b3.peer.getPreferredSize.height)
javax.swing.SwingUtilities.invokeLater(() => {
val frame: MainFrame = new MainFrame {
title = "AbsoluteLayoutDemo"
resizable = true
size = new Dimension(300, 150)
}
frame.peer.setLayout(null)
frame.peer.add(b1.peer)
frame.peer.add(b2.peer)
frame.peer.add(b3.peer)
frame.visible = true
})
}
I don't like it very much myself, but this works.
I compiled this code with scala version 2.13.8 and
libraryDependencies += "org.scala-lang.modules" %% "scala-swing" % "3.0.0"
in the build.sbt file
This was my translation of the java-example in docs.oracle.com/javase/tutorial/uiswing/layout/none.html
but I made a few changes that I thought would make sense for a scala example.
I am not exactly sure what the consequences of this approach are, so please use at your own risk - because I am really not sure how this works.

Groovy-Issue: recalling a closure inside of a vbox again, when a spinner's stateChanged fires an action

If you take the numPanels as a fixed value of e.g. 20, it's really a nice and groovy swinging.
But I try since two days to add more or less panels with a groovy.swing.Spinner dynamically (my program will be used to connect to different databases and exchange values from different db-tables). I tried with binding, revalidating, repainting - but I can't bring the closure to take the new value from the spinner...
import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.JScrollPane
import javax.swing.BoxLayout as BXL
import javax.swing.SpinnerNumberModel
int numPanels = 2
swing = new SwingBuilder()
def setPanelAmount = swing.action(name:'Amount of Panels in vbox-element', closure: this.&setPanelAmount )
frame = swing.frame(title:'test', pack:true, visible:true, defaultCloseOperation:WC.HIDE_ON_CLOSE) {
panel(id:'mainPanel'){
scrollPane( verticalScrollBarPolicy:JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ) {
vbox {
(1..numPanels).each { num ->
def panelID = "panel$num"
def pane = panel( alignmentX:0f, id:panelID, background:java.awt.Color.GREEN ) {
label('description')
textField( id: "description$num", text:panelID, columns: 70 )
button( id: "buttonpanel$num", text:panelID, actionPerformed:{
swing."$panelID".background = java.awt.Color.RED
} )
}
}
}
}
boxLayout(axis: BXL.Y_AXIS)
panel(id:'secondPanel' , alignmentX: 0f){
hbox(){
label 'Change amount of panels:'
hstrut(10)
spinner(id: 'numPanelSpinner', stateChanged: this.&setPanelAmount, model: new SpinnerNumberModel(2, 2, 10, 1))
hstrut(50)
button('Quit', actionPerformed:{
frame.visible = false
})
}
}
}
}
frame.size = [ frame.width, 600 ]
def setPanelAmount(event) {
numPanels = swing.numPanelSpinner.getValue()
}
It has to be combined with the following code - but unfortunately, it's not working with an included closure - at least as far as I know now...
import groovy.swing.*
import javax.swing.*
import java.awt.*
import java.awt.BorderLayout as BL
new SwingBuilder().edt {
frame(defaultCloseOperation: JFrame.EXIT_ON_CLOSE, visible: true, size: [600,500]) {
panel(id:'main') {
panel {
button(name:'x', action: action(name:'add', closure:{p.add(label('new')); p.revalidate()}))
button(action: action(name:'remove', closure:{p.removeAll();p.revalidate();scroll.repaint()}))
}
panel() {
scrollPane(id:'scroll',preferredSize: [200,200], constraints: context.CENTER) {
panel(id:'p') {
checkBoxList(listData: (1..20).collect([]){"Option $it"} as Object[])
}
}
}
}
}
}
BTW.: do you like to improve in programming groovy, too?
I would be glad to start a git-based "engineering" with my program.
This program will compare a user given amount of lists with articles coming from one or more databases or excel-files, save them and every result in PostgreSQL or MSSQL.
Every list may have zero, one or more articles listed identified by their article-numbers, with varying amounts in each entity.
And the user can - guided by a GUI - decide in which order to add or subtract the entities from another. The other (zero to infinite) attributes beside of article-number and amount will be carried through the lists.
Not too complicated but for a first project...