Why does one of these two indexing patterns give me an "index out of bounds" error? - indexing

With ncsim the following code throws the error:
Bit-select or part-select index out of declared bounds.
However, the commented out code which does exactly the same thing doesn't. Am I missing something or is the compiler mistaken?
module pd__test;
genvar i, j;
reg [10-1:0] assign_from_reg;
reg [256:0] assign_to_reg;
generate
for (i=0; i<2; i=i+1) begin
for (j=0; j<13; j=j+1) begin
always #* begin
if (i+2*j < 25) begin
// gives me an index out of bounds error
assign_to_reg[10*(i+2*j)+9 : 10*(i+2*j)] = assign_from_reg;
// gives me no such error, however the indices are the same
// assign_to_reg[10*(i+2*j)+9 -: 10] = assign_from_reg;
end else begin
// do something else
end
end
end
end
endgenerate
endmodule
I ran a python script to print the indeces to double check:
for i in range(2):
for j in range(13):
if (i+(2*j) < 25):
print("[", 10*(i+(2*j))+9, ":", 10*(i+(2*j)), "]")
Prints:
[ 9 : 0 ]
[ 29 : 20 ]
[ 49 : 40 ]
[ 69 : 60 ]
[ 89 : 80 ]
[ 109 : 100 ]
[ 129 : 120 ]
[ 149 : 140 ]
[ 169 : 160 ]
[ 189 : 180 ]
[ 209 : 200 ]
[ 229 : 220 ]
[ 249 : 240 ]
[ 19 : 10 ]
[ 39 : 30 ]
[ 59 : 50 ]
[ 79 : 70 ]
[ 99 : 90 ]
[ 119 : 110 ]
[ 139 : 130 ]
[ 159 : 150 ]
[ 179 : 170 ]
[ 199 : 190 ]
[ 219 : 210 ]
[ 239 : 230 ]

you asked verilog compiler to compile this code in the always block
assign_to_reg[10*(i+2*j)+9 -: 10]
which for i == 1 and j == 12 is generated by the generate block as:
assign_to_reg[259 : 250]
The above is clearly outside of the declared bounds [256:0].
Moving the 'if' into the generate block, as #toolic suggested would cause verilog not to generate the last always block at all, therefore, it will not be compiled and no warning/error would be produced.
So, the other solution with generate blocks would be to declare your assign_to_reg as [259:0].
And yet the best solution would be to get rid of the generate block all together and move all your loops inside the single always block:
always #* begin
for (int i=0; i<2; i=i+1) begin
for (int j=0; j<13; j=j+1) begin
if (i+2*j < 25) begin
assign_to_reg[10*(i+2*j)+9 -: 10] = assign_from_reg;
end
end
end
end
This should let compiler to calculate indexes at run-time and will not cause out-of-bound access as well.

Move the conditional if (i+2*j < 25) outside of the always block:
module pd__test;
genvar i, j;
reg [10-1:0] assign_from_reg;
reg [256:0] assign_to_reg;
generate
for (i=0; i<2; i=i+1) begin
for (j=0; j<13; j=j+1) begin
if (i+2*j < 25) begin
always #* begin
//assign_to_reg[10*(i+2*j)+9 : 10*(i+2*j)] = assign_from_reg;
assign_to_reg[10*(i+2*j)+9 -: 10] = assign_from_reg;
end
end
end
end
endgenerate
endmodule
Both assignments compile without warnings or errors for me.

Related

Tensorflow, Reshape like a convolution

