/usr/lib/petscdir/3.7.7/x86_64-linux-gnu-real-debug/share/petsc/matlab/PetscBinaryRead.m is in libpetsc3.7.7-dbg 3.7.7+dfsg1-2build5.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | function [varargout] = PetscBinaryRead(inarg,varargin)
%
% [varargout] = PetscBinaryRead(inarg,['complex',false or true],['indices','int32' or 'int64'],['cell',cnt],['precision','float64' or 'float32'])
%
% Reads in PETSc binary file matrices or vectors
% emits as Matlab sparse matrice or vectors.
%
% [] indices optional arguments
% There are no [] in the arguments
%
% Examples: A = PetscBinaryRead('myfile'); read from file
% b = PetscBinaryRead(1024); read from socket
% c = PetscBinaryRead(); read from default socket PETSc uses
%
% Argument may be file name (string), socket number (integer)
% or any Matlab class that provides the read() and close() methods
% [We provide PetscOpenFile() and PetscOpenSocket() for binary files and sockets]
% For example: fd = PetscOpenFile('filename');
% a = PetscBinaryRead(fd);
% b = PetscBinaryRead(fd);
%
% 'complex', true indicates the numbers in the file are complex, that is PETSc was built with --with-scalar-type=complex
% 'indices','int64' indicates the PETSc program was built with --with-64-bit-indices
% 'cell',cnt means return a Matlab cell array containing the first cnt objects in the file, use 10,000 to read in all objects
% 'precision','float32' indicates the PETSc program was built with --with-precision=single
%
% Examples: A = PetscBinaryRead('myfile','cell',10000); read all objects in file
% A = PetscBinaryRead(1024,'cell',2); read two objects from socket
%
if nargin == 0
fd = PetscOpenSocket();
else if ischar(inarg)
fd = PetscOpenFile(inarg);
else if isnumeric(inarg)
fd = PetscOpenSocket(inarg);
else % assume it is a PetscOpenFile or PetscOpenSocket object and handles read()
fd = inarg;
end
end
end
indices = 'int32';
precision = 'float64';
arecell = 0;
arecomplex = false;
tnargin = nargin;
for l=1:nargin-2
if ischar(varargin{l}) && strcmpi(varargin{l},'indices')
tnargin = min(l,tnargin-1);
indices = varargin{l+1};
end
if ischar(varargin{l}) && strcmpi(varargin{l},'precision')
tnargin = min(l,tnargin-1);
precision = varargin{l+1};
end
if ischar(varargin{l}) && strcmpi(varargin{l},'cell')
tnargin = min(l,tnargin-1);
arecell = varargin{l+1};
end
if ischar(varargin{l}) && strcmpi(varargin{l},'complex')
tnargin = min(l,tnargin-1);
arecomplex = varargin{l+1};
end
end
if strcmp(precision,'float128')
precision = 'float64';
system(['./convert -f ' inarg]);
fd = PetscOpenFile([inarg '_double']);
end
if arecell
narg = arecell;
rsult = cell(1);
else
narg = nargout;
end
for l=1:narg
header = double(read(fd,1,indices));
if isempty(header)
if arecell
varargout(1) = {result};
return
else
disp('File/Socket does not have that many items')
end
return
end
if header == 1211216 % Petsc Mat Object
header = double(read(fd,3,indices));
m = header(1);
n = header(2);
nz = header(3);
if (nz == -1)
if arecomplex
s = read(fd,2*m*n,precision);
iReal = 1:2:n*m*2-1;
iImag = iReal +1 ;
A = complex(reshape(s(iReal),n,m)',reshape(s(iImag),n,m)') ;
else
s = read(fd,m*n,precision);
A = reshape(s,n,m)';
end
else
nnz = double(read(fd,m,indices)); %nonzeros per row
sum_nz = sum(nnz);
if(sum_nz ~=nz)
str = sprintf('No-Nonzeros sum-rowlengths do not match %d %d',nz,sum_nz);
error(str);
end
j = double(read(fd,nz,indices)) + 1;
if arecomplex
s = read(fd,2*nz,precision);
else
s = read(fd,nz,precision);
end
i = ones(nz,1);
cnt = 1;
for k=1:m
next = cnt+nnz(k)-1;
i(cnt:next,1) = (double(k))*ones(nnz(k),1);
cnt = next+1;
end
if arecomplex
A = sparse(i,j,complex(s(1:2:2*nz),s(2:2:2*nz)),m,n,nz);
else
A = sparse(i,j,s,m,n,nz);
end
end
if arecell
result{l} = A;
else
varargout(l) = {A};
end
elseif header == 1211214 % Petsc Vec Object
m = double(read(fd,1,indices));
if arecomplex
v = read(fd,2*m,precision);
v = complex(v(1:2:2*m),v(2:2:2*m));
else
v = read(fd,m,precision);
end
if arecell
result{l} = v;
else
varargout(l) = {v};
end
elseif header == 1211218 % Petsc IS Object
m = double(read(fd,1,indices));
v = read(fd,m,'int') + 1; % Indexing in Matlab starts at 1, 0 in PETSc
if arecell
result{l} = v;
else
varargout(l) = {v};
end
elseif header == 1211219 % Petsc Bag Object
b = PetscBagRead(fd);
if arecell
result{l} = b;
else
varargout(l) = {b};
end
elseif header == 1211221 % Petsc DM Object
m = double(read(fd,7,indices));
me = double(read(fd,5,indices));
b = [' dm ' int2str(m(3)) ' by ' int2str(m(4)) ' by ' int2str(m(5))];
if arecell
result{l} = b;
else
varargout(l) = {b};
end
else
disp(['Found unrecogonized header ' int2str(header) ' in file. If your file contains complex numbers'])
disp(' then call PetscBinaryRead() with "complex",true as two additional arguments')
return
end
end
if arecell
varargout(1) = {result};
end
% close the reader if we opened it
if nargin > 0
if (ischar(inarg) || isinteger(inarg)) close(fd); end;
end
|