fopen error - works for a while but then gives an error - matlab

I have a script that is running a series of for loops, and within these for loops a file is created that is then run using an external program using the script command. In summary it looks like this:
for i=1:n1
for j=1:n2
for k=1:n3
fid=fopen('file.txt','w');
fprintf(fid,'Some commands to pass to external program depending on i j k');
fclose(fid);
system('program file.txt');
end
end
end
The script has in total about 500k cases (n1xn2xn3), and runs fine for a small scenario (about 100 runs), but for the entire script it runs for a while and then returns an error for no apparent reason, giving this error:
fopen invalid file identifier object
There is no obvious reason for this, and Im wondering if anyone could point out what is wrong?

Just a guess: an instance of your external program is reading file.txt and at the same time the next iteration of your nested loop wants to open file.txt for writing. The more instances of your external program are running at the same time, the slower your machine, the more likely becomes this scenario. (called a 'race condition')
Possible solution for this: use a separate text file per case with a unique file name
You should also consider using other ways to call your external function because file handling for 500k cases should be very slow.
Hope that helps,
Eli

Related

How can I make a saving code faster? -MatLab

I'm running a short code to open one by one a list of files and saving back only one of the variables contained in the files. The process seems to me much slower than I expected and getting slower with time, I don't fully understand why and how I could make it run faster. I always struggle with optimization. I'd appreciate if you have suggestions.
The code is the following (the ... substitute the actual path just for example):
main_dir=dir(strcat('\\storage2-...\Raw\DAQ5\'));
filename={};
for m=7:size(main_dir,1)
m
second_dir=dir([main_dir(m).folder '\' main_dir(m).name '\*.mat']);
for mm=1:numel(second_dir)
filename{end+1}=[second_dir(mm).folder '\' second_dir(mm).name];
for mmm=1:numel(filename)
namefile=sprintf(second_dir(mm,1).name);
load(string(filename(1,mmm)));
save(['\\storage2-...\DAQ5\Ch1_',namefile(end-18:end-4),'.mat'], 'Ch_1_y')
end
end
end
The original file is about 17 MB and once the single variable is saved it is about 6 MB in size.
The Matlab load function takes an optional additional argument to specify just a selected variable to read from the input file.
s = load('path/to/file.mat', 'Ch_1_y');
That way you don't have to spend time loading in all the other variables from those input .mat files that you're just going to immediately throw away.
And using save to save MAT-files over SMB shares can be slow. You might want to call save to write it to a temporary local file first, and then copy the completed file to the final destination. Sounds like more I/O, but it can actually be a net win, depending on your particular system and network. Measure it both ways to see if it's a win in your particular situation.

How do i get all lines of my MatLab (.m) files to run?

I have a .m file, that when i run it manually (meaning already in MatLab, and then type the three lines) it runs just like it should.
What im trying to do is put those three lines into a m file and have it run instead of me having to type the lines once im in MatLab
This is the m file im trying to run its called "ABOVE2019_TF01_MatLabCommands_Test.m"
These are the three lines in it:
in_dir_list = {'/j078_8/58667_TF01_G11','/j078_8/58667_TF01_G09',};
out_dir_list = {'/j078_8/58667_TF01_G11','/j078_8/58667_TF01_G09',};
resid_process_GPS(in_dir_list,out_dir_list);
(again running those three lines within MatLab works exactly like I would expect)
So i try to run the ABOVE2019_TF01_MatLabCommands_Test.m file like this
/Applications/MATLAB_R2017a.app/bin/matlab -r "cd /volumes/promiseraid9/workspace/colleen/NewResiduals/j078_8; try, run('ABOVE2019_TF01_MatLabCommands_Test.m'); end; quit"
It doesn't error or anything it just exits out of MatLab instead of running the third line
If instead i changed the third line of the m file to just print out what in_dir_list it. The above command will print that out no problem. But the problem comes with the third line. For whatever reason the above code will not run the third line. What am i doing wrong?
You are using a try statement without catching or handling any exceptions that occur. Due to this, any errors which occur inside your script ABOVE2019_TF01_MatLabCommands_Test.m would not be returned to the command line.
You can verify this by running the following code from the command line:
try; asdfasdfalwelknwerewr_THIS_LINE_SHOULD_ERROR; end;
You should use a catch statement to handle any exceptions returned.
If you are running this from outside of the MATLAB desktop environment (which maintains a path to which to search for functions), are you sure that your functions are located within the search path? As in, is resid_process_GPS located within the folder named /j078_8?

Matlab parfor in different script file "exist" error

My code runs ok if all parfor code is in the same script file, but since the code is huge and I want to select parallel or serial mode execution, I separated it in a different script file, as fallow:
if (useParFor)
myParforCode.m
else
serialCode.m
end
The problem is that Matlab gives me this error:
"Using exist to check for a variable in the transparent workspace is
not supported."
But if I copy all the code in myParforCode.m and put it after the if statement instead of calling the script, the code runs. I thought I could divide my code in scripts without problems, but it seams it not like that.
What are the limitations here, what I'm doing wrong?
My code is huge but I'll try to create a running code sample and add it here.

Running MATLAB system command in background with stdout

I'm using MATLAB and calling an .exe via the system command.
[status,cmdout] = system(command_s);
where command_s is a command string that is formatted earlier in my script to pass all the desired options to the .exe. The .exe would normally write to a .csv file via the > redirection operator in Windows/DOS. Instead, this output is going to cmdout where I use it later in the MATLAB script. It is working correctly and as expected. I'm doing it this way so that the process just uses memory and does not write a very large file to the disk, which would then have to be read from the disk and then deleted after I'm done with it. In the end, it saves a .mat file that's usually in hundreds of KB instead of 10s/100s of MBs as the .csv file would be (some unneeded data is thrown out in the end).
The issue I'm having is since I'm dealing with large files, the executable can take a significant amount of time. I typically have to wait about 2 minutes after executing this command. In the meantime, I have no feedback to know it is progressing and that my system hasn't froze. I know I could add the & symbol to the end of my string, command_s, and run MATLAB code while this is running in the background (or asynchronously as some would say), but that brings up an external window AND makes cmdout empty - so I cannot use the output - forcing me to sit there for 2 minutes wondering each time it executes.
Is there any way to run in the background AND get the stdout from the command?
Maybe you could try system(command_s,'-echo')?

Mistake when use abaqus subroutine to read file with multiple processors(cpus)

I got a mistake when I use abaqus subroutine to read file with multiple processors(cpus),could you help me to deal with this mistake.thanks a lot
I want to read variables from a file ,when one cpu is used,everything is ok,
but when more than one cpus are used,there will be a mistake,it seems that every cpu repeat the same command.
for example,the following is the contents of the file to read from,file name is data.dat
*matID ,2,1
131000.000, 8880.000, 8180.000
0.324, 0.324, 0.300
3990.000, 5320.000, 5320.000
1871.000, 59.700, 59.700
1291.000, 215.000, 215.000
90.000, 102.000, 102.000
my subroutine is shown as follow:
character*12 check1
integer check2,error
OPEN(10,file='data.dat',status='old',iostat=error)
if (error.EQ.0) then
read(10,*,iostat=error) check1,Nm
end if
close(10)
print *,'Nm=',nm,error
print *,'**'
when I use 2 cpus,the printed results will be :
Nm= 2 0
Nm= 8880 0
**
**
Depending on the reason for reading in data from a file, there are a couple of ways to avoid this problem:
If you only need to access the data once:
Read in the data in a subroutine that is always called in serial. UEXTERNALDB is a good example and can be used so that the file open only happens at the beginning of an analysis or the beginning of an increment as needed. You can then carefully store the information in common blocks. Reading from a common block in parallel should work fine, but do not write to them from the parallel subroutines.
Another way to get in a smaller amount of data is to define solution variables in your input file instead.
If you really need to open this file locally within each parallel thread (can't see why but open to correction), you can use GETNUMCPUS and GETRANK to open different copies of the files within each thread. GETRANK returns an integer giving you the rank/id of the process. I would advise against this method though. If your problem is large enough to warrant using parallel, then you should avoid slowing it down with file reads.
For more info see sections 1.1.31 and 2.1.4 of the Abaqus 6.14 docs.