Home > Enterprise >  Why we using inf and power in the matlab code
Why we using inf and power in the matlab code

Time:05-26

I am using the following code in the Matlab, confusion is why we using inf here and why we need power(2,14:28) and later we using size with in size.

gpu = gpuDevice();
fprintf('Using a %s GPU.\n', gpu.Name)
sizeOfDouble = 8; % Each double-precision number needs 8 bytes of storage
sizes = power(2, 14:28);
sendTimes = inf(size(sizes));
gatherTimes = inf(size(sizes));

I am new to Matlab here, trying to understand this code. here is the source of code

CodePudding user response:

power(2,14:28) is just the verbose way of computing element-wise power 2.^(14:28), which is shorthand for creating the vector of values [2^14,2^15,...,2^28].

The inf(size(sizes)) is just code for pre-allocating a double array the same size as what the power(etc.) call produced with all values initialized to inf. Presumably downstream in the code these inf values will be replaced with some other values.

EDIT for downstream code:

If you look at how the variable sizes is used downstream in the code:

numElements = sizes(ii)/sizeOfDouble;
hostData = randi([0 9], numElements, 1);
gpuData = randi([0 9], numElements, 1, 'gpuArray');

You can see that it is simply used for the number of elements of other variables. That is, the test will be run with various sized variables to see the timing implications, and the sizes variable is used to generate the number of elements for variables used in a particular iteration of the test.

  • Related