I have a matrix [3,3,256], my final output must be [4,2,2,256], I have to use a reshape like a 'convolution' without changing the values. (In this case using a filter 2x2). Is there a method to do this using tensorflow?
If I understand your question correctly, you want to store the original values redundantly in the new structure, like this (without the last dim of 256):
[ [ 1 2 3 ] [ [ 1 2 ] [ [ 2 3 ] [ [ 4 5 ] [ [ 5 6 ]
[ 4 5 6 ] => [ 4 5 ] ], [ 5 6 ] ], [ 7 8 ] ], [ 8 9 ] ]
[ 7 8 9 ] ]
If yes, you can use indexing, like this, with x being the original tensor, and then stack them:
x2 = []
for i in xrange( 2 ):
for j in xrange( 2 ):
x2.append( x[ i : i + 2, j : j + 2, : ] )
y = tf.stack( x2, axis = 0 )
Based on your comment, if you really want to avoid using any loops, you might utilize the tf.extract_image_patches, like below (tested code) but you should run some tests because this might actually be much worse than the above in terms of efficiency and perfomance:
import tensorflow as tf
sess = tf.Session()
x = tf.constant( [ [ [ 1, -1 ], [ 2, -2 ], [ 3, -3 ] ],
[ [ 4, -4 ], [ 5, -5 ], [ 6, -6 ] ],
[ [ 7, -7 ], [ 8, -8 ], [ 9, -9 ] ] ] )
xT = tf.transpose( x, perm = [ 2, 0, 1 ] ) # have to put channel dim as batch for tf.extract_image_patches
xTE = tf.expand_dims( xT, axis = -1 ) # extend dims to have fake channel dim
xP = tf.extract_image_patches( xTE, ksizes = [ 1, 2, 2, 1 ],
strides = [ 1, 1, 1, 1 ], rates = [ 1, 1, 1, 1 ], padding = "VALID" )
y = tf.transpose( xP, perm = [ 3, 1, 2, 0 ] ) # move dims back to original and new dim up front
print( sess.run(y) )
Output (horizontal separator lines added manually for readability):
[[[[ 1 -1]
[ 2 -2]]
[[ 4 -4]
[ 5 -5]]]
[[[ 2 -2]
[ 3 -3]]
[[ 5 -5]
[ 6 -6]]]
[[[ 4 -4]
[ 5 -5]]
[[ 7 -7]
[ 8 -8]]]
[[[ 5 -5]
[ 6 -6]]
[[ 8 -8]
[ 9 -9]]]]
I have a similar problem with you and I found that in tf.contrib.kfac.utils there is a function called extract_convolution_patches. Suppose you have a tensor X with shape (1, 3, 3, 256) where the initial 1 marks batch size, you can call
Y = tf.contrib.kfac.utils.extract_convolution_patches(X, (2, 2, 256, 1), padding='VALID')
Y.shape # (1, 2, 2, 2, 2, 256)
The first two 2's will be the number of your output filters (makes up the 4 in your description). The latter two 2's will be the shape of the filters. You can then call
Y = tf.reshape(Y, [4,2,2,256])
to get your final result.

How would I print the elements matching a regex pattern in order?

I have a text file that has text in this format:
ptr[0] = Alloc(1) returned 1000 (searched 1 elements)
Free List [ Size 1 ]: [ addr:1001 sz:99 ]
Free(ptr[0]) returned 0
Free List [ Size 2 ]: [ addr:1000 sz:1 ] [ addr:1001 sz:99 ]
ptr[1] = Alloc(7) returned 1001 (searched 2 elements)
Free List [ Size 2 ]: [ addr:1000 sz:1 ] [ addr:1008 sz:92 ]
Free(ptr[1]) returned 0
Free List [ Size 3 ]: [ addr:1000 sz:1 ] [ addr:1001 sz:7 ] [ addr:1008 sz:92 ]
ptr[2] = Alloc(5) returned 1001 (searched 3 elements)
Free List [ Size 3 ]: [ addr:1000 sz:1 ] [ addr:1006 sz:2 ] [ addr:1008 sz:92 ]
Free(ptr[2]) returned 0
Free List [ Size 5 ]: [ addr:1000 sz:1 ] [ addr:1001 sz:5 ] [ addr:1006 sz:2 ] [ addr:1008 sz:8 ] [ addr:1016 sz:84 ]
And I am trying to print out only the values that match with the sz: in the text file and print them in the order they are in but as a list. Like so:
$ awk -f list.awk file.txt | head
99
1 99
1 92
1 7 92
1 2 92
1 5 2 8 84
I've tried the following, but it prints out only the lines that contain sz:. How could I break it further to get the output I want?
/Free List/{
s = $0
split(s, a, /sz:/)
print s
}
Following awk solutions may help you on same.
Solution 1st: When you want only digit value associated with string sz then following may help you on same.
awk '{while(match($0,/sz:[0-9]+/)){val=(val?val FS:"") substr($0,RSTART+3,RLENGTH-3);$0=substr($0,RSTART+RLENGTH)}}val!=""{print val;val=""}' Input_file
Adding a non-one liner form of solution too now.
awk '
{
while(match($0,/sz:[0-9]+/)){
val=(val?val FS:"") substr($0,RSTART+3,RLENGTH-3);
$0=substr($0,RSTART+RLENGTH)}
}
val!=""{
print val;
val=""
}
' Input_file
Solution 2nd: In case you need to have string sz also with values then following may help you on same.
awk '{while(match($0,/sz:[0-9]+/)){val=(val?val FS:"") substr($0,RSTART+3,RLENGTH-3);$0=substr($0,RSTART+RLENGTH)}}val!=""{print val;val=""}' Input_file
Adding a non one liner form of solution too now.
awk '
{
while(match($0,/sz:[0-9]+/)){
val=(val?val FS:"") substr($0,RSTART+3,RLENGTH-3);
$0=substr($0,RSTART+RLENGTH)}
}
val!=""{
print val;
val=""
}
' Input_file
NOTE: In case you want to perform this operation only on those lines which havd string Free List then add /Free List/{ before while and add } before ' in above solutions simply.
if perl is okay:
$ perl -lne 'print join " ", /sz:(\d+)/g if /Free List/' ip.txt
99
1 99
1 92
1 7 92
1 2 92
1 5 2 8 84
if /Free List/ if line contains Free List
/sz:(\d+)/g match all digits that follows sz:
print join " " print those matches separated by space
see https://perldoc.perl.org/perlrun.html#Command-Switches for details on -lne options
Using bash and grep:
while IFS= read -r line; do
x=$(grep -oP '(sz:\K\d+)' <<< "$line")
[[ $x ]] && echo $x
done < file
Output :
99
1 99
1 92
1 7 92
1 2 92
1 5 2 8 84
With GNU awk for FPAT:
$ awk -v FPAT='sz:[0-9]+' '{for (i=1;i<=NF;i++) printf "%s%s", substr($i,4), (i<NF?OFS:ORS)}' file
99
1 99
1 92
1 7 92
1 2 92
1 5 2 8 84
With any awk:
$ awk '{out=""; while (match($0,/sz:[0-9]+/)) { out = (out=="" ? "" : out OFS) substr($0,RSTART+3,RLENGTH-3); $0=substr($0,RSTART+RLENGTH) } $0=out } NF' file
99
1 99
1 92
1 7 92
1 2 92
1 5 2 8 84

