Direct Mapping Cache - caching

Consider the cache system with the following properties:
Cache (direct mapped cache):
- Cache size 128 bytes, block size 16 bytes (24 bytes)
- Tag/Valid bits for cache blocks are as follows:
Block index - 0 1 2 3 4 5 6 7
Tag - 0 6 7 0 5 3 1 3
Valid - 1 0 0 1 0 0 0 1
Find Tag Block index, Block offset, Cache hit/miss for memory addresses - 0x7f6, 0x133.
I am not sure how to solve.

Since cache size is 128 bytes, cache has 128/16 = 8 blocks and hence block offset = 3.
Since block size is 16 bytes, block offset is 4.
Address bits are 12 for 0x7f6 = 0111 1111 0110:
Offset = (0110 >> 1) = 3
Index = 111 = 7
Tag = 01111 = f

Related

How to calculate byte offset in n-way associative cache

From what I know this is the formula of address mapping to cache
| tag | index | offset |
However I'm wondering that how the offset is calculated
For example in the following exercise:
Give a CPU this 32 KB cache module as the following figure.
What is the bit length of tag? (how many tag bits ?)
Total address bit = 32 bits = Tag + Index + Block Offset + byte Offset
1024 sets = 2^10 => set index = 10 bits
Block size = 8 byte = 2^3 => byte offset = 3 bits
4 block/set = 2^2 => block offset = 2 bits
=> Tag = 32 – (10 + 3 + 2) = 17 bits
The offset contains Block Offset + byte Offset but in the link (in the an example catalog) the offset in the answer contain only byte offset not block offset
So do offset contains Block Offset + byte Offset or byte offset only

Making all the preferred column elements to zero

REFERENCE STRUCTURE = 00000 A,B,C = 120.000 120.000 42.560
ALPHA,BETA,GAMMA = 90.000 90.000 90.000 SPGR = P1
31984 1 new.pdb
x y z
1 C 8.17500 93.80900 21.90700 8 4 2 0 0 0 0 0 -0.036 1
2 C 9.34800 94.14800 22.73500 1 16 9 0 0 0 0 0 0.038 1
3 C 8.05800 95.47500 24.28800 6 9 15 0 0 0 0 0 0.038 1
4 C 6.95800 94.40500 22.32000 12 1 6 0 0 0 0 0 0.060 1
5 O 7.20600 96.40600 26.25200 15 0 0 0 0 0 0 0 -0.270 1
6 C 6.88800 95.13100 23.50200 4 10 3 0 0 0 0 0 -0.036 1
7 O 4.60000 94.52600 21.81800 1645872 0 0 0 0 0 0 0 -0.245 1
8 H 8.26600 93.17800 21.03500 1 0 0 0 0 0 0 0 0.063 1
9 C 9.25800 94.94800 23.85500 2 3 11 0 0 0 0 0 -0.037 1
10 H 5.98600 95.70100 23.66700 6 0 0 0 0 0 0 0 0.063 1
11 H 10.19600 95.24800 24.29800 9 0 0 0 0 0 0 0 0.063 1
12 C 5.70900 94.23600 21.42300 13454 7 4 0 0 0 0 0 0.337 1
13 O 5.87600 93.60100 20.21100 14 12 0 0 0 0 0 0 -0.477 1
14 H 5.04400 93.52600 19.73800 13 0 0 0 0 0 0 0 0.295 1
I have this file structure and I need to make all the columns after the x, y and z columns to be zero and the last column to be deleted. for example I need to have the following as output (sample).
1 C 8.17500 93.80900 21.90700 0 0 0 0 0 0 0 0 0
2 C 9.34800 94.14800 22.73500 0 0 0 0 0 0 0 0 0
If the pattern is predictable, a find/replace would work
%s/\v(\d+ \w+\s+([0-9\.]+\s+){3}).*/\10 0 0 0 0 0 0 0 0
Breakdown
%s/ - substitute every line
\v - very magic
(\d+ \w+\s+([0-9\.]+\s+){3}) - capture everything between ()
searches for '1 C 8.17500 93.80900 21.90700 '
.* - remaining character after our captured group
/\1 - replace, insert the captured group
0 0 0 0 0 0 0 0 0 - add required zero's
Regexbuddy comment
// (\d+ \w+\s+([0-9\.]+\s+){3}).*
//
// Options: Case insensitive; Exact spacing; Dot matches line breaks; ^$ match at line breaks; Default line breaks; Numbered capture; Allow duplicate names; Greedy quantifiers; Allow zero-length matches
//
// Match the regex below and capture its match into backreference number 1 «(\d+ \w+\s+([0-9\.]+\s+){3})»
// Match a single character that is a “digit” (ASCII 0–9 only) «\d+»
// Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Match the character “ ” literally « »
// Match a single character that is a “word character” (ASCII letter, digit, or underscore only) «\w+»
// Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Match a single character that is a “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed) «\s+»
// Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Match the regex below and capture its match into backreference number 2 «([0-9\.]+\s+){3}»
// Exactly 3 times «{3}»
// Match a single character present in the list below «[0-9\.]+»
// Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// A character in the range between “0” and “9” «0-9»
// The literal character “.” «\.»
// Match a single character that is a “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed) «\s+»
// Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Match any single character «.*»
// Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»

