# Large scale Matrix-vector multiplication fixed-point

3 views (last 30 days)

Show older comments

Hi,

I have a strange behavior on a large matrix-vector product in fixed-point precision. The matrix is block banded, so it has few non-zero elements in each row. That is what happen

K>> size(A)

ans =

250 550

K>> A.numerictype

ans =

DataTypeMode: Fixed-point: binary point scaling

Signedness: Signed

WordLength: 32

FractionLength: 22

K>> A.fimath

ans =

RoundMode: floor

OverflowMode: saturate

ProductMode: KeepMSB

ProductWordLength: 32

SumMode: KeepMSB

SumWordLength: 32

CastBeforeSum: true

K>> size(x)

ans =

550 1

K>> x.numerictype

ans =

DataTypeMode: Fixed-point: binary point scaling

Signedness: Signed

WordLength: 32

FractionLength: 22

K>> x.fimath

ans =

RoundMode: floor

OverflowMode: saturate

ProductMode: KeepMSB

ProductWordLength: 32

SumMode: KeepMSB

SumWordLength: 32

CastBeforeSum: true

Lets consider just the first row of A in which only the first 11 entries are non-zero while vector x has entries all equal 1(x=ones(250,1)). If I perform the product:

K>> A(1,1:11)*x(1:11)

ans =

0.5742

DataTypeMode: Fixed-point: binary point scaling

Signedness: Signed

WordLength: 32

FractionLength: 8

K>> A(1,:)*x

ans =

-1

DataTypeMode: Fixed-point: binary point scaling

Signedness: Signed

WordLength: 32

FractionLength: 2

K>> A(1,12:550)*x(12:550)

ans =

0

DataTypeMode: Fixed-point: binary point scaling

Signedness: Signed

WordLength: 32

FractionLength: 2

Can someone explain this strange behavior? Thanks.

##### 0 Comments

### Answers (2)

DSP Masters
on 30 Apr 2012

Hong Ye
on 1 Aug 2012

Hi Giulio, The behavior you observed is due to the fimath setting you picked for A and x. Here is an explanation for the multiply and accumulate operation: 1) the full precision multiply of two s32,22 number result in s64,44, ProductMode keepMSB and productWordLength 32 indicate that the most significant (or from left side) 32 bits of the the 64 bits result will be kept, so you will get a product of s32,12 (with 20 bits Integer WL) for example,

>> A(1, 1)*x(1)

ans =

0.8147

DataTypeMode: Fixed-point: binary point scaling

Signedness: Signed

WordLength: 32

FractionLength: 12

2) SumMode is also keepMSB with SumWordLength as 32, so adding two s32,12 numbers will lead to result with s32,11 data type. the integer WordLength grows from 20 to 21 (so the fractionLength decrease by 1 bit to 11 given the fixed WordLength) to avoid overflow. for example

>> A(1, 1:2)*x(1:2)

ans =

1.7773

DataTypeMode: Fixed-point: binary point scaling

Signedness: Signed

WordLength: 32

FractionLength: 11

>> A(1, 1:11)*x(1:11) % Integer WL grow from 20 to 20+ceil(log2(11))(=24) bits

ans =

6.4102

DataTypeMode: Fixed-point: binary point scaling

Signedness: Signed

WordLength: 32

FractionLength: 8

For experimenting purpose, you can first remove fimath of variable x by x.fimathislocal = false, then vary A's fimath settings to see different numerical result and data type being returned. e.g., set A.ProductMode = 'FullPrecision' and A.SumMode = 'FullPrecision', and see what you will get for A(1,1)*x(1) and A(1,1:2)*x(1:2)

##### 0 Comments

### See Also

### Products

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!