H264 encoding and decoding using Videotoolbox

I was testing the encoding and decoding using videotoolbox, to convert the captured frames to H264 and using that data to display it in AVSampleBufferdisplayLayer.
error here while decompress CMVideoFormatDescriptionCreateFromH264ParameterSets with error code -12712
I follow this code from mobisoftinfotech.com
status = CMVideoFormatDescriptionCreateFromH264ParameterSets(
kCFAlloc‌​‌ atorDefault, 2,
(const uint8_t const)parameterSetPointers,
parameterSetSizes, 4, &_formatDesc);
videoCompressionTest; can anyone figure out the problem?
I am not sure if you did figure out the problem yet. However, I found 2 places in your code that leading to the error. After fixed them and run locally your test app, it seems to be working fine. (Tested with Xcode 9.4.1, MacOS 10.13)
The first one is in -(void)CompressAndConvertToData:(CMSampleBufferRef)sampleBuffer method where the while loop should be like this
while (bufferOffset < blockBufferLength - AVCCHeaderLength) {
// Read the NAL unit length
uint32_t NALUnitLength = 0;
memcpy(&NALUnitLength, bufferDataPointer + bufferOffset, AVCCHeaderLength);
// Convert the length value from Big-endian to Little-endian
NALUnitLength = CFSwapInt32BigToHost(NALUnitLength);
// Write start code to the elementary stream
[elementaryStream appendBytes:startCode length:startCodeLength];
// Write the NAL unit without the AVCC length header to the elementary stream
[elementaryStream appendBytes:bufferDataPointer + bufferOffset + AVCCHeaderLength
length:NALUnitLength];
// Move to the next NAL unit in the block buffer
bufferOffset += AVCCHeaderLength + NALUnitLength;
}
uint8_t *bytes = (uint8_t*)[elementaryStream bytes];
int size = (int)[elementaryStream length];
[self receivedRawVideoFrame:bytes withSize:size];
The second place is the decompression code where you process for NALU type 8, the block of code in if(nalu_type == 8) statement. This is a tricky one.
To fix it, update
for (int i = _spsSize + 12; i < _spsSize + 50; i++)
to
for (int i = _spsSize + 12; i < _spsSize + 12 + 50; i++)
And you are freely to remove this hack
//was crashing here
if(_ppsSize == 0)
_ppsSize = 4;
Why? Lets print out the frame packet format.
po frame
▿ 4282 elements
- 0 : 0
- 1 : 0
- 2 : 0
- 3 : 1
- 4 : 39
- 5 : 100
- 6 : 0
- 7 : 30
- 8 : 172
- 9 : 86
- 10 : 193
- 11 : 112
- 12 : 247
- 13 : 151
- 14 : 64
- 15 : 0
- 16 : 0
- 17 : 0
- 18 : 1
- 19 : 40
- 20 : 238
- 21 : 60
- 22 : 176
- 23 : 0
- 24 : 0
- 25 : 0
- 26 : 1
- 27 : 6
- 28 : 5
- 29 : 35
- 30 : 71
- 31 : 86
- 32 : 74
- 33 : 220
- 34 : 92
- 35 : 76
- 36 : 67
- 37 : 63
- 38 : 148
- 39 : 239
- 40 : 197
- 41 : 17
- 42 : 60
- 43 : 209
- 44 : 67
- 45 : 168
- 46 : 0
- 47 : 0
- 48 : 3
- 49 : 0
- 50 : 0
- 51 : 3
- 52 : 0
- 53 : 2
- 54 : 143
- 55 : 92
- 56 : 40
- 57 : 1
- 58 : 221
- 59 : 204
- 60 : 204
- 61 : 221
- 62 : 2
- 63 : 0
- 64 : 76
- 65 : 75
- 66 : 64
- 67 : 128
- 68 : 0
- 69 : 0
- 70 : 0
- 71 : 1
- 72 : 37
- 73 : 184
- 74 : 32
- 75 : 1
- 76 : 223
- 77 : 205
- 78 : 248
- 79 : 30
- 80 : 231
… more
The first NALU start code if (nalu_type == 7) is 0, 0, 0, 1 from index of 15 to 18. The next 0, 0, 0, 1 (from 23 to 26) is type 6, type 8 NALU start code is from 68 to 71. That why I modify the for loop a bit to scan from start index (_spsSize + 12) with a range of 50.
I haven't fully tested your code to make sure encode and decode work properly as expected. However, I hope this finding would help you.
By the way, if there is any misunderstanding, I would love to learn from your comments.