Count the frequency of matrix values including 0

I have a vector
A = [ 1 1 1 2 2 3 6 8 9 9 ]
I would like to write a loop that counts the frequencies of values in my vector within a range I choose, this would include values that have 0 frequencies
For example, if I chose the range of 1:9 my results would be
3 2 1 0 0 1 0 1 2
If I picked 1:11 the result would be
3 2 1 0 0 1 0 1 2 0 0
Is this possible? Also ideally I would have to do this for giant matrices and vectors, so the fasted way to calculate this would be appreciated.
Here's an alternative suggestion to histcounts, which appears to be ~8x faster on Matlab 2015b:
A = [ 1 1 1 2 2 3 6 8 9 9 ];
maxRange = 11;
N = accumarray(A(:), 1, [maxRange,1])';
N =
3 2 1 0 0 1 0 1 2 0 0
Comparing the speed:
K>> tic; for i = 1:100000, N1 = accumarray(A(:), 1, [maxRange,1])'; end; toc;
Elapsed time is 0.537597 seconds.
K>> tic; for i = 1:100000, N2 = histcounts(A,1:maxRange+1); end; toc;
Elapsed time is 4.333394 seconds.
K>> isequal(N1, N2)
ans =
1
As per the loop request, here's a looped version, which should not be too slow since the latest engine overhaul:
A = [ 1 1 1 2 2 3 6 8 9 9 ];
maxRange = 11; %// your range
output = zeros(1,maxRange); %// initialise output
for ii = 1:maxRange
tmp = A==ii; %// temporary storage
output(ii) = sum(tmp(:)); %// find the number of occurences
end
which would result in
output =
3 2 1 0 0 1 0 1 2 0 0
Faster and not-looping would be #beaker's suggestion to use histcounts:
[N,edges] = histcounts(A,1:maxRange+1);
N =
3 2 1 0 0 1 0 1 2 0
where the +1 makes sure the last entry is included as well.
Assuming the input A to be a sorted array and the range starts from 1 and goes until some value greater than or equal to the largest element in A, here's an approach using diff and find -
%// Inputs
A = [2 4 4 4 8 9 11 11 11 12]; %// Modified for variety
maxN = 13;
idx = [0 find(diff(A)>0) numel(A)]+1;
out = zeros(1,maxN); %// OR for better performance : out(maxN) = 0;
out(A(idx(1:end-1))) = diff(idx);
Output -
out =
0 1 0 3 0 0 0 1 1 0 3 1 0
This can be done very easily with bsxfun.
Let the data be
A = [ 1 1 1 2 2 3 6 8 9 9 ]; %// data
B = 1:9; %// possible values
Then
result = sum(bsxfun(#eq, A(:), B(:).'), 1);
gives
result =
3 2 1 0 0 1 0 1 2

Assemble a vector into 2D matrix [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Which is the most effective way of assembling vector v into matrix A, as shown below? (Without using for loops).
Input:
v = [1;2;3;4;5;6;7;8;9]
Desired output matrix:
A =
1 0 0
2 0 0
3 0 0
0 4 0
0 5 0
0 6 0
0 0 7
0 0 8
0 0 9
One approach using zero-padding and reshaping -
m = 3; %// To select group of "m" elements from v for each col in o/p
N = numel(v); %// Number of elements in input vector
%// Reshape, pad with zeros
vpad = [reshape(v,m,[]) ; zeros(N,N/m)]
%// Clip off at "N*N/m" elements and reshape into 2D array with N rows
A = reshape(vpad(1:N*N/m),N,[])
Sample run -
v =
31 19 46 82 57 10 36 5 46 39 90 74
m =
4
A =
31 0 0
19 0 0
46 0 0
82 0 0
0 57 0
0 10 0
0 36 0
0 5 0
0 0 46
0 0 39
0 0 90
0 0 74
Here's one way: create a matrix of zeros, and then define a linear index with the positions where the vector values will be written:
v = [1;2;3;4;5;6;7;8;9]; %// data vector
n = 3; %// group size
N = numel(v);
A = zeros(N, N/n); %// define A filled with zeros
A(bsxfun(#plus, reshape((1:N).',n,[]), (ceil(1:N/n)-1)*N)) = v; %'// fill in v with
%// linear indexing
Result in this example:
A =
1 0 0
2 0 0
3 0 0
0 4 0
0 5 0
0 6 0
0 0 7
0 0 8
0 0 9

how to compress the three bytes data into two bytes in java

I have to compress the 3 bytes of data in to two bytes.
the 3 bytes data includes day is in one byte,hour is in another byte, finally minutes is in one more byte.so totally i have 3 bytes data.how could i flip this data into two bytes only.
Thanks,
Minutes are ranging from 0 to 59 so the number could be stored on 6
bits (6 bits => 0 to 63)
Hours are ranging from 0 to 23 the number
could be stored on 5 bits (5 bits => 0 to 31)
Days... Err... Ranging from 0 to 6 ? Let's asusme that. 2 bytes = 16 bits, minus the 11
other bits, so you have 5 bits left which is more than enough.
To pack your 3 bytes of data into two, you have to dispatch the bits :
I will set bits 0 to 5 for the minutes, bits 6 to 10 for the hours, and the bits left for the day number.
So the formula to pack the bits is :
packed=minutes+hours*64+days*2048
To get back your uncompressed data :
minutes=packed & 63
hours=(packed & 1984) / 64
days=(packed & 63488) / 2048
I assume you need day from 1-31, hour from 0-23 and minute from 0-59, you thus need 5 bits for the day, 5 bits for the hours and 6 bits for the minutes. This makes exactly 16 bits. You should put 5 bits (day) and the first 3 bits for hours into your first byte and the remaining 2 bits for hours and the 6 bits for minutes into the second byte:
int day = 23;
int hour = 15;
int minute = 34;
byte fromint_dh = (day << 3) | (hour >> 2);
byte fromint_hm = ((hour & 0x03) << 6) | (minute); // take the last two bits from hour and put them at the beginning
....
int d = fromint_dh >> 3;
int h = ((fromint_dh & 0x07) << 2) | ((fromint_hm & 0xc0) >> 6); // take the last 3 bits of the fst byte and the fst 2 bits of the snd byte
int m = fromint_hm & 0x3F // only take the last 6 bits
Hope this helps. It's easy to get the bits wrong ...

Resources