SNAKE GAME- snake passing through barrier (PYTHON 3.8-TURTLE MODULE) - python-turtle

Tried putting it as a range but says that two operations cannot be done.
Check for collisions with barrier
if (head.ycor()<200 and head.ycor()>-200 and head.xcor()<10 and head.xcor()>-10):
time.sleep(1)
head.goto(-70, 0)
head.direction = "stop"
head.clear()
# Barrier
barrier = turtle.Turtle()
barrier.shape("square")
barrier.color("yellow")
barrier.speed(0)
barrier.goto(0, 0)
barrier.shapesize(stretch_len=1, stretch_wid=20)
barrier.penup()

Related

What does a "for i, v" loop do?

I understand, "for i, v" loops for tables, i is the index, and v is the value, but what does this script do? I do not think this has anything to do with tables, but the only type of for table loops I know in ROBLOX script is the first one I mentioned; "for i, v" loops, which loop through tables.
randomVariable = 1
for i = 1, randomVariable do
(random script)
end
This is a numeric loop statement.
for controlValue = startValue, endValue, stepValue do
-- for body
end
It goes from startValue until it reaches endValue, after running body code, controlValue is increased by stepValue. If controlValue is higher or equals to endValue the loop stops. If stepValue is not provided, it equals to 1.
It's equivalent to this code:
local controlValue = startValue
if not stepValue then stepValue = 1 end -- if no stepValue it equals to 1
while controlValue < endValue do
-- for body
controlValue = controlValue + stepValue
end
There are a few different ways to loop in Lua.
while variable < number do
repeat stuff until variable == number
for key, value in pairs do
for index, value in ipairs do
for i = 1, number do
i = 1 is the initial condition.
It usually begins at one, then loops through items in a table.
So you can think of it as an index in that regard.
where the randomVariable you mentioned would be #tab
however, you could set that initial condition to be larger, then count down.
for i = #tab, 1, -1 do
the third, optional argument is called "step size"
and it's the amount that initial condition is changed, after completing each loop. it defaults to 1, so it's not needed, most of the time.
so to step through all the even numbers in a table it would be
for i = 2, #tab, 2 do
Further reading: https://www.tutorialspoint.com/lua/lua_loops.htm

How to disable omp in Torch nn package?