What to make out of the output of QSG_RENDERER_DEBUG=render

When I set the environment variable QSG_RENDERER_DEBUG=render , I get the following output:
Renderer::render() QSGAbstractRenderer(0x383865f8) "rebuild: none"
Rendering:
-> Opaque: 38 nodes in 2 batches...
-> Alpha: 34 nodes in 13 batches...
- 0x3836a830 [retained] [noclip] [opaque] [ merged] Nodes: 14 Vertices: 88 Indices: 124 root: 0x0
- 0x3836a7f0 [ upload] [noclip] [opaque] [ merged] Nodes: 24 Vertices: 96 Indices: 144 root: 0x0
- 0x3836a8f0 [retained] [noclip] [ alpha] [unmerged] Nodes: 1 Vertices: 48 Indices: 74 root: 0x0
- 0x3836a870 [retained] [noclip] [ alpha] [unmerged] Nodes: 3 Vertices: 52 Indices: 78 root: 0x0
- 0x3836a8b0 [retained] [noclip] [ alpha] [unmerged] Nodes: 6 Vertices: 400 Indices: 720 root: 0x0
- 0x3836a530 [retained] [noclip] [ alpha] [unmerged] Nodes: 4 Vertices: 56 Indices: 84 root: 0x0
- 0x3836a570 [retained] [noclip] [ alpha] [ merged] Nodes: 1 Vertices: 4 Indices: 6 root: 0x0 opacity: 1
- 0x3836a5b0 [retained] [noclip] [ alpha] [unmerged] Nodes: 7 Vertices: 720 Indices: 1302 root: 0x0
- 0x3836a630 [retained] [noclip] [ alpha] [unmerged] Nodes: 3 Vertices: 28 Indices: 42 root: 0x0
- 0x3836a5f0 [retained] [noclip] [ alpha] [ merged] Nodes: 3 Vertices: 12 Indices: 18 root: 0x0 opacity: 1
- 0x3836a6f0 [retained] [noclip] [ alpha] [ merged] Nodes: 1 Vertices: 4 Indices: 6 root: 0x0 opacity: 1
- 0x3836a670 [retained] [noclip] [ alpha] [ merged] Nodes: 1 Vertices: 4 Indices: 6 root: 0x0 opacity: 1
- 0x3836a6b0 [retained] [noclip] [ alpha] [ merged] Nodes: 1 Vertices: 4 Indices: 6 root: 0x0 opacity: 1
- 0x3836a330 [retained] [noclip] [ alpha] [unmerged] Nodes: 2 Vertices: 24 Indices: 36 root: 0x0
- 0x3836a370 [retained] [noclip] [ alpha] [ merged] Nodes: 1 Vertices: 4 Indices: 6 root: 0x0 opacity: 1
-> times: build: 0, prepare(opaque/alpha): 0/0, sorting: 0, upload(opaque/alpha): 0/0, render: 1
which I deem bad to catastrophal, if I want to run it on a low-performance-device such as an old Raspberry PI.
However I fail to find the answers to: what to make out of this? How can I improve it, to reduce the amount of batches, especially the Alpha-batches?
I have only few objects (2) with opacity != 0 and at the time of this output all of them have been set to invisible.
I have two SVGs (8 nodes each) which might show up there.
I do not use any colors that would not fit in the format '#------ff' (unless the svg-colors of which I use a few might have the alpha-channel set otherwise) and do not use the color 'transparent'
I do have an object that consists out of two "concentric" Rectangles of opaque color, that are marked unmerged when visualizing the overdraw, which I have no clue, why. At least it could be merged with it's not-intersecting siblings, I deem. Why wouldn't they?
I think it migh help if I might identify the objects, but when I ouput the adresses of any object that is visible, (Component.onCompleted: console.log(this)) I do not get any of the listed adresses.
How might I achieve a mapping between the render-objects and my QML-Objects?
And - What doe all those list entries mean at all?
EDIT: It seems, I used some PNG's with alpha enabled. Replacing them with JPG reduced the message to this. But those were only 5 of the nodes/batches. 34 to go...
-> Opaque: 43 nodes in 6 batches...
-> Alpha: 29 nodes in 8 batches...
Greetings & Thanks,
-m-
From doc.qt.io: "[one batch] is created for each unique set of material state and geometry type". Reduce the number of differences and the default renderer puts compatible OpenGL primitives into the same batch.
For example:
Rectangle elements with different width/height/color/gradient/rotation properties are batched together but with different opacity/radius/antialiasing properties are not.
Rectangle elements with the color property set to an opaque value using hexadecimal triplets (Ex.: specifying 'forestgreen' with '#228B22' or '#282') are batched together. But specifying translucent colors (a hex quad that does not start with '#FF', ex.: '#44228B22' -- 25% opaque 'forestgreen') forces a separate batch even if the translucent color values are identical (verified on Qt 5.9.1). Having translucent border colors also forces separate batches even if the translucent color values are identical.
Text elements with different font letterspacing/underline
properties are grouped together, but different font bold/italic/pixelSize/hintingPreference properties are separated.
You can determine which objects are which by changing the visible property and seeing what appears or disappears.

