Skip to content

Commit fd18688

Browse files
committed
Merge commit '9cadd01917a421717bec2c763bdb177dfc91e706'
* Fixed error when initial size arguments fail the validation check (@marcsous) * Now correctly trim trailing dimensions (@marcsous) * Added (still unused) code for quick allocation on PCs (@marcsous)
2 parents b9ef47b + 9cadd01 commit fd18688

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

MappedTensor.m

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@
178178
methods
179179
%% MappedTensor - CONSTRUCTOR
180180
function [mtVar] = MappedTensor(varargin)
181+
182+
% - Get a handle to the appropriate shim function (should be done
183+
% before any errors are thrown)
184+
[mtVar.hShimFunc, ...
185+
mtVar.hRepSumFunc, ...
186+
mtVar.hChunkLengthFunc] = GetMexFunctionHandles;
187+
181188
% - Filter arguments for properties
182189
vbKeepArg = true(numel(varargin), 1);
183190
nArg = 2;
@@ -211,7 +218,7 @@
211218
otherwise
212219
% - No other properties are supported
213220
error('MappedTensor:InvalidProperty', ...
214-
'*** MappedTensor: ''%s'' is not a valid property. Use the ''Class'' keyword to specify the tensor class.', varargin{nArg});
221+
'*** MappedTensor: ''%s'' is not a valid property.', varargin{nArg});
215222
end
216223
end
217224

@@ -251,7 +258,6 @@
251258
if (isscalar(vnTensorSize))
252259
vnTensorSize = vnTensorSize * [1 1];
253260
end
254-
255261

256262
% - Validate tensor size argument
257263
try
@@ -260,17 +266,12 @@
260266
error('MappedTensor:Arguments', ...
261267
'*** MappedTensor: Error: ''vnTensorSize'' must be a positive integer vector.');
262268
end
263-
269+
264270
% - Make enough space for a temporary tensor
265271
if (mtVar.bTemporary)
266272
mtVar.strRealFilename = create_temp_file(prod(vnTensorSize) * mtVar.nClassSize + mtVar.nHeaderBytes);
267273
end
268274

269-
% - Get a handle to the appropriate shim function
270-
[mtVar.hShimFunc, ...
271-
mtVar.hRepSumFunc, ...
272-
mtVar.hChunkLengthFunc] = GetMexFunctionHandles;
273-
274275
% - Open the file
275276
if (isempty(mtVar.strMachineFormat))
276277
[mtVar.hRealContent, mtVar.strMachineFormat] = mtVar.hShimFunc('open', mtVar.strRealFilename);
@@ -291,14 +292,19 @@
291292
'*** MappedTensor: Error: only ''ieee-be'' and ''ieee-le'' machine formats are supported.');
292293
end
293294

295+
% - Record the original tensor size, remove trailing unitary dimensions
296+
if (vnTensorSize(end) == 1) && (numel(vnTensorSize) > 2)
297+
nLastNonUnitary = max(2, find(vnTensorSize ~= 1, 1, 'last'));
298+
vnTensorSize = vnTensorSize(1:nLastNonUnitary);
299+
end
300+
301+
mtVar.vnOriginalSize = vnTensorSize;
302+
294303
% - Initialise dimension order
295304
mtVar.vnDimensionOrder = 1:numel(vnTensorSize);
296305

297306
% - Record number of total elements
298307
mtVar.nNumElements = prod(vnTensorSize);
299-
300-
% - Record the original tensor size
301-
mtVar.vnOriginalSize = vnTensorSize;
302308
end
303309

304310
% delete - DESTRUCTOR
@@ -487,7 +493,7 @@ function delete(mtVar)
487493
% - Permute input data
488494
tfData = ipermute(tfData, mtVar.vnDimensionOrder);
489495

490-
if (~isreal(tfData))
496+
if (~isreal(tfData)) || (~isreal(mtVar))
491497
% - Assign to both real and complex parts
492498
mt_write_data(mtVar.hShimFunc, mtVar.hRealContent, subs, mtVar.vnOriginalSize, mtVar.strClass, mtVar.nHeaderBytes, real(tfData) ./ mtVar.fRealFactor, mtVar.bBigEndian, mtVar.hRepSumFunc, mtVar.hChunkLengthFunc);
493499
mt_write_data(mtVar.hShimFunc, mtVar.hCmplxContent, subs, mtVar.vnOriginalSize, mtVar.strClass, mtVar.nHeaderBytes, imag(tfData) ./ mtVar.fComplexFactor, mtVar.bBigEndian, mtVar.hRepSumFunc, mtVar.hChunkLengthFunc);
@@ -1376,7 +1382,11 @@ function make_complex(mtVar)
13761382

13771383
% - Fast allocation on some platforms
13781384
bFailed = true;
1379-
% [bFailed, ~] = system(sprintf('fallocate -l %i %s', nNumEntries, strFilename));
1385+
% if ispc()
1386+
% [bFailed, ~] = system(sprintf('fsutil file createnew %s %i', file, nbytes));
1387+
% else
1388+
% [bFailed, ~] = system(sprintf('fallocate -l %i %s', nNumEntries, strFilename));
1389+
% end
13801390

13811391
% - Slow fallback -- use Matlab to write data directly
13821392
if (bFailed)

0 commit comments

Comments
 (0)