Specifically I would like nn.LogSoftMax to not use omp when the size of the input tensor is small. I have a small script to test the run time.
require 'nn'
my_lsm = function(t)
o = torch.zeros((#t)[1])
sum = 0.0
for i = 1,(#t)[1] do
o[i] = torch.exp(t[i])
sum = sum + o[i]
end
o = o / sum
return torch.log(o)
end
ii=torch.randn(arg[1])
m=nn.LogSoftMax()
timer = torch.Timer()
timer:stop()
timer:reset()
timer:resume()
my_lsm(ii)
print(timer:time().real)
timer:stop()
timer:reset()
timer:resume()
m:forward(ii)
print(timer:time().real)
If arg[1] is 10, then my basic log softmax function run much faster:
0.00021696090698242
0.033425092697144
But once arg[1] is 10,000,000, omp really helps a lot:
29.561321973801
0.11547803878784
So I suspect that omp overhead is very high. If my code has to call log softmax several times with small inputs (says tensor size is only 3), it will cost too much time. Is there a way to manually disable omp usage in some cases (but not always)?
Is there a way to manually disable omp usage in some cases (but not always)?
If you really want to do that one possibility is to use torch.setnumthreads and torch.getnumthreads like that:
local nth = torch.getnumthreads()
torch.setnumthreads(1)
-- do something
torch.setnumthreads(nth)
So you can monkey-patch nn.LogSoftMax as follow:
nn.LogSoftMax.updateOutput = function(self, input)
local nth = torch.getnumthreads()
torch.setnumthreads(1)
local out = input.nn.LogSoftMax_updateOutput(self, input)
torch.setnumthreads(nth)
return out
end

(Lua) Why is my producer-consumer Lua coroutines experiment not yielding the expected resault?

I have created the following two coroutines a producer and a consumer in an attempt to learn/understand the coroutines.
function count01to10()
for i = 1, 10 do
coroutine.yield(i)
end
end
function printNumber(number)
while number ~= nil do
print("Counter: ", number)
coroutine.yield()
end
end
function main()
local number = 0
print("Creating coroutines")
local counter = coroutine.create(count01to10)
local printer = coroutine.create(printNumber)
print("Executing coroutines")
while (10 > number) do
isSuccessuful, number = coroutine.resume(counter)
print("counter: ", coroutine.status(counter))
coroutine.resume(printer, number)
print("printer: ", coroutine.status(printer))
end
print("Finished")
end
main()
The output is:
Creating coroutines
Executing coroutines
counter: suspended
Counter: 1
printer: suspended
counter: suspended
Counter: 1
printer: suspended
...
Counter: 1
printer: suspended
Finished
I am expecting the ouput to print out the numbers 1 to 10. Why is this not happening and is that a proper way to use coroutines?
A coroutine resumes at the same point where it yield (or just after it), not at the beginning.
Your code for printNumber does not change number, so the output you get is not surprising.
To fix this, use number=coroutine.yield() in printNumber.
The arguments passed to resume are returned by yield.

Computercraft Lua change nil to 0

I am using Computercraft, a Minecraft mod, and I needed help with something. I am attempting to find all peripherals of a type, get the energy from them, and add them together. However, I get a "attempt to perform arithmetic on nill" or something error. Here is my code:
local periList = peripheral.getNames()
energy = 0
totalenergy = 0
for i = 1, #periList do
if peripheral.getType(periList[i]) == "cofh_thermalexpansion_energycell" then
local cell = peripheral.wrap(periList[i])
print(periList[i])
if cell.getEnergyStored("potato") ~= "nil" then
energy = cell.getEnergyStored("potato")
print(cell.getEnergyStored("potato"))
else
energy = 0
print(0)
end
totalenergy = totalenergy + energy
end
end
print(totalenergy)
Sorry, codebox didn't work
Anyways, does anyone know how to fix this?
nil and "nil" are two different things.
The former is the nil type "singleton" the latter is a string of three characters. They are not equivalent.
Try dropping the quotes from the if line.
Also you can assign (the potential) nil to energy and then directly energy and set it to 0 if it is nil (or even just use
energy = cell.getEnergyStored("potato") or 0
directly since nil is a "false-y" value so nil or 0 evaluates to 0).

Can the STREAM and GUPS (single CPU) benchmark use non-local memory in NUMA machine

I want to run some tests from HPCC, STREAM and GUPS.
They will test memory bandwidth, latency, and throughput (in term of random accesses).
Can I start Single CPU test STREAM or Single CPU GUPS on NUMA node with memory interleaving enabled? (Is it allowed by the rules of HPCC - High Performance Computing Challenge?)
Usage of non-local memory can increase GUPS results, because it will increase 2- or 4- fold the number of memory banks, available for random accesses. (GUPS typically limited by nonideal memory-subsystem and by slow memory bank opening/closing. With more banks it can do update to one bank, while the other banks are opening/closing.)
Thanks.
UPDATE:
(you may nor reorder the memory accesses that the program makes).
But can compiler reorder loops nesting? E.g. hpcc/RandomAccess.c
/* Perform updates to main table. The scalar equivalent is:
*
* u64Int ran;
* ran = 1;
* for (i=0; i<NUPDATE; i++) {
* ran = (ran << 1) ^ (((s64Int) ran < 0) ? POLY : 0);
* table[ran & (TableSize-1)] ^= stable[ran >> (64-LSTSIZE)];
* }
*/
for (j=0; j<128; j++)
ran[j] = starts ((NUPDATE/128) * j);
for (i=0; i<NUPDATE/128; i++) {
/* #pragma ivdep */
for (j=0; j<128; j++) {
ran[j] = (ran[j] << 1) ^ ((s64Int) ran[j] < 0 ? POLY : 0);
Table[ran[j] & (TableSize-1)] ^= stable[ran[j] >> (64-LSTSIZE)];
}
}
The main loop here is for (i=0; i<NUPDATE/128; i++) { and the nested loop is for (j=0; j<128; j++) {. Using 'loop interchange' optimization, compiler can convert this code to
for (j=0; j<128; j++) {
for (i=0; i<NUPDATE/128; i++) {
ran[j] = (ran[j] << 1) ^ ((s64Int) ran[j] < 0 ? POLY : 0);
Table[ran[j] & (TableSize-1)] ^= stable[ran[j] >> (64-LSTSIZE)];
}
}
It can be done because this loop nest is perfect loop nest. Is such optimization prohibited by rules of HPCC?
As far as I can tell it is allowed given that the memory interleaving
is a system setting rather than a code modification (you may nor reorder
the memory accesses that the program makes).
If GUPS actually gets better performance with non-local memory on a
NUMA machine seems doubtful to me. Will bank conflict-induced latency
really be greater than the off-node memory access latency?
STREAM should not be limited by bank conflicts but will probably
benefit from off-node accesses if the CPU has an on-chip memory
controller (like the Opterons) since the bandwidth is then shared
between the local memory controller and the NUMA interconnect.

Resources