Is it impossible to select a nondeterministic value of an array element in Promela?

Following is the Promela code that I am writing.
491 byte api1[5];
492 byte api2[5];
493 byte api3[5];
494 byte reftask1[5]
495 byte reftask2[5];
496 byte reftask3[5];
497 byte rid1[5];
498 byte rid2[5];
499 byte rid3[5];
500
501
502 proctype init_call(){
503 byte i1 = 0;
504 do
505 :: (i1 == 5) -> break
506 :: else ->
507 select ( api1[i1]: 2 .. 9);
508 select ( api2[i1] : 2 .. 9);
509 select ( api3[i1] : 2 .. 9);
510 select ( reftask1[i1] : 1 .. 3);
511 select( reftask2[i1] : 1 .. 3);
512 select ( reftask3[i1] : 1 .. 3);
513 select ( rid[i1] : 0 .. 1);
514 select ( rid[i1] : 0 .. 1);
515 select ( rid[i1] : 0 .. 1);
516 i1++;
517 od
518 }
But if I try to simulate the code, I get the error message as following,
saw: '[', expected ':' spin: osek_sp2.pml:507, Error: expecting select
( name : constant .. constant ) near 'select'
However, according to the syntax definition, I can't find any problem.
SYNTAX
select '(' varref ':' expr '..' expr ')'
varref : name [ '[' any_expr ']' ] [ '.' varref ]
What is the reason of this error message?
Patrick is right. I'd say that this is a bug. If you look into spinlex.c, you'll see that when it scans for name before : only alphanumeric characters are allowed:
scan_to(':', isalnum, name)
Anyway, select is just a shorthand for a sequence of assignments. So a work-around might be to write the assignments yourself, e.g.
api1[i1] = 2;
do
:: (api1[i1] < 9) -> api1[i1]++
:: break
od